aboutsummaryrefslogtreecommitdiff
path: root/crc32.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-11 11:04:49 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-11 11:04:49 -0700
commit10daf0d4d7815447799d555d04d30325836e1d44 (patch)
tree75579fbe11e42dc3197acca53f5f887ac3808b57 /crc32.c
parent9712272c78b9d9c93746d9c8e156a3728c65ca72 (diff)
downloadzlib-10daf0d4d7815447799d555d04d30325836e1d44.tar.gz
zlib-10daf0d4d7815447799d555d04d30325836e1d44.tar.bz2
zlib-10daf0d4d7815447799d555d04d30325836e1d44.zip
zlib 1.2.5.1v1.2.5.1
Diffstat (limited to 'crc32.c')
-rw-r--r--crc32.c31
1 files changed, 18 insertions, 13 deletions
diff --git a/crc32.c b/crc32.c
index 91be372..c12471e 100644
--- a/crc32.c
+++ b/crc32.c
@@ -1,5 +1,5 @@
1/* crc32.c -- compute the CRC-32 of a data stream 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 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 * 4 *
5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster 5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
@@ -17,6 +17,8 @@
17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should 17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
18 first call get_crc_table() to initialize the tables before allowing more than 18 first call get_crc_table() to initialize the tables before allowing more than
19 one thread to use crc32(). 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#ifdef MAKECRCH 24#ifdef MAKECRCH
@@ -53,6 +55,7 @@
53 55
54/* Definitions for doing the crc four data bytes at a time. */ 56/* Definitions for doing the crc four data bytes at a time. */
55#ifdef BYFOUR 57#ifdef BYFOUR
58 typedef u4 crc_table_t;
56# define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \ 59# define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
57 (((w)&0xff00)<<8)+(((w)&0xff)<<24)) 60 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58 local unsigned long crc32_little OF((unsigned long, 61 local unsigned long crc32_little OF((unsigned long,
@@ -61,6 +64,7 @@
61 const unsigned char FAR *, unsigned)); 64 const unsigned char FAR *, unsigned));
62# define TBLS 8 65# define TBLS 8
63#else 66#else
67 typedef unsigned long crc_table_t;
64# define TBLS 1 68# define TBLS 1
65#endif /* BYFOUR */ 69#endif /* BYFOUR */
66 70
@@ -68,16 +72,16 @@
68local unsigned long gf2_matrix_times OF((unsigned long *mat, 72local unsigned long gf2_matrix_times OF((unsigned long *mat,
69 unsigned long vec)); 73 unsigned long vec));
70local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); 74local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
71local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2); 75local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
72 76
73 77
74#ifdef DYNAMIC_CRC_TABLE 78#ifdef DYNAMIC_CRC_TABLE
75 79
76local volatile int crc_table_empty = 1; 80local volatile int crc_table_empty = 1;
77local unsigned long FAR crc_table[TBLS][256]; 81local crc_table_t FAR crc_table[TBLS][256];
78local void make_crc_table OF((void)); 82local void make_crc_table OF((void));
79#ifdef MAKECRCH 83#ifdef MAKECRCH
80 local void write_table OF((FILE *, const unsigned long FAR *)); 84 local void write_table OF((FILE *, const crc_table_t FAR *));
81#endif /* MAKECRCH */ 85#endif /* MAKECRCH */
82/* 86/*
83 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 87 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
@@ -107,9 +111,9 @@ local void make_crc_table OF((void));
107*/ 111*/
108local void make_crc_table() 112local void make_crc_table()
109{ 113{
110 unsigned long c; 114 crc_table_t c;
111 int n, k; 115 int n, k;
112 unsigned long poly; /* polynomial exclusive-or pattern */ 116 crc_table_t poly; /* polynomial exclusive-or pattern */
113 /* terms of polynomial defining this crc (except x^32): */ 117 /* terms of polynomial defining this crc (except x^32): */
114 static volatile int first = 1; /* flag to limit concurrent making */ 118 static volatile int first = 1; /* flag to limit concurrent making */
115 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 119 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
@@ -121,13 +125,13 @@ local void make_crc_table()
121 first = 0; 125 first = 0;
122 126
123 /* make exclusive-or pattern from polynomial (0xedb88320UL) */ 127 /* make exclusive-or pattern from polynomial (0xedb88320UL) */
124 poly = 0UL; 128 poly = 0;
125 for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) 129 for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
126 poly |= 1UL << (31 - p[n]); 130 poly |= (crc_table_t)1 << (31 - p[n]);
127 131
128 /* generate a crc for every 8-bit value */ 132 /* generate a crc for every 8-bit value */
129 for (n = 0; n < 256; n++) { 133 for (n = 0; n < 256; n++) {
130 c = (unsigned long)n; 134 c = (crc_table_t)n;
131 for (k = 0; k < 8; k++) 135 for (k = 0; k < 8; k++)
132 c = c & 1 ? poly ^ (c >> 1) : c >> 1; 136 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
133 crc_table[0][n] = c; 137 crc_table[0][n] = c;
@@ -164,7 +168,7 @@ local void make_crc_table()
164 if (out == NULL) return; 168 if (out == NULL) return;
165 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); 169 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
166 fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); 170 fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
167 fprintf(out, "local const unsigned long FAR "); 171 fprintf(out, "local const crc_table_t FAR ");
168 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); 172 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
169 write_table(out, crc_table[0]); 173 write_table(out, crc_table[0]);
170# ifdef BYFOUR 174# ifdef BYFOUR
@@ -184,12 +188,13 @@ local void make_crc_table()
184#ifdef MAKECRCH 188#ifdef MAKECRCH
185local void write_table(out, table) 189local void write_table(out, table)
186 FILE *out; 190 FILE *out;
187 const unsigned long FAR *table; 191 const crc_table_t FAR *table;
188{ 192{
189 int n; 193 int n;
190 194
191 for (n = 0; n < 256; n++) 195 for (n = 0; n < 256; n++)
192 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], 196 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
197 (unsigned long)(table[n]),
193 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); 198 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
194} 199}
195#endif /* MAKECRCH */ 200#endif /* MAKECRCH */