1 /*
2 * Copyright (C) 2021-2022 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 "init/json_parser_utils.h"
17
18 #include <fstream>
19 #include <sstream>
20
21 #include "common/hap_verify_log.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace Verify {
ReadTrustedRootCAFromJson(cJSON ** jsonObj,const std::string & jsonPath,std::string & error)26 bool JsonParserUtils::ReadTrustedRootCAFromJson(cJSON** jsonObj,
27 const std::string& jsonPath, std::string& error)
28 {
29 if (jsonObj == NULL) {
30 error += "jsonObj is NULL";
31 return false;
32 }
33 std::ifstream jsonFileStream;
34 jsonFileStream.open(jsonPath.c_str(), std::ios::in);
35 if (!jsonFileStream.is_open()) {
36 error += "open file failed";
37 return false;
38 }
39 std::ostringstream buf;
40 char ch;
41 while (buf && jsonFileStream.get(ch)) {
42 buf.put(ch);
43 }
44 jsonFileStream.close();
45
46 std::string jsonStr = buf.str();
47 *jsonObj = cJSON_Parse(jsonStr.c_str());
48 if (*jsonObj == NULL) {
49 error += "parse jsonStr failed";
50 return false;
51 }
52 return true;
53 }
54
GetJsonString(const cJSON * json,const std::string & key,std::string & value)55 bool JsonParserUtils::GetJsonString(const cJSON* json, const std::string& key, std::string& value)
56 {
57 if (json == NULL || !cJSON_IsObject(json)) {
58 return false;
59 }
60 cJSON* jsonValue = cJSON_GetObjectItemCaseSensitive(json, key.c_str());
61 if (jsonValue != NULL && cJSON_IsString(jsonValue)) {
62 value = jsonValue->valuestring;
63 }
64 return true;
65 }
66
GetJsonInt(const cJSON * json,const std::string & key,int & value)67 bool JsonParserUtils::GetJsonInt(const cJSON* json, const std::string& key, int& value)
68 {
69 if (json == NULL || !cJSON_IsObject(json)) {
70 return false;
71 }
72 cJSON* jsonValue = cJSON_GetObjectItemCaseSensitive(json, key.c_str());
73 if (jsonValue != NULL && cJSON_IsNumber(jsonValue)) {
74 value = jsonValue->valueint;
75 }
76 return true;
77 }
78
GetJsonStringVec(const cJSON * json,const std::string & key,StringVec & value)79 bool JsonParserUtils::GetJsonStringVec(const cJSON* json, const std::string& key, StringVec& value)
80 {
81 if (json == NULL || !cJSON_IsObject(json)) {
82 return false;
83 }
84 cJSON* jsonArray = cJSON_GetObjectItemCaseSensitive(json, key.c_str());
85 if (jsonArray == NULL || !cJSON_IsArray(jsonArray)) {
86 return false;
87 }
88 cJSON* item = NULL;
89 cJSON_ArrayForEach(item, jsonArray) {
90 if (item != NULL && cJSON_IsString(item)) {
91 value.emplace_back(item->valuestring);
92 }
93 }
94 return true;
95 }
96
ParseJsonToObjVec(const cJSON * json,const std::string & key,JsonObjVec & jsonObjVec)97 bool JsonParserUtils::ParseJsonToObjVec(const cJSON* json, const std::string& key, JsonObjVec& jsonObjVec)
98 {
99 if (json == NULL || !cJSON_IsObject(json)) {
100 return false;
101 }
102 cJSON* jsonArray = cJSON_GetObjectItemCaseSensitive(json, key.c_str());
103 if (jsonArray == NULL || !cJSON_IsArray(jsonArray)) {
104 return false;
105 }
106 cJSON* item = NULL;
107 cJSON_ArrayForEach(item, jsonArray) {
108 if (item != NULL && cJSON_IsObject(item)) {
109 jsonObjVec.emplace_back(item);
110 }
111 }
112 return true;
113 }
114
ParseJsonToMap(const cJSON * json,JsonMap & jsonMap)115 void JsonParserUtils::ParseJsonToMap(const cJSON* json, JsonMap& jsonMap)
116 {
117 if (json == NULL || !cJSON_IsObject(json)) {
118 return;
119 }
120 cJSON* item = NULL;
121 cJSON_ArrayForEach(item, json) {
122 if (item != NULL && cJSON_IsString(item)) {
123 std::string key = item->string;
124 std::string value = item->valuestring;
125 jsonMap[key] = value;
126 }
127 }
128 }
129 } // namespace Verify
130 } // namespace Security
131 } // namespace OHOS
132