15ba71b47Sopenharmony_ci/*
25ba71b47Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
35ba71b47Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ba71b47Sopenharmony_ci * you may not use this file except in compliance with the License.
55ba71b47Sopenharmony_ci * You may obtain a copy of the License at
65ba71b47Sopenharmony_ci *
75ba71b47Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ba71b47Sopenharmony_ci *
95ba71b47Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ba71b47Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ba71b47Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ba71b47Sopenharmony_ci * See the License for the specific language governing permissions and
135ba71b47Sopenharmony_ci * limitations under the License.
145ba71b47Sopenharmony_ci */
155ba71b47Sopenharmony_ci
165ba71b47Sopenharmony_ci#include "parse_util.h"
175ba71b47Sopenharmony_ci
185ba71b47Sopenharmony_ci#include <cinttypes>
195ba71b47Sopenharmony_ci#include <dlfcn.h>
205ba71b47Sopenharmony_ci#include <fstream>
215ba71b47Sopenharmony_ci#include <unistd.h>
225ba71b47Sopenharmony_ci#include <memory>
235ba71b47Sopenharmony_ci#include <sstream>
245ba71b47Sopenharmony_ci#include <vector>
255ba71b47Sopenharmony_ci#include <algorithm>
265ba71b47Sopenharmony_ci
275ba71b47Sopenharmony_ci#include "datetime_ex.h"
285ba71b47Sopenharmony_ci#include "hisysevent_adapter.h"
295ba71b47Sopenharmony_ci#include "hitrace_meter.h"
305ba71b47Sopenharmony_ci#include "sam_log.h"
315ba71b47Sopenharmony_ci#include "string_ex.h"
325ba71b47Sopenharmony_ci#include "samgr_xcollie.h"
335ba71b47Sopenharmony_ci
345ba71b47Sopenharmony_cinamespace OHOS {
355ba71b47Sopenharmony_ciusing std::string;
365ba71b47Sopenharmony_ci
375ba71b47Sopenharmony_cinamespace {
385ba71b47Sopenharmony_ciconstexpr const char* EVENT_TYPE = "eventId";
395ba71b47Sopenharmony_ciconstexpr const char* EVENT_NAME = "name";
405ba71b47Sopenharmony_ciconstexpr const char* EVENT_VALUE = "value";
415ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_SYSTEM_ABILITY = "systemability";
425ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_PROCESS = "process";
435ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_LIB_PATH = "libpath";
445ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_NAME = "name";
455ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_DEPEND = "depend";
465ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_DEPEND_TIMEOUT = "depend-time-out";
475ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_DEPEND_TIMEOUT_COMPATIBILITY = "depend_time_out";
485ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_RUN_ON_CREATE = "run-on-create";
495ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_MODULE_UPDATE = "module-update";
505ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_AUTO_RESTART = "auto-restart";
515ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_DISTRIBUTED = "distributed";
525ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_CACHE_COMMON_EVENT = "cache-common-event";
535ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_DUMP_LEVEL = "dump-level";
545ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_CAPABILITY = "capability";
555ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_PERMISSION = "permission";
565ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_BOOT_PHASE = "bootphase";
575ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_SAID = "said";
585ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_START_ON_DEMAND = "start-on-demand";
595ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_STOP_ON_DEMAND = "stop-on-demand";
605ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_ALLOW_UPDATE = "allow-update";
615ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_RECYCLE_DELAYTIME = "recycle-delaytime";
625ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_DEVICE_ON_LINE = "deviceonline";
635ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_SETTING_SWITCH = "settingswitch";
645ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_COMMON_EVENT = "commonevent";
655ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_PARAM = "param";
665ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_TIEMD_EVENT = "timedevent";
675ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_RECYCLE_STRATEGY = "recycle-strategy";
685ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_EXTENSION = "extension";
695ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_UNREF_UNLOAD = "unreferenced-unload";
705ba71b47Sopenharmony_ciconstexpr const char* SA_TAG_LONGTIMEUNUSED_UNLOAD = "longtimeunused-unload";
715ba71b47Sopenharmony_ciconstexpr int32_t MAX_JSON_OBJECT_SIZE = 50 * 1024;
725ba71b47Sopenharmony_ciconstexpr int32_t MAX_JSON_STRING_LENGTH = 128;
735ba71b47Sopenharmony_ciconstexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000000;
745ba71b47Sopenharmony_ciconstexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff;
755ba71b47Sopenharmony_ciconstexpr int32_t MAX_EXTENSIONO_NUM = 100;
765ba71b47Sopenharmony_ciconstexpr int32_t MAX_DLOPEN_SECONDS = 60;
775ba71b47Sopenharmony_ciconstexpr const char* BOOT_START_PHASE = "BootStartPhase";
785ba71b47Sopenharmony_ciconstexpr const char* CORE_START_PHASE = "CoreStartPhase";
795ba71b47Sopenharmony_ciconstexpr const char* HIGH_LOAD_PRIORITY = "HighPriority";
805ba71b47Sopenharmony_ciconstexpr const char* MEDIUM_LOAD_PRIORITY = "MediumPriority";
815ba71b47Sopenharmony_ci
825ba71b47Sopenharmony_cienum {
835ba71b47Sopenharmony_ci    BOOT_START = 1,
845ba71b47Sopenharmony_ci    CORE_START = 2,
855ba71b47Sopenharmony_ci    OTHER_START = 3,
865ba71b47Sopenharmony_ci};
875ba71b47Sopenharmony_ci
885ba71b47Sopenharmony_cienum {
895ba71b47Sopenharmony_ci    EQ = 1,
905ba71b47Sopenharmony_ci    GREATER_EQ = 2,
915ba71b47Sopenharmony_ci    GREATER = 3,
925ba71b47Sopenharmony_ci    LESS_EQ = 4,
935ba71b47Sopenharmony_ci    LESS = 5
945ba71b47Sopenharmony_ci};
955ba71b47Sopenharmony_ci}
965ba71b47Sopenharmony_ci
975ba71b47Sopenharmony_ciParseUtil::~ParseUtil()
985ba71b47Sopenharmony_ci{
995ba71b47Sopenharmony_ci    ClearResource();
1005ba71b47Sopenharmony_ci}
1015ba71b47Sopenharmony_ci
1025ba71b47Sopenharmony_civoid ParseUtil::CloseHandle(SaProfile& saProfile)
1035ba71b47Sopenharmony_ci{
1045ba71b47Sopenharmony_ci    if (saProfile.handle == nullptr) {
1055ba71b47Sopenharmony_ci        return;
1065ba71b47Sopenharmony_ci    }
1075ba71b47Sopenharmony_ci    int32_t ret = dlclose(saProfile.handle);
1085ba71b47Sopenharmony_ci    if (ret) {
1095ba71b47Sopenharmony_ci        HILOGW("close handle failed with errno:%{public}d!", errno);
1105ba71b47Sopenharmony_ci    }
1115ba71b47Sopenharmony_ci    saProfile.handle = nullptr;
1125ba71b47Sopenharmony_ci}
1135ba71b47Sopenharmony_ci
1145ba71b47Sopenharmony_civoid ParseUtil::CloseSo()
1155ba71b47Sopenharmony_ci{
1165ba71b47Sopenharmony_ci    for (auto& saProfile : saProfiles_) {
1175ba71b47Sopenharmony_ci        CloseHandle(saProfile);
1185ba71b47Sopenharmony_ci    }
1195ba71b47Sopenharmony_ci}
1205ba71b47Sopenharmony_ci
1215ba71b47Sopenharmony_civoid ParseUtil::CloseSo(int32_t systemAbilityId)
1225ba71b47Sopenharmony_ci{
1235ba71b47Sopenharmony_ci    for (auto& saProfile : saProfiles_) {
1245ba71b47Sopenharmony_ci        if (saProfile.saId == systemAbilityId) {
1255ba71b47Sopenharmony_ci            CloseHandle(saProfile);
1265ba71b47Sopenharmony_ci            break;
1275ba71b47Sopenharmony_ci        }
1285ba71b47Sopenharmony_ci    }
1295ba71b47Sopenharmony_ci}
1305ba71b47Sopenharmony_ci
1315ba71b47Sopenharmony_civoid ParseUtil::ClearResource()
1325ba71b47Sopenharmony_ci{
1335ba71b47Sopenharmony_ci    CloseSo();
1345ba71b47Sopenharmony_ci    saProfiles_.clear();
1355ba71b47Sopenharmony_ci}
1365ba71b47Sopenharmony_ci
1375ba71b47Sopenharmony_civoid ParseUtil::OpenSo(uint32_t bootPhase)
1385ba71b47Sopenharmony_ci{
1395ba71b47Sopenharmony_ci    for (auto& saProfile : saProfiles_) {
1405ba71b47Sopenharmony_ci        if (saProfile.runOnCreate && saProfile.bootPhase == bootPhase) {
1415ba71b47Sopenharmony_ci            OpenSo(saProfile);
1425ba71b47Sopenharmony_ci        }
1435ba71b47Sopenharmony_ci    }
1445ba71b47Sopenharmony_ci}
1455ba71b47Sopenharmony_ci
1465ba71b47Sopenharmony_civoid ParseUtil::OpenSo(SaProfile& saProfile)
1475ba71b47Sopenharmony_ci{
1485ba71b47Sopenharmony_ci    if (saProfile.handle == nullptr) {
1495ba71b47Sopenharmony_ci        string dlopenTag = ToString(saProfile.saId) + "_DLOPEN";
1505ba71b47Sopenharmony_ci        HITRACE_METER_NAME(HITRACE_TAG_SAMGR, dlopenTag);
1515ba71b47Sopenharmony_ci        int64_t begin = GetTickCount();
1525ba71b47Sopenharmony_ci        DlHandle handle = nullptr;
1535ba71b47Sopenharmony_ci        if (saProfile.runOnCreate) {
1545ba71b47Sopenharmony_ci            handle = dlopen(saProfile.libPath.c_str(), RTLD_NOW);
1555ba71b47Sopenharmony_ci        } else {
1565ba71b47Sopenharmony_ci            SamgrXCollie samgrXCollie("safwk--openso_" + ToString(saProfile.saId), MAX_DLOPEN_SECONDS);
1575ba71b47Sopenharmony_ci            handle = dlopen(saProfile.libPath.c_str(), RTLD_NOW);
1585ba71b47Sopenharmony_ci        }
1595ba71b47Sopenharmony_ci        int64_t duration = GetTickCount() - begin;
1605ba71b47Sopenharmony_ci        ReportSaLoadDuration(saProfile.saId, SA_LOAD_OPENSO, duration);
1615ba71b47Sopenharmony_ci        KHILOGI("SA:%{public}d OpenSo spend %{public}" PRId64 "ms",
1625ba71b47Sopenharmony_ci            saProfile.saId, duration);
1635ba71b47Sopenharmony_ci        if (handle == nullptr) {
1645ba71b47Sopenharmony_ci            std::vector<string> libPathVec;
1655ba71b47Sopenharmony_ci            string fileName = "";
1665ba71b47Sopenharmony_ci            SplitStr(saProfile.libPath, "/", libPathVec);
1675ba71b47Sopenharmony_ci            if (libPathVec.size() > 0) {
1685ba71b47Sopenharmony_ci                fileName = libPathVec[libPathVec.size() - 1];
1695ba71b47Sopenharmony_ci            }
1705ba71b47Sopenharmony_ci            ReportAddSystemAbilityFailed(saProfile.saId, getpid(), getuid(), fileName);
1715ba71b47Sopenharmony_ci            HILOGE("SA:%{public}d dlopen %{public}s failed with errno:%{public}s!",
1725ba71b47Sopenharmony_ci                saProfile.saId, fileName.c_str(), dlerror());
1735ba71b47Sopenharmony_ci            return;
1745ba71b47Sopenharmony_ci        }
1755ba71b47Sopenharmony_ci        saProfile.handle = handle;
1765ba71b47Sopenharmony_ci    } else {
1775ba71b47Sopenharmony_ci        KHILOGI("SA:%{public}d handle is not null", saProfile.saId);
1785ba71b47Sopenharmony_ci    }
1795ba71b47Sopenharmony_ci}
1805ba71b47Sopenharmony_ci
1815ba71b47Sopenharmony_cibool ParseUtil::LoadSaLib(int32_t systemAbilityId)
1825ba71b47Sopenharmony_ci{
1835ba71b47Sopenharmony_ci    for (auto& saProfile : saProfiles_) {
1845ba71b47Sopenharmony_ci        if (saProfile.saId == systemAbilityId) {
1855ba71b47Sopenharmony_ci            OpenSo(saProfile);
1865ba71b47Sopenharmony_ci            return true;
1875ba71b47Sopenharmony_ci        }
1885ba71b47Sopenharmony_ci    }
1895ba71b47Sopenharmony_ci    return false;
1905ba71b47Sopenharmony_ci}
1915ba71b47Sopenharmony_ci
1925ba71b47Sopenharmony_ciconst std::list<SaProfile>& ParseUtil::GetAllSaProfiles() const
1935ba71b47Sopenharmony_ci{
1945ba71b47Sopenharmony_ci    return saProfiles_;
1955ba71b47Sopenharmony_ci}
1965ba71b47Sopenharmony_ci
1975ba71b47Sopenharmony_cibool ParseUtil::GetProfile(int32_t saId, SaProfile& saProfile)
1985ba71b47Sopenharmony_ci{
1995ba71b47Sopenharmony_ci    auto iter = std::find_if(saProfiles_.begin(), saProfiles_.end(), [saId](auto saProfile) {
2005ba71b47Sopenharmony_ci        return saProfile.saId == saId;
2015ba71b47Sopenharmony_ci    });
2025ba71b47Sopenharmony_ci    if (iter != saProfiles_.end()) {
2035ba71b47Sopenharmony_ci        saProfile = *iter;
2045ba71b47Sopenharmony_ci        return true;
2055ba71b47Sopenharmony_ci    }
2065ba71b47Sopenharmony_ci    return false;
2075ba71b47Sopenharmony_ci}
2085ba71b47Sopenharmony_ci
2095ba71b47Sopenharmony_civoid ParseUtil::RemoveSaProfile(int32_t saId)
2105ba71b47Sopenharmony_ci{
2115ba71b47Sopenharmony_ci    saProfiles_.remove_if([saId] (auto saInfo) -> bool { return saInfo.saId == saId; });
2125ba71b47Sopenharmony_ci}
2135ba71b47Sopenharmony_ci
2145ba71b47Sopenharmony_ciuint32_t ParseUtil::GetBootPriorityPara(const std::string& bootPhase)
2155ba71b47Sopenharmony_ci{
2165ba71b47Sopenharmony_ci    if (bootPhase == BOOT_START_PHASE) {
2175ba71b47Sopenharmony_ci        return static_cast<uint32_t>(BOOT_START);
2185ba71b47Sopenharmony_ci    } else if (bootPhase == CORE_START_PHASE) {
2195ba71b47Sopenharmony_ci        return static_cast<uint32_t>(CORE_START);
2205ba71b47Sopenharmony_ci    } else {
2215ba71b47Sopenharmony_ci        return static_cast<uint32_t>(OTHER_START);
2225ba71b47Sopenharmony_ci    }
2235ba71b47Sopenharmony_ci}
2245ba71b47Sopenharmony_ci
2255ba71b47Sopenharmony_ciuint32_t ParseUtil::GetOndemandPriorityPara(const std::string& loadPriority)
2265ba71b47Sopenharmony_ci{
2275ba71b47Sopenharmony_ci    if (loadPriority == HIGH_LOAD_PRIORITY) {
2285ba71b47Sopenharmony_ci        return static_cast<uint32_t>(HIGH_PRIORITY);
2295ba71b47Sopenharmony_ci    } else if (loadPriority == MEDIUM_LOAD_PRIORITY) {
2305ba71b47Sopenharmony_ci        return static_cast<uint32_t>(MEDIUM_PRIORITY);
2315ba71b47Sopenharmony_ci    } else {
2325ba71b47Sopenharmony_ci        return static_cast<uint32_t>(LOW_PRIORITY);
2335ba71b47Sopenharmony_ci    }
2345ba71b47Sopenharmony_ci}
2355ba71b47Sopenharmony_ci
2365ba71b47Sopenharmony_cibool ParseUtil::ParseSaProfiles(const string& profilePath)
2375ba71b47Sopenharmony_ci{
2385ba71b47Sopenharmony_ci    HILOGD("profilePath:%{private}s", profilePath.c_str());
2395ba71b47Sopenharmony_ci    string realPath = GetRealPath(profilePath);
2405ba71b47Sopenharmony_ci    if (!CheckPathExist(realPath.c_str())) {
2415ba71b47Sopenharmony_ci        HILOGE("bad profile path!");
2425ba71b47Sopenharmony_ci        return false;
2435ba71b47Sopenharmony_ci    }
2445ba71b47Sopenharmony_ci
2455ba71b47Sopenharmony_ci    if (Endswith(realPath, ".json")) {
2465ba71b47Sopenharmony_ci        return ParseJsonFile(realPath);
2475ba71b47Sopenharmony_ci    } else {
2485ba71b47Sopenharmony_ci        HILOGE("Invalid file format, please use json file!");
2495ba71b47Sopenharmony_ci        return false;
2505ba71b47Sopenharmony_ci    }
2515ba71b47Sopenharmony_ci}
2525ba71b47Sopenharmony_ci
2535ba71b47Sopenharmony_cibool ParseUtil::Endswith(const std::string& src, const std::string& sub)
2545ba71b47Sopenharmony_ci{
2555ba71b47Sopenharmony_ci    return (src.length() >= sub.length() && (src.rfind(sub) == (src.length() - sub.length())));
2565ba71b47Sopenharmony_ci}
2575ba71b47Sopenharmony_ci
2585ba71b47Sopenharmony_cistd::unordered_map<std::string, std::string> ParseUtil::StringToMap(const std::string& eventStr)
2595ba71b47Sopenharmony_ci{
2605ba71b47Sopenharmony_ci    nlohmann::json eventJson = StringToJsonObj(eventStr);
2615ba71b47Sopenharmony_ci    std::unordered_map<std::string, std::string> eventMap = JsonObjToMap(eventJson);
2625ba71b47Sopenharmony_ci    return eventMap;
2635ba71b47Sopenharmony_ci}
2645ba71b47Sopenharmony_ci
2655ba71b47Sopenharmony_cinlohmann::json ParseUtil::StringToJsonObj(const std::string& eventStr)
2665ba71b47Sopenharmony_ci{
2675ba71b47Sopenharmony_ci    nlohmann::json jsonObj = nlohmann::json::object();
2685ba71b47Sopenharmony_ci    if (eventStr.empty()) {
2695ba71b47Sopenharmony_ci        return jsonObj;
2705ba71b47Sopenharmony_ci    }
2715ba71b47Sopenharmony_ci    nlohmann::json eventJson = nlohmann::json::parse(eventStr, nullptr, false);
2725ba71b47Sopenharmony_ci    if (eventJson.is_discarded()) {
2735ba71b47Sopenharmony_ci        HILOGE("parse eventStr to json failed");
2745ba71b47Sopenharmony_ci        return jsonObj;
2755ba71b47Sopenharmony_ci    }
2765ba71b47Sopenharmony_ci    if (!eventJson.is_object()) {
2775ba71b47Sopenharmony_ci        HILOGE("eventStr converted result is not a jsonObj");
2785ba71b47Sopenharmony_ci        return jsonObj;
2795ba71b47Sopenharmony_ci    }
2805ba71b47Sopenharmony_ci    return eventJson;
2815ba71b47Sopenharmony_ci}
2825ba71b47Sopenharmony_ci
2835ba71b47Sopenharmony_cistd::unordered_map<std::string, std::string> ParseUtil::JsonObjToMap(const nlohmann::json& eventJson)
2845ba71b47Sopenharmony_ci{
2855ba71b47Sopenharmony_ci    std::unordered_map<std::string, std::string> eventMap;
2865ba71b47Sopenharmony_ci    if (eventJson.contains(EVENT_TYPE) && eventJson[EVENT_TYPE].is_string()) {
2875ba71b47Sopenharmony_ci        eventMap[EVENT_TYPE] = eventJson[EVENT_TYPE];
2885ba71b47Sopenharmony_ci    } else {
2895ba71b47Sopenharmony_ci        eventMap[EVENT_TYPE] = "";
2905ba71b47Sopenharmony_ci    }
2915ba71b47Sopenharmony_ci    if (eventJson.contains(EVENT_NAME) && eventJson[EVENT_NAME].is_string()) {
2925ba71b47Sopenharmony_ci        eventMap[EVENT_NAME] = eventJson[EVENT_NAME];
2935ba71b47Sopenharmony_ci    } else {
2945ba71b47Sopenharmony_ci        eventMap[EVENT_NAME] = "";
2955ba71b47Sopenharmony_ci    }
2965ba71b47Sopenharmony_ci    if (eventJson.contains(EVENT_VALUE) && eventJson[EVENT_VALUE].is_string()) {
2975ba71b47Sopenharmony_ci        eventMap[EVENT_VALUE] = eventJson[EVENT_VALUE];
2985ba71b47Sopenharmony_ci    } else {
2995ba71b47Sopenharmony_ci        eventMap[EVENT_VALUE] = "";
3005ba71b47Sopenharmony_ci    }
3015ba71b47Sopenharmony_ci    return eventMap;
3025ba71b47Sopenharmony_ci}
3035ba71b47Sopenharmony_ci
3045ba71b47Sopenharmony_cibool ParseUtil::ParseJsonFile(const string& realPath)
3055ba71b47Sopenharmony_ci{
3065ba71b47Sopenharmony_ci    nlohmann::json profileJson;
3075ba71b47Sopenharmony_ci    bool result = ParseJsonObj(profileJson, realPath);
3085ba71b47Sopenharmony_ci    if (!result) {
3095ba71b47Sopenharmony_ci        HILOGE("json file parse error!");
3105ba71b47Sopenharmony_ci        return false;
3115ba71b47Sopenharmony_ci    }
3125ba71b47Sopenharmony_ci    HILOGD("profileJson:%{private}s", profileJson.dump().c_str());
3135ba71b47Sopenharmony_ci    string process;
3145ba71b47Sopenharmony_ci    GetStringFromJson(profileJson, SA_TAG_PROCESS, process);
3155ba71b47Sopenharmony_ci    if (process.empty()) {
3165ba71b47Sopenharmony_ci        HILOGE("profile format error: no process tag");
3175ba71b47Sopenharmony_ci        return false;
3185ba71b47Sopenharmony_ci    }
3195ba71b47Sopenharmony_ci    if (process.length() > MAX_JSON_STRING_LENGTH) {
3205ba71b47Sopenharmony_ci        HILOGE("profile format error: process is too long");
3215ba71b47Sopenharmony_ci        return false;
3225ba71b47Sopenharmony_ci    }
3235ba71b47Sopenharmony_ci    procName_ = Str8ToStr16(process);
3245ba71b47Sopenharmony_ci    if (profileJson.find(SA_TAG_SYSTEM_ABILITY) == profileJson.end()) {
3255ba71b47Sopenharmony_ci        HILOGE("system ability parse error!");
3265ba71b47Sopenharmony_ci        return false;
3275ba71b47Sopenharmony_ci    }
3285ba71b47Sopenharmony_ci    nlohmann::json& systemAbilityJson = profileJson.at(SA_TAG_SYSTEM_ABILITY);
3295ba71b47Sopenharmony_ci    HILOGD("systemAbilityJson:%{private}s", systemAbilityJson.dump().c_str());
3305ba71b47Sopenharmony_ci    if (!systemAbilityJson.is_array()) {
3315ba71b47Sopenharmony_ci        HILOGE("system ability is not array!");
3325ba71b47Sopenharmony_ci        return false;
3335ba71b47Sopenharmony_ci    }
3345ba71b47Sopenharmony_ci    size_t size = systemAbilityJson.size();
3355ba71b47Sopenharmony_ci    for (size_t i = 0; i < size; i++) {
3365ba71b47Sopenharmony_ci        SaProfile saProfile = { procName_ };
3375ba71b47Sopenharmony_ci        if (!ParseSystemAbility(saProfile, systemAbilityJson[i])) {
3385ba71b47Sopenharmony_ci            continue;
3395ba71b47Sopenharmony_ci        }
3405ba71b47Sopenharmony_ci        saProfiles_.emplace_back(saProfile);
3415ba71b47Sopenharmony_ci    }
3425ba71b47Sopenharmony_ci    return !saProfiles_.empty();
3435ba71b47Sopenharmony_ci}
3445ba71b47Sopenharmony_ci
3455ba71b47Sopenharmony_cibool ParseUtil::ParseSystemAbilityGetExtension(SaProfile& saProfile, nlohmann::json& systemAbilityJson)
3465ba71b47Sopenharmony_ci{
3475ba71b47Sopenharmony_ci    if ((systemAbilityJson.find(SA_TAG_EXTENSION) != systemAbilityJson.end()) &&
3485ba71b47Sopenharmony_ci        (systemAbilityJson[SA_TAG_EXTENSION].is_array())) {
3495ba71b47Sopenharmony_ci        for (auto& item : systemAbilityJson[SA_TAG_EXTENSION]) {
3505ba71b47Sopenharmony_ci            std::string extension = item.get<std::string>();
3515ba71b47Sopenharmony_ci            if (extension.length() > MAX_JSON_STRING_LENGTH) {
3525ba71b47Sopenharmony_ci                HILOGE("profile format error: extension() len exceed limit");
3535ba71b47Sopenharmony_ci                return false;
3545ba71b47Sopenharmony_ci            }
3555ba71b47Sopenharmony_ci            if (saProfile.extension.size() >= MAX_EXTENSIONO_NUM) {
3565ba71b47Sopenharmony_ci                HILOGE("profile format error: extension num exceed limit");
3575ba71b47Sopenharmony_ci                return false;
3585ba71b47Sopenharmony_ci            }
3595ba71b47Sopenharmony_ci
3605ba71b47Sopenharmony_ci            if (std::find(saProfile.extension.begin(), saProfile.extension.end(), extension) ==
3615ba71b47Sopenharmony_ci                saProfile.extension.end()) {
3625ba71b47Sopenharmony_ci                saProfile.extension.push_back(extension);
3635ba71b47Sopenharmony_ci            }
3645ba71b47Sopenharmony_ci        }
3655ba71b47Sopenharmony_ci    }
3665ba71b47Sopenharmony_ci    return true;
3675ba71b47Sopenharmony_ci}
3685ba71b47Sopenharmony_ci
3695ba71b47Sopenharmony_cibool ParseUtil::ParseSystemAbilityGetSaBaseInfo(SaProfile& saProfile, nlohmann::json& systemAbilityJson)
3705ba71b47Sopenharmony_ci{
3715ba71b47Sopenharmony_ci    GetInt32FromJson(systemAbilityJson, SA_TAG_NAME, saProfile.saId);
3725ba71b47Sopenharmony_ci    if (saProfile.saId == 0) {
3735ba71b47Sopenharmony_ci        HILOGE("profile format error: no name tag");
3745ba71b47Sopenharmony_ci        return false;
3755ba71b47Sopenharmony_ci    }
3765ba71b47Sopenharmony_ci    if (saProfile.saId < FIRST_SYS_ABILITY_ID || saProfile.saId > LAST_SYS_ABILITY_ID) {
3775ba71b47Sopenharmony_ci        HILOGE("profile format error: saId error");
3785ba71b47Sopenharmony_ci        return false;
3795ba71b47Sopenharmony_ci    }
3805ba71b47Sopenharmony_ci    GetStringFromJson(systemAbilityJson, SA_TAG_LIB_PATH, saProfile.libPath);
3815ba71b47Sopenharmony_ci    if (saProfile.libPath.empty()) {
3825ba71b47Sopenharmony_ci        HILOGE("profile format error: no libPath tag");
3835ba71b47Sopenharmony_ci        return false;
3845ba71b47Sopenharmony_ci    }
3855ba71b47Sopenharmony_ci    if (saProfile.libPath.length() > MAX_JSON_STRING_LENGTH) {
3865ba71b47Sopenharmony_ci        HILOGE("profile format error: libPath is too long");
3875ba71b47Sopenharmony_ci        return false;
3885ba71b47Sopenharmony_ci    }
3895ba71b47Sopenharmony_ci    return true;
3905ba71b47Sopenharmony_ci}
3915ba71b47Sopenharmony_ci
3925ba71b47Sopenharmony_cibool ParseUtil::ParseSystemAbilityGetSaExtInfo(SaProfile& saProfile, nlohmann::json& systemAbilityJson)
3935ba71b47Sopenharmony_ci{
3945ba71b47Sopenharmony_ci    GetBoolFromJson(systemAbilityJson, SA_TAG_RUN_ON_CREATE, saProfile.runOnCreate);
3955ba71b47Sopenharmony_ci    GetBoolFromJson(systemAbilityJson, SA_TAG_MODULE_UPDATE, saProfile.moduleUpdate);
3965ba71b47Sopenharmony_ci    GetBoolFromJson(systemAbilityJson, SA_TAG_AUTO_RESTART, saProfile.autoRestart);
3975ba71b47Sopenharmony_ci    GetBoolFromJson(systemAbilityJson, SA_TAG_DISTRIBUTED, saProfile.distributed);
3985ba71b47Sopenharmony_ci    GetBoolFromJson(systemAbilityJson, SA_TAG_CACHE_COMMON_EVENT, saProfile.cacheCommonEvent);
3995ba71b47Sopenharmony_ci    GetIntArrayFromJson(systemAbilityJson, SA_TAG_DEPEND, saProfile.dependSa);
4005ba71b47Sopenharmony_ci    GetInt32FromJson(systemAbilityJson, SA_TAG_DEPEND_TIMEOUT, saProfile.dependTimeout);
4015ba71b47Sopenharmony_ci    if (saProfile.dependTimeout == 0) {
4025ba71b47Sopenharmony_ci        GetInt32FromJson(systemAbilityJson, SA_TAG_DEPEND_TIMEOUT_COMPATIBILITY, saProfile.dependTimeout);
4035ba71b47Sopenharmony_ci    }
4045ba71b47Sopenharmony_ci    GetInt32FromJson(systemAbilityJson, SA_TAG_DUMP_LEVEL, saProfile.dumpLevel);
4055ba71b47Sopenharmony_ci    string capability;
4065ba71b47Sopenharmony_ci    GetStringFromJson(systemAbilityJson, SA_TAG_CAPABILITY, capability);
4075ba71b47Sopenharmony_ci    saProfile.capability = capability.length() <= MAX_JSON_STRING_LENGTH ? Str8ToStr16(capability) : u"";
4085ba71b47Sopenharmony_ci    string permission;
4095ba71b47Sopenharmony_ci    GetStringFromJson(systemAbilityJson, SA_TAG_PERMISSION, permission);
4105ba71b47Sopenharmony_ci    saProfile.permission = permission.length() <= MAX_JSON_STRING_LENGTH ? Str8ToStr16(permission) : u"";
4115ba71b47Sopenharmony_ci    string bootPhase;
4125ba71b47Sopenharmony_ci    GetStringFromJson(systemAbilityJson, SA_TAG_BOOT_PHASE, bootPhase);
4135ba71b47Sopenharmony_ci    saProfile.bootPhase = GetBootPriorityPara(bootPhase);
4145ba71b47Sopenharmony_ci    // parse start-on-demand tag
4155ba71b47Sopenharmony_ci    ParseStartOndemandTag(systemAbilityJson, SA_TAG_START_ON_DEMAND, saProfile.startOnDemand);
4165ba71b47Sopenharmony_ci    // parse stop-on-demand tag
4175ba71b47Sopenharmony_ci    ParseStopOndemandTag(systemAbilityJson, SA_TAG_STOP_ON_DEMAND, saProfile.stopOnDemand);
4185ba71b47Sopenharmony_ci    string recycleStrategy;
4195ba71b47Sopenharmony_ci    GetStringFromJson(systemAbilityJson, SA_TAG_RECYCLE_STRATEGY, recycleStrategy);
4205ba71b47Sopenharmony_ci    if (!CheckRecycleStrategy(recycleStrategy, saProfile.recycleStrategy)) {
4215ba71b47Sopenharmony_ci        HILOGE("profile format error: recycleStrategy: %{public}s is not immediately or low-memory",
4225ba71b47Sopenharmony_ci            recycleStrategy.c_str());
4235ba71b47Sopenharmony_ci        return false;
4245ba71b47Sopenharmony_ci    }
4255ba71b47Sopenharmony_ci    if (!ParseSystemAbilityGetExtension(saProfile, systemAbilityJson)) {
4265ba71b47Sopenharmony_ci        return false;
4275ba71b47Sopenharmony_ci    }
4285ba71b47Sopenharmony_ci    return true;
4295ba71b47Sopenharmony_ci}
4305ba71b47Sopenharmony_ci
4315ba71b47Sopenharmony_cibool ParseUtil::ParseSystemAbility(SaProfile& saProfile, nlohmann::json& systemAbilityJson)
4325ba71b47Sopenharmony_ci{
4335ba71b47Sopenharmony_ci    HILOGD("ParseSystemAbility begin");
4345ba71b47Sopenharmony_ci    if (!ParseSystemAbilityGetSaBaseInfo(saProfile, systemAbilityJson)) {
4355ba71b47Sopenharmony_ci        return false;
4365ba71b47Sopenharmony_ci    }
4375ba71b47Sopenharmony_ci    if (!ParseSystemAbilityGetSaExtInfo(saProfile, systemAbilityJson)) {
4385ba71b47Sopenharmony_ci        return false;
4395ba71b47Sopenharmony_ci    }
4405ba71b47Sopenharmony_ci    HILOGD("ParseSystemAbility end");
4415ba71b47Sopenharmony_ci    return true;
4425ba71b47Sopenharmony_ci}
4435ba71b47Sopenharmony_ci
4445ba71b47Sopenharmony_cibool ParseUtil::CheckRecycleStrategy(const std::string& recycleStrategyStr, int32_t& recycleStrategy)
4455ba71b47Sopenharmony_ci{
4465ba71b47Sopenharmony_ci    if (recycleStrategyStr == "" || recycleStrategyStr == "immediately") {
4475ba71b47Sopenharmony_ci        recycleStrategy = IMMEDIATELY;
4485ba71b47Sopenharmony_ci        return true;
4495ba71b47Sopenharmony_ci    } else if (recycleStrategyStr == "low-memory") {
4505ba71b47Sopenharmony_ci        recycleStrategy = LOW_MEMORY;
4515ba71b47Sopenharmony_ci        return true;
4525ba71b47Sopenharmony_ci    }
4535ba71b47Sopenharmony_ci    return false;
4545ba71b47Sopenharmony_ci}
4555ba71b47Sopenharmony_ci
4565ba71b47Sopenharmony_cibool ParseUtil::ParseJsonTag(const nlohmann::json& systemAbilityJson, const std::string& jsonTag,
4575ba71b47Sopenharmony_ci    nlohmann::json& onDemandJson)
4585ba71b47Sopenharmony_ci{
4595ba71b47Sopenharmony_ci    if (systemAbilityJson.find(jsonTag) == systemAbilityJson.end()) {
4605ba71b47Sopenharmony_ci        return false;
4615ba71b47Sopenharmony_ci    }
4625ba71b47Sopenharmony_ci    onDemandJson = systemAbilityJson.at(jsonTag);
4635ba71b47Sopenharmony_ci    if (!onDemandJson.is_object()) {
4645ba71b47Sopenharmony_ci        HILOGE("parse ondemand tag error");
4655ba71b47Sopenharmony_ci        return false;
4665ba71b47Sopenharmony_ci    }
4675ba71b47Sopenharmony_ci    return true;
4685ba71b47Sopenharmony_ci}
4695ba71b47Sopenharmony_ci
4705ba71b47Sopenharmony_civoid ParseUtil::ParseOndemandTag(const nlohmann::json& onDemandJson, std::vector<OnDemandEvent>& onDemandEvents)
4715ba71b47Sopenharmony_ci{
4725ba71b47Sopenharmony_ci    GetOnDemandArrayFromJson(DEVICE_ONLINE, onDemandJson, SA_TAG_DEVICE_ON_LINE, onDemandEvents);
4735ba71b47Sopenharmony_ci    GetOnDemandArrayFromJson(SETTING_SWITCH, onDemandJson, SA_TAG_SETTING_SWITCH, onDemandEvents);
4745ba71b47Sopenharmony_ci    GetOnDemandArrayFromJson(COMMON_EVENT, onDemandJson, SA_TAG_COMMON_EVENT, onDemandEvents);
4755ba71b47Sopenharmony_ci    GetOnDemandArrayFromJson(PARAM, onDemandJson, SA_TAG_PARAM, onDemandEvents);
4765ba71b47Sopenharmony_ci    GetOnDemandArrayFromJson(TIMED_EVENT, onDemandJson, SA_TAG_TIEMD_EVENT, onDemandEvents);
4775ba71b47Sopenharmony_ci}
4785ba71b47Sopenharmony_ci
4795ba71b47Sopenharmony_civoid ParseUtil::ParseStartOndemandTag(const nlohmann::json& systemAbilityJson,
4805ba71b47Sopenharmony_ci    const std::string& jsonTag, StartOnDemand& startOnDemand)
4815ba71b47Sopenharmony_ci{
4825ba71b47Sopenharmony_ci    nlohmann::json onDemandJson;
4835ba71b47Sopenharmony_ci    if (!ParseJsonTag(systemAbilityJson, jsonTag, onDemandJson)) {
4845ba71b47Sopenharmony_ci        return;
4855ba71b47Sopenharmony_ci    }
4865ba71b47Sopenharmony_ci    ParseOndemandTag(onDemandJson, startOnDemand.onDemandEvents);
4875ba71b47Sopenharmony_ci    GetBoolFromJson(onDemandJson, SA_TAG_ALLOW_UPDATE, startOnDemand.allowUpdate);
4885ba71b47Sopenharmony_ci}
4895ba71b47Sopenharmony_ci
4905ba71b47Sopenharmony_civoid ParseUtil::ParseStopOndemandTag(const nlohmann::json& systemAbilityJson,
4915ba71b47Sopenharmony_ci    const std::string& jsonTag, StopOnDemand& stopOnDemand)
4925ba71b47Sopenharmony_ci{
4935ba71b47Sopenharmony_ci    nlohmann::json onDemandJson;
4945ba71b47Sopenharmony_ci    if (!ParseJsonTag(systemAbilityJson, jsonTag, onDemandJson)) {
4955ba71b47Sopenharmony_ci        return;
4965ba71b47Sopenharmony_ci    }
4975ba71b47Sopenharmony_ci    ParseOndemandTag(onDemandJson, stopOnDemand.onDemandEvents);
4985ba71b47Sopenharmony_ci    GetBoolFromJson(onDemandJson, SA_TAG_ALLOW_UPDATE, stopOnDemand.allowUpdate);
4995ba71b47Sopenharmony_ci    GetBoolFromJson(onDemandJson, SA_TAG_UNREF_UNLOAD, stopOnDemand.unrefUnload);
5005ba71b47Sopenharmony_ci    GetInt32FromJson(onDemandJson, SA_TAG_RECYCLE_DELAYTIME, stopOnDemand.delayTime);
5015ba71b47Sopenharmony_ci    GetInt32FromJson(onDemandJson, SA_TAG_LONGTIMEUNUSED_UNLOAD, stopOnDemand.unusedTimeout);
5025ba71b47Sopenharmony_ci}
5035ba71b47Sopenharmony_ci
5045ba71b47Sopenharmony_civoid ParseUtil::GetOnDemandArrayFromJson(int32_t eventId, const nlohmann::json& obj,
5055ba71b47Sopenharmony_ci    const std::string& key, std::vector<OnDemandEvent>& out)
5065ba71b47Sopenharmony_ci{
5075ba71b47Sopenharmony_ci    if (obj.find(key.c_str()) != obj.end() && obj[key.c_str()].is_array()) {
5085ba71b47Sopenharmony_ci        for (auto& item : obj[key.c_str()]) {
5095ba71b47Sopenharmony_ci            std::string name;
5105ba71b47Sopenharmony_ci            GetStringFromJson(item, "name", name);
5115ba71b47Sopenharmony_ci            std::string value;
5125ba71b47Sopenharmony_ci            GetStringFromJson(item, "value", value);
5135ba71b47Sopenharmony_ci            bool persistence = false;
5145ba71b47Sopenharmony_ci            GetBoolFromJson(item, "persistence", persistence);
5155ba71b47Sopenharmony_ci            std::vector<OnDemandCondition> conditions;
5165ba71b47Sopenharmony_ci            GetOnDemandConditionsFromJson(item, "conditions", conditions);
5175ba71b47Sopenharmony_ci            HILOGD("conditions size: %{public}zu", conditions.size());
5185ba71b47Sopenharmony_ci            bool enableOnce = false;
5195ba71b47Sopenharmony_ci            GetBoolFromJson(item, "enable-once", enableOnce);
5205ba71b47Sopenharmony_ci            std::string priority;
5215ba71b47Sopenharmony_ci            GetStringFromJson(item, "load-priority", priority);
5225ba71b47Sopenharmony_ci            uint32_t loadPriority = GetOndemandPriorityPara(priority);
5235ba71b47Sopenharmony_ci            std::map<std::string, std::string> extraMessages;
5245ba71b47Sopenharmony_ci            GetOnDemandExtraMessagesFromJson(item, "extra-messages", extraMessages);
5255ba71b47Sopenharmony_ci            HILOGD("extraMessages size: %{public}zu", extraMessages.size());
5265ba71b47Sopenharmony_ci            if (!name.empty() && name.length() <= MAX_JSON_STRING_LENGTH &&
5275ba71b47Sopenharmony_ci                value.length() <= MAX_JSON_STRING_LENGTH) {
5285ba71b47Sopenharmony_ci                OnDemandEvent event = {eventId, name, value, -1, persistence,
5295ba71b47Sopenharmony_ci                    conditions, enableOnce, loadPriority, extraMessages};
5305ba71b47Sopenharmony_ci                out.emplace_back(event);
5315ba71b47Sopenharmony_ci            }
5325ba71b47Sopenharmony_ci        }
5335ba71b47Sopenharmony_ci    }
5345ba71b47Sopenharmony_ci}
5355ba71b47Sopenharmony_ci
5365ba71b47Sopenharmony_civoid ParseUtil::GetOnDemandExtraMessagesFromJson(const nlohmann::json& obj,
5375ba71b47Sopenharmony_ci    const std::string& key, std::map<std::string, std::string>& out)
5385ba71b47Sopenharmony_ci{
5395ba71b47Sopenharmony_ci    if (obj.find(key.c_str()) == obj.end() || !obj[key.c_str()].is_object()) {
5405ba71b47Sopenharmony_ci        return;
5415ba71b47Sopenharmony_ci    }
5425ba71b47Sopenharmony_ci    for (auto &it: obj[key.c_str()].items()) {
5435ba71b47Sopenharmony_ci        if (it.value().is_string()) {
5445ba71b47Sopenharmony_ci            out[it.key()] = it.value();
5455ba71b47Sopenharmony_ci        } else {
5465ba71b47Sopenharmony_ci            HILOGW("extra-mesasge: not string type");
5475ba71b47Sopenharmony_ci        }
5485ba71b47Sopenharmony_ci    }
5495ba71b47Sopenharmony_ci}
5505ba71b47Sopenharmony_ci
5515ba71b47Sopenharmony_civoid ParseUtil::GetOnDemandConditionsFromJson(const nlohmann::json& obj,
5525ba71b47Sopenharmony_ci    const std::string& key, std::vector<OnDemandCondition>& out)
5535ba71b47Sopenharmony_ci{
5545ba71b47Sopenharmony_ci    nlohmann::json conditionsJson;
5555ba71b47Sopenharmony_ci    if (obj.find(key.c_str()) == obj.end() || !obj[key.c_str()].is_array()) {
5565ba71b47Sopenharmony_ci        return;
5575ba71b47Sopenharmony_ci    }
5585ba71b47Sopenharmony_ci    conditionsJson = obj.at(key.c_str());
5595ba71b47Sopenharmony_ci    for (auto& condition : conditionsJson) {
5605ba71b47Sopenharmony_ci        std::string type;
5615ba71b47Sopenharmony_ci        GetStringFromJson(condition, "eventId", type);
5625ba71b47Sopenharmony_ci        std::string name;
5635ba71b47Sopenharmony_ci        GetStringFromJson(condition, "name", name);
5645ba71b47Sopenharmony_ci        std::string value;
5655ba71b47Sopenharmony_ci        GetStringFromJson(condition, "value", value);
5665ba71b47Sopenharmony_ci        int32_t eventId = 0;
5675ba71b47Sopenharmony_ci        if (type == SA_TAG_DEVICE_ON_LINE) {
5685ba71b47Sopenharmony_ci            eventId = DEVICE_ONLINE;
5695ba71b47Sopenharmony_ci        } else if (type == SA_TAG_SETTING_SWITCH) {
5705ba71b47Sopenharmony_ci            eventId = SETTING_SWITCH;
5715ba71b47Sopenharmony_ci        } else if (type == SA_TAG_COMMON_EVENT) {
5725ba71b47Sopenharmony_ci            eventId = COMMON_EVENT;
5735ba71b47Sopenharmony_ci        } else if (type == SA_TAG_PARAM) {
5745ba71b47Sopenharmony_ci            eventId = PARAM;
5755ba71b47Sopenharmony_ci        } else if (type == SA_TAG_TIEMD_EVENT) {
5765ba71b47Sopenharmony_ci            eventId = TIMED_EVENT;
5775ba71b47Sopenharmony_ci        } else {
5785ba71b47Sopenharmony_ci            HILOGW("invalid condition eventId: %{public}s", type.c_str());
5795ba71b47Sopenharmony_ci            continue;
5805ba71b47Sopenharmony_ci        }
5815ba71b47Sopenharmony_ci        std::map<std::string, std::string> extraMessages;
5825ba71b47Sopenharmony_ci        GetOnDemandExtraMessagesFromJson(condition, "extra-messages", extraMessages);
5835ba71b47Sopenharmony_ci        OnDemandCondition conditionEvent = {eventId, name, value, extraMessages};
5845ba71b47Sopenharmony_ci        out.emplace_back(conditionEvent);
5855ba71b47Sopenharmony_ci    }
5865ba71b47Sopenharmony_ci}
5875ba71b47Sopenharmony_ci
5885ba71b47Sopenharmony_cistd::u16string ParseUtil::GetProcessName() const
5895ba71b47Sopenharmony_ci{
5905ba71b47Sopenharmony_ci    return procName_;
5915ba71b47Sopenharmony_ci}
5925ba71b47Sopenharmony_ci
5935ba71b47Sopenharmony_cistring ParseUtil::GetRealPath(const string& profilePath) const
5945ba71b47Sopenharmony_ci{
5955ba71b47Sopenharmony_ci    char path[PATH_MAX] = {'\0'};
5965ba71b47Sopenharmony_ci    if (realpath(profilePath.c_str(), path) == nullptr) {
5975ba71b47Sopenharmony_ci        HILOGD("get real path fail");
5985ba71b47Sopenharmony_ci        return "";
5995ba71b47Sopenharmony_ci    }
6005ba71b47Sopenharmony_ci    string realPath(path);
6015ba71b47Sopenharmony_ci    return realPath;
6025ba71b47Sopenharmony_ci}
6035ba71b47Sopenharmony_ci
6045ba71b47Sopenharmony_cibool ParseUtil::CheckPathExist(const string& profilePath)
6055ba71b47Sopenharmony_ci{
6065ba71b47Sopenharmony_ci    std::ifstream profileStream(profilePath.c_str());
6075ba71b47Sopenharmony_ci    return profileStream.good();
6085ba71b47Sopenharmony_ci}
6095ba71b47Sopenharmony_ci
6105ba71b47Sopenharmony_cibool ParseUtil::ParseTrustConfig(const string& profilePath,
6115ba71b47Sopenharmony_ci    std::map<std::u16string, std::set<int32_t>>& values)
6125ba71b47Sopenharmony_ci{
6135ba71b47Sopenharmony_ci    HILOGD("config path:%{private}s", profilePath.c_str());
6145ba71b47Sopenharmony_ci    string realPath = GetRealPath(profilePath);
6155ba71b47Sopenharmony_ci    if (!CheckPathExist(realPath.c_str())) {
6165ba71b47Sopenharmony_ci        HILOGE("bad profile path!");
6175ba71b47Sopenharmony_ci        return false;
6185ba71b47Sopenharmony_ci    }
6195ba71b47Sopenharmony_ci    nlohmann::json trustSaIdJson;
6205ba71b47Sopenharmony_ci    bool result = ParseJsonObj(trustSaIdJson, realPath);
6215ba71b47Sopenharmony_ci    if (!result) {
6225ba71b47Sopenharmony_ci        HILOGE("trust json file parse error!");
6235ba71b47Sopenharmony_ci        return false;
6245ba71b47Sopenharmony_ci    }
6255ba71b47Sopenharmony_ci    string process;
6265ba71b47Sopenharmony_ci    GetStringFromJson(trustSaIdJson, SA_TAG_PROCESS, process);
6275ba71b47Sopenharmony_ci    if (process.empty()) {
6285ba71b47Sopenharmony_ci        HILOGE("trust profile format error: no process tag");
6295ba71b47Sopenharmony_ci        return false;
6305ba71b47Sopenharmony_ci    }
6315ba71b47Sopenharmony_ci    if (process.length() > MAX_JSON_STRING_LENGTH) {
6325ba71b47Sopenharmony_ci        HILOGE("trust profile format error: process is too long");
6335ba71b47Sopenharmony_ci        return false;
6345ba71b47Sopenharmony_ci    }
6355ba71b47Sopenharmony_ci    auto& saIds = values[Str8ToStr16(process)];
6365ba71b47Sopenharmony_ci    GetIntArrayFromJson(trustSaIdJson, SA_TAG_SAID, saIds);
6375ba71b47Sopenharmony_ci    HILOGI("ParseTrustConfig realPath:%{public}s, saIds size = %{public}zu", realPath.c_str(), saIds.size());
6385ba71b47Sopenharmony_ci    return true;
6395ba71b47Sopenharmony_ci}
6405ba71b47Sopenharmony_ci
6415ba71b47Sopenharmony_cibool ParseUtil::ParseJsonObj(nlohmann::json& jsonObj, const string& jsonPath)
6425ba71b47Sopenharmony_ci{
6435ba71b47Sopenharmony_ci    std::ifstream jsonFileStream;
6445ba71b47Sopenharmony_ci    jsonFileStream.open(jsonPath.c_str(), std::ios::in);
6455ba71b47Sopenharmony_ci    if (!jsonFileStream.is_open()) {
6465ba71b47Sopenharmony_ci        HILOGE("open json file error!!");
6475ba71b47Sopenharmony_ci        return false;
6485ba71b47Sopenharmony_ci    }
6495ba71b47Sopenharmony_ci    std::ostringstream buffer;
6505ba71b47Sopenharmony_ci    char ch;
6515ba71b47Sopenharmony_ci    int32_t readSize = 0;
6525ba71b47Sopenharmony_ci    while (buffer && jsonFileStream.get(ch)) {
6535ba71b47Sopenharmony_ci        readSize++;
6545ba71b47Sopenharmony_ci        if (readSize < MAX_JSON_OBJECT_SIZE) {
6555ba71b47Sopenharmony_ci            buffer.put(ch);
6565ba71b47Sopenharmony_ci        } else {
6575ba71b47Sopenharmony_ci            jsonFileStream.close();
6585ba71b47Sopenharmony_ci            HILOGE("too big json file error!!");
6595ba71b47Sopenharmony_ci            return false;
6605ba71b47Sopenharmony_ci        }
6615ba71b47Sopenharmony_ci    }
6625ba71b47Sopenharmony_ci    jsonFileStream.close();
6635ba71b47Sopenharmony_ci    string jsonStr = buffer.str();
6645ba71b47Sopenharmony_ci    jsonObj = nlohmann::json::parse(jsonStr, nullptr, false);
6655ba71b47Sopenharmony_ci    if (jsonObj.is_discarded()) {
6665ba71b47Sopenharmony_ci        HILOGE("parse json obj error!!");
6675ba71b47Sopenharmony_ci        return false;
6685ba71b47Sopenharmony_ci    }
6695ba71b47Sopenharmony_ci    return true;
6705ba71b47Sopenharmony_ci}
6715ba71b47Sopenharmony_ci
6725ba71b47Sopenharmony_cibool ParseUtil::CheckLogicRelationship(const std::string& state, const std::string& profile)
6735ba71b47Sopenharmony_ci{
6745ba71b47Sopenharmony_ci    HILOGD("CheckLogicRelationship State:%{public}s || Profile:%{public}s", state.c_str(), profile.c_str());
6755ba71b47Sopenharmony_ci    if (profile.empty() || state == profile) {
6765ba71b47Sopenharmony_ci        return true;
6775ba71b47Sopenharmony_ci    }
6785ba71b47Sopenharmony_ci    if (state.empty()) {
6795ba71b47Sopenharmony_ci        return false;
6805ba71b47Sopenharmony_ci    }
6815ba71b47Sopenharmony_ci    int32_t logicRelationship = EQ;
6825ba71b47Sopenharmony_ci    int32_t valueStartPosition = 0;
6835ba71b47Sopenharmony_ci    if (profile[0] == '>') {
6845ba71b47Sopenharmony_ci        valueStartPosition ++;
6855ba71b47Sopenharmony_ci        if (profile[1] == '=') {
6865ba71b47Sopenharmony_ci            valueStartPosition ++;
6875ba71b47Sopenharmony_ci            logicRelationship = GREATER_EQ;
6885ba71b47Sopenharmony_ci        } else {
6895ba71b47Sopenharmony_ci            logicRelationship = GREATER;
6905ba71b47Sopenharmony_ci        }
6915ba71b47Sopenharmony_ci    } else if (profile[0] == '<') {
6925ba71b47Sopenharmony_ci        valueStartPosition ++;
6935ba71b47Sopenharmony_ci        if (profile[1] == '=') {
6945ba71b47Sopenharmony_ci            valueStartPosition ++;
6955ba71b47Sopenharmony_ci            logicRelationship = LESS_EQ;
6965ba71b47Sopenharmony_ci        } else {
6975ba71b47Sopenharmony_ci            logicRelationship = LESS;
6985ba71b47Sopenharmony_ci        }
6995ba71b47Sopenharmony_ci    }
7005ba71b47Sopenharmony_ci    int32_t stateInt, profileInt;
7015ba71b47Sopenharmony_ci    if (!StrToInt(profile.substr(valueStartPosition, profile.length() - 1), profileInt)) {
7025ba71b47Sopenharmony_ci        return false;
7035ba71b47Sopenharmony_ci    }
7045ba71b47Sopenharmony_ci    if (!StrToInt(state, stateInt)) {
7055ba71b47Sopenharmony_ci        return false;
7065ba71b47Sopenharmony_ci    }
7075ba71b47Sopenharmony_ci    if (logicRelationship == EQ) {
7085ba71b47Sopenharmony_ci        return stateInt == profileInt;
7095ba71b47Sopenharmony_ci    } else if (logicRelationship == GREATER_EQ) {
7105ba71b47Sopenharmony_ci        return stateInt >= profileInt;
7115ba71b47Sopenharmony_ci    } else if (logicRelationship == GREATER) {
7125ba71b47Sopenharmony_ci        return stateInt > profileInt;
7135ba71b47Sopenharmony_ci    } else if (logicRelationship == LESS_EQ) {
7145ba71b47Sopenharmony_ci        return stateInt <= profileInt;
7155ba71b47Sopenharmony_ci    } else if (logicRelationship == LESS) {
7165ba71b47Sopenharmony_ci        return stateInt < profileInt;
7175ba71b47Sopenharmony_ci    }
7185ba71b47Sopenharmony_ci    return false;
7195ba71b47Sopenharmony_ci}
7205ba71b47Sopenharmony_ci} // namespace OHOS
721