-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathjson.c
4328 lines (3275 loc) · 142 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include "sd-messages.h"
#include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "float.h"
#include "hexdecoct.h"
#include "json-internal.h"
#include "json.h"
#include "macro.h"
#include "memory-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
#include "utf8.h"
/* Refuse putting together variants with a larger depth than 2K by default (as a protection against overflowing stacks
* if code processes JSON objects recursively. Note that we store the depth in an uint16_t, hence make sure this
* remains under 2^16.
*
* The value first was 16k, but it was discovered to be too high on llvm/x86-64. See also:
* https://github.com/systemd/systemd/issues/10738
*
* The value then was 4k, but it was discovered to be too high on s390x/aarch64. See also:
* https://github.com/systemd/systemd/issues/14396 */
#define DEPTH_MAX (2U*1024U)
assert_cc(DEPTH_MAX <= UINT16_MAX);
typedef struct JsonSource {
/* When we parse from a file or similar, encodes the filename, to indicate the source of a json variant */
size_t n_ref;
unsigned max_line;
unsigned max_column;
char name[];
} JsonSource;
/* On x86-64 this whole structure should have a size of 6 * 64 bit = 48 bytes */
struct JsonVariant {
union {
/* We either maintain a reference counter for this variant itself, or we are embedded into an
* array/object, in which case only that surrounding object is ref-counted. (If 'embedded' is false,
* see below.) */
size_t n_ref;
/* If this JsonVariant is part of an array/object, then this field points to the surrounding
* JSON_VARIANT_ARRAY/JSON_VARIANT_OBJECT object. (If 'embedded' is true, see below.) */
JsonVariant *parent;
};
/* If this was parsed from some file or buffer, this stores where from, as well as the source line/column */
JsonSource *source;
unsigned line, column;
JsonVariantType type:5;
/* A marker whether this variant is embedded into in array/object or not. If true, the 'parent' pointer above
* is valid. If false, the 'n_ref' field above is valid instead. */
bool is_embedded:1;
/* In some conditions (for example, if this object is part of an array of strings or objects), we don't store
* any data inline, but instead simply reference an external object and act as surrogate of it. In that case
* this bool is set, and the external object is referenced through the .reference field below. */
bool is_reference:1;
/* While comparing two arrays, we use this for marking what we already have seen */
bool is_marked:1;
/* Erase from memory when freeing */
bool sensitive:1;
/* If this is an object the fields are strictly ordered by name */
bool sorted:1;
/* If in addition to this object all objects referenced by it are also ordered strictly by name */
bool normalized:1;
/* The current 'depth' of the JsonVariant, i.e. how many levels of member variants this has */
uint16_t depth;
union {
/* For simple types we store the value in-line. */
JsonValue value;
/* For objects and arrays we store the number of elements immediately following */
size_t n_elements;
/* If is_reference as indicated above is set, this is where the reference object is actually stored. */
JsonVariant *reference;
/* Strings are placed immediately after the structure. Note that when this is a JsonVariant embedded
* into an array we might encode strings up to INLINE_STRING_LENGTH characters directly inside the
* element, while longer strings are stored as references. When this object is not embedded into an
* array, but stand-alone we allocate the right size for the whole structure, i.e. the array might be
* much larger than INLINE_STRING_LENGTH.
*
* Note that because we want to allocate arrays of the JsonVariant structure we specify [0] here,
* rather than the prettier []. If we wouldn't, then this char array would have undefined size, and so
* would the union and then the struct this is included in. And of structures with undefined size we
* can't allocate arrays (at least not easily). */
char string[0];
};
};
/* Inside string arrays we have a series of JasonVariant structures one after the other. In this case, strings longer
* than INLINE_STRING_MAX are stored as references, and all shorter ones inline. (This means — on x86-64 — strings up
* to 15 chars are stored within the array elements, and all others in separate allocations) */
#define INLINE_STRING_MAX (sizeof(JsonVariant) - offsetof(JsonVariant, string) - 1U)
/* Let's make sure this structure isn't increased in size accidentally. This check is only for our most relevant arch
* (x86-64). */
#ifdef __x86_64__
assert_cc(sizeof(JsonVariant) == 48U);
assert_cc(INLINE_STRING_MAX == 15U);
#endif
static JsonSource* json_source_new(const char *name) {
JsonSource *s;
assert(name);
s = malloc(offsetof(JsonSource, name) + strlen(name) + 1);
if (!s)
return NULL;
*s = (JsonSource) {
.n_ref = 1,
};
strcpy(s->name, name);
return s;
}
DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(JsonSource, json_source, mfree);
static bool json_source_equal(JsonSource *a, JsonSource *b) {
if (a == b)
return true;
if (!a || !b)
return false;
return streq(a->name, b->name);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonSource*, json_source_unref);
/* There are four kind of JsonVariant* pointers:
*
* 1. NULL
* 2. A 'regular' one, i.e. pointing to malloc() memory
* 3. A 'magic' one, i.e. one of the special JSON_VARIANT_MAGIC_XYZ values, that encode a few very basic values directly in the pointer.
* 4. A 'const string' one, i.e. a pointer to a const string.
*
* The four kinds of pointers can be discerned like this:
*
* Detecting #1 is easy, just compare with NULL. Detecting #3 is similarly easy: all magic pointers are below
* _JSON_VARIANT_MAGIC_MAX (which is pretty low, within the first memory page, which is special on Linux and other
* OSes, as it is a faulting page). In order to discern #2 and #4 we check the lowest bit. If it's off it's #2,
* otherwise #4. This makes use of the fact that malloc() will return "maximum aligned" memory, which definitely
* means the pointer is even. This means we can use the uneven pointers to reference static strings, as long as we
* make sure that all static strings used like this are aligned to 2 (or higher), and that we mask the bit on
* access. The JSON_VARIANT_STRING_CONST() macro encodes strings as JsonVariant* pointers, with the bit set. */
static bool json_variant_is_magic(const JsonVariant *v) {
if (!v)
return false;
return v < _JSON_VARIANT_MAGIC_MAX;
}
static bool json_variant_is_const_string(const JsonVariant *v) {
if (v < _JSON_VARIANT_MAGIC_MAX)
return false;
/* A proper JsonVariant is aligned to whatever malloc() aligns things too, which is definitely not uneven. We
* hence use all uneven pointers as indicators for const strings. */
return (((uintptr_t) v) & 1) != 0;
}
static bool json_variant_is_regular(const JsonVariant *v) {
if (v < _JSON_VARIANT_MAGIC_MAX)
return false;
return (((uintptr_t) v) & 1) == 0;
}
static JsonVariant *json_variant_dereference(JsonVariant *v) {
/* Recursively dereference variants that are references to other variants */
if (!v)
return NULL;
if (!json_variant_is_regular(v))
return v;
if (!v->is_reference)
return v;
return json_variant_dereference(v->reference);
}
static uint16_t json_variant_depth(JsonVariant *v) {
v = json_variant_dereference(v);
if (!v)
return 0;
if (!json_variant_is_regular(v))
return 0;
return v->depth;
}
static JsonVariant *json_variant_formalize(JsonVariant *v) {
/* Converts json variant pointers to their normalized form, i.e. fully dereferenced and wherever
* possible converted to the "magic" version if there is one */
if (!v)
return NULL;
v = json_variant_dereference(v);
switch (json_variant_type(v)) {
case JSON_VARIANT_BOOLEAN:
return json_variant_boolean(v) ? JSON_VARIANT_MAGIC_TRUE : JSON_VARIANT_MAGIC_FALSE;
case JSON_VARIANT_NULL:
return JSON_VARIANT_MAGIC_NULL;
case JSON_VARIANT_INTEGER:
return json_variant_integer(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_INTEGER : v;
case JSON_VARIANT_UNSIGNED:
return json_variant_unsigned(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED : v;
case JSON_VARIANT_REAL:
DISABLE_WARNING_FLOAT_EQUAL;
return json_variant_real(v) == 0.0 ? JSON_VARIANT_MAGIC_ZERO_REAL : v;
REENABLE_WARNING;
case JSON_VARIANT_STRING:
return isempty(json_variant_string(v)) ? JSON_VARIANT_MAGIC_EMPTY_STRING : v;
case JSON_VARIANT_ARRAY:
return json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_ARRAY : v;
case JSON_VARIANT_OBJECT:
return json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_OBJECT : v;
default:
return v;
}
}
static JsonVariant *json_variant_conservative_formalize(JsonVariant *v) {
/* Much like json_variant_formalize(), but won't simplify if the variant has a source/line location attached to
* it, in order not to lose context */
if (!v)
return NULL;
if (!json_variant_is_regular(v))
return v;
if (v->source || v->line > 0 || v->column > 0)
return v;
return json_variant_formalize(v);
}
static int json_variant_new(JsonVariant **ret, JsonVariantType type, size_t space) {
JsonVariant *v;
assert_return(ret, -EINVAL);
v = malloc0(MAX(sizeof(JsonVariant),
offsetof(JsonVariant, value) + space));
if (!v)
return -ENOMEM;
v->n_ref = 1;
v->type = type;
*ret = v;
return 0;
}
int json_variant_new_integer(JsonVariant **ret, intmax_t i) {
JsonVariant *v;
int r;
assert_return(ret, -EINVAL);
if (i == 0) {
*ret = JSON_VARIANT_MAGIC_ZERO_INTEGER;
return 0;
}
r = json_variant_new(&v, JSON_VARIANT_INTEGER, sizeof(i));
if (r < 0)
return r;
v->value.integer = i;
*ret = v;
return 0;
}
int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u) {
JsonVariant *v;
int r;
assert_return(ret, -EINVAL);
if (u == 0) {
*ret = JSON_VARIANT_MAGIC_ZERO_UNSIGNED;
return 0;
}
r = json_variant_new(&v, JSON_VARIANT_UNSIGNED, sizeof(u));
if (r < 0)
return r;
v->value.unsig = u;
*ret = v;
return 0;
}
int json_variant_new_real(JsonVariant **ret, long double d) {
JsonVariant *v;
int r;
assert_return(ret, -EINVAL);
DISABLE_WARNING_FLOAT_EQUAL;
if (d == 0.0) {
*ret = JSON_VARIANT_MAGIC_ZERO_REAL;
return 0;
}
REENABLE_WARNING;
r = json_variant_new(&v, JSON_VARIANT_REAL, sizeof(d));
if (r < 0)
return r;
v->value.real = d;
*ret = v;
return 0;
}
int json_variant_new_boolean(JsonVariant **ret, bool b) {
assert_return(ret, -EINVAL);
if (b)
*ret = JSON_VARIANT_MAGIC_TRUE;
else
*ret = JSON_VARIANT_MAGIC_FALSE;
return 0;
}
int json_variant_new_null(JsonVariant **ret) {
assert_return(ret, -EINVAL);
*ret = JSON_VARIANT_MAGIC_NULL;
return 0;
}
int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n) {
JsonVariant *v;
int r;
assert_return(ret, -EINVAL);
if (!s) {
assert_return(IN_SET(n, 0, (size_t) -1), -EINVAL);
return json_variant_new_null(ret);
}
if (n == (size_t) -1) /* determine length automatically */
n = strlen(s);
else if (memchr(s, 0, n)) /* don't allow embedded NUL, as we can't express that in JSON */
return -EINVAL;
if (n == 0) {
*ret = JSON_VARIANT_MAGIC_EMPTY_STRING;
return 0;
}
r = json_variant_new(&v, JSON_VARIANT_STRING, n + 1);
if (r < 0)
return r;
memcpy(v->string, s, n);
v->string[n] = 0;
*ret = v;
return 0;
}
int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n) {
_cleanup_free_ char *s = NULL;
ssize_t k;
assert_return(ret, -EINVAL);
assert_return(n == 0 || p, -EINVAL);
k = base64mem(p, n, &s);
if (k < 0)
return k;
return json_variant_new_stringn(ret, s, k);
}
static void json_variant_set(JsonVariant *a, JsonVariant *b) {
assert(a);
b = json_variant_dereference(b);
if (!b) {
a->type = JSON_VARIANT_NULL;
return;
}
a->type = json_variant_type(b);
switch (a->type) {
case JSON_VARIANT_INTEGER:
a->value.integer = json_variant_integer(b);
break;
case JSON_VARIANT_UNSIGNED:
a->value.unsig = json_variant_unsigned(b);
break;
case JSON_VARIANT_REAL:
a->value.real = json_variant_real(b);
break;
case JSON_VARIANT_BOOLEAN:
a->value.boolean = json_variant_boolean(b);
break;
case JSON_VARIANT_STRING: {
const char *s;
assert_se(s = json_variant_string(b));
/* Short strings we can store inline */
if (strnlen(s, INLINE_STRING_MAX+1) <= INLINE_STRING_MAX) {
strcpy(a->string, s);
break;
}
/* For longer strings, use a reference… */
_fallthrough_;
}
case JSON_VARIANT_ARRAY:
case JSON_VARIANT_OBJECT:
a->is_reference = true;
a->reference = json_variant_ref(json_variant_conservative_formalize(b));
break;
case JSON_VARIANT_NULL:
break;
default:
assert_not_reached("Unexpected variant type");
}
}
static void json_variant_copy_source(JsonVariant *v, JsonVariant *from) {
assert(v);
assert(from);
if (!json_variant_is_regular(from))
return;
v->line = from->line;
v->column = from->column;
v->source = json_source_ref(from->source);
}
int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
bool normalized = true;
assert_return(ret, -EINVAL);
if (n == 0) {
*ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
return 0;
}
assert_return(array, -EINVAL);
v = new(JsonVariant, n + 1);
if (!v)
return -ENOMEM;
*v = (JsonVariant) {
.n_ref = 1,
.type = JSON_VARIANT_ARRAY,
};
for (v->n_elements = 0; v->n_elements < n; v->n_elements++) {
JsonVariant *w = v + 1 + v->n_elements,
*c = array[v->n_elements];
uint16_t d;
d = json_variant_depth(c);
if (d >= DEPTH_MAX) /* Refuse too deep nesting */
return -ELNRNG;
if (d >= v->depth)
v->depth = d + 1;
*w = (JsonVariant) {
.is_embedded = true,
.parent = v,
};
json_variant_set(w, c);
json_variant_copy_source(w, c);
if (!json_variant_is_normalized(c))
normalized = false;
}
v->normalized = normalized;
*ret = TAKE_PTR(v);
return 0;
}
int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) {
JsonVariant *v;
size_t i;
assert_return(ret, -EINVAL);
if (n == 0) {
*ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
return 0;
}
assert_return(p, -EINVAL);
v = new(JsonVariant, n + 1);
if (!v)
return -ENOMEM;
*v = (JsonVariant) {
.n_ref = 1,
.type = JSON_VARIANT_ARRAY,
.n_elements = n,
.depth = 1,
};
for (i = 0; i < n; i++) {
JsonVariant *w = v + 1 + i;
*w = (JsonVariant) {
.is_embedded = true,
.parent = v,
.type = JSON_VARIANT_UNSIGNED,
.value.unsig = ((const uint8_t*) p)[i],
};
}
v->normalized = true;
*ret = v;
return 0;
}
int json_variant_new_array_strv(JsonVariant **ret, char **l) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
size_t n;
int r;
assert(ret);
n = strv_length(l);
if (n == 0) {
*ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
return 0;
}
v = new(JsonVariant, n + 1);
if (!v)
return -ENOMEM;
*v = (JsonVariant) {
.n_ref = 1,
.type = JSON_VARIANT_ARRAY,
.depth = 1,
};
for (v->n_elements = 0; v->n_elements < n; v->n_elements++) {
JsonVariant *w = v + 1 + v->n_elements;
size_t k;
*w = (JsonVariant) {
.is_embedded = true,
.parent = v,
.type = JSON_VARIANT_STRING,
};
k = strlen(l[v->n_elements]);
if (k > INLINE_STRING_MAX) {
/* If string is too long, store it as reference. */
r = json_variant_new_string(&w->reference, l[v->n_elements]);
if (r < 0)
return r;
w->is_reference = true;
} else
memcpy(w->string, l[v->n_elements], k+1);
}
v->normalized = true;
*ret = TAKE_PTR(v);
return 0;
}
int json_variant_new_object(JsonVariant **ret, JsonVariant **array, size_t n) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
const char *prev = NULL;
bool sorted = true, normalized = true;
assert_return(ret, -EINVAL);
if (n == 0) {
*ret = JSON_VARIANT_MAGIC_EMPTY_OBJECT;
return 0;
}
assert_return(array, -EINVAL);
assert_return(n % 2 == 0, -EINVAL);
v = new(JsonVariant, n + 1);
if (!v)
return -ENOMEM;
*v = (JsonVariant) {
.n_ref = 1,
.type = JSON_VARIANT_OBJECT,
};
for (v->n_elements = 0; v->n_elements < n; v->n_elements++) {
JsonVariant *w = v + 1 + v->n_elements,
*c = array[v->n_elements];
uint16_t d;
if ((v->n_elements & 1) == 0) {
const char *k;
if (!json_variant_is_string(c))
return -EINVAL; /* Every second one needs to be a string, as it is the key name */
assert_se(k = json_variant_string(c));
if (prev && strcmp(k, prev) <= 0)
sorted = normalized = false;
prev = k;
} else if (!json_variant_is_normalized(c))
normalized = false;
d = json_variant_depth(c);
if (d >= DEPTH_MAX) /* Refuse too deep nesting */
return -ELNRNG;
if (d >= v->depth)
v->depth = d + 1;
*w = (JsonVariant) {
.is_embedded = true,
.parent = v,
};
json_variant_set(w, c);
json_variant_copy_source(w, c);
}
v->normalized = normalized;
v->sorted = sorted;
*ret = TAKE_PTR(v);
return 0;
}
static size_t json_variant_size(JsonVariant* v) {
if (!json_variant_is_regular(v))
return 0;
if (v->is_reference)
return offsetof(JsonVariant, reference) + sizeof(JsonVariant*);
switch (v->type) {
case JSON_VARIANT_STRING:
return offsetof(JsonVariant, string) + strlen(v->string) + 1;
case JSON_VARIANT_REAL:
return offsetof(JsonVariant, value) + sizeof(long double);
case JSON_VARIANT_UNSIGNED:
return offsetof(JsonVariant, value) + sizeof(uintmax_t);
case JSON_VARIANT_INTEGER:
return offsetof(JsonVariant, value) + sizeof(intmax_t);
case JSON_VARIANT_BOOLEAN:
return offsetof(JsonVariant, value) + sizeof(bool);
case JSON_VARIANT_ARRAY:
case JSON_VARIANT_OBJECT:
return offsetof(JsonVariant, n_elements) + sizeof(size_t);
case JSON_VARIANT_NULL:
return offsetof(JsonVariant, value);
default:
assert_not_reached("unexpected type");
}
}
static void json_variant_free_inner(JsonVariant *v, bool force_sensitive) {
bool sensitive;
assert(v);
if (!json_variant_is_regular(v))
return;
json_source_unref(v->source);
sensitive = v->sensitive || force_sensitive;
if (v->is_reference) {
if (sensitive)
json_variant_sensitive(v->reference);
json_variant_unref(v->reference);
return;
}
if (IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT)) {
size_t i;
for (i = 0; i < v->n_elements; i++)
json_variant_free_inner(v + 1 + i, sensitive);
}
if (sensitive)
explicit_bzero_safe(v, json_variant_size(v));
}
JsonVariant *json_variant_ref(JsonVariant *v) {
if (!v)
return NULL;
if (!json_variant_is_regular(v))
return v;
if (v->is_embedded)
json_variant_ref(v->parent); /* ref the compounding variant instead */
else {
assert(v->n_ref > 0);
v->n_ref++;
}
return v;
}
JsonVariant *json_variant_unref(JsonVariant *v) {
if (!v)
return NULL;
if (!json_variant_is_regular(v))
return NULL;
if (v->is_embedded)
json_variant_unref(v->parent);
else {
assert(v->n_ref > 0);
v->n_ref--;
if (v->n_ref == 0) {
json_variant_free_inner(v, false);
free(v);
}
}
return NULL;
}
void json_variant_unref_many(JsonVariant **array, size_t n) {
size_t i;
assert(array || n == 0);
for (i = 0; i < n; i++)
json_variant_unref(array[i]);
}
const char *json_variant_string(JsonVariant *v) {
if (!v)
return NULL;
if (v == JSON_VARIANT_MAGIC_EMPTY_STRING)
return "";
if (json_variant_is_magic(v))
goto mismatch;
if (json_variant_is_const_string(v)) {
uintptr_t p = (uintptr_t) v;
assert((p & 1) != 0);
return (const char*) (p ^ 1U);
}
if (v->is_reference)
return json_variant_string(v->reference);
if (v->type != JSON_VARIANT_STRING)
goto mismatch;
return v->string;
mismatch:
log_debug("Non-string JSON variant requested as string, returning NULL.");
return NULL;
}
bool json_variant_boolean(JsonVariant *v) {
if (!v)
goto mismatch;
if (v == JSON_VARIANT_MAGIC_TRUE)
return true;
if (v == JSON_VARIANT_MAGIC_FALSE)
return false;
if (!json_variant_is_regular(v))
goto mismatch;
if (v->type != JSON_VARIANT_BOOLEAN)
goto mismatch;
if (v->is_reference)
return json_variant_boolean(v->reference);
return v->value.boolean;
mismatch:
log_debug("Non-boolean JSON variant requested as boolean, returning false.");
return false;
}
intmax_t json_variant_integer(JsonVariant *v) {
if (!v)
goto mismatch;
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
v == JSON_VARIANT_MAGIC_ZERO_UNSIGNED ||
v == JSON_VARIANT_MAGIC_ZERO_REAL)
return 0;
if (!json_variant_is_regular(v))
goto mismatch;
if (v->is_reference)
return json_variant_integer(v->reference);
switch (v->type) {
case JSON_VARIANT_INTEGER:
return v->value.integer;
case JSON_VARIANT_UNSIGNED:
if (v->value.unsig <= INTMAX_MAX)
return (intmax_t) v->value.unsig;
log_debug("Unsigned integer %ju requested as signed integer and out of range, returning 0.", v->value.unsig);
return 0;
case JSON_VARIANT_REAL: {
intmax_t converted;
converted = (intmax_t) v->value.real;
DISABLE_WARNING_FLOAT_EQUAL;
if ((long double) converted == v->value.real)
return converted;
REENABLE_WARNING;
log_debug("Real %Lg requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
return 0;
}
default:
break;
}
mismatch:
log_debug("Non-integer JSON variant requested as integer, returning 0.");
return 0;
}
uintmax_t json_variant_unsigned(JsonVariant *v) {
if (!v)
goto mismatch;
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
v == JSON_VARIANT_MAGIC_ZERO_UNSIGNED ||
v == JSON_VARIANT_MAGIC_ZERO_REAL)
return 0;
if (!json_variant_is_regular(v))
goto mismatch;
if (v->is_reference)
return json_variant_integer(v->reference);
switch (v->type) {
case JSON_VARIANT_INTEGER:
if (v->value.integer >= 0)
return (uintmax_t) v->value.integer;
log_debug("Signed integer %ju requested as unsigned integer and out of range, returning 0.", v->value.integer);
return 0;
case JSON_VARIANT_UNSIGNED:
return v->value.unsig;
case JSON_VARIANT_REAL: {
uintmax_t converted;
converted = (uintmax_t) v->value.real;
DISABLE_WARNING_FLOAT_EQUAL;
if ((long double) converted == v->value.real)
return converted;
REENABLE_WARNING;
log_debug("Real %Lg requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
return 0;
}
default:
break;
}
mismatch:
log_debug("Non-integer JSON variant requested as unsigned, returning 0.");
return 0;
}
long double json_variant_real(JsonVariant *v) {
if (!v)
return 0.0;
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
v == JSON_VARIANT_MAGIC_ZERO_UNSIGNED ||
v == JSON_VARIANT_MAGIC_ZERO_REAL)
return 0.0;
if (!json_variant_is_regular(v))
goto mismatch;
if (v->is_reference)
return json_variant_real(v->reference);
switch (v->type) {
case JSON_VARIANT_REAL:
return v->value.real;
case JSON_VARIANT_INTEGER: {
long double converted;
converted = (long double) v->value.integer;
if ((intmax_t) converted == v->value.integer)
return converted;
log_debug("Signed integer %ji requested as real, and cannot be converted losslessly, returning 0.", v->value.integer);
return 0.0;
}
case JSON_VARIANT_UNSIGNED: {
long double converted;
converted = (long double) v->value.unsig;
if ((uintmax_t) converted == v->value.unsig)
return converted;
log_debug("Unsigned integer %ju requested as real, and cannot be converted losslessly, returning 0.", v->value.unsig);
return 0.0;