1 /* adler32.c -- compute the Adler-32 checksum of a data stream
2 * Copyright (C) 1995-2004 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 /* @(#) $Id: ./src/external/zlib-1.2.3/adler32.c, 2011/09/08 dcid Exp $
12 #define BASE 65521UL /* largest prime smaller than 65536 */
14 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
16 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
17 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
18 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
19 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
20 #define DO16(buf) DO8(buf,0); DO8(buf,8);
22 /* use NO_DIVIDE if your processor does not do division in hardware */
26 if (a >= (BASE << 16)) a -= (BASE << 16); \
27 if (a >= (BASE << 15)) a -= (BASE << 15); \
28 if (a >= (BASE << 14)) a -= (BASE << 14); \
29 if (a >= (BASE << 13)) a -= (BASE << 13); \
30 if (a >= (BASE << 12)) a -= (BASE << 12); \
31 if (a >= (BASE << 11)) a -= (BASE << 11); \
32 if (a >= (BASE << 10)) a -= (BASE << 10); \
33 if (a >= (BASE << 9)) a -= (BASE << 9); \
34 if (a >= (BASE << 8)) a -= (BASE << 8); \
35 if (a >= (BASE << 7)) a -= (BASE << 7); \
36 if (a >= (BASE << 6)) a -= (BASE << 6); \
37 if (a >= (BASE << 5)) a -= (BASE << 5); \
38 if (a >= (BASE << 4)) a -= (BASE << 4); \
39 if (a >= (BASE << 3)) a -= (BASE << 3); \
40 if (a >= (BASE << 2)) a -= (BASE << 2); \
41 if (a >= (BASE << 1)) a -= (BASE << 1); \
42 if (a >= BASE) a -= BASE; \
46 if (a >= (BASE << 4)) a -= (BASE << 4); \
47 if (a >= (BASE << 3)) a -= (BASE << 3); \
48 if (a >= (BASE << 2)) a -= (BASE << 2); \
49 if (a >= (BASE << 1)) a -= (BASE << 1); \
50 if (a >= BASE) a -= BASE; \
53 # define MOD(a) a %= BASE
54 # define MOD4(a) a %= BASE
57 /* ========================================================================= */
58 uLong ZEXPORT adler32(adler, buf, len)
66 /* split Adler-32 into component sums */
67 sum2 = (adler >> 16) & 0xffff;
70 /* in case user likes doing a byte at a time, keep it fast */
78 return adler | (sum2 << 16);
81 /* initial Adler-32 value (deferred check for len == 1 speed) */
85 /* in case short lengths are provided, keep it somewhat fast */
93 MOD4(sum2); /* only added so many BASE's */
94 return adler | (sum2 << 16);
97 /* do length NMAX blocks -- requires just one modulo operation */
100 n = NMAX / 16; /* NMAX is divisible by 16 */
102 DO16(buf); /* 16 sums unrolled */
109 /* do remaining bytes (less than NMAX, still just one modulo) */
110 if (len) { /* avoid modulos if none remaining */
124 /* return recombined sums */
125 return adler | (sum2 << 16);
128 /* ========================================================================= */
129 uLong ZEXPORT adler32_combine(adler1, adler2, len2)
138 /* the derivation of this formula is left as an exercise for the reader */
139 rem = (unsigned)(len2 % BASE);
140 sum1 = adler1 & 0xffff;
143 sum1 += (adler2 & 0xffff) + BASE - 1;
144 sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
145 if (sum1 > BASE) sum1 -= BASE;
146 if (sum1 > BASE) sum1 -= BASE;
147 if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
148 if (sum2 > BASE) sum2 -= BASE;
149 return sum1 | (sum2 << 16);