1
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, 2012 Mark Adler
3
3
* For conditions of distribution and use, see copyright notice in zlib.h
4
4
*
5
5
* Thanks to Rodney Brown <[email protected] > for his contribution of faster
17
17
of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
18
18
first call get_crc_table() to initialize the tables before allowing more than
19
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
24
#ifdef MAKECRCH
30
32
31
33
#define local static
32
34
33
- /* Find a four-byte integer type for crc32_little() and crc32_big(). */
34
- #ifndef NOBYFOUR
35
- # ifdef STDC /* need ANSI C limits.h to determine sizes */
36
- # include <limits.h>
37
- # define BYFOUR
38
- # if (UINT_MAX == 0xffffffffUL )
39
- typedef unsigned int u4 ;
40
- # else
41
- # if (ULONG_MAX == 0xffffffffUL )
42
- typedef unsigned long u4 ;
43
- # else
44
- # if (USHRT_MAX == 0xffffffffUL )
45
- typedef unsigned short u4 ;
46
- # else
47
- # undef BYFOUR /* can't find a four-byte integer type! */
48
- # endif
49
- # endif
50
- # endif
51
- # endif /* STDC */
52
- #endif /* !NOBYFOUR */
53
-
54
35
/* Definitions for doing the crc four data bytes at a time. */
36
+ #if !defined(NOBYFOUR ) && defined(Z_U4 )
37
+ # define BYFOUR
38
+ #endif
55
39
#ifdef BYFOUR
56
- # define REV (w ) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
57
- (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58
40
local unsigned long crc32_little OF ((unsigned long,
59
41
const unsigned char FAR * , unsigned ) );
60
42
local unsigned long crc32_big OF ((unsigned long,
68
50
local unsigned long gf2_matrix_times OF ((unsigned long * mat ,
69
51
unsigned long vec ));
70
52
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 );
53
+ local uLong crc32_combine_ OF ( ( uLong crc1 , uLong crc2 , z_off64_t len2 ) );
72
54
73
55
74
56
#ifdef DYNAMIC_CRC_TABLE
75
57
76
58
local volatile int crc_table_empty = 1 ;
77
- local unsigned long FAR crc_table [TBLS ][256 ];
59
+ local z_crc_t FAR crc_table [TBLS ][256 ];
78
60
local void make_crc_table OF ( (void ));
79
61
#ifdef MAKECRCH
80
- local void write_table OF ( (FILE * , const unsigned long FAR * ));
62
+ local void write_table OF ((FILE * , const z_crc_t FAR * ));
81
63
#endif /* MAKECRCH */
82
64
/*
83
65
Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
@@ -107,9 +89,9 @@ local void make_crc_table OF((void));
107
89
*/
108
90
local void make_crc_table ()
109
91
{
110
- unsigned long c ;
92
+ z_crc_t c ;
111
93
int n , k ;
112
- unsigned long poly ; /* polynomial exclusive-or pattern */
94
+ z_crc_t poly ; /* polynomial exclusive-or pattern */
113
95
/* terms of polynomial defining this crc (except x^32): */
114
96
static volatile int first = 1 ; /* flag to limit concurrent making */
115
97
static const unsigned char p [] = {0 ,1 ,2 ,4 ,5 ,7 ,8 ,10 ,11 ,12 ,16 ,22 ,23 ,26 };
@@ -121,13 +103,13 @@ local void make_crc_table()
121
103
first = 0 ;
122
104
123
105
/* make exclusive-or pattern from polynomial (0xedb88320UL) */
124
- poly = 0UL ;
125
- for (n = 0 ; n < sizeof (p )/sizeof (unsigned char ); n ++ )
126
- poly |= 1UL << (31 - p [n ]);
106
+ poly = 0 ;
107
+ for (n = 0 ; n < ( int )( sizeof (p )/sizeof (unsigned char ) ); n ++ )
108
+ poly |= ( z_crc_t ) 1 << (31 - p [n ]);
127
109
128
110
/* generate a crc for every 8-bit value */
129
111
for (n = 0 ; n < 256 ; n ++ ) {
130
- c = (unsigned long )n ;
112
+ c = (z_crc_t )n ;
131
113
for (k = 0 ; k < 8 ; k ++ )
132
114
c = c & 1 ? poly ^ (c >> 1 ) : c >> 1 ;
133
115
crc_table [0 ][n ] = c ;
@@ -138,11 +120,11 @@ local void make_crc_table()
138
120
and then the byte reversal of those as well as the first table */
139
121
for (n = 0 ; n < 256 ; n ++ ) {
140
122
c = crc_table [0 ][n ];
141
- crc_table [4 ][n ] = REV (c );
123
+ crc_table [4 ][n ] = ZSWAP32 (c );
142
124
for (k = 1 ; k < 4 ; k ++ ) {
143
125
c = crc_table [0 ][c & 0xff ] ^ (c >> 8 );
144
126
crc_table [k ][n ] = c ;
145
- crc_table [k + 4 ][n ] = REV (c );
127
+ crc_table [k + 4 ][n ] = ZSWAP32 (c );
146
128
}
147
129
}
148
130
#endif /* BYFOUR */
@@ -164,7 +146,7 @@ local void make_crc_table()
164
146
if (out == NULL ) return ;
165
147
fprintf (out , "/* crc32.h -- tables for rapid CRC calculation\n" );
166
148
fprintf (out , " * Generated automatically by crc32.c\n */\n\n" );
167
- fprintf (out , "local const unsigned long FAR " );
149
+ fprintf (out , "local const z_crc_t FAR " );
168
150
fprintf (out , "crc_table[TBLS][256] =\n{\n {\n" );
169
151
write_table (out , crc_table [0 ]);
170
152
# ifdef BYFOUR
@@ -184,12 +166,13 @@ local void make_crc_table()
184
166
#ifdef MAKECRCH
185
167
local void write_table (out , table )
186
168
FILE * out ;
187
- const unsigned long FAR * table ;
169
+ const z_crc_t FAR * table ;
188
170
{
189
171
int n ;
190
172
191
173
for (n = 0 ; n < 256 ; n ++ )
192
- fprintf (out , "%s0x%08lxUL%s" , n % 5 ? "" : " " , table [n ],
174
+ fprintf (out , "%s0x%08lxUL%s" , n % 5 ? "" : " " ,
175
+ (unsigned long )(table [n ]),
193
176
n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", " ));
194
177
}
195
178
#endif /* MAKECRCH */
@@ -204,13 +187,13 @@ local void write_table(out, table)
204
187
/* =========================================================================
205
188
* This function can be used by asm versions of crc32()
206
189
*/
207
- const unsigned long FAR * ZEXPORT get_crc_table ()
190
+ const z_crc_t FAR * ZEXPORT get_crc_table ()
208
191
{
209
192
#ifdef DYNAMIC_CRC_TABLE
210
193
if (crc_table_empty )
211
194
make_crc_table ();
212
195
#endif /* DYNAMIC_CRC_TABLE */
213
- return (const unsigned long FAR * )crc_table ;
196
+ return (const z_crc_t FAR * )crc_table ;
214
197
}
215
198
216
199
/* ========================================================================= */
@@ -232,7 +215,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
232
215
233
216
#ifdef BYFOUR
234
217
if (sizeof (void * ) == sizeof (ptrdiff_t )) {
235
- u4 endian ;
218
+ z_crc_t endian ;
236
219
237
220
endian = 1 ;
238
221
if (* ((unsigned char * )(& endian )))
@@ -266,17 +249,17 @@ local unsigned long crc32_little(crc, buf, len)
266
249
const unsigned char FAR * buf ;
267
250
unsigned len ;
268
251
{
269
- register u4 c ;
270
- register const u4 FAR * buf4 ;
252
+ register z_crc_t c ;
253
+ register const z_crc_t FAR * buf4 ;
271
254
272
- c = (u4 )crc ;
255
+ c = (z_crc_t )crc ;
273
256
c = ~c ;
274
257
while (len && ((ptrdiff_t )buf & 3 )) {
275
258
c = crc_table [0 ][(c ^ * buf ++ ) & 0xff ] ^ (c >> 8 );
276
259
len -- ;
277
260
}
278
261
279
- buf4 = (const u4 FAR * )(const void FAR * )buf ;
262
+ buf4 = (const z_crc_t FAR * )(const void FAR * )buf ;
280
263
while (len >= 32 ) {
281
264
DOLIT32 ;
282
265
len -= 32 ;
@@ -306,17 +289,17 @@ local unsigned long crc32_big(crc, buf, len)
306
289
const unsigned char FAR * buf ;
307
290
unsigned len ;
308
291
{
309
- register u4 c ;
310
- register const u4 FAR * buf4 ;
292
+ register z_crc_t c ;
293
+ register const z_crc_t FAR * buf4 ;
311
294
312
- c = REV (( u4 )crc );
295
+ c = ZSWAP32 (( z_crc_t )crc );
313
296
c = ~c ;
314
297
while (len && ((ptrdiff_t )buf & 3 )) {
315
298
c = crc_table [4 ][(c >> 24 ) ^ * buf ++ ] ^ (c << 8 );
316
299
len -- ;
317
300
}
318
301
319
- buf4 = (const u4 FAR * )(const void FAR * )buf ;
302
+ buf4 = (const z_crc_t FAR * )(const void FAR * )buf ;
320
303
buf4 -- ;
321
304
while (len >= 32 ) {
322
305
DOBIG32 ;
@@ -333,7 +316,7 @@ local unsigned long crc32_big(crc, buf, len)
333
316
c = crc_table [4 ][(c >> 24 ) ^ * buf ++ ] ^ (c << 8 );
334
317
} while (-- len );
335
318
c = ~c ;
336
- return (unsigned long )(REV (c ));
319
+ return (unsigned long )(ZSWAP32 (c ));
337
320
}
338
321
339
322
#endif /* BYFOUR */
0 commit comments