• 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 <cstring>
20 
21 #include <climits>
22 
23 #include "preferences_util.h"
24 #include "data_storage_log_wrapper.h"
25 
26 
27 namespace OHOS {
28 namespace Telephony {
ParserPdpProfileJson(std::vector<PdpProfile> & vec)29 int ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec)
30 {
31     char *content = nullptr;
32     int ret = LoaderJsonFile(content);
33     if (ret != DATA_STORAGE_SUCCESS) {
34         DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson LoaderJsonFile is fail!\n");
35         return ret;
36     }
37     const int contentLength = strlen(content);
38     const std::string rawJson(content);
39     delete content;
40     JSONCPP_STRING err;
41     Json::Value root;
42     Json::CharReaderBuilder builder;
43     Json::CharReader* reader(builder.newCharReader());
44     if (!reader->parse(rawJson.c_str(), rawJson.c_str() + contentLength, &root, &err)) {
45         DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson reader is error!\n");
46         return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
47     }
48     delete reader;
49     Json::Value itemRoots = root[ITEM_OPERATOR_INFOS];
50     if (itemRoots.size() == 0) {
51         DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson itemRoots size == 0!\n");
52         return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
53     }
54     ParserPdpProfileInfos(vec, itemRoots);
55     return DATA_STORAGE_SUCCESS;
56 }
57 
ParserPdpProfileInfos(std::vector<PdpProfile> & vec,Json::Value & root)58 void ParserUtil::ParserPdpProfileInfos(std::vector<PdpProfile> &vec, Json::Value &root)
59 {
60     for (int i = 0; i < root.size(); i++) {
61         Json::Value itemRoot = root[i];
62         PdpProfile bean;
63         bean.profileName = itemRoot[ITEM_OPERATOR_NAME].asString();
64         bean.authUser = itemRoot[ITEM_AUTH_USER].asString();
65         bean.authPwd = itemRoot[ITEM_AUTH_PWD].asString();
66         std::string authTypeStr = itemRoot[ITEM_AUTH_TYPE].asString();
67         if (authTypeStr.empty()) {
68             bean.authType = 0;
69         } else {
70             bean.authType = atoi(authTypeStr.c_str());
71         }
72         bean.mcc = itemRoot[ITEM_MCC].asString();
73         bean.mnc = itemRoot[ITEM_MNC].asString();
74         bean.apn = itemRoot[ITEM_APN].asString();
75         bean.apnTypes = itemRoot[ITEM_APN_TYPES].asString();
76         bean.mmsIpAddress = itemRoot[ITEM_MMS_IP_ADDRESS].asString();
77         bean.proxyIpAddress =  itemRoot[ITEM_IP_ADDRESS].asString();
78         bean.homeUrl =  itemRoot[ITEM_HOME_URL].asString();
79         vec.push_back(bean);
80     }
81 }
82 
ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket & value,const PdpProfile & bean)83 void ParserUtil::ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket &value, const PdpProfile &bean)
84 {
85     value.PutString(PdpProfileData::PROFILE_NAME, bean.profileName);
86     value.PutString(PdpProfileData::MCC, bean.mcc);
87     value.PutString(PdpProfileData::MNC, bean.mnc);
88     std::string mccmnc(bean.mcc);
89     mccmnc.append(bean.mnc);
90     value.PutString(PdpProfileData::MCCMNC, mccmnc);
91     value.PutString(PdpProfileData::APN, bean.apn);
92     value.PutInt(PdpProfileData::AUTH_TYPE, bean.authType);
93     value.PutString(PdpProfileData::AUTH_USER, bean.authUser);
94     value.PutString(PdpProfileData::AUTH_PWD, bean.authPwd);
95     value.PutString(PdpProfileData::APN_TYPES, bean.apnTypes);
96     value.PutBool(PdpProfileData::IS_ROAMING_APN, bean.isRoamingApn);
97     value.PutString(PdpProfileData::HOME_URL, bean.homeUrl);
98     value.PutString(PdpProfileData::PROXY_IP_ADDRESS, bean.proxyIpAddress);
99     value.PutString(PdpProfileData::MMS_IP_ADDRESS, bean.mmsIpAddress);
100     value.PutString(PdpProfileData::APN_PROTOCOL, bean.pdpProtocol);
101     value.PutString(PdpProfileData::APN_ROAM_PROTOCOL, bean.roamPdpProtocol);
102 }
103 
LoaderJsonFile(char * & content) const104 int ParserUtil::LoaderJsonFile(char *&content) const
105 {
106     size_t len = 0;
107     char realPath[PATH_MAX] = {0x00};
108     if (realpath(PATH, realPath) == nullptr) {
109         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile realpath fail! #PATH: %{public}s", PATH);
110         return static_cast<int>(LoadProFileErrorType::REALPATH_FAIL);
111     }
112     FILE *f = fopen(realPath, "rb");
113     if (f == nullptr) {
114         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile file is null!");
115         return static_cast<int>(LoadProFileErrorType::OPEN_FILE_ERROR);
116     }
117     int ret_seek_end = fseek(f, 0, SEEK_END);
118     if (ret_seek_end != 0) {
119         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_end != 0!");
120         CloseFile(f);
121         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
122     }
123     len = ftell(f);
124     int ret_seek_set = fseek(f, 0, SEEK_SET);
125     if (ret_seek_set != 0) {
126         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_set != 0!");
127         CloseFile(f);
128         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
129     }
130     if (len <= 0 || len > ULONG_MAX) {
131         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile len <= 0 or len > LONG_MAX!");
132         CloseFile(f);
133         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
134     }
135     content = (char *)malloc(len);
136     if (content == nullptr) {
137         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile malloc content fail!");
138         CloseFile(f);
139         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
140     }
141     size_t ret_read = fread(content, 1, len, f);
142     if (ret_read != len) {
143         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_read != len!");
144         CloseFile(f);
145         free(content);
146         return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
147     }
148     return CloseFile(f);
149 }
150 
CloseFile(FILE * f) const151 int ParserUtil::CloseFile(FILE *f) const
152 {
153     int ret_close = fclose(f);
154     if (ret_close != 0) {
155         DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_close != 0!");
156         return static_cast<int>(LoadProFileErrorType::CLOSE_FILE_ERROR);
157     }
158     return DATA_STORAGE_SUCCESS;
159 }
160 } // namespace Telephony
161 } // namespace OHOS