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