Skip to content

Commit

Permalink
Changed lfs_crc to match more common API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
geky committed Oct 17, 2018
1 parent 3b3981e commit 10f45ac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
22 changes: 11 additions & 11 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static int lfs_cache_crc(lfs_t *lfs,
return err;
}

lfs_crc(crc, &c, 1);
*crc = lfs_crc(*crc, &c, 1);
}

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

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

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

if (!(tag & 0x80000000)) {
// from memory
lfs_crc(&commit->crc, buffer, size);
commit->crc = lfs_crc(commit->crc, buffer, size);
err = lfs_bd_prog(lfs, commit->block, commit->off, buffer, size);
if (err) {
return err;
Expand All @@ -588,7 +588,7 @@ static int lfs_commit_attr(lfs_t *lfs, struct lfs_commit *commit,
return err;
}

lfs_crc(&commit->crc, &dat, 1);
commit->crc = lfs_crc(commit->crc, &dat, 1);
err = lfs_bd_prog(lfs, commit->block, commit->off+i, &dat, 1);
if (err) {
return err;
Expand Down Expand Up @@ -714,7 +714,7 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {
// write out crc
uint32_t footer[2];
footer[0] = lfs_tole32(tag ^ commit->ptag);
lfs_crc(&commit->crc, &footer[0], sizeof(footer[0]));
commit->crc = lfs_crc(commit->crc, &footer[0], sizeof(footer[0]));
footer[1] = lfs_tole32(commit->crc);
err = lfs_bd_prog(lfs, commit->block, commit->off, footer, sizeof(footer));
if (err) {
Expand All @@ -731,7 +731,7 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {

// successful commit, check checksum to make sure
uint32_t crc = 0xffffffff;
err = lfs_bd_crc(lfs, commit->block, commit->begin,
err = lfs_bd_crc32(lfs, commit->block, commit->begin,
commit->off-lfs_tag_size(tag)-commit->begin, &crc);
if (err) {
return err;
Expand Down Expand Up @@ -810,7 +810,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
uint32_t crc = 0xffffffff;

dir->rev = lfs_tole32(rev[0]);
lfs_crc(&crc, &dir->rev, sizeof(dir->rev));
crc = lfs_crc(crc, &dir->rev, sizeof(dir->rev));
dir->rev = lfs_fromle32(dir->rev);
dir->off = 0;

Expand All @@ -835,7 +835,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
return err;
}

lfs_crc(&crc, &tag, sizeof(tag));
crc = lfs_crc(crc, &tag, sizeof(tag));
tag = lfs_fromle32(tag) ^ ptag;

// next commit not yet programmed
Expand Down Expand Up @@ -879,7 +879,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
dir->locals = templocals;
crc = 0xffffffff;
} else {
err = lfs_bd_crc(lfs, dir->pair[0],
err = lfs_bd_crc32(lfs, dir->pair[0],
off+sizeof(tag), lfs_tag_size(tag), &crc);
if (err) {
if (err == LFS_ERR_CORRUPT) {
Expand Down Expand Up @@ -1062,7 +1062,7 @@ static int lfs_dir_compact(lfs_t *lfs,
// write out header
uint32_t crc = 0xffffffff;
uint32_t rev = lfs_tole32(dir->rev);
lfs_crc(&crc, &rev, sizeof(rev));
crc = lfs_crc(crc, &rev, sizeof(rev));
err = lfs_bd_prog(lfs, dir->pair[1], 0, &rev, sizeof(rev));
if (err) {
if (err == LFS_ERR_CORRUPT) {
Expand Down
8 changes: 5 additions & 3 deletions lfs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


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

for (size_t i = 0; i < size; i++) {
*crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 0)) & 0xf];
*crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 4)) & 0xf];
crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 0)) & 0xf];
crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 4)) & 0xf];
}

return crc;
}


Expand Down
2 changes: 1 addition & 1 deletion lfs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
}

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

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

0 comments on commit 10f45ac

Please sign in to comment.