2016-12-08 11:55:18 +00:00
|
|
|
// Copyright 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2018-06-18 12:39:38 +00:00
|
|
|
#include <stdlib.h>
|
2016-12-08 11:55:18 +00:00
|
|
|
|
|
|
|
#include <brotli/decode.h>
|
|
|
|
|
|
|
|
// Entry point for LibFuzzer.
|
2018-06-18 12:39:38 +00:00
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
2016-12-08 11:55:18 +00:00
|
|
|
size_t addend = 0;
|
|
|
|
if (size > 0)
|
|
|
|
addend = data[size - 1] & 7;
|
|
|
|
const uint8_t* next_in = data;
|
|
|
|
|
|
|
|
const int kBufferSize = 1024;
|
2018-06-18 12:39:38 +00:00
|
|
|
uint8_t* buffer = (uint8_t*) malloc(kBufferSize);
|
|
|
|
if (!buffer) {
|
|
|
|
// OOM is out-of-scope here.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-10 15:01:49 +00:00
|
|
|
/* The biggest "magic number" in brotli is 16MiB - 16, so no need to check
|
|
|
|
the cases with much longer output. */
|
|
|
|
const size_t total_out_limit = (addend == 0) ? (1 << 26) : (1 << 24);
|
|
|
|
size_t total_out = 0;
|
|
|
|
|
2016-12-08 11:55:18 +00:00
|
|
|
BrotliDecoderState* state = BrotliDecoderCreateInstance(0, 0, 0);
|
2023-07-03 19:13:24 +00:00
|
|
|
if (!state) {
|
|
|
|
// OOM is out-of-scope here.
|
|
|
|
free(buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-08 11:55:18 +00:00
|
|
|
|
|
|
|
if (addend == 0)
|
|
|
|
addend = size;
|
|
|
|
/* Test both fast (addend == size) and slow (addend <= 7) decoding paths. */
|
|
|
|
for (size_t i = 0; i < size;) {
|
|
|
|
size_t next_i = i + addend;
|
|
|
|
if (next_i > size)
|
|
|
|
next_i = size;
|
|
|
|
size_t avail_in = next_i - i;
|
|
|
|
i = next_i;
|
|
|
|
BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
|
|
|
|
while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
|
|
|
|
size_t avail_out = kBufferSize;
|
|
|
|
uint8_t* next_out = buffer;
|
|
|
|
result = BrotliDecoderDecompressStream(
|
|
|
|
state, &avail_in, &next_in, &avail_out, &next_out, &total_out);
|
2017-03-10 15:01:49 +00:00
|
|
|
if (total_out > total_out_limit)
|
|
|
|
break;
|
2016-12-08 11:55:18 +00:00
|
|
|
}
|
2017-03-10 15:01:49 +00:00
|
|
|
if (total_out > total_out_limit)
|
|
|
|
break;
|
2016-12-08 11:55:18 +00:00
|
|
|
if (result != BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
BrotliDecoderDestroyInstance(state);
|
2018-06-18 12:39:38 +00:00
|
|
|
free(buffer);
|
2016-12-08 11:55:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|