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 #include "zbuild.h"
7 #include "functable.h"
8 #include "adler32_p.h"
9 
10 /* ========================================================================= */
adler32_c(uint32_t adler,const unsigned char * buf,size_t len)11 Z_INTERNAL uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len) {
12     uint32_t sum2;
13     unsigned n;
14 
15     /* split Adler-32 into component sums */
16     sum2 = (adler >> 16) & 0xffff;
17     adler &= 0xffff;
18 
19     /* in case user likes doing a byte at a time, keep it fast */
20     if (UNLIKELY(len == 1))
21         return adler32_len_1(adler, buf, sum2);
22 
23     /* initial Adler-32 value (deferred check for len == 1 speed) */
24     if (UNLIKELY(buf == NULL))
25         return 1L;
26 
27     /* in case short lengths are provided, keep it somewhat fast */
28     if (UNLIKELY(len < 16))
29         return adler32_len_16(adler, buf, len, sum2);
30 
31     /* do length NMAX blocks -- requires just one modulo operation */
32     while (len >= NMAX) {
33         len -= NMAX;
34 #ifdef UNROLL_MORE
35         n = NMAX / 16;          /* NMAX is divisible by 16 */
36 #else
37         n = NMAX / 8;           /* NMAX is divisible by 8 */
38 #endif
39         do {
40 #ifdef UNROLL_MORE
41             DO16(adler, sum2, buf);          /* 16 sums unrolled */
42             buf += 16;
43 #else
44             DO8(adler, sum2, buf, 0);         /* 8 sums unrolled */
45             buf += 8;
46 #endif
47         } while (--n);
48         adler %= BASE;
49         sum2 %= BASE;
50     }
51 
52     /* do remaining bytes (less than NMAX, still just one modulo) */
53     return adler32_len_64(adler, buf, len, sum2);
54 }
55 
56 #ifdef ZLIB_COMPAT
PREFIX(adler32_z)57 unsigned long Z_EXPORT PREFIX(adler32_z)(unsigned long adler, const unsigned char *buf, size_t len) {
58     return (unsigned long)functable.adler32((uint32_t)adler, buf, len);
59 }
60 #else
PREFIX(adler32_z)61 uint32_t Z_EXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, size_t len) {
62     return functable.adler32(adler, buf, len);
63 }
64 #endif
65 
66 /* ========================================================================= */
67 #ifdef ZLIB_COMPAT
PREFIX(adler32)68 unsigned long Z_EXPORT PREFIX(adler32)(unsigned long adler, const unsigned char *buf, unsigned int len) {
69     return (unsigned long)functable.adler32((uint32_t)adler, buf, len);
70 }
71 #else
PREFIX(adler32)72 uint32_t Z_EXPORT PREFIX(adler32)(uint32_t adler, const unsigned char *buf, uint32_t len) {
73     return functable.adler32(adler, buf, len);
74 }
75 #endif
76 
77 /* ========================================================================= */
adler32_combine_(uint32_t adler1,uint32_t adler2,z_off64_t len2)78 static uint32_t adler32_combine_(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
79     uint32_t sum1;
80     uint32_t sum2;
81     unsigned rem;
82 
83     /* for negative len, return invalid adler32 as a clue for debugging */
84     if (len2 < 0)
85         return 0xffffffff;
86 
87     /* the derivation of this formula is left as an exercise for the reader */
88     len2 %= BASE;                 /* assumes len2 >= 0 */
89     rem = (unsigned)len2;
90     sum1 = adler1 & 0xffff;
91     sum2 = rem * sum1;
92     sum2 %= BASE;
93     sum1 += (adler2 & 0xffff) + BASE - 1;
94     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
95     if (sum1 >= BASE) sum1 -= BASE;
96     if (sum1 >= BASE) sum1 -= BASE;
97     if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
98     if (sum2 >= BASE) sum2 -= BASE;
99     return sum1 | (sum2 << 16);
100 }
101 
102 /* ========================================================================= */
103 #ifdef ZLIB_COMPAT
PREFIX(adler32_combine)104 unsigned long Z_EXPORT PREFIX(adler32_combine)(unsigned long adler1, unsigned long adler2, z_off_t len2) {
105     return (unsigned long)adler32_combine_((uint32_t)adler1, (uint32_t)adler2, len2);
106 }
107 
PREFIX4(adler32_combine)108 unsigned long Z_EXPORT PREFIX4(adler32_combine)(unsigned long adler1, unsigned long adler2, z_off64_t len2) {
109     return (unsigned long)adler32_combine_((uint32_t)adler1, (uint32_t)adler2, len2);
110 }
111 #else
PREFIX4(adler32_combine)112 uint32_t Z_EXPORT PREFIX4(adler32_combine)(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
113     return adler32_combine_(adler1, adler2, len2);
114 }
115 #endif
116