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 #ifndef OHOS_ABILITY_BASE_ZIP_FILE_H 17 #define OHOS_ABILITY_BASE_ZIP_FILE_H 18 19 #include <atomic> 20 #include <memory> 21 #include <mutex> 22 #include <set> 23 #include <string> 24 #include <unordered_map> 25 #include <vector> 26 27 #include "file_mapper.h" 28 #include "unzip.h" 29 30 namespace OHOS { 31 namespace AbilityBase { 32 class ZipFileReader; 33 struct CentralDirEntry; 34 struct ZipEntry; 35 using ZipPos = ZPOS64_T; 36 using ZipEntryMap = std::unordered_map<std::string, ZipEntry>; 37 using BytePtr = Byte *; 38 39 // Local file header: descript in APPNOTE-6.3.4 40 // local file header signature 4 bytes (0x04034b50) 41 // version needed to extract 2 bytes 42 // general purpose bit flag 2 bytes 43 // compression method 2 bytes 10 44 // last mod file time 2 bytes 45 // last mod file date 2 bytes 46 // crc-32 4 bytes 47 // compressed size 4 bytes 22 48 // uncompressed size 4 bytes 49 // file name length 2 bytes 50 // extra field length 2 bytes 30 51 struct __attribute__((packed)) LocalHeader { 52 uint32_t signature = 0; 53 uint16_t versionNeeded = 0; 54 uint16_t flags = 0; 55 uint16_t compressionMethod = 0; 56 uint16_t modifiedTime = 0; 57 uint16_t modifiedDate = 0; 58 uint32_t crc = 0; 59 uint32_t compressedSize = 0; 60 uint32_t uncompressedSize = 0; 61 uint16_t nameSize = 0; 62 uint16_t extraSize = 0; 63 }; 64 65 // central file header 66 // Central File header: 67 // central file header signature 4 bytes (0x02014b50) 68 // version made by 2 bytes 69 // version needed to extract 2 bytes 70 // general purpose bit flag 2 bytes 10 71 // compression method 2 bytes 72 // last mod file time 2 bytes 73 // last mod file date 2 bytes 74 // crc-32 4 bytes 20 75 // compressed size 4 bytes 76 // uncompressed size 4 bytes 77 // file name length 2 bytes 30 78 // extra field length 2 bytes 79 // file comment length 2 bytes 80 // disk number start 2 bytes 81 // internal file attributes 2 bytes 82 // external file attributes 4 bytes 83 // relative offset of local header 4 bytes 46byte 84 struct __attribute__((packed)) CentralDirEntry { 85 uint32_t signature = 0; 86 uint16_t versionMade = 0; 87 uint16_t versionNeeded = 0; 88 uint16_t flags = 0; // general purpose bit flag 89 uint16_t compressionMethod = 0; 90 uint16_t modifiedTime = 0; 91 uint16_t modifiedDate = 0; 92 uint32_t crc = 0; 93 uint32_t compressedSize = 0; 94 uint32_t uncompressedSize = 0; 95 uint16_t nameSize = 0; 96 uint16_t extraSize = 0; 97 uint16_t commentSize = 0; 98 uint16_t diskNumStart = 0; 99 uint16_t internalAttr = 0; 100 uint32_t externalAttr = 0; 101 uint32_t localHeaderOffset = 0; 102 }; 103 104 // end of central directory packed structure 105 // end of central dir signature 4 bytes (0x06054b50) 106 // number of this disk 2 bytes 107 // number of the disk with the 108 // start of the central directory 2 bytes 109 // total number of entries in the 110 // central directory on this disk 2 bytes 111 // total number of entries in 112 // the central directory 2 bytes 113 // size of the central directory 4 bytes 114 // offset of start of central 115 // directory with respect to 116 // the starting disk number 4 bytes 117 // .ZIP file comment length 2 bytes 118 struct __attribute__((packed)) EndDir { 119 uint32_t signature = 0; 120 uint16_t numDisk = 0; 121 uint16_t startDiskOfCentralDir = 0; 122 uint16_t totalEntriesInThisDisk = 0; 123 uint16_t totalEntries = 0; 124 uint32_t sizeOfCentralDir = 0; 125 uint32_t offset = 0; 126 uint16_t commentLen = 0; 127 }; 128 129 // Data descriptor: 130 // data descriptor signature 4 bytes (0x06054b50) 131 // crc-32 4 bytes 132 // compressed size 4 bytes 133 // uncompressed size 4 bytes 134 // This descriptor MUST exist if bit 3 of the general purpose bit flag is set (see below). 135 // It is byte aligned and immediately follows the last byte of compressed data. 136 struct __attribute__((packed)) DataDesc { 137 uint32_t signature = 0; 138 uint32_t crc = 0; 139 uint32_t compressedSize = 0; 140 uint32_t uncompressedSize = 0; 141 }; 142 143 struct ZipEntry { 144 ZipEntry() = default; 145 explicit ZipEntry(const CentralDirEntry ¢ralEntry); 146 ~ZipEntry() = default; // for CodeDEX warning 147 148 uint16_t compressionMethod = 0; 149 uint32_t uncompressedSize = 0; 150 uint32_t compressedSize = 0; 151 uint32_t localHeaderOffset = 0; 152 uint32_t crc = 0; 153 uint16_t flags = 0; 154 uint16_t modifiedTime = 0; 155 uint16_t modifiedDate = 0; 156 std::string fileName; 157 }; 158 159 struct DirTreeNode { 160 bool isDir = false; 161 std::unordered_map<std::string, std::shared_ptr<DirTreeNode>> children; 162 }; 163 164 enum class CacheMode: uint32_t { 165 CACHE_NONE = 0, 166 CACHE_CASE, // This mode depends on file amount in hap. 167 CACHE_ALL 168 }; 169 170 // zip file extract class for bundle format. 171 class ZipFile { 172 public: 173 explicit ZipFile(const std::string &pathName); 174 ~ZipFile(); 175 /** 176 * @brief Open zip file. 177 * @return Returns true if the zip file is successfully opened; returns false otherwise. 178 */ 179 bool Open(); 180 /** 181 * @brief Close zip file. 182 */ 183 void Close(); 184 /** 185 * @brief Get all entries in the zip file. 186 * @param start Indicates the zip content location start position. 187 * @param length Indicates the zip content length. 188 * @return Returns the ZipEntryMap object cotain all entries. 189 */ 190 const ZipEntryMap &GetAllEntries() const; 191 /** 192 * @brief Has entry by name. 193 * @param entryName Indicates the entry name. 194 * @return Returns true if the ZipEntry is successfully finded; returns false otherwise. 195 */ 196 bool HasEntry(const std::string &entryName) const; 197 198 bool IsDirExist(const std::string &dir); 199 void GetAllFileList(const std::string &srcPath, std::vector<std::string> &assetList); 200 void GetChildNames(const std::string &srcPath, std::set<std::string> &fileSet); 201 202 /** 203 * @brief Get entry by name. 204 * @param entryName Indicates the entry name. 205 * @param resultEntry Indicates the obtained ZipEntry object. 206 * @return Returns true if the ZipEntry is successfully finded; returns false otherwise. 207 */ 208 bool GetEntry(const std::string &entryName, ZipEntry &resultEntry) const; 209 bool GetDataOffsetRelative(const ZipEntry &zipEntry, ZipPos &offset, uint32_t &length) const; 210 bool ExtractFileFromMMap(const std::string &file, void *mmapDataPtr, 211 std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const; 212 213 std::unique_ptr<FileMapper> CreateFileMapper(const std::string &fileName, FileMapperType type) const; 214 bool ExtractToBufByName(const std::string &fileName, std::unique_ptr<uint8_t[]> &dataPtr, 215 size_t &len) const; 216 void SetCacheMode(CacheMode cacheMode); 217 bool UseDirCache() const; 218 private: 219 /** 220 * @brief Check the EndDir object. 221 * @param endDir Indicates the EndDir object to check. 222 * @return Returns true if successfully checked; returns false otherwise. 223 */ 224 bool CheckEndDir(const EndDir &endDir) const; 225 /** 226 * @brief Parse the EndDir. 227 * @return Returns true if successfully Parsed; returns false otherwise. 228 */ 229 bool ParseEndDirectory(); 230 /** 231 * @brief Parse one entry. 232 * @return Returns true if successfully parsed; returns false otherwise. 233 */ 234 bool ParseOneEntry(uint8_t* &entryPtr); 235 /** 236 * @brief Parse all Entries. 237 * @return Returns true if successfully parsed; returns false otherwise. 238 */ 239 bool ParseAllEntries(); 240 /** 241 * @brief Get LocalHeader object size. 242 * @param nameSize Indicates the nameSize. 243 * @param extraSize Indicates the extraSize. 244 * @return Returns size of LocalHeader. 245 */ 246 size_t GetLocalHeaderSize(const uint16_t nameSize = 0, const uint16_t extraSize = 0) const; 247 /** 248 * @brief Get entry data offset. 249 * @param zipEntry Indicates the ZipEntry object. 250 * @param extraSize Indicates the extraSize. 251 * @return Returns position. 252 */ 253 ZipPos GetEntryDataOffset(const ZipEntry &zipEntry, const uint16_t extraSize) const; 254 /** 255 * @brief Check data description. 256 * @param zipEntry Indicates the ZipEntry object. 257 * @param localHeader Indicates the localHeader object. 258 * @return Returns true if successfully checked; returns false otherwise. 259 */ 260 bool CheckDataDesc(const ZipEntry &zipEntry, const LocalHeader &localHeader) const; 261 /** 262 * @brief Check coherency LocalHeader object. 263 * @param zipEntry Indicates the ZipEntry object. 264 * @param extraSize Indicates the obtained size. 265 * @return Returns true if successfully checked; returns false otherwise. 266 */ 267 bool CheckCoherencyLocalHeader(const ZipEntry &zipEntry, uint16_t &extraSize) const; 268 /** 269 * @brief Get Entry start. 270 * @param zipEntry Indicates the ZipEntry object. 271 * @param extraSize Indicates the extra size. 272 * @return Returns true if successfully Seeked; returns false otherwise. 273 */ 274 size_t GetEntryStart(const ZipEntry &zipEntry, const uint16_t extraSize) const; 275 /** 276 * @brief Init zlib stream. 277 * @param zstream Indicates the obtained z_stream object. 278 * @return Returns true if successfully init; returns false otherwise. 279 */ 280 bool InitZStream(z_stream &zstream) const; 281 bool UnzipWithInflatedFromMMap(const ZipEntry &zipEntry, const uint16_t extraSize, 282 void *mmapDataPtr, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const; 283 bool CopyInflateOut(z_stream &zstream, size_t inflateLen, uint8_t** dstDataPtr, 284 BytePtr bufOut, uint8_t &errorTimes) const; 285 bool ReadZStreamFromMMap(const BytePtr &buffer, void* &dataPtr, 286 z_stream &zstream, uint32_t &remainCompressedSize) const; 287 288 std::shared_ptr<DirTreeNode> GetDirRoot(); 289 std::shared_ptr<DirTreeNode> MakeDirTree() const; 290 291 bool IsDirExistCache(const std::string &dir); 292 void GetAllFileListCache(const std::string &srcPath, std::vector<std::string> &assetList); 293 void GetChildNamesCache(const std::string &srcPath, std::set<std::string> &fileSet); 294 295 bool IsDirExistNormal(const std::string &dir); 296 void GetAllFileListNormal(const std::string &srcPath, std::vector<std::string> &assetList); 297 void GetChildNamesNormal(const std::string &srcPath, std::set<std::string> &fileSet); 298 299 private: 300 std::string pathName_; 301 std::shared_ptr<ZipFileReader> zipFileReader_; 302 EndDir endDir_; 303 ZipEntryMap entriesMap_; 304 std::mutex dirRootMutex_; 305 std::shared_ptr<DirTreeNode> dirRoot_; 306 // offset of central directory relative to zip file. 307 ZipPos centralDirPos_ = 0; 308 // this zip content start offset relative to zip file. 309 ZipPos fileStartPos_ = 0; 310 // this zip content length in the zip file. 311 ZipPos fileLength_ = 0; 312 std::atomic_bool isOpen_ = false; 313 std::mutex openMutex_; 314 CacheMode cacheMode_ = CacheMode::CACHE_CASE; 315 }; 316 } // namespace AbilityBase 317 } // namespace OHOS 318 #endif // OHOS_ABILITY_BASE_ZIP_FILE_H 319