1 /////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Mateusz Jurczyk (mjurczyk@google.com)
4 //
5 // Copyright 2020 Google LLC
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // https://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 #include <cstdio>
21 #include <string>
22
23 #include "SkAlphaType.h"
24 #include "SkAndroidCodec.h"
25 #include "SkBitmap.h"
26 #include "SkCodec.h"
27 #include "SkColorType.h"
28 #include "SkImageInfo.h"
29 #include "SkStream.h"
30 #include "SkString.h"
31
32 #include "fuzzer/FuzzedDataProvider.h"
33
34 #include <stddef.h>
35 #include <stdint.h>
36
getAndroidPixels(std::string & contents,bool requestPremul)37 static int getAndroidPixels(std::string& contents, bool requestPremul) {
38 // Generate stream contents
39 std::unique_ptr<SkMemoryStream> stream = SkMemoryStream::MakeDirect(contents.data(), contents.size());
40 if (!stream) {
41 return 0;
42 }
43
44 std::unique_ptr<SkCodec> c = SkCodec::MakeFromStream(std::move(stream),
45 nullptr);
46 if (!c) {
47 return 0;
48 }
49
50 std::unique_ptr<SkAndroidCodec> codec;
51 codec = SkAndroidCodec::MakeFromCodec(std::move(c));
52 if (!codec) {
53 return 0;
54 }
55
56 SkImageInfo info = codec->getInfo();
57 const int width = info.width();
58 const int height = info.height();
59
60 SkColorType decodeColorType = kN32_SkColorType;
61 SkAlphaType alphaType =
62 codec->computeOutputAlphaType(requestPremul);
63 const SkImageInfo decodeInfo =
64 SkImageInfo::Make(width, height, decodeColorType, alphaType);
65
66 SkImageInfo bitmapInfo = decodeInfo;
67 SkBitmap decodingBitmap;
68 if (!decodingBitmap.tryAllocPixels(bitmapInfo)) {
69 return 0;
70 }
71
72 codec->getAndroidPixels(
73 decodeInfo, decodingBitmap.getPixels(), decodingBitmap.rowBytes());
74 return 0;
75 }
76
LLVMFuzzerTestOneInput(char * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(char *data, size_t size) {
78 if (size == 0) {
79 return 0;
80 }
81
82 std::string sdata1(data, size);
83 getAndroidPixels(sdata1, true);
84 std::string sdata2(data, size);
85 getAndroidPixels(sdata2, false);
86 return 0;
87 }
88