117651Speter/* crc32.c -- compute the CRC-32 of a data stream 2313796Sdelphij * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler 3131377Stjr * For conditions of distribution and use, see copyright notice in zlib.h 4131377Stjr * 5131377Stjr * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster 6131377Stjr * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing 7131377Stjr * tables for updating the shift register in one step with three exclusive-ors 8157043Sdes * instead of four steps with four exclusive-ors. This results in about a 9157043Sdes * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. 1017651Speter */ 1117651Speter 1233904Ssteve/* @(#) $Id$ */ 1317651Speter 14145474Skientzle/* 15145474Skientzle Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore 16145474Skientzle protection on the static variables used to control the first-use generation 17145474Skientzle of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should 18145474Skientzle first call get_crc_table() to initialize the tables before allowing more than 19145474Skientzle one thread to use crc32(). 20230837Sdelphij 21230837Sdelphij DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. 22145474Skientzle */ 23145474Skientzle 24131377Stjr#ifdef MAKECRCH 25131377Stjr# include <stdio.h> 26131377Stjr# ifndef DYNAMIC_CRC_TABLE 27131377Stjr# define DYNAMIC_CRC_TABLE 28131377Stjr# endif /* !DYNAMIC_CRC_TABLE */ 29131377Stjr#endif /* MAKECRCH */ 3017651Speter 31131377Stjr#include "zutil.h" /* for STDC and FAR definitions */ 32131377Stjr 33131377Stjr/* Definitions for doing the crc four data bytes at a time. */ 34237248Sdelphij#if !defined(NOBYFOUR) && defined(Z_U4) 35237248Sdelphij# define BYFOUR 36237248Sdelphij#endif 37131377Stjr#ifdef BYFOUR 38131377Stjr local unsigned long crc32_little OF((unsigned long, 39313796Sdelphij const unsigned char FAR *, z_size_t)); 40131377Stjr local unsigned long crc32_big OF((unsigned long, 41313796Sdelphij const unsigned char FAR *, z_size_t)); 42131377Stjr# define TBLS 8 43131377Stjr#else 44131377Stjr# define TBLS 1 45131377Stjr#endif /* BYFOUR */ 46131377Stjr 47157043Sdes/* Local functions for crc concatenation */ 48157043Sdeslocal unsigned long gf2_matrix_times OF((unsigned long *mat, 49157043Sdes unsigned long vec)); 50157043Sdeslocal void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); 51230837Sdelphijlocal uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); 52157043Sdes 53205194Sdelphij 5417651Speter#ifdef DYNAMIC_CRC_TABLE 5517651Speter 56145474Skientzlelocal volatile int crc_table_empty = 1; 57237248Sdelphijlocal z_crc_t FAR crc_table[TBLS][256]; 5817651Speterlocal void make_crc_table OF((void)); 59131377Stjr#ifdef MAKECRCH 60237248Sdelphij local void write_table OF((FILE *, const z_crc_t FAR *)); 61131377Stjr#endif /* MAKECRCH */ 6217651Speter/* 63131377Stjr Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 6417651Speter x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. 6517651Speter 6617651Speter Polynomials over GF(2) are represented in binary, one bit per coefficient, 6717651Speter with the lowest powers in the most significant bit. Then adding polynomials 6817651Speter is just exclusive-or, and multiplying a polynomial by x is a right shift by 6917651Speter one. If we call the above polynomial p, and represent a byte as the 7017651Speter polynomial q, also with the lowest power in the most significant bit (so the 7117651Speter byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, 7217651Speter where a mod b means the remainder after dividing a by b. 7317651Speter 7417651Speter This calculation is done using the shift-register method of multiplying and 7517651Speter taking the remainder. The register is initialized to zero, and for each 7617651Speter incoming bit, x^32 is added mod p to the register if the bit is a one (where 7717651Speter x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by 7817651Speter x (which is shifting right by one and adding x^32 mod p if the bit shifted 7917651Speter out is a one). We start with the highest power (least significant bit) of 8017651Speter q and repeat for all eight bits of q. 8117651Speter 82131377Stjr The first table is simply the CRC of all possible eight bit values. This is 83131377Stjr all the information needed to generate CRCs on data a byte at a time for all 84131377Stjr combinations of CRC register values and incoming bytes. The remaining tables 85131377Stjr allow for word-at-a-time CRC calculation for both big-endian and little- 86131377Stjr endian machines, where a word is four bytes. 8717651Speter*/ 8817651Speterlocal void make_crc_table() 8917651Speter{ 90237248Sdelphij z_crc_t c; 91131377Stjr int n, k; 92237248Sdelphij z_crc_t poly; /* polynomial exclusive-or pattern */ 93131377Stjr /* terms of polynomial defining this crc (except x^32): */ 94145474Skientzle static volatile int first = 1; /* flag to limit concurrent making */ 95131377Stjr static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 9617651Speter 97145474Skientzle /* See if another task is already doing this (not thread-safe, but better 98145474Skientzle than nothing -- significantly reduces duration of vulnerability in 99145474Skientzle case the advice about DYNAMIC_CRC_TABLE is ignored) */ 100145474Skientzle if (first) { 101145474Skientzle first = 0; 102131377Stjr 103145474Skientzle /* make exclusive-or pattern from polynomial (0xedb88320UL) */ 104230837Sdelphij poly = 0; 105230837Sdelphij for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) 106237248Sdelphij poly |= (z_crc_t)1 << (31 - p[n]); 107131377Stjr 108145474Skientzle /* generate a crc for every 8-bit value */ 109145474Skientzle for (n = 0; n < 256; n++) { 110237248Sdelphij c = (z_crc_t)n; 111145474Skientzle for (k = 0; k < 8; k++) 112145474Skientzle c = c & 1 ? poly ^ (c >> 1) : c >> 1; 113145474Skientzle crc_table[0][n] = c; 114145474Skientzle } 115145474Skientzle 116131377Stjr#ifdef BYFOUR 117145474Skientzle /* generate crc for each value followed by one, two, and three zeros, 118145474Skientzle and then the byte reversal of those as well as the first table */ 119145474Skientzle for (n = 0; n < 256; n++) { 120145474Skientzle c = crc_table[0][n]; 121237248Sdelphij crc_table[4][n] = ZSWAP32(c); 122145474Skientzle for (k = 1; k < 4; k++) { 123145474Skientzle c = crc_table[0][c & 0xff] ^ (c >> 8); 124145474Skientzle crc_table[k][n] = c; 125237248Sdelphij crc_table[k + 4][n] = ZSWAP32(c); 126145474Skientzle } 127131377Stjr } 128131377Stjr#endif /* BYFOUR */ 129131377Stjr 130145474Skientzle crc_table_empty = 0; 131145474Skientzle } 132145474Skientzle else { /* not first */ 133145474Skientzle /* wait for the other guy to finish (not efficient, but rare) */ 134145474Skientzle while (crc_table_empty) 135145474Skientzle ; 136145474Skientzle } 137131377Stjr 138131377Stjr#ifdef MAKECRCH 139131377Stjr /* write out CRC tables to crc32.h */ 140131377Stjr { 141131377Stjr FILE *out; 142131377Stjr 143131377Stjr out = fopen("crc32.h", "w"); 144131377Stjr if (out == NULL) return; 145131377Stjr fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); 146131377Stjr fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); 147237248Sdelphij fprintf(out, "local const z_crc_t FAR "); 148131377Stjr fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); 149131377Stjr write_table(out, crc_table[0]); 150131377Stjr# ifdef BYFOUR 151131377Stjr fprintf(out, "#ifdef BYFOUR\n"); 152131377Stjr for (k = 1; k < 8; k++) { 153131377Stjr fprintf(out, " },\n {\n"); 154131377Stjr write_table(out, crc_table[k]); 155131377Stjr } 156131377Stjr fprintf(out, "#endif\n"); 157131377Stjr# endif /* BYFOUR */ 158131377Stjr fprintf(out, " }\n};\n"); 159131377Stjr fclose(out); 160131377Stjr } 161131377Stjr#endif /* MAKECRCH */ 16217651Speter} 163131377Stjr 164131377Stjr#ifdef MAKECRCH 165131377Stjrlocal void write_table(out, table) 166131377Stjr FILE *out; 167237248Sdelphij const z_crc_t FAR *table; 168131377Stjr{ 169131377Stjr int n; 170131377Stjr 171131377Stjr for (n = 0; n < 256; n++) 172230837Sdelphij fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", 173230837Sdelphij (unsigned long)(table[n]), 174131377Stjr n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); 175131377Stjr} 176131377Stjr#endif /* MAKECRCH */ 177131377Stjr 178131377Stjr#else /* !DYNAMIC_CRC_TABLE */ 17917651Speter/* ======================================================================== 180131377Stjr * Tables of CRC-32s of all single-byte values, made by make_crc_table(). 18117651Speter */ 182131377Stjr#include "crc32.h" 183131377Stjr#endif /* DYNAMIC_CRC_TABLE */ 18417651Speter 18517651Speter/* ========================================================================= 18617651Speter * This function can be used by asm versions of crc32() 18717651Speter */ 188237248Sdelphijconst z_crc_t FAR * ZEXPORT get_crc_table() 18917651Speter{ 19017651Speter#ifdef DYNAMIC_CRC_TABLE 191145474Skientzle if (crc_table_empty) 192145474Skientzle make_crc_table(); 193131377Stjr#endif /* DYNAMIC_CRC_TABLE */ 194237248Sdelphij return (const z_crc_t FAR *)crc_table; 19517651Speter} 19617651Speter 19717651Speter/* ========================================================================= */ 198131377Stjr#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) 199131377Stjr#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 20017651Speter 20117651Speter/* ========================================================================= */ 202313796Sdelphijunsigned long ZEXPORT crc32_z(crc, buf, len) 203131377Stjr unsigned long crc; 204131377Stjr const unsigned char FAR *buf; 205313796Sdelphij z_size_t len; 20617651Speter{ 207131377Stjr if (buf == Z_NULL) return 0UL; 208131377Stjr 20917651Speter#ifdef DYNAMIC_CRC_TABLE 21017651Speter if (crc_table_empty) 211131377Stjr make_crc_table(); 212131377Stjr#endif /* DYNAMIC_CRC_TABLE */ 213131377Stjr 214131377Stjr#ifdef BYFOUR 215131377Stjr if (sizeof(void *) == sizeof(ptrdiff_t)) { 216237248Sdelphij z_crc_t endian; 217131377Stjr 218131377Stjr endian = 1; 219131377Stjr if (*((unsigned char *)(&endian))) 220131377Stjr return crc32_little(crc, buf, len); 221131377Stjr else 222131377Stjr return crc32_big(crc, buf, len); 22317651Speter } 224131377Stjr#endif /* BYFOUR */ 225131377Stjr crc = crc ^ 0xffffffffUL; 226131377Stjr while (len >= 8) { 227131377Stjr DO8; 228131377Stjr len -= 8; 229131377Stjr } 23017651Speter if (len) do { 231131377Stjr DO1; 23217651Speter } while (--len); 233131377Stjr return crc ^ 0xffffffffUL; 23417651Speter} 235131377Stjr 236313796Sdelphij/* ========================================================================= */ 237313796Sdelphijunsigned long ZEXPORT crc32(crc, buf, len) 238313796Sdelphij unsigned long crc; 239313796Sdelphij const unsigned char FAR *buf; 240313796Sdelphij uInt len; 241313796Sdelphij{ 242313796Sdelphij return crc32_z(crc, buf, len); 243313796Sdelphij} 244313796Sdelphij 245131377Stjr#ifdef BYFOUR 246131377Stjr 247313796Sdelphij/* 248313796Sdelphij This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit 249313796Sdelphij integer pointer type. This violates the strict aliasing rule, where a 250313796Sdelphij compiler can assume, for optimization purposes, that two pointers to 251313796Sdelphij fundamentally different types won't ever point to the same memory. This can 252313796Sdelphij manifest as a problem only if one of the pointers is written to. This code 253313796Sdelphij only reads from those pointers. So long as this code remains isolated in 254313796Sdelphij this compilation unit, there won't be a problem. For this reason, this code 255313796Sdelphij should not be copied and pasted into a compilation unit in which other code 256313796Sdelphij writes to the buffer that is passed to these routines. 257313796Sdelphij */ 258313796Sdelphij 259131377Stjr/* ========================================================================= */ 260131377Stjr#define DOLIT4 c ^= *buf4++; \ 261131377Stjr c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ 262131377Stjr crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] 263131377Stjr#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 264131377Stjr 265131377Stjr/* ========================================================================= */ 266131377Stjrlocal unsigned long crc32_little(crc, buf, len) 267131377Stjr unsigned long crc; 268131377Stjr const unsigned char FAR *buf; 269313796Sdelphij z_size_t len; 270131377Stjr{ 271237248Sdelphij register z_crc_t c; 272237248Sdelphij register const z_crc_t FAR *buf4; 273131377Stjr 274237248Sdelphij c = (z_crc_t)crc; 275131377Stjr c = ~c; 276131377Stjr while (len && ((ptrdiff_t)buf & 3)) { 277131377Stjr c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 278131377Stjr len--; 279131377Stjr } 280131377Stjr 281237248Sdelphij buf4 = (const z_crc_t FAR *)(const void FAR *)buf; 282131377Stjr while (len >= 32) { 283131377Stjr DOLIT32; 284131377Stjr len -= 32; 285131377Stjr } 286131377Stjr while (len >= 4) { 287131377Stjr DOLIT4; 288131377Stjr len -= 4; 289131377Stjr } 290131377Stjr buf = (const unsigned char FAR *)buf4; 291131377Stjr 292131377Stjr if (len) do { 293131377Stjr c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 294131377Stjr } while (--len); 295131377Stjr c = ~c; 296131377Stjr return (unsigned long)c; 297131377Stjr} 298131377Stjr 299131377Stjr/* ========================================================================= */ 300313796Sdelphij#define DOBIG4 c ^= *buf4++; \ 301131377Stjr c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ 302131377Stjr crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] 303131377Stjr#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 304131377Stjr 305131377Stjr/* ========================================================================= */ 306131377Stjrlocal unsigned long crc32_big(crc, buf, len) 307131377Stjr unsigned long crc; 308131377Stjr const unsigned char FAR *buf; 309313796Sdelphij z_size_t len; 310131377Stjr{ 311237248Sdelphij register z_crc_t c; 312237248Sdelphij register const z_crc_t FAR *buf4; 313131377Stjr 314237248Sdelphij c = ZSWAP32((z_crc_t)crc); 315131377Stjr c = ~c; 316131377Stjr while (len && ((ptrdiff_t)buf & 3)) { 317131377Stjr c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 318131377Stjr len--; 319131377Stjr } 320131377Stjr 321237248Sdelphij buf4 = (const z_crc_t FAR *)(const void FAR *)buf; 322131377Stjr while (len >= 32) { 323131377Stjr DOBIG32; 324131377Stjr len -= 32; 325131377Stjr } 326131377Stjr while (len >= 4) { 327131377Stjr DOBIG4; 328131377Stjr len -= 4; 329131377Stjr } 330131377Stjr buf = (const unsigned char FAR *)buf4; 331131377Stjr 332131377Stjr if (len) do { 333131377Stjr c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 334131377Stjr } while (--len); 335131377Stjr c = ~c; 336237248Sdelphij return (unsigned long)(ZSWAP32(c)); 337131377Stjr} 338131377Stjr 339131377Stjr#endif /* BYFOUR */ 340157043Sdes 341157043Sdes#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ 342157043Sdes 343157043Sdes/* ========================================================================= */ 344157043Sdeslocal unsigned long gf2_matrix_times(mat, vec) 345157043Sdes unsigned long *mat; 346157043Sdes unsigned long vec; 347157043Sdes{ 348157043Sdes unsigned long sum; 349157043Sdes 350157043Sdes sum = 0; 351157043Sdes while (vec) { 352157043Sdes if (vec & 1) 353157043Sdes sum ^= *mat; 354157043Sdes vec >>= 1; 355157043Sdes mat++; 356157043Sdes } 357157043Sdes return sum; 358157043Sdes} 359157043Sdes 360157043Sdes/* ========================================================================= */ 361157043Sdeslocal void gf2_matrix_square(square, mat) 362157043Sdes unsigned long *square; 363157043Sdes unsigned long *mat; 364157043Sdes{ 365157043Sdes int n; 366157043Sdes 367157043Sdes for (n = 0; n < GF2_DIM; n++) 368157043Sdes square[n] = gf2_matrix_times(mat, mat[n]); 369157043Sdes} 370157043Sdes 371157043Sdes/* ========================================================================= */ 372205194Sdelphijlocal uLong crc32_combine_(crc1, crc2, len2) 373157043Sdes uLong crc1; 374157043Sdes uLong crc2; 375205194Sdelphij z_off64_t len2; 376157043Sdes{ 377157043Sdes int n; 378157043Sdes unsigned long row; 379157043Sdes unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ 380157043Sdes unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ 381157043Sdes 382205194Sdelphij /* degenerate case (also disallow negative lengths) */ 383205194Sdelphij if (len2 <= 0) 384157043Sdes return crc1; 385157043Sdes 386157043Sdes /* put operator for one zero bit in odd */ 387205194Sdelphij odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ 388157043Sdes row = 1; 389157043Sdes for (n = 1; n < GF2_DIM; n++) { 390157043Sdes odd[n] = row; 391157043Sdes row <<= 1; 392157043Sdes } 393157043Sdes 394157043Sdes /* put operator for two zero bits in even */ 395157043Sdes gf2_matrix_square(even, odd); 396157043Sdes 397157043Sdes /* put operator for four zero bits in odd */ 398157043Sdes gf2_matrix_square(odd, even); 399157043Sdes 400157043Sdes /* apply len2 zeros to crc1 (first square will put the operator for one 401157043Sdes zero byte, eight zero bits, in even) */ 402157043Sdes do { 403157043Sdes /* apply zeros operator for this bit of len2 */ 404157043Sdes gf2_matrix_square(even, odd); 405157043Sdes if (len2 & 1) 406157043Sdes crc1 = gf2_matrix_times(even, crc1); 407157043Sdes len2 >>= 1; 408157043Sdes 409157043Sdes /* if no more bits set, then done */ 410157043Sdes if (len2 == 0) 411157043Sdes break; 412157043Sdes 413157043Sdes /* another iteration of the loop with odd and even swapped */ 414157043Sdes gf2_matrix_square(odd, even); 415157043Sdes if (len2 & 1) 416157043Sdes crc1 = gf2_matrix_times(odd, crc1); 417157043Sdes len2 >>= 1; 418157043Sdes 419157043Sdes /* if no more bits set, then done */ 420157043Sdes } while (len2 != 0); 421157043Sdes 422157043Sdes /* return combined crc */ 423157043Sdes crc1 ^= crc2; 424157043Sdes return crc1; 425157043Sdes} 426205194Sdelphij 427205194Sdelphij/* ========================================================================= */ 428205194SdelphijuLong ZEXPORT crc32_combine(crc1, crc2, len2) 429205194Sdelphij uLong crc1; 430205194Sdelphij uLong crc2; 431205194Sdelphij z_off_t len2; 432205194Sdelphij{ 433205194Sdelphij return crc32_combine_(crc1, crc2, len2); 434205194Sdelphij} 435205194Sdelphij 436205194SdelphijuLong ZEXPORT crc32_combine64(crc1, crc2, len2) 437205194Sdelphij uLong crc1; 438205194Sdelphij uLong crc2; 439205194Sdelphij z_off64_t len2; 440205194Sdelphij{ 441205194Sdelphij return crc32_combine_(crc1, crc2, len2); 442205194Sdelphij} 443