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