/src/jansson-2.14/src/value.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org> |
3 | | * |
4 | | * Jansson is free software; you can redistribute it and/or modify |
5 | | * it under the terms of the MIT license. See LICENSE for details. |
6 | | */ |
7 | | |
8 | | #ifndef _GNU_SOURCE |
9 | | #define _GNU_SOURCE |
10 | | #endif |
11 | | |
12 | | #ifdef HAVE_CONFIG_H |
13 | | #include <jansson_private_config.h> |
14 | | #endif |
15 | | |
16 | | #include <math.h> |
17 | | #include <stddef.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #ifdef HAVE_STDINT_H |
22 | | #include <stdint.h> |
23 | | #endif |
24 | | |
25 | | #include "hashtable.h" |
26 | | #include "jansson.h" |
27 | | #include "jansson_private.h" |
28 | | #include "utf.h" |
29 | | |
30 | | /* Work around nonstandard isnan() and isinf() implementations */ |
31 | | #ifndef isnan |
32 | | #ifndef __sun |
33 | | static JSON_INLINE int isnan(double x) { return x != x; } |
34 | | #endif |
35 | | #endif |
36 | | #ifndef isinf |
37 | | static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); } |
38 | | #endif |
39 | | |
40 | | json_t *do_deep_copy(const json_t *json, hashtable_t *parents); |
41 | | |
42 | 0 | static JSON_INLINE void json_init(json_t *json, json_type type) { |
43 | 0 | json->type = type; |
44 | 0 | json->refcount = 1; |
45 | 0 | } |
46 | | |
47 | | int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size, |
48 | 0 | size_t *key_len_out) { |
49 | 0 | size_t key_len = snprintf(key, key_size, "%p", json); |
50 | |
|
51 | 0 | if (key_len_out) |
52 | 0 | *key_len_out = key_len; |
53 | |
|
54 | 0 | if (hashtable_get(parents, key, key_len)) |
55 | 0 | return -1; |
56 | | |
57 | 0 | return hashtable_set(parents, key, key_len, json_null()); |
58 | 0 | } |
59 | | |
60 | | /*** object ***/ |
61 | | |
62 | | extern volatile uint32_t hashtable_seed; |
63 | | |
64 | 0 | json_t *json_object(void) { |
65 | 0 | json_object_t *object = jsonp_malloc(sizeof(json_object_t)); |
66 | 0 | if (!object) |
67 | 0 | return NULL; |
68 | | |
69 | 0 | if (!hashtable_seed) { |
70 | | /* Autoseed */ |
71 | 0 | json_object_seed(0); |
72 | 0 | } |
73 | |
|
74 | 0 | json_init(&object->json, JSON_OBJECT); |
75 | |
|
76 | 0 | if (hashtable_init(&object->hashtable)) { |
77 | 0 | jsonp_free(object); |
78 | 0 | return NULL; |
79 | 0 | } |
80 | | |
81 | 0 | return &object->json; |
82 | 0 | } |
83 | | |
84 | 0 | static void json_delete_object(json_object_t *object) { |
85 | 0 | hashtable_close(&object->hashtable); |
86 | 0 | jsonp_free(object); |
87 | 0 | } |
88 | | |
89 | 0 | size_t json_object_size(const json_t *json) { |
90 | 0 | json_object_t *object; |
91 | |
|
92 | 0 | if (!json_is_object(json)) |
93 | 0 | return 0; |
94 | | |
95 | 0 | object = json_to_object(json); |
96 | 0 | return object->hashtable.size; |
97 | 0 | } |
98 | | |
99 | 0 | json_t *json_object_get(const json_t *json, const char *key) { |
100 | 0 | if (!key) |
101 | 0 | return NULL; |
102 | | |
103 | 0 | return json_object_getn(json, key, strlen(key)); |
104 | 0 | } |
105 | | |
106 | 0 | json_t *json_object_getn(const json_t *json, const char *key, size_t key_len) { |
107 | 0 | json_object_t *object; |
108 | |
|
109 | 0 | if (!key || !json_is_object(json)) |
110 | 0 | return NULL; |
111 | | |
112 | 0 | object = json_to_object(json); |
113 | 0 | return hashtable_get(&object->hashtable, key, key_len); |
114 | 0 | } |
115 | | |
116 | 0 | int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { |
117 | 0 | if (!key) { |
118 | 0 | json_decref(value); |
119 | 0 | return -1; |
120 | 0 | } |
121 | 0 | return json_object_setn_new_nocheck(json, key, strlen(key), value); |
122 | 0 | } |
123 | | |
124 | | int json_object_setn_new_nocheck(json_t *json, const char *key, size_t key_len, |
125 | 0 | json_t *value) { |
126 | 0 | json_object_t *object; |
127 | |
|
128 | 0 | if (!value) |
129 | 0 | return -1; |
130 | | |
131 | 0 | if (!key || !json_is_object(json) || json == value) { |
132 | 0 | json_decref(value); |
133 | 0 | return -1; |
134 | 0 | } |
135 | 0 | object = json_to_object(json); |
136 | |
|
137 | 0 | if (hashtable_set(&object->hashtable, key, key_len, value)) { |
138 | 0 | json_decref(value); |
139 | 0 | return -1; |
140 | 0 | } |
141 | | |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | 0 | int json_object_set_new(json_t *json, const char *key, json_t *value) { |
146 | 0 | if (!key) { |
147 | 0 | json_decref(value); |
148 | 0 | return -1; |
149 | 0 | } |
150 | | |
151 | 0 | return json_object_setn_new(json, key, strlen(key), value); |
152 | 0 | } |
153 | | |
154 | 0 | int json_object_setn_new(json_t *json, const char *key, size_t key_len, json_t *value) { |
155 | 0 | if (!key || !utf8_check_string(key, key_len)) { |
156 | 0 | json_decref(value); |
157 | 0 | return -1; |
158 | 0 | } |
159 | | |
160 | 0 | return json_object_setn_new_nocheck(json, key, key_len, value); |
161 | 0 | } |
162 | | |
163 | 0 | int json_object_del(json_t *json, const char *key) { |
164 | 0 | if (!key) |
165 | 0 | return -1; |
166 | | |
167 | 0 | return json_object_deln(json, key, strlen(key)); |
168 | 0 | } |
169 | | |
170 | 0 | int json_object_deln(json_t *json, const char *key, size_t key_len) { |
171 | 0 | json_object_t *object; |
172 | |
|
173 | 0 | if (!key || !json_is_object(json)) |
174 | 0 | return -1; |
175 | | |
176 | 0 | object = json_to_object(json); |
177 | 0 | return hashtable_del(&object->hashtable, key, key_len); |
178 | 0 | } |
179 | | |
180 | 0 | int json_object_clear(json_t *json) { |
181 | 0 | json_object_t *object; |
182 | |
|
183 | 0 | if (!json_is_object(json)) |
184 | 0 | return -1; |
185 | | |
186 | 0 | object = json_to_object(json); |
187 | 0 | hashtable_clear(&object->hashtable); |
188 | |
|
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | 0 | int json_object_update(json_t *object, json_t *other) { |
193 | 0 | const char *key; |
194 | 0 | json_t *value; |
195 | |
|
196 | 0 | if (!json_is_object(object) || !json_is_object(other)) |
197 | 0 | return -1; |
198 | | |
199 | 0 | json_object_foreach(other, key, value) { |
200 | 0 | if (json_object_set_nocheck(object, key, value)) |
201 | 0 | return -1; |
202 | 0 | } |
203 | | |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | 0 | int json_object_update_existing(json_t *object, json_t *other) { |
208 | 0 | const char *key; |
209 | 0 | size_t key_len; |
210 | 0 | json_t *value; |
211 | |
|
212 | 0 | if (!json_is_object(object) || !json_is_object(other)) |
213 | 0 | return -1; |
214 | | |
215 | 0 | json_object_keylen_foreach(other, key, key_len, value) { |
216 | 0 | if (json_object_getn(object, key, key_len)) |
217 | 0 | json_object_setn_nocheck(object, key, key_len, value); |
218 | 0 | } |
219 | |
|
220 | 0 | return 0; |
221 | 0 | } |
222 | | |
223 | 0 | int json_object_update_missing(json_t *object, json_t *other) { |
224 | 0 | const char *key; |
225 | 0 | json_t *value; |
226 | |
|
227 | 0 | if (!json_is_object(object) || !json_is_object(other)) |
228 | 0 | return -1; |
229 | | |
230 | 0 | json_object_foreach(other, key, value) { |
231 | 0 | if (!json_object_get(object, key)) |
232 | 0 | json_object_set_nocheck(object, key, value); |
233 | 0 | } |
234 | |
|
235 | 0 | return 0; |
236 | 0 | } |
237 | | |
238 | 0 | int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *parents) { |
239 | 0 | const char *key; |
240 | 0 | size_t key_len; |
241 | 0 | json_t *value; |
242 | 0 | char loop_key[LOOP_KEY_LEN]; |
243 | 0 | int res = 0; |
244 | 0 | size_t loop_key_len; |
245 | |
|
246 | 0 | if (!json_is_object(object) || !json_is_object(other)) |
247 | 0 | return -1; |
248 | | |
249 | 0 | if (jsonp_loop_check(parents, other, loop_key, sizeof(loop_key), &loop_key_len)) |
250 | 0 | return -1; |
251 | | |
252 | 0 | json_object_keylen_foreach(other, key, key_len, value) { |
253 | 0 | json_t *v = json_object_get(object, key); |
254 | |
|
255 | 0 | if (json_is_object(v) && json_is_object(value)) { |
256 | 0 | if (do_object_update_recursive(v, value, parents)) { |
257 | 0 | res = -1; |
258 | 0 | break; |
259 | 0 | } |
260 | 0 | } else { |
261 | 0 | if (json_object_setn_nocheck(object, key, key_len, value)) { |
262 | 0 | res = -1; |
263 | 0 | break; |
264 | 0 | } |
265 | 0 | } |
266 | 0 | } |
267 | |
|
268 | 0 | hashtable_del(parents, loop_key, loop_key_len); |
269 | |
|
270 | 0 | return res; |
271 | 0 | } |
272 | | |
273 | 0 | int json_object_update_recursive(json_t *object, json_t *other) { |
274 | 0 | int res; |
275 | 0 | hashtable_t parents_set; |
276 | |
|
277 | 0 | if (hashtable_init(&parents_set)) |
278 | 0 | return -1; |
279 | 0 | res = do_object_update_recursive(object, other, &parents_set); |
280 | 0 | hashtable_close(&parents_set); |
281 | |
|
282 | 0 | return res; |
283 | 0 | } |
284 | | |
285 | 0 | void *json_object_iter(json_t *json) { |
286 | 0 | json_object_t *object; |
287 | |
|
288 | 0 | if (!json_is_object(json)) |
289 | 0 | return NULL; |
290 | | |
291 | 0 | object = json_to_object(json); |
292 | 0 | return hashtable_iter(&object->hashtable); |
293 | 0 | } |
294 | | |
295 | 0 | void *json_object_iter_at(json_t *json, const char *key) { |
296 | 0 | json_object_t *object; |
297 | |
|
298 | 0 | if (!key || !json_is_object(json)) |
299 | 0 | return NULL; |
300 | | |
301 | 0 | object = json_to_object(json); |
302 | 0 | return hashtable_iter_at(&object->hashtable, key, strlen(key)); |
303 | 0 | } |
304 | | |
305 | 0 | void *json_object_iter_next(json_t *json, void *iter) { |
306 | 0 | json_object_t *object; |
307 | |
|
308 | 0 | if (!json_is_object(json) || iter == NULL) |
309 | 0 | return NULL; |
310 | | |
311 | 0 | object = json_to_object(json); |
312 | 0 | return hashtable_iter_next(&object->hashtable, iter); |
313 | 0 | } |
314 | | |
315 | 0 | const char *json_object_iter_key(void *iter) { |
316 | 0 | if (!iter) |
317 | 0 | return NULL; |
318 | | |
319 | 0 | return hashtable_iter_key(iter); |
320 | 0 | } |
321 | | |
322 | 0 | size_t json_object_iter_key_len(void *iter) { |
323 | 0 | if (!iter) |
324 | 0 | return 0; |
325 | | |
326 | 0 | return hashtable_iter_key_len(iter); |
327 | 0 | } |
328 | | |
329 | 0 | json_t *json_object_iter_value(void *iter) { |
330 | 0 | if (!iter) |
331 | 0 | return NULL; |
332 | | |
333 | 0 | return (json_t *)hashtable_iter_value(iter); |
334 | 0 | } |
335 | | |
336 | 0 | int json_object_iter_set_new(json_t *json, void *iter, json_t *value) { |
337 | 0 | if (!json_is_object(json) || !iter || !value) { |
338 | 0 | json_decref(value); |
339 | 0 | return -1; |
340 | 0 | } |
341 | | |
342 | 0 | hashtable_iter_set(iter, value); |
343 | 0 | return 0; |
344 | 0 | } |
345 | | |
346 | 0 | void *json_object_key_to_iter(const char *key) { |
347 | 0 | if (!key) |
348 | 0 | return NULL; |
349 | | |
350 | 0 | return hashtable_key_to_iter(key); |
351 | 0 | } |
352 | | |
353 | 0 | static int json_object_equal(const json_t *object1, const json_t *object2) { |
354 | 0 | const char *key; |
355 | 0 | const json_t *value1, *value2; |
356 | |
|
357 | 0 | if (json_object_size(object1) != json_object_size(object2)) |
358 | 0 | return 0; |
359 | | |
360 | 0 | json_object_foreach((json_t *)object1, key, value1) { |
361 | 0 | value2 = json_object_get(object2, key); |
362 | |
|
363 | 0 | if (!json_equal(value1, value2)) |
364 | 0 | return 0; |
365 | 0 | } |
366 | | |
367 | 0 | return 1; |
368 | 0 | } |
369 | | |
370 | 0 | static json_t *json_object_copy(json_t *object) { |
371 | 0 | json_t *result; |
372 | |
|
373 | 0 | const char *key; |
374 | 0 | json_t *value; |
375 | |
|
376 | 0 | result = json_object(); |
377 | 0 | if (!result) |
378 | 0 | return NULL; |
379 | | |
380 | 0 | json_object_foreach(object, key, value) json_object_set_nocheck(result, key, value); |
381 | |
|
382 | 0 | return result; |
383 | 0 | } |
384 | | |
385 | 0 | static json_t *json_object_deep_copy(const json_t *object, hashtable_t *parents) { |
386 | 0 | json_t *result; |
387 | 0 | void *iter; |
388 | 0 | char loop_key[LOOP_KEY_LEN]; |
389 | 0 | size_t loop_key_len; |
390 | |
|
391 | 0 | if (jsonp_loop_check(parents, object, loop_key, sizeof(loop_key), &loop_key_len)) |
392 | 0 | return NULL; |
393 | | |
394 | 0 | result = json_object(); |
395 | 0 | if (!result) |
396 | 0 | goto out; |
397 | | |
398 | | /* Cannot use json_object_foreach because object has to be cast |
399 | | non-const */ |
400 | 0 | iter = json_object_iter((json_t *)object); |
401 | 0 | while (iter) { |
402 | 0 | const char *key; |
403 | 0 | const json_t *value; |
404 | 0 | key = json_object_iter_key(iter); |
405 | 0 | value = json_object_iter_value(iter); |
406 | |
|
407 | 0 | if (json_object_set_new_nocheck(result, key, do_deep_copy(value, parents))) { |
408 | 0 | json_decref(result); |
409 | 0 | result = NULL; |
410 | 0 | break; |
411 | 0 | } |
412 | 0 | iter = json_object_iter_next((json_t *)object, iter); |
413 | 0 | } |
414 | |
|
415 | 0 | out: |
416 | 0 | hashtable_del(parents, loop_key, loop_key_len); |
417 | |
|
418 | 0 | return result; |
419 | 0 | } |
420 | | |
421 | | /*** array ***/ |
422 | | |
423 | 0 | json_t *json_array(void) { |
424 | 0 | json_array_t *array = jsonp_malloc(sizeof(json_array_t)); |
425 | 0 | if (!array) |
426 | 0 | return NULL; |
427 | 0 | json_init(&array->json, JSON_ARRAY); |
428 | |
|
429 | 0 | array->entries = 0; |
430 | 0 | array->size = 8; |
431 | |
|
432 | 0 | array->table = jsonp_malloc(array->size * sizeof(json_t *)); |
433 | 0 | if (!array->table) { |
434 | 0 | jsonp_free(array); |
435 | 0 | return NULL; |
436 | 0 | } |
437 | | |
438 | 0 | return &array->json; |
439 | 0 | } |
440 | | |
441 | 0 | static void json_delete_array(json_array_t *array) { |
442 | 0 | size_t i; |
443 | |
|
444 | 0 | for (i = 0; i < array->entries; i++) |
445 | 0 | json_decref(array->table[i]); |
446 | |
|
447 | 0 | jsonp_free(array->table); |
448 | 0 | jsonp_free(array); |
449 | 0 | } |
450 | | |
451 | 0 | size_t json_array_size(const json_t *json) { |
452 | 0 | if (!json_is_array(json)) |
453 | 0 | return 0; |
454 | | |
455 | 0 | return json_to_array(json)->entries; |
456 | 0 | } |
457 | | |
458 | 0 | json_t *json_array_get(const json_t *json, size_t index) { |
459 | 0 | json_array_t *array; |
460 | 0 | if (!json_is_array(json)) |
461 | 0 | return NULL; |
462 | 0 | array = json_to_array(json); |
463 | |
|
464 | 0 | if (index >= array->entries) |
465 | 0 | return NULL; |
466 | | |
467 | 0 | return array->table[index]; |
468 | 0 | } |
469 | | |
470 | 0 | int json_array_set_new(json_t *json, size_t index, json_t *value) { |
471 | 0 | json_array_t *array; |
472 | |
|
473 | 0 | if (!value) |
474 | 0 | return -1; |
475 | | |
476 | 0 | if (!json_is_array(json) || json == value) { |
477 | 0 | json_decref(value); |
478 | 0 | return -1; |
479 | 0 | } |
480 | 0 | array = json_to_array(json); |
481 | |
|
482 | 0 | if (index >= array->entries) { |
483 | 0 | json_decref(value); |
484 | 0 | return -1; |
485 | 0 | } |
486 | | |
487 | 0 | json_decref(array->table[index]); |
488 | 0 | array->table[index] = value; |
489 | |
|
490 | 0 | return 0; |
491 | 0 | } |
492 | | |
493 | 0 | static void array_move(json_array_t *array, size_t dest, size_t src, size_t count) { |
494 | 0 | memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *)); |
495 | 0 | } |
496 | | |
497 | | static void array_copy(json_t **dest, size_t dpos, json_t **src, size_t spos, |
498 | 0 | size_t count) { |
499 | 0 | memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *)); |
500 | 0 | } |
501 | | |
502 | 0 | static json_t **json_array_grow(json_array_t *array, size_t amount, int copy) { |
503 | 0 | size_t new_size; |
504 | 0 | json_t **old_table, **new_table; |
505 | |
|
506 | 0 | if (array->entries + amount <= array->size) |
507 | 0 | return array->table; |
508 | | |
509 | 0 | old_table = array->table; |
510 | |
|
511 | 0 | new_size = max(array->size + amount, array->size * 2); |
512 | 0 | new_table = jsonp_malloc(new_size * sizeof(json_t *)); |
513 | 0 | if (!new_table) |
514 | 0 | return NULL; |
515 | | |
516 | 0 | array->size = new_size; |
517 | 0 | array->table = new_table; |
518 | |
|
519 | 0 | if (copy) { |
520 | 0 | array_copy(array->table, 0, old_table, 0, array->entries); |
521 | 0 | jsonp_free(old_table); |
522 | 0 | return array->table; |
523 | 0 | } |
524 | | |
525 | 0 | return old_table; |
526 | 0 | } |
527 | | |
528 | 0 | int json_array_append_new(json_t *json, json_t *value) { |
529 | 0 | json_array_t *array; |
530 | |
|
531 | 0 | if (!value) |
532 | 0 | return -1; |
533 | | |
534 | 0 | if (!json_is_array(json) || json == value) { |
535 | 0 | json_decref(value); |
536 | 0 | return -1; |
537 | 0 | } |
538 | 0 | array = json_to_array(json); |
539 | |
|
540 | 0 | if (!json_array_grow(array, 1, 1)) { |
541 | 0 | json_decref(value); |
542 | 0 | return -1; |
543 | 0 | } |
544 | | |
545 | 0 | array->table[array->entries] = value; |
546 | 0 | array->entries++; |
547 | |
|
548 | 0 | return 0; |
549 | 0 | } |
550 | | |
551 | 0 | int json_array_insert_new(json_t *json, size_t index, json_t *value) { |
552 | 0 | json_array_t *array; |
553 | 0 | json_t **old_table; |
554 | |
|
555 | 0 | if (!value) |
556 | 0 | return -1; |
557 | | |
558 | 0 | if (!json_is_array(json) || json == value) { |
559 | 0 | json_decref(value); |
560 | 0 | return -1; |
561 | 0 | } |
562 | 0 | array = json_to_array(json); |
563 | |
|
564 | 0 | if (index > array->entries) { |
565 | 0 | json_decref(value); |
566 | 0 | return -1; |
567 | 0 | } |
568 | | |
569 | 0 | old_table = json_array_grow(array, 1, 0); |
570 | 0 | if (!old_table) { |
571 | 0 | json_decref(value); |
572 | 0 | return -1; |
573 | 0 | } |
574 | | |
575 | 0 | if (old_table != array->table) { |
576 | 0 | array_copy(array->table, 0, old_table, 0, index); |
577 | 0 | array_copy(array->table, index + 1, old_table, index, array->entries - index); |
578 | 0 | jsonp_free(old_table); |
579 | 0 | } else |
580 | 0 | array_move(array, index + 1, index, array->entries - index); |
581 | |
|
582 | 0 | array->table[index] = value; |
583 | 0 | array->entries++; |
584 | |
|
585 | 0 | return 0; |
586 | 0 | } |
587 | | |
588 | 0 | int json_array_remove(json_t *json, size_t index) { |
589 | 0 | json_array_t *array; |
590 | |
|
591 | 0 | if (!json_is_array(json)) |
592 | 0 | return -1; |
593 | 0 | array = json_to_array(json); |
594 | |
|
595 | 0 | if (index >= array->entries) |
596 | 0 | return -1; |
597 | | |
598 | 0 | json_decref(array->table[index]); |
599 | | |
600 | | /* If we're removing the last element, nothing has to be moved */ |
601 | 0 | if (index < array->entries - 1) |
602 | 0 | array_move(array, index, index + 1, array->entries - index - 1); |
603 | |
|
604 | 0 | array->entries--; |
605 | |
|
606 | 0 | return 0; |
607 | 0 | } |
608 | | |
609 | 0 | int json_array_clear(json_t *json) { |
610 | 0 | json_array_t *array; |
611 | 0 | size_t i; |
612 | |
|
613 | 0 | if (!json_is_array(json)) |
614 | 0 | return -1; |
615 | 0 | array = json_to_array(json); |
616 | |
|
617 | 0 | for (i = 0; i < array->entries; i++) |
618 | 0 | json_decref(array->table[i]); |
619 | |
|
620 | 0 | array->entries = 0; |
621 | 0 | return 0; |
622 | 0 | } |
623 | | |
624 | 0 | int json_array_extend(json_t *json, json_t *other_json) { |
625 | 0 | json_array_t *array, *other; |
626 | 0 | size_t i; |
627 | |
|
628 | 0 | if (!json_is_array(json) || !json_is_array(other_json)) |
629 | 0 | return -1; |
630 | 0 | array = json_to_array(json); |
631 | 0 | other = json_to_array(other_json); |
632 | |
|
633 | 0 | if (!json_array_grow(array, other->entries, 1)) |
634 | 0 | return -1; |
635 | | |
636 | 0 | for (i = 0; i < other->entries; i++) |
637 | 0 | json_incref(other->table[i]); |
638 | |
|
639 | 0 | array_copy(array->table, array->entries, other->table, 0, other->entries); |
640 | |
|
641 | 0 | array->entries += other->entries; |
642 | 0 | return 0; |
643 | 0 | } |
644 | | |
645 | 0 | static int json_array_equal(const json_t *array1, const json_t *array2) { |
646 | 0 | size_t i, size; |
647 | |
|
648 | 0 | size = json_array_size(array1); |
649 | 0 | if (size != json_array_size(array2)) |
650 | 0 | return 0; |
651 | | |
652 | 0 | for (i = 0; i < size; i++) { |
653 | 0 | json_t *value1, *value2; |
654 | |
|
655 | 0 | value1 = json_array_get(array1, i); |
656 | 0 | value2 = json_array_get(array2, i); |
657 | |
|
658 | 0 | if (!json_equal(value1, value2)) |
659 | 0 | return 0; |
660 | 0 | } |
661 | | |
662 | 0 | return 1; |
663 | 0 | } |
664 | | |
665 | 0 | static json_t *json_array_copy(json_t *array) { |
666 | 0 | json_t *result; |
667 | 0 | size_t i; |
668 | |
|
669 | 0 | result = json_array(); |
670 | 0 | if (!result) |
671 | 0 | return NULL; |
672 | | |
673 | 0 | for (i = 0; i < json_array_size(array); i++) |
674 | 0 | json_array_append(result, json_array_get(array, i)); |
675 | |
|
676 | 0 | return result; |
677 | 0 | } |
678 | | |
679 | 0 | static json_t *json_array_deep_copy(const json_t *array, hashtable_t *parents) { |
680 | 0 | json_t *result; |
681 | 0 | size_t i; |
682 | 0 | char loop_key[LOOP_KEY_LEN]; |
683 | 0 | size_t loop_key_len; |
684 | |
|
685 | 0 | if (jsonp_loop_check(parents, array, loop_key, sizeof(loop_key), &loop_key_len)) |
686 | 0 | return NULL; |
687 | | |
688 | 0 | result = json_array(); |
689 | 0 | if (!result) |
690 | 0 | goto out; |
691 | | |
692 | 0 | for (i = 0; i < json_array_size(array); i++) { |
693 | 0 | if (json_array_append_new(result, |
694 | 0 | do_deep_copy(json_array_get(array, i), parents))) { |
695 | 0 | json_decref(result); |
696 | 0 | result = NULL; |
697 | 0 | break; |
698 | 0 | } |
699 | 0 | } |
700 | |
|
701 | 0 | out: |
702 | 0 | hashtable_del(parents, loop_key, loop_key_len); |
703 | |
|
704 | 0 | return result; |
705 | 0 | } |
706 | | |
707 | | /*** string ***/ |
708 | | |
709 | 0 | static json_t *string_create(const char *value, size_t len, int own) { |
710 | 0 | char *v; |
711 | 0 | json_string_t *string; |
712 | |
|
713 | 0 | if (!value) |
714 | 0 | return NULL; |
715 | | |
716 | 0 | if (own) |
717 | 0 | v = (char *)value; |
718 | 0 | else { |
719 | 0 | v = jsonp_strndup(value, len); |
720 | 0 | if (!v) |
721 | 0 | return NULL; |
722 | 0 | } |
723 | | |
724 | 0 | string = jsonp_malloc(sizeof(json_string_t)); |
725 | 0 | if (!string) { |
726 | 0 | jsonp_free(v); |
727 | 0 | return NULL; |
728 | 0 | } |
729 | 0 | json_init(&string->json, JSON_STRING); |
730 | 0 | string->value = v; |
731 | 0 | string->length = len; |
732 | |
|
733 | 0 | return &string->json; |
734 | 0 | } |
735 | | |
736 | 0 | json_t *json_string_nocheck(const char *value) { |
737 | 0 | if (!value) |
738 | 0 | return NULL; |
739 | | |
740 | 0 | return string_create(value, strlen(value), 0); |
741 | 0 | } |
742 | | |
743 | 0 | json_t *json_stringn_nocheck(const char *value, size_t len) { |
744 | 0 | return string_create(value, len, 0); |
745 | 0 | } |
746 | | |
747 | | /* this is private; "steal" is not a public API concept */ |
748 | 0 | json_t *jsonp_stringn_nocheck_own(const char *value, size_t len) { |
749 | 0 | return string_create(value, len, 1); |
750 | 0 | } |
751 | | |
752 | 0 | json_t *json_string(const char *value) { |
753 | 0 | if (!value) |
754 | 0 | return NULL; |
755 | | |
756 | 0 | return json_stringn(value, strlen(value)); |
757 | 0 | } |
758 | | |
759 | 0 | json_t *json_stringn(const char *value, size_t len) { |
760 | 0 | if (!value || !utf8_check_string(value, len)) |
761 | 0 | return NULL; |
762 | | |
763 | 0 | return json_stringn_nocheck(value, len); |
764 | 0 | } |
765 | | |
766 | 0 | const char *json_string_value(const json_t *json) { |
767 | 0 | if (!json_is_string(json)) |
768 | 0 | return NULL; |
769 | | |
770 | 0 | return json_to_string(json)->value; |
771 | 0 | } |
772 | | |
773 | 0 | size_t json_string_length(const json_t *json) { |
774 | 0 | if (!json_is_string(json)) |
775 | 0 | return 0; |
776 | | |
777 | 0 | return json_to_string(json)->length; |
778 | 0 | } |
779 | | |
780 | 0 | int json_string_set_nocheck(json_t *json, const char *value) { |
781 | 0 | if (!value) |
782 | 0 | return -1; |
783 | | |
784 | 0 | return json_string_setn_nocheck(json, value, strlen(value)); |
785 | 0 | } |
786 | | |
787 | 0 | int json_string_setn_nocheck(json_t *json, const char *value, size_t len) { |
788 | 0 | char *dup; |
789 | 0 | json_string_t *string; |
790 | |
|
791 | 0 | if (!json_is_string(json) || !value) |
792 | 0 | return -1; |
793 | | |
794 | 0 | dup = jsonp_strndup(value, len); |
795 | 0 | if (!dup) |
796 | 0 | return -1; |
797 | | |
798 | 0 | string = json_to_string(json); |
799 | 0 | jsonp_free(string->value); |
800 | 0 | string->value = dup; |
801 | 0 | string->length = len; |
802 | |
|
803 | 0 | return 0; |
804 | 0 | } |
805 | | |
806 | 0 | int json_string_set(json_t *json, const char *value) { |
807 | 0 | if (!value) |
808 | 0 | return -1; |
809 | | |
810 | 0 | return json_string_setn(json, value, strlen(value)); |
811 | 0 | } |
812 | | |
813 | 0 | int json_string_setn(json_t *json, const char *value, size_t len) { |
814 | 0 | if (!value || !utf8_check_string(value, len)) |
815 | 0 | return -1; |
816 | | |
817 | 0 | return json_string_setn_nocheck(json, value, len); |
818 | 0 | } |
819 | | |
820 | 0 | static void json_delete_string(json_string_t *string) { |
821 | 0 | jsonp_free(string->value); |
822 | 0 | jsonp_free(string); |
823 | 0 | } |
824 | | |
825 | 0 | static int json_string_equal(const json_t *string1, const json_t *string2) { |
826 | 0 | json_string_t *s1, *s2; |
827 | |
|
828 | 0 | s1 = json_to_string(string1); |
829 | 0 | s2 = json_to_string(string2); |
830 | 0 | return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length); |
831 | 0 | } |
832 | | |
833 | 0 | static json_t *json_string_copy(const json_t *string) { |
834 | 0 | json_string_t *s; |
835 | |
|
836 | 0 | s = json_to_string(string); |
837 | 0 | return json_stringn_nocheck(s->value, s->length); |
838 | 0 | } |
839 | | |
840 | 0 | json_t *json_vsprintf(const char *fmt, va_list ap) { |
841 | 0 | json_t *json = NULL; |
842 | 0 | int length; |
843 | 0 | char *buf; |
844 | 0 | va_list aq; |
845 | 0 | va_copy(aq, ap); |
846 | |
|
847 | 0 | length = vsnprintf(NULL, 0, fmt, ap); |
848 | 0 | if (length < 0) |
849 | 0 | goto out; |
850 | 0 | if (length == 0) { |
851 | 0 | json = json_string(""); |
852 | 0 | goto out; |
853 | 0 | } |
854 | | |
855 | 0 | buf = jsonp_malloc((size_t)length + 1); |
856 | 0 | if (!buf) |
857 | 0 | goto out; |
858 | | |
859 | 0 | vsnprintf(buf, (size_t)length + 1, fmt, aq); |
860 | 0 | if (!utf8_check_string(buf, length)) { |
861 | 0 | jsonp_free(buf); |
862 | 0 | goto out; |
863 | 0 | } |
864 | | |
865 | 0 | json = jsonp_stringn_nocheck_own(buf, length); |
866 | |
|
867 | 0 | out: |
868 | 0 | va_end(aq); |
869 | 0 | return json; |
870 | 0 | } |
871 | | |
872 | 0 | json_t *json_sprintf(const char *fmt, ...) { |
873 | 0 | json_t *result; |
874 | 0 | va_list ap; |
875 | |
|
876 | 0 | va_start(ap, fmt); |
877 | 0 | result = json_vsprintf(fmt, ap); |
878 | 0 | va_end(ap); |
879 | |
|
880 | 0 | return result; |
881 | 0 | } |
882 | | |
883 | | /*** integer ***/ |
884 | | |
885 | 0 | json_t *json_integer(json_int_t value) { |
886 | 0 | json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t)); |
887 | 0 | if (!integer) |
888 | 0 | return NULL; |
889 | 0 | json_init(&integer->json, JSON_INTEGER); |
890 | |
|
891 | 0 | integer->value = value; |
892 | 0 | return &integer->json; |
893 | 0 | } |
894 | | |
895 | 0 | json_int_t json_integer_value(const json_t *json) { |
896 | 0 | if (!json_is_integer(json)) |
897 | 0 | return 0; |
898 | | |
899 | 0 | return json_to_integer(json)->value; |
900 | 0 | } |
901 | | |
902 | 0 | int json_integer_set(json_t *json, json_int_t value) { |
903 | 0 | if (!json_is_integer(json)) |
904 | 0 | return -1; |
905 | | |
906 | 0 | json_to_integer(json)->value = value; |
907 | |
|
908 | 0 | return 0; |
909 | 0 | } |
910 | | |
911 | 0 | static void json_delete_integer(json_integer_t *integer) { jsonp_free(integer); } |
912 | | |
913 | 0 | static int json_integer_equal(const json_t *integer1, const json_t *integer2) { |
914 | 0 | return json_integer_value(integer1) == json_integer_value(integer2); |
915 | 0 | } |
916 | | |
917 | 0 | static json_t *json_integer_copy(const json_t *integer) { |
918 | 0 | return json_integer(json_integer_value(integer)); |
919 | 0 | } |
920 | | |
921 | | /*** real ***/ |
922 | | |
923 | 0 | json_t *json_real(double value) { |
924 | 0 | json_real_t *real; |
925 | |
|
926 | 0 | if (isnan(value) || isinf(value)) |
927 | 0 | return NULL; |
928 | | |
929 | 0 | real = jsonp_malloc(sizeof(json_real_t)); |
930 | 0 | if (!real) |
931 | 0 | return NULL; |
932 | 0 | json_init(&real->json, JSON_REAL); |
933 | |
|
934 | 0 | real->value = value; |
935 | 0 | return &real->json; |
936 | 0 | } |
937 | | |
938 | 0 | double json_real_value(const json_t *json) { |
939 | 0 | if (!json_is_real(json)) |
940 | 0 | return 0; |
941 | | |
942 | 0 | return json_to_real(json)->value; |
943 | 0 | } |
944 | | |
945 | 0 | int json_real_set(json_t *json, double value) { |
946 | 0 | if (!json_is_real(json) || isnan(value) || isinf(value)) |
947 | 0 | return -1; |
948 | | |
949 | 0 | json_to_real(json)->value = value; |
950 | |
|
951 | 0 | return 0; |
952 | 0 | } |
953 | | |
954 | 0 | static void json_delete_real(json_real_t *real) { jsonp_free(real); } |
955 | | |
956 | 0 | static int json_real_equal(const json_t *real1, const json_t *real2) { |
957 | 0 | return json_real_value(real1) == json_real_value(real2); |
958 | 0 | } |
959 | | |
960 | 0 | static json_t *json_real_copy(const json_t *real) { |
961 | 0 | return json_real(json_real_value(real)); |
962 | 0 | } |
963 | | |
964 | | /*** number ***/ |
965 | | |
966 | 0 | double json_number_value(const json_t *json) { |
967 | 0 | if (json_is_integer(json)) |
968 | 0 | return (double)json_integer_value(json); |
969 | 0 | else if (json_is_real(json)) |
970 | 0 | return json_real_value(json); |
971 | 0 | else |
972 | 0 | return 0.0; |
973 | 0 | } |
974 | | |
975 | | /*** simple values ***/ |
976 | | |
977 | 0 | json_t *json_true(void) { |
978 | 0 | static json_t the_true = {JSON_TRUE, (size_t)-1}; |
979 | 0 | return &the_true; |
980 | 0 | } |
981 | | |
982 | 0 | json_t *json_false(void) { |
983 | 0 | static json_t the_false = {JSON_FALSE, (size_t)-1}; |
984 | 0 | return &the_false; |
985 | 0 | } |
986 | | |
987 | 0 | json_t *json_null(void) { |
988 | 0 | static json_t the_null = {JSON_NULL, (size_t)-1}; |
989 | 0 | return &the_null; |
990 | 0 | } |
991 | | |
992 | | /*** deletion ***/ |
993 | | |
994 | 0 | void json_delete(json_t *json) { |
995 | 0 | if (!json) |
996 | 0 | return; |
997 | | |
998 | 0 | switch (json_typeof(json)) { |
999 | 0 | case JSON_OBJECT: |
1000 | 0 | json_delete_object(json_to_object(json)); |
1001 | 0 | break; |
1002 | 0 | case JSON_ARRAY: |
1003 | 0 | json_delete_array(json_to_array(json)); |
1004 | 0 | break; |
1005 | 0 | case JSON_STRING: |
1006 | 0 | json_delete_string(json_to_string(json)); |
1007 | 0 | break; |
1008 | 0 | case JSON_INTEGER: |
1009 | 0 | json_delete_integer(json_to_integer(json)); |
1010 | 0 | break; |
1011 | 0 | case JSON_REAL: |
1012 | 0 | json_delete_real(json_to_real(json)); |
1013 | 0 | break; |
1014 | 0 | default: |
1015 | 0 | return; |
1016 | 0 | } |
1017 | | |
1018 | | /* json_delete is not called for true, false or null */ |
1019 | 0 | } |
1020 | | |
1021 | | /*** equality ***/ |
1022 | | |
1023 | 0 | int json_equal(const json_t *json1, const json_t *json2) { |
1024 | 0 | if (!json1 || !json2) |
1025 | 0 | return 0; |
1026 | | |
1027 | 0 | if (json_typeof(json1) != json_typeof(json2)) |
1028 | 0 | return 0; |
1029 | | |
1030 | | /* this covers true, false and null as they are singletons */ |
1031 | 0 | if (json1 == json2) |
1032 | 0 | return 1; |
1033 | | |
1034 | 0 | switch (json_typeof(json1)) { |
1035 | 0 | case JSON_OBJECT: |
1036 | 0 | return json_object_equal(json1, json2); |
1037 | 0 | case JSON_ARRAY: |
1038 | 0 | return json_array_equal(json1, json2); |
1039 | 0 | case JSON_STRING: |
1040 | 0 | return json_string_equal(json1, json2); |
1041 | 0 | case JSON_INTEGER: |
1042 | 0 | return json_integer_equal(json1, json2); |
1043 | 0 | case JSON_REAL: |
1044 | 0 | return json_real_equal(json1, json2); |
1045 | 0 | default: |
1046 | 0 | return 0; |
1047 | 0 | } |
1048 | 0 | } |
1049 | | |
1050 | | /*** copying ***/ |
1051 | | |
1052 | 0 | json_t *json_copy(json_t *json) { |
1053 | 0 | if (!json) |
1054 | 0 | return NULL; |
1055 | | |
1056 | 0 | switch (json_typeof(json)) { |
1057 | 0 | case JSON_OBJECT: |
1058 | 0 | return json_object_copy(json); |
1059 | 0 | case JSON_ARRAY: |
1060 | 0 | return json_array_copy(json); |
1061 | 0 | case JSON_STRING: |
1062 | 0 | return json_string_copy(json); |
1063 | 0 | case JSON_INTEGER: |
1064 | 0 | return json_integer_copy(json); |
1065 | 0 | case JSON_REAL: |
1066 | 0 | return json_real_copy(json); |
1067 | 0 | case JSON_TRUE: |
1068 | 0 | case JSON_FALSE: |
1069 | 0 | case JSON_NULL: |
1070 | 0 | return json; |
1071 | 0 | default: |
1072 | 0 | return NULL; |
1073 | 0 | } |
1074 | 0 | } |
1075 | | |
1076 | 0 | json_t *json_deep_copy(const json_t *json) { |
1077 | 0 | json_t *res; |
1078 | 0 | hashtable_t parents_set; |
1079 | |
|
1080 | 0 | if (hashtable_init(&parents_set)) |
1081 | 0 | return NULL; |
1082 | 0 | res = do_deep_copy(json, &parents_set); |
1083 | 0 | hashtable_close(&parents_set); |
1084 | |
|
1085 | 0 | return res; |
1086 | 0 | } |
1087 | | |
1088 | 0 | json_t *do_deep_copy(const json_t *json, hashtable_t *parents) { |
1089 | 0 | if (!json) |
1090 | 0 | return NULL; |
1091 | | |
1092 | 0 | switch (json_typeof(json)) { |
1093 | 0 | case JSON_OBJECT: |
1094 | 0 | return json_object_deep_copy(json, parents); |
1095 | 0 | case JSON_ARRAY: |
1096 | 0 | return json_array_deep_copy(json, parents); |
1097 | | /* for the rest of the types, deep copying doesn't differ from |
1098 | | shallow copying */ |
1099 | 0 | case JSON_STRING: |
1100 | 0 | return json_string_copy(json); |
1101 | 0 | case JSON_INTEGER: |
1102 | 0 | return json_integer_copy(json); |
1103 | 0 | case JSON_REAL: |
1104 | 0 | return json_real_copy(json); |
1105 | 0 | case JSON_TRUE: |
1106 | 0 | case JSON_FALSE: |
1107 | 0 | case JSON_NULL: |
1108 | 0 | return (json_t *)json; |
1109 | 0 | default: |
1110 | | return NULL; |
1111 | 0 | } |
1112 | 0 | } |