1 /* 2 * Copyright (c) 2020 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 BundleInfo *CreateBundleInfo(const char *path, const BundleProfile &bundleProfile, 38 const BundleRes &bundleRes); 39 static bool ConvertIconResToBundleInfo(const char *resPath, uint32_t iconId, BundleInfo *bundleInfo); 40 static uint8_t ParseModuleInfo(const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes); 41 static uint8_t ParseAbilityInfo(const cJSON *abilityInfoObjects, BundleProfile &bundleProfile, 42 BundleRes &bundleRes); 43 static uint8_t ParseModuleMetaData(const cJSON *moduleObject, BundleProfile &bundleProfile); 44 static bool SetModuleInfos(const BundleProfile &bundleProfile, BundleInfo *bundleInfo); 45 static uint8_t SaveBundleInfo(const BundleProfile &bundleProfile, const BundleRes &bundleRes, 46 BundleInfo **bundleInfo); 47 static bool SetBundleInfo(const char *installedPath, const BundleProfile &bundleProfile, 48 const BundleRes &bundleRes, BundleInfo *bundleInfo); 49 static char *ParseValue(const cJSON *object, const char *key); 50 static int32_t ParseValue(const cJSON *object, const char *key, int32_t defaultValue); 51 static cJSON *ParseValue(const cJSON *object, const char *key, cJSON *defaultValue); 52 static uint8_t ParsePermissions(const cJSON *object, Permissions &permissions); 53 static bool SetReqPermission(const cJSON *object, PermissionTrans *permission); 54 static bool CheckDeviceTypeIsValid(const cJSON *deviceTypeObject); 55 }; 56 57 #define CHECK_NULL(object, errorCode) \ 58 do { \ 59 if (object == nullptr) { \ 60 return errorCode; \ 61 } \ 62 } while (0) 63 64 #define CHECK_LENGTH(length, maxLength, errorCode) \ 65 do { \ 66 if (length > maxLength) { \ 67 return errorCode; \ 68 } \ 69 } while (0) 70 71 #define CHECK_IS_TRUE(result, errorCode) \ 72 do { \ 73 if (!result) { \ 74 return errorCode; \ 75 } \ 76 } while (0) 77 78 #define FREE_BUNDLE_PROFILE(bundleProfile) \ 79 do { \ 80 for (int32_t i = 0; i < METADATA_SIZE; i++) { \ 81 AdapterFree(bundleProfile.moduleInfo.metaData[i]); \ 82 } \ 83 } while (0) 84 85 #define FREE_BUNDLE_RES(bundleRes) \ 86 do { \ 87 if (bundleRes.abilityRes != nullptr) { \ 88 AdapterFree(bundleRes.abilityRes); \ 89 } \ 90 } while (0) 91 92 #define CHECK_PARSE_RESULT(errorCode, object, bundleProfile, bundleRes) \ 93 do { \ 94 if (errorCode != ERR_OK) { \ 95 FREE_BUNDLE_PROFILE(bundleProfile); \ 96 FREE_BUNDLE_RES(bundleRes); \ 97 cJSON_Delete(object); \ 98 return errorCode; \ 99 } \ 100 } while (0) 101 } // namespace OHOS 102 #endif // OHOS_GT_BUNDLE_PARSER_H 103