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