• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "zlib.h"
9 
10 #define CHECK_ERR(err, msg) { \
11     if (err != Z_OK) { \
12         fprintf(stderr, "%s error: %d\n", msg, err); \
13         exit(1); \
14     } \
15 }
16 
17 static const uint8_t *data;
18 static size_t dataLen;
19 static alloc_func zalloc = NULL;
20 static free_func zfree = NULL;
21 static unsigned int diff;
22 
23 /* ===========================================================================
24  * Test deflate() with large buffers and dynamic change of compression level
25  */
test_large_deflate(unsigned char * compr,size_t comprLen,unsigned char * uncompr,size_t uncomprLen)26 void test_large_deflate(unsigned char *compr, size_t comprLen,
27                         unsigned char *uncompr, size_t uncomprLen) {
28   z_stream c_stream; /* compression stream */
29   int err;
30 
31   c_stream.zalloc = zalloc;
32   c_stream.zfree = zfree;
33   c_stream.opaque = (void *)0;
34 
35   err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
36   CHECK_ERR(err, "deflateInit");
37 
38   c_stream.next_out = compr;
39   c_stream.avail_out = (unsigned int)comprLen;
40 
41   /* At this point, uncompr is still mostly zeroes, so it should compress
42    * very well:
43    */
44   c_stream.next_in = uncompr;
45   c_stream.avail_in = (unsigned int)uncomprLen;
46   err = deflate(&c_stream, Z_NO_FLUSH);
47   CHECK_ERR(err, "deflate large 1");
48   if (c_stream.avail_in != 0) {
49     fprintf(stderr, "deflate not greedy\n");
50     exit(1);
51   }
52 
53   /* Feed in already compressed data and switch to no compression: */
54   deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
55   c_stream.next_in = compr;
56   diff = (unsigned int)(c_stream.next_out - compr);
57   c_stream.avail_in = diff;
58   err = deflate(&c_stream, Z_NO_FLUSH);
59   CHECK_ERR(err, "deflate large 2");
60 
61   /* Switch back to compressing mode: */
62   deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
63   c_stream.next_in = uncompr;
64   c_stream.avail_in = (unsigned int)uncomprLen;
65   err = deflate(&c_stream, Z_NO_FLUSH);
66   CHECK_ERR(err, "deflate large 3");
67 
68   err = deflate(&c_stream, Z_FINISH);
69   if (err != Z_STREAM_END) {
70     fprintf(stderr, "deflate large should report Z_STREAM_END\n");
71     exit(1);
72   }
73   err = deflateEnd(&c_stream);
74   CHECK_ERR(err, "deflateEnd");
75 }
76 
77 /* ===========================================================================
78  * Test inflate() with large buffers
79  */
test_large_inflate(unsigned char * compr,size_t comprLen,unsigned char * uncompr,size_t uncomprLen)80 void test_large_inflate(unsigned char *compr, size_t comprLen,
81                         unsigned char *uncompr, size_t uncomprLen) {
82   int err;
83   z_stream d_stream; /* decompression stream */
84 
85   d_stream.zalloc = zalloc;
86   d_stream.zfree = zfree;
87   d_stream.opaque = (void *)0;
88 
89   d_stream.next_in = compr;
90   d_stream.avail_in = (unsigned int)comprLen;
91 
92   err = inflateInit(&d_stream);
93   CHECK_ERR(err, "inflateInit");
94 
95   for (;;) {
96     d_stream.next_out = uncompr; /* discard the output */
97     d_stream.avail_out = (unsigned int)uncomprLen;
98     err = inflate(&d_stream, Z_NO_FLUSH);
99     if (err == Z_STREAM_END)
100       break;
101     CHECK_ERR(err, "large inflate");
102   }
103 
104   err = inflateEnd(&d_stream);
105   CHECK_ERR(err, "inflateEnd");
106 
107   if (d_stream.total_out != 2 * uncomprLen + diff) {
108     fprintf(stderr, "bad large inflate: %zu\n", d_stream.total_out);
109     exit(1);
110   }
111 }
112 
LLVMFuzzerTestOneInput(const uint8_t * d,size_t size)113 int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
114   size_t comprLen = 100 + 3 * size;
115   size_t uncomprLen = comprLen;
116   uint8_t *compr, *uncompr;
117 
118   /* Discard inputs larger than 512Kb. */
119   static size_t kMaxSize = 512 * 1024;
120 
121   if (size < 1 || size > kMaxSize)
122     return 0;
123 
124   data = d;
125   dataLen = size;
126   compr = (uint8_t *)calloc(1, comprLen);
127   uncompr = (uint8_t *)calloc(1, uncomprLen);
128 
129   test_large_deflate(compr, comprLen, uncompr, uncomprLen);
130   test_large_inflate(compr, comprLen, uncompr, uncomprLen);
131 
132   free(compr);
133   free(uncompr);
134 
135   /* This function must return 0. */
136   return 0;
137 }
138