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