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