This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 87d84e437aaa4c1e994bab34a20d067ae223de2b
Author: hanzhijian <[email protected]>
AuthorDate: Wed Jul 1 20:25:04 2026 +0800

    libc/crc32: add IEEE-compatible crc32_ieee for Linux/zlib interop
    
    Add crc32_ieee() and crc32_ieeepart() that produce CRC32 values
    compatible with Linux/zlib. The difference from the existing crc32():
    
      crc32():        init=0, no final XOR (NuttX native)
      crc32_ieee():   init=0xFFFFFFFF, final XOR 0xFFFFFFFF (Linux/zlib)
    
    The existing crc32() and crc32part() are unchanged to avoid breaking
    existing callers (bbsram, sbram, etc.).
    
    New functions:
      crc32_ieee(src, len)              - full CRC, Linux-compatible
      crc32_ieeepart(src, len, crcval)  - incremental CRC, Linux-compatible
    
    Verified on sim:nsh against known Linux zlib test vectors:
      crc32_ieee("hello")     = 0x3610a686 (Linux: 0x3610a686) PASS
      crc32_ieee("123456789") = 0xcbf43926 (Linux: 0xcbf43926) PASS
      crc32_ieee("")           = 0x00000000 (Linux: 0x00000000) PASS
      crc32_ieeepart incremental  = 0xcbf43926               PASS
      crc32("hello")          = 0xf032519b (unchanged)       PASS
    
    Signed-off-by: hanzhijian <[email protected]>
---
 include/nuttx/crc32.h      | 24 ++++++++++++++++++++++++
 libs/libc/misc/lib_crc32.c | 30 ++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/include/nuttx/crc32.h b/include/nuttx/crc32.h
index 516f860cda6..82e781f1569 100644
--- a/include/nuttx/crc32.h
+++ b/include/nuttx/crc32.h
@@ -116,6 +116,30 @@ uint32_t crc32hf4acfb13_part(FAR const uint8_t *src,
 
 uint32_t crc32hf4acfb13(FAR const uint8_t *src, size_t len);
 
+/****************************************************************************
+ * Name: crc32_ieeepart
+ *
+ * Description:
+ *   Continue IEEE CRC32 calculation on a part of the buffer.
+ *   Compatible with Linux/zlib crc32(). Uses init value 0xFFFFFFFF and
+ *   final XOR with 0xFFFFFFFF.
+ *
+ ****************************************************************************/
+
+uint32_t crc32_ieeepart(FAR const uint8_t *src,
+                        size_t len, uint32_t crc32val);
+
+/****************************************************************************
+ * Name: crc32_ieee
+ *
+ * Description:
+ *   Return an IEEE-standard 32-bit CRC compatible with Linux/zlib crc32().
+ *   Uses init value 0xFFFFFFFF and final XOR with 0xFFFFFFFF.
+ *
+ ****************************************************************************/
+
+uint32_t crc32_ieee(FAR const uint8_t *src, size_t len);
+
 #undef EXTERN
 #ifdef __cplusplus
 }
diff --git a/libs/libc/misc/lib_crc32.c b/libs/libc/misc/lib_crc32.c
index 15bcc170ccc..e76065d2c02 100644
--- a/libs/libc/misc/lib_crc32.c
+++ b/libs/libc/misc/lib_crc32.c
@@ -180,3 +180,33 @@ uint32_t crc32(FAR const uint8_t *src, size_t len)
 {
   return crc32part(src, len, 0);
 }
+
+/****************************************************************************
+ * Name: crc32_ieeepart
+ *
+ * Description:
+ *   Continue IEEE CRC32 calculation on a part of the buffer.
+ *   Compatible with Linux/zlib crc32(). Uses init value 0xFFFFFFFF and
+ *   final XOR with 0xFFFFFFFF.
+ *
+ ****************************************************************************/
+
+uint32_t crc32_ieeepart(FAR const uint8_t *src,
+                        size_t len, uint32_t crc32val)
+{
+  return crc32part(src, len, crc32val ^ 0xffffffff) ^ 0xffffffff;
+}
+
+/****************************************************************************
+ * Name: crc32_ieee
+ *
+ * Description:
+ *   Return an IEEE-standard 32-bit CRC compatible with Linux/zlib crc32().
+ *   Uses init value 0xFFFFFFFF and final XOR with 0xFFFFFFFF.
+ *
+ ****************************************************************************/
+
+uint32_t crc32_ieee(FAR const uint8_t *src, size_t len)
+{
+  return crc32_ieeepart(src, len, 0);
+}

Reply via email to