| 1 |
#include <stdio.h> |
| 2 |
#include <assert.h> |
| 3 |
|
| 4 |
#include "zbuild.h" |
| 5 |
#ifdef ZLIB_COMPAT |
| 6 |
# include "zlib.h" |
| 7 |
#else |
| 8 |
# include "zlib-ng.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#define CHECK_ERR(err, msg) { \ |
| 12 |
if (err != Z_OK) { \ |
| 13 |
fprintf(stderr, "%s error: %d\n", msg, err); \ |
| 14 |
exit(1); \ |
| 15 |
} \ |
| 16 |
} |
| 17 |
|
| 18 |
static const uint8_t *data; |
| 19 |
static size_t dataLen; |
| 20 |
static alloc_func zalloc = NULL; |
| 21 |
static free_func zfree = NULL; |
| 22 |
static unsigned int dictionaryLen = 0; |
| 23 |
static unsigned long dictId; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
void test_dict_deflate(unsigned char **compr, size_t *comprLen) { |
| 29 |
PREFIX3(stream) c_stream; |
| 30 |
int err; |
| 31 |
int level = data[0] % 11 - 1; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
int method = Z_DEFLATED; |
| 39 |
|
| 40 |
int windowBits = 8 + data[(dataLen > 1) ? 1:0] % 8; |
| 41 |
|
| 42 |
|
| 43 |
int memLevel = 1 + data[(dataLen > 2) ? 2:0] % 9; |
| 44 |
|
| 45 |
|
| 46 |
int strategy = data[(dataLen > 3) ? 3:0] % 5; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if (level == 0 || level == 1) |
| 55 |
level = -1; |
| 56 |
|
| 57 |
c_stream.zalloc = zalloc; |
| 58 |
c_stream.zfree = zfree; |
| 59 |
c_stream.opaque = (void *)0; |
| 60 |
|
| 61 |
err = PREFIX(deflateInit2)(&c_stream, level, method, windowBits, memLevel, |
| 62 |
strategy); |
| 63 |
CHECK_ERR(err, "deflateInit"); |
| 64 |
|
| 65 |
err = PREFIX(deflateSetDictionary)( |
| 66 |
&c_stream, (const unsigned char *)data, dictionaryLen); |
| 67 |
CHECK_ERR(err, "deflateSetDictionary"); |
| 68 |
|
| 69 |
|
| 70 |
*comprLen = 100 + 2 * PREFIX(deflateBound)(&c_stream, (unsigned long)dataLen); |
| 71 |
*compr = (uint8_t *)calloc(1, *comprLen); |
| 72 |
|
| 73 |
dictId = c_stream.adler; |
| 74 |
c_stream.next_out = *compr; |
| 75 |
c_stream.avail_out = (unsigned int)(*comprLen); |
| 76 |
|
| 77 |
c_stream.next_in = (z_const unsigned char *)data; |
| 78 |
c_stream.avail_in = (uint32_t)dataLen; |
| 79 |
|
| 80 |
err = PREFIX(deflate)(&c_stream, Z_FINISH); |
| 81 |
if (err != Z_STREAM_END) { |
| 82 |
fprintf(stderr, "deflate dict should report Z_STREAM_END\n"); |
| 83 |
exit(1); |
| 84 |
} |
| 85 |
err = PREFIX(deflateEnd)(&c_stream); |
| 86 |
CHECK_ERR(err, "deflateEnd"); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
void test_dict_inflate(unsigned char *compr, size_t comprLen) { |
| 93 |
int err; |
| 94 |
PREFIX3(stream) d_stream; |
| 95 |
unsigned char *uncompr; |
| 96 |
|
| 97 |
d_stream.zalloc = zalloc; |
| 98 |
d_stream.zfree = zfree; |
| 99 |
d_stream.opaque = (void *)0; |
| 100 |
|
| 101 |
d_stream.next_in = compr; |
| 102 |
d_stream.avail_in = (unsigned int)comprLen; |
| 103 |
|
| 104 |
err = PREFIX(inflateInit)(&d_stream); |
| 105 |
CHECK_ERR(err, "inflateInit"); |
| 106 |
|
| 107 |
uncompr = (uint8_t *)calloc(1, dataLen); |
| 108 |
d_stream.next_out = uncompr; |
| 109 |
d_stream.avail_out = (unsigned int)dataLen; |
| 110 |
|
| 111 |
for (;;) { |
| 112 |
err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); |
| 113 |
if (err == Z_STREAM_END) |
| 114 |
break; |
| 115 |
if (err == Z_NEED_DICT) { |
| 116 |
if (d_stream.adler != dictId) { |
| 117 |
fprintf(stderr, "unexpected dictionary"); |
| 118 |
exit(1); |
| 119 |
} |
| 120 |
err = PREFIX(inflateSetDictionary)( |
| 121 |
&d_stream, (const unsigned char *)data, dictionaryLen); |
| 122 |
} |
| 123 |
CHECK_ERR(err, "inflate with dict"); |
| 124 |
} |
| 125 |
|
| 126 |
err = PREFIX(inflateEnd)(&d_stream); |
| 127 |
CHECK_ERR(err, "inflateEnd"); |
| 128 |
|
| 129 |
if (memcmp(uncompr, data, dataLen)) { |
| 130 |
fprintf(stderr, "bad inflate with dict\n"); |
| 131 |
exit(1); |
| 132 |
} |
| 133 |
|
| 134 |
free(uncompr); |
| 135 |
} |
| 136 |
|
| 137 |
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { |
| 138 |
size_t comprLen = 0; |
| 139 |
uint8_t *compr; |
| 140 |
|
| 141 |
|
| 142 |
static size_t kMaxSize = 100 * 1024; |
| 143 |
|
| 144 |
if (size < 1 || size > kMaxSize) |
| 145 |
return 0; |
| 146 |
|
| 147 |
data = d; |
| 148 |
dataLen = size; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
dictionaryLen = data[0]; |
| 154 |
if (dictionaryLen > dataLen) |
| 155 |
dictionaryLen = (unsigned int)dataLen; |
| 156 |
|
| 157 |
test_dict_deflate(&compr, &comprLen); |
| 158 |
test_dict_inflate(compr, comprLen); |
| 159 |
|
| 160 |
free(compr); |
| 161 |
|
| 162 |
|
| 163 |
return 0; |
| 164 |
} |