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 adler %= BASE;
24 sum2 += adler;
25 sum2 %= BASE;
26 return adler | (sum2 << 16);
27 }
28
adler32_len_16(uint32_t adler,const unsigned char * buf,size_t len,uint32_t sum2)29 static inline uint32_t adler32_len_16(uint32_t adler, const unsigned char *buf, size_t len, uint32_t sum2) {
30 while (len) {
31 --len;
32 adler += *buf++;
33 sum2 += adler;
34 }
35 adler %= BASE;
36 sum2 %= BASE; /* only added so many BASE's */
37 /* return recombined sums */
38 return adler | (sum2 << 16);
39 }
40
adler32_copy_len_16(uint32_t adler,const unsigned char * buf,uint8_t * dst,size_t len,uint32_t sum2)41 static inline uint32_t adler32_copy_len_16(uint32_t adler, const unsigned char *buf, uint8_t *dst, size_t len, uint32_t sum2) {
42 while (len--) {
43 *dst = *buf++;
44 adler += *dst++;
45 sum2 += adler;
46 }
47 adler %= BASE;
48 sum2 %= BASE; /* only added so many BASE's */
49 /* return recombined sums */
50 return adler | (sum2 << 16);
51 }
52
adler32_len_64(uint32_t adler,const unsigned char * buf,size_t len,uint32_t sum2)53 static inline uint32_t adler32_len_64(uint32_t adler, const unsigned char *buf, size_t len, uint32_t sum2) {
54 #ifdef UNROLL_MORE
55 while (len >= 16) {
56 len -= 16;
57 DO16(adler, sum2, buf);
58 buf += 16;
59 #else
60 while (len >= 8) {
61 len -= 8;
62 DO8(adler, sum2, buf, 0);
63 buf += 8;
64 #endif
65 }
66 /* Process tail (len < 16). */
67 return adler32_len_16(adler, buf, len, sum2);
68 }
69
70 #endif /* ADLER32_P_H */
71