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 "isvalidnumberstring_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "addcoreservicetoken_fuzzer.h"
23 #include "napi_util.h"
24 #include "sim_char_decode.h"
25 #include "sim_number_decode.h"
26 #include "system_ability_definition.h"
27 
28 using namespace OHOS::Telephony;
29 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)30 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
31 {
32     if (data == nullptr || size == 0) {
33         return;
34     }
35 
36     auto simCharDecode = std::make_shared<SimCharDecode>();
37     auto simNumberDecode = std::make_shared<SimNumberDecode>();
38     int32_t offset = 0;
39     int32_t bcdExtType = static_cast<int32_t>(*data + offset);
40     offset += sizeof(int32_t);
41     uint8_t result = static_cast<uint8_t>(*data + offset);
42     offset += sizeof(int32_t);
43     char argument = static_cast<char>(*data + offset);
44     std::string str(reinterpret_cast<const char *>(data), size);
45     std::string number(reinterpret_cast<const char *>(data), size);
46     std::vector<uint8_t> bcdCodes;
47     bcdCodes.push_back(result);
48     bool includeLen = static_cast<int32_t>(*data % 2);
49     simCharDecode->IsChineseString(str);
50     simNumberDecode->IsValidNumberString(number);
51     simNumberDecode->CharToBCD(argument, result, bcdExtType);
52     simNumberDecode->NumberConvertToBCD(number, bcdCodes, includeLen, bcdExtType);
53     simNumberDecode->BcdToChar(result, argument, bcdExtType);
54 }
55 } // namespace OHOS
56 
57 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)58 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
59 {
60     OHOS::AddCoreServiceTokenFuzzer token;
61     return 0;
62 }
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
66 {
67     /* Run your code on data */
68     OHOS::DoSomethingInterestingWithMyAPI(data, size);
69     return 0;
70 }
71