From 1a121cd3bcc647cf41fe442452663e5a0b75694e Mon Sep 17 00:00:00 2001 From: 18242988924 Date: Fri, 18 Aug 2023 02:24:39 +0000 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=A4=A7=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 18242988924 Change-Id: Id6265b39b6a9543a1d26a052d3bc7e8359e97cd6 --- .../bundlemgr_lite/include/gt_bundle_parser.h | 2 + .../bundlemgr_lite/src/gt_bundle_parser.cpp | 70 +++++++++++++++---- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/services/bundlemgr_lite/include/gt_bundle_parser.h b/services/bundlemgr_lite/include/gt_bundle_parser.h index f9599b6..2dac76d 100644 --- a/services/bundlemgr_lite/include/gt_bundle_parser.h +++ b/services/bundlemgr_lite/include/gt_bundle_parser.h @@ -34,6 +34,8 @@ public: private: static uint8_t ParseJsonInfo(const cJSON *appObject, const cJSON *configObject, const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes); + static uint8_t CheckApiVersion(const cJSON *appObject, BundleProfile &bundleProfile); + static uint8_t CheckApi10Version(int32_t compatibleApiVersion); static BundleInfo *CreateBundleInfo(const char *path, const BundleProfile &bundleProfile, const BundleRes &bundleRes); static bool ConvertIconResToBundleInfo(const char *resPath, uint32_t iconId, BundleInfo *bundleInfo); diff --git a/services/bundlemgr_lite/src/gt_bundle_parser.cpp b/services/bundlemgr_lite/src/gt_bundle_parser.cpp index dcbcf77..44134fb 100644 --- a/services/bundlemgr_lite/src/gt_bundle_parser.cpp +++ b/services/bundlemgr_lite/src/gt_bundle_parser.cpp @@ -15,6 +15,9 @@ #include "gt_bundle_parser.h" +#include +#include + #include "ability_info_utils.h" #include "adapter.h" #include "appexecfwk_errors.h" @@ -34,6 +37,12 @@ namespace OHOS { const int32_t BASE_API_VERSION = 3; +const int32_t API_VERSION_MASK = 1000; +const char *DEVICE_API_VERSION_KEY = "const.product.os.dist.apiversion"; +const int32_t DEVICE_API_VERSION_LEN = 16; +const int32_t DEVICE_API_VERSION_MINI_LEN = 5; +const int32_t STRTOL_DECIMALISM_FLAG = 10; +const char STRING_END_FLAG = '\0'; int32_t GtBundleParser::ParseValue(const cJSON *object, const char *key, int32_t defaultValue) { @@ -207,26 +216,57 @@ uint8_t GtBundleParser::ParseJsonInfo(const cJSON *appObject, const cJSON *confi return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_VERSIONCODE_ERROR; } // check apiVersion - if (cJSON_HasObjectItem(appObject, PROFILE_KEY_APIVERSION)) { - object = ParseValue(appObject, PROFILE_KEY_APIVERSION, nullptr); - CHECK_NULL(object, ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); - bundleProfile.profileApiVersion.minApiVersion = ParseValue(object, PROFILE_KEY_APIVERSION_COMPATIBLE, -1); - if (cJSON_HasObjectItem(object, PROFILE_KEY_APIVERSION_TARGET)) { - bundleProfile.profileApiVersion.maxApiVersion = ParseValue(object, PROFILE_KEY_APIVERSION_TARGET, -1); - CHECK_IS_TRUE( - (bundleProfile.profileApiVersion.maxApiVersion >= bundleProfile.profileApiVersion.minApiVersion), - ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); - } - } else { - // parse deviceConfig - bundleProfile.profileApiVersion.minApiVersion = BASE_API_VERSION; - bundleProfile.profileApiVersion.maxApiVersion = BASE_API_VERSION; - } + uint8_t checkRet = CheckApiVersion(appObject, bundleProfile); + CHECK_IS_TRUE((checkRet == ERR_OK), ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); uint8_t errorCode = ParseModuleInfo(moduleObject, bundleProfile, bundleRes); return errorCode; } +uint8_t GtBundleParser::CheckApiVersion(const cJSON *appObject, BundleProfile &bundleProfile) +{ + if (!cJSON_HasObjectItem(appObject, PROFILE_KEY_APIVERSION)) { + // parse deviceConfig + bundleProfile.profileApiVersion.minApiVersion = BASE_API_VERSION; + bundleProfile.profileApiVersion.maxApiVersion = BASE_API_VERSION; + return ERR_OK; + } + cJSON *object = ParseValue(appObject, PROFILE_KEY_APIVERSION, nullptr); + CHECK_NULL(object, ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + if (!cJSON_HasObjectItem(object, PROFILE_KEY_APIVERSION_COMPATIBLE) || + !cJSON_HasObjectItem(object, PROFILE_KEY_APIVERSION_TARGET)) { + return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR; + } + bundleProfile.profileApiVersion.minApiVersion = ParseValue(object, PROFILE_KEY_APIVERSION_COMPATIBLE, -1); + bundleProfile.profileApiVersion.maxApiVersion = ParseValue(object, PROFILE_KEY_APIVERSION_TARGET, -1); + CHECK_IS_TRUE( + (bundleProfile.profileApiVersion.maxApiVersion >= bundleProfile.profileApiVersion.minApiVersion), + ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + // API 10 + if (bundleProfile.profileApiVersion.minApiVersion >= API_VERSION_MASK) { + uint8_t checkRet = CheckApi10Version(bundleProfile.profileApiVersion.minApiVersion); + CHECK_IS_TRUE((checkRet == ERR_OK), ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + } + return ERR_OK; +} + +uint8_t GtBundleParser::CheckApi10Version(int32_t compatibleApiVersion) +{ + int32_t apiLevel = GetSdkApiVersion(); + char value[DEVICE_API_VERSION_LEN] = {0}; + int32_t ret = GetParameter(DEVICE_API_VERSION_KEY, "", value, DEVICE_API_VERSION_LEN); + CHECK_IS_TRUE((ret >= 0), ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + CHECK_IS_TRUE((strlen(value) >= DEVICE_API_VERSION_MINI_LEN), + ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + char* endptr; + long num = strtol(value, &endptr, STRTOL_DECIMALISM_FLAG); + CHECK_IS_TRUE((*endptr == STRING_END_FLAG), ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + int32_t apiVersion = static_cast(num); + int32_t deviceVersion = apiVersion * API_VERSION_MASK + apiLevel; + CHECK_IS_TRUE((deviceVersion >= compatibleApiVersion), ERR_APPEXECFWK_INSTALL_FAILED_PARSE_API_VERSION_ERROR); + return ERR_OK; +} + uint8_t GtBundleParser::ParseModuleInfo(const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes) { // parse deviceType