• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 #ifndef PANDA_LIBZIPARCHIVE_ZIP_ARCHIVE_H_
17 #define PANDA_LIBZIPARCHIVE_ZIP_ARCHIVE_H_
18 
19 #include <cstdint>
20 #include <vector>
21 #include "macros.h"
22 #include "unzip.h"
23 #include "zip.h"
24 
25 namespace ark {
26 
27 constexpr int ZIPARCHIVE_OK = 0;
28 constexpr int ZIPARCHIVE_ERR = 1;
29 
30 using ZipArchiveHandle = unzFile;
31 
32 struct EntryFileStat {
33 public:
GetUncompressedSizeEntryFileStat34     uint32_t GetUncompressedSize() const
35     {
36         return (uint32_t)fileStat.uncompressed_size;
37     }
38 
GetCompressedSizeEntryFileStat39     uint32_t GetCompressedSize() const
40     {
41         return (uint32_t)fileStat.compressed_size;
42     }
43 
GetOffsetEntryFileStat44     inline uint32_t GetOffset() const
45     {
46         return offset;
47     }
48 
IsCompressedEntryFileStat49     inline bool IsCompressed() const
50     {
51         return fileStat.compression_method != 0;
52     }
53 
54     // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
55     unz_file_info fileStat;
56     // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
57     uint32_t offset;
58 };
59 
60 struct GlobalStat {
61 public:
GetNumberOfEntryGlobalStat62     uint32_t GetNumberOfEntry() const
63     {
64         return (uint32_t)ginfo.number_entry;
65     }
66     // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
67     unz_global_info ginfo;
68 };
69 
70 /*
71  * Judge whether magic is zip magic.
72  */
73 PANDA_PUBLIC_API bool IsZipMagic(uint32_t magic);
74 
75 /*
76  * Open a Zip archive from filename path, and sets handle for the file.
77  * This handle must be released by calling CloseArchive with this handle.
78  * CloseArchive will close the file opened.
79  *
80  * Returns 0 on success, and 1 on failure.
81  */
82 int OpenArchive(ZipArchiveHandle &handle, const char *path);
83 
84 /*
85  * Close archive opened with OpenArchive, releasing internal resources associated with it.
86  */
87 int CloseArchive(ZipArchiveHandle &handle);
88 
89 /*
90  * Open a Zip archive from opened file FILE* fp, and sets handle for the file.
91  * This handle must be released by calling CloseArchiveFile with this handle.
92  * CloseArchiveFile will not close the fp. It is the caller's responsibility.
93  *
94  * Returns 0 on success, and 1 on failure.
95  */
96 PANDA_PUBLIC_API int OpenArchiveFile(ZipArchiveHandle &handle, FILE *fp);
97 
98 /*
99  * Close archive opened with OpenArchiveFile, releasing internal resources associated with it.
100  *
101  * Returns 0 on success, and 1 on failure.
102  */
103 PANDA_PUBLIC_API int CloseArchiveFile(ZipArchiveHandle &handle);
104 
105 /*
106  * Write info about the ZipFile in the *gstat structure.
107  * No preparation of the structure is needed
108  *
109  * Returns 0 on success, and 1 on failure.
110  */
111 int GetGlobalFileInfo(ZipArchiveHandle &handle, GlobalStat *gstat);
112 
113 /*
114  * Set the current file of the zipfile to the next file.
115  *
116  * Returns 0 on success, and 1 on failure.
117  */
118 int GoToNextFile(ZipArchiveHandle &handle);
119 
120 /*
121  * Try locate the file filename in the zipfile.
122  *
123  * Returns 0 on success, and 1 on failure.
124  */
125 PANDA_PUBLIC_API int LocateFile(ZipArchiveHandle &handle, const char *filename);
126 
127 /*
128  * Get Info about the current file within ZipFile and write info into the *entry structure.
129  * No preparation of the structure is needed
130  *
131  * Returns 0 on success, and 1 on failure.
132  */
133 PANDA_PUBLIC_API int GetCurrentFileInfo(ZipArchiveHandle &handle, EntryFileStat *entry);
134 
135 /*
136  * Open for reading data the current file in the zipfile.
137  * This handle must be released by calling CloseCurrentFile with this handle.
138  *
139  * Returns 0 on success, and 1 on failure.
140  */
141 PANDA_PUBLIC_API int OpenCurrentFile(ZipArchiveHandle &handle);
142 
143 /*
144  * Get the current file offset opened with OpenCurrentFile. The offset will be stored into entry->offset.
145  */
146 PANDA_PUBLIC_API void GetCurrentFileOffset(ZipArchiveHandle &handle, EntryFileStat *entry);
147 
148 /*
149  * Close the file in zip opened with unzOpenCurrentFile
150  *
151  * Returns 0 on success, and 1 on failure.
152  */
153 PANDA_PUBLIC_API int CloseCurrentFile(ZipArchiveHandle &handle);
154 
155 /*
156  * Uncompress a given zip archive represented with handle to buf of size |buf_size|.
157  * This size is expected to be equal or larger than the uncompressed length of the zip entry.
158  *
159  * Returns 0 on success and 1 on failure.
160  */
161 PANDA_PUBLIC_API int ExtractToMemory(ZipArchiveHandle &handle, void *buf, size_t bufSize);
162 
163 /*
164  * Add a new file filename(resident in memory pbuf which has size of size |buf_size|) to the archive zipname,
165  * append takes value from APPEND_STATUS_CREATE(which will create the archive zipname for first time) and
166  * APPEND_STATUS_ADDINZIP(which will append filename into exsisted zip archive zipname).
167  * level takes value from Z_BEST_COMPRESSION(which will deflate the pbuf with best compression effect) and
168  * Z_NO_COMPRESSION(which will store the pbuf into zipname without compression).
169  *
170  * Returns 0 on success and 1 on failure.
171  */
172 int CreateOrAddFileIntoZip(const char *zipname, const char *filename, std::vector<uint8_t> *buf,
173                            int append = APPEND_STATUS_CREATE, int level = Z_BEST_COMPRESSION);
174 }  // namespace ark
175 
176 #endif  // PANDA_LIBZIPARCHIVE_ZIP_ARCHIVE_H_
177