Coverage Report

Created: 2025-09-05 07:11

/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.03M
static JSON_INLINE void json_init(json_t *json, json_type type) {
43
3.03M
    json->type = type;
44
3.03M
    json->refcount = 1;
45
3.03M
}
46
47
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size,
48
762k
                     size_t *key_len_out) {
49
762k
    size_t key_len = snprintf(key, key_size, "%p", json);
50
51
762k
    if (key_len_out)
52
762k
        *key_len_out = key_len;
53
54
762k
    if (hashtable_get(parents, key, key_len))
55
0
        return -1;
56
57
762k
    return hashtable_set(parents, key, key_len, json_null());
58
762k
}
59
60
/*** object ***/
61
62
extern volatile uint32_t hashtable_seed;
63
64
4.68k
json_t *json_object(void) {
65
4.68k
    json_object_t *object = jsonp_malloc(sizeof(json_object_t));
66
4.68k
    if (!object)
67
0
        return NULL;
68
69
4.68k
    if (!hashtable_seed) {
70
        /* Autoseed */
71
1
        json_object_seed(0);
72
1
    }
73
74
4.68k
    json_init(&object->json, JSON_OBJECT);
75
76
4.68k
    if (hashtable_init(&object->hashtable)) {
77
0
        jsonp_free(object);
78
0
        return NULL;
79
0
    }
80
81
4.68k
    return &object->json;
82
4.68k
}
83
84
4.68k
static void json_delete_object(json_object_t *object) {
85
4.68k
    hashtable_close(&object->hashtable);
86
4.68k
    jsonp_free(object);
87
4.68k
}
88
89
1.79k
size_t json_object_size(const json_t *json) {
90
1.79k
    json_object_t *object;
91
92
1.79k
    if (!json_is_object(json))
93
0
        return 0;
94
95
1.79k
    object = json_to_object(json);
96
1.79k
    return object->hashtable.size;
97
1.79k
}
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
7.90k
json_t *json_object_getn(const json_t *json, const char *key, size_t key_len) {
107
7.90k
    json_object_t *object;
108
109
7.90k
    if (!key || !json_is_object(json))
110
0
        return NULL;
111
112
7.90k
    object = json_to_object(json);
113
7.90k
    return hashtable_get(&object->hashtable, key, key_len);
114
7.90k
}
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
11.2k
                                 json_t *value) {
126
11.2k
    json_object_t *object;
127
128
11.2k
    if (!value)
129
0
        return -1;
130
131
11.2k
    if (!key || !json_is_object(json) || json == value) {
132
0
        json_decref(value);
133
0
        return -1;
134
0
    }
135
11.2k
    object = json_to_object(json);
136
137
11.2k
    if (hashtable_set(&object->hashtable, key, key_len, value)) {
138
0
        json_decref(value);
139
0
        return -1;
140
0
    }
141
142
11.2k
    return 0;
143
11.2k
}
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
3.47k
void *json_object_iter(json_t *json) {
288
3.47k
    json_object_t *object;
289
290
3.47k
    if (!json_is_object(json))
291
0
        return NULL;
292
293
3.47k
    object = json_to_object(json);
294
3.47k
    return hashtable_iter(&object->hashtable);
295
3.47k
}
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
8.05k
void *json_object_iter_next(json_t *json, void *iter) {
308
8.05k
    json_object_t *object;
309
310
8.05k
    if (!json_is_object(json) || iter == NULL)
311
0
        return NULL;
312
313
8.05k
    object = json_to_object(json);
314
8.05k
    return hashtable_iter_next(&object->hashtable, iter);
315
8.05k
}
316
317
8.05k
const char *json_object_iter_key(void *iter) {
318
8.05k
    if (!iter)
319
0
        return NULL;
320
321
8.05k
    return hashtable_iter_key(iter);
322
8.05k
}
323
324
8.05k
size_t json_object_iter_key_len(void *iter) {
325
8.05k
    if (!iter)
326
0
        return 0;
327
328
8.05k
    return hashtable_iter_key_len(iter);
329
8.05k
}
330
331
2.79k
json_t *json_object_iter_value(void *iter) {
332
2.79k
    if (!iter)
333
0
        return NULL;
334
335
2.79k
    return (json_t *)hashtable_iter_value(iter);
336
2.79k
}
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
803k
json_t *json_array(void) {
432
803k
    json_array_t *array = jsonp_malloc(sizeof(json_array_t));
433
803k
    if (!array)
434
0
        return NULL;
435
803k
    json_init(&array->json, JSON_ARRAY);
436
437
803k
    array->entries = 0;
438
803k
    array->size = 8;
439
440
803k
    array->table = jsonp_malloc(array->size * sizeof(json_t *));
441
803k
    if (!array->table) {
442
0
        jsonp_free(array);
443
0
        return NULL;
444
0
    }
445
446
803k
    return &array->json;
447
803k
}
448
449
803k
static void json_delete_array(json_array_t *array) {
450
803k
    size_t i;
451
452
3.79M
    for (i = 0; i < array->entries; i++)
453
2.98M
        json_decref(array->table[i]);
454
455
803k
    jsonp_free(array->table);
456
803k
    jsonp_free(array);
457
803k
}
458
459
759k
size_t json_array_size(const json_t *json) {
460
759k
    if (!json_is_array(json))
461
0
        return 0;
462
463
759k
    return json_to_array(json)->entries;
464
759k
}
465
466
2.97M
json_t *json_array_get(const json_t *json, size_t index) {
467
2.97M
    json_array_t *array;
468
2.97M
    if (!json_is_array(json))
469
0
        return NULL;
470
2.97M
    array = json_to_array(json);
471
472
2.97M
    if (index >= array->entries)
473
0
        return NULL;
474
475
2.97M
    return array->table[index];
476
2.97M
}
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
0
                       size_t count) {
507
0
    memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
508
0
}
509
510
2.98M
static json_t **json_array_grow(json_array_t *array, size_t amount) {
511
2.98M
    size_t new_size;
512
2.98M
    json_t **old_table, **new_table;
513
514
2.98M
    if (array->entries + amount <= array->size)
515
2.98M
        return array->table;
516
517
2.02k
    old_table = array->table;
518
519
2.02k
    new_size = max(array->size + amount, array->size * 2);
520
2.02k
    new_table = jsonp_realloc(old_table, array->size * sizeof(json_t *),
521
2.02k
                              new_size * sizeof(json_t *));
522
2.02k
    if (!new_table)
523
0
        return NULL;
524
525
2.02k
    array->size = new_size;
526
2.02k
    array->table = new_table;
527
528
2.02k
    return array->table;
529
2.02k
}
530
531
2.98M
int json_array_append_new(json_t *json, json_t *value) {
532
2.98M
    json_array_t *array;
533
534
2.98M
    if (!value)
535
0
        return -1;
536
537
2.98M
    if (!json_is_array(json) || json == value) {
538
0
        json_decref(value);
539
0
        return -1;
540
0
    }
541
2.98M
    array = json_to_array(json);
542
543
2.98M
    if (!json_array_grow(array, 1)) {
544
0
        json_decref(value);
545
0
        return -1;
546
0
    }
547
548
2.98M
    array->table[array->entries] = value;
549
2.98M
    array->entries++;
550
551
2.98M
    return 0;
552
2.98M
}
553
554
0
int json_array_insert_new(json_t *json, size_t index, json_t *value) {
555
0
    json_array_t *array;
556
557
0
    if (!value)
558
0
        return -1;
559
560
0
    if (!json_is_array(json) || json == value) {
561
0
        json_decref(value);
562
0
        return -1;
563
0
    }
564
0
    array = json_to_array(json);
565
566
0
    if (index > array->entries) {
567
0
        json_decref(value);
568
0
        return -1;
569
0
    }
570
571
0
    if (!json_array_grow(array, 1)) {
572
0
        json_decref(value);
573
0
        return -1;
574
0
    }
575
0
    if (index != array->entries)
576
0
        array_move(array, index + 1, index, array->entries - index);
577
578
0
    array->table[index] = value;
579
0
    array->entries++;
580
581
0
    return 0;
582
0
}
583
584
0
int json_array_remove(json_t *json, size_t index) {
585
0
    json_array_t *array;
586
587
0
    if (!json_is_array(json))
588
0
        return -1;
589
0
    array = json_to_array(json);
590
591
0
    if (index >= array->entries)
592
0
        return -1;
593
594
0
    json_decref(array->table[index]);
595
596
    /* If we're removing the last element, nothing has to be moved */
597
0
    if (index < array->entries - 1)
598
0
        array_move(array, index, index + 1, array->entries - index - 1);
599
600
0
    array->entries--;
601
602
0
    return 0;
603
0
}
604
605
0
int json_array_clear(json_t *json) {
606
0
    json_array_t *array;
607
0
    size_t i;
608
609
0
    if (!json_is_array(json))
610
0
        return -1;
611
0
    array = json_to_array(json);
612
613
0
    for (i = 0; i < array->entries; i++)
614
0
        json_decref(array->table[i]);
615
616
0
    array->entries = 0;
617
0
    return 0;
618
0
}
619
620
0
int json_array_extend(json_t *json, json_t *other_json) {
621
0
    json_array_t *array, *other;
622
0
    size_t i;
623
624
0
    if (!json_is_array(json) || !json_is_array(other_json))
625
0
        return -1;
626
0
    array = json_to_array(json);
627
0
    other = json_to_array(other_json);
628
629
0
    if (!json_array_grow(array, other->entries))
630
0
        return -1;
631
632
0
    for (i = 0; i < other->entries; i++)
633
0
        json_incref(other->table[i]);
634
635
0
    array_copy(array->table, array->entries, other->table, 0, other->entries);
636
637
0
    array->entries += other->entries;
638
0
    return 0;
639
0
}
640
641
0
static int json_array_equal(const json_t *array1, const json_t *array2) {
642
0
    size_t i, size;
643
644
0
    size = json_array_size(array1);
645
0
    if (size != json_array_size(array2))
646
0
        return 0;
647
648
0
    for (i = 0; i < size; i++) {
649
0
        json_t *value1, *value2;
650
651
0
        value1 = json_array_get(array1, i);
652
0
        value2 = json_array_get(array2, i);
653
654
0
        if (!json_equal(value1, value2))
655
0
            return 0;
656
0
    }
657
658
0
    return 1;
659
0
}
660
661
0
static json_t *json_array_copy(json_t *array) {
662
0
    json_t *result;
663
0
    size_t i;
664
665
0
    result = json_array();
666
0
    if (!result)
667
0
        return NULL;
668
669
0
    for (i = 0; i < json_array_size(array); i++)
670
0
        json_array_append(result, json_array_get(array, i));
671
672
0
    return result;
673
0
}
674
675
0
static json_t *json_array_deep_copy(const json_t *array, hashtable_t *parents) {
676
0
    json_t *result;
677
0
    size_t i;
678
0
    char loop_key[LOOP_KEY_LEN];
679
0
    size_t loop_key_len;
680
681
0
    if (jsonp_loop_check(parents, array, loop_key, sizeof(loop_key), &loop_key_len))
682
0
        return NULL;
683
684
0
    result = json_array();
685
0
    if (!result)
686
0
        goto out;
687
688
0
    for (i = 0; i < json_array_size(array); i++) {
689
0
        if (json_array_append_new(result,
690
0
                                  do_deep_copy(json_array_get(array, i), parents))) {
691
0
            json_decref(result);
692
0
            result = NULL;
693
0
            break;
694
0
        }
695
0
    }
696
697
0
out:
698
0
    hashtable_del(parents, loop_key, loop_key_len);
699
700
0
    return result;
701
0
}
702
703
/*** string ***/
704
705
1.77k
static json_t *string_create(const char *value, size_t len, int own) {
706
1.77k
    char *v;
707
1.77k
    json_string_t *string;
708
709
1.77k
    if (!value)
710
0
        return NULL;
711
712
1.77k
    if (own)
713
1.77k
        v = (char *)value;
714
0
    else {
715
0
        v = jsonp_strndup(value, len);
716
0
        if (!v)
717
0
            return NULL;
718
0
    }
719
720
1.77k
    string = jsonp_malloc(sizeof(json_string_t));
721
1.77k
    if (!string) {
722
0
        jsonp_free(v);
723
0
        return NULL;
724
0
    }
725
1.77k
    json_init(&string->json, JSON_STRING);
726
1.77k
    string->value = v;
727
1.77k
    string->length = len;
728
729
1.77k
    return &string->json;
730
1.77k
}
731
732
0
json_t *json_string_nocheck(const char *value) {
733
0
    if (!value)
734
0
        return NULL;
735
736
0
    return string_create(value, strlen(value), 0);
737
0
}
738
739
0
json_t *json_stringn_nocheck(const char *value, size_t len) {
740
0
    return string_create(value, len, 0);
741
0
}
742
743
/* this is private; "steal" is not a public API concept */
744
1.77k
json_t *jsonp_stringn_nocheck_own(const char *value, size_t len) {
745
1.77k
    return string_create(value, len, 1);
746
1.77k
}
747
748
0
json_t *json_string(const char *value) {
749
0
    if (!value)
750
0
        return NULL;
751
752
0
    return json_stringn(value, strlen(value));
753
0
}
754
755
0
json_t *json_stringn(const char *value, size_t len) {
756
0
    if (!value || !utf8_check_string(value, len))
757
0
        return NULL;
758
759
0
    return json_stringn_nocheck(value, len);
760
0
}
761
762
974
const char *json_string_value(const json_t *json) {
763
974
    if (!json_is_string(json))
764
0
        return NULL;
765
766
974
    return json_to_string(json)->value;
767
974
}
768
769
974
size_t json_string_length(const json_t *json) {
770
974
    if (!json_is_string(json))
771
0
        return 0;
772
773
974
    return json_to_string(json)->length;
774
974
}
775
776
0
int json_string_set_nocheck(json_t *json, const char *value) {
777
0
    if (!value)
778
0
        return -1;
779
780
0
    return json_string_setn_nocheck(json, value, strlen(value));
781
0
}
782
783
0
int json_string_setn_nocheck(json_t *json, const char *value, size_t len) {
784
0
    char *dup;
785
0
    json_string_t *string;
786
787
0
    if (!json_is_string(json) || !value)
788
0
        return -1;
789
790
0
    dup = jsonp_strndup(value, len);
791
0
    if (!dup)
792
0
        return -1;
793
794
0
    string = json_to_string(json);
795
0
    jsonp_free(string->value);
796
0
    string->value = dup;
797
0
    string->length = len;
798
799
0
    return 0;
800
0
}
801
802
0
int json_string_set(json_t *json, const char *value) {
803
0
    if (!value)
804
0
        return -1;
805
806
0
    return json_string_setn(json, value, strlen(value));
807
0
}
808
809
0
int json_string_setn(json_t *json, const char *value, size_t len) {
810
0
    if (!value || !utf8_check_string(value, len))
811
0
        return -1;
812
813
0
    return json_string_setn_nocheck(json, value, len);
814
0
}
815
816
1.77k
static void json_delete_string(json_string_t *string) {
817
1.77k
    jsonp_free(string->value);
818
1.77k
    jsonp_free(string);
819
1.77k
}
820
821
0
static int json_string_equal(const json_t *string1, const json_t *string2) {
822
0
    json_string_t *s1, *s2;
823
824
0
    s1 = json_to_string(string1);
825
0
    s2 = json_to_string(string2);
826
0
    return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length);
