1
1
/* crc32.c -- compute the CRC-32 of a data stream
2
- * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
2
+ * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 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
30
30
31
31
#include "zutil.h" /* for STDC and FAR definitions */
32
32
33
- #define local static
34
-
35
33
/* Definitions for doing the crc four data bytes at a time. */
36
34
#if !defined(NOBYFOUR ) && defined(Z_U4 )
37
35
# define BYFOUR
38
36
#endif
39
37
#ifdef BYFOUR
40
38
local unsigned long crc32_little OF ((unsigned long,
41
- const unsigned char FAR * , unsigned ) );
39
+ const unsigned char FAR * , z_size_t ));
42
40
local unsigned long crc32_big OF ((unsigned long,
43
- const unsigned char FAR * , unsigned ) );
41
+ const unsigned char FAR * , z_size_t ));
44
42
# define TBLS 8
45
43
#else
46
44
# define TBLS 1
@@ -201,10 +199,10 @@ const z_crc_t FAR * ZEXPORT get_crc_table()
201
199
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
202
200
203
201
/* ========================================================================= */
204
- unsigned long ZEXPORT crc32 (crc , buf , len )
202
+ unsigned long ZEXPORT crc32_z (crc , buf , len )
205
203
unsigned long crc ;
206
204
const unsigned char FAR * buf ;
207
- uInt len ;
205
+ z_size_t len ;
208
206
{
209
207
if (buf == Z_NULL ) return 0UL ;
210
208
@@ -235,8 +233,29 @@ unsigned long ZEXPORT crc32(crc, buf, len)
235
233
return crc ^ 0xffffffffUL ;
236
234
}
237
235
236
+ /* ========================================================================= */
237
+ unsigned long ZEXPORT crc32 (crc , buf , len )
238
+ unsigned long crc ;
239
+ const unsigned char FAR * buf ;
240
+ uInt len ;
241
+ {
242
+ return crc32_z (crc , buf , len );
243
+ }
244
+
238
245
#ifdef BYFOUR
239
246
247
+ /*
248
+ This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
249
+ integer pointer type. This violates the strict aliasing rule, where a
250
+ compiler can assume, for optimization purposes, that two pointers to
251
+ fundamentally different types won't ever point to the same memory. This can
252
+ manifest as a problem only if one of the pointers is written to. This code
253
+ only reads from those pointers. So long as this code remains isolated in
254
+ this compilation unit, there won't be a problem. For this reason, this code
255
+ should not be copied and pasted into a compilation unit in which other code
256
+ writes to the buffer that is passed to these routines.
257
+ */
258
+
240
259
/* ========================================================================= */
241
260
#define DOLIT4 c ^= *buf4++; \
242
261
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
@@ -247,7 +266,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
247
266
local unsigned long crc32_little (crc , buf , len )
248
267
unsigned long crc ;
249
268
const unsigned char FAR * buf ;
250
- unsigned len ;
269
+ z_size_t len ;
251
270
{
252
271
register z_crc_t c ;
253
272
register const z_crc_t FAR * buf4 ;
@@ -278,7 +297,7 @@ local unsigned long crc32_little(crc, buf, len)
278
297
}
279
298
280
299
/* ========================================================================= */
281
- #define DOBIG4 c ^= *++buf4 ; \
300
+ #define DOBIG4 c ^= *buf4++ ; \
282
301
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
283
302
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
284
303
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
@@ -287,7 +306,7 @@ local unsigned long crc32_little(crc, buf, len)
287
306
local unsigned long crc32_big (crc , buf , len )
288
307
unsigned long crc ;
289
308
const unsigned char FAR * buf ;
290
- unsigned len ;
309
+ z_size_t len ;
291
310
{
292
311
register z_crc_t c ;
293
312
register const z_crc_t FAR * buf4 ;
@@ -300,7 +319,6 @@ local unsigned long crc32_big(crc, buf, len)
300
319
}
301
320
302
321
buf4 = (const z_crc_t FAR * )(const void FAR * )buf ;
303
- buf4 -- ;
304
322
while (len >= 32 ) {
305
323
DOBIG32 ;
306
324
len -= 32 ;
@@ -309,7 +327,6 @@ local unsigned long crc32_big(crc, buf, len)
309
327
DOBIG4 ;
310
328
len -= 4 ;
311
329
}
312
- buf4 ++ ;
313
330
buf = (const unsigned char FAR * )buf4 ;
314
331
315
332
if (len ) do {
0 commit comments