adler32.c

00001 /* adler32.c -- compute the Adler-32 checksum of a data stream
00002  * Copyright (C) 1995-2002 Mark Adler
00003  * For conditions of distribution and use, see copyright notice in zlib.h 
00004  */
00005 
00006 /* @(#) $Id: adler32.c,v 1.1 2003/11/09 01:37:56 gabest Exp $ */
00007 
00008 #include "zlib.h"
00009 
00010 #define BASE 65521L /* largest prime smaller than 65536 */
00011 #define NMAX 5552
00012 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
00013 
00014 #define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
00015 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
00016 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
00017 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
00018 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
00019 
00020 /* ========================================================================= */
00021 uLong ZEXPORT adler32(adler, buf, len)
00022     uLong adler;
00023     const Bytef *buf;
00024     uInt len;
00025 {
00026     unsigned long s1 = adler & 0xffff;
00027     unsigned long s2 = (adler >> 16) & 0xffff;
00028     int k;
00029 
00030     if (buf == Z_NULL) return 1L;
00031 
00032     while (len > 0) {
00033         k = len < NMAX ? len : NMAX;
00034         len -= k;
00035         while (k >= 16) {
00036             DO16(buf);
00037             buf += 16;
00038             k -= 16;
00039         }
00040         if (k != 0) do {
00041             s1 += *buf++;
00042             s2 += s1;
00043         } while (--k);
00044         s1 %= BASE;
00045         s2 %= BASE;
00046     }
00047     return (s2 << 16) | s1;
00048 }

Generated on Tue Dec 13 14:47:58 2005 for guliverkli by  doxygen 1.4.5