| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "third_party/brotli/include/brotli/decode.h" | 8 #include <brotli/decode.h> |
| 9 | 9 |
| 10 // Entry point for LibFuzzer. | 10 // Entry point for LibFuzzer. |
| 11 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 11 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 12 size_t addend = 0; | 12 size_t addend = 0; |
| 13 if (size > 0) | 13 if (size > 0) |
| 14 addend = data[size - 1] & 7; | 14 addend = data[size - 1] & 7; |
| 15 const uint8_t* next_in = data; | 15 const uint8_t* next_in = data; |
| 16 | 16 |
| 17 const int kBufferSize = 1024; | 17 const int kBufferSize = 1024; |
| 18 uint8_t* buffer = new uint8_t[kBufferSize]; | 18 uint8_t* buffer = new uint8_t[kBufferSize]; |
| 19 /* The biggest "magic number" in brotli is 16MiB - 16, so no need to check |
| 20 the cases with much longer output. */ |
| 21 const size_t total_out_limit = (addend == 0) ? (1 << 26) : (1 << 24); |
| 22 size_t total_out = 0; |
| 23 |
| 19 BrotliDecoderState* state = BrotliDecoderCreateInstance(0, 0, 0); | 24 BrotliDecoderState* state = BrotliDecoderCreateInstance(0, 0, 0); |
| 20 | 25 |
| 21 if (addend == 0) | 26 if (addend == 0) |
| 22 addend = size; | 27 addend = size; |
| 23 /* Test both fast (addend == size) and slow (addend <= 7) decoding paths. */ | 28 /* Test both fast (addend == size) and slow (addend <= 7) decoding paths. */ |
| 24 for (size_t i = 0; i < size;) { | 29 for (size_t i = 0; i < size;) { |
| 25 size_t next_i = i + addend; | 30 size_t next_i = i + addend; |
| 26 if (next_i > size) | 31 if (next_i > size) |
| 27 next_i = size; | 32 next_i = size; |
| 28 size_t avail_in = next_i - i; | 33 size_t avail_in = next_i - i; |
| 29 i = next_i; | 34 i = next_i; |
| 30 BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; | 35 BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
| 31 while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { | 36 while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { |
| 32 size_t avail_out = kBufferSize; | 37 size_t avail_out = kBufferSize; |
| 33 uint8_t* next_out = buffer; | 38 uint8_t* next_out = buffer; |
| 34 size_t total_out; | |
| 35 result = BrotliDecoderDecompressStream( | 39 result = BrotliDecoderDecompressStream( |
| 36 state, &avail_in, &next_in, &avail_out, &next_out, &total_out); | 40 state, &avail_in, &next_in, &avail_out, &next_out, &total_out); |
| 41 if (total_out > total_out_limit) |
| 42 break; |
| 37 } | 43 } |
| 44 if (total_out > total_out_limit) |
| 45 break; |
| 38 if (result != BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) | 46 if (result != BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) |
| 39 break; | 47 break; |
| 40 } | 48 } |
| 41 | 49 |
| 42 BrotliDecoderDestroyInstance(state); | 50 BrotliDecoderDestroyInstance(state); |
| 43 delete[] buffer; | 51 delete[] buffer; |
| 44 return 0; | 52 return 0; |
| 45 } | 53 } |
| OLD | NEW |