1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       crc32_small.c
4207753Smm/// \brief      CRC32 calculation (size-optimized)
5207753Smm//
6207753Smm//  Author:     Lasse Collin
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#include "check.h"
14207753Smm
15207753Smm
16207753Smmuint32_t lzma_crc32_table[1][256];
17207753Smm
18207753Smm
19207753Smmstatic void
20207753Smmcrc32_init(void)
21207753Smm{
22207753Smm	static const uint32_t poly32 = UINT32_C(0xEDB88320);
23207753Smm
24207753Smm	for (size_t b = 0; b < 256; ++b) {
25207753Smm		uint32_t r = b;
26207753Smm		for (size_t i = 0; i < 8; ++i) {
27207753Smm			if (r & 1)
28207753Smm				r = (r >> 1) ^ poly32;
29207753Smm			else
30207753Smm				r >>= 1;
31207753Smm		}
32207753Smm
33207753Smm		lzma_crc32_table[0][b] = r;
34207753Smm	}
35207753Smm
36207753Smm	return;
37207753Smm}
38207753Smm
39207753Smm
40207753Smmextern void
41207753Smmlzma_crc32_init(void)
42207753Smm{
43207753Smm	mythread_once(crc32_init);
44207753Smm	return;
45207753Smm}
46207753Smm
47207753Smm
48207753Smmextern LZMA_API(uint32_t)
49207753Smmlzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
50207753Smm{
51207753Smm	lzma_crc32_init();
52207753Smm
53207753Smm	crc = ~crc;
54207753Smm
55207753Smm	while (size != 0) {
56207753Smm		crc = lzma_crc32_table[0][*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
57207753Smm		--size;
58207753Smm	}
59207753Smm
60207753Smm	return ~crc;
61207753Smm}
62