| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "zbuild.h" |
| 11 |
#include "zutil.h" |
| 12 |
#include "functable.h" |
| 13 |
#include "crc32_braid_p.h" |
| 14 |
#include "crc32_braid_tbl.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const uint32_t * Z_EXPORT PREFIX(get_crc_table)(void) { |
| 19 |
return (const uint32_t *)crc_table; |
| 20 |
} |
| 21 |
|
| 22 |
#ifdef ZLIB_COMPAT |
| 23 |
unsigned long Z_EXPORT PREFIX(crc32_z)(unsigned long crc, const unsigned char *buf, size_t len) { |
| 24 |
if (buf == NULL) return 0; |
| 25 |
|
| 26 |
return (unsigned long)functable.crc32((uint32_t)crc, buf, len); |
| 27 |
} |
| 28 |
#else |
| 29 |
uint32_t Z_EXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t len) { |
| 30 |
if (buf == NULL) return 0; |
| 31 |
|
| 32 |
return functable.crc32(crc, buf, len); |
| 33 |
} |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef ZLIB_COMPAT |
| 37 |
unsigned long Z_EXPORT PREFIX(crc32)(unsigned long crc, const unsigned char *buf, unsigned int len) { |
| 38 |
return (unsigned long)PREFIX(crc32_z)((uint32_t)crc, buf, len); |
| 39 |
} |
| 40 |
#else |
| 41 |
uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t len) { |
| 42 |
return PREFIX(crc32_z)(crc, buf, len); |
| 43 |
} |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 72 |
# define ZSWAPWORD(word) (word) |
| 73 |
# define BRAID_TABLE crc_braid_table |
| 74 |
#elif BYTE_ORDER == BIG_ENDIAN |
| 75 |
# if W == 8 |
| 76 |
# define ZSWAPWORD(word) ZSWAP64(word) |
| 77 |
# elif W == 4 |
| 78 |
# define ZSWAPWORD(word) ZSWAP32(word) |
| 79 |
# endif |
| 80 |
# define BRAID_TABLE crc_braid_big_table |
| 81 |
#else |
| 82 |
# error "No endian defined" |
| 83 |
#endif |
| 84 |
#define DO1 c = crc_table[(c ^ *buf++) & 0xff] ^ (c >> 8) |
| 85 |
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 |
| 86 |
|
| 87 |
|
| 88 |
#ifdef W |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 95 |
static uint32_t crc_word(z_word_t data) { |
| 96 |
int k; |
| 97 |
for (k = 0; k < W; k++) |
| 98 |
data = (data >> 8) ^ crc_table[data & 0xff]; |
| 99 |
return (uint32_t)data; |
| 100 |
} |
| 101 |
#elif BYTE_ORDER == BIG_ENDIAN |
| 102 |
static z_word_t crc_word(z_word_t data) { |
| 103 |
int k; |
| 104 |
for (k = 0; k < W; k++) |
| 105 |
data = (data << 8) ^ |
| 106 |
crc_big_table[(data >> ((W - 1) << 3)) & 0xff]; |
| 107 |
return data; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
#endif |
| 112 |
|
| 113 |
|
| 114 |
Z_INTERNAL uint32_t crc32_braid(uint32_t crc, const uint8_t *buf, uint64_t len) { |
| 115 |
Z_REGISTER uint32_t c; |
| 116 |
|
| 117 |
|
| 118 |
c = (~crc) & 0xffffffff; |
| 119 |
|
| 120 |
#ifdef W |
| 121 |
|
| 122 |
if (len >= N * W + W - 1) { |
| 123 |
uint64_t blks; |
| 124 |
z_word_t const *words; |
| 125 |
int k; |
| 126 |
|
| 127 |
|
| 128 |
while (len && ((z_size_t)buf & (W - 1)) != 0) { |
| 129 |
len--; |
| 130 |
DO1; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
blks = len / (N * W); |
| 135 |
len -= blks * N * W; |
| 136 |
words = (z_word_t const *)buf; |
| 137 |
|
| 138 |
z_word_t crc0, word0, comb; |
| 139 |
#if N > 1 |
| 140 |
z_word_t crc1, word1; |
| 141 |
#if N > 2 |
| 142 |
z_word_t crc2, word2; |
| 143 |
#if N > 3 |
| 144 |
z_word_t crc3, word3; |
| 145 |
#if N > 4 |
| 146 |
z_word_t crc4, word4; |
| 147 |
#if N > 5 |
| 148 |
z_word_t crc5, word5; |
| 149 |
#endif |
| 150 |
#endif |
| 151 |
#endif |
| 152 |
#endif |
| 153 |
#endif |
| 154 |
|
| 155 |
crc0 = ZSWAPWORD(c); |
| 156 |
#if N > 1 |
| 157 |
crc1 = 0; |
| 158 |
#if N > 2 |
| 159 |
crc2 = 0; |
| 160 |
#if N > 3 |
| 161 |
crc3 = 0; |
| 162 |
#if N > 4 |
| 163 |
crc4 = 0; |
| 164 |
#if N > 5 |
| 165 |
crc5 = 0; |
| 166 |
#endif |
| 167 |
#endif |
| 168 |
#endif |
| 169 |
#endif |
| 170 |
#endif |
| 171 |
|
| 172 |
while (--blks) { |
| 173 |
|
| 174 |
word0 = crc0 ^ words[0]; |
| 175 |
#if N > 1 |
| 176 |
word1 = crc1 ^ words[1]; |
| 177 |
#if N > 2 |
| 178 |
word2 = crc2 ^ words[2]; |
| 179 |
#if N > 3 |
| 180 |
word3 = crc3 ^ words[3]; |
| 181 |
#if N > 4 |
| 182 |
word4 = crc4 ^ words[4]; |
| 183 |
#if N > 5 |
| 184 |
word5 = crc5 ^ words[5]; |
| 185 |
#endif |
| 186 |
#endif |
| 187 |
#endif |
| 188 |
#endif |
| 189 |
#endif |
| 190 |
words += N; |
| 191 |
|
| 192 |
|
| 193 |
crc0 = BRAID_TABLE[0][word0 & 0xff]; |
| 194 |
#if N > 1 |
| 195 |
crc1 = BRAID_TABLE[0][word1 & 0xff]; |
| 196 |
#if N > 2 |
| 197 |
crc2 = BRAID_TABLE[0][word2 & 0xff]; |
| 198 |
#if N > 3 |
| 199 |
crc3 = BRAID_TABLE[0][word3 & 0xff]; |
| 200 |
#if N > 4 |
| 201 |
crc4 = BRAID_TABLE[0][word4 & 0xff]; |
| 202 |
#if N > 5 |
| 203 |
crc5 = BRAID_TABLE[0][word5 & 0xff]; |
| 204 |
#endif |
| 205 |
#endif |
| 206 |
#endif |
| 207 |
#endif |
| 208 |
#endif |
| 209 |
for (k = 1; k < W; k++) { |
| 210 |
crc0 ^= BRAID_TABLE[k][(word0 >> (k << 3)) & 0xff]; |
| 211 |
#if N > 1 |
| 212 |
crc1 ^= BRAID_TABLE[k][(word1 >> (k << 3)) & 0xff]; |
| 213 |
#if N > 2 |
| 214 |
crc2 ^= BRAID_TABLE[k][(word2 >> (k << 3)) & 0xff]; |
| 215 |
#if N > 3 |
| 216 |
crc3 ^= BRAID_TABLE[k][(word3 >> (k << 3)) & 0xff]; |
| 217 |
#if N > 4 |
| 218 |
crc4 ^= BRAID_TABLE[k][(word4 >> (k << 3)) & 0xff]; |
| 219 |
#if N > 5 |
| 220 |
crc5 ^= BRAID_TABLE[k][(word5 >> (k << 3)) & 0xff]; |
| 221 |
#endif |
| 222 |
#endif |
| 223 |
#endif |
| 224 |
#endif |
| 225 |
#endif |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
comb = crc_word(crc0 ^ words[0]); |
| 231 |
#if N > 1 |
| 232 |
comb = crc_word(crc1 ^ words[1] ^ comb); |
| 233 |
#if N > 2 |
| 234 |
comb = crc_word(crc2 ^ words[2] ^ comb); |
| 235 |
#if N > 3 |
| 236 |
comb = crc_word(crc3 ^ words[3] ^ comb); |
| 237 |
#if N > 4 |
| 238 |
comb = crc_word(crc4 ^ words[4] ^ comb); |
| 239 |
#if N > 5 |
| 240 |
comb = crc_word(crc5 ^ words[5] ^ comb); |
| 241 |
#endif |
| 242 |
#endif |
| 243 |
#endif |
| 244 |
#endif |
| 245 |
#endif |
| 246 |
words += N; |
| 247 |
c = ZSWAPWORD(comb); |
| 248 |
|
| 249 |
|
| 250 |
buf = (const unsigned char *)words; |
| 251 |
} |
| 252 |
|
| 253 |
#endif |
| 254 |
|
| 255 |
|
| 256 |
while (len >= 8) { |
| 257 |
len -= 8; |
| 258 |
DO8; |
| 259 |
} |
| 260 |
while (len) { |
| 261 |
len--; |
| 262 |
DO1; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
return c ^ 0xffffffff; |
| 267 |
} |