1/* adler32.c -- compute the Adler-32 checksum of a data stream
2 * Copyright (C) 1995-2011, 2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#include "zbuild.h"
9#include "zutil.h"
10#include "functable.h"
11#include "adler32_p.h"
12
13uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len);
14static uint32_t adler32_combine_(uint32_t adler1, uint32_t adler2, z_off64_t len2);
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/* ========================================================================= */
23uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len) {
24 uint32_t sum2;
25 unsigned n;
26
27 /* split Adler-32 into component sums */
28 sum2 = (adler >> 16) & 0xffff;
29 adler &= 0xffff;
30
31 /* in case user likes doing a byte at a time, keep it fast */
32 if (len == 1)
33 return adler32_len_1(adler, buf, sum2);
34
35 /* initial Adler-32 value (deferred check for len == 1 speed) */
36 if (buf == NULL)
37 return 1L;
38
39 /* in case short lengths are provided, keep it somewhat fast */
40 if (len < 16)
41 return adler32_len_16(adler, buf, len, sum2);
42
43 /* do length NMAX blocks -- requires just one modulo operation */
44 while (len >= NMAX) {
45 len -= NMAX;
46#ifdef UNROLL_MORE
47 n = NMAX / 16; /* NMAX is divisible by 16 */
48#else
49 n = NMAX / 8; /* NMAX is divisible by 8 */
50#endif
51 do {
52#ifdef UNROLL_MORE
53 DO16(buf); /* 16 sums unrolled */
54 buf += 16;
55#else
56 DO8(buf, 0); /* 8 sums unrolled */
57 buf += 8;
58#endif
59 } while (--n);
60 MOD(adler);
61 MOD(sum2);
62 }
63
64 /* do remaining bytes (less than NMAX, still just one modulo) */
65 if (len) { /* avoid modulos if none remaining */
66#ifdef UNROLL_MORE
67 while (len >= 16) {
68 len -= 16;
69 DO16(buf);
70 buf += 16;
71#else
72 while (len >= 8) {
73 len -= 8;
74 DO8(buf, 0);
75 buf += 8;
76#endif
77 }
78 while (len) {
79 --len;
80 adler += *buf++;
81 sum2 += adler;
82 }
83 MOD(adler);
84 MOD(sum2);
85 }
86
87 /* return recombined sums */
88 return adler | (sum2 << 16);
89}
90
91uint32_t ZEXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, size_t len) {
92 return functable.adler32(adler, buf, len);
93}
94
95/* ========================================================================= */
96uint32_t ZEXPORT PREFIX(adler32)(uint32_t adler, const unsigned char *buf, uint32_t len) {
97 return functable.adler32(adler, buf, len);
98}
99
100/* ========================================================================= */
101static uint32_t adler32_combine_(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
102 uint32_t sum1;
103 uint32_t sum2;
104 unsigned rem;
105
106 /* for negative len, return invalid adler32 as a clue for debugging */
107 if (len2 < 0)
108 return 0xffffffff;
109
110 /* the derivation of this formula is left as an exercise for the reader */
111 MOD63(len2); /* assumes len2 >= 0 */
112 rem = (unsigned)len2;
113 sum1 = adler1 & 0xffff;
114 sum2 = rem * sum1;
115 MOD(sum2);
116 sum1 += (adler2 & 0xffff) + BASE - 1;
117 sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
118 if (sum1 >= BASE) sum1 -= BASE;
119 if (sum1 >= BASE) sum1 -= BASE;
120 if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
121 if (sum2 >= BASE) sum2 -= BASE;
122 return sum1 | (sum2 << 16);
123}
124
125/* ========================================================================= */
126uint32_t ZEXPORT PREFIX(adler32_combine)(uint32_t adler1, uint32_t adler2, z_off_t len2) {
127 return adler32_combine_(adler1, adler2, len2);
128}
129
130uint32_t ZEXPORT PREFIX(adler32_combine64)(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
131 return adler32_combine_(adler1, adler2, len2);
132}
133