/src/json-c/json_object.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
3 | | * Michael Clark <michael@metaparadigm.com> |
4 | | * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the MIT license. See COPYING for details. |
8 | | * |
9 | | */ |
10 | | |
11 | | #include "config.h" |
12 | | |
13 | | #include "strerror_override.h" |
14 | | |
15 | | #include <assert.h> |
16 | | #ifdef HAVE_LIMITS_H |
17 | | #include <limits.h> |
18 | | #endif |
19 | | #include <math.h> |
20 | | #include <stddef.h> |
21 | | #include <stdio.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | |
25 | | #include "arraylist.h" |
26 | | #include "debug.h" |
27 | | #include "json_inttypes.h" |
28 | | #include "json_object.h" |
29 | | #include "json_object_private.h" |
30 | | #include "json_util.h" |
31 | | #include "linkhash.h" |
32 | | #include "math_compat.h" |
33 | | #include "printbuf.h" |
34 | | #include "snprintf_compat.h" |
35 | | #include "strdup_compat.h" |
36 | | |
37 | | /* Avoid ctype.h and locale overhead */ |
38 | 0 | #define is_plain_digit(c) ((c) >= '0' && (c) <= '9') |
39 | | |
40 | | #if SIZEOF_LONG_LONG != SIZEOF_INT64_T |
41 | | #error The long long type is not 64-bits |
42 | | #endif |
43 | | |
44 | | #ifndef SSIZE_T_MAX |
45 | | #if SIZEOF_SSIZE_T == SIZEOF_INT |
46 | | #define SSIZE_T_MAX INT_MAX |
47 | | #elif SIZEOF_SSIZE_T == SIZEOF_LONG |
48 | 1.22k | #define SSIZE_T_MAX LONG_MAX |
49 | | #elif SIZEOF_SSIZE_T == SIZEOF_LONG_LONG |
50 | | #define SSIZE_T_MAX LLONG_MAX |
51 | | #else |
52 | | #error Unable to determine size of ssize_t |
53 | | #endif |
54 | | #endif |
55 | | |
56 | | const char *json_number_chars = "0123456789.+-eE"; /* Unused, but part of public API, drop for 1.0 */ |
57 | | const char *json_hex_chars = "0123456789abcdefABCDEF"; |
58 | | |
59 | | static void json_object_generic_delete(struct json_object *jso); |
60 | | |
61 | | #if defined(_MSC_VER) && (_MSC_VER <= 1800) |
62 | | /* VS2013 doesn't know about "inline" */ |
63 | | #define inline __inline |
64 | | #elif defined(AIX_CC) |
65 | | #define inline |
66 | | #endif |
67 | | |
68 | | /* define colors */ |
69 | | #define ANSI_COLOR_RESET "\033[0m" |
70 | | #define ANSI_COLOR_FG_GREEN "\033[0;32m" |
71 | | #define ANSI_COLOR_FG_BLUE "\033[0;34m" |
72 | | #define ANSI_COLOR_FG_MAGENTA "\033[0;35m" |
73 | | |
74 | | /* |
75 | | * Helper functions to more safely cast to a particular type of json_object |
76 | | */ |
77 | | static inline struct json_object_object *JC_OBJECT(struct json_object *jso) |
78 | 81.3k | { |
79 | 81.3k | return (void *)jso; |
80 | 81.3k | } |
81 | | static inline const struct json_object_object *JC_OBJECT_C(const struct json_object *jso) |
82 | 0 | { |
83 | 0 | return (const void *)jso; |
84 | 0 | } |
85 | | static inline struct json_object_array *JC_ARRAY(struct json_object *jso) |
86 | 3.70M | { |
87 | 3.70M | return (void *)jso; |
88 | 3.70M | } |
89 | | static inline const struct json_object_array *JC_ARRAY_C(const struct json_object *jso) |
90 | 0 | { |
91 | 0 | return (const void *)jso; |
92 | 0 | } |
93 | | static inline struct json_object_boolean *JC_BOOL(struct json_object *jso) |
94 | 0 | { |
95 | 0 | return (void *)jso; |
96 | 0 | } |
97 | | static inline const struct json_object_boolean *JC_BOOL_C(const struct json_object *jso) |
98 | 0 | { |
99 | 0 | return (const void *)jso; |
100 | 0 | } |
101 | | static inline struct json_object_double *JC_DOUBLE(struct json_object *jso) |
102 | 0 | { |
103 | 0 | return (void *)jso; |
104 | 0 | } |
105 | | static inline const struct json_object_double *JC_DOUBLE_C(const struct json_object *jso) |
106 | 0 | { |
107 | 0 | return (const void *)jso; |
108 | 0 | } |
109 | | static inline struct json_object_int *JC_INT(struct json_object *jso) |
110 | 0 | { |
111 | 0 | return (void *)jso; |
112 | 0 | } |
113 | | static inline const struct json_object_int *JC_INT_C(const struct json_object *jso) |
114 | 0 | { |
115 | 0 | return (const void *)jso; |
116 | 0 | } |
117 | | static inline struct json_object_string *JC_STRING(struct json_object *jso) |
118 | 1.22k | { |
119 | 1.22k | return (void *)jso; |
120 | 1.22k | } |
121 | | static inline const struct json_object_string *JC_STRING_C(const struct json_object *jso) |
122 | 0 | { |
123 | 0 | return (const void *)jso; |
124 | 0 | } |
125 | | |
126 | 3.72M | #define JC_CONCAT(a, b) a##b |
127 | 3.72M | #define JC_CONCAT3(a, b, c) a##b##c |
128 | | |
129 | | #define JSON_OBJECT_NEW(jtype) \ |
130 | 3.72M | (struct JC_CONCAT(json_object_, jtype) *)json_object_new( \ |
131 | 3.72M | JC_CONCAT(json_type_, jtype), sizeof(struct JC_CONCAT(json_object_, jtype)), \ |
132 | 3.72M | &JC_CONCAT3(json_object_, jtype, _to_json_string)) |
133 | | |
134 | | static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size, |
135 | | json_object_to_json_string_fn *to_json_string); |
136 | | |
137 | | static void json_object_object_delete(struct json_object *jso_base); |
138 | | static void json_object_string_delete(struct json_object *jso); |
139 | | static void json_object_array_delete(struct json_object *jso); |
140 | | |
141 | | static json_object_to_json_string_fn json_object_object_to_json_string; |
142 | | static json_object_to_json_string_fn json_object_boolean_to_json_string; |
143 | | static json_object_to_json_string_fn json_object_double_to_json_string_default; |
144 | | static json_object_to_json_string_fn json_object_int_to_json_string; |
145 | | static json_object_to_json_string_fn json_object_string_to_json_string; |
146 | | static json_object_to_json_string_fn json_object_array_to_json_string; |
147 | | static json_object_to_json_string_fn _json_object_userdata_to_json_string; |
148 | | |
149 | | #ifndef JSON_NORETURN |
150 | | #if defined(_MSC_VER) |
151 | | #define JSON_NORETURN __declspec(noreturn) |
152 | | #elif defined(__OS400__) |
153 | | #define JSON_NORETURN |
154 | | #else |
155 | | /* 'cold' attribute is for optimization, telling the computer this code |
156 | | * path is unlikely. |
157 | | */ |
158 | | #define JSON_NORETURN __attribute__((noreturn, cold)) |
159 | | #endif |
160 | | #endif |
161 | | /** |
162 | | * Abort and optionally print a message on standard error. |
163 | | * This should be used rather than assert() for unconditional abortion |
164 | | * (in particular for code paths which are never supposed to be run). |
165 | | * */ |
166 | | JSON_NORETURN static void json_abort(const char *message); |
167 | | |
168 | | /* helper for accessing the optimized string data component in json_object |
169 | | */ |
170 | | static inline char *get_string_component_mutable(struct json_object *jso) |
171 | 0 | { |
172 | 0 | if (JC_STRING_C(jso)->len < 0) |
173 | 0 | { |
174 | | /* Due to json_object_set_string(), we might have a pointer */ |
175 | 0 | return JC_STRING(jso)->c_string.pdata; |
176 | 0 | } |
177 | 0 | return JC_STRING(jso)->c_string.idata; |
178 | 0 | } |
179 | | static inline const char *get_string_component(const struct json_object *jso) |
180 | 0 | { |
181 | 0 | return get_string_component_mutable((void *)(uintptr_t)(const void *)jso); |
182 | 0 | } |
183 | | |
184 | | /* string escaping */ |
185 | | |
186 | | static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int flags) |
187 | 0 | { |
188 | 0 | size_t pos = 0, start_offset = 0; |
189 | 0 | unsigned char c; |
190 | 0 | while (len) |
191 | 0 | { |
192 | 0 | --len; |
193 | 0 | c = str[pos]; |
194 | 0 | switch (c) |
195 | 0 | { |
196 | 0 | case '\b': |
197 | 0 | case '\n': |
198 | 0 | case '\r': |
199 | 0 | case '\t': |
200 | 0 | case '\f': |
201 | 0 | case '"': |
202 | 0 | case '\\': |
203 | 0 | case '/': |
204 | 0 | if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/') |
205 | 0 | { |
206 | 0 | pos++; |
207 | 0 | break; |
208 | 0 | } |
209 | | |
210 | 0 | if (pos > start_offset) |
211 | 0 | printbuf_memappend(pb, str + start_offset, pos - start_offset); |
212 | |
|
213 | 0 | if (c == '\b') |
214 | 0 | printbuf_memappend(pb, "\\b", 2); |
215 | 0 | else if (c == '\n') |
216 | 0 | printbuf_memappend(pb, "\\n", 2); |
217 | 0 | else if (c == '\r') |
218 | 0 | printbuf_memappend(pb, "\\r", 2); |
219 | 0 | else if (c == '\t') |
220 | 0 | printbuf_memappend(pb, "\\t", 2); |
221 | 0 | else if (c == '\f') |
222 | 0 | printbuf_memappend(pb, "\\f", 2); |
223 | 0 | else if (c == '"') |
224 | 0 | printbuf_memappend(pb, "\\\"", 2); |
225 | 0 | else if (c == '\\') |
226 | 0 | printbuf_memappend(pb, "\\\\", 2); |
227 | 0 | else if (c == '/') |
228 | 0 | printbuf_memappend(pb, "\\/", 2); |
229 | |
|
230 | 0 | start_offset = ++pos; |
231 | 0 | break; |
232 | 0 | default: |
233 | 0 | if (c < ' ') |
234 | 0 | { |
235 | 0 | char sbuf[7]; |
236 | 0 | if (pos > start_offset) |
237 | 0 | printbuf_memappend(pb, str + start_offset, |
238 | 0 | pos - start_offset); |
239 | 0 | snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4], |
240 | 0 | json_hex_chars[c & 0xf]); |
241 | 0 | printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1); |
242 | 0 | start_offset = ++pos; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | pos++; |
246 | 0 | } |
247 | 0 | } |
248 | 0 | if (pos > start_offset) |
249 | 0 | printbuf_memappend(pb, str + start_offset, pos - start_offset); |
250 | 0 | return 0; |
251 | 0 | } |
252 | | |
253 | | /* reference counting */ |
254 | | |
255 | | struct json_object *json_object_get(struct json_object *jso) |
256 | 3.72M | { |
257 | 3.72M | if (!jso) |
258 | 896 | return jso; |
259 | | |
260 | | // Don't overflow the refcounter. |
261 | 3.72M | assert(jso->_ref_count < UINT32_MAX); |
262 | | |
263 | | #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING) |
264 | | __sync_add_and_fetch(&jso->_ref_count, 1); |
265 | | #else |
266 | 3.72M | ++jso->_ref_count; |
267 | 3.72M | #endif |
268 | | |
269 | 3.72M | return jso; |
270 | 3.72M | } |
271 | | |
272 | | int json_object_put(struct json_object *jso) |
273 | 11.1M | { |
274 | 11.1M | if (!jso) |
275 | 3.73M | return 0; |
276 | | |
277 | | /* Avoid invalid free and crash explicitly instead of (silently) |
278 | | * segfaulting. |
279 | | */ |
280 | 7.45M | assert(jso->_ref_count > 0); |
281 | | |
282 | | #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING) |
283 | | /* Note: this only allow the refcount to remain correct |
284 | | * when multiple threads are adjusting it. It is still an error |
285 | | * for a thread to decrement the refcount if it doesn't "own" it, |
286 | | * as that can result in the thread that loses the race to 0 |
287 | | * operating on an already-freed object. |
288 | | */ |
289 | | if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) |
290 | | return 0; |
291 | | #else |
292 | 7.45M | if (--jso->_ref_count > 0) |
293 | 3.72M | return 0; |
294 | 3.72M | #endif |
295 | | |
296 | 3.72M | if (jso->_user_delete) |
297 | 2.54k | jso->_user_delete(jso, jso->_userdata); |
298 | 3.72M | switch (jso->o_type) |
299 | 3.72M | { |
300 | 3.80k | case json_type_object: json_object_object_delete(jso); break; |
301 | 4.23k | case json_type_array: json_object_array_delete(jso); break; |
302 | 1.22k | case json_type_string: json_object_string_delete(jso); break; |
303 | 3.72M | default: json_object_generic_delete(jso); break; |
304 | 3.72M | } |
305 | 3.72M | return 1; |
306 | 3.72M | } |
307 | | |
308 | | /* generic object construction and destruction parts */ |
309 | | |
310 | | static void json_object_generic_delete(struct json_object *jso) |
311 | 3.72M | { |
312 | 3.72M | printbuf_free(jso->_pb); |
313 | 3.72M | free(jso); |
314 | 3.72M | } |
315 | | |
316 | | static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size, |
317 | | json_object_to_json_string_fn *to_json_string) |
318 | 3.72M | { |
319 | 3.72M | struct json_object *jso; |
320 | | |
321 | 3.72M | jso = (struct json_object *)malloc(alloc_size); |
322 | 3.72M | if (!jso) |
323 | 0 | return NULL; |
324 | | |
325 | 3.72M | jso->o_type = o_type; |
326 | 3.72M | jso->_ref_count = 1; |
327 | 3.72M | jso->_to_json_string = to_json_string; |
328 | 3.72M | jso->_pb = NULL; |
329 | 3.72M | jso->_user_delete = NULL; |
330 | 3.72M | jso->_userdata = NULL; |
331 | | //jso->... // Type-specific fields must be set by caller |
332 | | |
333 | 3.72M | return jso; |
334 | 3.72M | } |
335 | | |
336 | | /* type checking functions */ |
337 | | |
338 | | int json_object_is_type(const struct json_object *jso, enum json_type type) |
339 | 0 | { |
340 | 0 | if (!jso) |
341 | 0 | return (type == json_type_null); |
342 | 0 | return (jso->o_type == type); |
343 | 0 | } |
344 | | |
345 | | enum json_type json_object_get_type(const struct json_object *jso) |
346 | 3.72M | { |
347 | 3.72M | if (!jso) |
348 | 0 | return json_type_null; |
349 | 3.72M | return jso->o_type; |
350 | 3.72M | } |
351 | | |
352 | | void *json_object_get_userdata(json_object *jso) |
353 | 0 | { |
354 | 0 | return jso ? jso->_userdata : NULL; |
355 | 0 | } |
356 | | |
357 | | void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete) |
358 | 2.54k | { |
359 | | // Can't return failure, so abort if we can't perform the operation. |
360 | 2.54k | assert(jso != NULL); |
361 | | |
362 | | // First, clean up any previously existing user info |
363 | 2.54k | if (jso->_user_delete) |
364 | 0 | jso->_user_delete(jso, jso->_userdata); |
365 | | |
366 | 2.54k | jso->_userdata = userdata; |
367 | 2.54k | jso->_user_delete = user_delete; |
368 | 2.54k | } |
369 | | |
370 | | /* set a custom conversion to string */ |
371 | | |
372 | | void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func, |
373 | | void *userdata, json_object_delete_fn *user_delete) |
374 | 2.54k | { |
375 | 2.54k | json_object_set_userdata(jso, userdata, user_delete); |
376 | | |
377 | 2.54k | if (to_string_func == NULL) |
378 | 0 | { |
379 | | // Reset to the standard serialization function |
380 | 0 | switch (jso->o_type) |
381 | 0 | { |
382 | 0 | case json_type_null: jso->_to_json_string = NULL; break; |
383 | 0 | case json_type_boolean: |
384 | 0 | jso->_to_json_string = &json_object_boolean_to_json_string; |
385 | 0 | break; |
386 | 0 | case json_type_double: |
387 | 0 | jso->_to_json_string = &json_object_double_to_json_string_default; |
388 | 0 | break; |
389 | 0 | case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break; |
390 | 0 | case json_type_object: |
391 | 0 | jso->_to_json_string = &json_object_object_to_json_string; |
392 | 0 | break; |
393 | 0 | case json_type_array: |
394 | 0 | jso->_to_json_string = &json_object_array_to_json_string; |
395 | 0 | break; |
396 | 0 | case json_type_string: |
397 | 0 | jso->_to_json_string = &json_object_string_to_json_string; |
398 | 0 | break; |
399 | 0 | } |
400 | 0 | return; |
401 | 0 | } |
402 | | |
403 | 2.54k | jso->_to_json_string = to_string_func; |
404 | 2.54k | } |
405 | | |
406 | | /* extended conversion to string */ |
407 | | |
408 | | const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length) |
409 | 0 | { |
410 | 0 | const char *r = NULL; |
411 | 0 | size_t s = 0; |
412 | |
|
413 | 0 | if (!jso) |
414 | 0 | { |
415 | 0 | s = 4; |
416 | 0 | r = "null"; |
417 | 0 | } |
418 | 0 | else if ((jso->_pb) || (jso->_pb = printbuf_new())) |
419 | 0 | { |
420 | 0 | printbuf_reset(jso->_pb); |
421 | |
|
422 | 0 | if (jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0) |
423 | 0 | { |
424 | 0 | s = (size_t)jso->_pb->bpos; |
425 | 0 | r = jso->_pb->buf; |
426 | 0 | } |
427 | 0 | } |
428 | |
|
429 | 0 | if (length) |
430 | 0 | *length = s; |
431 | 0 | return r; |
432 | 0 | } |
433 | | |
434 | | const char *json_object_to_json_string_ext(struct json_object *jso, int flags) |
435 | 0 | { |
436 | 0 | return json_object_to_json_string_length(jso, flags, NULL); |
437 | 0 | } |
438 | | |
439 | | /* backwards-compatible conversion to string */ |
440 | | |
441 | | const char *json_object_to_json_string(struct json_object *jso) |
442 | 0 | { |
443 | 0 | return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED); |
444 | 0 | } |
445 | | |
446 | | static void indent(struct printbuf *pb, int level, int flags) |
447 | 0 | { |
448 | 0 | if (flags & JSON_C_TO_STRING_PRETTY) |
449 | 0 | { |
450 | 0 | if (flags & JSON_C_TO_STRING_PRETTY_TAB) |
451 | 0 | { |
452 | 0 | printbuf_memset(pb, -1, '\t', level); |
453 | 0 | } |
454 | 0 | else |
455 | 0 | { |
456 | 0 | printbuf_memset(pb, -1, ' ', level * 2); |
457 | 0 | } |
458 | 0 | } |
459 | 0 | } |
460 | | |
461 | | /* json_object_object */ |
462 | | |
463 | | static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb, |
464 | | int level, int flags) |
465 | 0 | { |
466 | 0 | int had_children = 0; |
467 | 0 | struct json_object_iter iter; |
468 | |
|
469 | 0 | printbuf_strappend(pb, "{" /*}*/); |
470 | 0 | json_object_object_foreachC(jso, iter) |
471 | 0 | { |
472 | 0 | if (had_children) |
473 | 0 | { |
474 | 0 | printbuf_strappend(pb, ","); |
475 | 0 | } |
476 | 0 | if (flags & JSON_C_TO_STRING_PRETTY) |
477 | 0 | printbuf_strappend(pb, "\n"); |
478 | 0 | had_children = 1; |
479 | 0 | if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
480 | 0 | printbuf_strappend(pb, " "); |
481 | 0 | indent(pb, level + 1, flags); |
482 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
483 | 0 | printbuf_strappend(pb, ANSI_COLOR_FG_BLUE); |
484 | |
|
485 | 0 | printbuf_strappend(pb, "\""); |
486 | 0 | json_escape_str(pb, iter.key, strlen(iter.key), flags); |
487 | 0 | printbuf_strappend(pb, "\""); |
488 | |
|
489 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
490 | 0 | printbuf_strappend(pb, ANSI_COLOR_RESET); |
491 | |
|
492 | 0 | if (flags & JSON_C_TO_STRING_SPACED) |
493 | 0 | printbuf_strappend(pb, ": "); |
494 | 0 | else |
495 | 0 | printbuf_strappend(pb, ":"); |
496 | |
|
497 | 0 | if (iter.val == NULL) { |
498 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
499 | 0 | printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA); |
500 | 0 | printbuf_strappend(pb, "null"); |
501 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
502 | 0 | printbuf_strappend(pb, ANSI_COLOR_RESET); |
503 | 0 | } else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0) |
504 | 0 | return -1; |
505 | 0 | } |
506 | 0 | if ((flags & JSON_C_TO_STRING_PRETTY) && had_children) |
507 | 0 | { |
508 | 0 | printbuf_strappend(pb, "\n"); |
509 | 0 | indent(pb, level, flags); |
510 | 0 | } |
511 | 0 | if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
512 | 0 | return printbuf_strappend(pb, /*{*/ " }"); |
513 | 0 | else |
514 | 0 | return printbuf_strappend(pb, /*{*/ "}"); |
515 | 0 | } |
516 | | |
517 | | static void json_object_lh_entry_free(struct lh_entry *ent) |
518 | 21.4k | { |
519 | 21.4k | if (!lh_entry_k_is_constant(ent)) |
520 | 21.4k | free(lh_entry_k(ent)); |
521 | 21.4k | json_object_put((struct json_object *)lh_entry_v(ent)); |
522 | 21.4k | } |
523 | | |
524 | | static void json_object_object_delete(struct json_object *jso_base) |
525 | 3.80k | { |
526 | 3.80k | lh_table_free(JC_OBJECT(jso_base)->c_object); |
527 | 3.80k | json_object_generic_delete(jso_base); |
528 | 3.80k | } |
529 | | |
530 | | struct json_object *json_object_new_object(void) |
531 | 3.80k | { |
532 | 3.80k | struct json_object_object *jso = JSON_OBJECT_NEW(object); |
533 | 3.80k | if (!jso) |
534 | 0 | return NULL; |
535 | 3.80k | jso->c_object = |
536 | 3.80k | lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free); |
537 | 3.80k | if (!jso->c_object) |
538 | 0 | { |
539 | 0 | json_object_generic_delete(&jso->base); |
540 | 0 | errno = ENOMEM; |
541 | 0 | return NULL; |
542 | 0 | } |
543 | 3.80k | return &jso->base; |
544 | 3.80k | } |
545 | | |
546 | | struct lh_table *json_object_get_object(const struct json_object *jso) |
547 | 0 | { |
548 | 0 | if (!jso) |
549 | 0 | return NULL; |
550 | 0 | switch (jso->o_type) |
551 | 0 | { |
552 | 0 | case json_type_object: return JC_OBJECT_C(jso)->c_object; |
553 | 0 | default: return NULL; |
554 | 0 | } |
555 | 0 | } |
556 | | |
557 | | int json_object_object_add_ex(struct json_object *jso, const char *const key, |
558 | | struct json_object *const val, const unsigned opts) |
559 | 28.0k | { |
560 | 28.0k | struct json_object *existing_value = NULL; |
561 | 28.0k | struct lh_entry *existing_entry; |
562 | 28.0k | unsigned long hash; |
563 | | |
564 | 28.0k | assert(json_object_get_type(jso) == json_type_object); |
565 | | |
566 | | // We lookup the entry and replace the value, rather than just deleting |
567 | | // and re-adding it, so the existing key remains valid. |
568 | 28.0k | hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key); |
569 | 28.0k | existing_entry = |
570 | 28.0k | (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) |
571 | 28.0k | ? NULL |
572 | 28.0k | : lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash); |
573 | | |
574 | | // The caller must avoid creating loops in the object tree, but do a |
575 | | // quick check anyway to make sure we're not creating a trivial loop. |
576 | 28.0k | if (jso == val) |
577 | 0 | return -1; |
578 | | |
579 | 28.0k | if (!existing_entry) |
580 | 21.4k | { |
581 | 21.4k | const void *const k = |
582 | 21.4k | (opts & JSON_C_OBJECT_ADD_CONSTANT_KEY) ? (const void *)key : strdup(key); |
583 | 21.4k | if (k == NULL) |
584 | 0 | return -1; |
585 | 21.4k | return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts); |
586 | 21.4k | } |
587 | 6.64k | existing_value = (json_object *)lh_entry_v(existing_entry); |
588 | 6.64k | if (existing_value) |
589 | 6.28k | json_object_put(existing_value); |
590 | 6.64k | lh_entry_set_val(existing_entry, val); |
591 | 6.64k | return 0; |
592 | 28.0k | } |
593 | | |
594 | | int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val) |
595 | 28.0k | { |
596 | 28.0k | return json_object_object_add_ex(jso, key, val, 0); |
597 | 28.0k | } |
598 | | |
599 | | int json_object_object_length(const struct json_object *jso) |
600 | 0 | { |
601 | 0 | assert(json_object_get_type(jso) == json_type_object); |
602 | 0 | return lh_table_length(JC_OBJECT_C(jso)->c_object); |
603 | 0 | } |
604 | | |
605 | | size_t json_c_object_sizeof(void) |
606 | 0 | { |
607 | 0 | return sizeof(struct json_object); |
608 | 0 | } |
609 | | |
610 | | struct json_object *json_object_object_get(const struct json_object *jso, const char *key) |
611 | 0 | { |
612 | 0 | struct json_object *result = NULL; |
613 | 0 | json_object_object_get_ex(jso, key, &result); |
614 | 0 | return result; |
615 | 0 | } |
616 | | |
617 | | json_bool json_object_object_get_ex(const struct json_object *jso, const char *key, |
618 | | struct json_object **value) |
619 | 0 | { |
620 | 0 | if (value != NULL) |
621 | 0 | *value = NULL; |
622 | |
|
623 | 0 | if (NULL == jso) |
624 | 0 | return 0; |
625 | | |
626 | 0 | switch (jso->o_type) |
627 | 0 | { |
628 | 0 | case json_type_object: |
629 | 0 | return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key, |
630 | 0 | (void **)value); |
631 | 0 | default: |
632 | 0 | if (value != NULL) |
633 | 0 | *value = NULL; |
634 | 0 | return 0; |
635 | 0 | } |
636 | 0 | } |
637 | | |
638 | | void json_object_object_del(struct json_object *jso, const char *key) |
639 | 0 | { |
640 | 0 | assert(json_object_get_type(jso) == json_type_object); |
641 | 0 | lh_table_delete(JC_OBJECT(jso)->c_object, key); |
642 | 0 | } |
643 | | |
644 | | /* json_object_boolean */ |
645 | | |
646 | | static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb, |
647 | | int level, int flags) |
648 | 0 | { |
649 | 0 | int ret; |
650 | |
|
651 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
652 | 0 | printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA); |
653 | |
|
654 | 0 | if (JC_BOOL(jso)->c_boolean) |
655 | 0 | ret = printbuf_strappend(pb, "true"); |
656 | 0 | else |
657 | 0 | ret = printbuf_strappend(pb, "false"); |
658 | 0 | if (ret > -1 && flags & JSON_C_TO_STRING_COLOR) |
659 | 0 | return printbuf_strappend(pb, ANSI_COLOR_RESET); |
660 | 0 | return ret; |
661 | 0 | } |
662 | | |
663 | | struct json_object *json_object_new_boolean(json_bool b) |
664 | 1.46k | { |
665 | 1.46k | struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean); |
666 | 1.46k | if (!jso) |
667 | 0 | return NULL; |
668 | 1.46k | jso->c_boolean = b; |
669 | 1.46k | return &jso->base; |
670 | 1.46k | } |
671 | | |
672 | | json_bool json_object_get_boolean(const struct json_object *jso) |
673 | 0 | { |
674 | 0 | if (!jso) |
675 | 0 | return 0; |
676 | 0 | switch (jso->o_type) |
677 | 0 | { |
678 | 0 | case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
679 | 0 | case json_type_int: |
680 | 0 | switch (JC_INT_C(jso)->cint_type) |
681 | 0 | { |
682 | 0 | case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0); |
683 | 0 | case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0); |
684 | 0 | default: json_abort("invalid cint_type"); |
685 | 0 | } |
686 | 0 | case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0); |
687 | 0 | case json_type_string: return (JC_STRING_C(jso)->len != 0); |
688 | 0 | default: return 0; |
689 | 0 | } |
690 | 0 | } |
691 | | |
692 | | int json_object_set_boolean(struct json_object *jso, json_bool new_value) |
693 | 0 | { |
694 | 0 | if (!jso || jso->o_type != json_type_boolean) |
695 | 0 | return 0; |
696 | 0 | JC_BOOL(jso)->c_boolean = new_value; |
697 | 0 | return 1; |
698 | 0 | } |
699 | | |
700 | | /* json_object_int */ |
701 | | |
702 | | static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
703 | | int flags) |
704 | 0 | { |
705 | | /* room for 19 digits, the sign char, and a null term */ |
706 | 0 | char sbuf[21]; |
707 | 0 | if (JC_INT(jso)->cint_type == json_object_int_type_int64) |
708 | 0 | snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64); |
709 | 0 | else |
710 | 0 | snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64); |
711 | 0 | return printbuf_memappend(pb, sbuf, strlen(sbuf)); |
712 | 0 | } |
713 | | |
714 | | struct json_object *json_object_new_int(int32_t i) |
715 | 0 | { |
716 | 0 | return json_object_new_int64(i); |
717 | 0 | } |
718 | | |
719 | | int32_t json_object_get_int(const struct json_object *jso) |
720 | 0 | { |
721 | 0 | int64_t cint64 = 0; |
722 | 0 | double cdouble; |
723 | 0 | enum json_type o_type; |
724 | |
|
725 | 0 | if (!jso) |
726 | 0 | return 0; |
727 | | |
728 | 0 | o_type = jso->o_type; |
729 | 0 | if (o_type == json_type_int) |
730 | 0 | { |
731 | 0 | const struct json_object_int *jsoint = JC_INT_C(jso); |
732 | 0 | if (jsoint->cint_type == json_object_int_type_int64) |
733 | 0 | { |
734 | 0 | cint64 = jsoint->cint.c_int64; |
735 | 0 | } |
736 | 0 | else |
737 | 0 | { |
738 | 0 | if (jsoint->cint.c_uint64 >= INT64_MAX) |
739 | 0 | cint64 = INT64_MAX; |
740 | 0 | else |
741 | 0 | cint64 = (int64_t)jsoint->cint.c_uint64; |
742 | 0 | } |
743 | 0 | } |
744 | 0 | else if (o_type == json_type_string) |
745 | 0 | { |
746 | | /* |
747 | | * Parse strings into 64-bit numbers, then use the |
748 | | * 64-to-32-bit number handling below. |
749 | | */ |
750 | 0 | if (json_parse_int64(get_string_component(jso), &cint64) != 0) |
751 | 0 | return 0; /* whoops, it didn't work. */ |
752 | 0 | o_type = json_type_int; |
753 | 0 | } |
754 | | |
755 | 0 | switch (o_type) |
756 | 0 | { |
757 | 0 | case json_type_int: |
758 | | /* Make sure we return the correct values for out of range numbers. */ |
759 | 0 | if (cint64 <= INT32_MIN) |
760 | 0 | return INT32_MIN; |
761 | 0 | if (cint64 >= INT32_MAX) |
762 | 0 | return INT32_MAX; |
763 | 0 | return (int32_t)cint64; |
764 | 0 | case json_type_double: |
765 | 0 | cdouble = JC_DOUBLE_C(jso)->c_double; |
766 | 0 | if (cdouble <= INT32_MIN) |
767 | 0 | return INT32_MIN; |
768 | 0 | if (cdouble >= INT32_MAX) |
769 | 0 | return INT32_MAX; |
770 | 0 | return (int32_t)cdouble; |
771 | 0 | case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
772 | 0 | default: return 0; |
773 | 0 | } |
774 | 0 | } |
775 | | |
776 | | int json_object_set_int(struct json_object *jso, int new_value) |
777 | 0 | { |
778 | 0 | return json_object_set_int64(jso, (int64_t)new_value); |
779 | 0 | } |
780 | | |
781 | | struct json_object *json_object_new_int64(int64_t i) |
782 | 3.70M | { |
783 | 3.70M | struct json_object_int *jso = JSON_OBJECT_NEW(int); |
784 | 3.70M | if (!jso) |
785 | 0 | return NULL; |
786 | 3.70M | jso->cint.c_int64 = i; |
787 | 3.70M | jso->cint_type = json_object_int_type_int64; |
788 | 3.70M | return &jso->base; |
789 | 3.70M | } |
790 | | |
791 | | struct json_object *json_object_new_uint64(uint64_t i) |
792 | 874 | { |
793 | 874 | struct json_object_int *jso = JSON_OBJECT_NEW(int); |
794 | 874 | if (!jso) |
795 | 0 | return NULL; |
796 | 874 | jso->cint.c_uint64 = i; |
797 | 874 | jso->cint_type = json_object_int_type_uint64; |
798 | 874 | return &jso->base; |
799 | 874 | } |
800 | | |
801 | | int64_t json_object_get_int64(const struct json_object *jso) |
802 | 0 | { |
803 | 0 | int64_t cint; |
804 | |
|
805 | 0 | if (!jso) |
806 | 0 | return 0; |
807 | 0 | switch (jso->o_type) |
808 | 0 | { |
809 | 0 | case json_type_int: |
810 | 0 | { |
811 | 0 | const struct json_object_int *jsoint = JC_INT_C(jso); |
812 | 0 | switch (jsoint->cint_type) |
813 | 0 | { |
814 | 0 | case json_object_int_type_int64: return jsoint->cint.c_int64; |
815 | 0 | case json_object_int_type_uint64: |
816 | 0 | if (jsoint->cint.c_uint64 >= INT64_MAX) |
817 | 0 | return INT64_MAX; |
818 | 0 | return (int64_t)jsoint->cint.c_uint64; |
819 | 0 | default: json_abort("invalid cint_type"); |
820 | 0 | } |
821 | 0 | } |
822 | 0 | case json_type_double: |
823 | | // INT64_MAX can't be exactly represented as a double |
824 | | // so cast to tell the compiler it's ok to round up. |
825 | 0 | if (JC_DOUBLE_C(jso)->c_double >= (double)INT64_MAX) |
826 | 0 | return INT64_MAX; |
827 | 0 | if (JC_DOUBLE_C(jso)->c_double <= INT64_MIN) |
828 | 0 | return INT64_MIN; |
829 | 0 | return (int64_t)JC_DOUBLE_C(jso)->c_double; |
830 | 0 | case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
831 | 0 | case json_type_string: |
832 | 0 | if (json_parse_int64(get_string_component(jso), &cint) == 0) |
833 | 0 | return cint; |
834 | | /* FALLTHRU */ |
835 | 0 | default: return 0; |
836 | 0 | } |
837 | 0 | } |
838 | | |
839 | | uint64_t json_object_get_uint64(const struct json_object *jso) |
840 | 0 | { |
841 | 0 | uint64_t cuint; |
842 | |
|
843 | 0 | if (!jso) |
844 | 0 | return 0; |
845 | 0 | switch (jso->o_type) |
846 | 0 | { |
847 | 0 | case json_type_int: |
848 | 0 | { |
849 | 0 | const struct json_object_int *jsoint = JC_INT_C(jso); |
850 | 0 | switch (jsoint->cint_type) |
851 | 0 | { |
852 | 0 | case json_object_int_type_int64: |
853 | 0 | if (jsoint->cint.c_int64 < 0) |
854 | 0 | return 0; |
855 | 0 | return (uint64_t)jsoint->cint.c_int64; |
856 | 0 | case json_object_int_type_uint64: return jsoint->cint.c_uint64; |
857 | 0 | default: json_abort("invalid cint_type"); |
858 | 0 | } |
859 | 0 | } |
860 | 0 | case json_type_double: |
861 | | // UINT64_MAX can't be exactly represented as a double |
862 | | // so cast to tell the compiler it's ok to round up. |
863 | 0 | if (JC_DOUBLE_C(jso)->c_double >= (double)UINT64_MAX) |
864 | 0 | return UINT64_MAX; |
865 | 0 | if (JC_DOUBLE_C(jso)->c_double < 0) |
866 | 0 | return 0; |
867 | 0 | return (uint64_t)JC_DOUBLE_C(jso)->c_double; |
868 | 0 | case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
869 | 0 | case json_type_string: |
870 | 0 | if (json_parse_uint64(get_string_component(jso), &cuint) == 0) |
871 | 0 | return cuint; |
872 | | /* FALLTHRU */ |
873 | 0 | default: return 0; |
874 | 0 | } |
875 | 0 | } |
876 | | |
877 | | int json_object_set_int64(struct json_object *jso, int64_t new_value) |
878 | 0 | { |
879 | 0 | if (!jso || jso->o_type != json_type_int) |
880 | 0 | return 0; |
881 | 0 | JC_INT(jso)->cint.c_int64 = new_value; |
882 | 0 | JC_INT(jso)->cint_type = json_object_int_type_int64; |
883 | 0 | return 1; |
884 | 0 | } |
885 | | |
886 | | int json_object_set_uint64(struct json_object *jso, uint64_t new_value) |
887 | 0 | { |
888 | 0 | if (!jso || jso->o_type != json_type_int) |
889 | 0 | return 0; |
890 | 0 | JC_INT(jso)->cint.c_uint64 = new_value; |
891 | 0 | JC_INT(jso)->cint_type = json_object_int_type_uint64; |
892 | 0 | return 1; |
893 | 0 | } |
894 | | |
895 | | int json_object_int_inc(struct json_object *jso, int64_t val) |
896 | 0 | { |
897 | 0 | struct json_object_int *jsoint; |
898 | 0 | if (!jso || jso->o_type != json_type_int) |
899 | 0 | return 0; |
900 | 0 | jsoint = JC_INT(jso); |
901 | 0 | switch (jsoint->cint_type) |
902 | 0 | { |
903 | 0 | case json_object_int_type_int64: |
904 | 0 | if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val) |
905 | 0 | { |
906 | 0 | jsoint->cint.c_uint64 = (uint64_t)jsoint->cint.c_int64 + (uint64_t)val; |
907 | 0 | jsoint->cint_type = json_object_int_type_uint64; |
908 | 0 | } |
909 | 0 | else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val) |
910 | 0 | { |
911 | 0 | jsoint->cint.c_int64 = INT64_MIN; |
912 | 0 | } |
913 | 0 | else |
914 | 0 | { |
915 | 0 | jsoint->cint.c_int64 += val; |
916 | 0 | } |
917 | 0 | return 1; |
918 | 0 | case json_object_int_type_uint64: |
919 | 0 | if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val) |
920 | 0 | { |
921 | 0 | jsoint->cint.c_uint64 = UINT64_MAX; |
922 | 0 | } |
923 | 0 | else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val)) |
924 | 0 | { |
925 | 0 | jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val; |
926 | 0 | jsoint->cint_type = json_object_int_type_int64; |
927 | 0 | } |
928 | 0 | else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val)) |
929 | 0 | { |
930 | 0 | jsoint->cint.c_uint64 -= (uint64_t)(-val); |
931 | 0 | } |
932 | 0 | else |
933 | 0 | { |
934 | 0 | jsoint->cint.c_uint64 += val; |
935 | 0 | } |
936 | 0 | return 1; |
937 | 0 | default: json_abort("invalid cint_type"); |
938 | 0 | } |
939 | 0 | } |
940 | | |
941 | | /* json_object_double */ |
942 | | |
943 | | #if defined(HAVE___THREAD) |
944 | | // i.e. __thread or __declspec(thread) |
945 | | static SPEC___THREAD char *tls_serialization_float_format = NULL; |
946 | | #endif |
947 | | static char *global_serialization_float_format = NULL; |
948 | | |
949 | | int json_c_set_serialization_double_format(const char *double_format, int global_or_thread) |
950 | 0 | { |
951 | 0 | if (global_or_thread == JSON_C_OPTION_GLOBAL) |
952 | 0 | { |
953 | 0 | #if defined(HAVE___THREAD) |
954 | 0 | if (tls_serialization_float_format) |
955 | 0 | { |
956 | 0 | free(tls_serialization_float_format); |
957 | 0 | tls_serialization_float_format = NULL; |
958 | 0 | } |
959 | 0 | #endif |
960 | 0 | if (global_serialization_float_format) |
961 | 0 | free(global_serialization_float_format); |
962 | 0 | if (double_format) |
963 | 0 | { |
964 | 0 | char *p = strdup(double_format); |
965 | 0 | if (p == NULL) |
966 | 0 | { |
967 | 0 | _json_c_set_last_err("json_c_set_serialization_double_format: " |
968 | 0 | "out of memory\n"); |
969 | 0 | return -1; |
970 | 0 | } |
971 | 0 | global_serialization_float_format = p; |
972 | 0 | } |
973 | 0 | else |
974 | 0 | { |
975 | 0 | global_serialization_float_format = NULL; |
976 | 0 | } |
977 | 0 | } |
978 | 0 | else if (global_or_thread == JSON_C_OPTION_THREAD) |
979 | 0 | { |
980 | 0 | #if defined(HAVE___THREAD) |
981 | 0 | if (tls_serialization_float_format) |
982 | 0 | { |
983 | 0 | free(tls_serialization_float_format); |
984 | 0 | tls_serialization_float_format = NULL; |
985 | 0 | } |
986 | 0 | if (double_format) |
987 | 0 | { |
988 | 0 | char *p = strdup(double_format); |
989 | 0 | if (p == NULL) |
990 | 0 | { |
991 | 0 | _json_c_set_last_err("json_c_set_serialization_double_format: " |
992 | 0 | "out of memory\n"); |
993 | 0 | return -1; |
994 | 0 | } |
995 | 0 | tls_serialization_float_format = p; |
996 | 0 | } |
997 | 0 | else |
998 | 0 | { |
999 | 0 | tls_serialization_float_format = NULL; |
1000 | 0 | } |
1001 | | #else |
1002 | | _json_c_set_last_err("json_c_set_serialization_double_format: not compiled " |
1003 | | "with __thread support\n"); |
1004 | | return -1; |
1005 | | #endif |
1006 | 0 | } |
1007 | 0 | else |
1008 | 0 | { |
1009 | 0 | _json_c_set_last_err("json_c_set_serialization_double_format: invalid " |
1010 | 0 | "global_or_thread value: %d\n", global_or_thread); |
1011 | 0 | return -1; |
1012 | 0 | } |
1013 | 0 | return 0; |
1014 | 0 | } |
1015 | | |
1016 | | static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb, |
1017 | | int level, int flags, const char *format) |
1018 | 0 | { |
1019 | 0 | struct json_object_double *jsodbl = JC_DOUBLE(jso); |
1020 | 0 | char buf[128], *p, *q; |
1021 | 0 | int size; |
1022 | | /* Although JSON RFC does not support |
1023 | | * NaN or Infinity as numeric values |
1024 | | * ECMA 262 section 9.8.1 defines |
1025 | | * how to handle these cases as strings |
1026 | | */ |
1027 | 0 | if (isnan(jsodbl->c_double)) |
1028 | 0 | { |
1029 | 0 | size = snprintf(buf, sizeof(buf), "NaN"); |
1030 | 0 | } |
1031 | 0 | else if (isinf(jsodbl->c_double)) |
1032 | 0 | { |
1033 | 0 | if (jsodbl->c_double > 0) |
1034 | 0 | size = snprintf(buf, sizeof(buf), "Infinity"); |
1035 | 0 | else |
1036 | 0 | size = snprintf(buf, sizeof(buf), "-Infinity"); |
1037 | 0 | } |
1038 | 0 | else |
1039 | 0 | { |
1040 | 0 | const char *std_format = "%.17g"; |
1041 | 0 | int format_drops_decimals = 0; |
1042 | 0 | int looks_numeric = 0; |
1043 | |
|
1044 | 0 | if (!format) |
1045 | 0 | { |
1046 | 0 | #if defined(HAVE___THREAD) |
1047 | 0 | if (tls_serialization_float_format) |
1048 | 0 | format = tls_serialization_float_format; |
1049 | 0 | else |
1050 | 0 | #endif |
1051 | 0 | if (global_serialization_float_format) |
1052 | 0 | format = global_serialization_float_format; |
1053 | 0 | else |
1054 | 0 | format = std_format; |
1055 | 0 | } |
1056 | 0 | size = snprintf(buf, sizeof(buf), format, jsodbl->c_double); |
1057 | |
|
1058 | 0 | if (size < 0) |
1059 | 0 | return -1; |
1060 | | |
1061 | 0 | p = strchr(buf, ','); |
1062 | 0 | if (p) |
1063 | 0 | *p = '.'; |
1064 | 0 | else |
1065 | 0 | p = strchr(buf, '.'); |
1066 | |
|
1067 | 0 | if (format == std_format || strstr(format, ".0f") == NULL) |
1068 | 0 | format_drops_decimals = 1; |
1069 | |
|
1070 | 0 | looks_numeric = /* Looks like *some* kind of number */ |
1071 | 0 | is_plain_digit(buf[0]) || (size > 1 && buf[0] == '-' && is_plain_digit(buf[1])); |
1072 | |
|
1073 | 0 | if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */ |
1074 | 0 | strchr(buf, 'e') == NULL && /* Not scientific notation */ |
1075 | 0 | format_drops_decimals) |
1076 | 0 | { |
1077 | | // Ensure it looks like a float, even if snprintf didn't, |
1078 | | // unless a custom format is set to omit the decimal. |
1079 | 0 | strcat(buf, ".0"); |
1080 | 0 | size += 2; |
1081 | 0 | } |
1082 | 0 | if (p && (flags & JSON_C_TO_STRING_NOZERO)) |
1083 | 0 | { |
1084 | | /* last useful digit, always keep 1 zero */ |
1085 | 0 | p++; |
1086 | 0 | for (q = p; *q; q++) |
1087 | 0 | { |
1088 | 0 | if (*q != '0') |
1089 | 0 | p = q; |
1090 | 0 | } |
1091 | | /* drop trailing zeroes */ |
1092 | 0 | if (*p != 0) |
1093 | 0 | *(++p) = 0; |
1094 | 0 | size = p - buf; |
1095 | 0 | } |
1096 | 0 | } |
1097 | | // although unlikely, snprintf can fail |
1098 | 0 | if (size < 0) |
1099 | 0 | return -1; |
1100 | | |
1101 | 0 | if (size >= (int)sizeof(buf)) |
1102 | | // The standard formats are guaranteed not to overrun the buffer, |
1103 | | // but if a custom one happens to do so, just silently truncate. |
1104 | 0 | size = sizeof(buf) - 1; |
1105 | 0 | printbuf_memappend(pb, buf, size); |
1106 | 0 | return size; |
1107 | 0 | } |
1108 | | |
1109 | | static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb, |
1110 | | int level, int flags) |
1111 | 0 | { |
1112 | 0 | return json_object_double_to_json_string_format(jso, pb, level, flags, NULL); |
1113 | 0 | } |
1114 | | |
1115 | | int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
1116 | | int flags) |
1117 | 0 | { |
1118 | 0 | return json_object_double_to_json_string_format(jso, pb, level, flags, |
1119 | 0 | (const char *)jso->_userdata); |
1120 | 0 | } |
1121 | | |
1122 | | struct json_object *json_object_new_double(double d) |
1123 | 8.81k | { |
1124 | 8.81k | struct json_object_double *jso = JSON_OBJECT_NEW(double); |
1125 | 8.81k | if (!jso) |
1126 | 0 | return NULL; |
1127 | 8.81k | jso->base._to_json_string = &json_object_double_to_json_string_default; |
1128 | 8.81k | jso->c_double = d; |
1129 | 8.81k | return &jso->base; |
1130 | 8.81k | } |
1131 | | |
1132 | | struct json_object *json_object_new_double_s(double d, const char *ds) |
1133 | 2.54k | { |
1134 | 2.54k | char *new_ds; |
1135 | 2.54k | struct json_object *jso = json_object_new_double(d); |
1136 | 2.54k | if (!jso) |
1137 | 0 | return NULL; |
1138 | | |
1139 | 2.54k | new_ds = strdup(ds); |
1140 | 2.54k | if (!new_ds) |
1141 | 0 | { |
1142 | 0 | json_object_generic_delete(jso); |
1143 | 0 | errno = ENOMEM; |
1144 | 0 | return NULL; |
1145 | 0 | } |
1146 | 2.54k | json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds, |
1147 | 2.54k | json_object_free_userdata); |
1148 | 2.54k | return jso; |
1149 | 2.54k | } |
1150 | | |
1151 | | /* |
1152 | | * A wrapper around json_object_userdata_to_json_string() used only |
1153 | | * by json_object_new_double_s() just so json_object_set_double() can |
1154 | | * detect when it needs to reset the serializer to the default. |
1155 | | */ |
1156 | | static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, |
1157 | | int level, int flags) |
1158 | 0 | { |
1159 | 0 | return json_object_userdata_to_json_string(jso, pb, level, flags); |
1160 | 0 | } |
1161 | | |
1162 | | int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
1163 | | int flags) |
1164 | 0 | { |
1165 | 0 | int userdata_len = strlen((const char *)jso->_userdata); |
1166 | 0 | printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len); |
1167 | 0 | return userdata_len; |
1168 | 0 | } |
1169 | | |
1170 | | void json_object_free_userdata(struct json_object *jso, void *userdata) |
1171 | 2.54k | { |
1172 | 2.54k | free(userdata); |
1173 | 2.54k | } |
1174 | | |
1175 | | double json_object_get_double(const struct json_object *jso) |
1176 | 0 | { |
1177 | 0 | double cdouble; |
1178 | 0 | char *errPtr = NULL; |
1179 | |
|
1180 | 0 | if (!jso) |
1181 | 0 | return 0.0; |
1182 | 0 | switch (jso->o_type) |
1183 | 0 | { |
1184 | 0 | case json_type_double: return JC_DOUBLE_C(jso)->c_double; |
1185 | 0 | case json_type_int: |
1186 | 0 | switch (JC_INT_C(jso)->cint_type) |
1187 | 0 | { |
1188 | 0 | case json_object_int_type_int64: return JC_INT_C(jso)->cint.c_int64; |
1189 | 0 | case json_object_int_type_uint64: return JC_INT_C(jso)->cint.c_uint64; |
1190 | 0 | default: json_abort("invalid cint_type"); |
1191 | 0 | } |
1192 | 0 | case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
1193 | 0 | case json_type_string: |
1194 | 0 | errno = 0; |
1195 | 0 | cdouble = strtod(get_string_component(jso), &errPtr); |
1196 | | |
1197 | | /* if conversion stopped at the first character, return 0.0 */ |
1198 | 0 | if (errPtr == get_string_component(jso)) |
1199 | 0 | { |
1200 | 0 | errno = EINVAL; |
1201 | 0 | return 0.0; |
1202 | 0 | } |
1203 | | |
1204 | | /* |
1205 | | * Check that the conversion terminated on something sensible |
1206 | | * |
1207 | | * For example, { "pay" : 123AB } would parse as 123. |
1208 | | */ |
1209 | 0 | if (*errPtr != '\0') |
1210 | 0 | { |
1211 | 0 | errno = EINVAL; |
1212 | 0 | return 0.0; |
1213 | 0 | } |
1214 | | |
1215 | | /* |
1216 | | * If strtod encounters a string which would exceed the |
1217 | | * capacity of a double, it returns +/- HUGE_VAL and sets |
1218 | | * errno to ERANGE. But +/- HUGE_VAL is also a valid result |
1219 | | * from a conversion, so we need to check errno. |
1220 | | * |
1221 | | * Underflow also sets errno to ERANGE, but it returns 0 in |
1222 | | * that case, which is what we will return anyway. |
1223 | | * |
1224 | | * See CERT guideline ERR30-C |
1225 | | */ |
1226 | 0 | if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno)) |
1227 | 0 | cdouble = 0.0; |
1228 | 0 | return cdouble; |
1229 | 0 | default: errno = EINVAL; return 0.0; |
1230 | 0 | } |
1231 | 0 | } |
1232 | | |
1233 | | int json_object_set_double(struct json_object *jso, double new_value) |
1234 | 0 | { |
1235 | 0 | if (!jso || jso->o_type != json_type_double) |
1236 | 0 | return 0; |
1237 | 0 | JC_DOUBLE(jso)->c_double = new_value; |
1238 | 0 | if (jso->_to_json_string == &_json_object_userdata_to_json_string) |
1239 | 0 | json_object_set_serializer(jso, NULL, NULL, NULL); |
1240 | 0 | return 1; |
1241 | 0 | } |
1242 | | |
1243 | | /* json_object_string */ |
1244 | | |
1245 | | static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb, |
1246 | | int level, int flags) |
1247 | 0 | { |
1248 | 0 | ssize_t len = JC_STRING(jso)->len; |
1249 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
1250 | 0 | printbuf_strappend(pb, ANSI_COLOR_FG_GREEN); |
1251 | 0 | printbuf_strappend(pb, "\""); |
1252 | 0 | json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags); |
1253 | 0 | printbuf_strappend(pb, "\""); |
1254 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
1255 | 0 | printbuf_strappend(pb, ANSI_COLOR_RESET); |
1256 | 0 | return 0; |
1257 | 0 | } |
1258 | | |
1259 | | static void json_object_string_delete(struct json_object *jso) |
1260 | 1.22k | { |
1261 | 1.22k | if (JC_STRING(jso)->len < 0) |
1262 | 0 | free(JC_STRING(jso)->c_string.pdata); |
1263 | 1.22k | json_object_generic_delete(jso); |
1264 | 1.22k | } |
1265 | | |
1266 | | static struct json_object *_json_object_new_string(const char *s, const size_t len) |
1267 | 1.22k | { |
1268 | 1.22k | size_t objsize; |
1269 | 1.22k | struct json_object_string *jso; |
1270 | | |
1271 | | /* |
1272 | | * Structures Actual memory layout |
1273 | | * ------------------- -------------------- |
1274 | | * [json_object_string [json_object_string |
1275 | | * [json_object] [json_object] |
1276 | | * ...other fields... ...other fields... |
1277 | | * c_string] len |
1278 | | * bytes |
1279 | | * of |
1280 | | * string |
1281 | | * data |
1282 | | * \0] |
1283 | | */ |
1284 | 1.22k | if (len > (SSIZE_T_MAX - (sizeof(*jso) - sizeof(jso->c_string)) - 1)) |
1285 | 0 | return NULL; |
1286 | 1.22k | objsize = (sizeof(*jso) - sizeof(jso->c_string)) + len + 1; |
1287 | 1.22k | if (len < sizeof(void *)) |
1288 | | // We need a minimum size to support json_object_set_string() mutability |
1289 | | // so we can stuff a pointer into pdata :( |
1290 | 938 | objsize += sizeof(void *) - len; |
1291 | | |
1292 | 1.22k | jso = (struct json_object_string *)json_object_new(json_type_string, objsize, |
1293 | 1.22k | &json_object_string_to_json_string); |
1294 | | |
1295 | 1.22k | if (!jso) |
1296 | 0 | return NULL; |
1297 | 1.22k | jso->len = len; |
1298 | 1.22k | memcpy(jso->c_string.idata, s, len); |
1299 | | // Cast below needed for Clang UB sanitizer |
1300 | 1.22k | ((char *)jso->c_string.idata)[len] = '\0'; |
1301 | 1.22k | return &jso->base; |
1302 | 1.22k | } |
1303 | | |
1304 | | struct json_object *json_object_new_string(const char *s) |
1305 | 0 | { |
1306 | 0 | return _json_object_new_string(s, strlen(s)); |
1307 | 0 | } |
1308 | | |
1309 | | struct json_object *json_object_new_string_len(const char *s, const int len) |
1310 | 1.22k | { |
1311 | 1.22k | return _json_object_new_string(s, len); |
1312 | 1.22k | } |
1313 | | |
1314 | | const char *json_object_get_string(struct json_object *jso) |
1315 | 0 | { |
1316 | 0 | if (!jso) |
1317 | 0 | return NULL; |
1318 | 0 | switch (jso->o_type) |
1319 | 0 | { |
1320 | 0 | case json_type_string: return get_string_component(jso); |
1321 | 0 | default: return json_object_to_json_string(jso); |
1322 | 0 | } |
1323 | 0 | } |
1324 | | |
1325 | | static inline ssize_t _json_object_get_string_len(const struct json_object_string *jso) |
1326 | 0 | { |
1327 | 0 | ssize_t len; |
1328 | 0 | len = jso->len; |
1329 | 0 | return (len < 0) ? -(ssize_t)len : len; |
1330 | 0 | } |
1331 | | int json_object_get_string_len(const struct json_object *jso) |
1332 | 0 | { |
1333 | 0 | if (!jso) |
1334 | 0 | return 0; |
1335 | 0 | switch (jso->o_type) |
1336 | 0 | { |
1337 | 0 | case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso)); |
1338 | 0 | default: return 0; |
1339 | 0 | } |
1340 | 0 | } |
1341 | | |
1342 | | static int _json_object_set_string_len(json_object *jso, const char *s, size_t len) |
1343 | 0 | { |
1344 | 0 | char *dstbuf; |
1345 | 0 | ssize_t curlen; |
1346 | 0 | ssize_t newlen; |
1347 | 0 | if (jso == NULL || jso->o_type != json_type_string) |
1348 | 0 | return 0; |
1349 | | |
1350 | 0 | if (len >= INT_MAX - 1) |
1351 | | // jso->len is a signed ssize_t, so it can't hold the |
1352 | | // full size_t range. json_object_get_string_len returns |
1353 | | // length as int, cap length at INT_MAX. |
1354 | 0 | return 0; |
1355 | | |
1356 | 0 | curlen = JC_STRING(jso)->len; |
1357 | 0 | if (curlen < 0) { |
1358 | 0 | if (len == 0) { |
1359 | 0 | free(JC_STRING(jso)->c_string.pdata); |
1360 | 0 | JC_STRING(jso)->len = curlen = 0; |
1361 | 0 | } else { |
1362 | 0 | curlen = -curlen; |
1363 | 0 | } |
1364 | 0 | } |
1365 | |
|
1366 | 0 | newlen = len; |
1367 | 0 | dstbuf = get_string_component_mutable(jso); |
1368 | |
|
1369 | 0 | if ((ssize_t)len > curlen) |
1370 | 0 | { |
1371 | | // We have no way to return the new ptr from realloc(jso, newlen) |
1372 | | // and we have no way of knowing whether there's extra room available |
1373 | | // so we need to stuff a pointer in to pdata :( |
1374 | 0 | dstbuf = (char *)malloc(len + 1); |
1375 | 0 | if (dstbuf == NULL) |
1376 | 0 | return 0; |
1377 | 0 | if (JC_STRING(jso)->len < 0) |
1378 | 0 | free(JC_STRING(jso)->c_string.pdata); |
1379 | 0 | JC_STRING(jso)->c_string.pdata = dstbuf; |
1380 | 0 | newlen = -(ssize_t)len; |
1381 | 0 | } |
1382 | 0 | else if (JC_STRING(jso)->len < 0) |
1383 | 0 | { |
1384 | | // We've got enough room in the separate allocated buffer, |
1385 | | // so use it as-is and continue to indicate that pdata is used. |
1386 | 0 | newlen = -(ssize_t)len; |
1387 | 0 | } |
1388 | | |
1389 | 0 | memcpy(dstbuf, (const void *)s, len); |
1390 | 0 | dstbuf[len] = '\0'; |
1391 | 0 | JC_STRING(jso)->len = newlen; |
1392 | 0 | return 1; |
1393 | 0 | } |
1394 | | |
1395 | | int json_object_set_string(json_object *jso, const char *s) |
1396 | 0 | { |
1397 | 0 | return _json_object_set_string_len(jso, s, strlen(s)); |
1398 | 0 | } |
1399 | | |
1400 | | int json_object_set_string_len(json_object *jso, const char *s, int len) |
1401 | 0 | { |
1402 | 0 | return _json_object_set_string_len(jso, s, len); |
1403 | 0 | } |
1404 | | |
1405 | | /* json_object_array */ |
1406 | | |
1407 | | static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
1408 | | int flags) |
1409 | 0 | { |
1410 | 0 | int had_children = 0; |
1411 | 0 | size_t ii; |
1412 | |
|
1413 | 0 | printbuf_strappend(pb, "["); |
1414 | 0 | for (ii = 0; ii < json_object_array_length(jso); ii++) |
1415 | 0 | { |
1416 | 0 | struct json_object *val; |
1417 | 0 | if (had_children) |
1418 | 0 | { |
1419 | 0 | printbuf_strappend(pb, ","); |
1420 | 0 | } |
1421 | 0 | if (flags & JSON_C_TO_STRING_PRETTY) |
1422 | 0 | printbuf_strappend(pb, "\n"); |
1423 | 0 | had_children = 1; |
1424 | 0 | if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
1425 | 0 | printbuf_strappend(pb, " "); |
1426 | 0 | indent(pb, level + 1, flags); |
1427 | 0 | val = json_object_array_get_idx(jso, ii); |
1428 | 0 | if (val == NULL) { |
1429 | |
|
1430 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
1431 | 0 | printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA); |
1432 | 0 | printbuf_strappend(pb, "null"); |
1433 | 0 | if (flags & JSON_C_TO_STRING_COLOR) |
1434 | 0 | printbuf_strappend(pb, ANSI_COLOR_RESET); |
1435 | |
|
1436 | 0 | } else if (val->_to_json_string(val, pb, level + 1, flags) < 0) |
1437 | 0 | return -1; |
1438 | 0 | } |
1439 | 0 | if ((flags & JSON_C_TO_STRING_PRETTY) && had_children) |
1440 | 0 | { |
1441 | 0 | printbuf_strappend(pb, "\n"); |
1442 | 0 | indent(pb, level, flags); |
1443 | 0 | } |
1444 | |
|
1445 | 0 | if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
1446 | 0 | return printbuf_strappend(pb, " ]"); |
1447 | 0 | return printbuf_strappend(pb, "]"); |
1448 | 0 | } |
1449 | | |
1450 | | static void json_object_array_entry_free(void *data) |
1451 | 3.69M | { |
1452 | 3.69M | json_object_put((struct json_object *)data); |
1453 | 3.69M | } |
1454 | | |
1455 | | static void json_object_array_delete(struct json_object *jso) |
1456 | 4.23k | { |
1457 | 4.23k | array_list_free(JC_ARRAY(jso)->c_array); |
1458 | 4.23k | json_object_generic_delete(jso); |
1459 | 4.23k | } |
1460 | | |
1461 | | struct json_object *json_object_new_array(void) |
1462 | 4.23k | { |
1463 | 4.23k | return json_object_new_array_ext(ARRAY_LIST_DEFAULT_SIZE); |
1464 | 4.23k | } |
1465 | | struct json_object *json_object_new_array_ext(int initial_size) |
1466 | 4.23k | { |
1467 | 4.23k | struct json_object_array *jso = JSON_OBJECT_NEW(array); |
1468 | 4.23k | if (!jso) |
1469 | 0 | return NULL; |
1470 | 4.23k | jso->c_array = array_list_new2(&json_object_array_entry_free, initial_size); |
1471 | 4.23k | if (jso->c_array == NULL) |
1472 | 0 | { |
1473 | 0 | free(jso); |
1474 | 0 | return NULL; |
1475 | 0 | } |
1476 | 4.23k | return &jso->base; |
1477 | 4.23k | } |
1478 | | |
1479 | | struct array_list *json_object_get_array(const struct json_object *jso) |
1480 | 0 | { |
1481 | 0 | if (!jso) |
1482 | 0 | return NULL; |
1483 | 0 | switch (jso->o_type) |
1484 | 0 | { |
1485 | 0 | case json_type_array: return JC_ARRAY_C(jso)->c_array; |
1486 | 0 | default: return NULL; |
1487 | 0 | } |
1488 | 0 | } |
1489 | | |
1490 | | void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *)) |
1491 | 0 | { |
1492 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1493 | 0 | array_list_sort(JC_ARRAY(jso)->c_array, sort_fn); |
1494 | 0 | } |
1495 | | |
1496 | | struct json_object *json_object_array_bsearch(const struct json_object *key, |
1497 | | const struct json_object *jso, |
1498 | | int (*sort_fn)(const void *, const void *)) |
1499 | 0 | { |
1500 | 0 | struct json_object **result; |
1501 | |
|
1502 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1503 | 0 | result = (struct json_object **)array_list_bsearch((const void **)(void *)&key, |
1504 | 0 | JC_ARRAY_C(jso)->c_array, sort_fn); |
1505 | |
|
1506 | 0 | if (!result) |
1507 | 0 | return NULL; |
1508 | 0 | return *result; |
1509 | 0 | } |
1510 | | |
1511 | | size_t json_object_array_length(const struct json_object *jso) |
1512 | 0 | { |
1513 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1514 | 0 | return array_list_length(JC_ARRAY_C(jso)->c_array); |
1515 | 0 | } |
1516 | | |
1517 | | int json_object_array_add(struct json_object *jso, struct json_object *val) |
1518 | 3.69M | { |
1519 | 3.69M | assert(json_object_get_type(jso) == json_type_array); |
1520 | 3.69M | return array_list_add(JC_ARRAY(jso)->c_array, val); |
1521 | 3.69M | } |
1522 | | |
1523 | | int json_object_array_insert_idx(struct json_object *jso, size_t idx, struct json_object *val) |
1524 | 0 | { |
1525 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1526 | 0 | return array_list_insert_idx(JC_ARRAY(jso)->c_array, idx, val); |
1527 | 0 | } |
1528 | | |
1529 | | int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val) |
1530 | 0 | { |
1531 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1532 | 0 | return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val); |
1533 | 0 | } |
1534 | | |
1535 | | int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count) |
1536 | 0 | { |
1537 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1538 | 0 | return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count); |
1539 | 0 | } |
1540 | | |
1541 | | struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx) |
1542 | 0 | { |
1543 | 0 | assert(json_object_get_type(jso) == json_type_array); |
1544 | 0 | return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx); |
1545 | 0 | } |
1546 | | |
1547 | | static int json_array_equal(struct json_object *jso1, struct json_object *jso2) |
1548 | 0 | { |
1549 | 0 | size_t len, i; |
1550 | |
|
1551 | 0 | len = json_object_array_length(jso1); |
1552 | 0 | if (len != json_object_array_length(jso2)) |
1553 | 0 | return 0; |
1554 | | |
1555 | 0 | for (i = 0; i < len; i++) |
1556 | 0 | { |
1557 | 0 | if (!json_object_equal(json_object_array_get_idx(jso1, i), |
1558 | 0 | json_object_array_get_idx(jso2, i))) |
1559 | 0 | return 0; |
1560 | 0 | } |
1561 | 0 | return 1; |
1562 | 0 | } |
1563 | | |
1564 | | int json_object_array_shrink(struct json_object *jso, int empty_slots) |
1565 | 2.35k | { |
1566 | 2.35k | if (empty_slots < 0) |
1567 | 0 | json_abort("json_object_array_shrink called with negative empty_slots"); |
1568 | 2.35k | return array_list_shrink(JC_ARRAY(jso)->c_array, empty_slots); |
1569 | 2.35k | } |
1570 | | |
1571 | | struct json_object *json_object_new_null(void) |
1572 | 0 | { |
1573 | 0 | return NULL; |
1574 | 0 | } |
1575 | | |
1576 | | static int json_object_all_values_equal(struct json_object *jso1, struct json_object *jso2) |
1577 | 0 | { |
1578 | 0 | struct json_object_iter iter; |
1579 | 0 | struct json_object *sub; |
1580 | |
|
1581 | 0 | assert(json_object_get_type(jso1) == json_type_object); |
1582 | 0 | assert(json_object_get_type(jso2) == json_type_object); |
1583 | | /* Iterate over jso1 keys and see if they exist and are equal in jso2 */ |
1584 | 0 | json_object_object_foreachC(jso1, iter) |
1585 | 0 | { |
1586 | 0 | if (!lh_table_lookup_ex(JC_OBJECT(jso2)->c_object, (void *)iter.key, |
1587 | 0 | (void **)(void *)&sub)) |
1588 | 0 | return 0; |
1589 | 0 | if (!json_object_equal(iter.val, sub)) |
1590 | 0 | return 0; |
1591 | 0 | } |
1592 | | |
1593 | | /* Iterate over jso2 keys to see if any exist that are not in jso1 */ |
1594 | 0 | json_object_object_foreachC(jso2, iter) |
1595 | 0 | { |
1596 | 0 | if (!lh_table_lookup_ex(JC_OBJECT(jso1)->c_object, (void *)iter.key, |
1597 | 0 | (void **)(void *)&sub)) |
1598 | 0 | return 0; |
1599 | 0 | } |
1600 | | |
1601 | 0 | return 1; |
1602 | 0 | } |
1603 | | |
1604 | | int json_object_equal(struct json_object *jso1, struct json_object *jso2) |
1605 | 0 | { |
1606 | 0 | if (jso1 == jso2) |
1607 | 0 | return 1; |
1608 | | |
1609 | 0 | if (!jso1 || !jso2) |
1610 | 0 | return 0; |
1611 | | |
1612 | 0 | if (jso1->o_type != jso2->o_type) |
1613 | 0 | return 0; |
1614 | | |
1615 | 0 | switch (jso1->o_type) |
1616 | 0 | { |
1617 | 0 | case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean); |
1618 | | |
1619 | 0 | case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double); |
1620 | | |
1621 | 0 | case json_type_int: |
1622 | 0 | { |
1623 | 0 | struct json_object_int *int1 = JC_INT(jso1); |
1624 | 0 | struct json_object_int *int2 = JC_INT(jso2); |
1625 | 0 | if (int1->cint_type == json_object_int_type_int64) |
1626 | 0 | { |
1627 | 0 | if (int2->cint_type == json_object_int_type_int64) |
1628 | 0 | return (int1->cint.c_int64 == int2->cint.c_int64); |
1629 | 0 | if (int1->cint.c_int64 < 0) |
1630 | 0 | return 0; |
1631 | 0 | return ((uint64_t)int1->cint.c_int64 == int2->cint.c_uint64); |
1632 | 0 | } |
1633 | | // else jso1 is a uint64 |
1634 | 0 | if (int2->cint_type == json_object_int_type_uint64) |
1635 | 0 | return (int1->cint.c_uint64 == int2->cint.c_uint64); |
1636 | 0 | if (int2->cint.c_int64 < 0) |
1637 | 0 | return 0; |
1638 | 0 | return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64); |
1639 | 0 | } |
1640 | | |
1641 | 0 | case json_type_string: |
1642 | 0 | { |
1643 | 0 | return (_json_object_get_string_len(JC_STRING(jso1)) == |
1644 | 0 | _json_object_get_string_len(JC_STRING(jso2)) && |
1645 | 0 | memcmp(get_string_component(jso1), get_string_component(jso2), |
1646 | 0 | _json_object_get_string_len(JC_STRING(jso1))) == 0); |
1647 | 0 | } |
1648 | | |
1649 | 0 | case json_type_object: return json_object_all_values_equal(jso1, jso2); |
1650 | | |
1651 | 0 | case json_type_array: return json_array_equal(jso1, jso2); |
1652 | | |
1653 | 0 | case json_type_null: return 1; |
1654 | 0 | }; |
1655 | |
|
1656 | 0 | return 0; |
1657 | 0 | } |
1658 | | |
1659 | | static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst) |
1660 | 0 | { |
1661 | 0 | if (!src->_userdata && !src->_user_delete) |
1662 | 0 | return 0; |
1663 | | |
1664 | 0 | if (dst->_to_json_string == json_object_userdata_to_json_string || |
1665 | 0 | dst->_to_json_string == _json_object_userdata_to_json_string) |
1666 | 0 | { |
1667 | 0 | char *p; |
1668 | 0 | assert(src->_userdata); |
1669 | 0 | p = strdup(src->_userdata); |
1670 | 0 | if (p == NULL) |
1671 | 0 | { |
1672 | 0 | _json_c_set_last_err("json_object_copy_serializer_data: out of memory\n"); |
1673 | 0 | return -1; |
1674 | 0 | } |
1675 | 0 | dst->_userdata = p; |
1676 | 0 | } |
1677 | | // else if ... other supported serializers ... |
1678 | 0 | else |
1679 | 0 | { |
1680 | 0 | _json_c_set_last_err( |
1681 | 0 | "json_object_copy_serializer_data: unable to copy unknown serializer data: " |
1682 | 0 | "%p\n", (void *)dst->_to_json_string); |
1683 | 0 | return -1; |
1684 | 0 | } |
1685 | 0 | dst->_user_delete = src->_user_delete; |
1686 | 0 | return 0; |
1687 | 0 | } |
1688 | | |
1689 | | /** |
1690 | | * The default shallow copy implementation. Simply creates a new object of the same |
1691 | | * type but does *not* copy over _userdata nor retain any custom serializer. |
1692 | | * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy |
1693 | | * implementation that is aware of how to copy them. |
1694 | | * |
1695 | | * This always returns -1 or 1. It will never return 2 since it does not copy the serializer. |
1696 | | */ |
1697 | | int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, |
1698 | | size_t index, json_object **dst) |
1699 | 0 | { |
1700 | 0 | switch (src->o_type) |
1701 | 0 | { |
1702 | 0 | case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break; |
1703 | | |
1704 | 0 | case json_type_double: *dst = json_object_new_double(JC_DOUBLE(src)->c_double); break; |
1705 | | |
1706 | 0 | case json_type_int: |
1707 | 0 | switch (JC_INT(src)->cint_type) |
1708 | 0 | { |
1709 | 0 | case json_object_int_type_int64: |
1710 | 0 | *dst = json_object_new_int64(JC_INT(src)->cint.c_int64); |
1711 | 0 | break; |
1712 | 0 | case json_object_int_type_uint64: |
1713 | 0 | *dst = json_object_new_uint64(JC_INT(src)->cint.c_uint64); |
1714 | 0 | break; |
1715 | 0 | default: json_abort("invalid cint_type"); |
1716 | 0 | } |
1717 | 0 | break; |
1718 | | |
1719 | 0 | case json_type_string: |
1720 | 0 | *dst = json_object_new_string_len(get_string_component(src), |
1721 | 0 | _json_object_get_string_len(JC_STRING(src))); |
1722 | 0 | break; |
1723 | | |
1724 | 0 | case json_type_object: *dst = json_object_new_object(); break; |
1725 | | |
1726 | 0 | case json_type_array: *dst = json_object_new_array(); break; |
1727 | | |
1728 | 0 | default: errno = EINVAL; return -1; |
1729 | 0 | } |
1730 | | |
1731 | 0 | if (!*dst) |
1732 | 0 | { |
1733 | 0 | errno = ENOMEM; |
1734 | 0 | return -1; |
1735 | 0 | } |
1736 | 0 | (*dst)->_to_json_string = src->_to_json_string; |
1737 | | // _userdata and _user_delete are copied later |
1738 | 0 | return 1; |
1739 | 0 | } |
1740 | | |
1741 | | /* |
1742 | | * The actual guts of json_object_deep_copy(), with a few additional args |
1743 | | * needed so we can keep track of where we are within the object tree. |
1744 | | * |
1745 | | * Note: caller is responsible for freeing *dst if this fails and returns -1. |
1746 | | */ |
1747 | | static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent, |
1748 | | const char *key_in_parent, size_t index_in_parent, |
1749 | | struct json_object **dst, |
1750 | | json_c_shallow_copy_fn *shallow_copy) |
1751 | 0 | { |
1752 | 0 | struct json_object_iter iter; |
1753 | 0 | size_t src_array_len, ii; |
1754 | |
|
1755 | 0 | int shallow_copy_rc = 0; |
1756 | 0 | shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst); |
1757 | | /* -1=error, 1=object created ok, 2=userdata set */ |
1758 | 0 | if (shallow_copy_rc < 1) |
1759 | 0 | { |
1760 | 0 | errno = EINVAL; |
1761 | 0 | return -1; |
1762 | 0 | } |
1763 | 0 | assert(*dst != NULL); |
1764 | | |
1765 | 0 | switch (src->o_type) |
1766 | 0 | { |
1767 | 0 | case json_type_object: |
1768 | 0 | json_object_object_foreachC(src, iter) |
1769 | 0 | { |
1770 | 0 | struct json_object *jso = NULL; |
1771 | | /* This handles the `json_type_null` case */ |
1772 | 0 | if (!iter.val) |
1773 | 0 | jso = NULL; |
1774 | 0 | else if (json_object_deep_copy_recursive(iter.val, src, iter.key, UINT_MAX, |
1775 | 0 | &jso, shallow_copy) < 0) |
1776 | 0 | { |
1777 | 0 | json_object_put(jso); |
1778 | 0 | return -1; |
1779 | 0 | } |
1780 | | |
1781 | 0 | if (json_object_object_add(*dst, iter.key, jso) < 0) |
1782 | 0 | { |
1783 | 0 | json_object_put(jso); |
1784 | 0 | return -1; |
1785 | 0 | } |
1786 | 0 | } |
1787 | 0 | break; |
1788 | | |
1789 | 0 | case json_type_array: |
1790 | 0 | src_array_len = json_object_array_length(src); |
1791 | 0 | for (ii = 0; ii < src_array_len; ii++) |
1792 | 0 | { |
1793 | 0 | struct json_object *jso = NULL; |
1794 | 0 | struct json_object *jso1 = json_object_array_get_idx(src, ii); |
1795 | | /* This handles the `json_type_null` case */ |
1796 | 0 | if (!jso1) |
1797 | 0 | jso = NULL; |
1798 | 0 | else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, |
1799 | 0 | shallow_copy) < 0) |
1800 | 0 | { |
1801 | 0 | json_object_put(jso); |
1802 | 0 | return -1; |
1803 | 0 | } |
1804 | | |
1805 | 0 | if (json_object_array_add(*dst, jso) < 0) |
1806 | 0 | { |
1807 | 0 | json_object_put(jso); |
1808 | 0 | return -1; |
1809 | 0 | } |
1810 | 0 | } |
1811 | 0 | break; |
1812 | | |
1813 | 0 | default: |
1814 | 0 | break; |
1815 | | /* else, nothing to do, shallow_copy already did. */ |
1816 | 0 | } |
1817 | | |
1818 | 0 | if (shallow_copy_rc != 2) |
1819 | 0 | return json_object_copy_serializer_data(src, *dst); |
1820 | | |
1821 | 0 | return 0; |
1822 | 0 | } |
1823 | | |
1824 | | int json_object_deep_copy(struct json_object *src, struct json_object **dst, |
1825 | | json_c_shallow_copy_fn *shallow_copy) |
1826 | 0 | { |
1827 | 0 | int rc; |
1828 | | |
1829 | | /* Check if arguments are sane ; *dst must not point to a non-NULL object */ |
1830 | 0 | if (!src || !dst || *dst) |
1831 | 0 | { |
1832 | 0 | errno = EINVAL; |
1833 | 0 | return -1; |
1834 | 0 | } |
1835 | | |
1836 | 0 | if (shallow_copy == NULL) |
1837 | 0 | shallow_copy = json_c_shallow_copy_default; |
1838 | |
|
1839 | 0 | rc = json_object_deep_copy_recursive(src, NULL, NULL, UINT_MAX, dst, shallow_copy); |
1840 | 0 | if (rc < 0) |
1841 | 0 | { |
1842 | 0 | json_object_put(*dst); |
1843 | 0 | *dst = NULL; |
1844 | 0 | } |
1845 | |
|
1846 | 0 | return rc; |
1847 | 0 | } |
1848 | | |
1849 | | static void json_abort(const char *message) |
1850 | 0 | { |
1851 | 0 | if (message != NULL) |
1852 | 0 | fprintf(stderr, "json-c aborts with error: %s\n", message); |
1853 | 0 | abort(); |
1854 | 0 | } |