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 
28 /* ===========================================================================
29  * Test deflate() with small buffers
30  */
test_deflate(unsigned char * compr,size_t comprLen)31 void test_deflate(unsigned char *compr, size_t comprLen) {
32     PREFIX3(stream) c_stream; /* compression stream */
33     int err;
34     unsigned long len = (unsigned long)dataLen;
35 
36     c_stream.zalloc = zalloc;
37     c_stream.zfree = zfree;
38     c_stream.opaque = (void *)0;
39 
40     err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION);
41     CHECK_ERR(err, "deflateInit");
42 
43     c_stream.next_in = (const unsigned char *)data;
44     c_stream.next_out = compr;
45 
46     while (c_stream.total_in != len && c_stream.total_out < comprLen) {
47         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
48         err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
49         CHECK_ERR(err, "deflate small 1");
50     }
51     /* Finish the stream, still forcing small buffers: */
52     for (;;) {
53         c_stream.avail_out = 1;
54         err = PREFIX(deflate)(&c_stream, Z_FINISH);
55         if (err == Z_STREAM_END)
56             break;
57         CHECK_ERR(err, "deflate small 2");
58     }
59 
60     err = PREFIX(deflateEnd)(&c_stream);
61     CHECK_ERR(err, "deflateEnd");
62 }
63 
64 /* ===========================================================================
65  * Test inflate() with small buffers
66  */
test_inflate(unsigned char * compr,size_t comprLen,unsigned char * uncompr,size_t uncomprLen)67 void test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
68     int err;
69     PREFIX3(stream) d_stream; /* decompression stream */
70 
71     d_stream.zalloc = zalloc;
72     d_stream.zfree = zfree;
73     d_stream.opaque = (void *)0;
74 
75     d_stream.next_in = compr;
76     d_stream.avail_in = 0;
77     d_stream.next_out = uncompr;
78 
79     err = PREFIX(inflateInit)(&d_stream);
80     CHECK_ERR(err, "inflateInit");
81 
82     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
83         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
84         err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH);
85         if (err == Z_STREAM_END)
86             break;
87         CHECK_ERR(err, "inflate");
88     }
89 
90     err = PREFIX(inflateEnd)(&d_stream);
91     CHECK_ERR(err, "inflateEnd");
92 
93     if (memcmp(uncompr, data, dataLen)) {
94         fprintf(stderr, "bad inflate\n");
95         exit(1);
96     }
97 }
98 
LLVMFuzzerTestOneInput(const uint8_t * d,size_t size)99 int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
100     size_t comprLen = PREFIX(compressBound)(size);
101     size_t uncomprLen = size;
102     uint8_t *compr, *uncompr;
103 
104     /* Discard inputs larger than 1Mb. */
105     static size_t kMaxSize = 1024 * 1024;
106 
107     if (size < 1 || size > kMaxSize)
108         return 0;
109 
110     data = d;
111     dataLen = size;
112     compr = (uint8_t *)calloc(1, comprLen);
113     uncompr = (uint8_t *)calloc(1, uncomprLen);
114 
115     test_deflate(compr, comprLen);
116     test_inflate(compr, comprLen, uncompr, uncomprLen);
117 
118     free(compr);
119     free(uncompr);
120 
121     /* This function must return 0. */
122     return 0;
123 }
124