diff options
Diffstat (limited to 'crc32.c')
-rw-r--r-- | crc32.c | 31 |
1 files changed, 18 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* crc32.c -- compute the CRC-32 of a data stream | 1 | /* crc32.c -- compute the CRC-32 of a data stream |
2 | * Copyright (C) 1995-2006, 2010 Mark Adler | 2 | * Copyright (C) 1995-2006, 2010, 2011 Mark Adler |
3 | * For conditions of distribution and use, see copyright notice in zlib.h | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | * | 4 | * |
5 | * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster | 5 | * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster |
@@ -17,6 +17,8 @@ | |||
17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should | 17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should |
18 | first call get_crc_table() to initialize the tables before allowing more than | 18 | first call get_crc_table() to initialize the tables before allowing more than |
19 | one thread to use crc32(). | 19 | one thread to use crc32(). |
20 | |||
21 | DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. | ||
20 | */ | 22 | */ |
21 | 23 | ||
22 | #ifdef MAKECRCH | 24 | #ifdef MAKECRCH |
@@ -53,6 +55,7 @@ | |||
53 | 55 | ||
54 | /* Definitions for doing the crc four data bytes at a time. */ | 56 | /* Definitions for doing the crc four data bytes at a time. */ |
55 | #ifdef BYFOUR | 57 | #ifdef BYFOUR |
58 | typedef u4 crc_table_t; | ||
56 | # define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \ | 59 | # define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \ |
57 | (((w)&0xff00)<<8)+(((w)&0xff)<<24)) | 60 | (((w)&0xff00)<<8)+(((w)&0xff)<<24)) |
58 | local unsigned long crc32_little OF((unsigned long, | 61 | local unsigned long crc32_little OF((unsigned long, |
@@ -61,6 +64,7 @@ | |||
61 | const unsigned char FAR *, unsigned)); | 64 | const unsigned char FAR *, unsigned)); |
62 | # define TBLS 8 | 65 | # define TBLS 8 |
63 | #else | 66 | #else |
67 | typedef unsigned long crc_table_t; | ||
64 | # define TBLS 1 | 68 | # define TBLS 1 |
65 | #endif /* BYFOUR */ | 69 | #endif /* BYFOUR */ |
66 | 70 | ||
@@ -68,16 +72,16 @@ | |||
68 | local unsigned long gf2_matrix_times OF((unsigned long *mat, | 72 | local unsigned long gf2_matrix_times OF((unsigned long *mat, |
69 | unsigned long vec)); | 73 | unsigned long vec)); |
70 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); | 74 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); |
71 | local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2); | 75 | local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); |
72 | 76 | ||
73 | 77 | ||
74 | #ifdef DYNAMIC_CRC_TABLE | 78 | #ifdef DYNAMIC_CRC_TABLE |
75 | 79 | ||
76 | local volatile int crc_table_empty = 1; | 80 | local volatile int crc_table_empty = 1; |
77 | local unsigned long FAR crc_table[TBLS][256]; | 81 | local crc_table_t FAR crc_table[TBLS][256]; |
78 | local void make_crc_table OF((void)); | 82 | local void make_crc_table OF((void)); |
79 | #ifdef MAKECRCH | 83 | #ifdef MAKECRCH |
80 | local void write_table OF((FILE *, const unsigned long FAR *)); | 84 | local void write_table OF((FILE *, const crc_table_t FAR *)); |
81 | #endif /* MAKECRCH */ | 85 | #endif /* MAKECRCH */ |
82 | /* | 86 | /* |
83 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: | 87 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
@@ -107,9 +111,9 @@ local void make_crc_table OF((void)); | |||
107 | */ | 111 | */ |
108 | local void make_crc_table() | 112 | local void make_crc_table() |
109 | { | 113 | { |
110 | unsigned long c; | 114 | crc_table_t c; |
111 | int n, k; | 115 | int n, k; |
112 | unsigned long poly; /* polynomial exclusive-or pattern */ | 116 | crc_table_t poly; /* polynomial exclusive-or pattern */ |
113 | /* terms of polynomial defining this crc (except x^32): */ | 117 | /* terms of polynomial defining this crc (except x^32): */ |
114 | static volatile int first = 1; /* flag to limit concurrent making */ | 118 | static volatile int first = 1; /* flag to limit concurrent making */ |
115 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | 119 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; |
@@ -121,13 +125,13 @@ local void make_crc_table() | |||
121 | first = 0; | 125 | first = 0; |
122 | 126 | ||
123 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ | 127 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ |
124 | poly = 0UL; | 128 | poly = 0; |
125 | for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) | 129 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) |
126 | poly |= 1UL << (31 - p[n]); | 130 | poly |= (crc_table_t)1 << (31 - p[n]); |
127 | 131 | ||
128 | /* generate a crc for every 8-bit value */ | 132 | /* generate a crc for every 8-bit value */ |
129 | for (n = 0; n < 256; n++) { | 133 | for (n = 0; n < 256; n++) { |
130 | c = (unsigned long)n; | 134 | c = (crc_table_t)n; |
131 | for (k = 0; k < 8; k++) | 135 | for (k = 0; k < 8; k++) |
132 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; | 136 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; |
133 | crc_table[0][n] = c; | 137 | crc_table[0][n] = c; |
@@ -164,7 +168,7 @@ local void make_crc_table() | |||
164 | if (out == NULL) return; | 168 | if (out == NULL) return; |
165 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); | 169 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); |
166 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); | 170 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); |
167 | fprintf(out, "local const unsigned long FAR "); | 171 | fprintf(out, "local const crc_table_t FAR "); |
168 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); | 172 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); |
169 | write_table(out, crc_table[0]); | 173 | write_table(out, crc_table[0]); |
170 | # ifdef BYFOUR | 174 | # ifdef BYFOUR |
@@ -184,12 +188,13 @@ local void make_crc_table() | |||
184 | #ifdef MAKECRCH | 188 | #ifdef MAKECRCH |
185 | local void write_table(out, table) | 189 | local void write_table(out, table) |
186 | FILE *out; | 190 | FILE *out; |
187 | const unsigned long FAR *table; | 191 | const crc_table_t FAR *table; |
188 | { | 192 | { |
189 | int n; | 193 | int n; |
190 | 194 | ||
191 | for (n = 0; n < 256; n++) | 195 | for (n = 0; n < 256; n++) |
192 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], | 196 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", |
197 | (unsigned long)(table[n]), | ||
193 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); | 198 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); |
194 | } | 199 | } |
195 | #endif /* MAKECRCH */ | 200 | #endif /* MAKECRCH */ |