• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkAndroidCodec.h"
24 #include "SkBitmap.h"
25 #include "SkCodec.h"
26 #include "SkString.h"
27 
28 #include "fuzzer/FuzzedDataProvider.h"
29 
30 #include <stddef.h>
31 #include <stdint.h>
32 
getAndroidPixels(std::string & contents,bool requestPremul)33 static int getAndroidPixels(std::string& contents, bool requestPremul) {
34   // Generate stream contents
35   std::unique_ptr<SkMemoryStream> stream = SkMemoryStream::MakeDirect(contents.data(), contents.size());
36   if (!stream) {
37     return 0;
38   }
39 
40   std::unique_ptr<SkCodec> c = SkCodec::MakeFromStream(std::move(stream),
41                                                        nullptr);
42   if (!c) {
43     return 0;
44   }
45 
46   std::unique_ptr<SkAndroidCodec> codec;
47   codec = SkAndroidCodec::MakeFromCodec(std::move(c));
48   if (!codec) {
49     return 0;
50   }
51 
52   SkImageInfo info = codec->getInfo();
53   const int width = info.width();
54   const int height = info.height();
55 
56   SkColorType decodeColorType = kN32_SkColorType;
57   SkAlphaType alphaType =
58       codec->computeOutputAlphaType(requestPremul);
59   const SkImageInfo decodeInfo =
60       SkImageInfo::Make(width, height, decodeColorType, alphaType);
61 
62   SkImageInfo bitmapInfo = decodeInfo;
63   SkBitmap decodingBitmap;
64   if (!decodingBitmap.tryAllocPixels(bitmapInfo)) {
65     return 0;
66   }
67 
68   codec->getAndroidPixels(
69       decodeInfo, decodingBitmap.getPixels(), decodingBitmap.rowBytes());
70   return 0;
71 }
72 
LLVMFuzzerTestOneInput(char * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(char *data, size_t size) {
74   if (size == 0) {
75     return 0;
76   }
77 
78   std::string sdata1(data, size);
79   getAndroidPixels(sdata1, true);
80   std::string sdata2(data, size);
81   getAndroidPixels(sdata2, false);
82   return 0;
83 }
84