abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
adler32.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "zutil.h"

Go to the source code of this file.

Macros

#define local   static
 
#define BASE   65521UL /* largest prime smaller than 65536 */
 
#define NMAX   5552
 
#define DO1(buf, i)   {adler += (buf)[i]; sum2 += adler;}
 
#define DO2(buf, i)   DO1(buf,i); DO1(buf,i+1);
 
#define DO4(buf, i)   DO2(buf,i); DO2(buf,i+2);
 
#define DO8(buf, i)   DO4(buf,i); DO4(buf,i+4);
 
#define DO16(buf)   DO8(buf,0); DO8(buf,8);
 
#define MOD(a)   a %= BASE
 
#define MOD4(a)   a %= BASE
 

Functions

local uLong adler32_combine_ (uLong adler1, uLong adler2, z_off64_t len2)
 
uLong ZEXPORT adler32 (uLong adler, const Bytef *buf, uInt len)
 
uLong ZEXPORT adler32_combine (uLong adler1, uLong adler2, z_off_t len2)
 
uLong ZEXPORT adler32_combine64 (uLong adler1, uLong adler2, z_off64_t len2)
 

Macro Definition Documentation

#define BASE   65521UL /* largest prime smaller than 65536 */

Definition at line 21 of file adler32.c.

#define DO1 (   buf,
 
)    {adler += (buf)[i]; sum2 += adler;}

Definition at line 25 of file adler32.c.

#define DO16 (   buf)    DO8(buf,0); DO8(buf,8);

Definition at line 29 of file adler32.c.

#define DO2 (   buf,
 
)    DO1(buf,i); DO1(buf,i+1);

Definition at line 26 of file adler32.c.

#define DO4 (   buf,
 
)    DO2(buf,i); DO2(buf,i+2);

Definition at line 27 of file adler32.c.

#define DO8 (   buf,
 
)    DO4(buf,i); DO4(buf,i+4);

Definition at line 28 of file adler32.c.

#define local   static

Definition at line 17 of file adler32.c.

#define MOD (   a)    a %= BASE

Definition at line 62 of file adler32.c.

#define MOD4 (   a)    a %= BASE

Definition at line 63 of file adler32.c.

#define NMAX   5552

Definition at line 22 of file adler32.c.

Function Documentation

uLong ZEXPORT adler32 ( uLong  adler,
const Bytef buf,
uInt  len 
)

Definition at line 67 of file adler32.c.

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 }
#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 NMAX
Definition: adler32.c:22
#define Z_NULL
Definition: zlib.h:216
uLong ZEXPORT adler32_combine ( uLong  adler1,
uLong  adler2,
z_off_t  len2 
)

Definition at line 156 of file adler32.c.

157 {
158  return adler32_combine_(adler1, adler2, len2);
159 }
local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2)
Definition: adler32.c:135
uLong ZEXPORT adler32_combine64 ( uLong  adler1,
uLong  adler2,
z_off64_t  len2 
)

Definition at line 161 of file adler32.c.

162 {
163  return adler32_combine_(adler1, adler2, len2);
164 }
local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2)
Definition: adler32.c:135
local uLong adler32_combine_ ( uLong  adler1,
uLong  adler2,
z_off64_t  len2 
)

Definition at line 135 of file adler32.c.

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 }
#define MOD(a)
Definition: adler32.c:62
#define BASE
Definition: adler32.c:21