1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <cstddef>
17094332d3Sopenharmony_ci#include <cstdint>
18094332d3Sopenharmony_ci#include "hdf_log.h"
19094332d3Sopenharmony_ci#include "hidddk_fuzzer.h"
20094332d3Sopenharmony_ci#include "v1_0/hid_ddk_stub.h"
21094332d3Sopenharmony_ci#include "v1_0/ihid_ddk.h"
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ciusing namespace OHOS::HDI::Input::Ddk::V1_0;
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_cinamespace OHOS {
26094332d3Sopenharmony_ciconstexpr size_t THRESHOLD = 10;
27094332d3Sopenharmony_ciconstexpr int32_t OFFSET = 4;
28094332d3Sopenharmony_ciconstexpr int32_t ZERO_BIT = 0;
29094332d3Sopenharmony_ciconstexpr int32_t FIRST_BIT = 1;
30094332d3Sopenharmony_ciconstexpr int32_t SECOND_BIT = 2;
31094332d3Sopenharmony_ciconstexpr int32_t THIRD_BIT = 3;
32094332d3Sopenharmony_ciconstexpr int32_t ZERO_MOVE_LEN = 24;
33094332d3Sopenharmony_ciconstexpr int32_t FIRST_MOVE_LEN = 16;
34094332d3Sopenharmony_ciconstexpr int32_t SECOND_MOVE_LEN = 8;
35094332d3Sopenharmony_ciconst std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_0.IHidDdk";
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ciuint32_t Convert2Uint32(const uint8_t *ptr)
38094332d3Sopenharmony_ci{
39094332d3Sopenharmony_ci    if (ptr == nullptr) {
40094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: ptr is null", __func__);
41094332d3Sopenharmony_ci        return 0;
42094332d3Sopenharmony_ci    }
43094332d3Sopenharmony_ci    /*
44094332d3Sopenharmony_ci     * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
45094332d3Sopenharmony_ci     * and the third digit no left
46094332d3Sopenharmony_ci     */
47094332d3Sopenharmony_ci    return (ptr[ZERO_BIT] << ZERO_MOVE_LEN) | (ptr[FIRST_BIT] << FIRST_MOVE_LEN) | (ptr[SECOND_BIT] <<
48094332d3Sopenharmony_ci        SECOND_MOVE_LEN) | (ptr[THIRD_BIT]);
49094332d3Sopenharmony_ci}
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_cibool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
52094332d3Sopenharmony_ci{
53094332d3Sopenharmony_ci    if (rawData == nullptr) {
54094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: rawData is null", __func__);
55094332d3Sopenharmony_ci        return false;
56094332d3Sopenharmony_ci    }
57094332d3Sopenharmony_ci    uint32_t code = Convert2Uint32(rawData);
58094332d3Sopenharmony_ci    rawData = rawData + OFFSET;
59094332d3Sopenharmony_ci    size = size - OFFSET;
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    MessageParcel data;
62094332d3Sopenharmony_ci    data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
63094332d3Sopenharmony_ci    data.WriteBuffer(rawData, size);
64094332d3Sopenharmony_ci    data.RewindRead(0);
65094332d3Sopenharmony_ci    MessageParcel reply;
66094332d3Sopenharmony_ci    MessageOption option;
67094332d3Sopenharmony_ci    sptr<IHidDdk> hidDdkInterface = IHidDdk::Get(false);
68094332d3Sopenharmony_ci    if (hidDdkInterface == nullptr) {
69094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
70094332d3Sopenharmony_ci        return false;
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci    sptr<HidDdkStub> hidDdk = new HidDdkStub(hidDdkInterface);
73094332d3Sopenharmony_ci    if (hidDdk == nullptr) {
74094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: new hidDdk failed", __func__);
75094332d3Sopenharmony_ci        return false;
76094332d3Sopenharmony_ci    }
77094332d3Sopenharmony_ci    hidDdk->OnRemoteRequest(code, data, reply, option);
78094332d3Sopenharmony_ci
79094332d3Sopenharmony_ci    return true;
80094332d3Sopenharmony_ci}
81094332d3Sopenharmony_ci} // namespace OHOS
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci/* Fuzzer entry point */
84094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85094332d3Sopenharmony_ci{
86094332d3Sopenharmony_ci    if (size < OHOS::THRESHOLD) {
87094332d3Sopenharmony_ci        return 0;
88094332d3Sopenharmony_ci    }
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
91094332d3Sopenharmony_ci    return 0;
92094332d3Sopenharmony_ci}
93094332d3Sopenharmony_ci
94