little_fs/
stubs.rs

1use core::{
2    ffi::{c_char, c_void},
3    ptr::null_mut,
4    slice,
5};
6
7#[unsafe(no_mangle)]
8pub extern "C" fn lfs_crc(mut crc: u32, data: *const c_void, size: usize) -> u32 {
9    const RTABLE: [u32; 16] = [
10        0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158,
11        0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4,
12        0xa00ae278, 0xbdbdf21c,
13    ];
14
15    if data.is_null() {
16        return crc;
17    }
18
19    let data = unsafe { slice::from_raw_parts(data as *const u8, size) };
20
21    for &byte in data {
22        let byte = byte as u32;
23        crc = (crc >> 4) ^ RTABLE[((crc ^ byte) & 0xf) as usize];
24        crc = (crc >> 4) ^ RTABLE[((crc ^ (byte >> 4)) & 0xf) as usize];
25    }
26
27    crc
28}
29
30#[unsafe(no_mangle)]
31pub extern "C" fn strcpy(destination: *mut c_char, source: *const c_char) -> *mut c_char {
32    if destination.is_null() || source.is_null() {
33        return destination;
34    }
35
36    let mut dst_ptr = destination;
37    let mut src_ptr = source;
38
39    unsafe {
40        // Copy characters until null terminator
41        while *src_ptr != 0 {
42            *dst_ptr = *src_ptr;
43            dst_ptr = dst_ptr.add(1);
44            src_ptr = src_ptr.add(1);
45        }
46
47        // Null-terminate the destination string
48        *dst_ptr = 0;
49    }
50
51    destination
52}
53
54/// Allocates memory of the given size.
55///
56/// # Safety
57///
58/// The caller must ensure that the allocated memory is properly freed
59/// using `lfs_free` to avoid memory leaks.
60#[unsafe(no_mangle)]
61pub unsafe extern "C" fn lfs_malloc(size: usize) -> *mut c_void {
62    unsafe {
63        abi_declarations::xila_memory_allocate(
64            null_mut(),
65            size,
66            1,
67            abi_declarations::XILA_MEMORY_CAPABILITIES_NONE,
68        )
69    }
70}
71
72/// Frees memory allocated with lfs_malloc
73///
74/// # Safety
75///
76/// The caller must ensure that the pointer was allocated with lfs_malloc
77/// and has not already been freed.
78#[unsafe(no_mangle)]
79pub unsafe extern "C" fn lfs_free(p: *mut c_void) {
80    unsafe {
81        abi_declarations::xila_memory_deallocate(p);
82    }
83}