• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
19 
20 #include "pasteboard_client.h"
21 
22 using namespace OHOS::MiscServices;
23 
24 namespace OHOS {
25 constexpr size_t THRESHOLD = 10;
26 constexpr size_t OFFSET = 4;
27 constexpr size_t RANDNUM_ZERO = 0;
28 constexpr size_t RANDNUM_ONE = 1;
29 
ConvertToUint32(const uint8_t * ptr)30 uint32_t ConvertToUint32(const uint8_t *ptr)
31 {
32     if (ptr == nullptr) {
33         return 0;
34     }
35     uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
36     return bigVar;
37 }
38 
FuzzPasteboardclient(const uint8_t * rawData,size_t size)39 void FuzzPasteboardclient(const uint8_t *rawData, size_t size)
40 {
41     std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
42     std::shared_ptr<PasteDataRecord> pasteDataRecord = std::make_shared<PasteDataRecord>();
43     uint32_t code = ConvertToUint32(rawData);
44     rawData = rawData + OFFSET;
45     size = size - OFFSET;
46     std::string str(reinterpret_cast<const char *>(rawData), size);
47     switch (code) {
48         case RANDNUM_ZERO:
49             pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(str);
50             pasteDataRecord = PasteboardClient::GetInstance()->CreatePlainTextRecord(str);
51             break;
52         case RANDNUM_ONE:
53             pasteData = PasteboardClient::GetInstance()->CreateHtmlData(str);
54             pasteDataRecord = PasteboardClient::GetInstance()->CreateHtmlTextRecord(str);
55             break;
56         default:
57             pasteData = PasteboardClient::GetInstance()->CreateUriData(Uri(str));
58             pasteDataRecord = PasteboardClient::GetInstance()->CreateUriRecord(Uri(str));
59             break;
60     }
61     pasteData->AddRecord(pasteDataRecord);
62     std::vector<uint8_t> buffer;
63     pasteData->Encode(buffer);
64 
65     PasteData pasteData2;
66     pasteData2.Decode(buffer);
67     pasteData2.HasMimeType(std::string(reinterpret_cast<const char *>(rawData), size));
68     pasteData2.RemoveRecordAt(code);
69     pasteData2.ReplaceRecordAt(code, pasteDataRecord);
70 }
71 } // namespace OHOS
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
74 {
75     if (size < OHOS::THRESHOLD) {
76         return 0;
77     }
78     /* Run your code on data */
79     OHOS::FuzzPasteboardclient(data, size);
80     return 0;
81 }
82