os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/adler32.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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 "zlib.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
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
sl@0
    22
{
sl@0
    23
    unsigned long s1 = adler & 0xffff;
sl@0
    24
    unsigned long s2 = (adler >> 16) & 0xffff;
sl@0
    25
    int k;
sl@0
    26
sl@0
    27
    if (buf == Z_NULL) return 1L;
sl@0
    28
sl@0
    29
    while (len > 0) {
sl@0
    30
        k = len < NMAX ? len : NMAX;
sl@0
    31
        len -= k;
sl@0
    32
        while (k >= 16) {
sl@0
    33
            DO16(buf);
sl@0
    34
	    buf += 16;
sl@0
    35
            k -= 16;
sl@0
    36
        }
sl@0
    37
        if (k != 0) do {
sl@0
    38
            s1 += *buf++;
sl@0
    39
	    s2 += s1;
sl@0
    40
        } while (--k);
sl@0
    41
        s1 %= BASE;
sl@0
    42
        s2 %= BASE;
sl@0
    43
    }
sl@0
    44
    return (s2 << 16) | s1;
sl@0
    45
}