53 lines
1.7 KiB
C
53 lines
1.7 KiB
C
/**
|
|
* @file crc32.c
|
|
*
|
|
* Copyright (c) 2021 Semidrive Semiconductor.
|
|
* All rights reserved.
|
|
*
|
|
* Description:compute the CRC-32 of a data stream
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "crc32_data.h"
|
|
|
|
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
|
|
#define DO8 \
|
|
DO1; \
|
|
DO1; \
|
|
DO1; \
|
|
DO1; \
|
|
DO1; \
|
|
DO1; \
|
|
DO1; \
|
|
DO1
|
|
|
|
/**
|
|
* @brief Calculate the CRC-32 checksum for a given data buffer.This function
|
|
* uses a predefined CRC-32 table to compute the CRC-32 checksum of the input
|
|
* data buffer.
|
|
*
|
|
* @param crc Initial CRC value, typically 0.
|
|
* @param buf Pointer to the unsigned character data buffer for which the CRC-32
|
|
* is to be calculated.
|
|
* @param len Length of the data buffer in bytes.
|
|
* @return The computed CRC-32 checksum.
|
|
*/
|
|
unsigned long crc32(unsigned long crc, const unsigned char *buf,
|
|
unsigned int len)
|
|
{
|
|
if (buf == NULL)
|
|
return 0UL;
|
|
|
|
crc = crc ^ 0xffffffffUL;
|
|
while (len >= 8) {
|
|
DO8;
|
|
len -= 8;
|
|
}
|
|
if (len)
|
|
do {
|
|
DO1;
|
|
} while (--len);
|
|
return crc ^ 0xffffffffUL;
|
|
}
|