1 /* 2 * Copyright (c) 2020-2023 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_GT_BUNDLE_PARSER_H 17 #define OHOS_GT_BUNDLE_PARSER_H 18 19 #include "bundle_common.h" 20 #include "bundle_info.h" 21 #include "stdint.h" 22 23 #include "cJSON.h" 24 25 namespace OHOS { 26 class GtBundleParser { 27 public: 28 static BundleInfo *ParseHapProfile(const char *path, BundleRes *bundleRes); 29 static uint8_t ParseHapProfile(int32_t fp, uint32_t fileSize, Permissions &permissions, BundleRes &bundleRes, 30 BundleInfo **bundleInfo); 31 static bool ParseBundleAttr(const char *path, char **bundleName, int32_t &versionCode); 32 static uint8_t ConvertResInfoToBundleInfo(const char *path, uint32_t labelId, uint32_t iconId, 33 BundleInfo *bundleInfo); 34 private: 35 static uint8_t ParseJsonInfo(const cJSON *appObject, const cJSON *configObject, const cJSON *moduleObject, 36 BundleProfile &bundleProfile, BundleRes &bundleRes); 37 static uint8_t CheckApiVersion(const cJSON *appObject, BundleProfile &bundleProfile); 38 static uint8_t CheckApi10Version(int32_t compatibleApiVersion); 39 static BundleInfo *CreateBundleInfo(const char *path, const BundleProfile &bundleProfile, 40 const BundleRes &bundleRes); 41 static bool ConvertIconResToBundleInfo(const char *resPath, uint32_t iconId, BundleInfo *bundleInfo); 42 static uint8_t ParseModuleInfo(const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes); 43 static uint8_t ParseAbilityInfo(const cJSON *abilityInfoObjects, BundleProfile &bundleProfile, 44 BundleRes &bundleRes); 45 static uint8_t ParseModuleMetaData(const cJSON *moduleObject, BundleProfile &bundleProfile); 46 static uint8_t ParseAllAbilityInfo(const cJSON *abilityObjects, BundleProfile &bundleProfile); 47 static uint8_t ParsePerAbilityInfo(const cJSON *abilityObjects, AbilityInfo &abilityInfo); 48 static uint8_t ParseMetaData(const cJSON *moduleObject, MetaData *metaData[], int maxCount); 49 static uint8_t ParseAbilitySkills(const cJSON *abilityObjectItem, AbilityInfo &abilityInfo); 50 static uint8_t ParseOneSkill(const cJSON *skillObject, Skill &skill); 51 static bool SetModuleInfos(const BundleProfile &bundleProfile, BundleInfo *bundleInfo); 52 static uint8_t SaveBundleInfo(const BundleProfile &bundleProfile, const BundleRes &bundleRes, 53 BundleInfo **bundleInfo); 54 static bool SetBundleInfo(const char *installedPath, const BundleProfile &bundleProfile, 55 const BundleRes &bundleRes, BundleInfo *bundleInfo); 56 static char *ParseValue(const cJSON *object, const char *key); 57 static int32_t ParseValue(const cJSON *object, const char *key, int32_t defaultValue); 58 static cJSON *ParseValue(const cJSON *object, const char *key, cJSON *defaultValue); 59 static uint8_t ParsePermissions(const cJSON *object, Permissions &permissions); 60 static bool SetReqPermission(const cJSON *object, PermissionTrans *permission); 61 static bool CheckDeviceTypeIsValid(const cJSON *deviceTypeObject); 62 }; 63 64 #define CHECK_NULL(object, errorCode) \ 65 do { \ 66 if ((object) == nullptr) { \ 67 return errorCode; \ 68 } \ 69 } while (0) 70 71 #define CHECK_LENGTH(length, maxLength, errorCode) \ 72 do { \ 73 if ((length) > (maxLength)) { \ 74 return errorCode; \ 75 } \ 76 } while (0) 77 78 #define CHECK_IS_TRUE(result, errorCode) \ 79 do { \ 80 if (!(result)) { \ 81 return errorCode; \ 82 } \ 83 } while (0) 84 85 #define FREE_BUNDLE_PROFILE(bundleProfile) \ 86 do { \ 87 for (int32_t i = 0; i < METADATA_SIZE; i++) { \ 88 AdapterFree((bundleProfile).moduleInfo.metaData[i]); \ 89 } \ 90 } while (0) 91 92 #define FREE_BUNDLE_RES(bundleRes) \ 93 do { \ 94 if ((bundleRes).abilityRes != nullptr) { \ 95 AdapterFree((bundleRes).abilityRes); \ 96 } \ 97 } while (0) 98 99 #define CHECK_PARSE_RESULT(errorCode, object, bundleProfile, bundleRes) \ 100 do { \ 101 if ((errorCode) != ERR_OK) { \ 102 FREE_BUNDLE_PROFILE(bundleProfile); \ 103 FREE_BUNDLE_RES(bundleRes); \ 104 cJSON_Delete(object); \ 105 return errorCode; \ 106 } \ 107 } while (0) 108 } // namespace OHOS 109 #endif // OHOS_GT_BUNDLE_PARSER_H 110