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