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 "climits"
23 #include "config_policy_utils.h"
24 #include "cstdint"
25 #include "cstdio"
26 #include "cstdlib"
27 #include "cstring"
28 #include "data_storage_errors.h"
29 #include "data_storage_log_wrapper.h"
30 #include "global_params_data.h"
31 #include "json/config.h"
32 #include "json/reader.h"
33 #include "json/value.h"
34 #include "memory"
35 #include "new"
36 #include "opkey_data.h"
37 #include "parameters.h"
38 #include "pdp_profile_data.h"
39 #include "values_bucket.h"
40 #include "vector"
41
42 namespace OHOS {
43 namespace Telephony {
44 const char *PATH = "/etc/telephony/pdp_profile.json";
45 const char *ITEM_OPERATOR_INFOS = "operator_infos";
46 const char *ITEM_OPERATOR_NAME = "operator_name";
47 const char *ITEM_AUTH_USER = "auth_user";
48 const char *ITEM_AUTH_PWD = "auth_pwd";
49 const char *ITEM_AUTH_TYPE = "auth_type";
50 const char *ITEM_MCC = "mcc";
51 const char *ITEM_MNC = "mnc";
52 const char *ITEM_APN = "apn";
53 const char *ITEM_APN_TYPES = "apn_types";
54 const char *ITEM_IP_ADDRESS = "ip_addr";
55 const char *ITEM_MMS_IP_ADDRESS = "mms_ip_addr";
56 const char *ITEM_HOME_URL = "home_url";
57 const char *ITEM_MVNO_TYPE = "mvno_type";
58 const char *ITEM_MVNO_MATCH_DATA = "mvno_match_data";
59 const char *ITEM_EDITED_STATUS = "edited";
60 const char *ITEM_SERVER = "server";
61 const char *ITEM_BEARER = "bearing_system_type";
62 const char *ITEM_IS_ROAMING_APN = "is_roaming_apn";
63 const char *ITEM_APN_PROTOCOL = "apn_protocol";
64 const char *ITEM_ROAMING_PROTOCOL = "apn_roam_protocol";
65 const char *APN_VERSION = "apn_version";
66 const char *OPKEY_INFO_PATH = "etc/telephony/OpkeyInfo.json";
67 const char *ITEM_OPERATOR_ID = "operator_id";
68 const char *ITEM_RULE = "rule";
69 const char *ITEM_MCCMNC = "mcc_mnc";
70 const char *ITEM_GID_ONE = "gid1";
71 const char *ITEM_GID_TWO = "gid2";
72 const char *ITEM_IMSI = "imsi";
73 const char *ITEM_SPN = "spn";
74 const char *ITEM_ICCID = "iccid";
75 const char *ITEM_OPERATOR_NAME_OPKEY = "operator_name";
76 const char *ITEM_OPERATOR_KEY = "operator_key";
77 const char *ITEM_OPERATOR_KEY_EXT = "operator_key_ext";
78 const char *NUM_MATCH_PATH = "etc/telephony/number_match.json";
79 const char *ECC_DATA_PATH = "etc/telephony/ecc_data.json";
80 const char *ITEM_NUM_MATCH_INFOS = "numMatchs";
81 const char *ITEM_NAME = "name";
82 const char *ITEM_NUM_MATCH = "num_match";
83 const char *ITEM_NUM_MATCH_SHORT = "num_match_short";
84 const char *ITEM_NUMERIC = "numeric";
85 const char *ITEM_ECC_WITH_CARD = "ecc_withcard";
86 const char *ITEM_ECC_NO_CARD = "ecc_nocard";
87 const char *ITEM_ECC_FAKE = "ecc_fake";
88 const int MAX_BYTE_LEN = 10 * 1024 * 1024;
89 static constexpr const char *CUST_RULE_PATH_KEY = "const.telephony.rule_path";
90 static constexpr const char *CUST_NETWORK_PATH_KEY = "const.telephony.network_path";
91
ParserPdpProfileJson(std::vector<PdpProfile> & vec)92 int ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec)
93 {
94 char *content = nullptr;
95 char buf[MAX_PATH_LEN];
96 char *path = GetOneCfgFile(PATH, buf, MAX_PATH_LEN);
97 int ret = DATA_STORAGE_SUCCESS;
98 if (path && *path != '\0') {
99 ret = LoaderJsonFile(content, path);
100 }
101 if (ret != DATA_STORAGE_SUCCESS) {
102 DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson LoaderJsonFile is fail!");
103 return ret;
104 }
105 if (content == nullptr) {
106 DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
107 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
108 }
109 const int contentLength = strlen(content);
110 const std::string rawJson(content);
111 free(content);
112 content = nullptr;
113 JSONCPP_STRING err;
114 Json::Value root;
115 Json::CharReaderBuilder builder;
116 Json::CharReader *reader(builder.newCharReader());
117 if (!reader->parse(rawJson.c_str(), rawJson.c_str() + contentLength, &root, &err)) {
118 DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson reader is error!");
119 delete reader;
120 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
121 }
122 delete reader;
123 reader = nullptr;
124 Json::Value itemRoots = root[ITEM_OPERATOR_INFOS];
125 if (itemRoots.size() == 0) {
126 DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson itemRoots size == 0!");
127 return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
128 }
129 ParserPdpProfileInfos(vec, itemRoots);
130 return DATA_STORAGE_SUCCESS;
131 }
132
ParserPdpProfileInfos(std::vector<PdpProfile> & vec,Json::Value & root)133 void ParserUtil::ParserPdpProfileInfos(std::vector<PdpProfile> &vec, Json::Value &root)
134 {
135 for (int32_t i = 0; i < static_cast<int32_t>(root.size()); i++) {
136 Json::Value itemRoot = root[i];
137 PdpProfile bean;
138 bean.profileName = ParseString(itemRoot[ITEM_OPERATOR_NAME]);
139 bean.authUser = ParseString(itemRoot[ITEM_AUTH_USER]);
140 bean.authPwd = ParseString(itemRoot[ITEM_AUTH_PWD]);
141 std::string authTypeStr = ParseString(itemRoot[ITEM_AUTH_TYPE]);
142 bean.authType = authTypeStr.empty() ? 0 : atoi(authTypeStr.c_str());
143 bean.mcc = ParseString(itemRoot[ITEM_MCC]);
144 bean.mnc = ParseString(itemRoot[ITEM_MNC]);
145 bean.apn = ParseString(itemRoot[ITEM_APN]);
146 bean.apnTypes = ParseString(itemRoot[ITEM_APN_TYPES]);
147 bean.mmsIpAddress = ParseString(itemRoot[ITEM_MMS_IP_ADDRESS]);
148 bean.proxyIpAddress = ParseString(itemRoot[ITEM_IP_ADDRESS]);
149 bean.homeUrl = ParseString(itemRoot[ITEM_HOME_URL]);
150 bean.mvnoType = ParseString(itemRoot[ITEM_MVNO_TYPE]);
151 bean.mvnoMatchData = ParseString(itemRoot[ITEM_MVNO_MATCH_DATA]);
152 bean.server = ParseString(itemRoot[ITEM_SERVER]);
153 std::string editedStr = ParseString(itemRoot[ITEM_EDITED_STATUS]);
154 bean.edited = editedStr.empty() ? 0 : atoi(editedStr.c_str());
155 std::string bearingStr = ParseString(itemRoot[ITEM_BEARER]);
156 bean.bearingSystemType = bearingStr.empty() ? 0 : atoi(bearingStr.c_str());
157 std::string isRoamingApnStr = ParseString(itemRoot[ITEM_IS_ROAMING_APN]);
158 bean.isRoamingApn = isRoamingApnStr.empty() ? 0 : atoi(isRoamingApnStr.c_str());
159 std::string pdpProtocolStr = ParseString(itemRoot[ITEM_APN_PROTOCOL]);
160 bean.pdpProtocol = pdpProtocolStr.empty() ? "IP" : pdpProtocolStr;
161 std::string roamPdpProtocolStr = ParseString(itemRoot[ITEM_ROAMING_PROTOCOL]);
162 bean.roamPdpProtocol = roamPdpProtocolStr.empty() ? "IP" : roamPdpProtocolStr;
163 vec.push_back(bean);
164 }
165 }
166
ParseString(const Json::Value & value)167 std::string ParserUtil::ParseString(const Json::Value &value)
168 {
169 return value.isString() ? value.asString() : "";
170 }
171
ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket & value,const PdpProfile & bean)172 void ParserUtil::ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket &value, const PdpProfile &bean)
173 {
174 value.PutString(PdpProfileData::PROFILE_NAME, bean.profileName);
175 value.PutString(PdpProfileData::MCC, bean.mcc);
176 value.PutString(PdpProfileData::MNC, bean.mnc);
177 std::string mccmnc(bean.mcc);
178 mccmnc.append(bean.mnc);
179 value.PutString(PdpProfileData::MCCMNC, mccmnc);
180 value.PutString(PdpProfileData::APN, bean.apn);
181 value.PutInt(PdpProfileData::AUTH_TYPE, bean.authType);
182 value.PutString(PdpProfileData::AUTH_USER, bean.authUser);
183 value.PutString(PdpProfileData::AUTH_PWD, bean.authPwd);
184 value.PutString(PdpProfileData::APN_TYPES, bean.apnTypes);
185 value.PutBool(PdpProfileData::IS_ROAMING_APN, bean.isRoamingApn);
186 value.PutString(PdpProfileData::HOME_URL, bean.homeUrl);
187 value.PutString(PdpProfileData::PROXY_IP_ADDRESS, bean.proxyIpAddress);
188 value.PutString(PdpProfileData::MMS_IP_ADDRESS, bean.mmsIpAddress);
189 value.PutString(PdpProfileData::APN_PROTOCOL, bean.pdpProtocol);
190 value.PutString(PdpProfileData::APN_ROAM_PROTOCOL, bean.roamPdpProtocol);
191 value.PutString(PdpProfileData::MVNO_TYPE, bean.mvnoType);
192 value.PutString(PdpProfileData::MVNO_MATCH_DATA, bean.mvnoMatchData);
193 value.PutInt(PdpProfileData::EDITED_STATUS, bean.edited);
194 value.PutString(PdpProfileData::SERVER, bean.server);
195 value.PutInt(PdpProfileData::BEARING_SYSTEM_TYPE, bean.bearingSystemType);
196 }
197
ParseFromCustomSystem(std::vector<OpKey> & vec)198 bool ParserUtil::ParseFromCustomSystem(std::vector<OpKey> &vec)
199 {
200 DATA_STORAGE_LOGI("ParserUtil ParseFromCustomSystem");
201 std::string file = GetCustFile(OPKEY_INFO_PATH, CUST_RULE_PATH_KEY);
202 CfgFiles *cfgFiles = GetCfgFiles(file.c_str());
203 if (cfgFiles == nullptr) {
204 DATA_STORAGE_LOGE("ParserUtil ParseFromCustomSystem cfgFiles is null");
205 return false;
206 }
207 char *filePath = nullptr;
208 int result = DATA_STORAGE_ERROR;
209 for (int32_t i = MAX_CFG_POLICY_DIRS_CNT - 1; i >= 0; i--) {
210 filePath = cfgFiles->paths[i];
211 if (filePath && *filePath != '\0') {
212 if (ParserOpKeyJson(vec, filePath) == DATA_STORAGE_SUCCESS) {
213 result = DATA_STORAGE_SUCCESS;
214 }
215 }
216 }
217 FreeCfgFiles(cfgFiles);
218 return result == DATA_STORAGE_SUCCESS;
219 }
220
ParserOpKeyJson(std::vector<OpKey> & vec,const char * path)221 int ParserUtil::ParserOpKeyJson(std::vector<OpKey> &vec, const char *path)
222 {
223 char *content = nullptr;
224 int ret = LoaderJsonFile(content, path);
225 if (ret != DATA_STORAGE_SUCCESS) {
226 DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyJson LoaderJsonFile is fail!");
227 return ret;
228 }
229 if (content == nullptr) {
230 DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
231 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
232 }
233 const int contentLength = strlen(content);
234 const std::string rawJson(content);
235 free(content);
236 content = nullptr;
237 JSONCPP_STRING err;
238 Json::Value root;
239 Json::CharReaderBuilder builder;
240 Json::CharReader *reader(builder.newCharReader());
241 if (!reader->parse(rawJson.c_str(), rawJson.c_str() + contentLength, &root, &err)) {
242 DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos reader is error!");
243 delete reader;
244 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
245 }
246 delete reader;
247 reader = nullptr;
248 Json::Value itemRoots = root[ITEM_OPERATOR_ID];
249 if (itemRoots.size() == 0) {
250 DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos itemRoots size == 0!");
251 return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
252 }
253 ParserOpKeyInfos(vec, itemRoots);
254 return DATA_STORAGE_SUCCESS;
255 }
256
ParserOpKeyInfos(std::vector<OpKey> & vec,Json::Value & root)257 void ParserUtil::ParserOpKeyInfos(std::vector<OpKey> &vec, Json::Value &root)
258 {
259 for (int i = 0; i < (int)root.size(); i++) {
260 Json::Value itemRoot = root[i];
261 OpKey bean;
262 Json::Value ruleRoot = itemRoot[ITEM_RULE];
263 if (ruleRoot[ITEM_MCCMNC].isString()) {
264 bean.mccmnc = ruleRoot[ITEM_MCCMNC].asString();
265 }
266 if (ruleRoot[ITEM_GID_ONE].isString()) {
267 bean.gid1 = ruleRoot[ITEM_GID_ONE].asString();
268 }
269 if (ruleRoot[ITEM_GID_TWO].isString()) {
270 bean.gid2 = ruleRoot[ITEM_GID_TWO].asString();
271 }
272 if (ruleRoot[ITEM_IMSI].isString()) {
273 bean.imsi = ruleRoot[ITEM_IMSI].asString();
274 }
275 if (ruleRoot[ITEM_SPN].isString()) {
276 bean.spn = ruleRoot[ITEM_SPN].asString();
277 }
278 if (ruleRoot[ITEM_ICCID].isString()) {
279 bean.iccid = ruleRoot[ITEM_ICCID].asString();
280 }
281 if (itemRoot[ITEM_OPERATOR_NAME_OPKEY].isString()) {
282 bean.operatorName = itemRoot[ITEM_OPERATOR_NAME_OPKEY].asString();
283 }
284 if (itemRoot[ITEM_OPERATOR_KEY].isString()) {
285 bean.operatorKey = itemRoot[ITEM_OPERATOR_KEY].asString();
286 }
287 if (itemRoot[ITEM_OPERATOR_KEY_EXT].isString()) {
288 bean.operatorKeyExt = itemRoot[ITEM_OPERATOR_KEY_EXT].asString();
289 }
290 bean.ruleId = GetRuleId(bean);
291 vec.push_back(bean);
292 }
293 }
294
GetRuleId(OpKey & bean)295 int ParserUtil::GetRuleId(OpKey &bean)
296 {
297 int ruleId = static_cast<int32_t>(RuleID::RULE_EMPTY);
298 if (!bean.mccmnc.empty()) {
299 ruleId += static_cast<int32_t>(RuleID::RULE_MCCMNC);
300 }
301 if (!bean.iccid.empty()) {
302 ruleId += static_cast<int32_t>(RuleID::RULE_ICCID);
303 }
304 if (!bean.imsi.empty()) {
305 ruleId += static_cast<int32_t>(RuleID::RULE_IMSI);
306 }
307 if (!bean.spn.empty()) {
308 ruleId += static_cast<int32_t>(RuleID::RULE_SPN);
309 }
310 if (!bean.gid1.empty()) {
311 ruleId += static_cast<int32_t>(RuleID::RULE_GID1);
312 }
313 if (!bean.gid2.empty()) {
314 ruleId += static_cast<int32_t>(RuleID::RULE_GID2);
315 }
316 return ruleId;
317 }
318
ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket & value,const OpKey & bean)319 void ParserUtil::ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket &value, const OpKey &bean)
320 {
321 value.PutString(OpKeyData::MCCMNC, bean.mccmnc);
322 value.PutString(OpKeyData::GID1, bean.gid1);
323 value.PutString(OpKeyData::GID2, bean.gid2);
324 value.PutString(OpKeyData::IMSI, bean.imsi);
325 value.PutString(OpKeyData::SPN, bean.spn);
326 value.PutString(OpKeyData::ICCID, bean.iccid);
327 value.PutString(OpKeyData::OPERATOR_NAME, bean.operatorName);
328 value.PutString(OpKeyData::OPERATOR_KEY, bean.operatorKey);
329 value.PutString(OpKeyData::OPERATOR_KEY_EXT, bean.operatorKeyExt);
330 value.PutInt(OpKeyData::RULE_ID, bean.ruleId);
331 }
332
ParserNumMatchJson(std::vector<NumMatch> & vec)333 int ParserUtil::ParserNumMatchJson(std::vector<NumMatch> &vec)
334 {
335 char *content = nullptr;
336 char buf[MAX_PATH_LEN];
337 std::string file = GetCustFile(NUM_MATCH_PATH, CUST_NETWORK_PATH_KEY);
338 char *path = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN);
339 int ret = DATA_STORAGE_SUCCESS;
340 if (path && *path != '\0') {
341 ParserUtil parser;
342 ret = parser.LoaderJsonFile(content, path);
343 }
344 if (ret != DATA_STORAGE_SUCCESS) {
345 DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson LoaderJsonFile is fail!\n");
346 return ret;
347 }
348 if (content == nullptr) {
349 DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
350 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
351 }
352 const int contentLength = strlen(content);
353 const std::string rawJson(content);
354 free(content);
355 content = nullptr;
356 JSONCPP_STRING err;
357 Json::Value root;
358 Json::CharReaderBuilder builder;
359 Json::CharReader *reader(builder.newCharReader());
360 if (!reader->parse(rawJson.c_str(), rawJson.c_str() +contentLength, &root, &err)) {
361 DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson reader is error!\n");
362 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
363 }
364 delete reader;
365 reader = nullptr;
366 Json::Value itemRoots = root[ITEM_NUM_MATCH_INFOS];
367 if (itemRoots.size() == 0) {
368 DATA_STORAGE_LOGE("ParserUtil::ParserNumMatchJson itemRoots size == 0!\n");
369 return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
370 }
371 ParserNumMatchInfos(vec, itemRoots);
372 return DATA_STORAGE_SUCCESS;
373 }
374
ParserNumMatchInfos(std::vector<NumMatch> & vec,Json::Value & root)375 void ParserUtil::ParserNumMatchInfos(std::vector<NumMatch> &vec, Json::Value &root)
376 {
377 for (int32_t i = 0; i < static_cast<int32_t>(root.size()); i++) {
378 Json::Value itemRoot = root[i];
379 NumMatch bean;
380 bean.name = itemRoot[ITEM_NAME].asString();
381 bean.mcc = itemRoot[ITEM_MCC].asString();
382 bean.mnc = itemRoot[ITEM_MNC].asString();
383 bean.numMatch = itemRoot[ITEM_NUM_MATCH].asInt();
384 bean.numMatchShort = itemRoot[ITEM_NUM_MATCH_SHORT].asInt();
385 vec.push_back(bean);
386 }
387 }
388
ParserNumMatchToValuesBucket(NativeRdb::ValuesBucket & value,const NumMatch & bean)389 void ParserUtil::ParserNumMatchToValuesBucket(NativeRdb::ValuesBucket &value, const NumMatch &bean)
390 {
391 value.PutString(NumMatchData::NAME, bean.name);
392 value.PutString(NumMatchData::MCC, bean.mcc);
393 value.PutString(NumMatchData::MNC, bean.mnc);
394 std::string mccmnc(bean.mcc);
395 mccmnc.append(bean.mnc);
396 value.PutString(NumMatchData::MCCMNC, mccmnc);
397 value.PutInt(NumMatchData::NUM_MATCH, bean.numMatch);
398 value.PutInt(NumMatchData::NUM_MATCH_SHORT, bean.numMatchShort);
399 }
400
ParserEccDataJson(std::vector<EccNum> & vec)401 int ParserUtil::ParserEccDataJson(std::vector<EccNum> &vec)
402 {
403 char *content = nullptr;
404 char buf[MAX_PATH_LEN];
405 std::string file = GetCustFile(ECC_DATA_PATH, CUST_NETWORK_PATH_KEY);
406 char *path = GetOneCfgFile(file.c_str(), buf, MAX_PATH_LEN);
407 int ret = DATA_STORAGE_SUCCESS;
408 if (path && *path != '\0') {
409 ret = LoaderJsonFile(content, path);
410 }
411 if (ret != DATA_STORAGE_SUCCESS) {
412 DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson LoaderJsonFile is fail!");
413 return ret;
414 }
415 if (content == nullptr) {
416 DATA_STORAGE_LOGE("ParserUtil::content is nullptr!");
417 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
418 }
419 const int contentLength = strlen(content);
420 const std::string rawJson(content);
421 free(content);
422 content = nullptr;
423 JSONCPP_STRING err;
424 Json::Value root;
425 Json::CharReaderBuilder builder;
426 Json::CharReader *reader(builder.newCharReader());
427 if (!reader->parse(rawJson.c_str(), rawJson.c_str() + contentLength, &root, &err)) {
428 DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson reader is error!");
429 delete reader;
430 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
431 }
432 delete reader;
433 reader = nullptr;
434 Json::Value itemRoots = root[ITEM_OPERATOR_INFOS];
435 if (itemRoots.size() == 0) {
436 DATA_STORAGE_LOGE("ParserUtil::ParserEccDataJson itemRoots size == 0!");
437 return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
438 }
439 ParserEccDataInfos(vec, itemRoots);
440 return DATA_STORAGE_SUCCESS;
441 }
442
ParserEccDataInfos(std::vector<EccNum> & vec,Json::Value & roots)443 void ParserUtil::ParserEccDataInfos(std::vector<EccNum> &vec, Json::Value &roots)
444 {
445 for (int i = 0; i < static_cast<int>(roots.size()); i++) {
446 Json::Value itemRoot = roots[i];
447 EccNum bean;
448 bean.name = itemRoot[ITEM_NAME].asString();
449 bean.mcc = itemRoot[ITEM_MCC].asString();
450 bean.mnc = itemRoot[ITEM_MNC].asString();
451 bean.numeric = itemRoot[ITEM_NUMERIC].asString();
452 bean.ecc_withcard = itemRoot[ITEM_ECC_WITH_CARD].asString();
453 bean.ecc_nocard = itemRoot[ITEM_ECC_NO_CARD].asString();
454 bean.ecc_fake = itemRoot[ITEM_ECC_FAKE].asString();
455 vec.push_back(bean);
456 }
457 }
458
ParserEccDataToValuesBucket(NativeRdb::ValuesBucket & value,const EccNum & bean)459 void ParserUtil::ParserEccDataToValuesBucket(NativeRdb::ValuesBucket &value, const EccNum &bean)
460 {
461 value.PutString(EccData::NAME, bean.name);
462 value.PutString(EccData::MCC, bean.mcc);
463 value.PutString(EccData::MNC, bean.mnc);
464 value.PutString(EccData::NUMERIC, bean.numeric);
465 value.PutString(EccData::ECC_WITH_CARD, bean.ecc_withcard);
466 value.PutString(EccData::ECC_NO_CARD, bean.ecc_nocard);
467 value.PutString(EccData::ECC_FAKE, bean.ecc_fake);
468 }
469
LoaderJsonFile(char * & content,const char * path) const470 int ParserUtil::LoaderJsonFile(char *&content, const char *path) const
471 {
472 long len = 0;
473 char realPath[PATH_MAX] = { 0x00 };
474 if (realpath(path, realPath) == nullptr) {
475 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile realpath fail! #PATH: %{public}s", path);
476 return static_cast<int>(LoadProFileErrorType::REALPATH_FAIL);
477 }
478 FILE *f = fopen(realPath, "rb");
479 if (f == nullptr) {
480 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile file is null!");
481 return static_cast<int>(LoadProFileErrorType::OPEN_FILE_ERROR);
482 }
483 int ret_seek_end = fseek(f, 0, SEEK_END);
484 if (ret_seek_end != 0) {
485 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_end != 0!");
486 CloseFile(f);
487 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
488 }
489 len = ftell(f);
490 int ret_seek_set = fseek(f, 0, SEEK_SET);
491 if (ret_seek_set != 0) {
492 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_set != 0!");
493 CloseFile(f);
494 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
495 }
496 if (len == 0 || len > static_cast<long>(MAX_BYTE_LEN)) {
497 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile len <= 0 or len > LONG_MAX!");
498 CloseFile(f);
499 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
500 }
501 content = static_cast<char *>(malloc(len + 1));
502 if (content == nullptr) {
503 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile malloc content fail!");
504 CloseFile(f);
505 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
506 }
507 if (memset_s(content, len + 1, 0, len + 1) != EOK) {
508 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile memset_s failed");
509 free(content);
510 content = nullptr;
511 CloseFile(f);
512 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
513 }
514 size_t ret_read = fread(content, 1, len, f);
515 if (ret_read != static_cast<size_t>(len)) {
516 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_read != len!");
517 free(content);
518 content = nullptr;
519 CloseFile(f);
520 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
521 }
522 return CloseFile(f);
523 }
524
CloseFile(FILE * f) const525 int ParserUtil::CloseFile(FILE *f) const
526 {
527 int ret_close = fclose(f);
528 if (ret_close != 0) {
529 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_close != 0!");
530 return static_cast<int>(LoadProFileErrorType::CLOSE_FILE_ERROR);
531 }
532 return DATA_STORAGE_SUCCESS;
533 }
534
GetCustFile(const char * & file,const char * key)535 std::string ParserUtil::GetCustFile(const char *&file, const char *key)
536 {
537 std::string custFile = system::GetParameter(key, "");
538 if (!custFile.empty()) {
539 custFile.append(file);
540 } else {
541 custFile = file;
542 }
543 return custFile;
544 }
545 } // namespace Telephony
546 } // namespace OHOS
547