littlefs-rust-core 0.1.0

Port of LittleFS to unsafe rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Utility functions. Per lfs_util.h static inline and lfs.c small type-level utils.

use crate::types::{lfs_block_t, lfs_size_t};

/// Per lfs_util.h lfs_max (lines 129-131)
///
/// C:
/// ```c
/// static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
///     return (a > b) ? a : b;
/// }
/// ```
#[inline(always)]
pub fn lfs_max(a: u32, b: u32) -> u32 {
    if a > b {
        a
    } else {
        b
    }
}

/// Per lfs_util.h lfs_min (lines 133-135)
///
/// C:
/// ```c
/// static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
///     return (a < b) ? a : b;
/// }
/// ```
#[inline(always)]
pub fn lfs_min(a: u32, b: u32) -> u32 {
    if a < b {
        a
    } else {
        b
    }
}

/// Per lfs_util.h lfs_aligndown (lines 138-140)
///
/// C:
/// ```c
/// static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
///     return a - (a % alignment);
/// }
/// ```
#[inline(always)]
pub fn lfs_aligndown(a: u32, alignment: u32) -> u32 {
    a - (a % alignment)
}

/// Per lfs_util.h lfs_alignup (lines 142-144)
///
/// C:
/// ```c
/// static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
///     return lfs_aligndown(a + alignment-1, alignment);
/// }
/// ```
#[inline(always)]
pub fn lfs_alignup(a: u32, alignment: u32) -> u32 {
    lfs_aligndown(a + alignment - 1, alignment)
}

/// Per lfs_util.h lfs_npw2 (lines 147-161) - smallest power of 2 >= a
///
/// C (fallback when LFS_NO_INTRINSICS):
/// ```c
/// static inline uint32_t lfs_npw2(uint32_t a) {
///     uint32_t r = 0;
///     uint32_t s;
///     a -= 1;
///     s = (a > 0xffff) << 4; a >>= s; r |= s;
///     s = (a > 0xff  ) << 3; a >>= s; r |= s;
///     s = (a > 0xf   ) << 2; a >>= s; r |= s;
///     s = (a > 0x3   ) << 1; a >>= s; r |= s;
///     return (r | (a >> 1)) + 1;
/// }
/// ```
#[inline(always)]
pub fn lfs_npw2(a: u32) -> u32 {
    let a = a.wrapping_sub(1);
    let s4 = if a > 0xffff { 1 } else { 0 };
    let a = a >> (s4 << 4);
    let s3 = if a > 0xff { 1 } else { 0 };
    let a = a >> (s3 << 3);
    let s2 = if a > 0xf { 1 } else { 0 };
    let a = a >> (s2 << 2);
    let s1 = if a > 0x3 { 1 } else { 0 };
    let a = a >> (s1 << 1);
    (s4 << 4 | s3 << 3 | s2 << 2 | s1 << 1 | (a >> 1)) + 1
}

/// Per lfs_util.h lfs_ctz (lines 164-170) - trailing zeros, lfs_ctz(0) may be undefined
///
/// C (fallback when LFS_NO_INTRINSICS):
/// ```c
/// static inline uint32_t lfs_ctz(uint32_t a) {
///     return lfs_npw2((a & -a) + 1) - 1;
/// }
/// ```
#[inline(always)]
pub fn lfs_ctz(a: u32) -> u32 {
    lfs_npw2((a & a.wrapping_neg()).wrapping_add(1)) - 1
}

/// Per lfs_util.h lfs_popc (lines 173-182) - population count
///
/// C (fallback when LFS_NO_INTRINSICS):
/// ```c
/// static inline uint32_t lfs_popc(uint32_t a) {
///     a = a - ((a >> 1) & 0x55555555);
///     a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
///     return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
/// }
/// ```
#[inline(always)]
pub fn lfs_popc(a: u32) -> u32 {
    let a = a - ((a >> 1) & 0x5555_5555);
    let a = (a & 0x3333_3333) + ((a >> 2) & 0x3333_3333);
    (((a.wrapping_add(a >> 4)) & 0x0f0f_0f0f).wrapping_mul(0x0101_0101)) >> 24
}

/// Per lfs_util.h lfs_scmp (lines 186-188) - sequence comparison
///
/// C:
/// ```c
/// static inline int lfs_scmp(uint32_t a, uint32_t b) {
///     return (int)(unsigned)(a - b);
/// }
/// ```
#[inline(always)]
pub fn lfs_scmp(a: u32, b: u32) -> i32 {
    (a.wrapping_sub(b)) as i32
}

