Coverage Report

Created: 2026-09-03 06:06

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