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