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 LIBZIP::OPTIONS options;
42 Zip(srcPath, destPath, options, includeHiddenFiles, zlibCallbackInfo);
43 std::string srcFile (reinterpret_cast<const char*>(data), size);
44 std::string destFile = "";
45 Unzip(srcFile, destFile, options, zlibCallbackInfo);
46 std::vector<std::string> srcFiles;
47 Zips(srcFiles, destPath, options, includeHiddenFiles, zlibCallbackInfo);
48 int64_t originalsSize = 0;
49 LIBZIP::GetOriginalSize(srcFile, originalsSize);
50 return true;
51 }
52 }
53
54 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
56 {
57 /* Run your code on data */
58 if (data == nullptr) {
59 return 0;
60 }
61
62 if (size < OHOS::U32_AT_SIZE) {
63 return 0;
64 }
65
66 /* Validate the length of size */
67 if (size > OHOS::FOO_MAX_LEN) {
68 return 0;
69 }
70 OHOS::DoSomethingInterestingWithMyAPI(data, size);
71 return 0;
72 }