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 * Parse resource hex to resDesc 123 * @param buffer the resource bytes 124 * @param bufLen length in bytes 125 * @param resDesc index file in hap 126 * @param defaultConfig the default config 127 * @return OK if the resource hex parse success, else SYS_ERROR 128 */ 129 static int32_t ParseResHex(const char *buffer, const size_t bufLen, ResDesc &resDesc, 130 const ResConfigImpl *defaultConfig = nullptr); 131 132 /** 133 * Create resource config from KeyParams 134 * @param keyParams the keyParams contain type and value 135 * @return the resource config related to the keyParams 136 */ 137 static ResConfigImpl *CreateResConfigFromKeyParams(const std::vector<KeyParam *> &keyParams); 138 139 /** 140 * To resource folder path 141 * @param keyParams the keyParams contain type and value 142 * @return the resources folder path 143 */ 144 static std::string ToFolderPath(const std::vector<KeyParam *> &keyParams); 145 146 /** 147 * Get screen density 148 * @param value the type of screen density 149 * @return the screen density related to the value 150 */ 151 static ScreenDensity GetScreenDensity(uint32_t value); 152 153 /** 154 * Get device type 155 * @param value the type of device 156 * @return the device type related to the value 157 */ 158 static DeviceType GetDeviceType(uint32_t value); 159 160 /** 161 * Get color mode 162 * @param value the type of device 163 * @return the color mode related to the value 164 */ 165 static ColorMode GetColorMode(uint32_t value); 166 167 /** 168 * Get mcc 169 * @param value the type of mcc 170 * @return the mcc related to the value 171 */ 172 static uint32_t GetMcc(uint32_t value); 173 174 /** 175 * Get mnc 176 * @param value the type of mnc 177 * @return the mnc related to the value 178 */ 179 static uint32_t GetMnc(uint32_t value); 180 181 /** 182 * Get input device 183 * @param value the type of input device 184 * @return the input device related to the value 185 */ 186 static InputDevice GetInputDevice(uint32_t value); 187 188 private: 189 static const char *RES_FILE_NAME; 190 struct Determiner { 191 std::string mcc; 192 std::string mnc; 193 std::string language; 194 std::string script; 195 std::string region; 196 std::string direction; 197 std::string deviceType; 198 std::string colorMode; 199 std::string inputDevice; 200 std::string screenDensity; 201 }; 202 203 struct ResConfigKey { 204 const char *language = nullptr; 205 const char *script = nullptr; 206 const char *region = nullptr; 207 ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET; 208 Direction direction = DIRECTION_NOT_SET; 209 DeviceType deviceType = DEVICE_NOT_SET; 210 ColorMode colorMode = COLOR_MODE_NOT_SET; 211 InputDevice inputDevice = INPUTDEVICE_NOT_SET; 212 uint32_t mcc = MCC_UNDEFINED; 213 uint32_t mnc = MNC_UNDEFINED; 214 }; 215 216 static std::string BuildFolderPath(Determiner *determiner); 217 static ResConfigImpl *BuildResConfig(ResConfigKey *configKey); 218 }; 219 } // namespace Resource 220 } // namespace Global 221 } // namespace OHOS 222 #endif 223