1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <cassert>
8 #include <vector>
9 
10 #include "third_party/zlib/zlib.h"
11 
12 static Bytef buffer[256 * 1024] = {0};
13 
14 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)15 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
16   // We need to strip the 'const' for zlib.
17   std::vector<unsigned char> input_buffer{data, data + size};
18 
19   uLongf buffer_length = static_cast<uLongf>(sizeof(buffer));
20 
21   z_stream stream;
22   stream.next_in = input_buffer.data();
23   stream.avail_in = size;
24   stream.total_in = size;
25   stream.next_out = buffer;
26   stream.avail_out = buffer_length;
27   stream.total_out = buffer_length;
28   stream.zalloc = Z_NULL;
29   stream.zfree = Z_NULL;
30 
31   if (Z_OK != deflateInit(&stream, Z_DEFAULT_COMPRESSION)) {
32     deflateEnd(&stream);
33     assert(false);
34   }
35 
36   auto deflate_set_dictionary_result =
37       deflateSetDictionary(&stream, data, size);
38   deflateEnd(&stream);
39   if (Z_OK != deflate_set_dictionary_result)
40     assert(false);
41 
42   return 0;
43 }
44