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 U32_AT_SIZE = 4;
29 constexpr size_t SIZE_FLAG = 2;
30
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
32 {
33 std::string srcPath (reinterpret_cast<const char*>(data), size);
34 std::string destPath = "";
35 std::shared_ptr<LIBZIP::ZlibCallbackInfo> zlibCallbackInfo;
36 bool includeHiddenFiles = false;
37 if (size % SIZE_FLAG == 0) {
38 includeHiddenFiles = true;
39 }
40 LIBZIP::OPTIONS options;
41 Zip(srcPath, destPath, options, includeHiddenFiles, zlibCallbackInfo);
42 std::string srcFile (reinterpret_cast<const char*>(data), size);
43 std::string destFile = "";
44 Unzip(srcFile, destFile, options, zlibCallbackInfo);
45 std::vector<std::string> srcFiles;
46 Zips(srcFiles, destPath, options, includeHiddenFiles, zlibCallbackInfo);
47 int64_t originalsSize = 0;
48 LIBZIP::GetOriginalSize(srcFile, originalsSize);
49 return true;
50 }
51 }
52
53 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56 /* Run your code on data */
57 if (data == nullptr) {
58 return 0;
59 }
60
61 if (size < OHOS::U32_AT_SIZE) {
62 return 0;
63 }
64 OHOS::DoSomethingInterestingWithMyAPI(data, size);
65 return 0;
66 }