xref: /aosp_15_r20/external/skia/fuzz/oss_fuzz/FuzzImage.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "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"
13 
FuzzImageDecode(const uint8_t * data,size_t size)14 bool FuzzImageDecode(const uint8_t *data, size_t size) {
15     auto img = SkImages::DeferredFromEncodedData(SkData::MakeWithoutCopy(data, size));
16     if (nullptr == img.get()) {
17         return false;
18     }
19 
20     auto s = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(128, 128));
21     if (!s) {
22         // May return nullptr in memory-constrained fuzzing environments
23         return false;
24     }
25 
26     s->getCanvas()->drawImage(img, 0, 0);
27     return true;
28 }
29 
30 #if defined(SK_BUILD_FOR_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)31 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32     if (size > 10240) {
33         return 0;
34     }
35     FuzzImageDecode(data, size);
36     return 0;
37 }
38 #endif
39