Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 1 | /* |
Kevin Lubick | db1e5c6 | 2018-02-27 08:30:43 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google, LLC |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkData.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkSurface.h" |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 13 | |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 14 | bool FuzzImageDecode(sk_sp<SkData> bytes) { |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 15 | auto img = SkImage::MakeFromEncoded(bytes); |
| 16 | if (nullptr == img.get()) { |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 17 | return false; |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | auto s = SkSurface::MakeRasterN32Premul(128, 128); |
| 21 | if (!s) { |
| 22 | // May return nullptr in memory-constrained fuzzing environments |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 23 | return false; |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | SkPaint p; |
| 27 | s->getCanvas()->drawImage(img, 0, 0, &p); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 28 | return true; |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | #if defined(IS_FUZZING_WITH_LIBFUZZER) |
| 32 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 33 | auto bytes = SkData::MakeWithoutCopy(data, size); |
Kevin Lubick | 0f3d2a6 | 2018-10-17 10:24:44 -0400 | [diff] [blame] | 34 | FuzzImageDecode(bytes); |
Kevin Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | #endif |