1 // Copyright 2020 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 /////////////////////////////////////////////////////////////////////////////
16
17 #include <fuzzer/FuzzedDataProvider.h>
18
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <string>
23
24 #include "gd.h"
25 #include "gdfontg.h"
26 #include "gdfontl.h"
27 #include "gdfontmb.h"
28 #include "gdfonts.h"
29 #include "gdfontt.h"
30
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)31 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
32 FuzzedDataProvider stream(data, size);
33 const uint8_t slate_width = stream.ConsumeIntegral<uint8_t>();
34 const uint8_t slate_height = stream.ConsumeIntegral<uint8_t>();
35 gdImagePtr slate_image = gdImageCreateTrueColor(slate_width, slate_height);
36 if (slate_image == nullptr) {
37 return 0;
38 }
39
40 const int x_position = stream.ConsumeIntegral<int>();
41 const int y_position = stream.ConsumeIntegral<int>();
42 const int text_color = stream.ConsumeIntegral<int>();
43 const gdFontPtr font_ptr = stream.PickValueInArray(
44 {gdFontGetGiant(), gdFontGetLarge(), gdFontGetMediumBold(),
45 gdFontGetSmall(), gdFontGetTiny()});
46 const std::string text = stream.ConsumeRemainingBytesAsString();
47
48 gdImageString(slate_image, font_ptr, x_position, y_position,
49 reinterpret_cast<uint8_t*>(const_cast<char*>(text.c_str())),
50 text_color);
51 gdImageDestroy(slate_image);
52 return 0;
53 }
54