1/*
2 * Copyright (c) 2021-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 RESOURCE_MANAGER_ZIPARCHIVE_H
17#define RESOURCE_MANAGER_ZIPARCHIVE_H
18
19#include <cstdint>
20#include <cstdio>
21#include <string>
22#include <set>
23#include <unzip.h>
24#include "res_desc.h"
25#include "res_config_impl.h"
26#include "resource_manager.h"
27
28#if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
29#include "file_mapper.h"
30#include "extractor.h"
31#endif
32
33namespace OHOS {
34namespace Global {
35namespace Resource {
36class HapParser {
37public:
38    /**
39     * Read specified file in zip to buffer
40     * @param uf
41     * @param fileName  file name in zip which we will read
42     * @param buffer    bytes will write to buffer
43     * @param bufLen    the file length in bytes
44     * @return
45     */
46    static int32_t ReadFileFromZip(unzFile &uf, const char *fileName, std::unique_ptr<uint8_t[]> &buffer,
47                                  size_t &bufLen);
48
49    /**
50     * Read resource.index in hap to buffer
51     * @param zipFile hap file path
52     * @param buffer  bytes will write to buffer
53     * @param bufLen  length in bytes
54     * @return
55     */
56    static int32_t ReadIndexFromFile(const char *zipFile, std::unique_ptr<uint8_t[]> &buffer, size_t &bufLen);
57
58    /**
59     * Whether the hap is STAGE MODE or not
60     * @param uf the hap fd
61     * @return true if the hap is STAGE MODE, else false
62     */
63    static bool IsStageMode(unzFile &uf);
64
65    /**
66     * Get the rawfile path
67     * @param filePath the hap path
68     * @param rawFilePath the rawFile path
69     * @return the rawFile path
70     */
71    static std::string GetPath(const std::string &filePath, std::string &rawFilePath);
72
73#if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
74    /**
75     * Parse modulename of FA Model
76     * @param extractor the ability extractor
77     * @return the modulename
78     */
79    static std::string ParseModuleName(std::shared_ptr<AbilityBase::Extractor> &extractor);
80
81    /**
82     * Get the raw file path
83     * @param extractor the ability extractor
84     * @param rawFilePath the rawFile path
85     * @return the rawFile path
86     */
87    static std::string GetRawFilePath(std::shared_ptr<AbilityBase::Extractor> &extractor,
88        const std::string &rawFileName);
89#endif
90
91    /**
92     * Get the raw file data from hap
93     * @param hapPath the hap path
94     * @param patchPath the hqf path
95     * @param rawFileName the rawFile path
96     * @param len the rawFile path
97     * @param outValue the rawFile path
98     * @return the rawFile path
99     */
100    static RState ReadRawFileFromHap(const std::string &hapPath, const std::string &patchPath,
101        const std::string &rawFileName, size_t &len, std::unique_ptr<uint8_t[]> &outValue);
102
103    /**
104     * Get the raw file descriptor
105     * @param hapPath the hap path
106     * @param patchPath the hqf path
107     * @param rawFileName the rawFile path
108     * @param descriptor the rawFile path
109     * @return the rawFile path
110     */
111    static RState ReadRawFileDescriptor(const char *hapPath, const char *patchPath,
112        const std::string &rawFileName, ResourceManager::RawFileDescriptor &descriptor);
113
114    /**
115     * Get the raw file list
116     * @param hapPath the hap path
117     * @param rawDirPath the rawfile directory path
118     * @param fileList the rawfile list write to
119     * @return SUCCESS if resource exist, else not found
120     */
121    static RState GetRawFileList(const std::string &hapPath, const std::string &rawDirPath,
122        std::set<std::string>& fileList);
123
124    /**
125     * Get the raw file list in UnCompressed
126     * @param indexPath the hap path
127     * @param rawDirPath the rawfile directory path
128     * @param fileList the rawfile list write to
129     * @return SUCCESS if resource exist, else not found
130     */
131    static RState GetRawFileListUnCompressed(const std::string &indexPath, const std::string &rawDirPath,
132        std::vector<std::string>& fileList);
133
134    /**
135     * Parse resource hex to resDesc
136     * @param buffer the resource bytes
137     * @param bufLen length in bytes
138     * @param resDesc index file in hap
139     * @param defaultConfig the default config
140     * @return OK if the resource hex parse success, else SYS_ERROR
141     */
142    static int32_t ParseResHex(const char *buffer, const size_t bufLen, ResDesc &resDesc,
143        const std::shared_ptr<ResConfigImpl> defaultConfig = nullptr, const uint32_t &selectedTypes = SELECT_ALL);
144
145    /**
146     * Create resource config from KeyParams
147     * @param keyParams the keyParams contain type and value
148     * @return the resource config related to the keyParams
149     */
150    static std::shared_ptr<ResConfigImpl> CreateResConfigFromKeyParams(
151        const std::vector<std::shared_ptr<KeyParam>> &keyParams);
152
153    /**
154     * To resource folder path
155     * @param keyParams the keyParams contain type and value
156     * @return the resources folder path
157     */
158    static std::string ToFolderPath(const std::vector<std::shared_ptr<KeyParam>> &keyParams);
159
160    /**
161     * Get screen density
162     * @param value the type of screen density
163     * @return the screen density related to the value
164     */
165    static ScreenDensity GetScreenDensity(uint32_t value);
166
167    /**
168     * Get device type
169     * @param value the type of device
170     * @return the device type related to the value
171     */
172    static DeviceType GetDeviceType(uint32_t value);
173
174    /**
175     * Get color mode
176     * @param value the type of device
177     * @return the color mode related to the value
178     */
179    static ColorMode GetColorMode(uint32_t value);
180
181    /**
182     * Get mcc
183     * @param value the type of mcc
184     * @return the mcc related to the value
185     */
186    static uint32_t GetMcc(uint32_t value);
187
188    /**
189     * Get mnc
190     * @param value the type of mnc
191     * @return the mnc related to the value
192     */
193    static uint32_t GetMnc(uint32_t value);
194
195    /**
196     * Get input device
197     * @param value the type of input device
198     * @return the input device related to the value
199     */
200    static InputDevice GetInputDevice(uint32_t value);
201
202    /**
203     * Whether this raw resource is a directory
204     *
205     * @param hapPath the hap path
206     * @param pathName the raw resource path
207     * @param outValue the obtain boolean value write to
208     * @return SUCCESS if raw resource is a directory, else not found
209     */
210    static RState IsRawDirFromHap(const char *hapPath, const std::string &pathName, bool &outValue);
211
212    /**
213     * Whether this raw resource is a directory in UnCompressed
214     *
215     * @param pathName the raw resource path
216     * @param outValue the obtain boolean value write to
217     * @return SUCCESS if raw resource is a directory, else not found
218     */
219    static RState IsRawDirUnCompressed(const std::string &pathName, bool &outValue);
220
221private:
222    static const char *RES_FILE_NAME;
223    struct Determiner {
224        std::string mcc;
225        std::string mnc;
226        std::string language;
227        std::string script;
228        std::string region;
229        std::string direction;
230        std::string deviceType;
231        std::string colorMode;
232        std::string inputDevice;
233        std::string screenDensity;
234    };
235
236    struct ResConfigKey {
237        const char *language = nullptr;
238        const char *script = nullptr;
239        const char *region = nullptr;
240        ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET;
241        Direction direction = DIRECTION_NOT_SET;
242        DeviceType deviceType = DEVICE_NOT_SET;
243        ColorMode colorMode = COLOR_MODE_NOT_SET;
244        InputDevice inputDevice = INPUTDEVICE_NOT_SET;
245        uint32_t mcc = MCC_UNDEFINED;
246        uint32_t mnc = MNC_UNDEFINED;
247    };
248
249    static std::string BuildFolderPath(Determiner *determiner);
250    static std::shared_ptr<ResConfigImpl> BuildResConfig(ResConfigKey *configKey);
251};
252} // namespace Resource
253} // namespace Global
254} // namespace OHOS
255#endif
256