1 /* adler32.c -- compute the Adler-32 checksum of a data stream
2 * Copyright (C) 1995-2011, 2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 /* @(#) $Id: adler32.c,v 1.1.1.2 2002/03/11 21:53:23 tromey Exp $ */
10 local uLong adler32_combine_
OF((uLong adler1
, uLong adler2
, z_off64_t len2
));
12 #define BASE 65521U /* 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 --
23 try it both ways to see which is faster */
25 /* note that this assumes BASE is 65521, where 65536 % 65521 == 15
26 (thank you to John Reiser for pointing this out) */
29 unsigned long tmp = a >> 16; \
31 a += (tmp << 4) - tmp; \
36 if (a >= BASE) a -= BASE; \
44 do { /* this assumes a is not negative */ \
45 z_off64_t tmp = a >> 32; \
47 a += (tmp << 8) - (tmp << 5) + tmp; \
50 a += (tmp << 4) - tmp; \
53 a += (tmp << 4) - tmp; \
54 if (a >= BASE) a -= BASE; \
57 # define MOD(a) a %= BASE
58 # define MOD28(a) a %= BASE
59 # define MOD63(a) a %= BASE
62 /* ========================================================================= */
63 uLong ZEXPORT
adler32_z(adler
, buf
, len
)
71 /* split Adler-32 into component sums */
72 sum2
= (adler
>> 16) & 0xffff;
75 /* in case user likes doing a byte at a time, keep it fast */
83 return adler
| (sum2
<< 16);
86 /* initial Adler-32 value (deferred check for len == 1 speed) */
90 /* in case short lengths are provided, keep it somewhat fast */
98 MOD28(sum2
); /* only added so many BASE's */
99 return adler
| (sum2
<< 16);
102 /* do length NMAX blocks -- requires just one modulo operation */
103 while (len
>= NMAX
) {
105 n
= NMAX
/ 16; /* NMAX is divisible by 16 */
107 DO16(buf
); /* 16 sums unrolled */
114 /* do remaining bytes (less than NMAX, still just one modulo) */
115 if (len
) { /* avoid modulos if none remaining */
129 /* return recombined sums */
130 return adler
| (sum2
<< 16);
133 /* ========================================================================= */
134 uLong ZEXPORT
adler32(adler
, buf
, len
)
139 return adler32_z(adler
, buf
, len
);
142 /* ========================================================================= */
143 local uLong
adler32_combine_(adler1
, adler2
, len2
)
152 /* for negative len, return invalid adler32 as a clue for debugging */
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;
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);
171 /* ========================================================================= */
172 uLong ZEXPORT
adler32_combine(adler1
, adler2
, len2
)
177 return adler32_combine_(adler1
, adler2
, len2
);
180 uLong ZEXPORT
adler32_combine64(adler1
, adler2
, len2
)
185 return adler32_combine_(adler1
, adler2
, len2
);