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