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