Eugene Kliuchnikov | ccabf81 | 2016-12-08 12:55:18 +0100 | [diff] [blame^] | 1 | // Copyright 2015 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 | |
| 8 | #include <brotli/decode.h> |
| 9 | |
| 10 | // Entry point for LibFuzzer. |
| 11 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 12 | size_t addend = 0; |
| 13 | if (size > 0) |
| 14 | addend = data[size - 1] & 7; |
| 15 | const uint8_t* next_in = data; |
| 16 | |
| 17 | const int kBufferSize = 1024; |
| 18 | uint8_t* buffer = new uint8_t[kBufferSize]; |
| 19 | BrotliDecoderState* state = BrotliDecoderCreateInstance(0, 0, 0); |
| 20 | |
| 21 | if (addend == 0) |
| 22 | addend = size; |
| 23 | /* Test both fast (addend == size) and slow (addend <= 7) decoding paths. */ |
| 24 | for (size_t i = 0; i < size;) { |
| 25 | size_t next_i = i + addend; |
| 26 | if (next_i > size) |
| 27 | next_i = size; |
| 28 | size_t avail_in = next_i - i; |
| 29 | i = next_i; |
| 30 | BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
| 31 | while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { |
| 32 | size_t avail_out = kBufferSize; |
| 33 | uint8_t* next_out = buffer; |
| 34 | size_t total_out; |
| 35 | result = BrotliDecoderDecompressStream( |
| 36 | state, &avail_in, &next_in, &avail_out, &next_out, &total_out); |
| 37 | } |
| 38 | if (result != BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | BrotliDecoderDestroyInstance(state); |
| 43 | delete[] buffer; |
| 44 | return 0; |
| 45 | } |