ยปCore Development>Code coverage>Modules/zlib/adler32.c

Python code coverage for Modules/zlib/adler32.c

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