1 /*
2 * Copyright (c) 2024 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 "ChangeJsonUtil.h"
17 #include <random>
18 #include <fstream>
19 #include <iostream>
20 #include "secodeFuzz.h"
21 #include "cJSON.h"
22 #include "securec.h"
23 using namespace std;
24 using namespace fuzztest;
25
26 namespace {
27 const int DEFAULT_LENGTH = 1000;
28 const int DEFAULT_INT = 1000;
29 const std::string DEFAULT_STRING = "aaaa";
30 }
31
ModifyObject(cJSON * object,uint64_t & idx)32 void ChangeJsonUtil::ModifyObject(cJSON *object, uint64_t& idx)
33 {
34 if (!object) {
35 return;
36 }
37 cJSON *item = nullptr;
38 cJSON_ArrayForEach(item, object) {
39 if (item->type == cJSON_String) {
40 std::string strVal = DT_SetGetString(&g_Element[idx], strlen(item->valuestring) + 1, DEFAULT_LENGTH,
41 (char*)item->valuestring);
42 idx++;
43 cJSON_free(item->valuestring); // 释放原字符串内存
44 item->valuestring = nullptr;
45 int length = strVal.length() + 1;
46 item->valuestring = reinterpret_cast<char*>(malloc(length * sizeof(char))); // 分配内存
47 errno_t ret1 = strcpy_s(item->valuestring, length, strVal.c_str()); // 复制字符串内容
48 if (ret1 != EOK) {
49 printf("strcpy_s in modifyObject copy error");
50 }
51 } else if (item->type == cJSON_Number) {
52 int intVal = *(s32 *)DT_SetGetS32(&g_Element[idx], item->valueint);
53 idx++;
54 item->valueint = intVal;
55 } else if (item->type == cJSON_True || item->type == cJSON_False) {
56 std::random_device rd;
57 std::mt19937 gen(rd());
58 std::bernoulli_distribution distribution(0.5); // 0.5 is probability to generate true or false
59 bool ret = distribution(gen);
60 item->type = ret ? cJSON_True : cJSON_False;
61 } else if (item->type == cJSON_Object) {
62 cJSON *arrayItem = nullptr;
63 cJSON_ArrayForEach(arrayItem, item) {
64 ModifyObject(arrayItem, idx);
65 }
66 } else if (item->type == cJSON_Array) {
67 cJSON *objItem = nullptr;
68 cJSON_ArrayForEach(objItem, item) {
69 ModifyObject(objItem, idx);
70 }
71 }
72 }
73 }
74
ModifyObject4ChangeType(cJSON * object,uint64_t & idx)75 void ChangeJsonUtil::ModifyObject4ChangeType(cJSON *object, uint64_t& idx)
76 {
77 if (!object) {
78 return;
79 }
80 cJSON *item = nullptr;
81 cJSON_ArrayForEach(item, object) {
82 if (item->type == cJSON_String) {
83 int32_t intValue = *(s32 *)DT_SetGetS32(&g_Element[idx], DEFAULT_INT);
84 idx++;
85 item->type = cJSON_Number;
86 item->valuedouble = static_cast<double>(intValue);
87 item->valueint = intValue;
88 cJSON_free(item->valuestring); // 释放原字符串内存
89 item->valuestring = nullptr;
90 } else if (item->type == cJSON_Number || item->type == cJSON_True || item->type == cJSON_False) {
91 std::string strVal = DT_SetGetString(&g_Element[idx], DEFAULT_STRING.size() + 1, DEFAULT_LENGTH,
92 (char*)DEFAULT_STRING.c_str());
93 idx++;
94 item->type = cJSON_String;
95 int length = strVal.length() + 1;
96 item->valuestring = reinterpret_cast<char*>(malloc(length * sizeof(char))); // 分配内存
97 errno_t ret2 = strcpy_s(item->valuestring, length, strVal.c_str()); // 复制字符串内容
98 if (ret2 != EOK) {
99 printf("strcpy_s in modifyObject4ChangeType copy error");
100 }
101 } else if (item->type == cJSON_Object) {
102 cJSON *arrayItem = nullptr;
103 cJSON_ArrayForEach(arrayItem, item) {
104 ModifyObject4ChangeType(arrayItem, idx);
105 }
106 } else if (item->type == cJSON_Array) {
107 cJSON *objItem = nullptr;
108 cJSON_ArrayForEach(objItem, item) {
109 ModifyObject4ChangeType(objItem, idx);
110 }
111 }
112 }
113 }
114
WriteFile(std::string filePath,cJSON * object)115 void ChangeJsonUtil::WriteFile(std::string filePath, cJSON *object)
116 {
117 // Check if cJSON object is valid
118 if (object == nullptr) {
119 std::cerr << "Error: cJSON object is null." << std::endl;
120 return;
121 }
122
123 // Open file stream
124 std::ofstream outFile(filePath);
125 if (!outFile.is_open()) {
126 std::cerr << "Error opening file: " << filePath << std::endl;
127 return;
128 }
129
130 // Convert cJSON object to string
131 char *jsonStr = cJSON_Print(object);
132 if (jsonStr == nullptr) {
133 std::cerr << "Error converting cJSON object to string." << std::endl;
134 outFile.close();
135 return;
136 }
137
138 // Write JSON string to file
139 outFile << jsonStr << std::endl;
140
141 // Clean up
142 cJSON_free(jsonStr);
143 outFile.close();
144
145 std::cout << "JSON content successfully written to file: " << filePath << std::endl;
146 }
147
WriteFile(std::string filePath,std::string str)148 void ChangeJsonUtil::WriteFile(std::string filePath, std::string str)
149 {
150 std::ofstream outFile(filePath);
151 if (!outFile.is_open()) {
152 std::cerr << "Error opening file: " << filePath << std::endl;
153 return;
154 }
155 outFile << str << std::endl;
156 std::cout << "Str content successfully written to file: " << filePath << std::endl;
157 }