11/* 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
33 * For conditions of distribution and use, see copyright notice in zlib.h
44 *
55 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
3030
3131#include "zutil.h" /* for STDC and FAR definitions */
3232
33- #define local static
34-
3533/* Definitions for doing the crc four data bytes at a time. */
3634#if !defined(NOBYFOUR ) && defined(Z_U4 )
3735# define BYFOUR
3836#endif
3937#ifdef BYFOUR
4038 local unsigned long crc32_little OF ((unsigned long,
41- const unsigned char FAR * , unsigned ) );
39+ const unsigned char FAR * , z_size_t ));
4240 local unsigned long crc32_big OF ((unsigned long,
43- const unsigned char FAR * , unsigned ) );
41+ const unsigned char FAR * , z_size_t ));
4442# define TBLS 8
4543#else
4644# define TBLS 1
@@ -201,10 +199,10 @@ const z_crc_t FAR * ZEXPORT get_crc_table()
201199#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
202200
203201/* ========================================================================= */
204- unsigned long ZEXPORT crc32 (crc , buf , len )
202+ unsigned long ZEXPORT crc32_z (crc , buf , len )
205203 unsigned long crc ;
206204 const unsigned char FAR * buf ;
207- uInt len ;
205+ z_size_t len ;
208206{
209207 if (buf == Z_NULL ) return 0UL ;
210208
@@ -235,8 +233,29 @@ unsigned long ZEXPORT crc32(crc, buf, len)
235233 return crc ^ 0xffffffffUL ;
236234}
237235
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+
238245#ifdef BYFOUR
239246
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+
240259/* ========================================================================= */
241260#define DOLIT4 c ^= *buf4++; \
242261 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
@@ -247,7 +266,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
247266local unsigned long crc32_little (crc , buf , len )
248267 unsigned long crc ;
249268 const unsigned char FAR * buf ;
250- unsigned len ;
269+ z_size_t len ;
251270{
252271 register z_crc_t c ;
253272 register const z_crc_t FAR * buf4 ;
@@ -278,7 +297,7 @@ local unsigned long crc32_little(crc, buf, len)
278297}
279298
280299/* ========================================================================= */
281- #define DOBIG4 c ^= *++buf4 ; \
300+ #define DOBIG4 c ^= *buf4++ ; \
282301 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
283302 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
284303#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
@@ -287,7 +306,7 @@ local unsigned long crc32_little(crc, buf, len)
287306local unsigned long crc32_big (crc , buf , len )
288307 unsigned long crc ;
289308 const unsigned char FAR * buf ;
290- unsigned len ;
309+ z_size_t len ;
291310{
292311 register z_crc_t c ;
293312 register const z_crc_t FAR * buf4 ;
@@ -300,7 +319,6 @@ local unsigned long crc32_big(crc, buf, len)
300319 }
301320
302321 buf4 = (const z_crc_t FAR * )(const void FAR * )buf ;
303- buf4 -- ;
304322 while (len >= 32 ) {
305323 DOBIG32 ;
306324 len -= 32 ;
@@ -309,7 +327,6 @@ local unsigned long crc32_big(crc, buf, len)
309327 DOBIG4 ;
310328 len -= 4 ;
311329 }
312- buf4 ++ ;
313330 buf = (const unsigned char FAR * )buf4 ;
314331
315332 if (len ) do {
0 commit comments