/// Per lfs_util.h lfs_fromle32 (lines 191-204) - little-endian to native
///
/// C:
/// ```c
/// static inline uint32_t lfs_fromle32(uint32_t a) {
///     // LE host: return a; BE: bswap or byte shuffle
///     return a;  // Rust uses u32::from_le
/// }
/// ```
#[inline(always)]
pub fn lfs_fromle32(a: u32) -> u32 {
    u32::from_le(a)
}

/// Per lfs_util.h lfs_tole32 (lines 206-208)
///
/// C:
/// ```c
/// static inline uint32_t lfs_tole32(uint32_t a) {
///     return lfs_fromle32(a);
/// }
/// ```
#[inline(always)]
pub fn lfs_tole32(a: u32) -> u32 {
    a.to_le()
}

/// Per lfs_util.h lfs_frombe32 (lines 211-226) - big-endian to native
///
/// C:
/// ```c
/// static inline uint32_t lfs_frombe32(uint32_t a) {
///     // platform-dependent; Rust uses u32::from_be
///     return a;
/// }
/// ```
#[inline(always)]
pub fn lfs_frombe32(a: u32) -> u32 {
    u32::from_be(a)
}

/// Per lfs_util.h lfs_tobe32 (lines 228-230)
///
/// C:
/// ```c
/// static inline uint32_t lfs_tobe32(uint32_t a) {
///     return lfs_frombe32(a);
/// }
/// ```
#[inline(always)]
pub fn lfs_tobe32(a: u32) -> u32 {
    a.to_be()
}

// --- lfs.c path operations ---

/// Per C strspn: count leading bytes equal to `c`, stop at first unequal or null.
#[inline(always)]
pub fn lfs_strspn(p: *const u8, c: u8) -> u32 {
    if p.is_null() {
        return 0;
    }
    let mut n: u32 = 0;
    unsafe {
        let mut q = p;
        #[cfg(feature = "loop_limits")]
        const MAX_STRSPN_ITER: u32 = 4096;
        #[cfg(feature = "loop_limits")]
        let mut iter: u32 = 0;
        while *q == c {
            #[cfg(feature = "loop_limits")]
            {
                if iter >= MAX_STRSPN_ITER {
                    panic!(
                        "loop_limits: MAX_STRSPN_ITER ({}) exceeded",
                        MAX_STRSPN_ITER
                    );
                }
                iter += 1;
            }
            n += 1;
            q = q.add(1);
        }
    }
    n
}

/// Per C strcspn: count bytes until we hit `c` or null.
#[inline(always)]
pub fn lfs_strcspn(p: *const u8, c: u8) -> u32 {
    if p.is_null() {
        return 0;
    }
    let mut n: u32 = 0;
    unsafe {
        let mut q = p;
        #[cfg(feature = "loop_limits")]
        const MAX_STRCSPN_ITER: u32 = 4096;
        #[cfg(feature = "loop_limits")]
        let mut iter: u32 = 0;
        while *q != c && *q != 0 {
            #[cfg(feature = "loop_limits")]
            {
                if iter >= MAX_STRCSPN_ITER {
                    panic!(
                        "loop_limits: MAX_STRCSPN_ITER ({}) exceeded",
                        MAX_STRCSPN_ITER
                    );
                }
                iter += 1;
            }
            n += 1;
            q = q.add(1);
        }
    }
    n
}

/// Per C: slice from NUL-terminated string. Max 256 bytes.
#[inline(always)]
pub fn lfs_path_slice_from_cstr(p: *const u8) -> &'static [u8] {
    if p.is_null() {
        return &[];
    }
    unsafe {
        let mut len = 0;
        while len < 256 && *p.add(len) != 0 {
            len += 1;
        }
        core::slice::from_raw_parts(p, len)
    }
}

/// Per lfs.c lfs_path_namelen (lines 289-291)
///
/// C:
/// ```c
/// static inline lfs_size_t lfs_path_namelen(const char *path) {
///     return strcspn(path, "/");
/// }
/// ```
#[inline(always)]
pub fn lfs_path_namelen(path: &[u8]) -> u32 {
    path.iter().position(|&b| b == b'/').unwrap_or(path.len()) as lfs_size_t
}

