diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:24:02 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:24:02 -0700 |
commit | 79fbcdc939b5d515218187a0d5f2526fb632075a (patch) | |
tree | dc82b3a452568093ab8a9f365126ba56a419eea1 /crc32.c | |
parent | 7a6955760ba950eb82f57929f8f6c9847c65f0af (diff) | |
download | zlib-79fbcdc939b5d515218187a0d5f2526fb632075a.tar.gz zlib-79fbcdc939b5d515218187a0d5f2526fb632075a.tar.bz2 zlib-79fbcdc939b5d515218187a0d5f2526fb632075a.zip |
zlib 1.2.2v1.2.2
Diffstat (limited to 'crc32.c')
-rw-r--r-- | crc32.c | 92 |
1 files changed, 46 insertions, 46 deletions
@@ -12,11 +12,11 @@ | |||
12 | /* @(#) $Id$ */ | 12 | /* @(#) $Id$ */ |
13 | 13 | ||
14 | /* | 14 | /* |
15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore protection | 15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore |
16 | on the static variables used to control the first-use generation of the crc | 16 | protection on the static variables used to control the first-use generation |
17 | tables. Therefore if you #define DYNAMIC_CRC_TABLE, you should first call | 17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should |
18 | get_crc_table() to initialize the tables before allowing more than on thread | 18 | first call get_crc_table() to initialize the tables before allowing more than |
19 | to use crc32(). | 19 | one thread to use crc32(). |
20 | */ | 20 | */ |
21 | 21 | ||
22 | #ifdef MAKECRCH | 22 | #ifdef MAKECRCH |
@@ -103,51 +103,51 @@ local void make_crc_table() | |||
103 | { | 103 | { |
104 | unsigned long c; | 104 | unsigned long c; |
105 | int n, k; | 105 | int n, k; |
106 | unsigned long poly; /* polynomial exclusive-or pattern */ | 106 | unsigned long poly; /* polynomial exclusive-or pattern */ |
107 | /* terms of polynomial defining this crc (except x^32): */ | 107 | /* terms of polynomial defining this crc (except x^32): */ |
108 | static volatile int first = 1; /* flag to limit concurrent making */ | 108 | static volatile int first = 1; /* flag to limit concurrent making */ |
109 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | 109 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; |
110 | 110 | ||
111 | /* See if another task is already doing this (not thread-safe, but better | 111 | /* See if another task is already doing this (not thread-safe, but better |
112 | than nothing -- significantly reduces duration of vulnerability in | 112 | than nothing -- significantly reduces duration of vulnerability in |
113 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ | 113 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ |
114 | if (first) { | 114 | if (first) { |
115 | first = 0; | 115 | first = 0; |
116 | 116 | ||
117 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ | 117 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ |
118 | poly = 0UL; | 118 | poly = 0UL; |
119 | for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) | 119 | for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) |
120 | poly |= 1UL << (31 - p[n]); | 120 | poly |= 1UL << (31 - p[n]); |
121 | 121 | ||
122 | /* generate a crc for every 8-bit value */ | 122 | /* generate a crc for every 8-bit value */ |
123 | for (n = 0; n < 256; n++) { | 123 | for (n = 0; n < 256; n++) { |
124 | c = (unsigned long)n; | 124 | c = (unsigned long)n; |
125 | for (k = 0; k < 8; k++) | 125 | for (k = 0; k < 8; k++) |
126 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; | 126 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; |
127 | crc_table[0][n] = c; | 127 | crc_table[0][n] = c; |
128 | } | 128 | } |
129 | 129 | ||
130 | #ifdef BYFOUR | 130 | #ifdef BYFOUR |
131 | /* generate crc for each value followed by one, two, and three zeros, and | 131 | /* generate crc for each value followed by one, two, and three zeros, |
132 | then the byte reversal of those as well as the first table */ | 132 | and then the byte reversal of those as well as the first table */ |
133 | for (n = 0; n < 256; n++) { | 133 | for (n = 0; n < 256; n++) { |
134 | c = crc_table[0][n]; | 134 | c = crc_table[0][n]; |
135 | crc_table[4][n] = REV(c); | 135 | crc_table[4][n] = REV(c); |
136 | for (k = 1; k < 4; k++) { | 136 | for (k = 1; k < 4; k++) { |
137 | c = crc_table[0][c & 0xff] ^ (c >> 8); | 137 | c = crc_table[0][c & 0xff] ^ (c >> 8); |
138 | crc_table[k][n] = c; | 138 | crc_table[k][n] = c; |
139 | crc_table[k + 4][n] = REV(c); | 139 | crc_table[k + 4][n] = REV(c); |
140 | } | 140 | } |
141 | } | 141 | } |
142 | #endif /* BYFOUR */ | 142 | #endif /* BYFOUR */ |
143 | 143 | ||
144 | crc_table_empty = 0; | 144 | crc_table_empty = 0; |
145 | } | 145 | } |
146 | else { /* not first */ | 146 | else { /* not first */ |
147 | /* wait for the other guy to finish (not exactly efficient, but rare) */ | 147 | /* wait for the other guy to finish (not efficient, but rare) */ |
148 | while (crc_table_empty) | 148 | while (crc_table_empty) |
149 | ; | 149 | ; |
150 | } | 150 | } |
151 | 151 | ||
152 | #ifdef MAKECRCH | 152 | #ifdef MAKECRCH |
153 | /* write out CRC tables to crc32.h */ | 153 | /* write out CRC tables to crc32.h */ |
@@ -201,10 +201,10 @@ local void write_table(out, table) | |||
201 | const unsigned long FAR * ZEXPORT get_crc_table() | 201 | const unsigned long FAR * ZEXPORT get_crc_table() |
202 | { | 202 | { |
203 | #ifdef DYNAMIC_CRC_TABLE | 203 | #ifdef DYNAMIC_CRC_TABLE |
204 | if (crc_table_empty) | 204 | if (crc_table_empty) |
205 | make_crc_table(); | 205 | make_crc_table(); |
206 | #endif /* DYNAMIC_CRC_TABLE */ | 206 | #endif /* DYNAMIC_CRC_TABLE */ |
207 | return (const unsigned long FAR *)crc_table; | 207 | return (const unsigned long FAR *)crc_table; |
208 | } | 208 | } |
209 | 209 | ||
210 | /* ========================================================================= */ | 210 | /* ========================================================================= */ |