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