/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2011 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zutil.h"
#define local static
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
/* use NO_DIVIDE if your processor does not do division in hardware --
try it both ways to see which is faster */
#ifdef NO_DIVIDE
/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
(thank you to John Reiser for pointing this out) */
# define CHOP(a) \
do { \
unsigned long tmp = a >> 16; \
a &= 0xffffUL; \
} while (0)
# define MOD28(a) \
do { \
CHOP(a); \
} while (0)
# define MOD(a) \
do { \
CHOP(a); \
MOD28(a); \
} while (0)
# define MOD63(a) \
do { /* this assumes a is not negative */ \
a &= 0xffffffffL; \
tmp = a >> 16; \
a &= 0xffffL; \
tmp = a >> 16; \
a &= 0xffffL; \
} while (0)
#else
#endif
/* ========================================================================= */
{
unsigned long sum2;
unsigned n;
/* split Adler-32 into component sums */
adler &= 0xffff;
/* in case user likes doing a byte at a time, keep it fast */
if (len == 1) {
}
/* initial Adler-32 value (deferred check for len == 1 speed) */
return 1L;
/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
while (len--) {
}
}
/* do length NMAX blocks -- requires just one modulo operation */
do {
buf += 16;
} while (--n);
}
/* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */
while (len >= 16) {
len -= 16;
buf += 16;
}
while (len--) {
}
}
/* return recombined sums */
}
/* ========================================================================= */
{
unsigned long sum1;
unsigned long sum2;
unsigned rem;
/* for negative len, return invalid adler32 as a clue for debugging */
if (len2 < 0)
return 0xffffffffUL;
/* the derivation of this formula is left as an exercise for the reader */
}
/* ========================================================================= */
{
}
{
}