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