blob: e99f7bc52ff2af01c894f87853c4011c9d698594 [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 "SkAndroidCodec.h"
9#include "SkAnimatedImage.h"
Kevin Lubick2416f962018-02-12 08:26:39 -050010#include "SkCanvas.h"
11#include "SkData.h"
12#include "SkSurface.h"
13
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040014bool FuzzAnimatedImage(sk_sp<SkData> bytes) {
Kevin Lubick2416f962018-02-12 08:26:39 -050015 auto codec = SkAndroidCodec::MakeFromData(bytes);
16 if (nullptr == codec) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040017 return false;
Kevin Lubick2416f962018-02-12 08:26:39 -050018 }
19 auto aImg = SkAnimatedImage::Make(std::move(codec));
20 if (nullptr == aImg) {
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040021 return false;
Kevin Lubick2416f962018-02-12 08:26:39 -050022 }
23
24 auto s = SkSurface::MakeRasterN32Premul(128, 128);
25 if (!s) {
26 // May return nullptr in memory-constrained fuzzing environments
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040027 return false;
Kevin Lubick2416f962018-02-12 08:26:39 -050028 }
29
Kevin Lubick2416f962018-02-12 08:26:39 -050030 int escape = 0;
31 while (!aImg->isFinished() && escape < 100) {
32 aImg->draw(s->getCanvas());
33 escape++;
34 aImg->decodeNextFrame();
35 }
Kevin Lubick0f3d2a62018-10-17 10:24:44 -040036 return true;
Kevin Lubick2416f962018-02-12 08:26:39 -050037}
38
39#if defined(IS_FUZZING_WITH_LIBFUZZER)
40extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
41 auto bytes = SkData::MakeWithoutCopy(data, size);
42 FuzzAnimatedImage(bytes);
43 return 0;
44}
45#endif