1/*
2 * Copyright 2018 Google, LLC
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
8#include "SkAndroidCodec.h"
9#include "SkAnimatedImage.h"
10#include "SkPaint.h"
11#include "SkCanvas.h"
12#include "SkData.h"
13#include "SkSurface.h"
14
15void FuzzAnimatedImage(sk_sp<SkData> bytes) {
16    auto codec = SkAndroidCodec::MakeFromData(bytes);
17    if (nullptr == codec) {
18        return;
19    }
20    auto aImg = SkAnimatedImage::Make(std::move(codec));
21    if (nullptr == aImg) {
22        return;
23    }
24
25    auto s = SkSurface::MakeRasterN32Premul(128, 128);
26    if (!s) {
27        // May return nullptr in memory-constrained fuzzing environments
28        return;
29    }
30
31    SkPaint p;
32    int escape = 0;
33    while (!aImg->isFinished() && escape < 100) {
34        aImg->draw(s->getCanvas());
35        escape++;
36        aImg->decodeNextFrame();
37    }
38
39}
40
41#if defined(IS_FUZZING_WITH_LIBFUZZER)
42extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
43    auto bytes = SkData::MakeWithoutCopy(data, size);
44    FuzzAnimatedImage(bytes);
45    return 0;
46}
47#endif
48