1*c9431fa1Sahl /* adler32.c -- compute the Adler-32 checksum of a data stream
2*c9431fa1Sahl * Copyright (C) 1995-2004 Mark Adler
3*c9431fa1Sahl * For conditions of distribution and use, see copyright notice in zlib.h
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
77c478bd9Sstevel@tonic-gate
8*c9431fa1Sahl #define ZLIB_INTERNAL
97c478bd9Sstevel@tonic-gate #include "zlib.h"
107c478bd9Sstevel@tonic-gate
11*c9431fa1Sahl #define BASE 65521UL /* largest prime smaller than 65536 */
127c478bd9Sstevel@tonic-gate #define NMAX 5552
137c478bd9Sstevel@tonic-gate /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
147c478bd9Sstevel@tonic-gate
15*c9431fa1Sahl #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
167c478bd9Sstevel@tonic-gate #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
177c478bd9Sstevel@tonic-gate #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
187c478bd9Sstevel@tonic-gate #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
197c478bd9Sstevel@tonic-gate #define DO16(buf) DO8(buf,0); DO8(buf,8);
207c478bd9Sstevel@tonic-gate
21*c9431fa1Sahl /* use NO_DIVIDE if your processor does not do division in hardware */
22*c9431fa1Sahl #ifdef NO_DIVIDE
23*c9431fa1Sahl # define MOD(a) \
24*c9431fa1Sahl do { \
25*c9431fa1Sahl if (a >= (BASE << 16)) a -= (BASE << 16); \
26*c9431fa1Sahl if (a >= (BASE << 15)) a -= (BASE << 15); \
27*c9431fa1Sahl if (a >= (BASE << 14)) a -= (BASE << 14); \
28*c9431fa1Sahl if (a >= (BASE << 13)) a -= (BASE << 13); \
29*c9431fa1Sahl if (a >= (BASE << 12)) a -= (BASE << 12); \
30*c9431fa1Sahl if (a >= (BASE << 11)) a -= (BASE << 11); \
31*c9431fa1Sahl if (a >= (BASE << 10)) a -= (BASE << 10); \
32*c9431fa1Sahl if (a >= (BASE << 9)) a -= (BASE << 9); \
33*c9431fa1Sahl if (a >= (BASE << 8)) a -= (BASE << 8); \
34*c9431fa1Sahl if (a >= (BASE << 7)) a -= (BASE << 7); \
35*c9431fa1Sahl if (a >= (BASE << 6)) a -= (BASE << 6); \
36*c9431fa1Sahl if (a >= (BASE << 5)) a -= (BASE << 5); \
37*c9431fa1Sahl if (a >= (BASE << 4)) a -= (BASE << 4); \
38*c9431fa1Sahl if (a >= (BASE << 3)) a -= (BASE << 3); \
39*c9431fa1Sahl if (a >= (BASE << 2)) a -= (BASE << 2); \
40*c9431fa1Sahl if (a >= (BASE << 1)) a -= (BASE << 1); \
41*c9431fa1Sahl if (a >= BASE) a -= BASE; \
42*c9431fa1Sahl } while (0)
43*c9431fa1Sahl # define MOD4(a) \
44*c9431fa1Sahl do { \
45*c9431fa1Sahl if (a >= (BASE << 4)) a -= (BASE << 4); \
46*c9431fa1Sahl if (a >= (BASE << 3)) a -= (BASE << 3); \
47*c9431fa1Sahl if (a >= (BASE << 2)) a -= (BASE << 2); \
48*c9431fa1Sahl if (a >= (BASE << 1)) a -= (BASE << 1); \
49*c9431fa1Sahl if (a >= BASE) a -= BASE; \
50*c9431fa1Sahl } while (0)
51*c9431fa1Sahl #else
52*c9431fa1Sahl # define MOD(a) a %= BASE
53*c9431fa1Sahl # define MOD4(a) a %= BASE
54*c9431fa1Sahl #endif
55*c9431fa1Sahl
56*c9431fa1Sahl /* ========================================================================= */
adler32(adler,buf,len)577c478bd9Sstevel@tonic-gate uLong ZEXPORT adler32(adler, buf, len)
587c478bd9Sstevel@tonic-gate uLong adler;
597c478bd9Sstevel@tonic-gate const Bytef *buf;
607c478bd9Sstevel@tonic-gate uInt len;
617c478bd9Sstevel@tonic-gate {
62*c9431fa1Sahl unsigned long sum2;
63*c9431fa1Sahl unsigned n;
647c478bd9Sstevel@tonic-gate
65*c9431fa1Sahl /* split Adler-32 into component sums */
66*c9431fa1Sahl sum2 = (adler >> 16) & 0xffff;
67*c9431fa1Sahl adler &= 0xffff;
687c478bd9Sstevel@tonic-gate
69*c9431fa1Sahl /* in case user likes doing a byte at a time, keep it fast */
70*c9431fa1Sahl if (len == 1) {
71*c9431fa1Sahl adler += buf[0];
72*c9431fa1Sahl if (adler >= BASE)
73*c9431fa1Sahl adler -= BASE;
74*c9431fa1Sahl sum2 += adler;
75*c9431fa1Sahl if (sum2 >= BASE)
76*c9431fa1Sahl sum2 -= BASE;
77*c9431fa1Sahl return adler | (sum2 << 16);
78*c9431fa1Sahl }
79*c9431fa1Sahl
80*c9431fa1Sahl /* initial Adler-32 value (deferred check for len == 1 speed) */
81*c9431fa1Sahl if (buf == Z_NULL)
82*c9431fa1Sahl return 1L;
83*c9431fa1Sahl
84*c9431fa1Sahl /* in case short lengths are provided, keep it somewhat fast */
85*c9431fa1Sahl if (len < 16) {
86*c9431fa1Sahl while (len--) {
87*c9431fa1Sahl adler += *buf++;
88*c9431fa1Sahl sum2 += adler;
89*c9431fa1Sahl }
90*c9431fa1Sahl if (adler >= BASE)
91*c9431fa1Sahl adler -= BASE;
92*c9431fa1Sahl MOD4(sum2); /* only added so many BASE's */
93*c9431fa1Sahl return adler | (sum2 << 16);
94*c9431fa1Sahl }
95*c9431fa1Sahl
96*c9431fa1Sahl /* do length NMAX blocks -- requires just one modulo operation */
97*c9431fa1Sahl while (len >= NMAX) {
98*c9431fa1Sahl len -= NMAX;
99*c9431fa1Sahl n = NMAX / 16; /* NMAX is divisible by 16 */
100*c9431fa1Sahl do {
101*c9431fa1Sahl DO16(buf); /* 16 sums unrolled */
102*c9431fa1Sahl buf += 16;
103*c9431fa1Sahl } while (--n);
104*c9431fa1Sahl MOD(adler);
105*c9431fa1Sahl MOD(sum2);
106*c9431fa1Sahl }
107*c9431fa1Sahl
108*c9431fa1Sahl /* do remaining bytes (less than NMAX, still just one modulo) */
109*c9431fa1Sahl if (len) { /* avoid modulos if none remaining */
110*c9431fa1Sahl while (len >= 16) {
111*c9431fa1Sahl len -= 16;
1127c478bd9Sstevel@tonic-gate DO16(buf);
1137c478bd9Sstevel@tonic-gate buf += 16;
1147c478bd9Sstevel@tonic-gate }
115*c9431fa1Sahl while (len--) {
116*c9431fa1Sahl adler += *buf++;
117*c9431fa1Sahl sum2 += adler;
1187c478bd9Sstevel@tonic-gate }
119*c9431fa1Sahl MOD(adler);
120*c9431fa1Sahl MOD(sum2);
121*c9431fa1Sahl }
122*c9431fa1Sahl
123*c9431fa1Sahl /* return recombined sums */
124*c9431fa1Sahl return adler | (sum2 << 16);
125*c9431fa1Sahl }
126*c9431fa1Sahl
127*c9431fa1Sahl /* ========================================================================= */
adler32_combine(adler1,adler2,len2)128*c9431fa1Sahl uLong ZEXPORT adler32_combine(adler1, adler2, len2)
129*c9431fa1Sahl uLong adler1;
130*c9431fa1Sahl uLong adler2;
131*c9431fa1Sahl z_off_t len2;
132*c9431fa1Sahl {
133*c9431fa1Sahl unsigned long sum1;
134*c9431fa1Sahl unsigned long sum2;
135*c9431fa1Sahl unsigned rem;
136*c9431fa1Sahl
137*c9431fa1Sahl /* the derivation of this formula is left as an exercise for the reader */
138*c9431fa1Sahl rem = (unsigned)(len2 % BASE);
139*c9431fa1Sahl sum1 = adler1 & 0xffff;
140*c9431fa1Sahl sum2 = rem * sum1;
141*c9431fa1Sahl MOD(sum2);
142*c9431fa1Sahl sum1 += (adler2 & 0xffff) + BASE - 1;
143*c9431fa1Sahl sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
144*c9431fa1Sahl if (sum1 > BASE) sum1 -= BASE;
145*c9431fa1Sahl if (sum1 > BASE) sum1 -= BASE;
146*c9431fa1Sahl if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
147*c9431fa1Sahl if (sum2 > BASE) sum2 -= BASE;
148*c9431fa1Sahl return sum1 | (sum2 << 16);
1497c478bd9Sstevel@tonic-gate }
150