Coverage Report

Created: 2025-09-05 06:28

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