MCPcopy Create free account
hub / github.com/F-Stack/f-stack / make_crc_table

Function make_crc_table

freebsd/contrib/zlib/crc32.c:88–162  ·  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

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

Callers 2

get_crc_tableFunction · 0.85
crc32_zFunction · 0.85

Calls 1

write_tableFunction · 0.85

Tested by

no test coverage detected