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_EXTRACTOR_H
17 #define OHOS_ABILITY_BASE_EXTRACTOR_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <optional>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "file_mapper.h"
28 #include "zip_file.h"
29 
30 namespace OHOS {
31 namespace AbilityBase {
32 struct FileInfo {
33     std::string fileName;
34     uint32_t offset = 0;
35     uint32_t length = 0;
36     uint16_t lastModTime = 0;
37     uint16_t lastModDate = 0;
38 };
39 
40 class Extractor {
41 public:
42     explicit Extractor(const std::string &source);
43     virtual ~Extractor();
44 
45     /**
46      * @brief Open compressed file.
47      * @return Returns true if the file is successfully opened; returns false otherwise.
48      */
49     virtual bool Init();
50 
51     /**
52      * @brief Extract to dest stream by file name.
53      * @param fileName Indicates the file name.
54      * @param dest Indicates the obtained std::ostream object.
55      * @return Returns true if the file extracted successfully; returns false otherwise.
56      */
57     bool ExtractByName(const std::string &fileName, std::ostream &dest) const;
58     /**
59      * @brief Get specified type names in a zip file.
60      * @param fileNames Indicates the obtained file names in zip.
61      * @param suffix Indicates the suffix of file.
62      */
63     void GetSpecifiedTypeFiles(std::vector<std::string> &fileNames, const std::string &suffix);
64     /**
65      * @brief Has entry by name.
66      * @param entryName Indicates the entry name.
67      * @return Returns true if the ZipEntry is successfully finded; returns false otherwise.
68      */
69     bool HasEntry(const std::string &fileName) const;
70     bool IsDirExist(const std::string &dir);
71     bool IsStageBasedModel(std::string abilityName);
72     bool GetFileBuffer(const std::string& srcPath, std::ostringstream& dest);
73     bool GetFileList(const std::string& srcPath, std::vector<std::string>& assetList);
74     bool GetFileList(const std::string &srcPath, std::set<std::string> &fileSet);
75     bool IsSameHap(const std::string& hapPath) const;
76 
77     std::unique_ptr<FileMapper> GetData(const std::string &fileName, bool safeRegion = false) const;
78     /**
79      * Do not use this method unless you exactly know what you are doing.
80      * For file item that user will handle errors, to mmap to safe region.
81      * User should make sure the extractor's release goes after the data's.
82      */
83     std::unique_ptr<FileMapper> GetMmapData(const std::string &fileName);
84 
85     bool UnzipData(std::unique_ptr<FileMapper> fileMapper, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const;
86     bool IsStageModel();
87 
88     bool GetFileInfo(const std::string &fileName, FileInfo &fileInfo) const;
89 
90     bool IsHapCompress(const std::string &fileName) const;
91 
92     bool ExtractToBufByName(const std::string &fileName, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len);
93     /**
94      * For abc file only, to mmap to safe region.
95      */
96     std::shared_ptr<FileMapper> GetSafeData(const std::string &fileName);
SetCacheMode(CacheMode cacheMode)97     void SetCacheMode(CacheMode cacheMode)
98     {
99         zipFile_.SetCacheMode(cacheMode);
100     }
101 private:
102     ZipFile zipFile_;
103     bool initial_ = false;
104     std::string hapPath_;
105     std::optional<bool> isStageModel_;
106 };
107 
108 class ExtractorUtil {
109 public:
110     static std::string GetLoadFilePath(const std::string &hapPath);
111     static std::shared_ptr<Extractor> GetExtractor(const std::string &hapPath, bool &newCreate, bool cache = false);
112     static void DeleteExtractor(const std::string &hapPath);
113 
114 private:
115     static std::mutex mapMutex_;
116     static std::unordered_map<std::string, std::shared_ptr<Extractor>> extractorMap_;
117 };
118 }  // namespace AbilityBase
119 }  // namespace OHOS
120 #endif  // OHOS_ABILITY_BASE_EXTRACTOR_H
121