Coverage Report

Created: 2023-06-07 06:28

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