diff options
Diffstat (limited to 'crc32.c')
-rw-r--r-- | crc32.c | 69 |
1 files changed, 38 insertions, 31 deletions
@@ -7,57 +7,35 @@ | |||
7 | 7 | ||
8 | #include "zlib.h" | 8 | #include "zlib.h" |
9 | 9 | ||
10 | extern uLong crc_table[]; /* crc table, defined below */ | 10 | #define local static |
11 | |||
12 | #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); | ||
13 | #define DO2(buf) DO1(buf); DO1(buf); | ||
14 | #define DO4(buf) DO2(buf); DO2(buf); | ||
15 | #define DO8(buf) DO4(buf); DO4(buf); | ||
16 | |||
17 | /* ========================================================================= */ | ||
18 | uLong crc32(crc, buf, len) | ||
19 | uLong crc; | ||
20 | Byte *buf; | ||
21 | uInt len; | ||
22 | { | ||
23 | if (buf == Z_NULL) return 0L; | ||
24 | crc = crc ^ 0xffffffffL; | ||
25 | while (len >= 8) | ||
26 | { | ||
27 | DO8(buf); | ||
28 | len -= 8; | ||
29 | } | ||
30 | if (len) do { | ||
31 | DO1(buf); | ||
32 | } while (--len); | ||
33 | return crc ^ 0xffffffffL; | ||
34 | } | ||
35 | 11 | ||
12 | #ifdef DYNAMIC_CRC_TABLE | ||
36 | /* ========================================================================= | 13 | /* ========================================================================= |
37 | * Make the crc table. This function is needed only if you want to compute | 14 | * Make the crc table. This function is needed only if you want to compute |
38 | * the table dynamically. | 15 | * the table dynamically. |
39 | */ | 16 | */ |
40 | #ifdef DYNAMIC_CRC_TABLE | 17 | local int crc_table_empty = 1; |
18 | local uLong crc_table[256]; | ||
41 | 19 | ||
42 | local void make_crc_table() | 20 | local void make_crc_table() |
43 | { | 21 | { |
44 | uLong c; | 22 | uLong c; |
45 | int n, k; | 23 | int n, k; |
46 | 24 | ||
47 | for (n = 0; n < 256; n++) | 25 | for (n = 0; n < 256; n++) |
48 | { | 26 | { |
49 | c = (uLong)n; | 27 | c = (uLong)n; |
50 | for (k = 0; k < 8; k++) | 28 | for (k = 0; k < 8; k++) |
51 | c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1; | 29 | c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1; |
52 | crc_table[n] = c; | 30 | crc_table[n] = c; |
53 | } | 31 | } |
32 | crc_table_empty = 0; | ||
54 | } | 33 | } |
55 | #endif | 34 | #else |
56 | |||
57 | /* ======================================================================== | 35 | /* ======================================================================== |
58 | * Table of CRC-32's of all single-byte values (made by make_crc_table) | 36 | * Table of CRC-32's of all single-byte values (made by make_crc_table) |
59 | */ | 37 | */ |
60 | uLong crc_table[] = { | 38 | local uLong crc_table[] = { |
61 | 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, | 39 | 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, |
62 | 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, | 40 | 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, |
63 | 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, | 41 | 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, |
@@ -111,3 +89,32 @@ uLong crc_table[] = { | |||
111 | 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, | 89 | 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, |
112 | 0x2d02ef8dL | 90 | 0x2d02ef8dL |
113 | }; | 91 | }; |
92 | #endif | ||
93 | |||
94 | #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); | ||
95 | #define DO2(buf) DO1(buf); DO1(buf); | ||
96 | #define DO4(buf) DO2(buf); DO2(buf); | ||
97 | #define DO8(buf) DO4(buf); DO4(buf); | ||
98 | |||
99 | /* ========================================================================= */ | ||
100 | uLong crc32(crc, buf, len) | ||
101 | uLong crc; | ||
102 | Byte *buf; | ||
103 | uInt len; | ||
104 | { | ||
105 | if (buf == Z_NULL) return 0L; | ||
106 | #ifdef DYNAMIC_CRC_TABLE | ||
107 | if (crc_table_empty) | ||
108 | make_crc_table(); | ||
109 | #endif | ||
110 | crc = crc ^ 0xffffffffL; | ||
111 | while (len >= 8) | ||
112 | { | ||
113 | DO8(buf); | ||
114 | len -= 8; | ||
115 | } | ||
116 | if (len) do { | ||
117 | DO1(buf); | ||
118 | } while (--len); | ||
119 | return crc ^ 0xffffffffL; | ||
120 | } | ||