Skip to content

Commit 10f45ac

Browse files
committed
Changed lfs_crc to match more common API
In looking at the common CRC APIs out there, this seemed the most common. At least more common than the current modified-in-place pointer API. It also seems to have a slightly better code footprint. I'm blaming pointer optimization issues. One downside is that lfs_crc can't report errors, however it was already assumed that lfs_crc can not error.
1 parent 3b3981e commit 10f45ac

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

lfs.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static int lfs_cache_crc(lfs_t *lfs,
121121
return err;
122122
}
123123

124-
lfs_crc(crc, &c, 1);
124+
*crc = lfs_crc(*crc, &c, 1);
125125
}
126126

127127
return 0;
@@ -236,7 +236,7 @@ static int lfs_bd_cmp(lfs_t *lfs, lfs_block_t block,
236236
return lfs_cache_cmp(lfs, NULL, &lfs->rcache, block, off, buffer, size);
237237
}
238238

239-
static int lfs_bd_crc(lfs_t *lfs, lfs_block_t block,
239+
static int lfs_bd_crc32(lfs_t *lfs, lfs_block_t block,
240240
lfs_off_t off, lfs_size_t size, uint32_t *crc) {
241241
return lfs_cache_crc(lfs, NULL, &lfs->rcache, block, off, size, crc);
242242
}
@@ -562,7 +562,7 @@ static int lfs_commit_attr(lfs_t *lfs, struct lfs_commit *commit,
562562

563563
// write out tag
564564
uint32_t ntag = lfs_tole32((tag & 0x7fffffff) ^ commit->ptag);
565-
lfs_crc(&commit->crc, &ntag, sizeof(ntag));
565+
commit->crc = lfs_crc(commit->crc, &ntag, sizeof(ntag));
566566
int err = lfs_bd_prog(lfs, commit->block, commit->off,
567567
&ntag, sizeof(ntag));
568568
if (err) {
@@ -572,7 +572,7 @@ static int lfs_commit_attr(lfs_t *lfs, struct lfs_commit *commit,
572572

573573
if (!(tag & 0x80000000)) {
574574
// from memory
575-
lfs_crc(&commit->crc, buffer, size);
575+
commit->crc = lfs_crc(commit->crc, buffer, size);
576576
err = lfs_bd_prog(lfs, commit->block, commit->off, buffer, size);
577577
if (err) {
578578
return err;
@@ -588,7 +588,7 @@ static int lfs_commit_attr(lfs_t *lfs, struct lfs_commit *commit,
588588
return err;
589589
}
590590

591-
lfs_crc(&commit->crc, &dat, 1);
591+
commit->crc = lfs_crc(commit->crc, &dat, 1);
592592
err = lfs_bd_prog(lfs, commit->block, commit->off+i, &dat, 1);
593593
if (err) {
594594
return err;
@@ -714,7 +714,7 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {
714714
// write out crc
715715
uint32_t footer[2];
716716
footer[0] = lfs_tole32(tag ^ commit->ptag);
717-
lfs_crc(&commit->crc, &footer[0], sizeof(footer[0]));
717+
commit->crc = lfs_crc(commit->crc, &footer[0], sizeof(footer[0]));
718718
footer[1] = lfs_tole32(commit->crc);
719719
err = lfs_bd_prog(lfs, commit->block, commit->off, footer, sizeof(footer));
720720
if (err) {
@@ -731,7 +731,7 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {
731731

732732
// successful commit, check checksum to make sure
733733
uint32_t crc = 0xffffffff;
734-
err = lfs_bd_crc(lfs, commit->block, commit->begin,
734+
err = lfs_bd_crc32(lfs, commit->block, commit->begin,
735735
commit->off-lfs_tag_size(tag)-commit->begin, &crc);
736736
if (err) {
737737
return err;
@@ -810,7 +810,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
810810
uint32_t crc = 0xffffffff;
811811

812812
dir->rev = lfs_tole32(rev[0]);
813-
lfs_crc(&crc, &dir->rev, sizeof(dir->rev));
813+
crc = lfs_crc(crc, &dir->rev, sizeof(dir->rev));
814814
dir->rev = lfs_fromle32(dir->rev);
815815
dir->off = 0;
816816

@@ -835,7 +835,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
835835
return err;
836836
}
837837

838-
lfs_crc(&crc, &tag, sizeof(tag));
838+
crc = lfs_crc(crc, &tag, sizeof(tag));
839839
tag = lfs_fromle32(tag) ^ ptag;
840840

841841
// next commit not yet programmed
@@ -879,7 +879,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
879879
dir->locals = templocals;
880880
crc = 0xffffffff;
881881
} else {
882-
err = lfs_bd_crc(lfs, dir->pair[0],
882+
err = lfs_bd_crc32(lfs, dir->pair[0],
883883
off+sizeof(tag), lfs_tag_size(tag), &crc);
884884
if (err) {
885885
if (err == LFS_ERR_CORRUPT) {
@@ -1062,7 +1062,7 @@ static int lfs_dir_compact(lfs_t *lfs,
10621062
// write out header
10631063
uint32_t crc = 0xffffffff;
10641064
uint32_t rev = lfs_tole32(dir->rev);
1065-
lfs_crc(&crc, &rev, sizeof(rev));
1065+
crc = lfs_crc(crc, &rev, sizeof(rev));
10661066
err = lfs_bd_prog(lfs, dir->pair[1], 0, &rev, sizeof(rev));
10671067
if (err) {
10681068
if (err == LFS_ERR_CORRUPT) {

lfs_util.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
// Software CRC implementation with small lookup table
14-
void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
14+
uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) {
1515
static const uint32_t rtable[16] = {
1616
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1717
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
@@ -22,9 +22,11 @@ void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
2222
const uint8_t *data = buffer;
2323

2424
for (size_t i = 0; i < size; i++) {
25-
*crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 0)) & 0xf];
26-
*crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 4)) & 0xf];
25+
crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 0)) & 0xf];
26+
crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 4)) & 0xf];
2727
}
28+
29+
return crc;
2830
}
2931

3032

lfs_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
187187
}
188188

189189
// Calculate CRC-32 with polynomial = 0x04c11db7
190-
void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
190+
uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
191191

192192
// Allocate memory, only used if buffers are not provided to littlefs
193193
static inline void *lfs_malloc(size_t size) {

0 commit comments

Comments
 (0)