diff --git a/lfs.c b/lfs.c index 246acc2ea6f..4c914ee4508 100644 --- a/lfs.c +++ b/lfs.c @@ -512,7 +512,7 @@ static int lfs_dir_traverse(lfs_t *lfs, return err; } - ntag = lfs_fromle32(ntag) ^ tag; + ntag = lfs_frombe32(ntag) ^ tag; tag |= 0x80000000; } @@ -617,7 +617,7 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, } crc = lfs_crc(crc, &tag, sizeof(tag)); - tag = lfs_fromle32(tag) ^ ptag; + tag = lfs_frombe32(tag) ^ ptag; // next commit not yet programmed if (!lfs_tag_isvalid(tag)) { @@ -1134,7 +1134,7 @@ static int lfs_commit_attr(lfs_t *lfs, struct lfs_commit *commit, } // write out tag - lfs_tag_t ntag = lfs_tole32((tag & 0x7fffffff) ^ commit->ptag); + lfs_tag_t ntag = lfs_tobe32((tag & 0x7fffffff) ^ commit->ptag); int err = lfs_commit_prog(lfs, commit, &ntag, sizeof(ntag)); if (err) { return err; @@ -1193,13 +1193,13 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit, } // build crc tag - bool reset = ~lfs_fromle32(tag) >> 31; + bool reset = ~lfs_frombe32(tag) >> 31; tag = LFS_MKTAG(LFS_TYPE_CRC + 2*compacting + reset, 0x1ff, off - (commit->off+sizeof(lfs_tag_t))); // write out crc uint32_t footer[2]; - footer[0] = lfs_tole32(tag ^ commit->ptag); + footer[0] = lfs_tobe32(tag ^ commit->ptag); commit->crc = lfs_crc(commit->crc, &footer[0], sizeof(footer[0])); footer[1] = lfs_tole32(commit->crc); err = lfs_bd_prog(lfs, diff --git a/lfs_util.h b/lfs_util.h index 8a65694c1f5..6e2f00206fd 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -186,6 +186,52 @@ static inline uint16_t lfs_tole16(uint16_t a) { return lfs_fromle16(a); } +// Convert between 32-bit big-endian and native order +static inline uint32_t lfs_frombe32(uint32_t a) { +#if !defined(LFS_NO_INTRINSICS) && ( \ + (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \ + (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \ + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) + return __builtin_bswap32(a); +#elif !defined(LFS_NO_INTRINSICS) && ( \ + (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \ + (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \ + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) + return a; +#else + return (((uint8_t*)&a)[0] << 24) | + (((uint8_t*)&a)[1] << 16) | + (((uint8_t*)&a)[2] << 8) | + (((uint8_t*)&a)[3] << 0); +#endif +} + +static inline uint32_t lfs_tobe32(uint32_t a) { + return lfs_frombe32(a); +} + +// Convert between 16-bit big-endian and native order +static inline uint16_t lfs_frombe16(uint16_t a) { +#if !defined(LFS_NO_INTRINSICS) && ( \ + (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \ + (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \ + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) + return __builtin_bswap16(a); +#elif !defined(LFS_NO_INTRINSICS) && ( \ + (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \ + (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \ + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) + return a; +#else + return (((uint8_t*)&a)[0] << 8) | + (((uint8_t*)&a)[1] << 0); +#endif +} + +static inline uint16_t lfs_tobe16(uint16_t a) { + return lfs_frombe16(a); +} + // Calculate CRC-32 with polynomial = 0x04c11db7 uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size); diff --git a/tests/corrupt.py b/tests/corrupt.py index d9943892904..b6671ff6072 100755 --- a/tests/corrupt.py +++ b/tests/corrupt.py @@ -14,7 +14,7 @@ def corrupt(block): tag = 0xffffffff while True: try: - ntag, = struct.unpack('I', file.read(4)) except struct.error: break diff --git a/tests/debug.py b/tests/debug.py index 71f204a7cb6..c76dc3d3284 100755 --- a/tests/debug.py +++ b/tests/debug.py @@ -68,7 +68,7 @@ def main(*blocks): try: data = file.read(4) crc = binascii.crc32(data, crc) - ntag, = struct.unpack('I', data) except struct.error: break