| 1 |
// Copyright 2018 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
//////////////////////////////////////////////////////////////////////////////// |
| 16 |
|
| 17 |
#include <cstddef> |
| 18 |
#include <cstdint> |
| 19 |
#include <string_view> |
| 20 |
|
| 21 |
#include "./fuzz_utils.h" |
| 22 |
#include "src/webp/decode.h" |
| 23 |
|
| 24 |
namespace { |
| 25 |
|
| 26 |
void SimpleApiTest(std::string_view data_in) { |
| 27 |
const uint8_t* const data = reinterpret_cast<const uint8_t*>(data_in.data()); |
| 28 |
const size_t size = data_in.size(); |
| 29 |
int w, h; |
| 30 |
if (!WebPGetInfo(data, size, &w, &h)) return; |
| 31 |
if ((size_t)w * h > fuzz_utils::kFuzzPxLimit) return; |
| 32 |
|
| 33 |
const uint8_t value = fuzz_utils::FuzzHash(data, size); |
| 34 |
uint8_t* buf = NULL; |
| 35 |
|
| 36 |
// For *Into functions, which decode into an external buffer, an |
| 37 |
// intentionally too small buffer can be given with low probability. |
| 38 |
if (value < 0x16) { |
| 39 |
buf = WebPDecodeRGBA(data, size, &w, &h); |
| 40 |
} else if (value < 0x2b) { |
| 41 |
buf = WebPDecodeBGRA(data, size, &w, &h); |
| 42 |
#if !defined(WEBP_REDUCE_CSP) |
| 43 |
} else if (value < 0x40) { |
| 44 |
buf = WebPDecodeARGB(data, size, &w, &h); |
| 45 |
} else if (value < 0x55) { |
| 46 |
buf = WebPDecodeRGB(data, size, &w, &h); |
| 47 |
} else if (value < 0x6a) { |
| 48 |
buf = WebPDecodeBGR(data, size, &w, &h); |
| 49 |
#endif // !defined(WEBP_REDUCE_CSP) |
| 50 |
} else if (value < 0x7f) { |
| 51 |
uint8_t *u, *v; |
| 52 |
int stride, uv_stride; |
| 53 |
buf = WebPDecodeYUV(data, size, &w, &h, &u, &v, &stride, &uv_stride); |
| 54 |
} else if (value < 0xe8) { |
| 55 |
const int stride = (value < 0xbe ? 4 : 3) * w; |
| 56 |
size_t buf_size = stride * h; |
| 57 |
if (value % 0x10 == 0) buf_size--; |
| 58 |
uint8_t* const ext_buf = (uint8_t*)malloc(buf_size); |
| 59 |
if (value < 0x94) { |
| 60 |
(void)WebPDecodeRGBAInto(data, size, ext_buf, buf_size, stride); |
| 61 |
#if !defined(WEBP_REDUCE_CSP) |
| 62 |
} else if (value < 0xa9) { |
| 63 |
(void)WebPDecodeARGBInto(data, size, ext_buf, buf_size, stride); |
| 64 |
} else if (value < 0xbe) { |
| 65 |
(void)WebPDecodeBGRInto(data, size, ext_buf, buf_size, stride); |
| 66 |
} else if (value < 0xd3) { |
| 67 |
(void)WebPDecodeRGBInto(data, size, ext_buf, buf_size, stride); |
| 68 |
#endif // !defined(WEBP_REDUCE_CSP) |
| 69 |
} else { |
| 70 |
(void)WebPDecodeBGRAInto(data, size, ext_buf, buf_size, stride); |
| 71 |
} |
| 72 |
free(ext_buf); |
| 73 |
} else { |
| 74 |
size_t luma_size = w * h; |
| 75 |
const int uv_stride = (w + 1) / 2; |
| 76 |
size_t u_size = uv_stride * (h + 1) / 2; |
| 77 |
size_t v_size = uv_stride * (h + 1) / 2; |
| 78 |
if (value % 0x10 == 0) { |
| 79 |
if (size & 1) luma_size--; |
| 80 |
if (size & 2) u_size--; |
| 81 |
if (size & 4) v_size--; |
| 82 |
} |
| 83 |
uint8_t* const luma_buf = (uint8_t*)malloc(luma_size); |
| 84 |
uint8_t* const u_buf = (uint8_t*)malloc(u_size); |
| 85 |
uint8_t* const v_buf = (uint8_t*)malloc(v_size); |
| 86 |
(void)WebPDecodeYUVInto(data, size, luma_buf, luma_size, |
| 87 |
w /* luma_stride */, u_buf, u_size, uv_stride, |
| 88 |
v_buf, v_size, uv_stride); |
| 89 |
free(luma_buf); |
| 90 |
free(u_buf); |
| 91 |
free(v_buf); |
| 92 |
} |
| 93 |
|
| 94 |
if (buf) WebPFree(buf); |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace |
| 98 |
|
| 99 |
FUZZ_TEST(SimpleApi, SimpleApiTest) |
| 100 |
.WithDomains( |
| 101 |
fuzztest::String() |
| 102 |
.WithMaxSize(fuzz_utils::kMaxWebPFileSize + 1)); |