Line data Source code
1 : /* SPDX-License-Identifier: Zlib */
2 :
3 : /*
4 : * CRC32 checksum
5 : *
6 : * Copyright (c) 1998-2019 Joergen Ibsen
7 : *
8 : * This software is provided 'as-is', without any express or implied
9 : * warranty. In no event will the authors be held liable for any damages
10 : * arising from the use of this software.
11 : *
12 : * Permission is granted to anyone to use this software for any purpose,
13 : * including commercial applications, and to alter it and redistribute it
14 : * freely, subject to the following restrictions:
15 : *
16 : * 1. The origin of this software must not be misrepresented; you must
17 : * not claim that you wrote the original software. If you use this
18 : * software in a product, an acknowledgment in the product
19 : * documentation would be appreciated but is not required.
20 : *
21 : * 2. Altered source versions must be plainly marked as such, and must
22 : * not be misrepresented as being the original software.
23 : *
24 : * 3. This notice may not be removed or altered from any source
25 : * distribution.
26 : */
27 :
28 : /*
29 : * CRC32 algorithm taken from the zlib source, which is
30 : * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
31 : */
32 :
33 : #include "tinf.h"
34 :
35 : static const unsigned int tinf_crc32tab[16] = {
36 : 0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190,
37 : 0x6B6B51F4, 0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344,
38 : 0xD6D6A3E8, 0xCB61B38C, 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278,
39 : 0xBDBDF21C
40 : };
41 :
42 10 : unsigned int tinf_crc32(const void *data, unsigned int length)
43 : {
44 10 : const unsigned char *buf = (const unsigned char *) data;
45 10 : unsigned int crc = 0xFFFFFFFF;
46 : unsigned int i;
47 :
48 10 : if (length == 0) {
49 0 : return 0;
50 : }
51 :
52 109864 : for (i = 0; i < length; ++i) {
53 109854 : crc ^= buf[i];
54 109854 : crc = tinf_crc32tab[crc & 0x0F] ^ (crc >> 4);
55 109854 : crc = tinf_crc32tab[crc & 0x0F] ^ (crc >> 4);
56 : }
57 :
58 10 : return crc ^ 0xFFFFFFFF;
59 : }
|