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 <vector>
23 #include "res_desc.h"
24 #include "res_config_impl.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Resource {
29 class HapParser {
30 public:
31     /**
32      * Read specified file in zip to buffer
33      * @param zipFile
34      * @param fileName  file name in zip which we will read
35      * @param buffer    bytes will write to buffer
36      * @param bufLen    the file length in bytes
37      * @param errInfo
38      * @return
39      */
40     static int32_t ReadFileFromZip(const char *zipFile, const char *fileName, void **buffer,
41                                   size_t &bufLen, std::string &errInfo);
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      * @param errInfo
49      * @return
50      */
51     static int32_t ReadIndexFromFile(const char *zipFile, void **buffer,
52                                      size_t &bufLen, std::string &errInfo);
53 
54     /**
55      * Parse resource hex to resDesc
56      * @param buffer the resource bytes
57      * @param bufLen length in bytes
58      * @param resDesc index file in hap
59      * @param defaultConfig the default config
60      * @return OK if the resource hex parse success, else SYS_ERROR
61      */
62     static int32_t ParseResHex(const char *buffer, const size_t bufLen, ResDesc &resDesc,
63                                const ResConfigImpl *defaultConfig = nullptr);
64 
65     /**
66      * Create resource config from KeyParams
67      * @param keyParams the keyParams contain type and value
68      * @return the resource config related to the keyParams
69      */
70     static ResConfigImpl *CreateResConfigFromKeyParams(const std::vector<KeyParam *> &keyParams);
71 
72     /**
73      * To resource folder path
74      * @param keyParams the keyParams contain type and value
75      * @return the resources folder path
76      */
77     static std::string ToFolderPath(const std::vector<KeyParam *> &keyParams);
78 
79     /**
80      * Get screen density
81      * @param value the type of screen density
82      * @return the screen density related to the value
83      */
84     static ScreenDensity GetScreenDensity(uint32_t value);
85 
86     /**
87      * Get device type
88      * @param value the type of device
89      * @return the device type related to the value
90      */
91     static DeviceType GetDeviceType(uint32_t value);
92 
93 private:
94     static const char *RES_FILE_NAME;
95 };
96 } // namespace Resource
97 } // namespace Global
98 } // namespace OHOS
99 #endif
100