security/nss/lib/zlib/adler32.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial