2018-09-12 22:38:48 +00:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-06-11 13:56:36 +00:00
|
|
|
#include "fuzz.h"
|
2018-05-29 16:48:50 +00:00
|
|
|
#include "webp/decode.h"
|
|
|
|
|
2018-09-12 22:38:48 +00:00
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
|
2018-05-29 16:48:50 +00:00
|
|
|
int w, h;
|
2018-09-12 22:38:48 +00:00
|
|
|
if (!WebPGetInfo(data, size, &w, &h)) return 0;
|
|
|
|
if ((size_t)w * h > kFuzzPxLimit) return 0;
|
2018-05-29 16:48:50 +00:00
|
|
|
|
2018-09-12 22:38:48 +00:00
|
|
|
const uint8_t value = FuzzHash(data, size);
|
2018-06-11 13:56:36 +00:00
|
|
|
uint8_t* buf = NULL;
|
2018-05-29 16:48:50 +00:00
|
|
|
|
2018-06-25 21:17:52 +00:00
|
|
|
// For *Into functions, which decode into an external buffer, an
|
|
|
|
// intentionally too small buffer can be given with low probability.
|
2018-05-29 16:48:50 +00:00
|
|
|
if (value < 0x16) {
|
|
|
|
buf = WebPDecodeRGBA(data, size, &w, &h);
|
|
|
|
} else if (value < 0x2b) {
|
|
|
|
buf = WebPDecodeARGB(data, size, &w, &h);
|
|
|
|
} else if (value < 0x40) {
|
|
|
|
buf = WebPDecodeBGRA(data, size, &w, &h);
|
|
|
|
} else if (value < 0x55) {
|
|
|
|
buf = WebPDecodeRGB(data, size, &w, &h);
|
|
|
|
} else if (value < 0x6a) {
|
|
|
|
buf = WebPDecodeBGR(data, size, &w, &h);
|
|
|
|
} else if (value < 0x7f) {
|
|
|
|
uint8_t *u, *v;
|
|
|
|
int stride, uv_stride;
|
|
|
|
buf = WebPDecodeYUV(data, size, &w, &h, &u, &v, &stride, &uv_stride);
|
|
|
|
} else if (value < 0xe8) {
|
2018-09-12 22:38:48 +00:00
|
|
|
const int stride = (value < 0xbe ? 4 : 3) * w;
|
2018-05-29 16:48:50 +00:00
|
|
|
size_t buf_size = stride * h;
|
|
|
|
if (value % 0x10 == 0) buf_size--;
|
2018-09-12 22:38:48 +00:00
|
|
|
uint8_t* const ext_buf = (uint8_t*)malloc(buf_size);
|
2018-05-29 16:48:50 +00:00
|
|
|
if (value < 0x94) {
|
|
|
|
WebPDecodeRGBAInto(data, size, ext_buf, buf_size, stride);
|
|
|
|
} else if (value < 0xa9) {
|
|
|
|
WebPDecodeARGBInto(data, size, ext_buf, buf_size, stride);
|
|
|
|
} else if (value < 0xbe) {
|
|
|
|
WebPDecodeBGRAInto(data, size, ext_buf, buf_size, stride);
|
|
|
|
} else if (value < 0xd3) {
|
|
|
|
WebPDecodeRGBInto(data, size, ext_buf, buf_size, stride);
|
|
|
|
} else {
|
|
|
|
WebPDecodeBGRInto(data, size, ext_buf, buf_size, stride);
|
|
|
|
}
|
|
|
|
free(ext_buf);
|
|
|
|
} else {
|
|
|
|
size_t luma_size = w * h;
|
2018-09-12 22:38:48 +00:00
|
|
|
const int uv_stride = (w + 1) / 2;
|
2018-05-29 16:48:50 +00:00
|
|
|
size_t u_size = uv_stride * (h + 1) / 2;
|
|
|
|
size_t v_size = uv_stride * (h + 1) / 2;
|
|
|
|
if (value % 0x10 == 0) {
|
|
|
|
if (size & 1) luma_size--;
|
|
|
|
if (size & 2) u_size--;
|
|
|
|
if (size & 4) v_size--;
|
|
|
|
}
|
2018-09-12 22:38:48 +00:00
|
|
|
uint8_t* const luma_buf = (uint8_t*)malloc(luma_size);
|
|
|
|
uint8_t* const u_buf = (uint8_t*)malloc(u_size);
|
|
|
|
uint8_t* const v_buf = (uint8_t*)malloc(v_size);
|
2018-05-29 16:48:50 +00:00
|
|
|
WebPDecodeYUVInto(data, size, luma_buf, luma_size, w /* luma_stride */,
|
|
|
|
u_buf, u_size, uv_stride, v_buf, v_size, uv_stride);
|
|
|
|
free(luma_buf);
|
|
|
|
free(u_buf);
|
|
|
|
free(v_buf);
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:38:48 +00:00
|
|
|
if (buf) WebPFree(buf);
|
2018-05-29 16:48:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|