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 * Get screen density 152 * @param value the type of screen density 153 * @return the screen density related to the value 154 */ 155 static ScreenDensity GetScreenDensity(uint32_t value); 156 157 /** 158 * Get device type 159 * @param value the type of device 160 * @return the device type related to the value 161 */ 162 static DeviceType GetDeviceType(uint32_t value); 163 164 /** 165 * Get color mode 166 * @param value the type of device 167 * @return the color mode related to the value 168 */ 169 static ColorMode GetColorMode(uint32_t value); 170 171 /** 172 * Get mcc 173 * @param value the type of mcc 174 * @return the mcc related to the value 175 */ 176 static uint32_t GetMcc(uint32_t value); 177 178 /** 179 * Get mnc 180 * @param value the type of mnc 181 * @return the mnc related to the value 182 */ 183 static uint32_t GetMnc(uint32_t value); 184 185 /** 186 * Get input device 187 * @param value the type of input device 188 * @return the input device related to the value 189 */ 190 static InputDevice GetInputDevice(uint32_t value); 191 192 /** 193 * Whether this raw resource is a directory 194 * 195 * @param hapPath the hap path 196 * @param pathName the raw resource path 197 * @param outValue the obtain boolean value write to 198 * @return SUCCESS if raw resource is a directory, else not found 199 */ 200 static RState IsRawDirFromHap(const char *hapPath, const std::string &pathName, bool &outValue); 201 202 /** 203 * Whether this raw resource is a directory in UnCompressed 204 * 205 * @param pathName the raw resource path 206 * @param outValue the obtain boolean value write to 207 * @return SUCCESS if raw resource is a directory, else not found 208 */ 209 static RState IsRawDirUnCompressed(const std::string &pathName, bool &outValue); 210 211 private: 212 static const char *RES_FILE_NAME; 213 struct Determiner { 214 std::string mcc; 215 std::string mnc; 216 std::string language; 217 std::string script; 218 std::string region; 219 std::string direction; 220 std::string deviceType; 221 std::string colorMode; 222 std::string inputDevice; 223 std::string screenDensity; 224 }; 225 226 struct ResConfigKey { 227 const char *language = nullptr; 228 const char *script = nullptr; 229 const char *region = nullptr; 230 ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET; 231 Direction direction = DIRECTION_NOT_SET; 232 DeviceType deviceType = DEVICE_NOT_SET; 233 ColorMode colorMode = COLOR_MODE_NOT_SET; 234 InputDevice inputDevice = INPUTDEVICE_NOT_SET; 235 uint32_t mcc = MCC_UNDEFINED; 236 uint32_t mnc = MNC_UNDEFINED; 237 }; 238 239 static std::shared_ptr<ResConfigImpl> BuildResConfig(ResConfigKey *configKey); 240 }; 241 } // namespace Resource 242 } // namespace Global 243 } // namespace OHOS 244 #endif 245