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