125c1cde8Sopenharmony_ci/* 225c1cde8Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 325c1cde8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 425c1cde8Sopenharmony_ci * you may not use this file except in compliance with the License. 525c1cde8Sopenharmony_ci * You may obtain a copy of the License at 625c1cde8Sopenharmony_ci * 725c1cde8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 825c1cde8Sopenharmony_ci * 925c1cde8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1025c1cde8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1125c1cde8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1225c1cde8Sopenharmony_ci * See the License for the specific language governing permissions and 1325c1cde8Sopenharmony_ci * limitations under the License. 1425c1cde8Sopenharmony_ci */ 1525c1cde8Sopenharmony_ci 1625c1cde8Sopenharmony_ci#include "parser_util.h" 1725c1cde8Sopenharmony_ci 1825c1cde8Sopenharmony_ci#include <cstdio> 1925c1cde8Sopenharmony_ci#include <securec.h> 2025c1cde8Sopenharmony_ci#include <unistd.h> 2125c1cde8Sopenharmony_ci 2225c1cde8Sopenharmony_ci#include <fstream> 2325c1cde8Sopenharmony_ci#include <iostream> 2425c1cde8Sopenharmony_ci#include <zlib.h> 2525c1cde8Sopenharmony_ci#include "climits" 2625c1cde8Sopenharmony_ci#include "config_policy_utils.h" 2725c1cde8Sopenharmony_ci#include "cstdint" 2825c1cde8Sopenharmony_ci#include "cstdio" 2925c1cde8Sopenharmony_ci#include "cstdlib" 3025c1cde8Sopenharmony_ci#include "cstring" 3125c1cde8Sopenharmony_ci#include "core_service_client.h" 3225c1cde8Sopenharmony_ci#include "data_storage_errors.h" 3325c1cde8Sopenharmony_ci#include "data_storage_log_wrapper.h" 3425c1cde8Sopenharmony_ci#include "global_params_data.h" 3525c1cde8Sopenharmony_ci#include "memory" 3625c1cde8Sopenharmony_ci#include "new" 3725c1cde8Sopenharmony_ci#include "opkey_data.h" 3825c1cde8Sopenharmony_ci#include "parameters.h" 3925c1cde8Sopenharmony_ci#include "pdp_profile_data.h" 4025c1cde8Sopenharmony_ci#include "telephony_types.h" 4125c1cde8Sopenharmony_ci#include "values_bucket.h" 4225c1cde8Sopenharmony_ci#include "vector" 4325c1cde8Sopenharmony_ci#include "preferences_util.h" 4425c1cde8Sopenharmony_ci 4525c1cde8Sopenharmony_cinamespace OHOS { 4625c1cde8Sopenharmony_cinamespace Telephony { 4725c1cde8Sopenharmony_ciconst char *PATH = "/etc/telephony/pdp_profile.json"; 4825c1cde8Sopenharmony_ciconst char *ITEM_OPERATOR_INFOS = "operator_infos"; 4925c1cde8Sopenharmony_ciconst char *ITEM_OPERATOR_NAME = "operator_name"; 5025c1cde8Sopenharmony_ciconst char *ITEM_AUTH_USER = "auth_user"; 5125c1cde8Sopenharmony_ciconst char *ITEM_AUTH_PWD = "auth_pwd"; 5225c1cde8Sopenharmony_ciconst char *ITEM_AUTH_TYPE = "auth_type"; 5325c1cde8Sopenharmony_ciconst char *ITEM_MCC = "mcc"; 5425c1cde8Sopenharmony_ciconst char *ITEM_MNC = "mnc"; 5525c1cde8Sopenharmony_ciconst char *ITEM_APN = "apn"; 5625c1cde8Sopenharmony_ciconst char *ITEM_APN_TYPES = "apn_types"; 5725c1cde8Sopenharmony_ciconst char *ITEM_IP_ADDRESS = "ip_addr"; 5825c1cde8Sopenharmony_ciconst char *ITEM_MMS_IP_ADDRESS = "mms_ip_addr"; 5925c1cde8Sopenharmony_ciconst char *ITEM_HOME_URL = "home_url"; 6025c1cde8Sopenharmony_ciconst char *ITEM_MVNO_TYPE = "mvno_type"; 6125c1cde8Sopenharmony_ciconst char *ITEM_MVNO_MATCH_DATA = "mvno_match_data"; 6225c1cde8Sopenharmony_ciconst char *ITEM_EDITED_STATUS = "edited"; 6325c1cde8Sopenharmony_ciconst char *ITEM_SERVER = "server"; 6425c1cde8Sopenharmony_ciconst char *ITEM_BEARER = "bearing_system_type"; 6525c1cde8Sopenharmony_ciconst char *ITEM_IS_ROAMING_APN = "is_roaming_apn"; 6625c1cde8Sopenharmony_ciconst char *ITEM_APN_PROTOCOL = "apn_protocol"; 6725c1cde8Sopenharmony_ciconst char *ITEM_ROAMING_PROTOCOL = "apn_roam_protocol"; 6825c1cde8Sopenharmony_ciconst char *APN_VERSION = "apn_version"; 6925c1cde8Sopenharmony_ciconst char *OPKEY_INFO_PATH = "etc/telephony/OpkeyInfo.json"; 7025c1cde8Sopenharmony_ciconst char *ITEM_OPERATOR_ID = "operator_id"; 7125c1cde8Sopenharmony_ciconst char *ITEM_RULE = "rule"; 7225c1cde8Sopenharmony_ciconst char *ITEM_MCCMNC = "mcc_mnc"; 7325c1cde8Sopenharmony_ciconst char *ITEM_GID_ONE = "gid1"; 7425c1cde8Sopenharmony_ciconst char *ITEM_GID_TWO = "gid2"; 7525c1cde8Sopenharmony_ciconst char *ITEM_IMSI = "imsi"; 7625c1cde8Sopenharmony_ciconst char *ITEM_SPN = "spn"; 7725c1cde8Sopenharmony_ciconst char *ITEM_ICCID = "iccid"; 7825c1cde8Sopenharmony_ciconst char *ITEM_OPERATOR_NAME_OPKEY = "operator_name"; 7925c1cde8Sopenharmony_ciconst char *ITEM_OPERATOR_KEY = "operator_key"; 8025c1cde8Sopenharmony_ciconst char *ITEM_OPERATOR_KEY_EXT = "operator_key_ext"; 8125c1cde8Sopenharmony_ciconst char *NUM_MATCH_PATH = "etc/telephony/number_match.json"; 8225c1cde8Sopenharmony_ciconst char *ECC_DATA_PATH = "etc/telephony/ecc_data.json"; 8325c1cde8Sopenharmony_ciconst char *ITEM_NUM_MATCH_INFOS = "numMatchs"; 8425c1cde8Sopenharmony_ciconst char *ITEM_NAME = "name"; 8525c1cde8Sopenharmony_ciconst char *ITEM_NUM_MATCH = "num_match"; 8625c1cde8Sopenharmony_ciconst char *ITEM_NUM_MATCH_SHORT = "num_match_short"; 8725c1cde8Sopenharmony_ciconst char *ITEM_NUMERIC = "numeric"; 8825c1cde8Sopenharmony_ciconst char *ITEM_ECC_WITH_CARD = "ecc_withcard"; 8925c1cde8Sopenharmony_ciconst char *ITEM_ECC_NO_CARD = "ecc_nocard"; 9025c1cde8Sopenharmony_ciconst char *ITEM_ECC_FAKE = "ecc_fake"; 9125c1cde8Sopenharmony_ciconst int BYTE_LEN = 1024 * 1024; 9225c1cde8Sopenharmony_ciconst int MAX_BYTE_LEN = 10 * 1024 * 1024; 9325c1cde8Sopenharmony_cistatic constexpr const char *CUST_RULE_PATH_KEY = "const.telephony.rule_path"; 9425c1cde8Sopenharmony_cistatic constexpr const char *CUST_NETWORK_PATH_KEY = "const.telephony.network_path"; 9525c1cde8Sopenharmony_ciconst std::string DEFAULT_PREFERENCES_STRING_VALUE = "default_value"; 9625c1cde8Sopenharmony_ciconst std::string TEMP_SUFFIX = "_temp"; 9725c1cde8Sopenharmony_ci 9825c1cde8Sopenharmony_ciint ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec) 9925c1cde8Sopenharmony_ci{ 10025c1cde8Sopenharmony_ci char buf[MAX_PATH_LEN]; 10125c1cde8Sopenharmony_ci char *path = GetOneCfgFile(PATH, buf, MAX_PATH_LEN); 10225c1cde8Sopenharmony_ci return ParserPdpProfileJson(vec, path); 10325c1cde8Sopenharmony_ci} 10425c1cde8Sopenharmony_ci 10525c1cde8Sopenharmony_ciint ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec, const char *path) 10625c1cde8Sopenharmony_ci{ 10725c1cde8Sopenharmony_ci char *content = nullptr; 10825c1cde8Sopenharmony_ci int ret = DATA_STORAGE_SUCCESS; 10925c1cde8Sopenharmony_ci if (path && *path != '\0') { 11025c1cde8Sopenharmony_ci ret = LoaderJsonFile(content, path); 11125c1cde8Sopenharmony_ci } 11225c1cde8Sopenharmony_ci if (ret != DATA_STORAGE_SUCCESS) { 11325c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson LoaderJsonFile is fail!"); 11425c1cde8Sopenharmony_ci return ret; 11525c1cde8Sopenharmony_ci } 11625c1cde8Sopenharmony_ci if (content == nullptr) { 11725c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::content is nullptr!"); 11825c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 11925c1cde8Sopenharmony_ci } 12025c1cde8Sopenharmony_ci 12125c1cde8Sopenharmony_ci cJSON *root = cJSON_Parse(content); 12225c1cde8Sopenharmony_ci free(content); 12325c1cde8Sopenharmony_ci content = nullptr; 12425c1cde8Sopenharmony_ci if (root == nullptr) { 12525c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson root is error!"); 12625c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 12725c1cde8Sopenharmony_ci } 12825c1cde8Sopenharmony_ci 12925c1cde8Sopenharmony_ci cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_OPERATOR_INFOS); 13025c1cde8Sopenharmony_ci if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) { 13125c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson itemRoots size == 0!"); 13225c1cde8Sopenharmony_ci cJSON_Delete(root); 13325c1cde8Sopenharmony_ci itemRoots = nullptr; 13425c1cde8Sopenharmony_ci root = nullptr; 13525c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL); 13625c1cde8Sopenharmony_ci } 13725c1cde8Sopenharmony_ci ParserPdpProfileInfos(vec, itemRoots); 13825c1cde8Sopenharmony_ci cJSON_Delete(root); 13925c1cde8Sopenharmony_ci itemRoots = nullptr; 14025c1cde8Sopenharmony_ci root = nullptr; 14125c1cde8Sopenharmony_ci return DATA_STORAGE_SUCCESS; 14225c1cde8Sopenharmony_ci} 14325c1cde8Sopenharmony_ci 14425c1cde8Sopenharmony_civoid ParserUtil::ParserPdpProfileInfos(std::vector<PdpProfile> &vec, cJSON *itemRoots) 14525c1cde8Sopenharmony_ci{ 14625c1cde8Sopenharmony_ci cJSON *itemRoot = nullptr; 14725c1cde8Sopenharmony_ci for (int32_t i = 0; i < cJSON_GetArraySize(itemRoots); i++) { 14825c1cde8Sopenharmony_ci itemRoot = cJSON_GetArrayItem(itemRoots, i); 14925c1cde8Sopenharmony_ci if (itemRoot == nullptr || !IsNeedInsertToTable(itemRoot)) { 15025c1cde8Sopenharmony_ci continue; 15125c1cde8Sopenharmony_ci } 15225c1cde8Sopenharmony_ci PdpProfile bean; 15325c1cde8Sopenharmony_ci bean.profileName = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_NAME)); 15425c1cde8Sopenharmony_ci bean.authUser = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_AUTH_USER)); 15525c1cde8Sopenharmony_ci bean.authPwd = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_AUTH_PWD)); 15625c1cde8Sopenharmony_ci std::string authTypeStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_AUTH_TYPE)); 15725c1cde8Sopenharmony_ci bean.authType = authTypeStr.empty() ? 0 : atoi(authTypeStr.c_str()); 15825c1cde8Sopenharmony_ci bean.mcc = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MCC)); 15925c1cde8Sopenharmony_ci bean.mnc = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MNC)); 16025c1cde8Sopenharmony_ci bean.apn = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_APN)); 16125c1cde8Sopenharmony_ci bean.apnTypes = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_APN_TYPES)); 16225c1cde8Sopenharmony_ci bean.mmsIpAddress = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MMS_IP_ADDRESS)); 16325c1cde8Sopenharmony_ci bean.proxyIpAddress = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_IP_ADDRESS)); 16425c1cde8Sopenharmony_ci bean.homeUrl = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_HOME_URL)); 16525c1cde8Sopenharmony_ci bean.mvnoType = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MVNO_TYPE)); 16625c1cde8Sopenharmony_ci bean.mvnoMatchData = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MVNO_MATCH_DATA)); 16725c1cde8Sopenharmony_ci bean.server = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_SERVER)); 16825c1cde8Sopenharmony_ci std::string editedStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_EDITED_STATUS)); 16925c1cde8Sopenharmony_ci bean.edited = editedStr.empty() ? 0 : atoi(editedStr.c_str()); 17025c1cde8Sopenharmony_ci std::string bearingStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_BEARER)); 17125c1cde8Sopenharmony_ci bean.bearingSystemType = bearingStr.empty() ? 0 : atoi(bearingStr.c_str()); 17225c1cde8Sopenharmony_ci std::string isRoamingApnStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_IS_ROAMING_APN)); 17325c1cde8Sopenharmony_ci bean.isRoamingApn = isRoamingApnStr.empty() ? 0 : atoi(isRoamingApnStr.c_str()); 17425c1cde8Sopenharmony_ci std::string pdpProtocolStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_APN_PROTOCOL)); 17525c1cde8Sopenharmony_ci bean.pdpProtocol = pdpProtocolStr.empty() ? "IP" : pdpProtocolStr; 17625c1cde8Sopenharmony_ci std::string roamPdpProtocolStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_ROAMING_PROTOCOL)); 17725c1cde8Sopenharmony_ci bean.roamPdpProtocol = roamPdpProtocolStr.empty() ? "IP" : roamPdpProtocolStr; 17825c1cde8Sopenharmony_ci vec.push_back(bean); 17925c1cde8Sopenharmony_ci } 18025c1cde8Sopenharmony_ci itemRoot = nullptr; 18125c1cde8Sopenharmony_ci} 18225c1cde8Sopenharmony_ci 18325c1cde8Sopenharmony_cistd::string ParserUtil::ParseString(const cJSON *value) 18425c1cde8Sopenharmony_ci{ 18525c1cde8Sopenharmony_ci if (value != nullptr && value->type == cJSON_String && value->valuestring != nullptr) { 18625c1cde8Sopenharmony_ci return value->valuestring; 18725c1cde8Sopenharmony_ci } 18825c1cde8Sopenharmony_ci return ""; 18925c1cde8Sopenharmony_ci} 19025c1cde8Sopenharmony_ci 19125c1cde8Sopenharmony_civoid ParserUtil::ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket &value, const PdpProfile &bean) 19225c1cde8Sopenharmony_ci{ 19325c1cde8Sopenharmony_ci value.PutString(PdpProfileData::PROFILE_NAME, bean.profileName); 19425c1cde8Sopenharmony_ci value.PutString(PdpProfileData::MCC, bean.mcc); 19525c1cde8Sopenharmony_ci value.PutString(PdpProfileData::MNC, bean.mnc); 19625c1cde8Sopenharmony_ci std::string mccmnc(bean.mcc); 19725c1cde8Sopenharmony_ci mccmnc.append(bean.mnc); 19825c1cde8Sopenharmony_ci value.PutString(PdpProfileData::MCCMNC, mccmnc); 19925c1cde8Sopenharmony_ci value.PutString(PdpProfileData::APN, bean.apn); 20025c1cde8Sopenharmony_ci value.PutInt(PdpProfileData::AUTH_TYPE, bean.authType); 20125c1cde8Sopenharmony_ci value.PutString(PdpProfileData::AUTH_USER, bean.authUser); 20225c1cde8Sopenharmony_ci value.PutString(PdpProfileData::AUTH_PWD, bean.authPwd); 20325c1cde8Sopenharmony_ci value.PutString(PdpProfileData::APN_TYPES, bean.apnTypes); 20425c1cde8Sopenharmony_ci value.PutBool(PdpProfileData::IS_ROAMING_APN, bean.isRoamingApn); 20525c1cde8Sopenharmony_ci value.PutString(PdpProfileData::HOME_URL, bean.homeUrl); 20625c1cde8Sopenharmony_ci value.PutString(PdpProfileData::PROXY_IP_ADDRESS, bean.proxyIpAddress); 20725c1cde8Sopenharmony_ci value.PutString(PdpProfileData::MMS_IP_ADDRESS, bean.mmsIpAddress); 20825c1cde8Sopenharmony_ci value.PutString(PdpProfileData::APN_PROTOCOL, bean.pdpProtocol); 20925c1cde8Sopenharmony_ci value.PutString(PdpProfileData::APN_ROAM_PROTOCOL, bean.roamPdpProtocol); 21025c1cde8Sopenharmony_ci value.PutString(PdpProfileData::MVNO_TYPE, bean.mvnoType); 21125c1cde8Sopenharmony_ci value.PutString(PdpProfileData::MVNO_MATCH_DATA, bean.mvnoMatchData); 21225c1cde8Sopenharmony_ci value.PutInt(PdpProfileData::EDITED_STATUS, bean.edited); 21325c1cde8Sopenharmony_ci value.PutString(PdpProfileData::SERVER, bean.server); 21425c1cde8Sopenharmony_ci value.PutInt(PdpProfileData::BEARING_SYSTEM_TYPE, bean.bearingSystemType); 21525c1cde8Sopenharmony_ci} 21625c1cde8Sopenharmony_ci 21725c1cde8Sopenharmony_ciint ParserUtil::GetOpKeyFilePath(std::string &path) 21825c1cde8Sopenharmony_ci{ 21925c1cde8Sopenharmony_ci char buf[MAX_PATH_LEN]; 22025c1cde8Sopenharmony_ci std::string file = GetCustFile(OPKEY_INFO_PATH, CUST_RULE_PATH_KEY); 22125c1cde8Sopenharmony_ci char *ret = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN); 22225c1cde8Sopenharmony_ci if (ret && *ret != '\0') { 22325c1cde8Sopenharmony_ci path = ret; 22425c1cde8Sopenharmony_ci return OPERATION_OK; 22525c1cde8Sopenharmony_ci } 22625c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::GetOpKeyFilePath fail"); 22725c1cde8Sopenharmony_ci return OPERATION_ERROR; 22825c1cde8Sopenharmony_ci} 22925c1cde8Sopenharmony_ci 23025c1cde8Sopenharmony_ciint ParserUtil::ParserOpKeyJson(std::vector<OpKey> &vec, const char *path) 23125c1cde8Sopenharmony_ci{ 23225c1cde8Sopenharmony_ci char *content = nullptr; 23325c1cde8Sopenharmony_ci int ret = LoaderJsonFile(content, path); 23425c1cde8Sopenharmony_ci if (ret != DATA_STORAGE_SUCCESS) { 23525c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyJson LoaderJsonFile is fail!"); 23625c1cde8Sopenharmony_ci return ret; 23725c1cde8Sopenharmony_ci } 23825c1cde8Sopenharmony_ci if (content == nullptr) { 23925c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::content is nullptr!"); 24025c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 24125c1cde8Sopenharmony_ci } 24225c1cde8Sopenharmony_ci 24325c1cde8Sopenharmony_ci cJSON *root = cJSON_Parse(content); 24425c1cde8Sopenharmony_ci free(content); 24525c1cde8Sopenharmony_ci content = nullptr; 24625c1cde8Sopenharmony_ci if (root == nullptr) { 24725c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos root is error!"); 24825c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 24925c1cde8Sopenharmony_ci } 25025c1cde8Sopenharmony_ci cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_OPERATOR_ID); 25125c1cde8Sopenharmony_ci if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) { 25225c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos itemRoots size == 0!"); 25325c1cde8Sopenharmony_ci cJSON_Delete(root); 25425c1cde8Sopenharmony_ci itemRoots = nullptr; 25525c1cde8Sopenharmony_ci root = nullptr; 25625c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL); 25725c1cde8Sopenharmony_ci } 25825c1cde8Sopenharmony_ci ParserOpKeyInfos(vec, itemRoots); 25925c1cde8Sopenharmony_ci cJSON_Delete(root); 26025c1cde8Sopenharmony_ci itemRoots = nullptr; 26125c1cde8Sopenharmony_ci root = nullptr; 26225c1cde8Sopenharmony_ci return DATA_STORAGE_SUCCESS; 26325c1cde8Sopenharmony_ci} 26425c1cde8Sopenharmony_ci 26525c1cde8Sopenharmony_civoid ParserUtil::ParserOpKeyInfos(std::vector<OpKey> &vec, cJSON *itemRoots) 26625c1cde8Sopenharmony_ci{ 26725c1cde8Sopenharmony_ci cJSON *itemRoot = nullptr; 26825c1cde8Sopenharmony_ci cJSON *ruleRoot = nullptr; 26925c1cde8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(itemRoots); i++) { 27025c1cde8Sopenharmony_ci itemRoot = cJSON_GetArrayItem(itemRoots, i); 27125c1cde8Sopenharmony_ci if (itemRoot == nullptr) { 27225c1cde8Sopenharmony_ci continue; 27325c1cde8Sopenharmony_ci } 27425c1cde8Sopenharmony_ci OpKey bean; 27525c1cde8Sopenharmony_ci ruleRoot = cJSON_GetObjectItem(itemRoot, ITEM_RULE); 27625c1cde8Sopenharmony_ci if (ruleRoot != nullptr) { 27725c1cde8Sopenharmony_ci bean.mccmnc = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_MCCMNC)); 27825c1cde8Sopenharmony_ci bean.gid1 = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_GID_ONE)); 27925c1cde8Sopenharmony_ci bean.gid2 = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_GID_TWO)); 28025c1cde8Sopenharmony_ci bean.imsi = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_IMSI)); 28125c1cde8Sopenharmony_ci bean.spn = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_SPN)); 28225c1cde8Sopenharmony_ci bean.iccid = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_ICCID)); 28325c1cde8Sopenharmony_ci } 28425c1cde8Sopenharmony_ci bean.operatorName = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_NAME_OPKEY)); 28525c1cde8Sopenharmony_ci if (bean.operatorName.empty()) { 28625c1cde8Sopenharmony_ci bean.operatorName = "COMMON"; 28725c1cde8Sopenharmony_ci } 28825c1cde8Sopenharmony_ci bean.operatorKey = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_KEY)); 28925c1cde8Sopenharmony_ci bean.operatorKeyExt = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_KEY_EXT)); 29025c1cde8Sopenharmony_ci bean.ruleId = GetRuleId(bean); 29125c1cde8Sopenharmony_ci vec.push_back(bean); 29225c1cde8Sopenharmony_ci } 29325c1cde8Sopenharmony_ci itemRoot = nullptr; 29425c1cde8Sopenharmony_ci ruleRoot = nullptr; 29525c1cde8Sopenharmony_ci} 29625c1cde8Sopenharmony_ci 29725c1cde8Sopenharmony_ciint ParserUtil::GetRuleId(OpKey &bean) 29825c1cde8Sopenharmony_ci{ 29925c1cde8Sopenharmony_ci int ruleId = static_cast<int32_t>(RuleID::RULE_EMPTY); 30025c1cde8Sopenharmony_ci if (!bean.mccmnc.empty()) { 30125c1cde8Sopenharmony_ci ruleId += static_cast<int32_t>(RuleID::RULE_MCCMNC); 30225c1cde8Sopenharmony_ci } 30325c1cde8Sopenharmony_ci if (!bean.iccid.empty()) { 30425c1cde8Sopenharmony_ci ruleId += static_cast<int32_t>(RuleID::RULE_ICCID); 30525c1cde8Sopenharmony_ci } 30625c1cde8Sopenharmony_ci if (!bean.imsi.empty()) { 30725c1cde8Sopenharmony_ci ruleId += static_cast<int32_t>(RuleID::RULE_IMSI); 30825c1cde8Sopenharmony_ci } 30925c1cde8Sopenharmony_ci if (!bean.spn.empty()) { 31025c1cde8Sopenharmony_ci ruleId += static_cast<int32_t>(RuleID::RULE_SPN); 31125c1cde8Sopenharmony_ci } 31225c1cde8Sopenharmony_ci if (!bean.gid1.empty()) { 31325c1cde8Sopenharmony_ci ruleId += static_cast<int32_t>(RuleID::RULE_GID1); 31425c1cde8Sopenharmony_ci } 31525c1cde8Sopenharmony_ci if (!bean.gid2.empty()) { 31625c1cde8Sopenharmony_ci ruleId += static_cast<int32_t>(RuleID::RULE_GID2); 31725c1cde8Sopenharmony_ci } 31825c1cde8Sopenharmony_ci return ruleId; 31925c1cde8Sopenharmony_ci} 32025c1cde8Sopenharmony_ci 32125c1cde8Sopenharmony_civoid ParserUtil::ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket &value, const OpKey &bean) 32225c1cde8Sopenharmony_ci{ 32325c1cde8Sopenharmony_ci value.PutString(OpKeyData::MCCMNC, bean.mccmnc); 32425c1cde8Sopenharmony_ci value.PutString(OpKeyData::GID1, bean.gid1); 32525c1cde8Sopenharmony_ci value.PutString(OpKeyData::GID2, bean.gid2); 32625c1cde8Sopenharmony_ci value.PutString(OpKeyData::IMSI, bean.imsi); 32725c1cde8Sopenharmony_ci value.PutString(OpKeyData::SPN, bean.spn); 32825c1cde8Sopenharmony_ci value.PutString(OpKeyData::ICCID, bean.iccid); 32925c1cde8Sopenharmony_ci value.PutString(OpKeyData::OPERATOR_NAME, bean.operatorName); 33025c1cde8Sopenharmony_ci value.PutString(OpKeyData::OPERATOR_KEY, bean.operatorKey); 33125c1cde8Sopenharmony_ci value.PutString(OpKeyData::OPERATOR_KEY_EXT, bean.operatorKeyExt); 33225c1cde8Sopenharmony_ci value.PutInt(OpKeyData::RULE_ID, bean.ruleId); 33325c1cde8Sopenharmony_ci} 33425c1cde8Sopenharmony_ci 33525c1cde8Sopenharmony_ciint ParserUtil::ParserNumMatchJson(std::vector<NumMatch> &vec, const bool hashCheck) 33625c1cde8Sopenharmony_ci{ 33725c1cde8Sopenharmony_ci char *content = nullptr; 33825c1cde8Sopenharmony_ci char buf[MAX_PATH_LEN]; 33925c1cde8Sopenharmony_ci std::string file = GetCustFile(NUM_MATCH_PATH, CUST_NETWORK_PATH_KEY); 34025c1cde8Sopenharmony_ci char *path = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN); 34125c1cde8Sopenharmony_ci int ret = DATA_STORAGE_SUCCESS; 34225c1cde8Sopenharmony_ci if (path && *path != '\0') { 34325c1cde8Sopenharmony_ci ParserUtil parser; 34425c1cde8Sopenharmony_ci ret = parser.LoaderJsonFile(content, path); 34525c1cde8Sopenharmony_ci } 34625c1cde8Sopenharmony_ci if (ret != DATA_STORAGE_SUCCESS) { 34725c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson LoaderJsonFile is fail!\n"); 34825c1cde8Sopenharmony_ci return ret; 34925c1cde8Sopenharmony_ci } 35025c1cde8Sopenharmony_ci if (content == nullptr) { 35125c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::content is nullptr!"); 35225c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 35325c1cde8Sopenharmony_ci } 35425c1cde8Sopenharmony_ci if (hashCheck && !IsDigestChanged(path, NUM_MATCH_HASH)) { 35525c1cde8Sopenharmony_ci free(content); 35625c1cde8Sopenharmony_ci return FILE_HASH_NO_CHANGE; 35725c1cde8Sopenharmony_ci } 35825c1cde8Sopenharmony_ci cJSON *root = cJSON_Parse(content); 35925c1cde8Sopenharmony_ci free(content); 36025c1cde8Sopenharmony_ci content = nullptr; 36125c1cde8Sopenharmony_ci if (root == nullptr) { 36225c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson root is error!\n"); 36325c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 36425c1cde8Sopenharmony_ci } 36525c1cde8Sopenharmony_ci cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_NUM_MATCH_INFOS); 36625c1cde8Sopenharmony_ci if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) { 36725c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson itemRoots size == 0!\n"); 36825c1cde8Sopenharmony_ci cJSON_Delete(root); 36925c1cde8Sopenharmony_ci itemRoots = nullptr; 37025c1cde8Sopenharmony_ci root = nullptr; 37125c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL); 37225c1cde8Sopenharmony_ci } 37325c1cde8Sopenharmony_ci ParserNumMatchInfos(vec, itemRoots); 37425c1cde8Sopenharmony_ci cJSON_Delete(root); 37525c1cde8Sopenharmony_ci itemRoots = nullptr; 37625c1cde8Sopenharmony_ci root = nullptr; 37725c1cde8Sopenharmony_ci return DATA_STORAGE_SUCCESS; 37825c1cde8Sopenharmony_ci} 37925c1cde8Sopenharmony_ci 38025c1cde8Sopenharmony_civoid ParserUtil::ParserNumMatchInfos(std::vector<NumMatch> &vec, cJSON *itemRoots) 38125c1cde8Sopenharmony_ci{ 38225c1cde8Sopenharmony_ci cJSON *itemRoot = nullptr; 38325c1cde8Sopenharmony_ci for (int32_t i = 0; i < cJSON_GetArraySize(itemRoots); i++) { 38425c1cde8Sopenharmony_ci itemRoot = cJSON_GetArrayItem(itemRoots, i); 38525c1cde8Sopenharmony_ci if (itemRoot == nullptr) { 38625c1cde8Sopenharmony_ci continue; 38725c1cde8Sopenharmony_ci } 38825c1cde8Sopenharmony_ci NumMatch bean; 38925c1cde8Sopenharmony_ci bean.name = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_NAME)); 39025c1cde8Sopenharmony_ci bean.mcc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MCC)); 39125c1cde8Sopenharmony_ci bean.mnc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MNC)); 39225c1cde8Sopenharmony_ci bean.numMatch = ParseInt(cJSON_GetObjectItem(itemRoot, ITEM_NUM_MATCH)); 39325c1cde8Sopenharmony_ci bean.numMatchShort = ParseInt(cJSON_GetObjectItem(itemRoot, ITEM_NUM_MATCH_SHORT)); 39425c1cde8Sopenharmony_ci vec.push_back(bean); 39525c1cde8Sopenharmony_ci } 39625c1cde8Sopenharmony_ci itemRoot = nullptr; 39725c1cde8Sopenharmony_ci} 39825c1cde8Sopenharmony_ci 39925c1cde8Sopenharmony_cistd::string ParserUtil::ParseAsString(const cJSON *value) 40025c1cde8Sopenharmony_ci{ 40125c1cde8Sopenharmony_ci if (value != nullptr && value->type == cJSON_String && value->valuestring != nullptr) { 40225c1cde8Sopenharmony_ci return value->valuestring; 40325c1cde8Sopenharmony_ci } else if (value != nullptr && value->type == cJSON_Number) { 40425c1cde8Sopenharmony_ci return std::to_string(static_cast<int64_t>(cJSON_GetNumberValue(value))); 40525c1cde8Sopenharmony_ci } 40625c1cde8Sopenharmony_ci return ""; 40725c1cde8Sopenharmony_ci} 40825c1cde8Sopenharmony_ci 40925c1cde8Sopenharmony_ciint32_t ParserUtil::ParseInt(const cJSON *value) 41025c1cde8Sopenharmony_ci{ 41125c1cde8Sopenharmony_ci if (value != nullptr && value->type == cJSON_Number) { 41225c1cde8Sopenharmony_ci return value->valueint; 41325c1cde8Sopenharmony_ci } 41425c1cde8Sopenharmony_ci return 0; 41525c1cde8Sopenharmony_ci} 41625c1cde8Sopenharmony_ci 41725c1cde8Sopenharmony_civoid ParserUtil::ParserNumMatchToValuesBucket(NativeRdb::ValuesBucket &value, const NumMatch &bean) 41825c1cde8Sopenharmony_ci{ 41925c1cde8Sopenharmony_ci value.PutString(NumMatchData::NAME, bean.name); 42025c1cde8Sopenharmony_ci value.PutString(NumMatchData::MCC, bean.mcc); 42125c1cde8Sopenharmony_ci value.PutString(NumMatchData::MNC, bean.mnc); 42225c1cde8Sopenharmony_ci std::string mccmnc(bean.mcc); 42325c1cde8Sopenharmony_ci mccmnc.append(bean.mnc); 42425c1cde8Sopenharmony_ci value.PutString(NumMatchData::MCCMNC, mccmnc); 42525c1cde8Sopenharmony_ci value.PutInt(NumMatchData::NUM_MATCH, bean.numMatch); 42625c1cde8Sopenharmony_ci value.PutInt(NumMatchData::NUM_MATCH_SHORT, bean.numMatchShort); 42725c1cde8Sopenharmony_ci} 42825c1cde8Sopenharmony_ci 42925c1cde8Sopenharmony_ciint ParserUtil::ParserEccDataJson(std::vector<EccNum> &vec, const bool hashCheck) 43025c1cde8Sopenharmony_ci{ 43125c1cde8Sopenharmony_ci char *content = nullptr; 43225c1cde8Sopenharmony_ci char buf[MAX_PATH_LEN]; 43325c1cde8Sopenharmony_ci std::string file = GetCustFile(ECC_DATA_PATH, CUST_NETWORK_PATH_KEY); 43425c1cde8Sopenharmony_ci char *path = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN); 43525c1cde8Sopenharmony_ci int ret = DATA_STORAGE_SUCCESS; 43625c1cde8Sopenharmony_ci if (path && *path != '\0') { 43725c1cde8Sopenharmony_ci ret = LoaderJsonFile(content, path); 43825c1cde8Sopenharmony_ci } 43925c1cde8Sopenharmony_ci if (ret != DATA_STORAGE_SUCCESS) { 44025c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson LoaderJsonFile is fail!"); 44125c1cde8Sopenharmony_ci return ret; 44225c1cde8Sopenharmony_ci } 44325c1cde8Sopenharmony_ci if (content == nullptr) { 44425c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::content is nullptr!"); 44525c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 44625c1cde8Sopenharmony_ci } 44725c1cde8Sopenharmony_ci if (hashCheck && !IsDigestChanged(path, ECC_DATA_HASH)) { 44825c1cde8Sopenharmony_ci free(content); 44925c1cde8Sopenharmony_ci return FILE_HASH_NO_CHANGE; 45025c1cde8Sopenharmony_ci } 45125c1cde8Sopenharmony_ci cJSON *root = cJSON_Parse(content); 45225c1cde8Sopenharmony_ci free(content); 45325c1cde8Sopenharmony_ci content = nullptr; 45425c1cde8Sopenharmony_ci if (root == nullptr) { 45525c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson root is error!"); 45625c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR); 45725c1cde8Sopenharmony_ci } 45825c1cde8Sopenharmony_ci 45925c1cde8Sopenharmony_ci cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_OPERATOR_INFOS); 46025c1cde8Sopenharmony_ci if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) { 46125c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson itemRoots size == 0!"); 46225c1cde8Sopenharmony_ci cJSON_Delete(root); 46325c1cde8Sopenharmony_ci itemRoots = nullptr; 46425c1cde8Sopenharmony_ci root = nullptr; 46525c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL); 46625c1cde8Sopenharmony_ci } 46725c1cde8Sopenharmony_ci ParserEccDataInfos(vec, itemRoots); 46825c1cde8Sopenharmony_ci cJSON_Delete(root); 46925c1cde8Sopenharmony_ci itemRoots = nullptr; 47025c1cde8Sopenharmony_ci root = nullptr; 47125c1cde8Sopenharmony_ci return DATA_STORAGE_SUCCESS; 47225c1cde8Sopenharmony_ci} 47325c1cde8Sopenharmony_ci 47425c1cde8Sopenharmony_civoid ParserUtil::ParserEccDataInfos(std::vector<EccNum> &vec, cJSON *itemRoots) 47525c1cde8Sopenharmony_ci{ 47625c1cde8Sopenharmony_ci cJSON *itemRoot = nullptr; 47725c1cde8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(itemRoots); i++) { 47825c1cde8Sopenharmony_ci itemRoot = cJSON_GetArrayItem(itemRoots, i); 47925c1cde8Sopenharmony_ci if (itemRoot == nullptr) { 48025c1cde8Sopenharmony_ci continue; 48125c1cde8Sopenharmony_ci } 48225c1cde8Sopenharmony_ci EccNum bean; 48325c1cde8Sopenharmony_ci bean.name = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_NAME)); 48425c1cde8Sopenharmony_ci bean.mcc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MCC)); 48525c1cde8Sopenharmony_ci bean.mnc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MNC)); 48625c1cde8Sopenharmony_ci bean.numeric = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_NUMERIC)); 48725c1cde8Sopenharmony_ci bean.ecc_withcard = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_ECC_WITH_CARD)); 48825c1cde8Sopenharmony_ci bean.ecc_nocard = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_ECC_NO_CARD)); 48925c1cde8Sopenharmony_ci bean.ecc_fake = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_ECC_FAKE)); 49025c1cde8Sopenharmony_ci vec.push_back(bean); 49125c1cde8Sopenharmony_ci } 49225c1cde8Sopenharmony_ci itemRoot = nullptr; 49325c1cde8Sopenharmony_ci} 49425c1cde8Sopenharmony_ci 49525c1cde8Sopenharmony_civoid ParserUtil::ParserEccDataToValuesBucket(NativeRdb::ValuesBucket &value, const EccNum &bean) 49625c1cde8Sopenharmony_ci{ 49725c1cde8Sopenharmony_ci value.PutString(EccData::NAME, bean.name); 49825c1cde8Sopenharmony_ci value.PutString(EccData::MCC, bean.mcc); 49925c1cde8Sopenharmony_ci value.PutString(EccData::MNC, bean.mnc); 50025c1cde8Sopenharmony_ci value.PutString(EccData::NUMERIC, bean.numeric); 50125c1cde8Sopenharmony_ci value.PutString(EccData::ECC_WITH_CARD, bean.ecc_withcard); 50225c1cde8Sopenharmony_ci value.PutString(EccData::ECC_NO_CARD, bean.ecc_nocard); 50325c1cde8Sopenharmony_ci value.PutString(EccData::ECC_FAKE, bean.ecc_fake); 50425c1cde8Sopenharmony_ci} 50525c1cde8Sopenharmony_ci 50625c1cde8Sopenharmony_ciint ParserUtil::LoaderJsonFile(char *&content, const char *path) const 50725c1cde8Sopenharmony_ci{ 50825c1cde8Sopenharmony_ci long len = 0; 50925c1cde8Sopenharmony_ci char realPath[PATH_MAX] = { 0x00 }; 51025c1cde8Sopenharmony_ci if (realpath(path, realPath) == nullptr) { 51125c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile realpath fail! #PATH: %{public}s", path); 51225c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::REALPATH_FAIL); 51325c1cde8Sopenharmony_ci } 51425c1cde8Sopenharmony_ci FILE *f = fopen(realPath, "rb"); 51525c1cde8Sopenharmony_ci if (f == nullptr) { 51625c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile file is null!"); 51725c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::OPEN_FILE_ERROR); 51825c1cde8Sopenharmony_ci } 51925c1cde8Sopenharmony_ci int ret_seek_end = fseek(f, 0, SEEK_END); 52025c1cde8Sopenharmony_ci if (ret_seek_end != 0) { 52125c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_end != 0!"); 52225c1cde8Sopenharmony_ci CloseFile(f); 52325c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR); 52425c1cde8Sopenharmony_ci } 52525c1cde8Sopenharmony_ci len = ftell(f); 52625c1cde8Sopenharmony_ci int ret_seek_set = fseek(f, 0, SEEK_SET); 52725c1cde8Sopenharmony_ci if (ret_seek_set != 0) { 52825c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_set != 0!"); 52925c1cde8Sopenharmony_ci CloseFile(f); 53025c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR); 53125c1cde8Sopenharmony_ci } 53225c1cde8Sopenharmony_ci if (len == 0 || len > static_cast<long>(MAX_BYTE_LEN)) { 53325c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile len <= 0 or len > LONG_MAX!"); 53425c1cde8Sopenharmony_ci CloseFile(f); 53525c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR); 53625c1cde8Sopenharmony_ci } 53725c1cde8Sopenharmony_ci content = static_cast<char *>(malloc(len + 1)); 53825c1cde8Sopenharmony_ci if (content == nullptr) { 53925c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile malloc content fail!"); 54025c1cde8Sopenharmony_ci CloseFile(f); 54125c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR); 54225c1cde8Sopenharmony_ci } 54325c1cde8Sopenharmony_ci if (memset_s(content, len + 1, 0, len + 1) != EOK) { 54425c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile memset_s failed"); 54525c1cde8Sopenharmony_ci free(content); 54625c1cde8Sopenharmony_ci content = nullptr; 54725c1cde8Sopenharmony_ci CloseFile(f); 54825c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR); 54925c1cde8Sopenharmony_ci } 55025c1cde8Sopenharmony_ci size_t ret_read = fread(content, 1, len, f); 55125c1cde8Sopenharmony_ci if (ret_read != static_cast<size_t>(len)) { 55225c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_read != len!"); 55325c1cde8Sopenharmony_ci free(content); 55425c1cde8Sopenharmony_ci content = nullptr; 55525c1cde8Sopenharmony_ci CloseFile(f); 55625c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR); 55725c1cde8Sopenharmony_ci } 55825c1cde8Sopenharmony_ci return CloseFile(f); 55925c1cde8Sopenharmony_ci} 56025c1cde8Sopenharmony_ci 56125c1cde8Sopenharmony_ciint ParserUtil::CloseFile(FILE *f) const 56225c1cde8Sopenharmony_ci{ 56325c1cde8Sopenharmony_ci int ret_close = fclose(f); 56425c1cde8Sopenharmony_ci if (ret_close != 0) { 56525c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_close != 0!"); 56625c1cde8Sopenharmony_ci return static_cast<int>(LoadProFileErrorType::CLOSE_FILE_ERROR); 56725c1cde8Sopenharmony_ci } 56825c1cde8Sopenharmony_ci return DATA_STORAGE_SUCCESS; 56925c1cde8Sopenharmony_ci} 57025c1cde8Sopenharmony_ci 57125c1cde8Sopenharmony_cistd::string ParserUtil::GetCustFile(const char *&file, const char *key) 57225c1cde8Sopenharmony_ci{ 57325c1cde8Sopenharmony_ci std::string custFile = system::GetParameter(key, ""); 57425c1cde8Sopenharmony_ci if (!custFile.empty()) { 57525c1cde8Sopenharmony_ci custFile.append(file); 57625c1cde8Sopenharmony_ci } else { 57725c1cde8Sopenharmony_ci custFile = file; 57825c1cde8Sopenharmony_ci } 57925c1cde8Sopenharmony_ci return custFile; 58025c1cde8Sopenharmony_ci} 58125c1cde8Sopenharmony_ci 58225c1cde8Sopenharmony_ciint ParserUtil::GetPdpProfilePath(int slotId, std::string &path) 58325c1cde8Sopenharmony_ci{ 58425c1cde8Sopenharmony_ci int mode = MODE_SLOT_0; 58525c1cde8Sopenharmony_ci if (slotId == SimSlotId::SIM_SLOT_1) { 58625c1cde8Sopenharmony_ci mode = MODE_SLOT_1; 58725c1cde8Sopenharmony_ci } 58825c1cde8Sopenharmony_ci char buf[MAX_PATH_LEN]; 58925c1cde8Sopenharmony_ci char *ret = GetOneCfgFileEx(PATH, buf, MAX_PATH_LEN, mode, nullptr); 59025c1cde8Sopenharmony_ci if (ret && *ret != '\0') { 59125c1cde8Sopenharmony_ci path = ret; 59225c1cde8Sopenharmony_ci return OPERATION_OK; 59325c1cde8Sopenharmony_ci } 59425c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::GetPdpProfilePath fail"); 59525c1cde8Sopenharmony_ci return OPERATION_ERROR; 59625c1cde8Sopenharmony_ci} 59725c1cde8Sopenharmony_ci 59825c1cde8Sopenharmony_ciint ParserUtil::GetFileChecksum(const char *path, std::string &checkSum) 59925c1cde8Sopenharmony_ci{ 60025c1cde8Sopenharmony_ci char realPath[PATH_MAX] = {0x00}; 60125c1cde8Sopenharmony_ci if (realpath(path, realPath) == nullptr) { 60225c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::GetFileChecksum Failed to get realPath!"); 60325c1cde8Sopenharmony_ci return OPERATION_ERROR; 60425c1cde8Sopenharmony_ci } 60525c1cde8Sopenharmony_ci std::ifstream file(realPath, std::ios::binary); 60625c1cde8Sopenharmony_ci if (!file.is_open()) { 60725c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::GetFileChecksum Failed to open file!"); 60825c1cde8Sopenharmony_ci return OPERATION_ERROR; 60925c1cde8Sopenharmony_ci } 61025c1cde8Sopenharmony_ci std::vector<char> buffer(BYTE_LEN); 61125c1cde8Sopenharmony_ci uint32_t crc32 = crc32_z(0L, Z_NULL, 0); 61225c1cde8Sopenharmony_ci while (file) { 61325c1cde8Sopenharmony_ci file.read(buffer.data(), buffer.size()); 61425c1cde8Sopenharmony_ci auto bytesRead = file.gcount(); 61525c1cde8Sopenharmony_ci if (bytesRead > 0) { 61625c1cde8Sopenharmony_ci crc32 = crc32_z(crc32, reinterpret_cast<const Bytef *>(buffer.data()), static_cast<uInt>(bytesRead)); 61725c1cde8Sopenharmony_ci } 61825c1cde8Sopenharmony_ci } 61925c1cde8Sopenharmony_ci checkSum = std::to_string(crc32); 62025c1cde8Sopenharmony_ci return OPERATION_OK; 62125c1cde8Sopenharmony_ci} 62225c1cde8Sopenharmony_ci 62325c1cde8Sopenharmony_cibool ParserUtil::IsNeedInsertToTable(cJSON *value) 62425c1cde8Sopenharmony_ci{ 62525c1cde8Sopenharmony_ci if (value == nullptr || cJSON_GetObjectItem(value, ITEM_APN) == nullptr) { 62625c1cde8Sopenharmony_ci return false; 62725c1cde8Sopenharmony_ci } 62825c1cde8Sopenharmony_ci char *tempChar = cJSON_PrintUnformatted(value); 62925c1cde8Sopenharmony_ci if (tempChar == nullptr) { 63025c1cde8Sopenharmony_ci return false; 63125c1cde8Sopenharmony_ci } 63225c1cde8Sopenharmony_ci std::string res = tempChar; 63325c1cde8Sopenharmony_ci free(tempChar); 63425c1cde8Sopenharmony_ci tempChar = nullptr; 63525c1cde8Sopenharmony_ci return DelayedRefSingleton<CoreServiceClient>::GetInstance().IsAllowedInsertApn(res); 63625c1cde8Sopenharmony_ci} 63725c1cde8Sopenharmony_ci 63825c1cde8Sopenharmony_cibool ParserUtil::IsDigestChanged(const char *path, const std::string &key) 63925c1cde8Sopenharmony_ci{ 64025c1cde8Sopenharmony_ci std::string newHash; 64125c1cde8Sopenharmony_ci ParserUtil util; 64225c1cde8Sopenharmony_ci util.GetFileChecksum(path, newHash); 64325c1cde8Sopenharmony_ci auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance(); 64425c1cde8Sopenharmony_ci if (preferencesUtil == nullptr) { 64525c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::IsDigestChanged preferencesUtil is nullptr!"); 64625c1cde8Sopenharmony_ci return true; 64725c1cde8Sopenharmony_ci } 64825c1cde8Sopenharmony_ci std::string oldHash = preferencesUtil->ObtainString(key, DEFAULT_PREFERENCES_STRING_VALUE); 64925c1cde8Sopenharmony_ci if (oldHash.compare(newHash) == 0) { 65025c1cde8Sopenharmony_ci DATA_STORAGE_LOGD("ParserUtil::IsDigestChanged file not changed"); 65125c1cde8Sopenharmony_ci return false; 65225c1cde8Sopenharmony_ci } 65325c1cde8Sopenharmony_ci DATA_STORAGE_LOGI("ParserUtil::IsDigestChanged file is changed"); 65425c1cde8Sopenharmony_ci preferencesUtil->SaveString(key + TEMP_SUFFIX, newHash); 65525c1cde8Sopenharmony_ci return true; 65625c1cde8Sopenharmony_ci} 65725c1cde8Sopenharmony_ci 65825c1cde8Sopenharmony_civoid ParserUtil::RefreshDigest(const std::string &key) 65925c1cde8Sopenharmony_ci{ 66025c1cde8Sopenharmony_ci auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance(); 66125c1cde8Sopenharmony_ci if (preferencesUtil == nullptr) { 66225c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::RefreshDigest preferencesUtil is nullptr!"); 66325c1cde8Sopenharmony_ci return; 66425c1cde8Sopenharmony_ci } 66525c1cde8Sopenharmony_ci std::string tempHash = preferencesUtil->ObtainString(key + TEMP_SUFFIX, DEFAULT_PREFERENCES_STRING_VALUE); 66625c1cde8Sopenharmony_ci if (tempHash != DEFAULT_PREFERENCES_STRING_VALUE) { 66725c1cde8Sopenharmony_ci preferencesUtil->SaveString(key, tempHash); 66825c1cde8Sopenharmony_ci preferencesUtil->RemoveKey(key + TEMP_SUFFIX); 66925c1cde8Sopenharmony_ci preferencesUtil->Refresh(); 67025c1cde8Sopenharmony_ci } 67125c1cde8Sopenharmony_ci} 67225c1cde8Sopenharmony_ci 67325c1cde8Sopenharmony_civoid ParserUtil::ClearTempDigest(const std::string &key) 67425c1cde8Sopenharmony_ci{ 67525c1cde8Sopenharmony_ci auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance(); 67625c1cde8Sopenharmony_ci if (preferencesUtil == nullptr) { 67725c1cde8Sopenharmony_ci DATA_STORAGE_LOGE("ParserUtil::ClearTempDigest preferencesUtil is nullptr!"); 67825c1cde8Sopenharmony_ci return; 67925c1cde8Sopenharmony_ci } 68025c1cde8Sopenharmony_ci preferencesUtil->RemoveKey(key + TEMP_SUFFIX); 68125c1cde8Sopenharmony_ci preferencesUtil->Refresh(); 68225c1cde8Sopenharmony_ci} 68325c1cde8Sopenharmony_ci} // namespace Telephony 68425c1cde8Sopenharmony_ci} // namespace OHOS 685