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