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 <hanzhijian@zepp.com>
This commit is contained in:
hanzhijian 2026-07-01 20:25:04 +08:00 committed by Xiang Xiao
parent f0c94bb64a
commit 87d84e437a
2 changed files with 54 additions and 0 deletions

View file

@ -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
}

View file

@ -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);
}