1 /*
2 * Copyright (c) 2022-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 "urihandler_fuzzer.h"
17
18 #include "copy_uri_handler.h"
19
20 using namespace OHOS::MiscServices;
21
22 namespace OHOS {
23 constexpr size_t THRESHOLD = 10;
24 constexpr size_t OFFSET = 4;
ConvertToUint32(const uint8_t * ptr)25 uint32_t ConvertToUint32(const uint8_t *ptr)
26 {
27 if (ptr == nullptr) {
28 return 0;
29 }
30 uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
31 return bigVar;
32 }
33
FuzzUriToFd(const uint8_t * rawData,size_t size)34 bool FuzzUriToFd(const uint8_t *rawData, size_t size)
35 {
36 uint32_t value = ConvertToUint32(rawData);
37 rawData = rawData + OFFSET;
38 size = size - OFFSET;
39 std::string uri(reinterpret_cast<const char *>(rawData), size);
40 bool isClient = value % 2;
41 CopyUriHandler uriHandler;
42 uriHandler.ToFd(uri, isClient);
43 return true;
44 }
45 } // namespace OHOS
46
47 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)48 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
49 {
50 if (size < OHOS::THRESHOLD) {
51 return 0;
52 }
53 /* Run your code on data */
54 OHOS::FuzzUriToFd(data, size);
55 return 0;
56 }
57