#include <stdio.h>
#include <assert.h>
#include "zbuild.h"
#ifdef ZLIB_COMPAT
# include "zlib.h"
#else
# include "zlib-ng.h"
#endif
#define CHECK_ERR(err, msg) { \
if (err != Z_OK) { \
fprintf(stderr, "%s error: %d\n", msg, err); \
exit(1); \
} \
}
static const uint8_t *data;
static size_t dataLen;
static alloc_func zalloc = NULL;
static free_func zfree = NULL;
static unsigned int dictionaryLen = 0;
static unsigned long dictId;
void test_dict_deflate(unsigned char **compr, size_t *comprLen) {
PREFIX3(stream) c_stream;
int err;
int level = data[0] % 11 - 1;
int method = Z_DEFLATED;
int windowBits = 8 + data[(dataLen > 1) ? 1:0] % 8;
int memLevel = 1 + data[(dataLen > 2) ? 2:0] % 9;
int strategy = data[(dataLen > 3) ? 3:0] % 5;
if (level == 0 || level == 1)
level = -1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (void *)0;
err = PREFIX(deflateInit2)(&c_stream, level, method, windowBits, memLevel,
strategy);
CHECK_ERR(err, "deflateInit");
err = PREFIX(deflateSetDictionary)(
&c_stream, (const unsigned char *)data, dictionaryLen);
CHECK_ERR(err, "deflateSetDictionary");
*comprLen = 100 + 2 * PREFIX(deflateBound)(&c_stream, (unsigned long)dataLen);
*compr = (uint8_t *)calloc(1, *comprLen);
dictId = c_stream.adler;
c_stream.next_out = *compr;
c_stream.avail_out = (unsigned int)(*comprLen);
c_stream.next_in = (z_const unsigned char *)data;
c_stream.avail_in = (uint32_t)dataLen;
err = PREFIX(deflate)(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "deflate dict should report Z_STREAM_END\n");
exit(1);
}
err = PREFIX(deflateEnd)(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
void test_dict_inflate(unsigned char *compr, size_t comprLen) {
int err;
PREFIX3(stream) d_stream;
unsigned char *uncompr;
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (void *)0;
d_stream.next_in = compr;
d_stream.avail_in = (unsigned int)comprLen;
err = PREFIX(inflateInit)(&d_stream);
CHECK_ERR(err, "inflateInit");
uncompr = (uint8_t *)calloc(1, dataLen);
d_stream.next_out = uncompr;
d_stream.avail_out = (unsigned int)dataLen;
for (;;) {
err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END)
break;
if (err == Z_NEED_DICT) {
if (d_stream.adler != dictId) {
fprintf(stderr, "unexpected dictionary");
exit(1);
}
err = PREFIX(inflateSetDictionary)(
&d_stream, (const unsigned char *)data, dictionaryLen);
}
CHECK_ERR(err, "inflate with dict");
}
err = PREFIX(inflateEnd)(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (memcmp(uncompr, data, dataLen)) {
fprintf(stderr, "bad inflate with dict\n");
exit(1);
}
free(uncompr);
}
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
size_t comprLen = 0;
uint8_t *compr;
static size_t kMaxSize = 100 * 1024;
if (size < 1 || size > kMaxSize)
return 0;
data = d;
dataLen = size;
dictionaryLen = data[0];
if (dictionaryLen > dataLen)
dictionaryLen = (unsigned int)dataLen;
test_dict_deflate(&compr, &comprLen);
test_dict_inflate(compr, comprLen);
free(compr);
return 0;
}