Coverage Report

Created: 2024-07-27 06:04

/work/_deps/deflate-src/lib/adler32.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * adler32.c - Adler-32 checksum algorithm
3
 *
4
 * Copyright 2016 Eric Biggers
5
 *
6
 * Permission is hereby granted, free of charge, to any person
7
 * obtaining a copy of this software and associated documentation
8
 * files (the "Software"), to deal in the Software without
9
 * restriction, including without limitation the rights to use,
10
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the
12
 * Software is furnished to do so, subject to the following
13
 * conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
 * OTHER DEALINGS IN THE SOFTWARE.
26
 */
27
28
#include "lib_common.h"
29
30
/* The Adler-32 divisor, or "base", value */
31
2.58k
#define DIVISOR 65521
32
33
/*
34
 * MAX_CHUNK_LEN is the most bytes that can be processed without the possibility
35
 * of s2 overflowing when it is represented as an unsigned 32-bit integer.  This
36
 * value was computed using the following Python script:
37
 *
38
 *  divisor = 65521
39
 *  count = 0
40
 *  s1 = divisor - 1
41
 *  s2 = divisor - 1
42
 *  while True:
43
 *    s1 += 0xFF
44
 *    s2 += s1
45
 *    if s2 > 0xFFFFFFFF:
46
 *      break
47
 *    count += 1
48
 *  print(count)
49
 *
50
 * Note that to get the correct worst-case value, we must assume that every byte
51
 * has value 0xFF and that s1 and s2 started with the highest possible values
52
 * modulo the divisor.
53
 */
54
#define MAX_CHUNK_LEN 5552
55
56
static u32 MAYBE_UNUSED
57
adler32_generic(u32 adler, const u8 *p, size_t len)
58
0
{
59
0
  u32 s1 = adler & 0xFFFF;
60
0
  u32 s2 = adler >> 16;
61
0
  const u8 * const end = p + len;
62
63
0
  while (p != end) {
64
0
    size_t chunk_len = MIN(end - p, MAX_CHUNK_LEN);
65
0
    const u8 *chunk_end = p + chunk_len;
66
0
    size_t num_unrolled_iterations = chunk_len / 4;
67
68
0
    while (num_unrolled_iterations--) {
69
0
      s1 += *p++;
70
0
      s2 += s1;
71
0
      s1 += *p++;
72
0
      s2 += s1;
73
0
      s1 += *p++;
74
0
      s2 += s1;
75
0
      s1 += *p++;
76
0
      s2 += s1;
77
0
    }
78
0
    while (p != chunk_end) {
79
0
      s1 += *p++;
80
0
      s2 += s1;
81
0
    }
82
0
    s1 %= DIVISOR;
83
0
    s2 %= DIVISOR;
84
0
  }
85
86
0
  return (s2 << 16) | s1;
87
0
}
88
89
/* Include architecture-specific implementation(s) if available. */
90
#undef DEFAULT_IMPL
91
#undef arch_select_adler32_func
92
typedef u32 (*adler32_func_t)(u32 adler, const u8 *p, size_t len);
93
#if defined(ARCH_ARM32) || defined(ARCH_ARM64)
94
#  include "arm/adler32_impl.h"
95
#elif defined(ARCH_X86_32) || defined(ARCH_X86_64)
96
#  include "x86/adler32_impl.h"
97
#endif
98
99
#ifndef DEFAULT_IMPL
100
0
#  define DEFAULT_IMPL adler32_generic
101
#endif
102
103
#ifdef arch_select_adler32_func
104
static u32 dispatch_adler32(u32 adler, const u8 *p, size_t len);
105
106
static volatile adler32_func_t adler32_impl = dispatch_adler32;
107
108
/* Choose the best implementation at runtime. */
109
static u32 dispatch_adler32(u32 adler, const u8 *p, size_t len)
110
2
{
111
2
  adler32_func_t f = arch_select_adler32_func();
112
113
2
  if (f == NULL)
114
0
    f = DEFAULT_IMPL;
115
116
2
  adler32_impl = f;
117
2
  return f(adler, p, len);
118
2
}
119
#else
120
/* The best implementation is statically known, so call it directly. */
121
#define adler32_impl DEFAULT_IMPL
122
#endif
123
124
LIBDEFLATEAPI u32
125
libdeflate_adler32(u32 adler, const void *buffer, size_t len)
126
609
{
127
609
  if (buffer == NULL) /* Return initial value. */
128
0
    return 1;
129
609
  return adler32_impl(adler, buffer, len);
130
609
}