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
38 #define CONST_TEN_TIMES 10
39 #define CONST_TWENTY_TIMES 20
40 #define CONST_THIRTY_TIMES 30
41 #define CONST_FORTY_TIMES 40
42 #define CONST_FIFTY_TIMES 50
43
GetHandle(void)44 static void* GetHandle(void)
45 {
46 #if defined(__LP64__)
47 void* handle = dlopen("/system/lib64/libcjson.z.so", RTLD_LAZY);
48 #else
49 void* handle = dlopen("/system/lib/libcjson.z.so", RTLD_LAZY);
50 #endif
51 return handle;
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 void* handle = GetHandle();
61 if (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(handle, "cJSON_GetObjectItem");
66 if (func == NULL) {
67 return NULL;
68 }
69 cJSON* res = func(object, string);
70 if (handle != NULL) {
71 dlclose(handle);
72 handle = NULL;
73 }
74 return res;
75 }
76
cJSON_IsNumber(const cJSON * const item)77 cJSON_bool cJSON_IsNumber(const cJSON* const item)
78 {
79 void* handle = GetHandle();
80 if (handle == NULL) {
81 return 0;
82 }
83 cJSON_bool (*func)(const cJSON* const item);
84 func = (cJSON_bool (*)(const cJSON* const item))dlsym(handle, "cJSON_IsNumber");
85 if (func == NULL) {
86 return 0;
87 }
88 cJSON_bool res = func(item);
89 if (handle != NULL) {
90 dlclose(handle);
91 handle = NULL;
92 }
93 return res;
94 }
95
cJSON_IsString(const cJSON * const item)96 cJSON_bool cJSON_IsString(const cJSON* const item)
97 {
98 g_isStringTime++;
99 if (g_isStringTime == 0) {
100 return 0;
101 }
102 void* handle = GetHandle();
103 if (handle == NULL) {
104 return 0;
105 }
106 cJSON_bool (*func)(const cJSON* const item);
107 func = (cJSON_bool (*)(const cJSON* const item))dlsym(handle, "cJSON_IsString");
108 if (func == NULL) {
109 return 0;
110 }
111 cJSON_bool res = func(item);
112 if (handle != NULL) {
113 dlclose(handle);
114 handle = NULL;
115 }
116 return res;
117 }
118
cJSON_GetNumberValue(const cJSON * const item)119 double cJSON_GetNumberValue(const cJSON* const item)
120 {
121 if (cJSON_IsNumber(item) == 0) {
122 return (double)0;
123 }
124
125 return item->valuedouble;
126 }
127
cJSON_GetArraySize(const cJSON * array)128 int cJSON_GetArraySize(const cJSON* array)
129 {
130 g_getArraySize++;
131 if (g_getArraySize == 0 ||
132 g_getArraySize == CONST_TEN_TIMES ||
133 g_getArraySize == CONST_TWENTY_TIMES) {
134 return 10000; // 10000 invalid
135 }
136 cJSON* child = NULL;
137 size_t size = 0;
138 if (array == NULL) {
139 return 0;
140 }
141 child = array->child;
142 while (child != NULL) {
143 size++;
144 child = child->next;
145 }
146 return (int)size;
147 }
148
cJSON_CreateArray(void)149 cJSON* cJSON_CreateArray(void)
150 {
151 g_createArrayTime++;
152 if (g_createArrayTime == 0) {
153 return NULL;
154 }
155 void* handle = GetHandle();
156 if (handle == NULL) {
157 return NULL;
158 }
159 cJSON* (*func)(void);
160 func = (cJSON* (*)(void))dlsym(handle, "cJSON_CreateArray");
161 if (func == NULL) {
162 return NULL;
163 }
164 cJSON* res = func();
165 if (handle != NULL) {
166 dlclose(handle);
167 handle = NULL;
168 }
169 return res;
170 }
171
cJSON_CreateObject(void)172 cJSON* cJSON_CreateObject(void)
173 {
174 g_createObject++;
175 if (g_createObject == 0) {
176 return NULL;
177 }
178 void* handle = GetHandle();
179 if (handle == NULL) {
180 return NULL;
181 }
182 cJSON* (*func)(void);
183 func = (cJSON* (*)(void))dlsym(handle, "cJSON_CreateObject");
184 if (func == NULL) {
185 return NULL;
186 }
187 cJSON* res = func();
188 if (handle != NULL) {
189 dlclose(handle);
190 handle = NULL;
191 }
192 return res;
193 }
194
cJSON_CreateNumber(double num)195 cJSON* cJSON_CreateNumber(double num)
196 {
197 g_createNumberTime++;
198 if (g_createNumberTime == 0 ||
199 g_createNumberTime == CONST_TEN_TIMES ||
200 g_createNumberTime == CONST_TWENTY_TIMES ||
201 g_createNumberTime == CONST_THIRTY_TIMES) {
202 return NULL;
203 }
204 void* handle = GetHandle();
205 if (handle == NULL) {
206 return NULL;
207 }
208 cJSON* (*func)(double num);
209 func = (cJSON* (*)(double num))dlsym(handle, "cJSON_CreateNumber");
210 if (func == NULL) {
211 return NULL;
212 }
213 cJSON* res = func(num);
214 if (handle != NULL) {
215 dlclose(handle);
216 handle = NULL;
217 }
218 return res;
219 }
220
cJSON_CreateString(const char * string)221 cJSON* cJSON_CreateString(const char *string)
222 {
223 g_createStringTime++;
224 if (g_createStringTime == 0 ||
225 g_createStringTime == CONST_TEN_TIMES) {
226 return NULL;
227 }
228 if (string != NULL && strcmp(string, "processUnique") == 0) {
229 printf("processUnique failed\n");
230 return NULL;
231 }
232 void* handle = GetHandle();
233 if (handle == NULL) {
234 return NULL;
235 }
236 cJSON* (*func)(const char *string);
237 func = (cJSON* (*)(const char *string))dlsym(handle, "cJSON_CreateString");
238 if (func == NULL) {
239 return NULL;
240 }
241 cJSON* res = func(string);
242 if (handle != NULL) {
243 dlclose(handle);
244 handle = NULL;
245 }
246 return res;
247 }
248
cJSON_AddItemToArray(cJSON * array,cJSON * item)249 cJSON_bool cJSON_AddItemToArray(cJSON* array, cJSON* item)
250 {
251 g_addItemToArray++;
252 if (g_addItemToArray == 0) {
253 return 0;
254 }
255 void* handle = GetHandle();
256 if (handle == NULL) {
257 return 0;
258 }
259 cJSON_bool (*func)(cJSON* array, cJSON* item);
260 func = (cJSON_bool (*)(cJSON* array, cJSON* item))dlsym(handle, "cJSON_AddItemToArray");
261 if (func == NULL) {
262 return 0;
263 }
264 cJSON_bool res = func(array, item);
265 if (handle != NULL) {
266 dlclose(handle);
267 handle = NULL;
268 }
269 return res;
270 }
271
cJSON_Delete(cJSON * item)272 void cJSON_Delete(cJSON* item)
273 {
274 void* handle = GetHandle();
275 if (handle == NULL) {
276 return;
277 }
278 void (*func)(cJSON* item);
279 func = (void (*)(cJSON* item))dlsym(handle, "cJSON_Delete");
280 if (func == NULL) {
281 return;
282 }
283 func(item);
284 if (handle != NULL) {
285 dlclose(handle);
286 handle = NULL;
287 }
288 return;
289 }
290
cJSON_AddItemToObject(cJSON * object,const char * string,cJSON * item)291 cJSON_bool cJSON_AddItemToObject(cJSON*object, const char *string, cJSON* item)
292 {
293 g_addItemToObject++;
294 if (g_addItemToObject == 0 ||
295 g_addItemToObject == CONST_TEN_TIMES ||
296 g_addItemToObject == CONST_TWENTY_TIMES ||
297 g_addItemToObject == CONST_THIRTY_TIMES ||
298 g_addItemToObject == CONST_FORTY_TIMES ||
299 g_addItemToObject == CONST_FIFTY_TIMES) {
300 return 0;
301 }
302 void* handle = GetHandle();
303 if (handle == NULL) {
304 return 0;
305 }
306 cJSON_bool (*func)(cJSON*object, const char *string, cJSON* item);
307 func = (cJSON_bool (*)(cJSON*object, const char *string, cJSON* item))dlsym(handle, "cJSON_AddItemToObject");
308 if (func == NULL) {
309 return 0;
310 }
311 cJSON_bool res = func(object, string, item);
312 if (handle != NULL) {
313 dlclose(handle);
314 handle = NULL;
315 }
316 return res;
317 }
318
cJSON_ReplaceItemInObject(cJSON * object,const char * string,cJSON * newitem)319 cJSON_bool cJSON_ReplaceItemInObject(cJSON*object, const char *string, cJSON*newitem)
320 {
321 g_replaceItemInObjectTime++;
322 if (g_replaceItemInObjectTime == 0 || g_replaceItemInObjectTime == CONST_TEN_TIMES) {
323 return 0;
324 }
325 void* handle = GetHandle();
326 if (handle == NULL) {
327 return 0;
328 }
329 cJSON_bool (*func)(cJSON*object, const char *string, cJSON*newitem);
330 func = (cJSON_bool (*)(cJSON*object, const char *string, cJSON*newitem))dlsym(handle, "cJSON_ReplaceItemInObject");
331 if (func == NULL) {
332 return 0;
333 }
334 cJSON_bool res = func(object, string, newitem);
335 if (handle != NULL) {
336 dlclose(handle);
337 handle = NULL;
338 }
339 return res;
340 }
341
cJSON_GetArrayItem(const cJSON * array,int index)342 cJSON* cJSON_GetArrayItem(const cJSON* array, int index)
343 {
344 g_getArrayItemTime++;
345 if (g_getArrayItemTime == 0 ||
346 g_getArrayItemTime == CONST_TEN_TIMES ||
347 g_getArrayItemTime == CONST_TWENTY_TIMES) {
348 return NULL;
349 }
350 if (index < 0) {
351 return NULL;
352 }
353
354 void* handle = GetHandle();
355 if (handle == NULL) {
356 return NULL;
357 }
358 cJSON* (*func)(const cJSON* array, int index);
359 func = (cJSON* (*)(const cJSON* array, int index))dlsym(handle, "cJSON_GetArrayItem");
360 if (func == NULL) {
361 return NULL;
362 }
363 cJSON* res = func(array, index);
364 if (handle != NULL) {
365 dlclose(handle);
366 handle = NULL;
367 }
368 return res;
369 }
370
cJSON_Parse(const char * value)371 cJSON* cJSON_Parse(const char *value)
372 {
373 g_parse++;
374 if (g_parse == 0 ||
375 g_parse == CONST_TEN_TIMES) {
376 return NULL;
377 }
378 void* handle = GetHandle();
379 if (handle == NULL) {
380 return NULL;
381 }
382 cJSON* (*func)(const char *value);
383 func = (cJSON* (*)(const char *value))dlsym(handle, "cJSON_Parse");
384 if (func == NULL) {
385 return NULL;
386 }
387 cJSON* res = func(value);
388 if (handle != NULL) {
389 dlclose(handle);
390 handle = NULL;
391 }
392 return res;
393 }
394
cJSON_PrintUnformatted(const cJSON * item)395 char* cJSON_PrintUnformatted(const cJSON* item)
396 {
397 g_printUnformatted++;
398 if (g_printUnformatted == 0) {
399 return NULL;
400 }
401 void* handle = GetHandle();
402 if (handle == NULL) {
403 return NULL;
404 }
405 char* (*func)(const cJSON* item);
406 func = (char* (*)(const cJSON* item))dlsym(handle, "cJSON_PrintUnformatted");
407 if (func == NULL) {
408 return NULL;
409 }
410 char* res = func(item);
411 if (handle != NULL) {
412 dlclose(handle);
413 handle = NULL;
414 }
415 return res;
416 }
417
cJSON_free(void * object)418 void cJSON_free(void* object)
419 {
420 void* handle = GetHandle();
421 if (handle == NULL) {
422 return;
423 }
424 void (*func)(void* object);
425 func = (void (*)(void* object))dlsym(handle, "cJSON_free");
426 if (func == NULL) {
427 return;
428 }
429 func(object);
430 if (handle != NULL) {
431 dlclose(handle);
432 handle = NULL;
433 }
434 return;
435 }
436