827
0
}
828
829
0
static json_t *json_string_copy(const json_t *string) {
830
0
    json_string_t *s;
831
832
0
    s = json_to_string(string);
833
0
    return json_stringn_nocheck(s->value, s->length);
834
0
}
835
836
0
json_t *json_vsprintf(const char *fmt, va_list ap) {
837
0
    json_t *json = NULL;
838
0
    int length;
839
0
    char *buf;
840
0
    va_list aq;
841
0
    va_copy(aq, ap);
842
843
0
    length = vsnprintf(NULL, 0, fmt, ap);
844
0
    if (length < 0)
845
0
        goto out;
846
0
    if (length == 0) {
847
0
        json = json_string("");
848
0
        goto out;
849
0
    }
850
851
0
    buf = jsonp_malloc((size_t)length + 1);
852
0
    if (!buf)
853
0
        goto out;
854
855
0
    vsnprintf(buf, (size_t)length + 1, fmt, aq);
856
0
    if (!utf8_check_string(buf, length)) {
857
0
        jsonp_free(buf);
858
0
        goto out;
859
0
    }
860
861
0
    json = jsonp_stringn_nocheck_own(buf, length);
862
863
0
out:
864
0
    va_end(aq);
865
0
    return json;
866
0
}
867
868
0
json_t *json_sprintf(const char *fmt, ...) {
869
0
    json_t *result;
870
0
    va_list ap;
871
872
0
    va_start(ap, fmt);
873
0
    result = json_vsprintf(fmt, ap);
874
0
    va_end(ap);
875
876
0
    return result;
877
0
}
878
879
/*** integer ***/
880
881
1.34M
json_t *json_integer(json_int_t value) {
882
1.34M
    json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
883
1.34M
    if (!integer)
884
0
        return NULL;
885
1.34M
    json_init(&integer->json, JSON_INTEGER);
886
887
1.34M
    integer->value = value;
888
1.34M
    return &integer->json;
889
1.34M
}
890
891
1.33M
json_int_t json_integer_value(const json_t *json) {
892
1.33M
    if (!json_is_integer(json))
893
0
        return 0;
894
895
1.33M
    return json_to_integer(json)->value;
896
1.33M
}
897
898
0
int json_integer_set(json_t *json, json_int_t value) {
899
0
    if (!json_is_integer(json))
900
0
        return -1;
901
902
0
    json_to_integer(json)->value = value;
903
904
0
    return 0;
905
0
}
906
907
1.34M
static void json_delete_integer(json_integer_t *integer) { jsonp_free(integer); }
908
909
0
static int json_integer_equal(const json_t *integer1, const json_t *integer2) {
910
0
    return json_integer_value(integer1) == json_integer_value(integer2);
911
0
}
912
913
0
static json_t *json_integer_copy(const json_t *integer) {
914
0
    return json_integer(json_integer_value(integer));
915
0
}
916
917
/*** real ***/
918
919
882k
json_t *json_real(double value) {
920
882k
    json_real_t *real;
921
922
882k
    if (isnan(value) || isinf(value))
923
0
        return NULL;
924
925
882k
    real = jsonp_malloc(sizeof(json_real_t));
926
882k
    if (!real)
927
0
        return NULL;
928
882k
    json_init(&real->json, JSON_REAL);
929
930
882k
    real->value = value;
931
882k
    return &real->json;
932
882k
}
933
934
878k
double json_real_value(const json_t *json) {
935
878k
    if (!json_is_real(json))
936
0
        return 0;
937
938
878k
    return json_to_real(json)->value;
939
878k
}
940
941
0
int json_real_set(json_t *json, double value) {
942
0
    if (!json_is_real(json) || isnan(value) || isinf(value))
943
0
        return -1;
944
945
0
    json_to_real(json)->value = value;
946
947
0
    return 0;
948
0
}
949
950
882k
static void json_delete_real(json_real_t *real) { jsonp_free(real); }
951
952
0
static int json_real_equal(const json_t *real1, const json_t *real2) {
953
0
    return json_real_value(real1) == json_real_value(real2);
954
0
}
955
956
0
static json_t *json_real_copy(const json_t *real) {
957
0
    return json_real(json_real_value(real));
958
0
}
959
960
/*** number ***/
961
962
0
double json_number_value(const json_t *json) {
963
0
    if (json_is_integer(json))
964
0
        return (double)json_integer_value(json);
965
0
    else if (json_is_real(json))
966
0
        return json_real_value(json);
967
0
    else
968
0
        return 0.0;
969
0
}
970
971
/*** simple values ***/
972
973
839
json_t *json_true(void) {
974
839
    static json_t the_true = {JSON_TRUE, (size_t)-1};
975
839
    return &the_true;
976
839
}
977
978
10.4k
json_t *json_false(void) {
979
10.4k
    static json_t the_false = {JSON_FALSE, (size_t)-1};
980
10.4k
    return &the_false;
981
10.4k
}
982
983
764k
json_t *json_null(void) {
984
764k
    static json_t the_null = {JSON_NULL, (size_t)-1};
985
764k
    return &the_null;
986
764k
}
987
988
/*** deletion ***/
989
990
3.03M
void json_delete(json_t *json) {
991
3.03M
    if (!json)
992
0
        return;
993
994
3.03M
    switch (json_typeof(json)) {
995
4.68k
        case JSON_OBJECT:
996
4.68k
            json_delete_object(json_to_object(json));
997
4.68k
            break;
998
803k
        case JSON_ARRAY:
999
803k
            json_delete_array(json_to_array(json));
1000
803k
            break;
1001
1.77k
        case JSON_STRING:
1002
1.77k
            json_delete_string(json_to_string(json));
1003
1.77k
            break;
1004
1.34M
        case JSON_INTEGER:
1005
1.34M
            json_delete_integer(json_to_integer(json));
1006
1.34M
            break;
1007
882k
        case JSON_REAL:
1008
882k
            json_delete_real(json_to_real(json));
1009
882k
            break;
1010
0
        default:
1011
0
            return;
1012
3.03M
    }
1013
1014
    /* json_delete is not called for true, false or null */
1015
3.03M
}
1016
1017
/*** equality ***/
1018
1019
0
int json_equal(const json_t *json1, const json_t *json2) {
1020
0
    if (!json1 || !json2)
1021
0
        return 0;
1022
1023
0
    if (json_typeof(json1) != json_typeof(json2))
1024
0
        return 0;
1025
1026
    /* this covers true, false and null as they are singletons */
1027
0
    if (json1 == json2)
1028
0
        return 1;
1029
1030
0
    switch (json_typeof(json1)) {
1031
0
        case JSON_OBJECT:
1032
0
            return json_object_equal(json1, json2);
1033
0
        case JSON_ARRAY:
1034
0
            return json_array_equal(json1, json2);
1035
0
        case JSON_STRING:
1036
0
            return json_string_equal(json1, json2);
1037
0
        case JSON_INTEGER:
1038
0
            return json_integer_equal(json1, json2);
1039
0
        case JSON_REAL:
1040
0
            return json_real_equal(json1, json2);
1041
0
        default:
1042
0
            return 0;
1043
0
    }
1044
0
}
1045
1046
/*** copying ***/
1047
1048
0
json_t *json_copy(json_t *json) {
1049
0
    if (!json)
1050
0
        return NULL;
1051
1052
0
    switch (json_typeof(json)) {
1053
0
        case JSON_OBJECT:
1054
0
            return json_object_copy(json);
1055
0
        case JSON_ARRAY:
1056
0
            return json_array_copy(json);
1057
0
        case JSON_STRING:
1058
0
            return json_string_copy(json);
1059
0
        case JSON_INTEGER:
1060
0
            return json_integer_copy(json);
1061
0
        case JSON_REAL:
1062
0
            return json_real_copy(json);
1063
0
        case JSON_TRUE:
1064
0
        case JSON_FALSE:
1065
0
        case JSON_NULL:
1066
0
            return json;
1067
0
        default:
1068
0
            return NULL;
1069
0
    }
1070
0
}
1071
1072
0
json_t *json_deep_copy(const json_t *json) {
1073
0
    json_t *res;
1074
0
    hashtable_t parents_set;
1075
1076
0
    if (hashtable_init(&parents_set))
1077
0
        return NULL;
1078
0
    res = do_deep_copy(json, &parents_set);
1079
0
    hashtable_close(&parents_set);
1080
1081
0
    return res;
1082
0
}
1083
1084
0
json_t *do_deep_copy(const json_t *json, hashtable_t *parents) {
1085
0
    if (!json)
1086
0
        return NULL;
1087
1088
0
    switch (json_typeof(json)) {
1089
0
        case JSON_OBJECT:
1090
0
            return json_object_deep_copy(json, parents);
1091
0
        case JSON_ARRAY:
1092
0
            return json_array_deep_copy(json, parents);
1093
            /* for the rest of the types, deep copying doesn't differ from
1094
               shallow copying */
1095
0
        case JSON_STRING:
1096
0
            return json_string_copy(json);
1097
0
        case JSON_INTEGER:
1098
0
            return json_integer_copy(json);
1099
0
        case JSON_REAL:
1100
0
            return json_real_copy(json);
1101
0
        case JSON_TRUE:
1102
0
        case JSON_FALSE:
1103
0
        case JSON_NULL:
1104
0
            return (json_t *)json;
1105
0
        default:
1106
0
            return NULL;
1107
0
    }
1108
0
}