/src/zlib-ng/test/fuzz/fuzzer_example_dict.c
Line | Count | Source |
1 | | #include "zbuild.h" |
2 | | #include <assert.h> |
3 | | #ifdef ZLIB_COMPAT |
4 | | # include "zlib.h" |
5 | | #else |
6 | | # include "zlib-ng.h" |
7 | | #endif |
8 | | |
9 | 66.0k | #define CHECK_ERR(err, msg) { \ |
10 | 66.0k | if (err != Z_OK) { \ |
11 | 0 | fprintf(stderr, "%s error: %d\n", msg, err); \ |
12 | 0 | exit(1); \ |
13 | 0 | } \ |
14 | 66.0k | } |
15 | | |
16 | | static const uint8_t *data; |
17 | | static size_t dataLen; |
18 | | static alloc_func zalloc = NULL; |
19 | | static free_func zfree = NULL; |
20 | | static unsigned int dictionaryLen = 0; |
21 | | static unsigned long dictId; /* Adler32 value of the dictionary */ |
22 | | |
23 | | /* =========================================================================== |
24 | | * Test deflate() with preset dictionary |
25 | | */ |
26 | 11.0k | void test_dict_deflate(unsigned char **compr, size_t *comprLen) { |
27 | 11.0k | PREFIX3(stream) c_stream; /* compression stream */ |
28 | 11.0k | int err; |
29 | 11.0k | int level = data[0] % 11 - 1; /* [-1..9] |
30 | | compression levels |
31 | | #define Z_NO_COMPRESSION 0 |
32 | | #define Z_BEST_SPEED 1 |
33 | | #define Z_BEST_COMPRESSION 9 |
34 | | #define Z_DEFAULT_COMPRESSION (-1) */ |
35 | | |
36 | 11.0k | int method = Z_DEFLATED; /* The deflate compression method (the only one |
37 | | supported in this version) */ |
38 | 11.0k | int windowBits = 8 + data[(dataLen > 1) ? 1:0] % 8; /* The windowBits parameter is the base |
39 | | two logarithm of the window size (the size of the history buffer). It |
40 | | should be in the range 8..15 for this version of the library. */ |
41 | 11.0k | int memLevel = 1 + data[(dataLen > 2) ? 2:0] % 9; /* memLevel=1 uses minimum memory but is |
42 | | slow and reduces compression ratio; memLevel=9 uses maximum memory for |
43 | | optimal speed. */ |
44 | 11.0k | int strategy = data[(dataLen > 3) ? 3:0] % 5; /* [0..4] |
45 | | #define Z_FILTERED 1 |
46 | | #define Z_HUFFMAN_ONLY 2 |
47 | | #define Z_RLE 3 |
48 | | #define Z_FIXED 4 |
49 | | #define Z_DEFAULT_STRATEGY 0 */ |
50 | | |
51 | | /* deflate would fail for no-compression or for speed levels. */ |
52 | 11.0k | if (level == 0 || level == 1) |
53 | 2.95k | level = -1; |
54 | | |
55 | 11.0k | c_stream.zalloc = zalloc; |
56 | 11.0k | c_stream.zfree = zfree; |
57 | 11.0k | c_stream.opaque = (void *)0; |
58 | | |
59 | 11.0k | err = PREFIX(deflateInit2)(&c_stream, level, method, windowBits, memLevel, |
60 | 11.0k | strategy); |
61 | 11.0k | CHECK_ERR(err, "deflateInit"); |
62 | | |
63 | 11.0k | err = PREFIX(deflateSetDictionary)( |
64 | 11.0k | &c_stream, (const unsigned char *)data, dictionaryLen); |
65 | 11.0k | CHECK_ERR(err, "deflateSetDictionary"); |
66 | | |
67 | | /* deflateBound does not provide enough space for low compression levels. */ |
68 | 11.0k | *comprLen = 100 + 2 * PREFIX(deflateBound)(&c_stream, (unsigned long)dataLen); |
69 | 11.0k | *compr = (uint8_t *)calloc(1, *comprLen); |
70 | | |
71 | 11.0k | dictId = c_stream.adler; |
72 | 11.0k | c_stream.next_out = *compr; |
73 | 11.0k | c_stream.avail_out = (unsigned int)(*comprLen); |
74 | | |
75 | 11.0k | c_stream.next_in = (z_const unsigned char *)data; |
76 | 11.0k | c_stream.avail_in = (uint32_t)dataLen; |
77 | | |
78 | 11.0k | err = PREFIX(deflate)(&c_stream, Z_FINISH); |
79 | 11.0k | if (err != Z_STREAM_END) { |
80 | 0 | fprintf(stderr, "deflate dict should report Z_STREAM_END\n"); |
81 | 0 | exit(1); |
82 | 0 | } |
83 | 11.0k | err = PREFIX(deflateEnd)(&c_stream); |
84 | 11.0k | CHECK_ERR(err, "deflateEnd"); |
85 | 11.0k | } |
86 | | |
87 | | /* =========================================================================== |
88 | | * Test inflate() with a preset dictionary |
89 | | */ |
90 | 11.0k | void test_dict_inflate(unsigned char *compr, size_t comprLen) { |
91 | 11.0k | int err; |
92 | 11.0k | PREFIX3(stream) d_stream; /* decompression stream */ |
93 | 11.0k | unsigned char *uncompr; |
94 | | |
95 | 11.0k | d_stream.zalloc = zalloc; |
96 | 11.0k | d_stream.zfree = zfree; |
97 | 11.0k | d_stream.opaque = (void *)0; |
98 | | |
99 | 11.0k | d_stream.next_in = compr; |
100 | 11.0k | d_stream.avail_in = (unsigned int)comprLen; |
101 | | |
102 | 11.0k | err = PREFIX(inflateInit)(&d_stream); |
103 | 11.0k | CHECK_ERR(err, "inflateInit"); |
104 | | |
105 | 11.0k | uncompr = (uint8_t *)calloc(1, dataLen); |
106 | 11.0k | d_stream.next_out = uncompr; |
107 | 11.0k | d_stream.avail_out = (unsigned int)dataLen; |
108 | | |
109 | 21.7k | for (;;) { |
110 | 21.7k | err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); |
111 | 21.7k | if (err == Z_STREAM_END) |
112 | 11.0k | break; |
113 | 10.7k | if (err == Z_NEED_DICT) { |
114 | 10.7k | if (d_stream.adler != dictId) { |
115 | 0 | fprintf(stderr, "unexpected dictionary"); |
116 | 0 | exit(1); |
117 | 0 | } |
118 | 10.7k | err = PREFIX(inflateSetDictionary)( |
119 | 10.7k | &d_stream, (const unsigned char *)data, dictionaryLen); |
120 | 10.7k | } |
121 | 10.7k | CHECK_ERR(err, "inflate with dict"); |
122 | 10.7k | } |
123 | | |
124 | 11.0k | err = PREFIX(inflateEnd)(&d_stream); |
125 | 11.0k | CHECK_ERR(err, "inflateEnd"); |
126 | | |
127 | 11.0k | if (memcmp(uncompr, data, dataLen)) { |
128 | 0 | fprintf(stderr, "bad inflate with dict\n"); |
129 | 0 | exit(1); |
130 | 0 | } |
131 | | |
132 | 11.0k | free(uncompr); |
133 | 11.0k | } |
134 | | |
135 | 11.0k | int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { |
136 | 11.0k | size_t comprLen = 0; |
137 | 11.0k | uint8_t *compr; |
138 | | |
139 | | /* Discard inputs larger than 100Kb. */ |
140 | 11.0k | static size_t kMaxSize = 100 * 1024; |
141 | | |
142 | 11.0k | if (size < 1 || size > kMaxSize) |
143 | 9 | return 0; |
144 | | |
145 | 11.0k | data = d; |
146 | 11.0k | dataLen = size; |
147 | | |
148 | | /* Set up the contents of the dictionary. The size of the dictionary is |
149 | | intentionally selected to be of unusual size. To help cover more corner |
150 | | cases, the size of the dictionary is read from the input data. */ |
151 | 11.0k | dictionaryLen = data[0]; |
152 | 11.0k | if (dictionaryLen > dataLen) |
153 | 3.85k | dictionaryLen = (unsigned int)dataLen; |
154 | | |
155 | 11.0k | test_dict_deflate(&compr, &comprLen); |
156 | 11.0k | test_dict_inflate(compr, comprLen); |
157 | | |
158 | 11.0k | free(compr); |
159 | | |
160 | | /* This function must return 0. */ |
161 | 11.0k | return 0; |
162 | 11.0k | } |