]> git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/adler32.c
Security-30.1.tar.gz
[apple/security.git] / SecurityServer / MacYarrow / zlib / adler32.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /* adler32.c -- compute the Adler-32 checksum of a data stream
20 * Copyright (C) 1995-1998 Mark Adler
21 * For conditions of distribution and use, see copyright notice in zlib.h
22 */
23
24 /* @(#) $Id: adler32.c,v 1.1.1.1 2001/05/18 23:14:03 mb Exp $ */
25
26 #include "zlib.h"
27
28 #define BASE 65521L /* largest prime smaller than 65536 */
29 #define NMAX 5552
30 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
31
32 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
33 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
34 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
35 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
36 #define DO16(buf) DO8(buf,0); DO8(buf,8);
37
38 /* ========================================================================= */
39 uLong ZEXPORT adler32(adler, buf, len)
40 uLong adler;
41 const Bytef *buf;
42 uInt len;
43 {
44 unsigned long s1 = adler & 0xffff;
45 unsigned long s2 = (adler >> 16) & 0xffff;
46 int k;
47
48 if (buf == Z_NULL) return 1L;
49
50 while (len > 0) {
51 k = len < NMAX ? len : NMAX;
52 len -= k;
53 while (k >= 16) {
54 DO16(buf);
55 buf += 16;
56 k -= 16;
57 }
58 if (k != 0) do {
59 s1 += *buf++;
60 s2 += s1;
61 } while (--k);
62 s1 %= BASE;
63 s2 %= BASE;
64 }
65 return (s2 << 16) | s1;
66 }