|
version 1.1, 2012/02/17 15:09:30
|
version 1.1.1.3, 2021/03/17 00:32:36
|
|
Line 1
|
Line 1
|
| /* crc32.c -- compute the CRC-32 of a data stream |
/* crc32.c -- compute the CRC-32 of a data stream |
| * Copyright (C) 1995-2005 Mark Adler | * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler |
| * For conditions of distribution and use, see copyright notice in zlib.h |
* For conditions of distribution and use, see copyright notice in zlib.h |
| * |
* |
| * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster |
* Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster |
|
Line 17
|
Line 17
|
| of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should |
of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should |
| first call get_crc_table() to initialize the tables before allowing more than |
first call get_crc_table() to initialize the tables before allowing more than |
| one thread to use crc32(). |
one thread to use crc32(). |
| |
|
| |
DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. |
| */ |
*/ |
| |
|
| #ifdef MAKECRCH |
#ifdef MAKECRCH |
|
Line 30
|
Line 32
|
| |
|
| #define local static |
#define local static |
| |
|
| /* Find a four-byte integer type for crc32_little() and crc32_big(). */ |
|
| #ifndef NOBYFOUR |
|
| # ifdef STDC /* need ANSI C limits.h to determine sizes */ |
|
| # include <limits.h> |
|
| # define BYFOUR |
|
| # if (UINT_MAX == 0xffffffffUL) |
|
| typedef unsigned int u4; |
|
| # else |
|
| # if (ULONG_MAX == 0xffffffffUL) |
|
| typedef unsigned long u4; |
|
| # else |
|
| # if (USHRT_MAX == 0xffffffffUL) |
|
| typedef unsigned short u4; |
|
| # else |
|
| # undef BYFOUR /* can't find a four-byte integer type! */ |
|
| # endif |
|
| # endif |
|
| # endif |
|
| # endif /* STDC */ |
|
| #endif /* !NOBYFOUR */ |
|
| |
|
| /* Definitions for doing the crc four data bytes at a time. */ |
/* Definitions for doing the crc four data bytes at a time. */ |
| |
#if !defined(NOBYFOUR) && defined(Z_U4) |
| |
# define BYFOUR |
| |
#endif |
| #ifdef BYFOUR |
#ifdef BYFOUR |
| # define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ |
|
| (((w)&0xff00)<<8)+(((w)&0xff)<<24)) |
|
| local unsigned long crc32_little OF((unsigned long, |
local unsigned long crc32_little OF((unsigned long, |
| const unsigned char FAR *, unsigned)); |
const unsigned char FAR *, unsigned)); |
| local unsigned long crc32_big OF((unsigned long, |
local unsigned long crc32_big OF((unsigned long, |
|
Line 68
|
Line 50
|
| local unsigned long gf2_matrix_times OF((unsigned long *mat, |
local unsigned long gf2_matrix_times OF((unsigned long *mat, |
| unsigned long vec)); |
unsigned long vec)); |
| local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); |
local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); |
| |
local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); |
| |
|
| |
|
| #ifdef DYNAMIC_CRC_TABLE |
#ifdef DYNAMIC_CRC_TABLE |
| |
|
| local volatile int crc_table_empty = 1; |
local volatile int crc_table_empty = 1; |
| local unsigned long FAR crc_table[TBLS][256]; | local z_crc_t FAR crc_table[TBLS][256]; |
| local void make_crc_table OF((void)); |
local void make_crc_table OF((void)); |
| #ifdef MAKECRCH |
#ifdef MAKECRCH |
| local void write_table OF((FILE *, const unsigned long FAR *)); | local void write_table OF((FILE *, const z_crc_t FAR *)); |
| #endif /* MAKECRCH */ |
#endif /* MAKECRCH */ |
| /* |
/* |
| Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
|
Line 105 local void make_crc_table OF((void));
|
Line 89 local void make_crc_table OF((void));
|
| */ |
*/ |
| local void make_crc_table() |
local void make_crc_table() |
| { |
{ |
| unsigned long c; | z_crc_t c; |
| int n, k; |
int n, k; |
| unsigned long poly; /* polynomial exclusive-or pattern */ | z_crc_t poly; /* polynomial exclusive-or pattern */ |
| /* terms of polynomial defining this crc (except x^32): */ |
/* terms of polynomial defining this crc (except x^32): */ |
| static volatile int first = 1; /* flag to limit concurrent making */ |
static volatile int first = 1; /* flag to limit concurrent making */ |
| static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; |
static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; |
|
Line 119 local void make_crc_table()
|
Line 103 local void make_crc_table()
|
| first = 0; |
first = 0; |
| |
|
| /* make exclusive-or pattern from polynomial (0xedb88320UL) */ |
/* make exclusive-or pattern from polynomial (0xedb88320UL) */ |
| poly = 0UL; | poly = 0; |
| for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) |
| poly |= 1UL << (31 - p[n]); | poly |= (z_crc_t)1 << (31 - p[n]); |
| |
|
| /* generate a crc for every 8-bit value */ |
/* generate a crc for every 8-bit value */ |
| for (n = 0; n < 256; n++) { |
for (n = 0; n < 256; n++) { |
| c = (unsigned long)n; | c = (z_crc_t)n; |
| for (k = 0; k < 8; k++) |
for (k = 0; k < 8; k++) |
| c = c & 1 ? poly ^ (c >> 1) : c >> 1; |
c = c & 1 ? poly ^ (c >> 1) : c >> 1; |
| crc_table[0][n] = c; |
crc_table[0][n] = c; |
|
Line 136 local void make_crc_table()
|
Line 120 local void make_crc_table()
|
| and then the byte reversal of those as well as the first table */ |
and then the byte reversal of those as well as the first table */ |
| for (n = 0; n < 256; n++) { |
for (n = 0; n < 256; n++) { |
| c = crc_table[0][n]; |
c = crc_table[0][n]; |
| crc_table[4][n] = REV(c); | crc_table[4][n] = ZSWAP32(c); |
| for (k = 1; k < 4; k++) { |
for (k = 1; k < 4; k++) { |
| c = crc_table[0][c & 0xff] ^ (c >> 8); |
c = crc_table[0][c & 0xff] ^ (c >> 8); |
| crc_table[k][n] = c; |
crc_table[k][n] = c; |
| crc_table[k + 4][n] = REV(c); | crc_table[k + 4][n] = ZSWAP32(c); |
| } |
} |
| } |
} |
| #endif /* BYFOUR */ |
#endif /* BYFOUR */ |
|
Line 162 local void make_crc_table()
|
Line 146 local void make_crc_table()
|
| if (out == NULL) return; |
if (out == NULL) return; |
| fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); |
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); |
| fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); |
fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); |
| fprintf(out, "local const unsigned long FAR "); | fprintf(out, "local const z_crc_t FAR "); |
| fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); |
fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); |
| write_table(out, crc_table[0]); |
write_table(out, crc_table[0]); |
| # ifdef BYFOUR |
# ifdef BYFOUR |
|
Line 182 local void make_crc_table()
|
Line 166 local void make_crc_table()
|
| #ifdef MAKECRCH |
#ifdef MAKECRCH |
| local void write_table(out, table) |
local void write_table(out, table) |
| FILE *out; |
FILE *out; |
| const unsigned long FAR *table; | const z_crc_t FAR *table; |
| { |
{ |
| int n; |
int n; |
| |
|
| for (n = 0; n < 256; n++) |
for (n = 0; n < 256; n++) |
| fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", |
| | (unsigned long)(table[n]), |
| n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); |
n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); |
| } |
} |
| #endif /* MAKECRCH */ |
#endif /* MAKECRCH */ |
|
Line 202 local void write_table(out, table)
|
Line 187 local void write_table(out, table)
|
| /* ========================================================================= |
/* ========================================================================= |
| * This function can be used by asm versions of crc32() |
* This function can be used by asm versions of crc32() |
| */ |
*/ |
| const unsigned long FAR * ZEXPORT get_crc_table() | const z_crc_t FAR * ZEXPORT get_crc_table() |
| { |
{ |
| #ifdef DYNAMIC_CRC_TABLE |
#ifdef DYNAMIC_CRC_TABLE |
| if (crc_table_empty) |
if (crc_table_empty) |
| make_crc_table(); |
make_crc_table(); |
| #endif /* DYNAMIC_CRC_TABLE */ |
#endif /* DYNAMIC_CRC_TABLE */ |
| return (const unsigned long FAR *)crc_table; | return (const z_crc_t FAR *)crc_table; |
| } |
} |
| |
|
| /* ========================================================================= */ |
/* ========================================================================= */ |
|
Line 219 const unsigned long FAR * ZEXPORT get_crc_table()
|
Line 204 const unsigned long FAR * ZEXPORT get_crc_table()
|
| unsigned long ZEXPORT crc32(crc, buf, len) |
unsigned long ZEXPORT crc32(crc, buf, len) |
| unsigned long crc; |
unsigned long crc; |
| const unsigned char FAR *buf; |
const unsigned char FAR *buf; |
| unsigned len; | uInt len; |
| { |
{ |
| if (buf == Z_NULL) return 0UL; |
if (buf == Z_NULL) return 0UL; |
| |
|
|
Line 230 unsigned long ZEXPORT crc32(crc, buf, len)
|
Line 215 unsigned long ZEXPORT crc32(crc, buf, len)
|
| |
|
| #ifdef BYFOUR |
#ifdef BYFOUR |
| if (sizeof(void *) == sizeof(ptrdiff_t)) { |
if (sizeof(void *) == sizeof(ptrdiff_t)) { |
| u4 endian; | z_crc_t endian; |
| |
|
| endian = 1; |
endian = 1; |
| if (*((unsigned char *)(&endian))) |
if (*((unsigned char *)(&endian))) |
|
Line 264 local unsigned long crc32_little(crc, buf, len)
|
Line 249 local unsigned long crc32_little(crc, buf, len)
|
| const unsigned char FAR *buf; |
const unsigned char FAR *buf; |
| unsigned len; |
unsigned len; |
| { |
{ |
| register u4 c; | register z_crc_t c; |
| register const u4 FAR *buf4; | register const z_crc_t FAR *buf4; |
| |
|
| c = (u4)crc; | c = (z_crc_t)crc; |
| c = ~c; |
c = ~c; |
| while (len && ((ptrdiff_t)buf & 3)) { |
while (len && ((ptrdiff_t)buf & 3)) { |
| c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); |
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); |
| len--; |
len--; |
| } |
} |
| |
|
| buf4 = (const u4 FAR *)(const void FAR *)buf; | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; |
| while (len >= 32) { |
while (len >= 32) { |
| DOLIT32; |
DOLIT32; |
| len -= 32; |
len -= 32; |
|
Line 293 local unsigned long crc32_little(crc, buf, len)
|
Line 278 local unsigned long crc32_little(crc, buf, len)
|
| } |
} |
| |
|
| /* ========================================================================= */ |
/* ========================================================================= */ |
| #define DOBIG4 c ^= *++buf4; \ | #define DOBIG4 c ^= *buf4++; \ |
| c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ |
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ |
| crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] |
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] |
| #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 |
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 |
|
Line 304 local unsigned long crc32_big(crc, buf, len)
|
Line 289 local unsigned long crc32_big(crc, buf, len)
|
| const unsigned char FAR *buf; |
const unsigned char FAR *buf; |
| unsigned len; |
unsigned len; |
| { |
{ |
| register u4 c; | register z_crc_t c; |
| register const u4 FAR *buf4; | register const z_crc_t FAR *buf4; |
| |
|
| c = REV((u4)crc); | c = ZSWAP32((z_crc_t)crc); |
| c = ~c; |
c = ~c; |
| while (len && ((ptrdiff_t)buf & 3)) { |
while (len && ((ptrdiff_t)buf & 3)) { |
| c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
| len--; |
len--; |
| } |
} |
| |
|
| buf4 = (const u4 FAR *)(const void FAR *)buf; | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; |
| buf4--; | |
| while (len >= 32) { |
while (len >= 32) { |
| DOBIG32; |
DOBIG32; |
| len -= 32; |
len -= 32; |
|
Line 324 local unsigned long crc32_big(crc, buf, len)
|
Line 308 local unsigned long crc32_big(crc, buf, len)
|
| DOBIG4; |
DOBIG4; |
| len -= 4; |
len -= 4; |
| } |
} |
| buf4++; |
|
| buf = (const unsigned char FAR *)buf4; |
buf = (const unsigned char FAR *)buf4; |
| |
|
| if (len) do { |
if (len) do { |
| c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
| } while (--len); |
} while (--len); |
| c = ~c; |
c = ~c; |
| return (unsigned long)(REV(c)); | return (unsigned long)(ZSWAP32(c)); |
| } |
} |
| |
|
| #endif /* BYFOUR */ |
#endif /* BYFOUR */ |
|
Line 367 local void gf2_matrix_square(square, mat)
|
Line 350 local void gf2_matrix_square(square, mat)
|
| } |
} |
| |
|
| /* ========================================================================= */ |
/* ========================================================================= */ |
| uLong ZEXPORT crc32_combine(crc1, crc2, len2) | local uLong crc32_combine_(crc1, crc2, len2) |
| uLong crc1; |
uLong crc1; |
| uLong crc2; |
uLong crc2; |
| z_off_t len2; | z_off64_t len2; |
| { |
{ |
| int n; |
int n; |
| unsigned long row; |
unsigned long row; |
| unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ |
unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ |
| unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ |
unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ |
| |
|
| /* degenerate case */ | /* degenerate case (also disallow negative lengths) */ |
| if (len2 == 0) | if (len2 <= 0) |
| return crc1; |
return crc1; |
| |
|
| /* put operator for one zero bit in odd */ |
/* put operator for one zero bit in odd */ |
| odd[0] = 0xedb88320L; /* CRC-32 polynomial */ | odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ |
| row = 1; |
row = 1; |
| for (n = 1; n < GF2_DIM; n++) { |
for (n = 1; n < GF2_DIM; n++) { |
| odd[n] = row; |
odd[n] = row; |
|
Line 420 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
|
Line 403 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
|
| /* return combined crc */ |
/* return combined crc */ |
| crc1 ^= crc2; |
crc1 ^= crc2; |
| return crc1; |
return crc1; |
| |
} |
| |
|
| |
/* ========================================================================= */ |
| |
uLong ZEXPORT crc32_combine(crc1, crc2, len2) |
| |
uLong crc1; |
| |
uLong crc2; |
| |
z_off_t len2; |
| |
{ |
| |
return crc32_combine_(crc1, crc2, len2); |
| |
} |
| |
|
| |
uLong ZEXPORT crc32_combine64(crc1, crc2, len2) |
| |
uLong crc1; |
| |
uLong crc2; |
| |
z_off64_t len2; |
| |
{ |
| |
return crc32_combine_(crc1, crc2, len2); |
| } |
} |