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