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