Coverage Report

Created: 2025-07-18 06:49

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