xref: /titanic_50/usr/src/uts/common/zmod/adler32.c (revision 711890bc9379ceea66272dc8d4981812224ea86e)
1 /*
2  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * adler32.c -- compute the Adler-32 checksum of a data stream
10  * Copyright (C) 1995-1998 Mark Adler
11  * For conditions of distribution and use, see copyright notice in zlib.h
12  */
13 
14 #include "zlib.h"
15 
16 #define BASE 65521L /* largest prime smaller than 65536 */
17 #define NMAX 5552
18 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
19 
20 #define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
21 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
22 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
23 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
24 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
25 
26 uLong ZEXPORT adler32(adler, buf, len)
27     uLong adler;
28     const Bytef *buf;
29     uInt len;
30 {
31     unsigned long s1 = adler & 0xffff;
32     unsigned long s2 = (adler >> 16) & 0xffff;
33     int k;
34 
35     if (buf == Z_NULL) return 1L;
36 
37     while (len > 0) {
38         k = len < NMAX ? len : NMAX;
39         len -= k;
40         while (k >= 16) {
41             DO16(buf);
42 	    buf += 16;
43             k -= 16;
44         }
45         if (k != 0) do {
46             s1 += *buf++;
47 	    s2 += s1;
48         } while (--k);
49         s1 %= BASE;
50         s2 %= BASE;
51     }
52     return (s2 << 16) | s1;
53 }
54