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