MCPcopy Create free account
hub / github.com/Meituan-Dianping/SQLAdvisor / make_crc_table

Function make_crc_table

zlib/crc32.c:106–180  ·  view source on GitHub ↗

MAKECRCH */ Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 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. Polynomials over GF(2) are represented in binary, one bit per coefficient, with the lowest powers in the most significant bit. Then adding polynomials is just exclusive-or, and multiplying a polynomial by x is a right shift by one. If w

Source from the content-addressed store, hash-verified

104 endian machines, where a word is four bytes.
105*/
106local void make_crc_table()
107{
108 unsigned long c;
109 int n, k;
110 unsigned long poly; /* polynomial exclusive-or pattern */
111 /* terms of polynomial defining this crc (except x^32): */
112 static volatile int first = 1; /* flag to limit concurrent making */
113 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
114
115 /* See if another task is already doing this (not thread-safe, but better
116 than nothing -- significantly reduces duration of vulnerability in
117 case the advice about DYNAMIC_CRC_TABLE is ignored) */
118 if (first) {
119 first = 0;
120
121 /* make exclusive-or pattern from polynomial (0xedb88320UL) */
122 poly = 0UL;
123 for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
124 poly |= 1UL << (31 - p[n]);
125
126 /* generate a crc for every 8-bit value */
127 for (n = 0; n < 256; n++) {
128 c = (unsigned long)n;
129 for (k = 0; k < 8; k++)
130 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
131 crc_table[0][n] = c;
132 }
133
134#ifdef BYFOUR
135 /* generate crc for each value followed by one, two, and three zeros,
136 and then the byte reversal of those as well as the first table */
137 for (n = 0; n < 256; n++) {
138 c = crc_table[0][n];
139 crc_table[4][n] = REV(c);
140 for (k = 1; k < 4; k++) {
141 c = crc_table[0][c & 0xff] ^ (c >> 8);
142 crc_table[k][n] = c;
143 crc_table[k + 4][n] = REV(c);
144 }
145 }
146#endif /* BYFOUR */
147
148 crc_table_empty = 0;
149 }
150 else { /* not first */
151 /* wait for the other guy to finish (not efficient, but rare) */
152 while (crc_table_empty)
153 ;
154 }
155
156#ifdef MAKECRCH
157 /* write out CRC tables to crc32.h */
158 {
159 FILE *out;
160
161 out = fopen("crc32.h", "w");
162 if (out == NULL) return;
163 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");

Callers 2

get_crc_tableFunction · 0.85
crc32Function · 0.85

Calls 1

write_tableFunction · 0.85

Tested by

no test coverage detected