1/*
2 * Copyright (c) 2022 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 "pointerstyle_fuzzer.h"
17
18#include "ipc_skeleton.h"
19#include "securec.h"
20
21#include "input_manager.h"
22
23namespace OHOS {
24namespace MMI {
25template <class T> size_t GetObject(const uint8_t *data, size_t size, T &object)
26{
27    size_t objectSize = sizeof(object);
28    if (objectSize > size) {
29        return 0;
30    }
31    errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
32    if (ret != EOK) {
33        return 0;
34    }
35    return objectSize;
36}
37
38size_t GetString(const uint8_t *data, size_t size, char *object, size_t objectSize)
39{
40    if (objectSize > size) {
41        return 0;
42    }
43    errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
44    if (ret != EOK) {
45        return 0;
46    }
47    return objectSize;
48}
49
50void UpdateHotAreas(const uint8_t *data, size_t size, WindowInfo &windowInfo)
51{
52    size_t startPos = 0;
53    std::vector<Rect> defaultHotAreasInfo;
54    std::vector<Rect> pointerHotAreasInfo;
55    for (size_t j = 0; j < WindowInfo::MAX_HOTAREA_COUNT; ++j) {
56        Rect defaultRect;
57        startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.height);
58        startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.width);
59        startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.x);
60        startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.y);
61        defaultHotAreasInfo.push_back(defaultRect);
62        Rect pointerRect;
63        startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.height);
64        startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.width);
65        startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.x);
66        startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.y);
67        pointerHotAreasInfo.push_back(pointerRect);
68    }
69    windowInfo.defaultHotAreas = defaultHotAreasInfo;
70    windowInfo.pointerHotAreas = pointerHotAreasInfo;
71}
72
73void UpdateDisplayInfo(const uint8_t *data, size_t size, int32_t windowId)
74{
75    DisplayGroupInfo displayGroupInfo;
76    size_t startPos = 0;
77    size_t stringSize = 4;
78    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.width);
79    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.height);
80    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.focusWindowId);
81    std::vector<WindowInfo> windowsInfo;
82    std::vector<DisplayInfo> displaysInfo;
83    WindowInfo windowInfo;
84    windowInfo.id = windowId;
85    windowInfo.pid = IPCSkeleton::GetCallingPid();
86    startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.uid);
87    startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.x);
88    startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.y);
89    startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.width);
90    startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.height);
91    UpdateHotAreas(data, size, windowInfo);
92    windowsInfo.push_back(windowInfo);
93
94    DisplayInfo displayInfo;
95    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.id);
96    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.x);
97    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.y);
98    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.width);
99    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.height);
100    startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.dpi);
101    char name[] = "name";
102    startPos += GetString(data + startPos, size - startPos, name, stringSize);
103    displayInfo.name = name;
104    char uniq[] = "uniq";
105    GetString(data + startPos, size - startPos, uniq, stringSize);
106    displayInfo.uniq = uniq;
107    displaysInfo.push_back(displayInfo);
108    displayGroupInfo.windowsInfo = windowsInfo;
109    displayGroupInfo.displaysInfo = displaysInfo;
110    InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo);
111}
112
113void PointerStyleFuzzTest(const uint8_t *data, size_t size)
114{
115    int32_t windowId;
116    size_t startPos = 0;
117    startPos += GetObject<int32_t>(data + startPos, size - startPos, windowId);
118    UpdateDisplayInfo(data, size, windowId);
119    PointerStyle pointerStyle;
120    GetObject<int32_t>(data + startPos, size - startPos, pointerStyle.id);
121    InputManager::GetInstance()->SetPointerStyle(windowId, pointerStyle);
122    InputManager::GetInstance()->GetPointerStyle(windowId, pointerStyle);
123}
124} // MMI
125} // OHOS
126
127/* Fuzzer entry point */
128extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
129{
130    /* Run your code on data */
131    OHOS::MMI::PointerStyleFuzzTest(data, size);
132    return 0;
133}