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