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