1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
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 #include "ndktypeface_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "get_object.h"
22 #include "rosen_text/typography.h"
23 #include "rosen_text/typography_create.h"
24 
25 #include "drawing_memory_stream.h"
26 #include "drawing_typeface.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
31 constexpr size_t DATA_MIN_SIZE = 2;
32 constexpr size_t MAX_ARRAY_SIZE = 5000;
33 } // namespace
34 
35 namespace Drawing {
36 
NativeDrawingTypefaceTest001(const uint8_t * data,size_t size)37 void NativeDrawingTypefaceTest001(const uint8_t* data, size_t size)
38 {
39     if (data == nullptr || size < DATA_MIN_SIZE) {
40         return;
41     }
42     // initialize
43     g_data = data;
44     g_size = size;
45     g_pos = 0;
46 
47     int index = GetObject<int>();
48     uint32_t path_size = GetObject<uint32_t>() % MAX_ARRAY_SIZE + 1;
49     char* path = new char[path_size];
50     for (size_t i = 0; i < path_size; i++) {
51         path[i] = GetObject<char>();
52     }
53     path[path_size - 1] = '\0';
54     OH_Drawing_Typeface* typeface = OH_Drawing_TypefaceCreateDefault();
55     OH_Drawing_TypefaceCreateFromFile(nullptr, 0);
56     OH_Drawing_Typeface* typefaceOne = OH_Drawing_TypefaceCreateFromFile(path, index);
57 
58     char pathTwo[] = "system/fonts/NotoSansBengaliUI-Bold.ttf";
59     OH_Drawing_Typeface* typefaceTwo = OH_Drawing_TypefaceCreateFromFile(pathTwo, index);
60 
61     bool copyData = GetObject<bool>();
62     uint32_t str_size = GetObject<uint32_t>() % MAX_ARRAY_SIZE;
63     char* str = new char[str_size];
64     for (size_t i = 0; i < str_size; i++) {
65         str[i] = GetObject<char>();
66     }
67     int32_t streamIndex = GetObject<int32_t>() % MAX_ARRAY_SIZE;
68     OH_Drawing_MemoryStream* memoryStream = OH_Drawing_MemoryStreamCreate(str, str_size, copyData);
69     OH_Drawing_TypefaceCreateFromStream(nullptr, streamIndex);
70     if (path != nullptr) {
71         delete [] path;
72         path = nullptr;
73     }
74     if (str != nullptr) {
75         delete [] str;
76         str = nullptr;
77     }
78 
79     OH_Drawing_TypefaceDestroy(typeface);
80     OH_Drawing_TypefaceDestroy(typefaceOne);
81     OH_Drawing_TypefaceDestroy(typefaceTwo);
82     OH_Drawing_MemoryStreamDestroy(memoryStream);
83 }
NativeDrawingTypefaceTest002(const uint8_t * data,size_t size)84 void NativeDrawingTypefaceTest002(const uint8_t* data, size_t size)
85 {
86     if (data == nullptr || size < DATA_MIN_SIZE) {
87         return;
88     }
89     // initialize
90     g_data = data;
91     g_size = size;
92     g_pos = 0;
93 
94     bool copyData = GetObject<bool>();
95     uint32_t str_size = GetObject<uint32_t>() % MAX_ARRAY_SIZE;
96     char* str = new char[str_size];
97     for (size_t i = 0; i < str_size; i++) {
98         str[i] = GetObject<char>();
99     }
100     int32_t streamIndex = GetObject<int32_t>() % MAX_ARRAY_SIZE;
101     OH_Drawing_MemoryStream* memoryStream = OH_Drawing_MemoryStreamCreate(str, str_size, copyData);
102     OH_Drawing_Typeface* typeface = OH_Drawing_TypefaceCreateFromStream(memoryStream, streamIndex);
103 
104     if (str != nullptr) {
105         delete [] str;
106         str = nullptr;
107     }
108     OH_Drawing_TypefaceDestroy(typeface);
109 }
110 
111 
112 } // namespace Drawing
113 } // namespace Rosen
114 } // namespace OHOS
115 
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
118 {
119     /* Run your code on data */
120     OHOS::Rosen::Drawing::NativeDrawingTypefaceTest001(data, size);
121     OHOS::Rosen::Drawing::NativeDrawingTypefaceTest002(data, size);
122     return 0;
123 }
124