source: rtems/cpukit/zlib/adler32.c @ 660db8c8

5
Last change on this file since 660db8c8 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/* adler32.c -- compute the Adler-32 checksum of a data stream
2 * Copyright (C) 1995-2007 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7
8#define local static
9
10local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
11
12#define BASE 65521UL    /* largest prime smaller than 65536 */
13#define NMAX 5552
14/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
15
16#define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
17#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
18#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
19#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
20#define DO16(buf)   DO8(buf,0); DO8(buf,8);
21
22/* use NO_DIVIDE if your processor does not do division in hardware */
23#ifdef NO_DIVIDE
24#  define MOD(a) \
25    do { \
26        if (a >= (BASE << 16)) a -= (BASE << 16); \
27        if (a >= (BASE << 15)) a -= (BASE << 15); \
28        if (a >= (BASE << 14)) a -= (BASE << 14); \
29        if (a >= (BASE << 13)) a -= (BASE << 13); \
30        if (a >= (BASE << 12)) a -= (BASE << 12); \
31        if (a >= (BASE << 11)) a -= (BASE << 11); \
32        if (a >= (BASE << 10)) a -= (BASE << 10); \
33        if (a >= (BASE << 9)) a -= (BASE << 9); \
34        if (a >= (BASE << 8)) a -= (BASE << 8); \
35        if (a >= (BASE << 7)) a -= (BASE << 7); \
36        if (a >= (BASE << 6)) a -= (BASE << 6); \
37        if (a >= (BASE << 5)) a -= (BASE << 5); \
38        if (a >= (BASE << 4)) a -= (BASE << 4); \
39        if (a >= (BASE << 3)) a -= (BASE << 3); \
40        if (a >= (BASE << 2)) a -= (BASE << 2); \
41        if (a >= (BASE << 1)) a -= (BASE << 1); \
42        if (a >= BASE) a -= BASE; \
43    } while (0)
44#  define MOD4(a) \
45    do { \
46        if (a >= (BASE << 4)) a -= (BASE << 4); \
47        if (a >= (BASE << 3)) a -= (BASE << 3); \
48        if (a >= (BASE << 2)) a -= (BASE << 2); \
49        if (a >= (BASE << 1)) a -= (BASE << 1); \
50        if (a >= BASE) a -= BASE; \
51    } while (0)
52#else
53#  define MOD(a) a %= BASE
54#  define MOD4(a) a %= BASE
55#endif
56
57/* ========================================================================= */
58uLong ZEXPORT adler32(adler, buf, len)
59    uLong adler;
60    const Bytef *buf;
61    uInt len;
62{
63    unsigned long sum2;
64    unsigned n;
65
66    /* split Adler-32 into component sums */
67    sum2 = (adler >> 16) & 0xffff;
68    adler &= 0xffff;
69
70    /* in case user likes doing a byte at a time, keep it fast */
71    if (len == 1) {
72        adler += buf[0];
73        if (adler >= BASE)
74            adler -= BASE;
75        sum2 += adler;
76        if (sum2 >= BASE)
77            sum2 -= BASE;
78        return adler | (sum2 << 16);
79    }
80
81    /* initial Adler-32 value (deferred check for len == 1 speed) */
82    if (buf == Z_NULL)
83        return 1L;
84
85    /* in case short lengths are provided, keep it somewhat fast */
86    if (len < 16) {
87        while (len--) {
88            adler += *buf++;
89            sum2 += adler;
90        }
91        if (adler >= BASE)
92            adler -= BASE;
93        MOD4(sum2);             /* only added so many BASE's */
94        return adler | (sum2 << 16);
95    }
96
97    /* do length NMAX blocks -- requires just one modulo operation */
98    while (len >= NMAX) {
99        len -= NMAX;
100        n = NMAX / 16;          /* NMAX is divisible by 16 */
101        do {
102            DO16(buf);          /* 16 sums unrolled */
103            buf += 16;
104        } while (--n);
105        MOD(adler);
106        MOD(sum2);
107    }
108
109    /* do remaining bytes (less than NMAX, still just one modulo) */
110    if (len) {                  /* avoid modulos if none remaining */
111        while (len >= 16) {
112            len -= 16;
113            DO16(buf);
114            buf += 16;
115        }
116        while (len--) {
117            adler += *buf++;
118            sum2 += adler;
119        }
120        MOD(adler);
121        MOD(sum2);
122    }
123
124    /* return recombined sums */
125    return adler | (sum2 << 16);
126}
127
128/* ========================================================================= */
129local uLong adler32_combine_(adler1, adler2, len2)
130    uLong adler1;
131    uLong adler2;
132    z_off64_t len2;
133{
134    unsigned long sum1;
135    unsigned long sum2;
136    unsigned rem;
137
138    /* the derivation of this formula is left as an exercise for the reader */
139    rem = (unsigned)(len2 % BASE);
140    sum1 = adler1 & 0xffff;
141    sum2 = rem * sum1;
142    MOD(sum2);
143    sum1 += (adler2 & 0xffff) + BASE - 1;
144    sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
145    if (sum1 >= BASE) sum1 -= BASE;
146    if (sum1 >= BASE) sum1 -= BASE;
147    if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
148    if (sum2 >= BASE) sum2 -= BASE;
149    return sum1 | (sum2 << 16);
150}
151
152/* ========================================================================= */
153uLong ZEXPORT adler32_combine(adler1, adler2, len2)
154    uLong adler1;
155    uLong adler2;
156    z_off_t len2;
157{
158    return adler32_combine_(adler1, adler2, len2);
159}
160
161uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
162    uLong adler1;
163    uLong adler2;
164    z_off64_t len2;
165{
166    return adler32_combine_(adler1, adler2, len2);
167}
Note: See TracBrowser for help on using the repository browser.