1 /*
2 * Copyright (c) 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 "cJSON.h"
17
18 #include <dlfcn.h>
19 #include <float.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 int g_getArrayItemTime;
25 int g_getObjectItem;
26 int g_isStringTime;
27 int g_replaceItemInObjectTime;
28 int g_createNumberTime;
29 int g_createArrayTime;
30 int g_createStringTime;
31 int g_addItemToArray;
32 int g_addItemToObject;
33 int g_createObject;
34 int g_parse;
35 int g_getArraySize;
36 int g_printUnformatted;
37 void* g_handle = NULL;
38
39 #define CONST_TEN_TIMES 10
40 #define CONST_TWENTY_TIMES 20
41 #define CONST_THIRTY_TIMES 30
42 #define CONST_FORTY_TIMES 40
43 #define CONST_FIFTY_TIMES 50
44
GetHandle(void)45 void GetHandle(void)
46 {
47 printf("GetHandle\n");
48 if (g_handle != NULL) {
49 return;
50 }
51 g_handle = dlopen("libcjson.z.so", RTLD_LAZY);
52 }
53
cJSON_GetObjectItem(const cJSON * const object,const char * const string)54 cJSON* cJSON_GetObjectItem(const cJSON* const object, const char* const string)
55 {
56 g_getObjectItem++;
57 if (g_getObjectItem == 0 || g_getObjectItem == CONST_TEN_TIMES) { // CONST_TEN_TIMES times failed
58 return NULL;
59 }
60
61 if (g_handle == NULL) {
62 return NULL;
63 }
64 cJSON* (*func)(const cJSON* const object, const char* const string);
65 func = (cJSON* (*)(const cJSON* const object, const char* const string))dlsym(g_handle, "cJSON_GetObjectItem");
66 if (func == NULL) {
67 return NULL;
68 }
69 cJSON* res = func(object, string);
70
71 return res;
72 }
73
cJSON_IsNumber(const cJSON * const item)74 cJSON_bool cJSON_IsNumber(const cJSON* const item)
75 {
76 if (g_handle == NULL) {
77 return 0;
78 }
79 cJSON_bool (*func)(const cJSON* const item);
80 func = (cJSON_bool (*)(const cJSON* const item))dlsym(g_handle, "cJSON_IsNumber");
81 if (func == NULL) {
82 return 0;
83 }
84 cJSON_bool res = func(item);
85 return res;
86 }
87
cJSON_IsString(const cJSON * const item)88 cJSON_bool cJSON_IsString(const cJSON* const item)
89 {
90 g_isStringTime++;
91 if (g_isStringTime == 0) {
92 return 0;
93 }
94 if (g_handle == NULL) {
95 return 0;
96 }
97 cJSON_bool (*func)(const cJSON* const item);
98 func = (cJSON_bool (*)(const cJSON* const item))dlsym(g_handle, "cJSON_IsString");
99 if (func == NULL) {
100 return 0;
101 }
102 cJSON_bool res = func(item);
103 return res;
104 }
105
cJSON_GetNumberValue(const cJSON * const item)106 double cJSON_GetNumberValue(const cJSON* const item)
107 {
108 if (cJSON_IsNumber(item) == 0) {
109 return (double)0;
110 }
111 return item->valuedouble;
112 }
113
cJSON_GetArraySize(const cJSON * array)114 int cJSON_GetArraySize(const cJSON* array)
115 {
116 g_getArraySize++;
117 if (g_getArraySize == 0 ||
118 g_getArraySize == CONST_TEN_TIMES ||
119 g_getArraySize == CONST_TWENTY_TIMES) {
120 return 10000; // 10000 invalid
121 }
122 cJSON* child = NULL;
123 size_t size = 0;
124 if (array == NULL) {
125 return 0;
126 }
127 child = array->child;
128 while (child != NULL) {
129 size++;
130 child = child->next;
131 }
132 return (int)size;
133 }
134
cJSON_CreateArray(void)135 cJSON* cJSON_CreateArray(void)
136 {
137 g_createArrayTime++;
138 if (g_createArrayTime == 0) {
139 return NULL;
140 }
141 if (g_handle == NULL) {
142 return NULL;
143 }
144 cJSON* (*func)(void);
145 func = (cJSON* (*)(void))dlsym(g_handle, "cJSON_CreateArray");
146 if (func == NULL) {
147 return NULL;
148 }
149 cJSON* res = func();
150
151 return res;
152 }
153
cJSON_CreateObject(void)154 cJSON* cJSON_CreateObject(void)
155 {
156 g_createObject++;
157 if (g_createObject == 0) {
158 return NULL;
159 }
160
161 if (g_handle == NULL) {
162 return NULL;
163 }
164 cJSON* (*func)(void);
165 func = (cJSON* (*)(void))dlsym(g_handle, "cJSON_CreateObject");
166 if (func == NULL) {
167 return NULL;
168 }
169 cJSON* res = func();
170 return res;
171 }
172
cJSON_CreateNumber(double num)173 cJSON* cJSON_CreateNumber(double num)
174 {
175 g_createNumberTime++;
176 if (g_createNumberTime == 0 ||
177 g_createNumberTime == CONST_TEN_TIMES ||
178 g_createNumberTime == CONST_TWENTY_TIMES ||
179 g_createNumberTime == CONST_THIRTY_TIMES) {
180 return NULL;
181 }
182
183 if (g_handle == NULL) {
184 return NULL;
185 }
186 cJSON* (*func)(double num);
187 func = (cJSON* (*)(double num))dlsym(g_handle, "cJSON_CreateNumber");
188 if (func == NULL) {
189 return NULL;
190 }
191 cJSON* res = func(num);
192
193 return res;
194 }
195
cJSON_CreateString(const char * string)196 cJSON* cJSON_CreateString(const char *string)
197 {
198 g_createStringTime++;
199 if (g_createStringTime == 0 ||
200 g_createStringTime == CONST_TEN_TIMES) {
201 return NULL;
202 }
203 if (string != NULL && strcmp(string, "processUnique") == 0) {
204 printf("processUnique failed\n");
205 return NULL;
206 }
207
208 if (g_handle == NULL) {
209 return NULL;
210 }
211 cJSON* (*func)(const char *string);
212 func = (cJSON* (*)(const char *string))dlsym(g_handle, "cJSON_CreateString");
213 if (func == NULL) {
214 return NULL;
215 }
216 cJSON* res = func(string);
217
218 return res;
219 }
220
cJSON_AddItemToArray(cJSON * array,cJSON * item)221 cJSON_bool cJSON_AddItemToArray(cJSON* array, cJSON* item)
222 {
223 g_addItemToArray++;
224 if (g_addItemToArray == 0) {
225 return 0;
226 }
227
228 if (g_handle == NULL) {
229 return 0;
230 }
231 cJSON_bool (*func)(cJSON* array, cJSON* item);
232 func = (cJSON_bool (*)(cJSON* array, cJSON* item))dlsym(g_handle, "cJSON_AddItemToArray");
233 if (func == NULL) {
234 return 0;
235 }
236 cJSON_bool res = func(array, item);
237
238 return res;
239 }
240
cJSON_Delete(cJSON * item)241 void cJSON_Delete(cJSON* item)
242 {
243 if (g_handle == NULL) {
244 return;
245 }
246 void (*func)(cJSON* item);
247 func = (void (*)(cJSON* item))dlsym(g_handle, "cJSON_Delete");
248 if (func == NULL) {
249 return;
250 }
251 func(item);
252 return;
253 }
254
cJSON_AddItemToObject(cJSON * object,const char * string,cJSON * item)255 cJSON_bool cJSON_AddItemToObject(cJSON*object, const char *string, cJSON* item)
256 {
257 g_addItemToObject++;
258 if (g_addItemToObject == 0 ||
259 g_addItemToObject == CONST_TEN_TIMES ||
260 g_addItemToObject == CONST_TWENTY_TIMES ||
261 g_addItemToObject == CONST_THIRTY_TIMES ||
262 g_addItemToObject == CONST_FORTY_TIMES ||
263 g_addItemToObject == CONST_FIFTY_TIMES) {
264 return 0;
265 }
266 if (g_handle == NULL) {
267 return 0;
268 }
269 cJSON_bool (*func)(cJSON*object, const char *string, cJSON* item);
270 func = (cJSON_bool (*)(cJSON*object, const char *string, cJSON* item))dlsym(g_handle, "cJSON_AddItemToObject");
271 if (func == NULL) {
272 return 0;
273 }
274 cJSON_bool res = func(object, string, item);
275 return res;
276 }
277
cJSON_ReplaceItemInObject(cJSON * object,const char * string,cJSON * newitem)278 cJSON_bool cJSON_ReplaceItemInObject(cJSON*object, const char *string, cJSON*newitem)
279 {
280 g_replaceItemInObjectTime++;
281 if (g_replaceItemInObjectTime == 0 || g_replaceItemInObjectTime == CONST_TEN_TIMES) {
282 return 0;
283 }
284 if (g_handle == NULL) {
285 return 0;
286 }
287 cJSON_bool (*func)(cJSON*object, const char *string, cJSON*newitem);
288 func = (cJSON_bool (*)(cJSON*object, const char *string, cJSON*newitem))dlsym(
289 g_handle, "cJSON_ReplaceItemInObject");
290 if (func == NULL) {
291 return 0;
292 }
293 cJSON_bool res = func(object, string, newitem);
294 return res;
295 }
296
cJSON_GetArrayItem(const cJSON * array,int index)297 cJSON* cJSON_GetArrayItem(const cJSON* array, int index)
298 {
299 g_getArrayItemTime++;
300 if (g_getArrayItemTime == 0 ||
301 g_getArrayItemTime == CONST_TEN_TIMES ||
302 g_getArrayItemTime == CONST_TWENTY_TIMES) {
303 return NULL;
304 }
305 if (index < 0) {
306 return NULL;
307 }
308
309 if (g_handle == NULL) {
310 return NULL;
311 }
312 cJSON* (*func)(const cJSON* array, int index);
313 func = (cJSON* (*)(const cJSON* array, int index))dlsym(g_handle, "cJSON_GetArrayItem");
314 if (func == NULL) {
315 return NULL;
316 }
317 cJSON* res = func(array, index);
318 return res;
319 }
320
cJSON_Parse(const char * value)321 cJSON* cJSON_Parse(const char *value)
322 {
323 g_parse++;
324 if (g_parse == 0 ||
325 g_parse == CONST_TEN_TIMES) {
326 return NULL;
327 }
328 if (g_handle == NULL) {
329 return NULL;
330 }
331 cJSON* (*func)(const char *value);
332 func = (cJSON* (*)(const char *value))dlsym(g_handle, "cJSON_Parse");
333 if (func == NULL) {
334 return NULL;
335 }
336 cJSON* res = func(value);
337 return res;
338 }
339
cJSON_PrintUnformatted(const cJSON * item)340 char* cJSON_PrintUnformatted(const cJSON* item)
341 {
342 g_printUnformatted++;
343 if (g_printUnformatted == 0) {
344 return NULL;
345 }
346 if (g_handle == NULL) {
347 return NULL;
348 }
349 char* (*func)(const cJSON* item);
350 func = (char* (*)(const cJSON* item))dlsym(g_handle, "cJSON_PrintUnformatted");
351 if (func == NULL) {
352 return NULL;
353 }
354 char* res = func(item);
355 return res;
356 }
357
cJSON_free(void * object)358 void cJSON_free(void* object)
359 {
360 if (g_handle == NULL) {
361 return;
362 }
363 void (*func)(void* object);
364 func = (void (*)(void* object))dlsym(g_handle, "cJSON_free");
365 if (func == NULL) {
366 return;
367 }
368 func(object);
369 return;
370 }
371