annotate lib/zlib/adler32.c @ 0:9c0607679772

init
author Ritor1
date Sat, 12 Jan 2013 09:45:18 +0600
parents
children
rev   line source
0
Ritor1
parents:
diff changeset
1 /* adler32.c -- compute the Adler-32 checksum of a data stream
Ritor1
parents:
diff changeset
2 * Copyright (C) 1995-1998 Mark Adler
Ritor1
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
Ritor1
parents:
diff changeset
4 */
Ritor1
parents:
diff changeset
5
Ritor1
parents:
diff changeset
6 /* @(#) $Id$ */
Ritor1
parents:
diff changeset
7
Ritor1
parents:
diff changeset
8 #include "zlib.h"
Ritor1
parents:
diff changeset
9
Ritor1
parents:
diff changeset
10 #define BASE 65521L /* largest prime smaller than 65536 */
Ritor1
parents:
diff changeset
11 #define NMAX 5552
Ritor1
parents:
diff changeset
12 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
Ritor1
parents:
diff changeset
13
Ritor1
parents:
diff changeset
14 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
Ritor1
parents:
diff changeset
15 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
Ritor1
parents:
diff changeset
16 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
Ritor1
parents:
diff changeset
17 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
Ritor1
parents:
diff changeset
18 #define DO16(buf) DO8(buf,0); DO8(buf,8);
Ritor1
parents:
diff changeset
19
Ritor1
parents:
diff changeset
20 /* ========================================================================= */
Ritor1
parents:
diff changeset
21 uLong ZEXPORT adler32(adler, buf, len)
Ritor1
parents:
diff changeset
22 uLong adler;
Ritor1
parents:
diff changeset
23 const Bytef *buf;
Ritor1
parents:
diff changeset
24 uInt len;
Ritor1
parents:
diff changeset
25 {
Ritor1
parents:
diff changeset
26 unsigned long s1 = adler & 0xffff;
Ritor1
parents:
diff changeset
27 unsigned long s2 = (adler >> 16) & 0xffff;
Ritor1
parents:
diff changeset
28 int k;
Ritor1
parents:
diff changeset
29
Ritor1
parents:
diff changeset
30 if (buf == Z_NULL) return 1L;
Ritor1
parents:
diff changeset
31
Ritor1
parents:
diff changeset
32 while (len > 0) {
Ritor1
parents:
diff changeset
33 k = len < NMAX ? len : NMAX;
Ritor1
parents:
diff changeset
34 len -= k;
Ritor1
parents:
diff changeset
35 while (k >= 16) {
Ritor1
parents:
diff changeset
36 DO16(buf);
Ritor1
parents:
diff changeset
37 buf += 16;
Ritor1
parents:
diff changeset
38 k -= 16;
Ritor1
parents:
diff changeset
39 }
Ritor1
parents:
diff changeset
40 if (k != 0) do {
Ritor1
parents:
diff changeset
41 s1 += *buf++;
Ritor1
parents:
diff changeset
42 s2 += s1;
Ritor1
parents:
diff changeset
43 } while (--k);
Ritor1
parents:
diff changeset
44 s1 %= BASE;
Ritor1
parents:
diff changeset
45 s2 %= BASE;
Ritor1
parents:
diff changeset
46 }
Ritor1
parents:
diff changeset
47 return (s2 << 16) | s1;
Ritor1
parents:
diff changeset
48 }