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 <cstddef>
17 #include <cstdint>
18 #include "breakiterator_fuzzer.h"
19 #include "i18n_break_iterator.h"
20 
21 namespace OHOS {
22     const int VALUE_NUMBER = 3;
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)23     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
24     {
25         using namespace Global::I18n;
26 
27         if (size < sizeof(int32_t) * VALUE_NUMBER) {
28             return false;
29         }
30 
31         uint32_t offset = 0;
32 
33         std::string text(reinterpret_cast<const char*>(data + offset), size - sizeof(int32_t) * 3);
34         offset += size - sizeof(int32_t) * VALUE_NUMBER;
35 
36         int32_t number = *(reinterpret_cast<const int32_t*>(data + offset));
37         offset += sizeof(int32_t);
38 
39         int32_t next = *(reinterpret_cast<const int32_t*>(data + offset));
40         offset += sizeof(int32_t);
41 
42         int32_t following = *(reinterpret_cast<const int32_t*>(data + offset));
43 
44         I18nBreakIterator iterator(text);
45         iterator.SetText(text.c_str());
46 
47         std::string str;
48         iterator.GetText(str);
49 
50         iterator.IsBoundary(number);
51         iterator.Current();
52         iterator.First();
53         iterator.Last();
54 
55         iterator.Next(next);
56         iterator.Next();
57         iterator.Previous();
58 
59         iterator.Following(following);
60 
61         return true;
62     }
63 }
64 
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
67 {
68     /* Run your code on data */
69     OHOS::DoSomethingInterestingWithMyAPI(data, size);
70     return 0;
71 }
72 
73