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