Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream |
michael@0 | 2 | * Copyright (C) 1995-2011 Mark Adler |
michael@0 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | /* @(#) $Id$ */ |
michael@0 | 7 | |
michael@0 | 8 | #include "zutil.h" |
michael@0 | 9 | |
michael@0 | 10 | #define local static |
michael@0 | 11 | |
michael@0 | 12 | local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); |
michael@0 | 13 | |
michael@0 | 14 | #define BASE 65521 /* largest prime smaller than 65536 */ |
michael@0 | 15 | #define NMAX 5552 |
michael@0 | 16 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
michael@0 | 17 | |
michael@0 | 18 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} |
michael@0 | 19 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); |
michael@0 | 20 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); |
michael@0 | 21 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); |
michael@0 | 22 | #define DO16(buf) DO8(buf,0); DO8(buf,8); |
michael@0 | 23 | |
michael@0 | 24 | /* use NO_DIVIDE if your processor does not do division in hardware -- |
michael@0 | 25 | try it both ways to see which is faster */ |
michael@0 | 26 | #ifdef NO_DIVIDE |
michael@0 | 27 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 |
michael@0 | 28 | (thank you to John Reiser for pointing this out) */ |
michael@0 | 29 | # define CHOP(a) \ |
michael@0 | 30 | do { \ |
michael@0 | 31 | unsigned long tmp = a >> 16; \ |
michael@0 | 32 | a &= 0xffffUL; \ |
michael@0 | 33 | a += (tmp << 4) - tmp; \ |
michael@0 | 34 | } while (0) |
michael@0 | 35 | # define MOD28(a) \ |
michael@0 | 36 | do { \ |
michael@0 | 37 | CHOP(a); \ |
michael@0 | 38 | if (a >= BASE) a -= BASE; \ |
michael@0 | 39 | } while (0) |
michael@0 | 40 | # define MOD(a) \ |
michael@0 | 41 | do { \ |
michael@0 | 42 | CHOP(a); \ |
michael@0 | 43 | MOD28(a); \ |
michael@0 | 44 | } while (0) |
michael@0 | 45 | # define MOD63(a) \ |
michael@0 | 46 | do { /* this assumes a is not negative */ \ |
michael@0 | 47 | z_off64_t tmp = a >> 32; \ |
michael@0 | 48 | a &= 0xffffffffL; \ |
michael@0 | 49 | a += (tmp << 8) - (tmp << 5) + tmp; \ |
michael@0 | 50 | tmp = a >> 16; \ |
michael@0 | 51 | a &= 0xffffL; \ |
michael@0 | 52 | a += (tmp << 4) - tmp; \ |
michael@0 | 53 | tmp = a >> 16; \ |
michael@0 | 54 | a &= 0xffffL; \ |
michael@0 | 55 | a += (tmp << 4) - tmp; \ |
michael@0 | 56 | if (a >= BASE) a -= BASE; \ |
michael@0 | 57 | } while (0) |
michael@0 | 58 | #else |
michael@0 | 59 | # define MOD(a) a %= BASE |
michael@0 | 60 | # define MOD28(a) a %= BASE |
michael@0 | 61 | # define MOD63(a) a %= BASE |
michael@0 | 62 | #endif |
michael@0 | 63 | |
michael@0 | 64 | /* ========================================================================= */ |
michael@0 | 65 | uLong ZEXPORT adler32(adler, buf, len) |
michael@0 | 66 | uLong adler; |
michael@0 | 67 | const Bytef *buf; |
michael@0 | 68 | uInt len; |
michael@0 | 69 | { |
michael@0 | 70 | unsigned long sum2; |
michael@0 | 71 | unsigned n; |
michael@0 | 72 | |
michael@0 | 73 | /* split Adler-32 into component sums */ |
michael@0 | 74 | sum2 = (adler >> 16) & 0xffff; |
michael@0 | 75 | adler &= 0xffff; |
michael@0 | 76 | |
michael@0 | 77 | /* in case user likes doing a byte at a time, keep it fast */ |
michael@0 | 78 | if (len == 1) { |
michael@0 | 79 | adler += buf[0]; |
michael@0 | 80 | if (adler >= BASE) |
michael@0 | 81 | adler -= BASE; |
michael@0 | 82 | sum2 += adler; |
michael@0 | 83 | if (sum2 >= BASE) |
michael@0 | 84 | sum2 -= BASE; |
michael@0 | 85 | return adler | (sum2 << 16); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | /* initial Adler-32 value (deferred check for len == 1 speed) */ |
michael@0 | 89 | if (buf == Z_NULL) |
michael@0 | 90 | return 1L; |
michael@0 | 91 | |
michael@0 | 92 | /* in case short lengths are provided, keep it somewhat fast */ |
michael@0 | 93 | if (len < 16) { |
michael@0 | 94 | while (len--) { |
michael@0 | 95 | adler += *buf++; |
michael@0 | 96 | sum2 += adler; |
michael@0 | 97 | } |
michael@0 | 98 | if (adler >= BASE) |
michael@0 | 99 | adler -= BASE; |
michael@0 | 100 | MOD28(sum2); /* only added so many BASE's */ |
michael@0 | 101 | return adler | (sum2 << 16); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | /* do length NMAX blocks -- requires just one modulo operation */ |
michael@0 | 105 | while (len >= NMAX) { |
michael@0 | 106 | len -= NMAX; |
michael@0 | 107 | n = NMAX / 16; /* NMAX is divisible by 16 */ |
michael@0 | 108 | do { |
michael@0 | 109 | DO16(buf); /* 16 sums unrolled */ |
michael@0 | 110 | buf += 16; |
michael@0 | 111 | } while (--n); |
michael@0 | 112 | MOD(adler); |
michael@0 | 113 | MOD(sum2); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /* do remaining bytes (less than NMAX, still just one modulo) */ |
michael@0 | 117 | if (len) { /* avoid modulos if none remaining */ |
michael@0 | 118 | while (len >= 16) { |
michael@0 | 119 | len -= 16; |
michael@0 | 120 | DO16(buf); |
michael@0 | 121 | buf += 16; |
michael@0 | 122 | } |
michael@0 | 123 | while (len--) { |
michael@0 | 124 | adler += *buf++; |
michael@0 | 125 | sum2 += adler; |
michael@0 | 126 | } |
michael@0 | 127 | MOD(adler); |
michael@0 | 128 | MOD(sum2); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /* return recombined sums */ |
michael@0 | 132 | return adler | (sum2 << 16); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | /* ========================================================================= */ |
michael@0 | 136 | local uLong adler32_combine_(adler1, adler2, len2) |
michael@0 | 137 | uLong adler1; |
michael@0 | 138 | uLong adler2; |
michael@0 | 139 | z_off64_t len2; |
michael@0 | 140 | { |
michael@0 | 141 | unsigned long sum1; |
michael@0 | 142 | unsigned long sum2; |
michael@0 | 143 | unsigned rem; |
michael@0 | 144 | |
michael@0 | 145 | /* for negative len, return invalid adler32 as a clue for debugging */ |
michael@0 | 146 | if (len2 < 0) |
michael@0 | 147 | return 0xffffffffUL; |
michael@0 | 148 | |
michael@0 | 149 | /* the derivation of this formula is left as an exercise for the reader */ |
michael@0 | 150 | MOD63(len2); /* assumes len2 >= 0 */ |
michael@0 | 151 | rem = (unsigned)len2; |
michael@0 | 152 | sum1 = adler1 & 0xffff; |
michael@0 | 153 | sum2 = rem * sum1; |
michael@0 | 154 | MOD(sum2); |
michael@0 | 155 | sum1 += (adler2 & 0xffff) + BASE - 1; |
michael@0 | 156 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; |
michael@0 | 157 | if (sum1 >= BASE) sum1 -= BASE; |
michael@0 | 158 | if (sum1 >= BASE) sum1 -= BASE; |
michael@0 | 159 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); |
michael@0 | 160 | if (sum2 >= BASE) sum2 -= BASE; |
michael@0 | 161 | return sum1 | (sum2 << 16); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | /* ========================================================================= */ |
michael@0 | 165 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) |
michael@0 | 166 | uLong adler1; |
michael@0 | 167 | uLong adler2; |
michael@0 | 168 | z_off_t len2; |
michael@0 | 169 | { |
michael@0 | 170 | return adler32_combine_(adler1, adler2, len2); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2) |
michael@0 | 174 | uLong adler1; |
michael@0 | 175 | uLong adler2; |
michael@0 | 176 | z_off64_t len2; |
michael@0 | 177 | { |
michael@0 | 178 | return adler32_combine_(adler1, adler2, len2); |
michael@0 | 179 | } |