nuttx/include/nuttx/crc32.h
hanzhijian 87d84e437a 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>
2026-07-03 10:18:28 +08:00

148 lines
5 KiB
C

/****************************************************************************
* include/nuttx/crc32.h
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_CRC32_H
#define __INCLUDE_NUTTX_CRC32_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/types.h>
#include <stdint.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Append full suffix to avoid the penitential symbol collision */
#define crc32 crc32full
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: crc32part
*
* Description:
* Continue CRC calculation on a part of the buffer.
*
****************************************************************************/
uint32_t crc32part(FAR const uint8_t *src, size_t len, uint32_t crc32val);
/****************************************************************************
* Name: crc32
*
* Description:
* Return a 32-bit CRC of the contents of the 'src' buffer, length 'len'
*
****************************************************************************/
uint32_t crc32(FAR const uint8_t *src, size_t len);
/****************************************************************************
* Name: crc32h04c11db7_part
*
* Description:
* Continue CRC calculation on a part of the buffer
* using the 0x04c11db7 polynomial.
*
****************************************************************************/
uint32_t crc32h04c11db7_part(FAR const uint8_t *src,
size_t len, uint32_t crc32val);
/****************************************************************************
* Name: crc32h04c11db7
*
* Description:
* Return a 32-bit CRC of the contents of the 'src' buffer, length 'len'
* using the 0x04c11db7 polynomial.
*
****************************************************************************/
uint32_t crc32h04c11db7(FAR const uint8_t *src, size_t len);
/****************************************************************************
* Name: crc32hf4acfb13_part
*
* Description:
* Continue CRC calculation on a part of the buffer
* using the 0xF4ACFB13 polynomial.
*
****************************************************************************/
uint32_t crc32hf4acfb13_part(FAR const uint8_t *src,
size_t len, uint32_t crc32val);
/****************************************************************************
* Name: crc32hf4acfb13
*
* Description:
* Return a 32-bit CRC of the contents of the 'src' buffer, length 'len'
* using the 0xF4ACFB13 polynomial.
*
****************************************************************************/
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
}
#endif
#endif /* __INCLUDE_NUTTX_CRC32_H */