1/*
2 * Copyright (c) 2023 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
30namespace panda {
31namespace ecmascript {
32struct 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
40class Extractor {
41public:
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 Extract to dest path on filesystem.
60     * @param fileName Indicates the file name.
61     * @param targetPath Indicates the target Path.
62     * @return Returns true if the file extracted to filesystem successfully; returns false otherwise.
63     */
64    bool ExtractFile(const std::string &fileName, const std::string &targetPath) const;
65    /**
66     * @brief Get specified type names in a zip file.
67     * @param fileNames Indicates the obtained file names in zip.
68     * @param suffix Indicates the suffix of file.
69     */
70    bool HasEntry(const std::string &fileName) const;
71    bool IsDirExist(const std::string &dir) const;
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);
97    friend class ExtractorFriend;
98private:
99    ZipFile zipFile_;
100    bool initial_ = false;
101    std::string hapPath_;
102    std::optional<bool> isStageModel_;
103};
104
105class ExtractorUtil {
106public:
107    static std::string GetLoadFilePath(const std::string &hapPath);
108    static std::shared_ptr<Extractor> GetExtractor(const std::string &hapPath, bool &newCreate, bool cache = false);
109    static void DeleteExtractor(const std::string &hapPath);
110    friend class ExtractorUtilFriend;
111private:
112    static std::mutex mapMutex_;
113    static std::unordered_map<std::string, std::shared_ptr<Extractor>> extractorMap_;
114};
115}  // namespace AbilityBase
116}  // namespace OHOS
117#endif  // OHOS_ABILITY_BASE_EXTRACTOR_H
118