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 "openuncompressedarchive_fuzzer.h"
17 #include "libpandafile/file.h"
18 #include "libziparchive/zip_archive.h"
19 
20 namespace OHOS {
CloseAndRemoveZipFile(panda::ZipArchiveHandle &handle, FILE *fp, const char *filename)21 void CloseAndRemoveZipFile(panda::ZipArchiveHandle &handle, FILE *fp, const char *filename)
22 {
23     panda::CloseArchiveFile(handle);
24     (void)fclose(fp);
25     (void)remove(filename);
26 }
27 
OpenUncompressedArchiveFuzzTest(const uint8_t *data, size_t size)28 void OpenUncompressedArchiveFuzzTest(const uint8_t *data, size_t size)
29 {
30     // Create zip file
31     const char *zip_filename = "__OpenUncompressedArchiveFuzzTest.zip";
32     const char *filename = panda::panda_file::ARCHIVE_FILENAME;
33     int ret = panda::CreateOrAddFileIntoZip(zip_filename, filename, data, size, APPEND_STATUS_CREATE, Z_NO_COMPRESSION);
34     if (ret != 0) {
35         (void)remove(zip_filename);
36         return;
37     }
38 
39     // Acquire entry
40 #ifdef PANDA_TARGET_WINDOWS
41     constexpr char const *mode = "rb";
42 #else
43     constexpr char const *mode = "rbe";
44 #endif
45     FILE *fp = fopen(zip_filename, mode);
46     if (fp == nullptr) {
47         (void)remove(zip_filename);
48         return;
49     }
50     panda::ZipArchiveHandle zipfile = nullptr;
51     if (panda::OpenArchiveFile(zipfile, fp) != panda::ZIPARCHIVE_OK) {
52         (void)fclose(fp);
53         (void)remove(zip_filename);
54         return;
55     }
56     if (panda::LocateFile(zipfile, filename) != panda::ZIPARCHIVE_OK) {
57         CloseAndRemoveZipFile(zipfile, fp, zip_filename);
58         return;
59     }
60     panda::EntryFileStat entry;
61     if (panda::GetCurrentFileInfo(zipfile, &entry) != panda::ZIPARCHIVE_OK) {
62         CloseAndRemoveZipFile(zipfile, fp, zip_filename);
63         return;
64     }
65     if (panda::OpenCurrentFile(zipfile) != panda::ZIPARCHIVE_OK) {
66         panda::CloseCurrentFile(zipfile);
67         CloseAndRemoveZipFile(zipfile, fp, zip_filename);
68         return;
69     }
70     panda::GetCurrentFileOffset(zipfile, &entry);
71     // Call OpenUncompressedArchive
72     {
73         panda::panda_file::File::OpenUncompressedArchive(fileno(fp), zip_filename, entry.GetUncompressedSize(),
74                                                          entry.GetOffset());
75     }
76     panda::CloseCurrentFile(zipfile);
77     CloseAndRemoveZipFile(zipfile, fp, zip_filename);
78 }
79 }  // namespace OHOS
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83 {
84     /* Run your code on data */
85     OHOS::OpenUncompressedArchiveFuzzTest(data, size);
86     return 0;
87 }
88