diff options
| -rw-r--r-- | crc32.c | 58 |
1 files changed, 26 insertions, 32 deletions
| @@ -159,10 +159,10 @@ local z_word_t byte_swap(z_word_t word) { | |||
| 159 | Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial, | 159 | Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial, |
| 160 | reflected. For speed, this requires that a not be zero. | 160 | reflected. For speed, this requires that a not be zero. |
| 161 | */ | 161 | */ |
| 162 | local z_crc_t multmodp(z_crc_t a, z_crc_t b) { | 162 | local uLong multmodp(uLong a, uLong b) { |
| 163 | z_crc_t m, p; | 163 | uLong m, p; |
| 164 | 164 | ||
| 165 | m = (z_crc_t)1 << 31; | 165 | m = (uLong)1 << 31; |
| 166 | p = 0; | 166 | p = 0; |
| 167 | for (;;) { | 167 | for (;;) { |
| 168 | if (a & m) { | 168 | if (a & m) { |
| @@ -178,12 +178,12 @@ local z_crc_t multmodp(z_crc_t a, z_crc_t b) { | |||
| 178 | 178 | ||
| 179 | /* | 179 | /* |
| 180 | Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been | 180 | Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been |
| 181 | initialized. | 181 | initialized. n must not be negative. |
| 182 | */ | 182 | */ |
| 183 | local z_crc_t x2nmodp(z_off64_t n, unsigned k) { | 183 | local uLong x2nmodp(z_off64_t n, unsigned k) { |
| 184 | z_crc_t p; | 184 | uLong p; |
| 185 | 185 | ||
| 186 | p = (z_crc_t)1 << 31; /* x^0 == 1 */ | 186 | p = (uLong)1 << 31; /* x^0 == 1 */ |
| 187 | while (n) { | 187 | while (n) { |
| 188 | if (n & 1) | 188 | if (n & 1) |
| 189 | p = multmodp(x2n_table[k & 31], p); | 189 | p = multmodp(x2n_table[k & 31], p); |
| @@ -258,7 +258,7 @@ local void make_crc_table(void) { | |||
| 258 | p = (z_crc_t)1 << 30; /* x^1 */ | 258 | p = (z_crc_t)1 << 30; /* x^1 */ |
| 259 | x2n_table[0] = p; | 259 | x2n_table[0] = p; |
| 260 | for (n = 1; n < 32; n++) | 260 | for (n = 1; n < 32; n++) |
| 261 | x2n_table[n] = p = multmodp(p, p); | 261 | x2n_table[n] = p = (z_crc_t)multmodp(p, p); |
| 262 | 262 | ||
| 263 | #ifdef W | 263 | #ifdef W |
| 264 | /* initialize the braiding tables -- needs x2n_table[] */ | 264 | /* initialize the braiding tables -- needs x2n_table[] */ |
| @@ -461,11 +461,11 @@ local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) { | |||
| 461 | int k; | 461 | int k; |
| 462 | z_crc_t i, p, q; | 462 | z_crc_t i, p, q; |
| 463 | for (k = 0; k < w; k++) { | 463 | for (k = 0; k < w; k++) { |
| 464 | p = x2nmodp((n * w + 3 - k) << 3, 0); | 464 | p = (z_crc_t)x2nmodp((n * w + 3 - k) << 3, 0); |
| 465 | ltl[k][0] = 0; | 465 | ltl[k][0] = 0; |
| 466 | big[w - 1 - k][0] = 0; | 466 | big[w - 1 - k][0] = 0; |
| 467 | for (i = 1; i < 256; i++) { | 467 | for (i = 1; i < 256; i++) { |
| 468 | ltl[k][i] = q = multmodp(i << 24, p); | 468 | ltl[k][i] = q = (z_crc_t)multmodp(i << 24, p); |
| 469 | big[w - 1 - k][i] = byte_swap(q); | 469 | big[w - 1 - k][i] = byte_swap(q); |
| 470 | } | 470 | } |
| 471 | } | 471 | } |
| @@ -504,9 +504,8 @@ const z_crc_t FAR * ZEXPORT get_crc_table(void) { | |||
| 504 | #define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */ | 504 | #define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */ |
| 505 | #define Z_BATCH_MIN 800 /* fewest words in a final batch */ | 505 | #define Z_BATCH_MIN 800 /* fewest words in a final batch */ |
| 506 | 506 | ||
| 507 | unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, | 507 | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
| 508 | z_size_t len) { | 508 | uLong val; |
| 509 | z_crc_t val; | ||
| 510 | z_word_t crc1, crc2; | 509 | z_word_t crc1, crc2; |
| 511 | const z_word_t *word; | 510 | const z_word_t *word; |
| 512 | z_word_t val0, val1, val2; | 511 | z_word_t val0, val1, val2; |
| @@ -572,7 +571,7 @@ unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, | |||
| 572 | } | 571 | } |
| 573 | word += 3 * last; | 572 | word += 3 * last; |
| 574 | num -= 3 * last; | 573 | num -= 3 * last; |
| 575 | val = x2nmodp(last, 6); | 574 | val = x2nmodp((int)last, 6); |
| 576 | crc = multmodp(val, crc) ^ crc1; | 575 | crc = multmodp(val, crc) ^ crc1; |
| 577 | crc = multmodp(val, crc) ^ crc2; | 576 | crc = multmodp(val, crc) ^ crc2; |
| 578 | } | 577 | } |
| @@ -623,8 +622,7 @@ local z_word_t crc_word_big(z_word_t data) { | |||
| 623 | #endif | 622 | #endif |
| 624 | 623 | ||
| 625 | /* ========================================================================= */ | 624 | /* ========================================================================= */ |
| 626 | unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, | 625 | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
| 627 | z_size_t len) { | ||
| 628 | /* Return initial CRC, if requested. */ | 626 | /* Return initial CRC, if requested. */ |
| 629 | if (buf == Z_NULL) return 0; | 627 | if (buf == Z_NULL) return 0; |
| 630 | 628 | ||
| @@ -944,8 +942,7 @@ unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, | |||
| 944 | #endif | 942 | #endif |
| 945 | 943 | ||
| 946 | /* ========================================================================= */ | 944 | /* ========================================================================= */ |
| 947 | unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, | 945 | uLong ZEXPORT crc32(uLong crc, const unsigned char FAR *buf, uInt len) { |
| 948 | uInt len) { | ||
| 949 | #ifdef HAVE_S390X_VX | 946 | #ifdef HAVE_S390X_VX |
| 950 | return crc32_z_hook(crc, buf, len); | 947 | return crc32_z_hook(crc, buf, len); |
| 951 | #endif | 948 | #endif |
| @@ -953,36 +950,33 @@ unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, | |||
| 953 | } | 950 | } |
| 954 | 951 | ||
| 955 | /* ========================================================================= */ | 952 | /* ========================================================================= */ |
| 956 | uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) { | 953 | uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) { |
| 957 | if (len2 < 0) | 954 | if (len2 < 0) |
| 958 | return 0; | 955 | return 0; |
| 959 | #ifdef DYNAMIC_CRC_TABLE | 956 | #ifdef DYNAMIC_CRC_TABLE |
| 960 | z_once(&made, make_crc_table); | 957 | z_once(&made, make_crc_table); |
| 961 | #endif /* DYNAMIC_CRC_TABLE */ | 958 | #endif /* DYNAMIC_CRC_TABLE */ |
| 962 | return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff); | 959 | return x2nmodp(len2, 3); |
| 963 | } | 960 | } |
| 964 | 961 | ||
| 965 | /* ========================================================================= */ | 962 | /* ========================================================================= */ |
| 966 | uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) { | 963 | uLong ZEXPORT crc32_combine_gen(z_off_t len2) { |
| 967 | return crc32_combine64(crc1, crc2, (z_off64_t)len2); | 964 | return crc32_combine_gen64((z_off64_t)len2); |
| 968 | } | 965 | } |
| 969 | 966 | ||
| 970 | /* ========================================================================= */ | 967 | /* ========================================================================= */ |
| 971 | uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) { | 968 | uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) { |
| 972 | if (len2 < 0) | 969 | if (op == 0) |
| 973 | return 0; | 970 | return 0; |
| 974 | #ifdef DYNAMIC_CRC_TABLE | 971 | return multmodp(op, crc1 & 0xffffffff) ^ (crc2 & 0xffffffff); |
| 975 | z_once(&made, make_crc_table); | ||
| 976 | #endif /* DYNAMIC_CRC_TABLE */ | ||
| 977 | return x2nmodp(len2, 3); | ||
| 978 | } | 972 | } |
| 979 | 973 | ||
| 980 | /* ========================================================================= */ | 974 | /* ========================================================================= */ |
| 981 | uLong ZEXPORT crc32_combine_gen(z_off_t len2) { | 975 | uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) { |
| 982 | return crc32_combine_gen64((z_off64_t)len2); | 976 | return crc32_combine_op(crc1, crc2, crc32_combine_gen64(len2)); |
| 983 | } | 977 | } |
| 984 | 978 | ||
| 985 | /* ========================================================================= */ | 979 | /* ========================================================================= */ |
| 986 | uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) { | 980 | uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) { |
| 987 | return multmodp(op, crc1) ^ (crc2 & 0xffffffff); | 981 | return crc32_combine64(crc1, crc2, (z_off64_t)len2); |
| 988 | } | 982 | } |
