• 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 ResConfigImpl *defaultConfig = nullptr);
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 ResConfigImpl *CreateResConfigFromKeyParams(const std::vector<KeyParam *> &keyParams);
148 
149     /**
150      * To resource folder path
151      * @param keyParams the keyParams contain type and value
152      * @return the resources folder path
153      */
154     static std::string ToFolderPath(const std::vector<KeyParam *> &keyParams);
155 
156     /**
157      * Get screen density
158      * @param value the type of screen density
159      * @return the screen density related to the value
160      */
161     static ScreenDensity GetScreenDensity(uint32_t value);
162 
163     /**
164      * Get device type
165      * @param value the type of device
166      * @return the device type related to the value
167      */
168     static DeviceType GetDeviceType(uint32_t value);
169 
170     /**
171      * Get color mode
172      * @param value the type of device
173      * @return the color mode related to the value
174      */
175     static ColorMode GetColorMode(uint32_t value);
176 
177     /**
178      * Get mcc
179      * @param value the type of mcc
180      * @return the mcc related to the value
181      */
182     static uint32_t GetMcc(uint32_t value);
183 
184     /**
185      * Get mnc
186      * @param value the type of mnc
187      * @return the mnc related to the value
188      */
189     static uint32_t GetMnc(uint32_t value);
190 
191     /**
192      * Get input device
193      * @param value the type of input device
194      * @return the input device related to the value
195      */
196     static InputDevice GetInputDevice(uint32_t value);
197 
198 private:
199     static const char *RES_FILE_NAME;
200     struct Determiner {
201         std::string mcc;
202         std::string mnc;
203         std::string language;
204         std::string script;
205         std::string region;
206         std::string direction;
207         std::string deviceType;
208         std::string colorMode;
209         std::string inputDevice;
210         std::string screenDensity;
211     };
212 
213     struct ResConfigKey {
214         const char *language = nullptr;
215         const char *script = nullptr;
216         const char *region = nullptr;
217         ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET;
218         Direction direction = DIRECTION_NOT_SET;
219         DeviceType deviceType = DEVICE_NOT_SET;
220         ColorMode colorMode = COLOR_MODE_NOT_SET;
221         InputDevice inputDevice = INPUTDEVICE_NOT_SET;
222         uint32_t mcc = MCC_UNDEFINED;
223         uint32_t mnc = MNC_UNDEFINED;
224     };
225 
226     static std::string BuildFolderPath(Determiner *determiner);
227     static ResConfigImpl *BuildResConfig(ResConfigKey *configKey);
228 };
229 } // namespace Resource
230 } // namespace Global
231 } // namespace OHOS
232 #endif
233