/// Per lfs.c lfs_path_islast (lines 293-296)
///
/// C:
/// ```c
/// static inline bool lfs_path_islast(const char *path) {
///     lfs_size_t namelen = lfs_path_namelen(path);
///     return path[namelen + strspn(path + namelen, "/")] == '\0';
/// }
/// ```
#[inline(always)]
pub fn lfs_path_islast(path: &[u8]) -> bool {
    let namelen = lfs_path_namelen(path) as usize;
    let rest = path.get(namelen..).unwrap_or(&[]);
    let skip = rest.iter().take_while(|&&b| b == b'/').count();
    path.get(namelen + skip).is_none_or(|&b| b == 0)
}

/// Per lfs.c lfs_path_isdir (lines 298-300)
///
/// C:
/// ```c
/// static inline bool lfs_path_isdir(const char *path) {
///     return path[lfs_path_namelen(path)] != '\0';
/// }
/// ```
#[inline(always)]
pub fn lfs_path_isdir(path: &[u8]) -> bool {
    let namelen = lfs_path_namelen(path) as usize;
    path.get(namelen).is_some_and(|&b| b != 0)
}

/// Per lfs.c lfs_pair_fromle32 (lines 326-329)
///
/// C:
/// ```c
/// static inline void lfs_pair_fromle32(lfs_block_t pair[2]) {
///     pair[0] = lfs_fromle32(pair[0]);
///     pair[1] = lfs_fromle32(pair[1]);
/// }
/// ```
#[inline(always)]
pub fn lfs_pair_fromle32(pair: &mut [lfs_block_t; 2]) {
    pair[0] = lfs_fromle32(pair[0]);
    pair[1] = lfs_fromle32(pair[1]);
}

/// Per lfs.c lfs_pair_tole32 (lines 333-336)
///
/// C:
/// ```c
/// static inline void lfs_pair_tole32(lfs_block_t pair[2]) {
///     pair[0] = lfs_tole32(pair[0]);
///     pair[1] = lfs_tole32(pair[1]);
/// }
/// ```
#[inline(always)]
pub fn lfs_pair_tole32(pair: &mut [lfs_block_t; 2]) {
    pair[0] = lfs_tole32(pair[0]);
    pair[1] = lfs_tole32(pair[1]);
}

/// Per lfs.c lfs_pair_swap (lines 302-306)
///
/// C:
/// ```c
/// static inline void lfs_pair_swap(lfs_block_t pair[2]) {
///     lfs_block_t t = pair[0];
///     pair[0] = pair[1];
///     pair[1] = t;
/// }
/// ```
#[inline(always)]
pub fn lfs_pair_swap(pair: &mut [lfs_block_t; 2]) {
    pair.swap(0, 1);
}

/// Per lfs.c lfs_pair_isnull (lines 308-310)
///
/// C:
/// ```c
/// static inline bool lfs_pair_isnull(const lfs_block_t pair[2]) {
///     return pair[0] == LFS_BLOCK_NULL || pair[1] == LFS_BLOCK_NULL;
/// }
/// ```
#[inline(always)]
pub fn lfs_pair_isnull(pair: &[lfs_block_t; 2]) -> bool {
    use crate::types::LFS_BLOCK_NULL;
    pair[0] == LFS_BLOCK_NULL || pair[1] == LFS_BLOCK_NULL
}

/// Per lfs.c lfs_pair_cmp (lines 312-317) - returns 0 if equal
///
/// C:
/// ```c
/// static inline int lfs_pair_cmp(const lfs_block_t paira[2], const lfs_block_t pairb[2]) {
///     return !(paira[0] == pairb[0] || paira[1] == pairb[1] ||
///              paira[0] == pairb[1] || paira[1] == pairb[0]);
/// }
/// ```
#[inline(always)]
pub fn lfs_pair_cmp(paira: &[lfs_block_t; 2], pairb: &[lfs_block_t; 2]) -> i32 {
    let eq = paira[0] == pairb[0]
        || paira[1] == pairb[1]
        || paira[0] == pairb[1]
        || paira[1] == pairb[0];
    if eq {
        0
    } else {
        1
    }
}

/// Per lfs.c lfs_pair_issync (lines 319-324)
///
/// C:
/// ```c
/// static inline bool lfs_pair_issync(const lfs_block_t paira[2], const lfs_block_t pairb[2]) {
///     return (paira[0] == pairb[0] && paira[1] == pairb[1]) ||
///            (paira[0] == pairb[1] && paira[1] == pairb[0]);
/// }
/// ```
#[inline(always)]
pub fn lfs_pair_issync(paira: &[lfs_block_t; 2], pairb: &[lfs_block_t; 2]) -> bool {
    (paira[0] == pairb[0] && paira[1] == pairb[1]) || (paira[0] == pairb[1] && paira[1] == pairb[0])
}