blob: 0ae18d4112df748e8ccecbfa87abc822093dcaf7 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Lubick2416f962018-02-12 08:26:39 -050013
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