abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
adler32.c
Go to the documentation of this file.
1 /* adler32.c -- compute the Adler-32 checksum of a data stream
2  * Copyright (C) 1995-2007 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 /* @(#) $Id$ */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "misc/util/abc_global.h"
12 
13 #include "zutil.h"
14 
16 
17 #define local static
18 
19 local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
20 
21 #define BASE 65521UL /* largest prime smaller than 65536 */
22 #define NMAX 5552
23 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
24 
25 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
26 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
27 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
28 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
29 #define DO16(buf) DO8(buf,0); DO8(buf,8);
30 
31 /* use NO_DIVIDE if your processor does not do division in hardware */
32 #ifdef NO_DIVIDE
33 # define MOD(a) \
34  do { \
35  if (a >= (BASE << 16)) a -= (BASE << 16); \
36  if (a >= (BASE << 15)) a -= (BASE << 15); \
37  if (a >= (BASE << 14)) a -= (BASE << 14); \
38  if (a >= (BASE << 13)) a -= (BASE << 13); \
39  if (a >= (BASE << 12)) a -= (BASE << 12); \
40  if (a >= (BASE << 11)) a -= (BASE << 11); \
41  if (a >= (BASE << 10)) a -= (BASE << 10); \
42  if (a >= (BASE << 9)) a -= (BASE << 9); \
43  if (a >= (BASE << 8)) a -= (BASE << 8); \
44  if (a >= (BASE << 7)) a -= (BASE << 7); \
45  if (a >= (BASE << 6)) a -= (BASE << 6); \
46  if (a >= (BASE << 5)) a -= (BASE << 5); \
47  if (a >= (BASE << 4)) a -= (BASE << 4); \
48  if (a >= (BASE << 3)) a -= (BASE << 3); \
49  if (a >= (BASE << 2)) a -= (BASE << 2); \
50  if (a >= (BASE << 1)) a -= (BASE << 1); \
51  if (a >= BASE) a -= BASE; \
52  } while (0)
53 # define MOD4(a) \
54  do { \
55  if (a >= (BASE << 4)) a -= (BASE << 4); \
56  if (a >= (BASE << 3)) a -= (BASE << 3); \
57  if (a >= (BASE << 2)) a -= (BASE << 2); \
58  if (a >= (BASE << 1)) a -= (BASE << 1); \
59  if (a >= BASE) a -= BASE; \
60  } while (0)
61 #else
62 # define MOD(a) a %= BASE
63 # define MOD4(a) a %= BASE
64 #endif
65 
66 /* ========================================================================= */
67 uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt 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  MOD4(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 /* ========================================================================= */
136 {
137  unsigned long sum1;
138  unsigned long sum2;
139  unsigned rem;
140 
141  /* the derivation of this formula is left as an exercise for the reader */
142  rem = (unsigned)(len2 % BASE);
143  sum1 = adler1 & 0xffff;
144  sum2 = rem * sum1;
145  MOD(sum2);
146  sum1 += (adler2 & 0xffff) + BASE - 1;
147  sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
148  if (sum1 >= BASE) sum1 -= BASE;
149  if (sum1 >= BASE) sum1 -= BASE;
150  if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
151  if (sum2 >= BASE) sum2 -= BASE;
152  return sum1 | (sum2 << 16);
153 }
154 
155 /* ========================================================================= */
157 {
158  return adler32_combine_(adler1, adler2, len2);
159 }
160 
162 {
163  return adler32_combine_(adler1, adler2, len2);
164 }
165 
166 
168 
#define MOD(a)
Definition: adler32.c:62
#define DO16(buf)
Definition: adler32.c:29
#define MOD4(a)
Definition: adler32.c:63
#define BASE
Definition: adler32.c:21
#define z_off_t
Definition: zconf.h:396
#define z_off64_t
Definition: zconf.h:402
#define NMAX
Definition: adler32.c:22
unsigned long uLong
Definition: zconf.h:336
Byte FAR Bytef
Definition: zconf.h:342
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define local
Definition: adler32.c:17
uLong ZEXPORT adler32_combine64(uLong adler1, uLong adler2, z_off64_t len2)
Definition: adler32.c:161
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:67
#define Z_NULL
Definition: zlib.h:216
local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2)
Definition: adler32.c:135
uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, z_off_t len2)
Definition: adler32.c:156
#define ZEXPORT
Definition: zconf.h:322
unsigned int uInt
Definition: zconf.h:335