blob: 735eb9c4c28305d36e6eb2f374508c2a0464b9df [file] [log] [blame]
Kevin Lubick2416f962018-02-12 08:26:39 -05001/*
Kevin Lubickdb1e5c62018-02-27 08:30:43 -05002 * Copyright 2018 Google, LLC
Kevin Lubick2416f962018-02-12 08:26:39 -05003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkImage.h"
9#include "SkPaint.h"
10#include "SkCanvas.h"
11#include "SkData.h"
12#include "SkSurface.h"
13
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040014bool FuzzImageDecode(sk_sp<SkData> bytes) {
Kevin Lubick2416f962018-02-12 08:26:39 -050015 auto img = SkImage::MakeFromEncoded(bytes);
16 if (nullptr == img.get()) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040017 return false;
Kevin Lubick2416f962018-02-12 08:26:39 -050018 }
19
20 auto s = SkSurface::MakeRasterN32Premul(128, 128);
21 if (!s) {
22 // May return nullptr in memory-constrained fuzzing environments
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040023 return false;
Kevin Lubick2416f962018-02-12 08:26:39 -050024 }
25
26 SkPaint p;
27 s->getCanvas()->drawImage(img, 0, 0, &p);
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040028 return true;
Kevin Lubick2416f962018-02-12 08:26:39 -050029}
30
31#if defined(IS_FUZZING_WITH_LIBFUZZER)
32extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33 auto bytes = SkData::MakeWithoutCopy(data, size);
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040034 FuzzImageDecode(bytes);
Kevin Lubick2416f962018-02-12 08:26:39 -050035 return 0;
36}
37#endif