Mercurial > hg > CbC > CbC_gcc
comparison zlib/adler32.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | ae3a4bfb450b |
children |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
1 /* adler32.c -- compute the Adler-32 checksum of a data stream | 1 /* adler32.c -- compute the Adler-32 checksum of a data stream |
2 * Copyright (C) 1995-2004 Mark Adler | 2 * Copyright (C) 1995-2011, 2016 Mark Adler |
3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
4 */ | 4 */ |
5 | 5 |
6 /* @(#) $Id: adler32.c,v 1.1.1.2 2002/03/11 21:53:23 tromey Exp $ */ | 6 /* @(#) $Id: adler32.c,v 1.1.1.2 2002/03/11 21:53:23 tromey Exp $ */ |
7 | 7 |
8 #define ZLIB_INTERNAL | 8 #include "zutil.h" |
9 #include "zlib.h" | |
10 | 9 |
11 #define BASE 65521UL /* largest prime smaller than 65536 */ | 10 local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); |
11 | |
12 #define BASE 65521U /* largest prime smaller than 65536 */ | |
12 #define NMAX 5552 | 13 #define NMAX 5552 |
13 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ | 14 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
14 | 15 |
15 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} | 16 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} |
16 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); | 17 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); |
17 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); | 18 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); |
18 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); | 19 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); |
19 #define DO16(buf) DO8(buf,0); DO8(buf,8); | 20 #define DO16(buf) DO8(buf,0); DO8(buf,8); |
20 | 21 |
21 /* use NO_DIVIDE if your processor does not do division in hardware */ | 22 /* use NO_DIVIDE if your processor does not do division in hardware -- |
23 try it both ways to see which is faster */ | |
22 #ifdef NO_DIVIDE | 24 #ifdef NO_DIVIDE |
25 /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 | |
26 (thank you to John Reiser for pointing this out) */ | |
27 # define CHOP(a) \ | |
28 do { \ | |
29 unsigned long tmp = a >> 16; \ | |
30 a &= 0xffffUL; \ | |
31 a += (tmp << 4) - tmp; \ | |
32 } while (0) | |
33 # define MOD28(a) \ | |
34 do { \ | |
35 CHOP(a); \ | |
36 if (a >= BASE) a -= BASE; \ | |
37 } while (0) | |
23 # define MOD(a) \ | 38 # define MOD(a) \ |
24 do { \ | 39 do { \ |
25 if (a >= (BASE << 16)) a -= (BASE << 16); \ | 40 CHOP(a); \ |
26 if (a >= (BASE << 15)) a -= (BASE << 15); \ | 41 MOD28(a); \ |
27 if (a >= (BASE << 14)) a -= (BASE << 14); \ | |
28 if (a >= (BASE << 13)) a -= (BASE << 13); \ | |
29 if (a >= (BASE << 12)) a -= (BASE << 12); \ | |
30 if (a >= (BASE << 11)) a -= (BASE << 11); \ | |
31 if (a >= (BASE << 10)) a -= (BASE << 10); \ | |
32 if (a >= (BASE << 9)) a -= (BASE << 9); \ | |
33 if (a >= (BASE << 8)) a -= (BASE << 8); \ | |
34 if (a >= (BASE << 7)) a -= (BASE << 7); \ | |
35 if (a >= (BASE << 6)) a -= (BASE << 6); \ | |
36 if (a >= (BASE << 5)) a -= (BASE << 5); \ | |
37 if (a >= (BASE << 4)) a -= (BASE << 4); \ | |
38 if (a >= (BASE << 3)) a -= (BASE << 3); \ | |
39 if (a >= (BASE << 2)) a -= (BASE << 2); \ | |
40 if (a >= (BASE << 1)) a -= (BASE << 1); \ | |
41 if (a >= BASE) a -= BASE; \ | |
42 } while (0) | 42 } while (0) |
43 # define MOD4(a) \ | 43 # define MOD63(a) \ |
44 do { \ | 44 do { /* this assumes a is not negative */ \ |
45 if (a >= (BASE << 4)) a -= (BASE << 4); \ | 45 z_off64_t tmp = a >> 32; \ |
46 if (a >= (BASE << 3)) a -= (BASE << 3); \ | 46 a &= 0xffffffffL; \ |
47 if (a >= (BASE << 2)) a -= (BASE << 2); \ | 47 a += (tmp << 8) - (tmp << 5) + tmp; \ |
48 if (a >= (BASE << 1)) a -= (BASE << 1); \ | 48 tmp = a >> 16; \ |
49 a &= 0xffffL; \ | |
50 a += (tmp << 4) - tmp; \ | |
51 tmp = a >> 16; \ | |
52 a &= 0xffffL; \ | |
53 a += (tmp << 4) - tmp; \ | |
49 if (a >= BASE) a -= BASE; \ | 54 if (a >= BASE) a -= BASE; \ |
50 } while (0) | 55 } while (0) |
51 #else | 56 #else |
52 # define MOD(a) a %= BASE | 57 # define MOD(a) a %= BASE |
53 # define MOD4(a) a %= BASE | 58 # define MOD28(a) a %= BASE |
59 # define MOD63(a) a %= BASE | |
54 #endif | 60 #endif |
55 | 61 |
56 /* ========================================================================= */ | 62 /* ========================================================================= */ |
57 uLong ZEXPORT adler32(adler, buf, len) | 63 uLong ZEXPORT adler32_z(adler, buf, len) |
58 uLong adler; | 64 uLong adler; |
59 const Bytef *buf; | 65 const Bytef *buf; |
60 uInt len; | 66 z_size_t len; |
61 { | 67 { |
62 unsigned long sum2; | 68 unsigned long sum2; |
63 unsigned n; | 69 unsigned n; |
64 | 70 |
65 /* split Adler-32 into component sums */ | 71 /* split Adler-32 into component sums */ |
87 adler += *buf++; | 93 adler += *buf++; |
88 sum2 += adler; | 94 sum2 += adler; |
89 } | 95 } |
90 if (adler >= BASE) | 96 if (adler >= BASE) |
91 adler -= BASE; | 97 adler -= BASE; |
92 MOD4(sum2); /* only added so many BASE's */ | 98 MOD28(sum2); /* only added so many BASE's */ |
93 return adler | (sum2 << 16); | 99 return adler | (sum2 << 16); |
94 } | 100 } |
95 | 101 |
96 /* do length NMAX blocks -- requires just one modulo operation */ | 102 /* do length NMAX blocks -- requires just one modulo operation */ |
97 while (len >= NMAX) { | 103 while (len >= NMAX) { |
123 /* return recombined sums */ | 129 /* return recombined sums */ |
124 return adler | (sum2 << 16); | 130 return adler | (sum2 << 16); |
125 } | 131 } |
126 | 132 |
127 /* ========================================================================= */ | 133 /* ========================================================================= */ |
134 uLong ZEXPORT adler32(adler, buf, len) | |
135 uLong adler; | |
136 const Bytef *buf; | |
137 uInt len; | |
138 { | |
139 return adler32_z(adler, buf, len); | |
140 } | |
141 | |
142 /* ========================================================================= */ | |
143 local uLong adler32_combine_(adler1, adler2, len2) | |
144 uLong adler1; | |
145 uLong adler2; | |
146 z_off64_t len2; | |
147 { | |
148 unsigned long sum1; | |
149 unsigned long sum2; | |
150 unsigned rem; | |
151 | |
152 /* for negative len, return invalid adler32 as a clue for debugging */ | |
153 if (len2 < 0) | |
154 return 0xffffffffUL; | |
155 | |
156 /* the derivation of this formula is left as an exercise for the reader */ | |
157 MOD63(len2); /* assumes len2 >= 0 */ | |
158 rem = (unsigned)len2; | |
159 sum1 = adler1 & 0xffff; | |
160 sum2 = rem * sum1; | |
161 MOD(sum2); | |
162 sum1 += (adler2 & 0xffff) + BASE - 1; | |
163 sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; | |
164 if (sum1 >= BASE) sum1 -= BASE; | |
165 if (sum1 >= BASE) sum1 -= BASE; | |
166 if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1); | |
167 if (sum2 >= BASE) sum2 -= BASE; | |
168 return sum1 | (sum2 << 16); | |
169 } | |
170 | |
171 /* ========================================================================= */ | |
128 uLong ZEXPORT adler32_combine(adler1, adler2, len2) | 172 uLong ZEXPORT adler32_combine(adler1, adler2, len2) |
129 uLong adler1; | 173 uLong adler1; |
130 uLong adler2; | 174 uLong adler2; |
131 z_off_t len2; | 175 z_off_t len2; |
132 { | 176 { |
133 unsigned long sum1; | 177 return adler32_combine_(adler1, adler2, len2); |
134 unsigned long sum2; | 178 } |
135 unsigned rem; | |
136 | 179 |
137 /* the derivation of this formula is left as an exercise for the reader */ | 180 uLong ZEXPORT adler32_combine64(adler1, adler2, len2) |
138 rem = (unsigned)(len2 % BASE); | 181 uLong adler1; |
139 sum1 = adler1 & 0xffff; | 182 uLong adler2; |
140 sum2 = rem * sum1; | 183 z_off64_t len2; |
141 MOD(sum2); | 184 { |
142 sum1 += (adler2 & 0xffff) + BASE - 1; | 185 return adler32_combine_(adler1, adler2, len2); |
143 sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; | |
144 if (sum1 > BASE) sum1 -= BASE; | |
145 if (sum1 > BASE) sum1 -= BASE; | |
146 if (sum2 > (BASE << 1)) sum2 -= (BASE << 1); | |
147 if (sum2 > BASE) sum2 -= BASE; | |
148 return sum1 | (sum2 << 16); | |
149 } | 186 } |