Path: blob/master/Utilities/cmliblzma/liblzma/check/crc32_small.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file crc32_small.c5/// \brief CRC32 calculation (size-optimized)6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "check.h"121314uint32_t lzma_crc32_table[1][256];151617#ifdef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR18__attribute__((__constructor__))19#endif20static void21crc32_init(void)22{23static const uint32_t poly32 = UINT32_C(0xEDB88320);2425for (size_t b = 0; b < 256; ++b) {26uint32_t r = b;27for (size_t i = 0; i < 8; ++i) {28if (r & 1)29r = (r >> 1) ^ poly32;30else31r >>= 1;32}3334lzma_crc32_table[0][b] = r;35}3637return;38}394041#ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR42extern void43lzma_crc32_init(void)44{45mythread_once(crc32_init);46return;47}48#endif495051extern LZMA_API(uint32_t)52lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)53{54#ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR55lzma_crc32_init();56#endif5758crc = ~crc;5960while (size != 0) {61crc = lzma_crc32_table[0][*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);62--size;63}6465return ~crc;66}676869