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