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