1 /* adler32_p.h -- Private inline functions and macros shared with
2 * different computation of the Adler-32 checksum
3 * of a data stream.
4 * Copyright (C) 1995-2011, 2016 Mark Adler
5 * For conditions of distribution and use, see copyright notice in zlib.h
6 */
7
8 #ifndef ADLER32_P_H
9 #define ADLER32_P_H
10
11 #define BASE 65521U /* largest prime smaller than 65536 */
12 #define NMAX 5552
13 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
14
15 #define DO1(sum1, sum2, buf, i) {(sum1) += buf[(i)]; (sum2) += (sum1);}
16 #define DO2(sum1, sum2, buf, i) {DO1(sum1, sum2, buf, i); DO1(sum1, sum2, buf, i+1);}
17 #define DO4(sum1, sum2, buf, i) {DO2(sum1, sum2, buf, i); DO2(sum1, sum2, buf, i+2);}
18 #define DO8(sum1, sum2, buf, i) {DO4(sum1, sum2, buf, i); DO4(sum1, sum2, buf, i+4);}
19 #define DO16(sum1, sum2, buf) {DO8(sum1, sum2, buf, 0); DO8(sum1, sum2, buf, 8);}
20
adler32_len_1(uint32_t adler,const unsigned char * buf,uint32_t sum2)21 static inline uint32_t adler32_len_1(uint32_t adler, const unsigned char *buf, uint32_t sum2) {
22 adler += buf[0];
23 if (adler >= BASE)
24 adler -= BASE;
25 sum2 += adler;
26 if (sum2 >= BASE)
27 sum2 -= BASE;
28 return adler | (sum2 << 16);
29 }
30
adler32_len_16(uint32_t adler,const unsigned char * buf,size_t len,uint32_t sum2)31 static inline uint32_t adler32_len_16(uint32_t adler, const unsigned char *buf, size_t len, uint32_t sum2) {
32 while (len) {
33 --len;
34 adler += *buf++;
35 sum2 += adler;
36 }
37 if (adler >= BASE)
38 adler -= BASE;
39 sum2 %= BASE; /* only added so many BASE's */
40 return adler | (sum2 << 16);
41 }
42
adler32_len_64(uint32_t adler,const unsigned char * buf,size_t len,uint32_t sum2)43 static inline uint32_t adler32_len_64(uint32_t adler, const unsigned char *buf, size_t len, uint32_t sum2) {
44 while (len >= 16) {
45 len -= 16;
46 DO16(adler, sum2, buf);
47 buf += 16;
48 }
49 /* Process tail (len < 16). */
50 return adler32_len_16(adler, buf, len, sum2);
51 }
52
53 #endif /* ADLER32_P_H */
54