1 /*
2  * Copyright (c) 2022-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 #include "zip_file.h"
17 
18 #include <ostream>
19 
20 #include "ability_base_log_wrapper.h"
21 #include "constants.h"
22 #include "file_mapper.h"
23 #include "file_path_utils.h"
24 #include "hitrace_meter.h"
25 #include "securec.h"
26 #include "zip_file_reader.h"
27 #include "zlib.h"
28 
29 namespace OHOS {
30 namespace AbilityBase {
31 namespace {
32 constexpr uint32_t MAX_FILE_NAME = 4096;
33 constexpr uint32_t UNZIP_BUFFER_SIZE = 1024;
34 constexpr uint32_t UNZIP_BUF_IN_LEN = 160 * UNZIP_BUFFER_SIZE;   // in  buffer length: 160KB
35 constexpr uint32_t UNZIP_BUF_OUT_LEN = 320 * UNZIP_BUFFER_SIZE;  // out buffer length: 320KB
36 constexpr uint32_t LOCAL_HEADER_SIGNATURE = 0x04034b50;
37 constexpr uint32_t CENTRAL_SIGNATURE = 0x02014b50;
38 constexpr uint32_t EOCD_SIGNATURE = 0x06054b50;
39 constexpr uint32_t DATA_DESC_SIGNATURE = 0x08074b50;
40 constexpr uint32_t FLAG_DATA_DESC = 0x8;
41 constexpr uint8_t INFLATE_ERROR_TIMES = 5;
42 constexpr uint8_t MAP_FILE_SUFFIX = 4;
43 constexpr char FILE_SEPARATOR_CHAR = '/';
44 constexpr const char* WRONG_FILE_SEPARATOR = "//";
45 constexpr uint32_t CACHE_CASE_THRESHOLD = 10000;
46 
GetTreeFileList(std::shared_ptr<DirTreeNode> root,const std::string & rootPath,std::vector<std::string> & assetList)47 void GetTreeFileList(std::shared_ptr<DirTreeNode> root, const std::string &rootPath,
48     std::vector<std::string> &assetList)
49 {
50     if (root == nullptr) {
51         return;
52     }
53     if (!root->isDir && !rootPath.empty()) {
54         assetList.push_back(rootPath);
55     } else {
56         std::string prefix = rootPath;
57         if (!prefix.empty()) {
58             prefix.push_back(FILE_SEPARATOR_CHAR);
59         }
60         for (const auto &child : root->children) {
61             GetTreeFileList(child.second, prefix + child.first, assetList);
62         }
63     }
64 }
65 
AddEntryToTree(const std::string & fileName,std::shared_ptr<DirTreeNode> root)66 void AddEntryToTree(const std::string &fileName, std::shared_ptr<DirTreeNode> root)
67 {
68     if (root == nullptr) {
69         return;
70     }
71     size_t cur = 0;
72     auto parent = root;
73     do {
74         while (cur < fileName.size() && fileName[cur] == FILE_SEPARATOR_CHAR) {
75             cur++;
76         }
77         if (cur >= fileName.size()) {
78             break;
79         }
80         auto next = fileName.find(FILE_SEPARATOR_CHAR, cur);
81         auto nodeName = fileName.substr(cur, next - cur);
82         auto it = parent->children.find(nodeName);
83         if (it != parent->children.end()) {
84             parent = it->second;
85         } else {
86             auto node = std::make_shared<DirTreeNode>();
87             node->isDir = next != std::string::npos;
88             parent->children.emplace(nodeName, node);
89             parent = node;
90         }
91         cur = next;
92     } while (cur != std::string::npos);
93 }
94 
IsRootDir(const std::string & dirName)95 inline bool IsRootDir(const std::string &dirName)
96 {
97     return dirName.size() == 1 && dirName.back() == FILE_SEPARATOR_CHAR;
98 }
99 }  // namespace
100 
ZipEntry(const CentralDirEntry & centralEntry)101 ZipEntry::ZipEntry(const CentralDirEntry &centralEntry)
102 {
103     compressionMethod = centralEntry.compressionMethod;
104     uncompressedSize = centralEntry.uncompressedSize;
105     compressedSize = centralEntry.compressedSize;
106     localHeaderOffset = centralEntry.localHeaderOffset;
107     crc = centralEntry.crc;
108     flags = centralEntry.flags;
109     modifiedTime = centralEntry.modifiedTime;
110     modifiedDate = centralEntry.modifiedDate;
111 }
112 
ZipFile(const std::string & pathName)113 ZipFile::ZipFile(const std::string &pathName) : pathName_(pathName) {}
114 
~ZipFile()115 ZipFile::~ZipFile()
116 {
117     Close();
118 }
119 
SetContentLocation(const ZipPos start,const size_t length)120 void ZipFile::SetContentLocation(const ZipPos start, const size_t length)
121 {
122     if (isOpen_) {
123         ABILITYBASE_LOGE("opened");
124         return;
125     }
126     fileStartPos_ = start;
127     fileLength_ = length;
128 }
129 
CheckEndDir(const EndDir & endDir) const130 bool ZipFile::CheckEndDir(const EndDir &endDir) const
131 {
132     size_t lenEndDir = sizeof(EndDir);
133     if ((endDir.numDisk != 0) || (endDir.signature != EOCD_SIGNATURE) || (endDir.startDiskOfCentralDir != 0) ||
134         (endDir.offset >= fileLength_) || (endDir.totalEntriesInThisDisk != endDir.totalEntries) ||
135         (endDir.commentLen != 0) ||
136         // central dir can't overlap end of central dir
137         ((endDir.offset + endDir.sizeOfCentralDir + lenEndDir) > fileLength_)) {
138         ABILITYBASE_LOGW("failed:fileLen: %{public}llu, signature: %{public}u, numDisk: %{public}hu, "
139             "startDiskOfCentralDir: %{public}hu, totalEntriesInThisDisk: %{public}hu, totalEntries: %{public}hu, "
140             "sizeOfCentralDir: %{public}u, offset: %{public}u, commentLen: %{public}hu",
141             fileLength_, endDir.signature, endDir.numDisk, endDir.startDiskOfCentralDir, endDir.totalEntriesInThisDisk,
142             endDir.totalEntries, endDir.sizeOfCentralDir, endDir.offset, endDir.commentLen);
143         return false;
144     }
145     return true;
146 }
147 
ParseEndDirectory()148 bool ZipFile::ParseEndDirectory()
149 {
150     size_t endDirLen = sizeof(EndDir);
151     size_t endFilePos = fileStartPos_ + fileLength_;
152 
153     if (fileLength_ <= endDirLen) {
154         ABILITYBASE_LOGE("fileStartPos_:(%{public}llu) <= fileLength_:(%{public}llu)",
155             fileStartPos_, fileLength_);
156         return false;
157     }
158 
159     size_t eocdPos = endFilePos - endDirLen;
160     if (!zipFileReader_->ReadBuffer(reinterpret_cast<uint8_t*>(&endDir_), eocdPos, sizeof(EndDir))) {
161         ABILITYBASE_LOGE("read EOCD failed");
162         return false;
163     }
164 
165     centralDirPos_ = endDir_.offset + fileStartPos_;
166 
167     return CheckEndDir(endDir_);
168 }
169 
ParseOneEntry(uint8_t * & entryPtr)170 bool ZipFile::ParseOneEntry(uint8_t* &entryPtr)
171 {
172     if (entryPtr == nullptr) {
173         ABILITYBASE_LOGE("null entryPtr");
174         return false;
175     }
176 
177     CentralDirEntry directoryEntry;
178     if (memcpy_s(&directoryEntry, sizeof(CentralDirEntry), entryPtr, sizeof(CentralDirEntry)) != EOK) {
179         ABILITYBASE_LOGE("Mem copy directory entry failed");
180         return false;
181     }
182 
183     if (directoryEntry.signature != CENTRAL_SIGNATURE) {
184         ABILITYBASE_LOGE("check signature failed");
185         return false;
186     }
187 
188     entryPtr += sizeof(CentralDirEntry);
189     size_t fileLength = (directoryEntry.nameSize >= MAX_FILE_NAME) ? (MAX_FILE_NAME - 1) : directoryEntry.nameSize;
190     std::string fileName(fileLength, 0);
191     if (memcpy_s(&(fileName[0]), fileLength, entryPtr, fileLength) != EOK) {
192         ABILITYBASE_LOGE("Mem copy file name failed");
193         return false;
194     }
195 
196     ZipEntry currentEntry(directoryEntry);
197     currentEntry.fileName = fileName;
198     entriesMap_[fileName] = currentEntry;
199     entryPtr += directoryEntry.nameSize + directoryEntry.extraSize + directoryEntry.commentSize;
200     return true;
201 }
202 
MakeDirTree() const203 std::shared_ptr<DirTreeNode> ZipFile::MakeDirTree() const
204 {
205     ABILITYBASE_LOGI("called");
206     auto root = std::make_shared<DirTreeNode>();
207     root->isDir = true;
208     for (const auto &[fileName, entry]: entriesMap_) {
209         AddEntryToTree(fileName, root);
210     }
211     return root;
212 }
213 
GetDirRoot()214 std::shared_ptr<DirTreeNode> ZipFile::GetDirRoot()
215 {
216     if (!isOpen_) {
217         return nullptr;
218     }
219     std::lock_guard guard(dirRootMutex_);
220     if (dirRoot_ == nullptr) {
221         HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "make_dir_tree");
222         dirRoot_ = MakeDirTree();
223     }
224     return dirRoot_;
225 }
226 
ParseAllEntries()227 bool ZipFile::ParseAllEntries()
228 {
229     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
230     auto centralData = zipFileReader_->ReadBuffer(static_cast<size_t>(centralDirPos_),
231         static_cast<size_t>(endDir_.sizeOfCentralDir));
232     if (centralData.empty()) {
233         ABILITYBASE_LOGE("centralData empty for [%{public}s] failed", pathName_.c_str());
234         return false;
235     }
236 
237     bool ret = true;
238     uint8_t *entryPtr = reinterpret_cast<uint8_t *>(centralData.data());
239     for (uint16_t i = 0; i < endDir_.totalEntries; i++) {
240         if (!ParseOneEntry(entryPtr)) {
241             ABILITYBASE_LOGE("Parse entry[%{public}d] failed", i);
242             ret = false;
243             break;
244         }
245     }
246 
247     return ret;
248 }
249 
Open()250 bool ZipFile::Open()
251 {
252     if (isOpen_) {
253         ABILITYBASE_LOGE("opened");
254         return true;
255     }
256 
257     if (pathName_.length() > PATH_MAX) {
258         ABILITYBASE_LOGE("failed:path length:%{public}u",
259             static_cast<unsigned int>(pathName_.length()));
260         return false;
261     }
262     std::string realPath;
263     realPath.reserve(PATH_MAX);
264     realPath.resize(PATH_MAX - 1);
265     if (pathName_.substr(0, Constants::GetProcPrefix().size()) == Constants::GetProcPrefix()) {
266         realPath = pathName_;
267     } else {
268         HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "realpath_file");
269         if (realpath(pathName_.c_str(), &(realPath[0])) == nullptr) {
270             ABILITYBASE_LOGE("realpath error: %{public}d, pathName: %{public}s", errno, pathName_.c_str());
271             return false;
272         }
273     }
274 
275     zipFileReader_ = ZipFileReader::CreateZipFileReader(realPath);
276     if (!zipFileReader_) {
277         ABILITYBASE_LOGE("open file(%{public}s) failed", pathName_.c_str());
278         return false;
279     }
280 
281     if (fileLength_ == 0) {
282         auto fileLength = zipFileReader_->GetFileLen();
283         fileLength_ = static_cast<ZipPos>(fileLength);
284         if (fileStartPos_ >= fileLength_) {
285             ABILITYBASE_LOGE("pos > length");
286             zipFileReader_.reset();
287             return false;
288         }
289 
290         fileLength_ -= fileStartPos_;
291     }
292 
293     bool result = ParseEndDirectory();
294     if (result) {
295         result = ParseAllEntries();
296     }
297     // it means open file success.
298     isOpen_ = true;
299     return result;
300 }
301 
Close()302 void ZipFile::Close()
303 {
304     if (!isOpen_ || zipFileReader_ == nullptr) {
305         ABILITYBASE_LOGD("not opened");
306         return;
307     }
308 
309     isOpen_ = false;
310     entriesMap_.clear();
311     {
312         std::lock_guard guard(dirRootMutex_);
313         dirRoot_.reset();
314     }
315     pathName_ = "";
316 
317     zipFileReader_.reset();
318 }
319 
320 // Get all file zipEntry in this file
GetAllEntries() const321 const ZipEntryMap &ZipFile::GetAllEntries() const
322 {
323     return entriesMap_;
324 }
325 
HasEntry(const std::string & entryName) const326 bool ZipFile::HasEntry(const std::string &entryName) const
327 {
328     return entriesMap_.find(entryName) != entriesMap_.end();
329 }
330 
SetCacheMode(CacheMode cacheMode)331 void ZipFile::SetCacheMode(CacheMode cacheMode)
332 {
333     std::lock_guard lock(dirRootMutex_);
334     cacheMode_ = cacheMode;
335     if (!UseDirCache()) {
336         dirRoot_.reset();
337     }
338 }
339 
UseDirCache() const340 bool ZipFile::UseDirCache() const
341 {
342     auto mode = cacheMode_;
343     bool useCache = mode == CacheMode::CACHE_ALL;
344     if (mode == CacheMode::CACHE_CASE && entriesMap_.size() >= CACHE_CASE_THRESHOLD) {
345         useCache = true;
346     }
347     return useCache;
348 }
349 
IsDirExist(const std::string & dir)350 bool ZipFile::IsDirExist(const std::string &dir)
351 {
352     if (dir.empty()) {
353         ABILITYBASE_LOGE("dir empty");
354         return false;
355     }
356     if (IsRootDir(dir)) {
357         return true;
358     }
359     if (dir.find(WRONG_FILE_SEPARATOR) != std::string::npos) {
360         ABILITYBASE_LOGD("Wrong format");
361         return false;
362     }
363 
364     auto tmpDir = dir;
365     if (tmpDir.front() == FILE_SEPARATOR_CHAR) {
366         tmpDir.erase(tmpDir.begin());
367     }
368     if (tmpDir.back() != FILE_SEPARATOR_CHAR) {
369         tmpDir.push_back(FILE_SEPARATOR_CHAR);
370     }
371     if (entriesMap_.count(tmpDir) > 0) {
372         return true;
373     }
374     tmpDir.pop_back();
375     if (entriesMap_.count(tmpDir) > 0) {
376         ABILITYBASE_LOGW("file not dir");
377         return false;
378     }
379 
380     if (UseDirCache()) {
381         return IsDirExistCache(tmpDir);
382     }
383     return IsDirExistNormal(tmpDir);
384 }
385 
GetAllFileList(const std::string & srcPath,std::vector<std::string> & assetList)386 void ZipFile::GetAllFileList(const std::string &srcPath, std::vector<std::string> &assetList)
387 {
388     if (srcPath.empty()) {
389         ABILITYBASE_LOGW("empty dir");
390         return;
391     }
392     if (IsRootDir(srcPath)) {
393         for (const auto &[fileName, fileInfo] : entriesMap_) {
394             if (!fileName.empty() && fileName.back() != FILE_SEPARATOR_CHAR) {
395                 assetList.push_back(fileName);
396             }
397         }
398         return;
399     }
400     if (srcPath.find(WRONG_FILE_SEPARATOR) != std::string::npos) {
401         ABILITYBASE_LOGW("Wrong format");
402         return;
403     }
404 
405     auto tmpDir = srcPath;
406     if (tmpDir.front() == FILE_SEPARATOR_CHAR) {
407         tmpDir.erase(tmpDir.begin());
408     }
409     if (tmpDir.back() != FILE_SEPARATOR_CHAR) {
410         tmpDir.push_back(FILE_SEPARATOR_CHAR);
411     }
412     if (entriesMap_.count(tmpDir) > 0) {
413         return;
414     }
415     tmpDir.pop_back();
416     if (entriesMap_.count(tmpDir) > 0) {
417         ABILITYBASE_LOGW("file not dir");
418         return;
419     }
420 
421     if (UseDirCache()) {
422         GetAllFileListCache(tmpDir, assetList);
423     } else {
424         GetAllFileListNormal(tmpDir, assetList);
425     }
426 }
427 
GetChildNames(const std::string & srcPath,std::set<std::string> & fileSet)428 void ZipFile::GetChildNames(const std::string &srcPath, std::set<std::string> &fileSet)
429 {
430     if (srcPath.empty()) {
431         ABILITYBASE_LOGE("empty dir");
432         return;
433     }
434     if (srcPath.find(WRONG_FILE_SEPARATOR) != std::string::npos) {
435         ABILITYBASE_LOGW("Wrong input format");
436         return;
437     }
438     auto tmpDir = srcPath;
439     if (!IsRootDir(tmpDir)) {
440         if (tmpDir.front() == FILE_SEPARATOR_CHAR) {
441             tmpDir.erase(tmpDir.begin());
442         }
443         if (tmpDir.back() != FILE_SEPARATOR_CHAR) {
444             tmpDir.push_back(FILE_SEPARATOR_CHAR);
445         }
446         if (entriesMap_.count(tmpDir) > 0) {
447             return;
448         }
449         tmpDir.pop_back();
450         if (entriesMap_.count(tmpDir) > 0) {
451             ABILITYBASE_LOGW("file not dir");
452             return;
453         }
454     }
455 
456     if (UseDirCache()) {
457         GetChildNamesCache(tmpDir, fileSet);
458     } else {
459         GetChildNamesNormal(tmpDir, fileSet);
460     }
461 }
462 
IsDirExistCache(const std::string & dir)463 bool ZipFile::IsDirExistCache(const std::string &dir)
464 {
465     auto parent = GetDirRoot();
466     if (parent == nullptr) {
467         ABILITYBASE_LOGE("null parent");
468         return false;
469     }
470     size_t cur = 0;
471     do {
472         while (cur < dir.size() && dir[cur] == FILE_SEPARATOR_CHAR) {
473             cur++;
474         }
475         if (cur >= dir.size()) {
476             break;
477         }
478         auto next = dir.find(FILE_SEPARATOR_CHAR, cur);
479         auto nodeName = dir.substr(cur, next - cur);
480         auto it = parent->children.find(nodeName);
481         if (it == parent->children.end()) {
482             ABILITYBASE_LOGD("dir not found, dir : %{public}s", dir.c_str());
483             return false;
484         }
485         parent = it->second;
486         cur = next;
487     } while (cur != std::string::npos);
488 
489     return true;
490 }
491 
GetAllFileListCache(const std::string & srcPath,std::vector<std::string> & assetList)492 void ZipFile::GetAllFileListCache(const std::string &srcPath, std::vector<std::string> &assetList)
493 {
494     auto parent = GetDirRoot();
495     if (parent == nullptr) {
496         ABILITYBASE_LOGE("null parent");
497         return;
498     }
499 
500     auto rootName = srcPath.back() == FILE_SEPARATOR_CHAR ?
501         srcPath.substr(0, srcPath.length() - 1) : srcPath;
502 
503     size_t cur = 0;
504     do {
505         while (cur < rootName.size() && rootName[cur] == FILE_SEPARATOR_CHAR) {
506             cur++;
507         }
508         if (cur >= rootName.size()) {
509             break;
510         }
511         auto next = rootName.find(FILE_SEPARATOR_CHAR, cur);
512         auto nodeName = rootName.substr(cur, next - cur);
513         auto it = parent->children.find(nodeName);
514         if (it == parent->children.end()) {
515             ABILITYBASE_LOGE("srcPath not found: %{public}s", rootName.c_str());
516             return;
517         }
518         parent = it->second;
519         cur = next;
520     } while (cur != std::string::npos);
521 
522     GetTreeFileList(parent, rootName, assetList);
523 }
524 
GetChildNamesCache(const std::string & srcPath,std::set<std::string> & fileSet)525 void ZipFile::GetChildNamesCache(const std::string &srcPath, std::set<std::string> &fileSet)
526 {
527     size_t cur = 0;
528     auto parent = GetDirRoot();
529     if (parent == nullptr) {
530         ABILITYBASE_LOGE("null parent");
531         return;
532     }
533     do {
534         while (cur < srcPath.size() && srcPath[cur] == FILE_SEPARATOR_CHAR) {
535             cur++;
536         }
537         if (cur >= srcPath.size()) {
538             break;
539         }
540         auto next = srcPath.find(FILE_SEPARATOR_CHAR, cur);
541         auto nodeName = srcPath.substr(cur, next - cur);
542         auto it = parent->children.find(nodeName);
543         if (it == parent->children.end()) {
544             ABILITYBASE_LOGI("srcPath not found: %{public}s", srcPath.c_str());
545             return;
546         }
547         parent = it->second;
548         cur = next;
549     } while (cur != std::string::npos);
550 
551     for (const auto &child : parent->children) {
552         fileSet.insert(child.first);
553     }
554 }
555 
IsDirExistNormal(const std::string & dir)556 bool ZipFile::IsDirExistNormal(const std::string &dir)
557 {
558     auto targetDir = dir;
559     if (targetDir.back() != FILE_SEPARATOR_CHAR) {
560         targetDir.push_back(FILE_SEPARATOR_CHAR);
561     }
562     for (const auto &[fileName, fileInfo] : entriesMap_) {
563         if (fileName.size() > targetDir.size() && fileName.substr(0, targetDir.size()) == targetDir) {
564             return true;
565         }
566     }
567     return false;
568 }
569 
GetAllFileListNormal(const std::string & srcPath,std::vector<std::string> & assetList)570 void ZipFile::GetAllFileListNormal(const std::string &srcPath, std::vector<std::string> &assetList)
571 {
572     auto targetDir = srcPath;
573     if (targetDir.back() != FILE_SEPARATOR_CHAR) {
574         targetDir.push_back(FILE_SEPARATOR_CHAR);
575     }
576     for (const auto &[fileName, fileInfo] : entriesMap_) {
577         if (fileName.size() > targetDir.size() && fileName.back() != FILE_SEPARATOR_CHAR &&
578             fileName.substr(0, targetDir.size()) == targetDir) {
579             assetList.push_back(fileName);
580         }
581     }
582 }
583 
GetChildNamesNormal(const std::string & srcPath,std::set<std::string> & fileSet)584 void ZipFile::GetChildNamesNormal(const std::string &srcPath, std::set<std::string> &fileSet)
585 {
586     auto targetDir = srcPath;
587     if (targetDir.back() != FILE_SEPARATOR_CHAR) {
588         targetDir.push_back(FILE_SEPARATOR_CHAR);
589     }
590     if (IsRootDir(srcPath)) {
591         for (const auto &[fileName, fileInfo] : entriesMap_) {
592             auto nextPos = fileName.find(FILE_SEPARATOR_CHAR);
593             fileSet.insert(nextPos == std::string::npos ? fileName : fileName.substr(0, nextPos));
594         }
595         return;
596     }
597     for (const auto &[fileName, fileInfo] : entriesMap_) {
598         if (fileName.size() > targetDir.size() && fileName.substr(0, targetDir.size()) == targetDir) {
599             fileSet.insert(fileName.substr(targetDir.size(),
600                 fileName.find(FILE_SEPARATOR_CHAR, targetDir.size()) - targetDir.size()));
601         }
602     }
603 }
604 
GetEntry(const std::string & entryName,ZipEntry & resultEntry) const605 bool ZipFile::GetEntry(const std::string &entryName, ZipEntry &resultEntry) const
606 {
607     auto iter = entriesMap_.find(entryName);
608     if (iter != entriesMap_.end()) {
609         resultEntry = iter->second;
610         return true;
611     }
612     return false;
613 }
614 
GetLocalHeaderSize(const uint16_t nameSize,const uint16_t extraSize) const615 size_t ZipFile::GetLocalHeaderSize(const uint16_t nameSize, const uint16_t extraSize) const
616 {
617     return sizeof(LocalHeader) + nameSize + extraSize;
618 }
619 
CheckDataDesc(const ZipEntry & zipEntry,const LocalHeader & localHeader) const620 bool ZipFile::CheckDataDesc(const ZipEntry &zipEntry, const LocalHeader &localHeader) const
621 {
622     uint32_t crcLocal = 0;
623     uint32_t compressedLocal = 0;
624     uint32_t uncompressedLocal = 0;
625 
626     if (localHeader.flags & FLAG_DATA_DESC) {  // use data desc
627         DataDesc dataDesc;
628         auto descPos = zipEntry.localHeaderOffset + GetLocalHeaderSize(localHeader.nameSize, localHeader.extraSize);
629         descPos += fileStartPos_ + zipEntry.compressedSize;
630 
631         if (!zipFileReader_->ReadBuffer(reinterpret_cast<uint8_t*>(&dataDesc), descPos, sizeof(DataDesc))) {
632             ABILITYBASE_LOGE("ReadBuffer failed");
633             return false;
634         }
635 
636         if (dataDesc.signature != DATA_DESC_SIGNATURE) {
637             ABILITYBASE_LOGE("check signature failed");
638             return false;
639         }
640 
641         crcLocal = dataDesc.crc;
642         compressedLocal = dataDesc.compressedSize;
643         uncompressedLocal = dataDesc.uncompressedSize;
644     } else {
645         crcLocal = localHeader.crc;
646         compressedLocal = localHeader.compressedSize;
647         uncompressedLocal = localHeader.uncompressedSize;
648     }
649 
650     if ((zipEntry.crc != crcLocal) || (zipEntry.compressedSize != compressedLocal) ||
651         (zipEntry.uncompressedSize != uncompressedLocal)) {
652         ABILITYBASE_LOGE("size corrupted");
653         return false;
654     }
655 
656     return true;
657 }
658 
CheckCoherencyLocalHeader(const ZipEntry & zipEntry,uint16_t & extraSize) const659 bool ZipFile::CheckCoherencyLocalHeader(const ZipEntry &zipEntry, uint16_t &extraSize) const
660 {
661     // current only support store and Z_DEFLATED method
662     if ((zipEntry.compressionMethod != Z_DEFLATED) && (zipEntry.compressionMethod != 0)) {
663         ABILITYBASE_LOGE("compressionMethod(%{public}d) not support", zipEntry.compressionMethod);
664         return false;
665     }
666 
667     auto nameSize = zipEntry.fileName.length();
668     auto startPos = fileStartPos_ + zipEntry.localHeaderOffset;
669     size_t buffSize = sizeof(LocalHeader) + nameSize;
670     auto buff = zipFileReader_->ReadBuffer(startPos, buffSize);
671     if (buff.size() < buffSize) {
672         ABILITYBASE_LOGE("read header failed");
673         return false;
674     }
675 
676     LocalHeader localHeader = {0};
677     if (memcpy_s(&localHeader, sizeof(LocalHeader), buff.data(), sizeof(LocalHeader)) != EOK) {
678         ABILITYBASE_LOGE("memcpy localheader failed");
679         return false;
680     }
681     if ((localHeader.signature != LOCAL_HEADER_SIGNATURE) ||
682         (zipEntry.compressionMethod != localHeader.compressionMethod)) {
683         ABILITYBASE_LOGE("signature or compressionMethod failed");
684         return false;
685     }
686 
687     if (localHeader.nameSize != nameSize && nameSize < MAX_FILE_NAME - 1) {
688         ABILITYBASE_LOGE("name corrupted");
689         return false;
690     }
691     std::string fileName = buff.substr(sizeof(LocalHeader));
692     if (zipEntry.fileName != fileName) {
693         ABILITYBASE_LOGE("name corrupted");
694         return false;
695     }
696 
697     if (!CheckDataDesc(zipEntry, localHeader)) {
698         ABILITYBASE_LOGE("check data desc failed");
699         return false;
700     }
701 
702     extraSize = localHeader.extraSize;
703     return true;
704 }
705 
GetEntryStart(const ZipEntry & zipEntry,const uint16_t extraSize) const706 size_t ZipFile::GetEntryStart(const ZipEntry &zipEntry, const uint16_t extraSize) const
707 {
708     ZipPos startOffset = zipEntry.localHeaderOffset;
709     // get data offset, add signature+localheader+namesize+extrasize
710     startOffset += GetLocalHeaderSize(zipEntry.fileName.length(), extraSize);
711     startOffset += fileStartPos_;  // add file start relative to file stream
712 
713     return startOffset;
714 }
715 
UnzipWithStore(const ZipEntry & zipEntry,const uint16_t extraSize,std::ostream & dest) const716 bool ZipFile::UnzipWithStore(const ZipEntry &zipEntry, const uint16_t extraSize, std::ostream &dest) const
717 {
718     auto startPos = GetEntryStart(zipEntry, extraSize);
719     uint32_t remainSize = zipEntry.compressedSize;
720     while (remainSize > 0) {
721         size_t readLen = (remainSize > UNZIP_BUF_OUT_LEN) ? UNZIP_BUF_OUT_LEN : remainSize;
722         std::string readBuffer = zipFileReader_->ReadBuffer(startPos, readLen);
723         if (readBuffer.empty()) {
724             ABILITYBASE_LOGE("unzip store read failed");
725             return false;
726         }
727         remainSize -= readLen;
728         startPos += readLen;
729         dest.write(readBuffer.data(), readBuffer.length());
730     }
731 
732     return true;
733 }
734 
InitZStream(z_stream & zstream) const735 bool ZipFile::InitZStream(z_stream &zstream) const
736 {
737     // init zlib stream
738     if (memset_s(&zstream, sizeof(z_stream), 0, sizeof(z_stream))) {
739         ABILITYBASE_LOGE("stream buffer init failed");
740         return false;
741     }
742     int32_t zlibErr = inflateInit2(&zstream, -MAX_WBITS);
743     if (zlibErr != Z_OK) {
744         ABILITYBASE_LOGE("init failed");
745         return false;
746     }
747 
748     BytePtr bufOut = new (std::nothrow) Byte[UNZIP_BUF_OUT_LEN];
749     if (bufOut == nullptr) {
750         ABILITYBASE_LOGE("null bufOut");
751         return false;
752     }
753 
754     BytePtr bufIn = new (std::nothrow) Byte[UNZIP_BUF_IN_LEN];
755     if (bufIn == nullptr) {
756         ABILITYBASE_LOGE("null bufIn");
757         delete[] bufOut;
758         return false;
759     }
760     zstream.next_out = bufOut;
761     zstream.next_in = bufIn;
762     zstream.avail_out = UNZIP_BUF_OUT_LEN;
763     return true;
764 }
765 
ReadZStream(const BytePtr & buffer,z_stream & zstream,uint32_t & remainCompressedSize,size_t & startPos) const766 bool ZipFile::ReadZStream(const BytePtr &buffer, z_stream &zstream, uint32_t &remainCompressedSize,
767     size_t &startPos) const
768 {
769     if (zstream.avail_in == 0) {
770         size_t remainBytes = (remainCompressedSize > UNZIP_BUF_IN_LEN) ? UNZIP_BUF_IN_LEN : remainCompressedSize;
771         if (!zipFileReader_->ReadBuffer(buffer, startPos, remainBytes)) {
772             ABILITYBASE_LOGE("read failed");
773             return false;
774         }
775 
776         remainCompressedSize -= remainBytes;
777         startPos += remainBytes;
778         zstream.avail_in = remainBytes;
779         zstream.next_in = buffer;
780     }
781     return true;
782 }
783 
UnzipWithInflated(const ZipEntry & zipEntry,const uint16_t extraSize,std::ostream & dest) const784 bool ZipFile::UnzipWithInflated(const ZipEntry &zipEntry, const uint16_t extraSize, std::ostream &dest) const
785 {
786     z_stream zstream;
787     if (!InitZStream(zstream)) {
788         return false;
789     }
790 
791     auto startPos = GetEntryStart(zipEntry, extraSize);
792 
793     BytePtr bufIn = zstream.next_in;
794     BytePtr bufOut = zstream.next_out;
795 
796     bool ret = true;
797     int32_t zlibErr = Z_OK;
798     uint32_t remainCompressedSize = zipEntry.compressedSize;
799     size_t inflateLen = 0;
800     uint8_t errorTimes = 0;
801     while ((remainCompressedSize > 0) || (zstream.avail_in > 0)) {
802         if (!ReadZStream(bufIn, zstream, remainCompressedSize, startPos)) {
803             ret = false;
804             break;
805         }
806 
807         zlibErr = inflate(&zstream, Z_SYNC_FLUSH);
808         if ((zlibErr >= Z_OK) && (zstream.msg != nullptr)) {
809             ABILITYBASE_LOGE("unzip failed: %{public}d, msg: %{public}s", zlibErr, zstream.msg);
810             ret = false;
811             break;
812         }
813 
814         inflateLen = UNZIP_BUF_OUT_LEN - zstream.avail_out;
815         if (inflateLen > 0) {
816             dest.write((const char *)bufOut, inflateLen);
817             zstream.next_out = bufOut;
818             zstream.avail_out = UNZIP_BUF_OUT_LEN;
819             errorTimes = 0;
820         } else {
821             errorTimes++;
822         }
823         if (errorTimes >= INFLATE_ERROR_TIMES) {
824             ABILITYBASE_LOGE("data is wrong");
825             ret = false;
826             break;
827         }
828     }
829 
830     // free all dynamically allocated data structures except the next_in and next_out for this stream.
831     zlibErr = inflateEnd(&zstream);
832     if (zlibErr != Z_OK) {
833         ABILITYBASE_LOGE("inflateEnd error: %{public}d", zlibErr);
834         ret = false;
835     }
836 
837     delete[] bufOut;
838     delete[] bufIn;
839     return ret;
840 }
841 
GetEntryDataOffset(const ZipEntry & zipEntry,const uint16_t extraSize) const842 ZipPos ZipFile::GetEntryDataOffset(const ZipEntry &zipEntry, const uint16_t extraSize) const
843 {
844     // get entry data offset relative file
845     ZipPos offset = zipEntry.localHeaderOffset;
846 
847     offset += GetLocalHeaderSize(zipEntry.fileName.length(), extraSize);
848     offset += fileStartPos_;
849 
850     return offset;
851 }
852 
GetDataOffsetRelative(const std::string & file,ZipPos & offset,uint32_t & length) const853 bool ZipFile::GetDataOffsetRelative(const std::string &file, ZipPos &offset, uint32_t &length) const
854 {
855     ZipEntry zipEntry;
856     if (!GetEntry(file, zipEntry)) {
857         ABILITYBASE_LOGE("not find file");
858         return false;
859     }
860 
861     return GetDataOffsetRelative(zipEntry, offset, length);
862 }
863 
GetDataOffsetRelative(const ZipEntry & zipEntry,ZipPos & offset,uint32_t & length) const864 bool ZipFile::GetDataOffsetRelative(const ZipEntry &zipEntry, ZipPos &offset, uint32_t &length) const
865 {
866     uint16_t extraSize = 0;
867     if (!CheckCoherencyLocalHeader(zipEntry, extraSize)) {
868         ABILITYBASE_LOGE("check coherency local header failed");
869         return false;
870     }
871 
872     offset = GetEntryDataOffset(zipEntry, extraSize);
873     length = zipEntry.compressedSize;
874     return true;
875 }
876 
ExtractFile(const std::string & file,std::ostream & dest) const877 bool ZipFile::ExtractFile(const std::string &file, std::ostream &dest) const
878 {
879     ZipEntry zipEntry;
880     if (!GetEntry(file, zipEntry)) {
881         ABILITYBASE_LOGE("not find file");
882         return false;
883     }
884 
885     uint16_t extraSize = 0;
886     if (!CheckCoherencyLocalHeader(zipEntry, extraSize)) {
887         ABILITYBASE_LOGE("check coherency local header failed");
888         return false;
889     }
890 
891     bool ret = true;
892     if (zipEntry.compressionMethod == 0) {
893         ret = UnzipWithStore(zipEntry, extraSize, dest);
894     } else {
895         ret = UnzipWithInflated(zipEntry, extraSize, dest);
896     }
897 
898     return ret;
899 }
900 
ExtractFileFromMMap(const std::string & file,void * mmapDataPtr,std::unique_ptr<uint8_t[]> & dataPtr,size_t & len) const901 bool ZipFile::ExtractFileFromMMap(const std::string &file, void *mmapDataPtr,
902     std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const
903 {
904     ZipEntry zipEntry;
905     if (!GetEntry(file, zipEntry)) {
906         ABILITYBASE_LOGE("not find file");
907         return false;
908     }
909 
910     if (!zipEntry.compressionMethod) {
911         ABILITYBASE_LOGE("file[%{public}s] is not extracted", file.c_str());
912         return false;
913     }
914 
915     uint16_t extraSize = 0;
916     if (!CheckCoherencyLocalHeader(zipEntry, extraSize)) {
917         ABILITYBASE_LOGE("check coherency local header failed");
918         return false;
919     }
920 
921     bool ret = false;
922     ret = UnzipWithInflatedFromMMap(zipEntry, extraSize, mmapDataPtr, dataPtr, len);
923 
924     return ret;
925 }
926 
UnzipWithInflatedFromMMap(const ZipEntry & zipEntry,const uint16_t extraSize,void * mmapDataPtr,std::unique_ptr<uint8_t[]> & dataPtr,size_t & len) const927 bool ZipFile::UnzipWithInflatedFromMMap(const ZipEntry &zipEntry, const uint16_t extraSize,
928     void *mmapDataPtr, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const
929 {
930     z_stream zstream;
931     if (!InitZStream(zstream)) {
932         ABILITYBASE_LOGE("Init zstream failed");
933         return false;
934     }
935 
936     BytePtr bufIn = zstream.next_in;
937     BytePtr bufOut = zstream.next_out;
938 
939     bool ret = true;
940     int32_t zlibErr = Z_OK;
941     uint32_t remainCompressedSize = zipEntry.compressedSize;
942     size_t inflateLen = 0;
943     uint8_t errorTimes = 0;
944 
945     len = zipEntry.uncompressedSize;
946     dataPtr = std::make_unique<uint8_t[]>(len);
947     uint8_t *dstDataPtr = static_cast<uint8_t *>(dataPtr.get());
948     void *mmapSrcDataPtr = mmapDataPtr;
949 
950     while ((remainCompressedSize > 0) || (zstream.avail_in > 0)) {
951         if (!ReadZStreamFromMMap(bufIn, mmapSrcDataPtr, zstream, remainCompressedSize)) {
952             ret = false;
953             break;
954         }
955 
956         zlibErr = inflate(&zstream, Z_SYNC_FLUSH);
957         if ((zlibErr >= Z_OK) && (zstream.msg != nullptr)) {
958             ABILITYBASE_LOGE("unzip error: %{public}d, msg: %{public}s", zlibErr, zstream.msg);
959             ret = false;
960             break;
961         }
962 
963         inflateLen = UNZIP_BUF_OUT_LEN - zstream.avail_out;
964         if (!CopyInflateOut(zstream, inflateLen, &dstDataPtr, bufOut, errorTimes)) {
965             break;
966         }
967     }
968 
969     // free all dynamically allocated data structures except the next_in and next_out for this stream.
970     zlibErr = inflateEnd(&zstream);
971     if (zlibErr != Z_OK) {
972         ABILITYBASE_LOGE("inflateEnd error: %{public}d", zlibErr);
973         ret = false;
974     }
975 
976     delete[] bufOut;
977     delete[] bufIn;
978     return ret;
979 }
980 
CopyInflateOut(z_stream & zstream,size_t inflateLen,uint8_t ** dstDataPtr,BytePtr bufOut,uint8_t & errorTimes) const981 bool ZipFile::CopyInflateOut(z_stream &zstream, size_t inflateLen, uint8_t** dstDataPtr,
982     BytePtr bufOut, uint8_t &errorTimes) const
983 {
984     if (inflateLen > 0) {
985         if (memcpy_s(*dstDataPtr, inflateLen, bufOut, inflateLen) != EOK) {
986             ABILITYBASE_LOGE("Mem copy failed");
987             return false;
988         }
989 
990         *dstDataPtr += inflateLen;
991         zstream.next_out = bufOut;
992         zstream.avail_out = UNZIP_BUF_OUT_LEN;
993         errorTimes = 0;
994     } else {
995         errorTimes++;
996     }
997     if (errorTimes >= INFLATE_ERROR_TIMES) {
998         ABILITYBASE_LOGE("data is abnormal");
999         return false;
1000     }
1001 
1002     return true;
1003 }
1004 
ReadZStreamFromMMap(const BytePtr & buffer,void * & dataPtr,z_stream & zstream,uint32_t & remainCompressedSize) const1005 bool ZipFile::ReadZStreamFromMMap(const BytePtr &buffer, void* &dataPtr,
1006     z_stream &zstream, uint32_t &remainCompressedSize) const
1007 {
1008     if (!dataPtr) {
1009         ABILITYBASE_LOGE("null dataPtr");
1010         return false;
1011     }
1012 
1013     uint8_t *srcDataPtr = static_cast<uint8_t *>(dataPtr);
1014     if (zstream.avail_in == 0) {
1015         size_t remainBytes = (remainCompressedSize > UNZIP_BUF_IN_LEN) ? UNZIP_BUF_IN_LEN : remainCompressedSize;
1016         size_t readBytes = sizeof(Byte) * remainBytes;
1017         if (memcpy_s(buffer, readBytes, srcDataPtr, readBytes) != EOK) {
1018             ABILITYBASE_LOGE("Mem copy failed");
1019             return false;
1020         }
1021         srcDataPtr += readBytes;
1022         remainCompressedSize -= remainBytes;
1023         zstream.avail_in = remainBytes;
1024         zstream.next_in = buffer;
1025     }
1026     dataPtr = srcDataPtr;
1027     return true;
1028 }
1029 
CreateFileMapper(const std::string & fileName,FileMapperType type) const1030 std::unique_ptr<FileMapper> ZipFile::CreateFileMapper(const std::string &fileName, FileMapperType type) const
1031 {
1032     ZipEntry zipEntry;
1033     if (!GetEntry(fileName, zipEntry)) {
1034         ABILITYBASE_LOGE("GetEntry failed hapPath %{public}s", fileName.c_str());
1035         return nullptr;
1036     }
1037 
1038     ZipPos offset = 0;
1039     uint32_t length = 0;
1040     if (!GetDataOffsetRelative(zipEntry, offset, length)) {
1041         ABILITYBASE_LOGE("GetDataOffsetRelative failed hapPath %{public}s", fileName.c_str());
1042         return nullptr;
1043     }
1044     bool compress = zipEntry.compressionMethod > 0;
1045     if (type == FileMapperType::SAFE_ABC && compress) {
1046         ABILITYBASE_LOGW("Entry is compressed for safe: %{public}s", fileName.c_str());
1047     }
1048     std::unique_ptr<FileMapper> fileMapper = std::make_unique<FileMapper>();
1049     auto result = false;
1050     if (type == FileMapperType::NORMAL_MEM) {
1051         result = fileMapper->CreateFileMapper(zipFileReader_, fileName, offset, length, compress);
1052     } else {
1053         result = fileMapper->CreateFileMapper(fileName, compress, zipFileReader_->GetFd(), offset, length, type);
1054         if (result && type == FileMapperType::SAFE_ABC) {
1055             zipFileReader_->SetClosable(false);
1056         }
1057     }
1058 
1059     if (!result) {
1060         return nullptr;
1061     }
1062     return fileMapper;
1063 }
1064 
ExtractToBufByName(const std::string & fileName,std::unique_ptr<uint8_t[]> & dataPtr,size_t & len) const1065 bool ZipFile::ExtractToBufByName(const std::string &fileName, std::unique_ptr<uint8_t[]> &dataPtr,
1066     size_t &len) const
1067 {
1068     ZipEntry zipEntry;
1069     if (!GetEntry(fileName, zipEntry)) {
1070         if (fileName.length() > MAP_FILE_SUFFIX && fileName.substr(fileName.length() - MAP_FILE_SUFFIX) != ".map") {
1071             ABILITYBASE_LOGE("GetEntry failed hapPath %{public}s", fileName.c_str());
1072         }
1073         return false;
1074     }
1075     uint16_t extraSize = 0;
1076     if (!CheckCoherencyLocalHeader(zipEntry, extraSize)) {
1077         ABILITYBASE_LOGE("check coherency local header failed");
1078         return false;
1079     }
1080 
1081     ZipPos offset = GetEntryDataOffset(zipEntry, extraSize);
1082     uint32_t length = zipEntry.compressedSize;
1083     auto dataTmp = std::make_unique<uint8_t[]>(length);
1084     if (!zipFileReader_->ReadBuffer(dataTmp.get(), offset, length)) {
1085         ABILITYBASE_LOGE("read file failed, len[%{public}zu] fileName: %{public}s, offset: %{public}zu",
1086             len, fileName.c_str(), (size_t)offset);
1087         dataTmp.reset();
1088         return false;
1089     }
1090 
1091     if (zipEntry.compressionMethod > 0) {
1092         return UnzipWithInflatedFromMMap(zipEntry, extraSize, dataTmp.get(), dataPtr, len);
1093     }
1094 
1095     len = length;
1096     dataPtr = std::move(dataTmp);
1097 
1098     return true;
1099 }
1100 }  // namespace AbilityBase
1101 }  // namespace OHOS
1102