]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc32_small.c
Add a separate internal function to initialize the CRC32
[icculus/xz.git] / src / liblzma / check / crc32_small.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       crc32_small.c
4 /// \brief      CRC32 calculation (size-optimized)
5 //
6 //  This code has been put into the public domain.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #include "check.h"
15
16
17 uint32_t lzma_crc32_table[1][256];
18
19
20 static void
21 crc32_init(void)
22 {
23         static const uint32_t poly32 = UINT32_C(0xEDB88320);
24
25         for (size_t b = 0; b < 256; ++b) {
26                 uint32_t r = b;
27                 for (size_t i = 0; i < 8; ++i) {
28                         if (r & 1)
29                                 r = (r >> 1) ^ poly32;
30                         else
31                                 r >>= 1;
32                 }
33
34                 lzma_crc32_table[0][b] = r;
35         }
36
37         return;
38 }
39
40
41 extern void
42 lzma_crc32_init(void)
43 {
44         mythread_once(crc32_init);
45         return;
46 }
47
48
49 extern LZMA_API(uint32_t)
50 lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
51 {
52         lzma_crc32_init();
53
54         crc = ~crc;
55
56         while (size != 0) {
57                 crc = lzma_crc32_table[0][*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
58                 --size;
59         }
60
61         return ~crc;
62 }