1/*
2 * Copyright (c) 2023 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 <cstddef>
17#include <cstdint>
18#include "hdf_log.h"
19#include "hidddk_fuzzer.h"
20#include "v1_0/hid_ddk_stub.h"
21#include "v1_0/ihid_ddk.h"
22
23using namespace OHOS::HDI::Input::Ddk::V1_0;
24
25namespace OHOS {
26constexpr size_t THRESHOLD = 10;
27constexpr int32_t OFFSET = 4;
28constexpr int32_t ZERO_BIT = 0;
29constexpr int32_t FIRST_BIT = 1;
30constexpr int32_t SECOND_BIT = 2;
31constexpr int32_t THIRD_BIT = 3;
32constexpr int32_t ZERO_MOVE_LEN = 24;
33constexpr int32_t FIRST_MOVE_LEN = 16;
34constexpr int32_t SECOND_MOVE_LEN = 8;
35const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_0.IHidDdk";
36
37uint32_t Convert2Uint32(const uint8_t *ptr)
38{
39    if (ptr == nullptr) {
40        HDF_LOGE("%{public}s: ptr is null", __func__);
41        return 0;
42    }
43    /*
44     * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
45     * and the third digit no left
46     */
47    return (ptr[ZERO_BIT] << ZERO_MOVE_LEN) | (ptr[FIRST_BIT] << FIRST_MOVE_LEN) | (ptr[SECOND_BIT] <<
48        SECOND_MOVE_LEN) | (ptr[THIRD_BIT]);
49}
50
51bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
52{
53    if (rawData == nullptr) {
54        HDF_LOGE("%{public}s: rawData is null", __func__);
55        return false;
56    }
57    uint32_t code = Convert2Uint32(rawData);
58    rawData = rawData + OFFSET;
59    size = size - OFFSET;
60
61    MessageParcel data;
62    data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
63    data.WriteBuffer(rawData, size);
64    data.RewindRead(0);
65    MessageParcel reply;
66    MessageOption option;
67    sptr<IHidDdk> hidDdkInterface = IHidDdk::Get(false);
68    if (hidDdkInterface == nullptr) {
69        HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
70        return false;
71    }
72    sptr<HidDdkStub> hidDdk = new HidDdkStub(hidDdkInterface);
73    if (hidDdk == nullptr) {
74        HDF_LOGE("%{public}s: new hidDdk failed", __func__);
75        return false;
76    }
77    hidDdk->OnRemoteRequest(code, data, reply, option);
78
79    return true;
80}
81} // namespace OHOS
82
83/* Fuzzer entry point */
84extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85{
86    if (size < OHOS::THRESHOLD) {
87        return 0;
88    }
89
90    OHOS::DoSomethingInterestingWithMyAPI(data, size);
91    return 0;
92}
93
94