os/ossrv/compressionlibs/ziplib/test/oldezlib/EZLib/adler32.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* adler32.c -- compute the Adler-32 checksum of a data stream
sl@0
     2
 * Copyright (C) 1995-1998 Mark Adler
sl@0
     3
 * For conditions of distribution and use, see copyright notice in zlib.h 
sl@0
     4
 */
sl@0
     5
sl@0
     6
/* @(#) $Id$ */
sl@0
     7
sl@0
     8
#include "OldEZlib.h"
sl@0
     9
sl@0
    10
#define BASE 65521L /* largest prime smaller than 65536 */
sl@0
    11
#define NMAX 5552
sl@0
    12
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
sl@0
    13
sl@0
    14
#define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
sl@0
    15
#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
sl@0
    16
#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
sl@0
    17
#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
sl@0
    18
#define DO16(buf)   DO8(buf,0); DO8(buf,8);
sl@0
    19
sl@0
    20
/* ========================================================================= */
sl@0
    21
EXPORT_C uLong ZEXPORT adler32(
sl@0
    22
    uLong adler,
sl@0
    23
    const Bytef *buf,
sl@0
    24
    uInt len)
sl@0
    25
{
sl@0
    26
    unsigned long s1 = adler & 0xffff;
sl@0
    27
    unsigned long s2 = (adler >> 16) & 0xffff;
sl@0
    28
    int k;
sl@0
    29
sl@0
    30
    if (buf == Z_NULL) return 1L;
sl@0
    31
sl@0
    32
    while (len > 0) {
sl@0
    33
        k = len < NMAX ? len : NMAX;
sl@0
    34
        len -= k;
sl@0
    35
        while (k >= 16) {
sl@0
    36
            DO16(buf);
sl@0
    37
	    buf += 16;
sl@0
    38
            k -= 16;
sl@0
    39
        }
sl@0
    40
        if (k != 0) do {
sl@0
    41
            s1 += *buf++;
sl@0
    42
	    s2 += s1;
sl@0
    43
        } while (--k);
sl@0
    44
        s1 %= BASE;
sl@0
    45
        s2 %= BASE;
sl@0
    46
    }
sl@0
    47
    return (s2 << 16) | s1;
sl@0
    48
}