sl@0: /* adler32.c -- compute the Adler-32 checksum of a data stream sl@0: * Copyright (C) 1995-1998 Mark Adler sl@0: * For conditions of distribution and use, see copyright notice in zlib.h sl@0: */ sl@0: sl@0: /* @(#) $Id$ */ sl@0: sl@0: #include "zlib.h" sl@0: sl@0: #define BASE 65521L /* largest prime smaller than 65536 */ sl@0: #define NMAX 5552 sl@0: /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ sl@0: sl@0: #define DO1(buf,i) {s1 += buf[i]; s2 += s1;} sl@0: #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); sl@0: #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); sl@0: #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); sl@0: #define DO16(buf) DO8(buf,0); DO8(buf,8); sl@0: sl@0: /* ========================================================================= */ sl@0: uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len) sl@0: { sl@0: unsigned long s1 = adler & 0xffff; sl@0: unsigned long s2 = (adler >> 16) & 0xffff; sl@0: int k; sl@0: sl@0: if (buf == Z_NULL) return 1L; sl@0: sl@0: while (len > 0) { sl@0: k = len < NMAX ? len : NMAX; sl@0: len -= k; sl@0: while (k >= 16) { sl@0: DO16(buf); sl@0: buf += 16; sl@0: k -= 16; sl@0: } sl@0: if (k != 0) do { sl@0: s1 += *buf++; sl@0: s2 += s1; sl@0: } while (--k); sl@0: s1 %= BASE; sl@0: s2 %= BASE; sl@0: } sl@0: return (s2 << 16) | s1; sl@0: }