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