• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "parser_util.h"
17 
18 #include <cstdio>
19 #include <securec.h>
20 #include <unistd.h>
21 
22 #include <fstream>
23 #include <iostream>
24 #include <zlib.h>
25 #include "climits"
26 #include "config_policy_utils.h"
27 #include "cstdint"
28 #include "cstdio"
29 #include "cstdlib"
30 #include "cstring"
31 #include "core_service_client.h"
32 #include "data_storage_errors.h"
33 #include "data_storage_log_wrapper.h"
34 #include "global_params_data.h"
35 #include "memory"
36 #include "new"
37 #include "opkey_data.h"
38 #include "parameters.h"
39 #include "pdp_profile_data.h"
40 #include "telephony_types.h"
41 #include "values_bucket.h"
42 #include "vector"
43 #include "preferences_util.h"
44 
45 namespace OHOS {
46 namespace Telephony {
47 const char *PATH = "/etc/telephony/pdp_profile.json";
48 const char *ITEM_OPERATOR_INFOS = "operator_infos";
49 const char *ITEM_OPERATOR_NAME = "operator_name";
50 const char *ITEM_AUTH_USER = "auth_user";
51 const char *ITEM_AUTH_PWD = "auth_pwd";
52 const char *ITEM_AUTH_TYPE = "auth_type";
53 const char *ITEM_MCC = "mcc";
54 const char *ITEM_MNC = "mnc";
55 const char *ITEM_APN = "apn";
56 const char *ITEM_APN_TYPES = "apn_types";
57 const char *ITEM_IP_ADDRESS = "ip_addr";
58 const char *ITEM_MMS_IP_ADDRESS = "mms_ip_addr";
59 const char *ITEM_HOME_URL = "home_url";
60 const char *ITEM_MVNO_TYPE = "mvno_type";
61 const char *ITEM_MVNO_MATCH_DATA = "mvno_match_data";
62 const char *ITEM_EDITED_STATUS = "edited";
63 const char *ITEM_SERVER = "server";
64 const char *ITEM_BEARER = "bearing_system_type";
65 const char *ITEM_IS_ROAMING_APN = "is_roaming_apn";
66 const char *ITEM_APN_PROTOCOL = "apn_protocol";
67 const char *ITEM_ROAMING_PROTOCOL = "apn_roam_protocol";
68 const char *APN_VERSION = "apn_version";
69 const char *OPKEY_INFO_PATH = "etc/telephony/OpkeyInfo.json";
70 const char *ITEM_OPERATOR_ID = "operator_id";
71 const char *ITEM_RULE = "rule";
72 const char *ITEM_MCCMNC = "mcc_mnc";
73 const char *ITEM_GID_ONE = "gid1";
74 const char *ITEM_GID_TWO = "gid2";
75 const char *ITEM_IMSI = "imsi";
76 const char *ITEM_SPN = "spn";
77 const char *ITEM_ICCID = "iccid";
78 const char *ITEM_OPERATOR_NAME_OPKEY = "operator_name";
79 const char *ITEM_OPERATOR_KEY = "operator_key";
80 const char *ITEM_OPERATOR_KEY_EXT = "operator_key_ext";
81 const char *NUM_MATCH_PATH = "etc/telephony/number_match.json";
82 const char *ECC_DATA_PATH = "etc/telephony/ecc_data.json";
83 const char *ITEM_NUM_MATCH_INFOS = "numMatchs";
84 const char *ITEM_NAME = "name";
85 const char *ITEM_NUM_MATCH = "num_match";
86 const char *ITEM_NUM_MATCH_SHORT = "num_match_short";
87 const char *ITEM_NUMERIC = "numeric";
88 const char *ITEM_ECC_WITH_CARD = "ecc_withcard";
89 const char *ITEM_ECC_NO_CARD = "ecc_nocard";
90 const char *ITEM_ECC_FAKE = "ecc_fake";
91 const int BYTE_LEN = 1024 * 1024;
92 const int MAX_BYTE_LEN = 10 * 1024 * 1024;
93 static constexpr const char *CUST_RULE_PATH_KEY = "const.telephony.rule_path";
94 static constexpr const char *CUST_NETWORK_PATH_KEY = "const.telephony.network_path";
95 const std::string DEFAULT_PREFERENCES_STRING_VALUE = "default_value";
96 const std::string TEMP_SUFFIX = "_temp";
97 
ParserPdpProfileJson(std::vector<PdpProfile> & vec)98 int ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec)
99 {
100     char buf[MAX_PATH_LEN];
101     char *path = GetOneCfgFile(PATH, buf, MAX_PATH_LEN);
102     return ParserPdpProfileJson(vec, path);
103 }
104 
ParserPdpProfileJson(std::vector<PdpProfile> & vec,const char * path)105 int ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec, const char *path)
106 {
107     char *content = nullptr;
108     int ret = DATA_STORAGE_SUCCESS;
109     if (path && *path != '\0') {
110         ret = LoaderJsonFile(content, path);
111     }
112     if (ret != DATA_STORAGE_SUCCESS) {
113         DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson LoaderJsonFile is fail!");
114         return ret;
115     }
116     if (content == nullptr) {
117         DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
118         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
119     }
120 
121     cJSON *root = cJSON_Parse(content);
122     free(content);
123     content = nullptr;
124     if (root == nullptr) {
125         DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson root is error!");
126         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
127     }
128 
129     cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_OPERATOR_INFOS);
130     if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) {
131         DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson itemRoots size == 0!");
132         cJSON_Delete(root);
133         itemRoots = nullptr;
134         root = nullptr;
135         return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
136     }
137     ParserPdpProfileInfos(vec, itemRoots);
138     cJSON_Delete(root);
139     itemRoots = nullptr;
140     root = nullptr;
141     return DATA_STORAGE_SUCCESS;
142 }
143 
ParserPdpProfileInfos(std::vector<PdpProfile> & vec,cJSON * itemRoots)144 void ParserUtil::ParserPdpProfileInfos(std::vector<PdpProfile> &vec, cJSON *itemRoots)
145 {
146     cJSON *itemRoot = nullptr;
147     for (int32_t i = 0; i < cJSON_GetArraySize(itemRoots); i++) {
148         itemRoot = cJSON_GetArrayItem(itemRoots, i);
149         if (itemRoot == nullptr || !IsNeedInsertToTable(itemRoot)) {
150             continue;
151         }
152         PdpProfile bean;
153         bean.profileName = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_NAME));
154         bean.authUser = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_AUTH_USER));
155         bean.authPwd = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_AUTH_PWD));
156         std::string authTypeStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_AUTH_TYPE));
157         int authType = authTypeStr.empty() ? static_cast<int>(ApnAuthType::INIT) : atoi(authTypeStr.c_str());
158         if (authType == static_cast<int>(ApnAuthType::INIT)) {
159             authType = bean.authUser.empty() ?
160                 static_cast<int>(ApnAuthType::NONE) : static_cast<int>(ApnAuthType::PAP_OR_CHAP);
161         }
162         bean.authType = authType;
163         bean.mcc = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MCC));
164         bean.mnc = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MNC));
165         bean.apn = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_APN));
166         bean.apnTypes = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_APN_TYPES));
167         bean.mmsIpAddress = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MMS_IP_ADDRESS));
168         bean.proxyIpAddress = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_IP_ADDRESS));
169         bean.homeUrl = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_HOME_URL));
170         bean.mvnoType = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MVNO_TYPE));
171         bean.mvnoMatchData = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_MVNO_MATCH_DATA));
172         bean.server = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_SERVER));
173         std::string editedStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_EDITED_STATUS));
174         bean.edited = editedStr.empty() ? 0 : atoi(editedStr.c_str());
175         std::string bearingStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_BEARER));
176         bean.bearingSystemType = bearingStr.empty() ? 0 : atoi(bearingStr.c_str());
177         std::string isRoamingApnStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_IS_ROAMING_APN));
178         bean.isRoamingApn = isRoamingApnStr.empty() ? 0 : atoi(isRoamingApnStr.c_str());
179         std::string pdpProtocolStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_APN_PROTOCOL));
180         bean.pdpProtocol = pdpProtocolStr.empty() ? "IP" : pdpProtocolStr;
181         std::string roamPdpProtocolStr = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_ROAMING_PROTOCOL));
182         bean.roamPdpProtocol = roamPdpProtocolStr.empty() ? "IP" : roamPdpProtocolStr;
183         vec.push_back(bean);
184     }
185     itemRoot = nullptr;
186 }
187 
ParseString(const cJSON * value)188 std::string ParserUtil::ParseString(const cJSON *value)
189 {
190     if (value != nullptr && value->type == cJSON_String && value->valuestring != nullptr) {
191         return value->valuestring;
192     }
193     return "";
194 }
195 
ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket & value,const PdpProfile & bean)196 void ParserUtil::ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket &value, const PdpProfile &bean)
197 {
198     value.PutString(PdpProfileData::PROFILE_NAME, bean.profileName);
199     value.PutString(PdpProfileData::MCC, bean.mcc);
200     value.PutString(PdpProfileData::MNC, bean.mnc);
201     std::string mccmnc(bean.mcc);
202     mccmnc.append(bean.mnc);
203     value.PutString(PdpProfileData::MCCMNC, mccmnc);
204     value.PutString(PdpProfileData::APN, bean.apn);
205     value.PutInt(PdpProfileData::AUTH_TYPE, bean.authType);
206     value.PutString(PdpProfileData::AUTH_USER, bean.authUser);
207     value.PutString(PdpProfileData::AUTH_PWD, bean.authPwd);
208     value.PutString(PdpProfileData::APN_TYPES, bean.apnTypes);
209     value.PutBool(PdpProfileData::IS_ROAMING_APN, bean.isRoamingApn);
210     value.PutString(PdpProfileData::HOME_URL, bean.homeUrl);
211     value.PutString(PdpProfileData::PROXY_IP_ADDRESS, bean.proxyIpAddress);
212     value.PutString(PdpProfileData::MMS_IP_ADDRESS, bean.mmsIpAddress);
213     value.PutString(PdpProfileData::APN_PROTOCOL, bean.pdpProtocol);
214     value.PutString(PdpProfileData::APN_ROAM_PROTOCOL, bean.roamPdpProtocol);
215     value.PutString(PdpProfileData::MVNO_TYPE, bean.mvnoType);
216     value.PutString(PdpProfileData::MVNO_MATCH_DATA, bean.mvnoMatchData);
217     value.PutInt(PdpProfileData::EDITED_STATUS, bean.edited);
218     value.PutString(PdpProfileData::SERVER, bean.server);
219     value.PutInt(PdpProfileData::BEARING_SYSTEM_TYPE, bean.bearingSystemType);
220 }
221 
GetOpKeyFilePath(std::string & path)222 int ParserUtil::GetOpKeyFilePath(std::string &path)
223 {
224     char buf[MAX_PATH_LEN];
225     std::string file = GetCustFile(OPKEY_INFO_PATH, CUST_RULE_PATH_KEY);
226     char *ret = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN);
227     if (ret && *ret != '\0') {
228         path = ret;
229         return OPERATION_OK;
230     }
231     DATA_STORAGE_LOGE("ParserUtil::GetOpKeyFilePath fail");
232     return OPERATION_ERROR;
233 }
234 
ParserOpKeyJson(std::vector<OpKey> & vec,const char * path)235 int ParserUtil::ParserOpKeyJson(std::vector<OpKey> &vec, const char *path)
236 {
237     char *content = nullptr;
238     int ret = LoaderJsonFile(content, path);
239     if (ret != DATA_STORAGE_SUCCESS) {
240         DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyJson LoaderJsonFile is fail!");
241         return ret;
242     }
243     if (content == nullptr) {
244         DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
245         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
246     }
247 
248     cJSON *root = cJSON_Parse(content);
249     free(content);
250     content = nullptr;
251     if (root == nullptr) {
252         DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos root is error!");
253         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
254     }
255     cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_OPERATOR_ID);
256     if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) {
257         DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos itemRoots size == 0!");
258         cJSON_Delete(root);
259         itemRoots = nullptr;
260         root = nullptr;
261         return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
262     }
263     ParserOpKeyInfos(vec, itemRoots);
264     cJSON_Delete(root);
265     itemRoots = nullptr;
266     root = nullptr;
267     return DATA_STORAGE_SUCCESS;
268 }
269 
ParserOpKeyInfos(std::vector<OpKey> & vec,cJSON * itemRoots)270 void ParserUtil::ParserOpKeyInfos(std::vector<OpKey> &vec, cJSON *itemRoots)
271 {
272     cJSON *itemRoot = nullptr;
273     cJSON *ruleRoot = nullptr;
274     for (int i = 0; i < cJSON_GetArraySize(itemRoots); i++) {
275         itemRoot = cJSON_GetArrayItem(itemRoots, i);
276         if (itemRoot == nullptr) {
277             continue;
278         }
279         OpKey bean;
280         ruleRoot = cJSON_GetObjectItem(itemRoot, ITEM_RULE);
281         if (ruleRoot != nullptr) {
282             bean.mccmnc = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_MCCMNC));
283             bean.gid1 = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_GID_ONE));
284             bean.gid2 = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_GID_TWO));
285             bean.imsi = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_IMSI));
286             bean.spn = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_SPN));
287             bean.iccid = ParseString(cJSON_GetObjectItem(ruleRoot, ITEM_ICCID));
288         }
289         bean.operatorName = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_NAME_OPKEY));
290         if (bean.operatorName.empty()) {
291             bean.operatorName = "COMMON";
292         }
293         bean.operatorKey = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_KEY));
294         bean.operatorKeyExt = ParseString(cJSON_GetObjectItem(itemRoot, ITEM_OPERATOR_KEY_EXT));
295         bean.ruleId = GetRuleId(bean);
296         vec.push_back(bean);
297     }
298     itemRoot = nullptr;
299     ruleRoot = nullptr;
300 }
301 
GetRuleId(OpKey & bean)302 int ParserUtil::GetRuleId(OpKey &bean)
303 {
304     int ruleId = static_cast<int32_t>(RuleID::RULE_EMPTY);
305     if (!bean.mccmnc.empty()) {
306         ruleId += static_cast<int32_t>(RuleID::RULE_MCCMNC);
307     }
308     if (!bean.iccid.empty()) {
309         ruleId += static_cast<int32_t>(RuleID::RULE_ICCID);
310     }
311     if (!bean.imsi.empty()) {
312         ruleId += static_cast<int32_t>(RuleID::RULE_IMSI);
313     }
314     if (!bean.spn.empty()) {
315         ruleId += static_cast<int32_t>(RuleID::RULE_SPN);
316     }
317     if (!bean.gid1.empty()) {
318         ruleId += static_cast<int32_t>(RuleID::RULE_GID1);
319     }
320     if (!bean.gid2.empty()) {
321         ruleId += static_cast<int32_t>(RuleID::RULE_GID2);
322     }
323     return ruleId;
324 }
325 
ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket & value,const OpKey & bean)326 void ParserUtil::ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket &value, const OpKey &bean)
327 {
328     value.PutString(OpKeyData::MCCMNC, bean.mccmnc);
329     value.PutString(OpKeyData::GID1, bean.gid1);
330     value.PutString(OpKeyData::GID2, bean.gid2);
331     value.PutString(OpKeyData::IMSI, bean.imsi);
332     value.PutString(OpKeyData::SPN, bean.spn);
333     value.PutString(OpKeyData::ICCID, bean.iccid);
334     value.PutString(OpKeyData::OPERATOR_NAME, bean.operatorName);
335     value.PutString(OpKeyData::OPERATOR_KEY, bean.operatorKey);
336     value.PutString(OpKeyData::OPERATOR_KEY_EXT, bean.operatorKeyExt);
337     value.PutInt(OpKeyData::RULE_ID, bean.ruleId);
338 }
339 
ParserNumMatchJson(std::vector<NumMatch> & vec,const bool hashCheck)340 int ParserUtil::ParserNumMatchJson(std::vector<NumMatch> &vec, const bool hashCheck)
341 {
342     char *content = nullptr;
343     char buf[MAX_PATH_LEN];
344     std::string file = GetCustFile(NUM_MATCH_PATH, CUST_NETWORK_PATH_KEY);
345     char *path = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN);
346     int ret = DATA_STORAGE_SUCCESS;
347     if (path && *path != '\0') {
348         ParserUtil parser;
349         ret = parser.LoaderJsonFile(content, path);
350     }
351     if (ret != DATA_STORAGE_SUCCESS) {
352         DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson LoaderJsonFile is fail!\n");
353         return ret;
354     }
355     if (content == nullptr) {
356         DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
357         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
358     }
359     if (hashCheck && !IsDigestChanged(path, NUM_MATCH_HASH)) {
360         free(content);
361         return FILE_HASH_NO_CHANGE;
362     }
363     cJSON *root = cJSON_Parse(content);
364     free(content);
365     content = nullptr;
366     if (root == nullptr) {
367         DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson root is error!\n");
368         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
369     }
370     cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_NUM_MATCH_INFOS);
371     if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) {
372         DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson itemRoots size == 0!\n");
373         cJSON_Delete(root);
374         itemRoots = nullptr;
375         root = nullptr;
376         return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
377     }
378     ParserNumMatchInfos(vec, itemRoots);
379     cJSON_Delete(root);
380     itemRoots = nullptr;
381     root = nullptr;
382     return DATA_STORAGE_SUCCESS;
383 }
384 
ParserNumMatchInfos(std::vector<NumMatch> & vec,cJSON * itemRoots)385 void ParserUtil::ParserNumMatchInfos(std::vector<NumMatch> &vec, cJSON *itemRoots)
386 {
387     cJSON *itemRoot = nullptr;
388     for (int32_t i = 0; i < cJSON_GetArraySize(itemRoots); i++) {
389         itemRoot = cJSON_GetArrayItem(itemRoots, i);
390         if (itemRoot == nullptr) {
391             continue;
392         }
393         NumMatch bean;
394         bean.name = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_NAME));
395         bean.mcc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MCC));
396         bean.mnc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MNC));
397         bean.numMatch = ParseInt(cJSON_GetObjectItem(itemRoot, ITEM_NUM_MATCH));
398         bean.numMatchShort = ParseInt(cJSON_GetObjectItem(itemRoot, ITEM_NUM_MATCH_SHORT));
399         vec.push_back(bean);
400     }
401     itemRoot = nullptr;
402 }
403 
ParseAsString(const cJSON * value)404 std::string ParserUtil::ParseAsString(const cJSON *value)
405 {
406     if (value != nullptr && value->type == cJSON_String && value->valuestring != nullptr) {
407         return value->valuestring;
408     } else if (value != nullptr && value->type == cJSON_Number) {
409         return std::to_string(static_cast<int64_t>(cJSON_GetNumberValue(value)));
410     }
411     return "";
412 }
413 
ParseInt(const cJSON * value)414 int32_t ParserUtil::ParseInt(const cJSON *value)
415 {
416     if (value != nullptr && value->type == cJSON_Number) {
417         return value->valueint;
418     }
419     return 0;
420 }
421 
ParserNumMatchToValuesBucket(NativeRdb::ValuesBucket & value,const NumMatch & bean)422 void ParserUtil::ParserNumMatchToValuesBucket(NativeRdb::ValuesBucket &value, const NumMatch &bean)
423 {
424     value.PutString(NumMatchData::NAME, bean.name);
425     value.PutString(NumMatchData::MCC, bean.mcc);
426     value.PutString(NumMatchData::MNC, bean.mnc);
427     std::string mccmnc(bean.mcc);
428     mccmnc.append(bean.mnc);
429     value.PutString(NumMatchData::MCCMNC, mccmnc);
430     value.PutInt(NumMatchData::NUM_MATCH, bean.numMatch);
431     value.PutInt(NumMatchData::NUM_MATCH_SHORT, bean.numMatchShort);
432 }
433 
ParserEccDataJson(std::vector<EccNum> & vec,const bool hashCheck)434 int ParserUtil::ParserEccDataJson(std::vector<EccNum> &vec, const bool hashCheck)
435 {
436     char *content = nullptr;
437     char buf[MAX_PATH_LEN];
438     std::string file = GetCustFile(ECC_DATA_PATH, CUST_NETWORK_PATH_KEY);
439     char *path = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN);
440     int ret = DATA_STORAGE_SUCCESS;
441     if (path && *path != '\0') {
442         ret = LoaderJsonFile(content, path);
443     }
444     if (ret != DATA_STORAGE_SUCCESS) {
445         DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson LoaderJsonFile is fail!");
446         return ret;
447     }
448     if (content == nullptr) {
449         DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
450         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
451     }
452     if (hashCheck && !IsDigestChanged(path, ECC_DATA_HASH)) {
453         free(content);
454         return FILE_HASH_NO_CHANGE;
455     }
456     cJSON *root = cJSON_Parse(content);
457     free(content);
458     content = nullptr;
459     if (root == nullptr) {
460         DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson root is error!");
461         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
462     }
463 
464     cJSON *itemRoots = cJSON_GetObjectItem(root, ITEM_OPERATOR_INFOS);
465     if (itemRoots == nullptr || !cJSON_IsArray(itemRoots) || cJSON_GetArraySize(itemRoots) <= 0) {
466         DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson itemRoots size == 0!");
467         cJSON_Delete(root);
468         itemRoots = nullptr;
469         root = nullptr;
470         return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
471     }
472     ParserEccDataInfos(vec, itemRoots);
473     cJSON_Delete(root);
474     itemRoots = nullptr;
475     root = nullptr;
476     return DATA_STORAGE_SUCCESS;
477 }
478 
ParserEccDataInfos(std::vector<EccNum> & vec,cJSON * itemRoots)479 void ParserUtil::ParserEccDataInfos(std::vector<EccNum> &vec, cJSON *itemRoots)
480 {
481     cJSON *itemRoot = nullptr;
482     for (int i = 0; i < cJSON_GetArraySize(itemRoots); i++) {
483         itemRoot = cJSON_GetArrayItem(itemRoots, i);
484         if (itemRoot == nullptr) {
485             continue;
486         }
487         EccNum bean;
488         bean.name = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_NAME));
489         bean.mcc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MCC));
490         bean.mnc = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_MNC));
491         bean.numeric = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_NUMERIC));
492         bean.ecc_withcard = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_ECC_WITH_CARD));
493         bean.ecc_nocard = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_ECC_NO_CARD));
494         bean.ecc_fake = ParseAsString(cJSON_GetObjectItem(itemRoot, ITEM_ECC_FAKE));
495         vec.push_back(bean);
496     }
497     itemRoot = nullptr;
498 }
499 
ParserEccDataToValuesBucket(NativeRdb::ValuesBucket & value,const EccNum & bean)500 void ParserUtil::ParserEccDataToValuesBucket(NativeRdb::ValuesBucket &value, const EccNum &bean)
501 {
502     value.PutString(EccData::NAME, bean.name);
503     value.PutString(EccData::MCC, bean.mcc);
504     value.PutString(EccData::MNC, bean.mnc);
505     value.PutString(EccData::NUMERIC, bean.numeric);
506     value.PutString(EccData::ECC_WITH_CARD, bean.ecc_withcard);
507     value.PutString(EccData::ECC_NO_CARD, bean.ecc_nocard);
508     value.PutString(EccData::ECC_FAKE, bean.ecc_fake);
509 }
510 
LoaderJsonFile(char * & content,const char * path) const511 int ParserUtil::LoaderJsonFile(char *&content, const char *path) const
512 {
513     long len = 0;
514     char realPath[PATH_MAX] = { 0x00 };
515     if (realpath(path, realPath) == nullptr) {
516         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile realpath fail!");
517         return static_cast<int>(LoadProFileErrorType::REALPATH_FAIL);
518     }
519     FILE *f = fopen(realPath, "rb");
520     if (f == nullptr) {
521         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile file is null!");
522         return static_cast<int>(LoadProFileErrorType::OPEN_FILE_ERROR);
523     }
524     int ret_seek_end = fseek(f, 0, SEEK_END);
525     if (ret_seek_end != 0) {
526         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_end != 0!");
527         CloseFile(f);
528         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
529     }
530     len = ftell(f);
531     int ret_seek_set = fseek(f, 0, SEEK_SET);
532     if (ret_seek_set != 0) {
533         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_set != 0!");
534         CloseFile(f);
535         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
536     }
537     if (len == 0 || len > static_cast<long>(MAX_BYTE_LEN)) {
538         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile len <= 0 or len > LONG_MAX!");
539         CloseFile(f);
540         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
541     }
542     content = static_cast<char *>(malloc(len + 1));
543     if (content == nullptr) {
544         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile malloc content fail!");
545         CloseFile(f);
546         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
547     }
548     if (memset_s(content, len + 1, 0, len + 1) != EOK) {
549         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile memset_s failed");
550         free(content);
551         content = nullptr;
552         CloseFile(f);
553         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
554     }
555     size_t ret_read = fread(content, 1, len, f);
556     if (ret_read != static_cast<size_t>(len)) {
557         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_read != len!");
558         free(content);
559         content = nullptr;
560         CloseFile(f);
561         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
562     }
563     auto ret = CloseFile(f);
564     if (ret != DATA_STORAGE_SUCCESS) {
565         free(content);
566         content = nullptr;
567         return ret;
568     }
569     return ret;
570 }
571 
CloseFile(FILE * f) const572 int ParserUtil::CloseFile(FILE *f) const
573 {
574     int ret_close = fclose(f);
575     if (ret_close != 0) {
576         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_close != 0!");
577         return static_cast<int>(LoadProFileErrorType::CLOSE_FILE_ERROR);
578     }
579     return DATA_STORAGE_SUCCESS;
580 }
581 
GetCustFile(const char * & file,const char * key)582 std::string ParserUtil::GetCustFile(const char *&file, const char *key)
583 {
584     std::string custFile = system::GetParameter(key, "");
585     if (!custFile.empty()) {
586         custFile.append(file);
587     } else {
588         custFile = file;
589     }
590     return custFile;
591 }
592 
GetPdpProfilePath(int slotId,std::string & path)593 int ParserUtil::GetPdpProfilePath(int slotId, std::string &path)
594 {
595     int mode = MODE_SLOT_0;
596     if (slotId == SimSlotId::SIM_SLOT_1) {
597         mode = MODE_SLOT_1;
598     }
599     char buf[MAX_PATH_LEN];
600     char *ret = GetOneCfgFileEx(PATH, buf, MAX_PATH_LEN, mode, nullptr);
601     if (ret && *ret != '\0') {
602         path = ret;
603         return OPERATION_OK;
604     }
605     DATA_STORAGE_LOGE("ParserUtil::GetPdpProfilePath fail");
606     return OPERATION_ERROR;
607 }
608 
GetFileChecksum(const char * path,std::string & checkSum)609 int ParserUtil::GetFileChecksum(const char *path, std::string &checkSum)
610 {
611     char realPath[PATH_MAX] = {0x00};
612     if (realpath(path, realPath) == nullptr) {
613         DATA_STORAGE_LOGE("ParserUtil::GetFileChecksum Failed to get realPath!");
614         return OPERATION_ERROR;
615     }
616     std::ifstream file(realPath, std::ios::binary);
617     if (!file.is_open()) {
618         DATA_STORAGE_LOGE("ParserUtil::GetFileChecksum Failed to open file!");
619         return OPERATION_ERROR;
620     }
621     std::vector<char> buffer(BYTE_LEN);
622     uint32_t crc32 = crc32_z(0L, Z_NULL, 0);
623     while (file) {
624         file.read(buffer.data(), buffer.size());
625         auto bytesRead = file.gcount();
626         if (bytesRead > 0) {
627             crc32 = crc32_z(crc32, reinterpret_cast<const Bytef *>(buffer.data()), static_cast<uInt>(bytesRead));
628         }
629     }
630     checkSum = std::to_string(crc32);
631     return OPERATION_OK;
632 }
633 
IsNeedInsertToTable(cJSON * value)634 bool ParserUtil::IsNeedInsertToTable(cJSON *value)
635 {
636     if (value == nullptr || cJSON_GetObjectItem(value, ITEM_APN) == nullptr) {
637         return false;
638     }
639     char *tempChar = cJSON_PrintUnformatted(value);
640     if (tempChar == nullptr) {
641         return false;
642     }
643     std::string res = tempChar;
644     free(tempChar);
645     tempChar = nullptr;
646     return DelayedRefSingleton<CoreServiceClient>::GetInstance().IsAllowedInsertApn(res);
647 }
648 
IsDigestChanged(const char * path,const std::string & key)649 bool ParserUtil::IsDigestChanged(const char *path, const std::string &key)
650 {
651     std::string newHash;
652     ParserUtil util;
653     util.GetFileChecksum(path, newHash);
654     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
655     if (preferencesUtil == nullptr) {
656         DATA_STORAGE_LOGE("ParserUtil::IsDigestChanged preferencesUtil is nullptr!");
657         return true;
658     }
659     std::string oldHash = preferencesUtil->ObtainString(key, DEFAULT_PREFERENCES_STRING_VALUE);
660     if (oldHash.compare(newHash) == 0) {
661         DATA_STORAGE_LOGD("ParserUtil::IsDigestChanged file not changed");
662         return false;
663     }
664     DATA_STORAGE_LOGI("ParserUtil::IsDigestChanged file is changed");
665     preferencesUtil->SaveString(key + TEMP_SUFFIX, newHash);
666     return true;
667 }
668 
RefreshDigest(const std::string & key)669 void ParserUtil::RefreshDigest(const std::string &key)
670 {
671     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
672     if (preferencesUtil == nullptr) {
673         DATA_STORAGE_LOGE("ParserUtil::RefreshDigest preferencesUtil is nullptr!");
674         return;
675     }
676     std::string tempHash = preferencesUtil->ObtainString(key + TEMP_SUFFIX, DEFAULT_PREFERENCES_STRING_VALUE);
677     if (tempHash != DEFAULT_PREFERENCES_STRING_VALUE) {
678         preferencesUtil->SaveString(key, tempHash);
679         preferencesUtil->RemoveKey(key + TEMP_SUFFIX);
680         preferencesUtil->Refresh();
681     }
682 }
683 
ClearTempDigest(const std::string & key)684 void ParserUtil::ClearTempDigest(const std::string &key)
685 {
686     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
687     if (preferencesUtil == nullptr) {
688         DATA_STORAGE_LOGE("ParserUtil::ClearTempDigest preferencesUtil is nullptr!");
689         return;
690     }
691     preferencesUtil->RemoveKey(key + TEMP_SUFFIX);
692     preferencesUtil->Refresh();
693 }
694 
GetJsonItemStringVaule(const std::string & path,const std::string & rootName,const std::string & key,std::string & resultValue)695 bool ParserUtil::GetJsonItemStringVaule(const std::string &path, const std::string &rootName,
696     const std::string &key, std::string &resultValue)
697 {
698     char realPath[PATH_MAX] = {0};
699     if (realpath(path.c_str(), realPath) == nullptr) {
700         DATA_STORAGE_LOGE("realpath is fail!");
701         return false;
702     }
703     char* content = nullptr;
704     if (LoaderJsonFile(content, realPath) != DATA_STORAGE_SUCCESS) {
705         DATA_STORAGE_LOGE("LoaderJsonFile is fail!");
706         return false;
707     }
708     if (content == nullptr) {
709         DATA_STORAGE_LOGE("content is nullptr!");
710         return false;
711     }
712     cJSON *root = cJSON_Parse(content);
713     free(content);
714     content = nullptr;
715     if (root == nullptr) {
716         DATA_STORAGE_LOGE("root is error!");
717         return false;
718     }
719     cJSON *itemRoots = cJSON_GetObjectItem(root, rootName.c_str());
720     if (itemRoots == nullptr || cJSON_IsArray(itemRoots)) {
721         DATA_STORAGE_LOGE("itemRoots size == 0!");
722         cJSON_Delete(root);
723         itemRoots = nullptr;
724         root = nullptr;
725         return false;
726     }
727     cJSON *value = cJSON_GetObjectItem(itemRoots, key.c_str());
728     if (value == nullptr || value->type != cJSON_String) {
729         DATA_STORAGE_LOGE("value is error!");
730         cJSON_Delete(root);
731         value = nullptr;
732         itemRoots = nullptr;
733         root = nullptr;
734         return false;
735     }
736     resultValue = value->valuestring;
737     cJSON_Delete(root);
738     value = nullptr;
739     itemRoots = nullptr;
740     root = nullptr;
741     return true;
742 }
743 } // namespace Telephony
744 } // namespace OHOS
745