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 "zip_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "zip.h"
22 #include "zlib_callback_info.h"
23
24 #include "securec.h"
25
26 using namespace OHOS::AppExecFwk;
27 namespace OHOS {
28 constexpr size_t FOO_MAX_LEN = 1024;
29 constexpr size_t U32_AT_SIZE = 4;
30 constexpr size_t SIZE_FLAG = 2;
31
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)32 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
33 {
34 std::string srcPath (reinterpret_cast<const char*>(data), size);
35 std::string destPath = "";
36 std::shared_ptr<LIBZIP::ZlibCallbackInfo> zlibCallbackInfo;
37 bool includeHiddenFiles = false;
38 if (size % SIZE_FLAG == 0) {
39 includeHiddenFiles = true;
40 }
41 Zip(srcPath, destPath, includeHiddenFiles, zlibCallbackInfo);
42 LIBZIP::OPTIONS options;
43 std::string srcFile (reinterpret_cast<const char*>(data), size);
44 std::string destFile = "";
45 Unzip(srcFile, destFile, options, zlibCallbackInfo);
46 return true;
47 }
48 }
49
50 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
52 {
53 /* Run your code on data */
54 if (data == nullptr) {
55 return 0;
56 }
57
58 if (size < OHOS::U32_AT_SIZE) {
59 return 0;
60 }
61
62 /* Validate the length of size */
63 if (size > OHOS::FOO_MAX_LEN) {
64 return 0;
65 }
66 OHOS::DoSomethingInterestingWithMyAPI(data, size);
67 return 0;
68 }