annotate lib/zlib/adler32.c @ 185:48f9a1ac8376

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