annotate src/zlib-1.2.7/adler32.c @ 169:223a55898ab9 tip default

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