• 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 namespace OHOS {
28 namespace Global {
29 namespace Resource {
30 class HapParser {
31 public:
32     /**
33      * Read specified file in zip to buffer
34      * @param uf
35      * @param fileName  file name in zip which we will read
36      * @param buffer    bytes will write to buffer
37      * @param bufLen    the file length in bytes
38      * @return
39      */
40     static int32_t ReadFileFromZip(unzFile &uf, const char *fileName, std::unique_ptr<uint8_t[]> &buffer,
41                                   size_t &bufLen);
42 
43     /**
44      * Read resource.index in hap to buffer
45      * @param zipFile hap file path
46      * @param buffer  bytes will write to buffer
47      * @param bufLen  length in bytes
48      * @return
49      */
50     static int32_t ReadIndexFromFile(const char *zipFile, std::unique_ptr<uint8_t[]> &buffer, size_t &bufLen);
51 
52     /**
53      * Read rawfile from hap
54      * @param zipFile hap file path
55      * @param buffer  bytes will write to buffer
56      * @param bufLen  length in bytes
57      * @param rawFilePath the path of rawfile
58      * @param rawFile the rawfile info write to
59      * @return
60      */
61     static int32_t ReadRawFileFromHap(const char *zipFile, std::unique_ptr<uint8_t[]> &buffer, size_t &bufLen,
62                             const std::string &rawFilePath, std::unique_ptr<ResourceManager::RawFile> &rawFile);
63 
64     /**
65      * Whether the hap is STAGE MODE or not
66      * @param hapPath hap hap path
67      * @return true if the hap is STAGE MODE, else false
68      */
69     static bool IsStageMode(unzFile &uf);
70 
71     /**
72      * Parse resource hex to resDesc
73      * @param buffer the resource bytes
74      * @param bufLen length in bytes
75      * @param resDesc index file in hap
76      * @param defaultConfig the default config
77      * @return OK if the resource hex parse success, else SYS_ERROR
78      */
79     static int32_t ParseResHex(const char *buffer, const size_t bufLen, ResDesc &resDesc,
80                                const ResConfigImpl *defaultConfig = nullptr);
81 
82     /**
83      * Create resource config from KeyParams
84      * @param keyParams the keyParams contain type and value
85      * @return the resource config related to the keyParams
86      */
87     static ResConfigImpl *CreateResConfigFromKeyParams(const std::vector<KeyParam *> &keyParams);
88 
89     /**
90      * To resource folder path
91      * @param keyParams the keyParams contain type and value
92      * @return the resources folder path
93      */
94     static std::string ToFolderPath(const std::vector<KeyParam *> &keyParams);
95 
96     /**
97      * Get screen density
98      * @param value the type of screen density
99      * @return the screen density related to the value
100      */
101     static ScreenDensity GetScreenDensity(uint32_t value);
102 
103     /**
104      * Get device type
105      * @param value the type of device
106      * @return the device type related to the value
107      */
108     static DeviceType GetDeviceType(uint32_t value);
109 
110     /**
111      * Get color mode
112      * @param value the type of device
113      * @return the color mode related to the value
114      */
115     static ColorMode GetColorMode(uint32_t value);
116 
117     /**
118      * Get mcc
119      * @param value the type of mcc
120      * @return the mcc related to the value
121      */
122     static uint32_t GetMcc(uint32_t value);
123 
124     /**
125      * Get mnc
126      * @param value the type of mnc
127      * @return the mnc related to the value
128      */
129     static uint32_t GetMnc(uint32_t value);
130 
131     /**
132      * Get input device
133      * @param value the type of input device
134      * @return the input device related to the value
135      */
136     static InputDevice GetInputDevice(uint32_t value);
137 
138 private:
139     static const char *RES_FILE_NAME;
140     struct Determiner {
141         std::string mcc;
142         std::string mnc;
143         std::string language;
144         std::string script;
145         std::string region;
146         std::string direction;
147         std::string deviceType;
148         std::string colorMode;
149         std::string inputDevice;
150         std::string screenDensity;
151     };
152 
153     struct ResConfigKey {
154         const char *language = nullptr;
155         const char *script = nullptr;
156         const char *region = nullptr;
157         ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET;
158         Direction direction = DIRECTION_NOT_SET;
159         DeviceType deviceType = DEVICE_NOT_SET;
160         ColorMode colorMode = COLOR_MODE_NOT_SET;
161         InputDevice inputDevice = INPUTDEVICE_NOT_SET;
162         uint32_t mcc = MCC_UNDEFINED;
163         uint32_t mnc = MNC_UNDEFINED;
164     };
165 
166     static std::string BuildFolderPath(Determiner *determiner);
167     static ResConfigImpl *BuildResConfig(ResConfigKey *configKey);
168 };
169 } // namespace Resource
170 } // namespace Global
171 } // namespace OHOS
172 #endif
173