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