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 <string.h> 8 #include <cassert> 9 #include <vector> 10 11 #include "third_party/zlib/zlib.h" 12 13 static Bytef buffer[256 * 1024] = {0}; 14 15 // Entry point for LibFuzzer. LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)16extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 17 // zlib's deflate requires non-zero input sizes 18 if (!size) 19 return 0; 20 21 // We need to strip the 'const' for zlib. 22 std::vector<unsigned char> input_buffer{data, data+size}; 23 24 uLongf buffer_length = static_cast<uLongf>(sizeof(buffer)); 25 26 z_stream stream; 27 stream.next_in = input_buffer.data(); 28 stream.avail_in = size; 29 stream.total_in = size; 30 stream.next_out = buffer; 31 stream.avail_out = buffer_length; 32 stream.total_out = buffer_length; 33 stream.zalloc = Z_NULL; 34 stream.zfree = Z_NULL; 35 36 if (Z_OK != deflateInit(&stream, Z_DEFAULT_COMPRESSION)) { 37 deflateEnd(&stream); 38 assert(false); 39 } 40 41 auto deflate_result = deflate(&stream, Z_NO_FLUSH); 42 deflateEnd(&stream); 43 if (Z_OK != deflate_result) 44 assert(false); 45 46 return 0; 47 } 48