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