Coverage Report

Created: 2025-10-28 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/test/fuzz/fuzzer_checksum.c
Line
Count
Source
1
#include <stdio.h>
2
#include <assert.h>
3
4
#include "zbuild.h"
5
#ifdef ZLIB_COMPAT
6
#  include "zlib.h"
7
#else
8
#  include "zlib-ng.h"
9
#endif
10
11
652
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) {
12
652
    uint32_t crc0 = PREFIX(crc32)(0L, NULL, 0);
13
652
    uint32_t crc1 = crc0;
14
652
    uint32_t crc2 = crc0;
15
652
    uint32_t adler0 = PREFIX(adler32)(0L, NULL, 0);
16
652
    uint32_t adler1 = adler0;
17
652
    uint32_t adler2 = adler0;
18
652
    uint32_t combine1, combine2;
19
    /* Checksum with a buffer of size equal to the first byte in the input. */
20
652
    uint32_t buffSize = data[0];
21
652
    uint32_t offset = 0;
22
652
    uint32_t op;
23
24
    /* Discard inputs larger than 1Mb. */
25
652
    static size_t kMaxSize = 1024 * 1024;
26
652
    if (dataLen < 1 || dataLen > kMaxSize)
27
0
        return 0;
28
29
    /* Make sure the buffer has at least a byte. */
30
652
    if (buffSize == 0)
31
42
        ++buffSize;
32
33
    /* CRC32 */
34
652
    op = PREFIX(crc32_combine_gen)(buffSize);
35
654k
    for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) {
36
654k
        uint32_t crc3 = PREFIX(crc32_z)(crc0, data + offset, buffSize);
37
654k
        uint32_t crc4 = PREFIX(crc32_combine_op)(crc1, crc3, op);
38
654k
        crc1 = PREFIX(crc32_z)(crc1, data + offset, buffSize);
39
654k
        assert(crc1 == crc4);
40
654k
        Z_UNUSED(crc1);
41
654k
        Z_UNUSED(crc4);
42
654k
    }
43
652
    crc1 = PREFIX(crc32_z)(crc1, data + offset, dataLen % buffSize);
44
45
652
    crc2 = PREFIX(crc32_z)(crc2, data, dataLen);
46
47
652
    assert(crc1 == crc2);
48
652
    Z_UNUSED(crc1);
49
652
    Z_UNUSED(crc2);
50
652
    combine1 = PREFIX(crc32_combine)(crc1, crc2, (z_off_t)dataLen);
51
652
    combine2 = PREFIX(crc32_combine)(crc1, crc1, (z_off_t)dataLen);
52
652
    assert(combine1 == combine2);
53
54
    /* Fast CRC32 combine. */
55
652
    op = PREFIX(crc32_combine_gen)((z_off_t)dataLen);
56
652
    combine1 = PREFIX(crc32_combine_op)(crc1, crc2, op);
57
652
    combine2 = PREFIX(crc32_combine_op)(crc2, crc1, op);
58
652
    assert(combine1 == combine2);
59
652
    combine1 = PREFIX(crc32_combine)(crc1, crc2, (z_off_t)dataLen);
60
652
    combine2 = PREFIX(crc32_combine_op)(crc2, crc1, op);
61
652
    assert(combine1 == combine2);
62
63
    /* Adler32 */
64
654k
    for (offset = 0; offset + buffSize <= dataLen; offset += buffSize)
65
654k
        adler1 = PREFIX(adler32_z)(adler1, data + offset, buffSize);
66
652
    adler1 = PREFIX(adler32_z)(adler1, data + offset, dataLen % buffSize);
67
68
652
    adler2 = PREFIX(adler32_z)(adler2, data, dataLen);
69
70
652
    assert(adler1 == adler2);
71
652
    Z_UNUSED(adler1);
72
652
    Z_UNUSED(adler2);
73
652
    combine1 = PREFIX(adler32_combine)(adler1, adler2, (z_off_t)dataLen);
74
652
    combine2 = PREFIX(adler32_combine)(adler1, adler1, (z_off_t)dataLen);
75
652
    assert(combine1 == combine2);
76
652
    Z_UNUSED(combine1);
77
652
    Z_UNUSED(combine2);
78
79
    /* This function must return 0. */
80
652
    return 0;
81
652
}