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