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