1 /* 2 * Copyright (c) 2021-2025 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 OHOS_RESOURCE_MANAGER_HAPPARSER_H 17 #define OHOS_RESOURCE_MANAGER_HAPPARSER_H 18 19 #include <cstdint> 20 #include <cstdio> 21 #include <string> 22 #include <set> 23 #include <unzip.h> 24 25 #include "hap_resource.h" 26 #include "mmap_file.h" 27 #include "res_desc.h" 28 #include "res_config_impl.h" 29 #include "resource_manager.h" 30 31 namespace OHOS { 32 namespace Global { 33 namespace Resource { 34 class HapParser { 35 public: 36 HapParser(); 37 38 virtual ~HapParser(); 39 40 virtual bool Init(const char *path); 41 42 virtual int32_t ParseResHex(); 43 44 virtual std::shared_ptr<HapResource> GetHapResource(const char *path, bool isSystem, bool isOverlay); 45 46 static bool GetIndexData(const char *path, std::unique_ptr<uint8_t[]> &buf, size_t &bufLen); 47 48 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__) 49 static std::string GetIndexFilePath(std::shared_ptr<AbilityBase::Extractor> &extractor); 50 #endif 51 52 static bool GetIndexDataFromHap(const char *path, std::unique_ptr<uint8_t[]> &buf, size_t &bufLen); 53 54 static bool GetIndexDataFromIndex(const char *path, std::unique_ptr<uint8_t[]> &buf, size_t &bufLen); 55 56 /** 57 * Get the raw file data from hap 58 * @param hapPath the hap path 59 * @param patchPath the hqf path 60 * @param rawFileName the rawFile path 61 * @param len the rawFile path 62 * @param outValue the rawFile path 63 * @return the rawFile path 64 */ 65 static RState ReadRawFileFromHap(const std::string &hapPath, const std::string &patchPath, 66 const std::string &rawFileName, size_t &len, std::unique_ptr<uint8_t[]> &outValue); 67 68 /** 69 * Get the raw file descriptor 70 * @param hapPath the hap path 71 * @param patchPath the hqf path 72 * @param rawFileName the rawFile path 73 * @param descriptor the rawFile path 74 * @return the rawFile path 75 */ 76 static RState ReadRawFileDescriptor(const char *hapPath, const char *patchPath, 77 const std::string &rawFileName, ResourceManager::RawFileDescriptor &descriptor); 78 79 /** 80 * Get the raw file list 81 * @param hapPath the hap path 82 * @param rawDirPath the rawfile directory path 83 * @param fileList the rawfile list write to 84 * @return SUCCESS if resource exist, else not found 85 */ 86 static RState GetRawFileList(const std::string &hapPath, const std::string &rawDirPath, 87 std::set<std::string>& fileList); 88 89 /** 90 * Get the raw file list in UnCompressed 91 * @param indexPath the hap path 92 * @param rawDirPath the rawfile directory path 93 * @param fileList the rawfile list write to 94 * @return SUCCESS if resource exist, else not found 95 */ 96 static RState GetRawFileListUnCompressed(const std::string &indexPath, const std::string &rawDirPath, 97 std::vector<std::string>& fileList); 98 99 /** 100 * Get screen density 101 * @param value the type of screen density 102 * @return the screen density related to the value 103 */ 104 static ScreenDensity GetScreenDensity(uint32_t value); 105 106 /** 107 * Get device type 108 * @param value the type of device 109 * @return the device type related to the value 110 */ 111 static DeviceType GetDeviceType(uint32_t value); 112 113 /** 114 * Get color mode 115 * @param value the type of device 116 * @return the color mode related to the value 117 */ 118 static ColorMode GetColorMode(uint32_t value); 119 120 /** 121 * Get mcc 122 * @param value the type of mcc 123 * @return the mcc related to the value 124 */ 125 static uint32_t GetMcc(uint32_t value); 126 127 /** 128 * Get mnc 129 * @param value the type of mnc 130 * @return the mnc related to the value 131 */ 132 static uint32_t GetMnc(uint32_t value); 133 134 /** 135 * Get input device 136 * @param value the type of input device 137 * @return the input device related to the value 138 */ 139 static InputDevice GetInputDevice(uint32_t value); 140 141 /** 142 * Whether this raw resource is a directory 143 * 144 * @param hapPath the hap path 145 * @param pathName the raw resource path 146 * @param outValue the obtain boolean value write to 147 * @return SUCCESS if raw resource is a directory, else not found 148 */ 149 static RState IsRawDirFromHap(const char *hapPath, const std::string &pathName, bool &outValue); 150 151 /** 152 * Whether this raw resource is a directory in UnCompressed 153 * 154 * @param pathName the raw resource path 155 * @param outValue the obtain boolean value write to 156 * @return SUCCESS if raw resource is a directory, else not found 157 */ 158 static RState IsRawDirUnCompressed(const std::string &pathName, bool &outValue); 159 160 /** 161 * Create resource config from KeyParams 162 * @param keyParams the keyParams contain type and value 163 * @return the resource config related to the keyParams 164 */ 165 static std::shared_ptr<ResConfigImpl> CreateResConfigFromKeyParams( 166 const std::vector<std::shared_ptr<KeyParam>> &keyParams); 167 168 struct ResConfigKey { 169 const char *language = nullptr; 170 const char *script = nullptr; 171 const char *region = nullptr; 172 ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET; 173 Direction direction = DIRECTION_NOT_SET; 174 DeviceType deviceType = DEVICE_NOT_SET; 175 ColorMode colorMode = COLOR_MODE_NOT_SET; 176 InputDevice inputDevice = INPUTDEVICE_NOT_SET; 177 uint32_t mcc = MCC_UNDEFINED; 178 uint32_t mnc = MNC_UNDEFINED; 179 }; 180 181 static std::shared_ptr<ResConfigImpl> BuildResConfig(ResConfigKey *configKey); 182 183 static void GetKeyParamsLocales(std::shared_ptr<KeyParam> kp, std::string &locale, bool &isLocale); 184 185 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__) 186 static std::string ParseModuleName(std::shared_ptr<AbilityBase::Extractor> &extractor); 187 #endif 188 protected: 189 uint32_t limitKeyValue_{0}; 190 191 std::set<std::string> locales_; 192 }; 193 } // namespace Resource 194 } // namespace Global 195 } // namespace OHOS 196 #endif 197