adler32.c revision 131380
119370Spst/* adler32.c -- compute the Adler-32 checksum of a data stream
2130803Smarcel * Copyright (C) 1995-2003 Mark Adler
3130803Smarcel * For conditions of distribution and use, see copyright notice in zlib.h
4130803Smarcel */
5130803Smarcel
619370Spst#include <sys/cdefs.h>
719370Spst__FBSDID("$FreeBSD: head/lib/libz/adler32.c 131380 2004-06-30 23:54:46Z tjr $");
819370Spst
998944Sobrien#define ZLIB_INTERNAL
1019370Spst#include "zlib.h"
1198944Sobrien
1298944Sobrien#define BASE 65521UL    /* largest prime smaller than 65536 */
1398944Sobrien#define NMAX 5552
1498944Sobrien/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
1519370Spst
1698944Sobrien#define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
1798944Sobrien#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
1898944Sobrien#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
1998944Sobrien#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
2019370Spst#define DO16(buf)   DO8(buf,0); DO8(buf,8);
2198944Sobrien
2298944Sobrien#ifdef NO_DIVIDE
2398944Sobrien#  define MOD(a) \
2498944Sobrien    do { \
2519370Spst        if (a >= (BASE << 16)) a -= (BASE << 16); \
2619370Spst        if (a >= (BASE << 15)) a -= (BASE << 15); \
2719370Spst        if (a >= (BASE << 14)) a -= (BASE << 14); \
2819370Spst        if (a >= (BASE << 13)) a -= (BASE << 13); \
2998944Sobrien        if (a >= (BASE << 12)) a -= (BASE << 12); \
3019370Spst        if (a >= (BASE << 11)) a -= (BASE << 11); \
31130803Smarcel        if (a >= (BASE << 10)) a -= (BASE << 10); \
3298944Sobrien        if (a >= (BASE << 9)) a -= (BASE << 9); \
3319370Spst        if (a >= (BASE << 8)) a -= (BASE << 8); \
3419370Spst        if (a >= (BASE << 7)) a -= (BASE << 7); \
3519370Spst        if (a >= (BASE << 6)) a -= (BASE << 6); \
3619370Spst        if (a >= (BASE << 5)) a -= (BASE << 5); \
3719370Spst        if (a >= (BASE << 4)) a -= (BASE << 4); \
3819370Spst        if (a >= (BASE << 3)) a -= (BASE << 3); \
3919370Spst        if (a >= (BASE << 2)) a -= (BASE << 2); \
4019370Spst        if (a >= (BASE << 1)) a -= (BASE << 1); \
4119370Spst        if (a >= BASE) a -= BASE; \
42130803Smarcel    } while (0)
43130803Smarcel#else
44130803Smarcel#  define MOD(a) a %= BASE
45130803Smarcel#endif
46130803Smarcel
47130803Smarcel/* ========================================================================= */
4819370SpstuLong ZEXPORT adler32(adler, buf, len)
4919370Spst    uLong adler;
5019370Spst    const Bytef *buf;
5119370Spst    uInt len;
5219370Spst{
5319370Spst    unsigned long s1 = adler & 0xffff;
5419370Spst    unsigned long s2 = (adler >> 16) & 0xffff;
5519370Spst    int k;
5619370Spst
5719370Spst    if (buf == Z_NULL) return 1L;
5819370Spst
5919370Spst    while (len > 0) {
6019370Spst        k = len < NMAX ? (int)len : NMAX;
6119370Spst        len -= k;
6219370Spst        while (k >= 16) {
6319370Spst            DO16(buf);
6419370Spst            buf += 16;
6519370Spst            k -= 16;
6619370Spst        }
6719370Spst        if (k != 0) do {
6819370Spst            s1 += *buf++;
6919370Spst            s2 += s1;
7019370Spst        } while (--k);
7119370Spst        MOD(s1);
7219370Spst        MOD(s2);
7319370Spst    }
7419370Spst    return (s2 << 16) | s1;
7519370Spst}
7619370Spst