1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "errno-util.h"
17 #include "hexdecoct.h"
18 #include "json-internal.h"
21 #include "memory-util.h"
22 #include "string-table.h"
23 #include "string-util.h"
25 #include "terminal-util.h"
26 #include "user-util.h"
29 /* Refuse putting together variants with a larger depth than 2K by default (as a protection against overflowing stacks
30 * if code processes JSON objects recursively. Note that we store the depth in an uint16_t, hence make sure this
33 * The value first was 16k, but it was discovered to be too high on llvm/x86-64. See also:
34 * https://github.com/systemd/systemd/issues/10738
36 * The value then was 4k, but it was discovered to be too high on s390x/aarch64. See also:
37 * https://github.com/systemd/systemd/issues/14396 */
39 #define DEPTH_MAX (2U*1024U)
40 assert_cc(DEPTH_MAX
<= UINT16_MAX
);
42 typedef struct JsonSource
{
43 /* When we parse from a file or similar, encodes the filename, to indicate the source of a json variant */
50 /* On x86-64 this whole structure should have a size of 6 * 64 bit = 48 bytes */
53 /* We either maintain a reference counter for this variant itself, or we are embedded into an
54 * array/object, in which case only that surrounding object is ref-counted. (If 'embedded' is false,
58 /* If this JsonVariant is part of an array/object, then this field points to the surrounding
59 * JSON_VARIANT_ARRAY/JSON_VARIANT_OBJECT object. (If 'embedded' is true, see below.) */
63 /* If this was parsed from some file or buffer, this stores where from, as well as the source line/column */
65 unsigned line
, column
;
67 JsonVariantType type
:5;
69 /* A marker whether this variant is embedded into in array/object or not. If true, the 'parent' pointer above
70 * is valid. If false, the 'n_ref' field above is valid instead. */
73 /* In some conditions (for example, if this object is part of an array of strings or objects), we don't store
74 * any data inline, but instead simply reference an external object and act as surrogate of it. In that case
75 * this bool is set, and the external object is referenced through the .reference field below. */
78 /* While comparing two arrays, we use this for marking what we already have seen */
81 /* Erase from memory when freeing */
84 /* If this is an object the fields are strictly ordered by name */
87 /* If in addition to this object all objects referenced by it are also ordered strictly by name */
90 /* The current 'depth' of the JsonVariant, i.e. how many levels of member variants this has */
94 /* For simple types we store the value in-line. */
97 /* For objects and arrays we store the number of elements immediately following */
100 /* If is_reference as indicated above is set, this is where the reference object is actually stored. */
101 JsonVariant
*reference
;
103 /* Strings are placed immediately after the structure. Note that when this is a JsonVariant embedded
104 * into an array we might encode strings up to INLINE_STRING_LENGTH characters directly inside the
105 * element, while longer strings are stored as references. When this object is not embedded into an
106 * array, but stand-alone we allocate the right size for the whole structure, i.e. the array might be
107 * much larger than INLINE_STRING_LENGTH.
109 * Note that because we want to allocate arrays of the JsonVariant structure we specify [0] here,
110 * rather than the prettier []. If we wouldn't, then this char array would have undefined size, and so
111 * would the union and then the struct this is included in. And of structures with undefined size we
112 * can't allocate arrays (at least not easily). */
117 /* Inside string arrays we have a series of JasonVariant structures one after the other. In this case, strings longer
118 * than INLINE_STRING_MAX are stored as references, and all shorter ones inline. (This means — on x86-64 — strings up
119 * to 15 chars are stored within the array elements, and all others in separate allocations) */
120 #define INLINE_STRING_MAX (sizeof(JsonVariant) - offsetof(JsonVariant, string) - 1U)
122 /* Let's make sure this structure isn't increased in size accidentally. This check is only for our most relevant arch
125 assert_cc(sizeof(JsonVariant
) == 48U);
126 assert_cc(INLINE_STRING_MAX
== 15U);
129 static JsonSource
* json_source_new(const char *name
) {
134 s
= malloc(offsetof(JsonSource
, name
) + strlen(name
) + 1);
141 strcpy(s
->name
, name
);
146 DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(JsonSource
, json_source
, mfree
);
148 static bool json_source_equal(JsonSource
*a
, JsonSource
*b
) {
155 return streq(a
->name
, b
->name
);
158 DEFINE_TRIVIAL_CLEANUP_FUNC(JsonSource
*, json_source_unref
);
160 /* There are four kind of JsonVariant* pointers:
163 * 2. A 'regular' one, i.e. pointing to malloc() memory
164 * 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.
165 * 4. A 'const string' one, i.e. a pointer to a const string.
167 * The four kinds of pointers can be discerned like this:
169 * Detecting #1 is easy, just compare with NULL. Detecting #3 is similarly easy: all magic pointers are below
170 * _JSON_VARIANT_MAGIC_MAX (which is pretty low, within the first memory page, which is special on Linux and other
171 * 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,
172 * otherwise #4. This makes use of the fact that malloc() will return "maximum aligned" memory, which definitely
173 * means the pointer is even. This means we can use the uneven pointers to reference static strings, as long as we
174 * make sure that all static strings used like this are aligned to 2 (or higher), and that we mask the bit on
175 * access. The JSON_VARIANT_STRING_CONST() macro encodes strings as JsonVariant* pointers, with the bit set. */
177 static bool json_variant_is_magic(const JsonVariant
*v
) {
181 return v
< _JSON_VARIANT_MAGIC_MAX
;
184 static bool json_variant_is_const_string(const JsonVariant
*v
) {
186 if (v
< _JSON_VARIANT_MAGIC_MAX
)
189 /* A proper JsonVariant is aligned to whatever malloc() aligns things too, which is definitely not uneven. We
190 * hence use all uneven pointers as indicators for const strings. */
192 return (((uintptr_t) v
) & 1) != 0;
195 static bool json_variant_is_regular(const JsonVariant
*v
) {
197 if (v
< _JSON_VARIANT_MAGIC_MAX
)
200 return (((uintptr_t) v
) & 1) == 0;
203 static JsonVariant
*json_variant_dereference(JsonVariant
*v
) {
205 /* Recursively dereference variants that are references to other variants */
210 if (!json_variant_is_regular(v
))
213 if (!v
->is_reference
)
216 return json_variant_dereference(v
->reference
);
219 static uint16_t json_variant_depth(JsonVariant
*v
) {
221 v
= json_variant_dereference(v
);
225 if (!json_variant_is_regular(v
))
231 static JsonVariant
*json_variant_formalize(JsonVariant
*v
) {
233 /* Converts json variant pointers to their normalized form, i.e. fully dereferenced and wherever
234 * possible converted to the "magic" version if there is one */
239 v
= json_variant_dereference(v
);
241 switch (json_variant_type(v
)) {
243 case JSON_VARIANT_BOOLEAN
:
244 return json_variant_boolean(v
) ? JSON_VARIANT_MAGIC_TRUE
: JSON_VARIANT_MAGIC_FALSE
;
246 case JSON_VARIANT_NULL
:
247 return JSON_VARIANT_MAGIC_NULL
;
249 case JSON_VARIANT_INTEGER
:
250 return json_variant_integer(v
) == 0 ? JSON_VARIANT_MAGIC_ZERO_INTEGER
: v
;
252 case JSON_VARIANT_UNSIGNED
:
253 return json_variant_unsigned(v
) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED
: v
;
255 case JSON_VARIANT_REAL
:
256 DISABLE_WARNING_FLOAT_EQUAL
;
257 return json_variant_real(v
) == 0.0 ? JSON_VARIANT_MAGIC_ZERO_REAL
: v
;
260 case JSON_VARIANT_STRING
:
261 return isempty(json_variant_string(v
)) ? JSON_VARIANT_MAGIC_EMPTY_STRING
: v
;
263 case JSON_VARIANT_ARRAY
:
264 return json_variant_elements(v
) == 0 ? JSON_VARIANT_MAGIC_EMPTY_ARRAY
: v
;
266 case JSON_VARIANT_OBJECT
:
267 return json_variant_elements(v
) == 0 ? JSON_VARIANT_MAGIC_EMPTY_OBJECT
: v
;
274 static JsonVariant
*json_variant_conservative_formalize(JsonVariant
*v
) {
276 /* Much like json_variant_formalize(), but won't simplify if the variant has a source/line location attached to
277 * it, in order not to lose context */
282 if (!json_variant_is_regular(v
))
285 if (v
->source
|| v
->line
> 0 || v
->column
> 0)
288 return json_variant_formalize(v
);
291 static int json_variant_new(JsonVariant
**ret
, JsonVariantType type
, size_t space
) {
294 assert_return(ret
, -EINVAL
);
296 v
= malloc0(MAX(sizeof(JsonVariant
),
297 offsetof(JsonVariant
, value
) + space
));
308 int json_variant_new_integer(JsonVariant
**ret
, intmax_t i
) {
312 assert_return(ret
, -EINVAL
);
315 *ret
= JSON_VARIANT_MAGIC_ZERO_INTEGER
;
319 r
= json_variant_new(&v
, JSON_VARIANT_INTEGER
, sizeof(i
));
323 v
->value
.integer
= i
;
329 int json_variant_new_unsigned(JsonVariant
**ret
, uintmax_t u
) {
333 assert_return(ret
, -EINVAL
);
335 *ret
= JSON_VARIANT_MAGIC_ZERO_UNSIGNED
;
339 r
= json_variant_new(&v
, JSON_VARIANT_UNSIGNED
, sizeof(u
));
349 int json_variant_new_real(JsonVariant
**ret
, long double d
) {
353 assert_return(ret
, -EINVAL
);
355 DISABLE_WARNING_FLOAT_EQUAL
;
357 *ret
= JSON_VARIANT_MAGIC_ZERO_REAL
;
362 r
= json_variant_new(&v
, JSON_VARIANT_REAL
, sizeof(d
));
372 int json_variant_new_boolean(JsonVariant
**ret
, bool b
) {
373 assert_return(ret
, -EINVAL
);
376 *ret
= JSON_VARIANT_MAGIC_TRUE
;
378 *ret
= JSON_VARIANT_MAGIC_FALSE
;
383 int json_variant_new_null(JsonVariant
**ret
) {
384 assert_return(ret
, -EINVAL
);
386 *ret
= JSON_VARIANT_MAGIC_NULL
;
390 int json_variant_new_stringn(JsonVariant
**ret
, const char *s
, size_t n
) {
394 assert_return(ret
, -EINVAL
);
396 assert_return(IN_SET(n
, 0, (size_t) -1), -EINVAL
);
397 return json_variant_new_null(ret
);
399 if (n
== (size_t) -1) /* determine length automatically */
401 else if (memchr(s
, 0, n
)) /* don't allow embedded NUL, as we can't express that in JSON */
404 *ret
= JSON_VARIANT_MAGIC_EMPTY_STRING
;
408 if (!utf8_is_valid_n(s
, n
)) /* JSON strings must be valid UTF-8 */
411 r
= json_variant_new(&v
, JSON_VARIANT_STRING
, n
+ 1);
415 memcpy(v
->string
, s
, n
);
422 int json_variant_new_base64(JsonVariant
**ret
, const void *p
, size_t n
) {
423 _cleanup_free_
char *s
= NULL
;
426 assert_return(ret
, -EINVAL
);
427 assert_return(n
== 0 || p
, -EINVAL
);
429 k
= base64mem(p
, n
, &s
);
433 return json_variant_new_stringn(ret
, s
, k
);
436 int json_variant_new_id128(JsonVariant
**ret
, sd_id128_t id
) {
437 char s
[SD_ID128_STRING_MAX
];
439 return json_variant_new_string(ret
, sd_id128_to_string(id
, s
));
442 static void json_variant_set(JsonVariant
*a
, JsonVariant
*b
) {
445 b
= json_variant_dereference(b
);
447 a
->type
= JSON_VARIANT_NULL
;
451 a
->type
= json_variant_type(b
);
454 case JSON_VARIANT_INTEGER
:
455 a
->value
.integer
= json_variant_integer(b
);
458 case JSON_VARIANT_UNSIGNED
:
459 a
->value
.unsig
= json_variant_unsigned(b
);
462 case JSON_VARIANT_REAL
:
463 a
->value
.real
= json_variant_real(b
);
466 case JSON_VARIANT_BOOLEAN
:
467 a
->value
.boolean
= json_variant_boolean(b
);
470 case JSON_VARIANT_STRING
: {
473 assert_se(s
= json_variant_string(b
));
475 /* Short strings we can store inline */
476 if (strnlen(s
, INLINE_STRING_MAX
+1) <= INLINE_STRING_MAX
) {
477 strcpy(a
->string
, s
);
481 /* For longer strings, use a reference… */
485 case JSON_VARIANT_ARRAY
:
486 case JSON_VARIANT_OBJECT
:
487 a
->is_reference
= true;
488 a
->reference
= json_variant_ref(json_variant_conservative_formalize(b
));
491 case JSON_VARIANT_NULL
:
495 assert_not_reached("Unexpected variant type");
499 static void json_variant_copy_source(JsonVariant
*v
, JsonVariant
*from
) {
503 if (!json_variant_is_regular(from
))
506 v
->line
= from
->line
;
507 v
->column
= from
->column
;
508 v
->source
= json_source_ref(from
->source
);
511 int json_variant_new_array(JsonVariant
**ret
, JsonVariant
**array
, size_t n
) {
512 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
513 bool normalized
= true;
515 assert_return(ret
, -EINVAL
);
517 *ret
= JSON_VARIANT_MAGIC_EMPTY_ARRAY
;
520 assert_return(array
, -EINVAL
);
522 v
= new(JsonVariant
, n
+ 1);
528 .type
= JSON_VARIANT_ARRAY
,
531 for (v
->n_elements
= 0; v
->n_elements
< n
; v
->n_elements
++) {
532 JsonVariant
*w
= v
+ 1 + v
->n_elements
,
533 *c
= array
[v
->n_elements
];
536 d
= json_variant_depth(c
);
537 if (d
>= DEPTH_MAX
) /* Refuse too deep nesting */
547 json_variant_set(w
, c
);
548 json_variant_copy_source(w
, c
);
550 if (!json_variant_is_normalized(c
))
554 v
->normalized
= normalized
;
560 int json_variant_new_array_bytes(JsonVariant
**ret
, const void *p
, size_t n
) {
564 assert_return(ret
, -EINVAL
);
566 *ret
= JSON_VARIANT_MAGIC_EMPTY_ARRAY
;
569 assert_return(p
, -EINVAL
);
571 v
= new(JsonVariant
, n
+ 1);
577 .type
= JSON_VARIANT_ARRAY
,
582 for (i
= 0; i
< n
; i
++) {
583 JsonVariant
*w
= v
+ 1 + i
;
588 .type
= JSON_VARIANT_UNSIGNED
,
589 .value
.unsig
= ((const uint8_t*) p
)[i
],
593 v
->normalized
= true;
599 int json_variant_new_array_strv(JsonVariant
**ret
, char **l
) {
600 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
608 *ret
= JSON_VARIANT_MAGIC_EMPTY_ARRAY
;
612 v
= new(JsonVariant
, n
+ 1);
618 .type
= JSON_VARIANT_ARRAY
,
622 for (v
->n_elements
= 0; v
->n_elements
< n
; v
->n_elements
++) {
623 JsonVariant
*w
= v
+ 1 + v
->n_elements
;
629 .type
= JSON_VARIANT_STRING
,
632 k
= strlen(l
[v
->n_elements
]);
634 if (k
> INLINE_STRING_MAX
) {
635 /* If string is too long, store it as reference. */
637 r
= json_variant_new_string(&w
->reference
, l
[v
->n_elements
]);
641 w
->is_reference
= true;
643 if (!utf8_is_valid_n(l
[v
->n_elements
], k
)) /* JSON strings must be valid UTF-8 */
646 memcpy(w
->string
, l
[v
->n_elements
], k
+1);
650 v
->normalized
= true;
656 int json_variant_new_object(JsonVariant
**ret
, JsonVariant
**array
, size_t n
) {
657 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
658 const char *prev
= NULL
;
659 bool sorted
= true, normalized
= true;
661 assert_return(ret
, -EINVAL
);
663 *ret
= JSON_VARIANT_MAGIC_EMPTY_OBJECT
;
666 assert_return(array
, -EINVAL
);
667 assert_return(n
% 2 == 0, -EINVAL
);
669 v
= new(JsonVariant
, n
+ 1);
675 .type
= JSON_VARIANT_OBJECT
,
678 for (v
->n_elements
= 0; v
->n_elements
< n
; v
->n_elements
++) {
679 JsonVariant
*w
= v
+ 1 + v
->n_elements
,
680 *c
= array
[v
->n_elements
];
683 if ((v
->n_elements
& 1) == 0) {
686 if (!json_variant_is_string(c
))
687 return -EINVAL
; /* Every second one needs to be a string, as it is the key name */
689 assert_se(k
= json_variant_string(c
));
691 if (prev
&& strcmp(k
, prev
) <= 0)
692 sorted
= normalized
= false;
695 } else if (!json_variant_is_normalized(c
))
698 d
= json_variant_depth(c
);
699 if (d
>= DEPTH_MAX
) /* Refuse too deep nesting */
709 json_variant_set(w
, c
);
710 json_variant_copy_source(w
, c
);
713 v
->normalized
= normalized
;
720 static size_t json_variant_size(JsonVariant
* v
) {
722 if (!json_variant_is_regular(v
))
726 return offsetof(JsonVariant
, reference
) + sizeof(JsonVariant
*);
730 case JSON_VARIANT_STRING
:
731 return offsetof(JsonVariant
, string
) + strlen(v
->string
) + 1;
733 case JSON_VARIANT_REAL
:
734 return offsetof(JsonVariant
, value
) + sizeof(long double);
736 case JSON_VARIANT_UNSIGNED
:
737 return offsetof(JsonVariant
, value
) + sizeof(uintmax_t);
739 case JSON_VARIANT_INTEGER
:
740 return offsetof(JsonVariant
, value
) + sizeof(intmax_t);
742 case JSON_VARIANT_BOOLEAN
:
743 return offsetof(JsonVariant
, value
) + sizeof(bool);
745 case JSON_VARIANT_ARRAY
:
746 case JSON_VARIANT_OBJECT
:
747 return offsetof(JsonVariant
, n_elements
) + sizeof(size_t);
749 case JSON_VARIANT_NULL
:
750 return offsetof(JsonVariant
, value
);
753 assert_not_reached("unexpected type");
757 static void json_variant_free_inner(JsonVariant
*v
, bool force_sensitive
) {
762 if (!json_variant_is_regular(v
))
765 json_source_unref(v
->source
);
767 sensitive
= v
->sensitive
|| force_sensitive
;
769 if (v
->is_reference
) {
771 json_variant_sensitive(v
->reference
);
773 json_variant_unref(v
->reference
);
777 if (IN_SET(v
->type
, JSON_VARIANT_ARRAY
, JSON_VARIANT_OBJECT
)) {
780 for (i
= 0; i
< v
->n_elements
; i
++)
781 json_variant_free_inner(v
+ 1 + i
, sensitive
);
785 explicit_bzero_safe(v
, json_variant_size(v
));
788 JsonVariant
*json_variant_ref(JsonVariant
*v
) {
791 if (!json_variant_is_regular(v
))
795 json_variant_ref(v
->parent
); /* ref the compounding variant instead */
797 assert(v
->n_ref
> 0);
804 JsonVariant
*json_variant_unref(JsonVariant
*v
) {
807 if (!json_variant_is_regular(v
))
811 json_variant_unref(v
->parent
);
813 assert(v
->n_ref
> 0);
817 json_variant_free_inner(v
, false);
825 void json_variant_unref_many(JsonVariant
**array
, size_t n
) {
828 assert(array
|| n
== 0);
830 for (i
= 0; i
< n
; i
++)
831 json_variant_unref(array
[i
]);
834 const char *json_variant_string(JsonVariant
*v
) {
837 if (v
== JSON_VARIANT_MAGIC_EMPTY_STRING
)
839 if (json_variant_is_magic(v
))
841 if (json_variant_is_const_string(v
)) {
842 uintptr_t p
= (uintptr_t) v
;
844 assert((p
& 1) != 0);
845 return (const char*) (p
^ 1U);
849 return json_variant_string(v
->reference
);
850 if (v
->type
!= JSON_VARIANT_STRING
)
856 log_debug("Non-string JSON variant requested as string, returning NULL.");
860 bool json_variant_boolean(JsonVariant
*v
) {
863 if (v
== JSON_VARIANT_MAGIC_TRUE
)
865 if (v
== JSON_VARIANT_MAGIC_FALSE
)
867 if (!json_variant_is_regular(v
))
869 if (v
->type
!= JSON_VARIANT_BOOLEAN
)
872 return json_variant_boolean(v
->reference
);
874 return v
->value
.boolean
;
877 log_debug("Non-boolean JSON variant requested as boolean, returning false.");
881 intmax_t json_variant_integer(JsonVariant
*v
) {
884 if (v
== JSON_VARIANT_MAGIC_ZERO_INTEGER
||
885 v
== JSON_VARIANT_MAGIC_ZERO_UNSIGNED
||
886 v
== JSON_VARIANT_MAGIC_ZERO_REAL
)
888 if (!json_variant_is_regular(v
))
891 return json_variant_integer(v
->reference
);
895 case JSON_VARIANT_INTEGER
:
896 return v
->value
.integer
;
898 case JSON_VARIANT_UNSIGNED
:
899 if (v
->value
.unsig
<= INTMAX_MAX
)
900 return (intmax_t) v
->value
.unsig
;
902 log_debug("Unsigned integer %ju requested as signed integer and out of range, returning 0.", v
->value
.unsig
);
905 case JSON_VARIANT_REAL
: {
908 converted
= (intmax_t) v
->value
.real
;
910 DISABLE_WARNING_FLOAT_EQUAL
;
911 if ((long double) converted
== v
->value
.real
)
915 log_debug("Real %Lg requested as integer, and cannot be converted losslessly, returning 0.", v
->value
.real
);
924 log_debug("Non-integer JSON variant requested as integer, returning 0.");
928 uintmax_t json_variant_unsigned(JsonVariant
*v
) {
931 if (v
== JSON_VARIANT_MAGIC_ZERO_INTEGER
||
932 v
== JSON_VARIANT_MAGIC_ZERO_UNSIGNED
||
933 v
== JSON_VARIANT_MAGIC_ZERO_REAL
)
935 if (!json_variant_is_regular(v
))
938 return json_variant_integer(v
->reference
);
942 case JSON_VARIANT_INTEGER
:
943 if (v
->value
.integer
>= 0)
944 return (uintmax_t) v
->value
.integer
;
946 log_debug("Signed integer %ju requested as unsigned integer and out of range, returning 0.", v
->value
.integer
);
949 case JSON_VARIANT_UNSIGNED
:
950 return v
->value
.unsig
;
952 case JSON_VARIANT_REAL
: {
955 converted
= (uintmax_t) v
->value
.real
;
957 DISABLE_WARNING_FLOAT_EQUAL
;
958 if ((long double) converted
== v
->value
.real
)
962 log_debug("Real %Lg requested as unsigned integer, and cannot be converted losslessly, returning 0.", v
->value
.real
);
971 log_debug("Non-integer JSON variant requested as unsigned, returning 0.");
975 long double json_variant_real(JsonVariant
*v
) {
978 if (v
== JSON_VARIANT_MAGIC_ZERO_INTEGER
||
979 v
== JSON_VARIANT_MAGIC_ZERO_UNSIGNED
||
980 v
== JSON_VARIANT_MAGIC_ZERO_REAL
)
982 if (!json_variant_is_regular(v
))
985 return json_variant_real(v
->reference
);
989 case JSON_VARIANT_REAL
:
990 return v
->value
.real
;
992 case JSON_VARIANT_INTEGER
: {
993 long double converted
;
995 converted
= (long double) v
->value
.integer
;
997 if ((intmax_t) converted
== v
->value
.integer
)
1000 log_debug("Signed integer %ji requested as real, and cannot be converted losslessly, returning 0.", v
->value
.integer
);
1004 case JSON_VARIANT_UNSIGNED
: {
1005 long double converted
;
1007 converted
= (long double) v
->value
.unsig
;
1009 if ((uintmax_t) converted
== v
->value
.unsig
)
1012 log_debug("Unsigned integer %ju requested as real, and cannot be converted losslessly, returning 0.", v
->value
.unsig
);
1021 log_debug("Non-integer JSON variant requested as integer, returning 0.");
1025 bool json_variant_is_negative(JsonVariant
*v
) {
1028 if (v
== JSON_VARIANT_MAGIC_ZERO_INTEGER
||
1029 v
== JSON_VARIANT_MAGIC_ZERO_UNSIGNED
||
1030 v
== JSON_VARIANT_MAGIC_ZERO_REAL
)
1032 if (!json_variant_is_regular(v
))
1034 if (v
->is_reference
)
1035 return json_variant_is_negative(v
->reference
);
1037 /* This function is useful as checking whether numbers are negative is pretty complex since we have three types
1038 * of numbers. And some JSON code (OCI for example) uses negative numbers to mark "not defined" numeric
1043 case JSON_VARIANT_REAL
:
1044 return v
->value
.real
< 0;
1046 case JSON_VARIANT_INTEGER
:
1047 return v
->value
.integer
< 0;
1049 case JSON_VARIANT_UNSIGNED
:
1057 log_debug("Non-integer JSON variant tested for negativity, returning false.");
1061 bool json_variant_is_blank_object(JsonVariant
*v
) {
1062 /* Returns true if the specified object is null or empty */
1064 json_variant_is_null(v
) ||
1065 (json_variant_is_object(v
) && json_variant_elements(v
) == 0);
1068 bool json_variant_is_blank_array(JsonVariant
*v
) {
1070 json_variant_is_null(v
) ||
1071 (json_variant_is_array(v
) && json_variant_elements(v
) == 0);
1074 JsonVariantType
json_variant_type(JsonVariant
*v
) {
1077 return _JSON_VARIANT_TYPE_INVALID
;
1079 if (json_variant_is_const_string(v
))
1080 return JSON_VARIANT_STRING
;
1082 if (v
== JSON_VARIANT_MAGIC_TRUE
|| v
== JSON_VARIANT_MAGIC_FALSE
)
1083 return JSON_VARIANT_BOOLEAN
;
1085 if (v
== JSON_VARIANT_MAGIC_NULL
)
1086 return JSON_VARIANT_NULL
;
1088 if (v
== JSON_VARIANT_MAGIC_ZERO_INTEGER
)
1089 return JSON_VARIANT_INTEGER
;
1091 if (v
== JSON_VARIANT_MAGIC_ZERO_UNSIGNED
)
1092 return JSON_VARIANT_UNSIGNED
;
1094 if (v
== JSON_VARIANT_MAGIC_ZERO_REAL
)
1095 return JSON_VARIANT_REAL
;
1097 if (v
== JSON_VARIANT_MAGIC_EMPTY_STRING
)
1098 return JSON_VARIANT_STRING
;
1100 if (v
== JSON_VARIANT_MAGIC_EMPTY_ARRAY
)
1101 return JSON_VARIANT_ARRAY
;
1103 if (v
== JSON_VARIANT_MAGIC_EMPTY_OBJECT
)
1104 return JSON_VARIANT_OBJECT
;
1109 _function_no_sanitize_float_cast_overflow_
bool json_variant_has_type(JsonVariant
*v
, JsonVariantType type
) {
1112 /* Note: we turn off ubsan float cast overflo detection for this function, since it would complain
1113 * about our float casts but we do them explicitly to detect conversion errors. */
1115 v
= json_variant_dereference(v
);
1119 rt
= json_variant_type(v
);
1123 /* If it's a const string, then it only can be a string, and if it is not, it's not */
1124 if (json_variant_is_const_string(v
))
1127 /* All three magic zeroes qualify as integer, unsigned and as real */
1128 if ((v
== JSON_VARIANT_MAGIC_ZERO_INTEGER
|| v
== JSON_VARIANT_MAGIC_ZERO_UNSIGNED
|| v
== JSON_VARIANT_MAGIC_ZERO_REAL
) &&
1129 IN_SET(type
, JSON_VARIANT_INTEGER
, JSON_VARIANT_UNSIGNED
, JSON_VARIANT_REAL
, JSON_VARIANT_NUMBER
))
1132 /* All other magic variant types are only equal to themselves */
1133 if (json_variant_is_magic(v
))
1136 /* Handle the "number" pseudo type */
1137 if (type
== JSON_VARIANT_NUMBER
)
1138 return IN_SET(rt
, JSON_VARIANT_INTEGER
, JSON_VARIANT_UNSIGNED
, JSON_VARIANT_REAL
);
1140 /* Integer conversions are OK in many cases */
1141 if (rt
== JSON_VARIANT_INTEGER
&& type
== JSON_VARIANT_UNSIGNED
)
1142 return v
->value
.integer
>= 0;
1143 if (rt
== JSON_VARIANT_UNSIGNED
&& type
== JSON_VARIANT_INTEGER
)
1144 return v
->value
.unsig
<= INTMAX_MAX
;
1146 /* Any integer that can be converted lossley to a real and back may also be considered a real */
1147 if (rt
== JSON_VARIANT_INTEGER
&& type
== JSON_VARIANT_REAL
)
1148 return (intmax_t) (long double) v
->value
.integer
== v
->value
.integer
;
1149 if (rt
== JSON_VARIANT_UNSIGNED
&& type
== JSON_VARIANT_REAL
)
1150 return (uintmax_t) (long double) v
->value
.unsig
== v
->value
.unsig
;
1152 DISABLE_WARNING_FLOAT_EQUAL
;
1154 /* Any real that can be converted losslessly to an integer and back may also be considered an integer */
1155 if (rt
== JSON_VARIANT_REAL
&& type
== JSON_VARIANT_INTEGER
)
1156 return (long double) (intmax_t) v
->value
.real
== v
->value
.real
;
1157 if (rt
== JSON_VARIANT_REAL
&& type
== JSON_VARIANT_UNSIGNED
)
1158 return (long double) (uintmax_t) v
->value
.real
== v
->value
.real
;
1165 size_t json_variant_elements(JsonVariant
*v
) {
1168 if (v
== JSON_VARIANT_MAGIC_EMPTY_ARRAY
||
1169 v
== JSON_VARIANT_MAGIC_EMPTY_OBJECT
)
1171 if (!json_variant_is_regular(v
))
1173 if (!IN_SET(v
->type
, JSON_VARIANT_ARRAY
, JSON_VARIANT_OBJECT
))
1175 if (v
->is_reference
)
1176 return json_variant_elements(v
->reference
);
1178 return v
->n_elements
;
1181 log_debug("Number of elements in non-array/non-object JSON variant requested, returning 0.");
1185 JsonVariant
*json_variant_by_index(JsonVariant
*v
, size_t idx
) {
1188 if (v
== JSON_VARIANT_MAGIC_EMPTY_ARRAY
||
1189 v
== JSON_VARIANT_MAGIC_EMPTY_OBJECT
)
1191 if (!json_variant_is_regular(v
))
1193 if (!IN_SET(v
->type
, JSON_VARIANT_ARRAY
, JSON_VARIANT_OBJECT
))
1195 if (v
->is_reference
)
1196 return json_variant_by_index(v
->reference
, idx
);
1197 if (idx
>= v
->n_elements
)
1200 return json_variant_conservative_formalize(v
+ 1 + idx
);
1203 log_debug("Element in non-array/non-object JSON variant requested by index, returning NULL.");
1207 JsonVariant
*json_variant_by_key_full(JsonVariant
*v
, const char *key
, JsonVariant
**ret_key
) {
1214 if (v
== JSON_VARIANT_MAGIC_EMPTY_OBJECT
)
1216 if (!json_variant_is_regular(v
))
1218 if (v
->type
!= JSON_VARIANT_OBJECT
)
1220 if (v
->is_reference
)
1221 return json_variant_by_key(v
->reference
, key
);
1224 size_t a
= 0, b
= v
->n_elements
/2;
1226 /* If the variant is sorted we can use bisection to find the entry we need in O(log(n)) time */
1234 p
= json_variant_dereference(v
+ 1 + i
*2);
1236 assert_se(f
= json_variant_string(p
));
1241 *ret_key
= json_variant_conservative_formalize(v
+ 1 + i
*2);
1243 return json_variant_conservative_formalize(v
+ 1 + i
*2 + 1);
1253 /* The variant is not sorted, hence search for the field linearly */
1254 for (i
= 0; i
< v
->n_elements
; i
+= 2) {
1257 p
= json_variant_dereference(v
+ 1 + i
);
1259 if (!json_variant_has_type(p
, JSON_VARIANT_STRING
))
1262 if (streq(json_variant_string(p
), key
)) {
1265 *ret_key
= json_variant_conservative_formalize(v
+ 1 + i
);
1267 return json_variant_conservative_formalize(v
+ 1 + i
+ 1);
1278 log_debug("Element in non-object JSON variant requested by key, returning NULL.");
1285 JsonVariant
*json_variant_by_key(JsonVariant
*v
, const char *key
) {
1286 return json_variant_by_key_full(v
, key
, NULL
);
1289 bool json_variant_equal(JsonVariant
*a
, JsonVariant
*b
) {
1292 a
= json_variant_formalize(a
);
1293 b
= json_variant_formalize(b
);
1298 t
= json_variant_type(a
);
1299 if (!json_variant_has_type(b
, t
))
1304 case JSON_VARIANT_STRING
:
1305 return streq(json_variant_string(a
), json_variant_string(b
));
1307 case JSON_VARIANT_INTEGER
:
1308 return json_variant_integer(a
) == json_variant_integer(b
);
1310 case JSON_VARIANT_UNSIGNED
:
1311 return json_variant_unsigned(a
) == json_variant_unsigned(b
);
1313 case JSON_VARIANT_REAL
:
1314 DISABLE_WARNING_FLOAT_EQUAL
;
1315 return json_variant_real(a
) == json_variant_real(b
);
1318 case JSON_VARIANT_BOOLEAN
:
1319 return json_variant_boolean(a
) == json_variant_boolean(b
);
1321 case JSON_VARIANT_NULL
:
1324 case JSON_VARIANT_ARRAY
: {
1327 n
= json_variant_elements(a
);
1328 if (n
!= json_variant_elements(b
))
1331 for (i
= 0; i
< n
; i
++) {
1332 if (!json_variant_equal(json_variant_by_index(a
, i
), json_variant_by_index(b
, i
)))
1339 case JSON_VARIANT_OBJECT
: {
1342 n
= json_variant_elements(a
);
1343 if (n
!= json_variant_elements(b
))
1346 /* Iterate through all keys in 'a' */
1347 for (i
= 0; i
< n
; i
+= 2) {
1351 /* Match them against all keys in 'b' */
1352 for (j
= 0; j
< n
; j
+= 2) {
1355 key_b
= json_variant_by_index(b
, j
);
1357 /* During the first iteration unmark everything */
1359 key_b
->is_marked
= false;
1360 else if (key_b
->is_marked
) /* In later iterations if we already marked something, don't bother with it again */
1366 if (json_variant_equal(json_variant_by_index(a
, i
), key_b
) &&
1367 json_variant_equal(json_variant_by_index(a
, i
+1), json_variant_by_index(b
, j
+1))) {
1368 /* Key and values match! */
1369 key_b
->is_marked
= found
= true;
1371 /* In the first iteration we continue the inner loop since we want to mark
1372 * everything, otherwise exit the loop quickly after we found what we were
1387 assert_not_reached("Unknown variant type.");
1391 void json_variant_sensitive(JsonVariant
*v
) {
1394 /* Marks a variant as "sensitive", so that it is erased from memory when it is destroyed. This is a
1395 * one-way operation: as soon as it is marked this way it remains marked this way until it's
1396 * destroyed. A magic variant is never sensitive though, even when asked, since it's too
1397 * basic. Similar, const string variant are never sensitive either, after all they are included in
1398 * the source code as they are, which is not suitable for inclusion of secrets.
1400 * Note that this flag has a recursive effect: when we destroy an object or array we'll propagate the
1401 * flag to all contained variants. And if those are then destroyed this is propagated further down,
1404 v
= json_variant_formalize(v
);
1405 if (!json_variant_is_regular(v
))
1408 v
->sensitive
= true;
1411 bool json_variant_is_sensitive(JsonVariant
*v
) {
1412 v
= json_variant_formalize(v
);
1413 if (!json_variant_is_regular(v
))
1416 return v
->sensitive
;
1419 static void json_variant_propagate_sensitive(JsonVariant
*from
, JsonVariant
*to
) {
1420 if (json_variant_is_sensitive(from
))
1421 json_variant_sensitive(to
);
1424 int json_variant_get_source(JsonVariant
*v
, const char **ret_source
, unsigned *ret_line
, unsigned *ret_column
) {
1425 assert_return(v
, -EINVAL
);
1428 *ret_source
= json_variant_is_regular(v
) && v
->source
? v
->source
->name
: NULL
;
1431 *ret_line
= json_variant_is_regular(v
) ? v
->line
: 0;
1434 *ret_column
= json_variant_is_regular(v
) ? v
->column
: 0;
1439 static int print_source(FILE *f
, JsonVariant
*v
, JsonFormatFlags flags
, bool whitespace
) {
1442 if (!FLAGS_SET(flags
, JSON_FORMAT_SOURCE
|JSON_FORMAT_PRETTY
))
1445 if (!json_variant_is_regular(v
))
1448 if (!v
->source
&& v
->line
== 0 && v
->column
== 0)
1451 /* The max width we need to format the line numbers for this source file */
1452 w
= (v
->source
&& v
->source
->max_line
> 0) ?
1453 DECIMAL_STR_WIDTH(v
->source
->max_line
) :
1454 DECIMAL_STR_MAX(unsigned)-1;
1455 k
= (v
->source
&& v
->source
->max_column
> 0) ?
1456 DECIMAL_STR_WIDTH(v
->source
->max_column
) :
1457 DECIMAL_STR_MAX(unsigned) -1;
1462 n
= 1 + (v
->source
? strlen(v
->source
->name
) : 0) +
1463 ((v
->source
&& (v
->line
> 0 || v
->column
> 0)) ? 1 : 0) +
1464 (v
->line
> 0 ? w
: 0) +
1465 (((v
->source
|| v
->line
> 0) && v
->column
> 0) ? 1 : 0) +
1466 (v
->column
> 0 ? k
: 0) +
1469 for (i
= 0; i
< n
; i
++)
1475 fputs(v
->source
->name
, f
);
1476 if (v
->source
&& (v
->line
> 0 || v
->column
> 0))
1479 fprintf(f
, "%*u", (int) w
, v
->line
);
1480 if ((v
->source
|| v
->line
> 0) || v
->column
> 0)
1483 fprintf(f
, "%*u", (int) k
, v
->column
);
1492 static void json_format_string(FILE *f
, const char *q
, JsonFormatFlags flags
) {
1497 if (flags
& JSON_FORMAT_COLOR
)
1498 fputs(ANSI_GREEN
, f
);
1531 if ((signed char) *q
>= 0 && *q
< ' ')
1532 fprintf(f
, "\\u%04x", *q
);
1538 if (flags
& JSON_FORMAT_COLOR
)
1539 fputs(ANSI_NORMAL
, f
);
1544 static int json_format(FILE *f
, JsonVariant
*v
, JsonFormatFlags flags
, const char *prefix
) {
1550 switch (json_variant_type(v
)) {
1552 case JSON_VARIANT_REAL
: {
1555 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
1556 if (loc
== (locale_t
) 0)
1559 if (flags
& JSON_FORMAT_COLOR
)
1560 fputs(ANSI_HIGHLIGHT_BLUE
, f
);
1562 fprintf(f
, "%.*Le", DECIMAL_DIG
, json_variant_real(v
));
1564 if (flags
& JSON_FORMAT_COLOR
)
1565 fputs(ANSI_NORMAL
, f
);
1571 case JSON_VARIANT_INTEGER
:
1572 if (flags
& JSON_FORMAT_COLOR
)
1573 fputs(ANSI_HIGHLIGHT_BLUE
, f
);
1575 fprintf(f
, "%" PRIdMAX
, json_variant_integer(v
));
1577 if (flags
& JSON_FORMAT_COLOR
)
1578 fputs(ANSI_NORMAL
, f
);
1581 case JSON_VARIANT_UNSIGNED
:
1582 if (flags
& JSON_FORMAT_COLOR
)
1583 fputs(ANSI_HIGHLIGHT_BLUE
, f
);
1585 fprintf(f
, "%" PRIuMAX
, json_variant_unsigned(v
));
1587 if (flags
& JSON_FORMAT_COLOR
)
1588 fputs(ANSI_NORMAL
, f
);
1591 case JSON_VARIANT_BOOLEAN
:
1593 if (flags
& JSON_FORMAT_COLOR
)
1594 fputs(ANSI_HIGHLIGHT
, f
);
1596 if (json_variant_boolean(v
))
1601 if (flags
& JSON_FORMAT_COLOR
)
1602 fputs(ANSI_NORMAL
, f
);
1606 case JSON_VARIANT_NULL
:
1607 if (flags
& JSON_FORMAT_COLOR
)
1608 fputs(ANSI_HIGHLIGHT
, f
);
1612 if (flags
& JSON_FORMAT_COLOR
)
1613 fputs(ANSI_NORMAL
, f
);
1616 case JSON_VARIANT_STRING
:
1617 json_format_string(f
, json_variant_string(v
), flags
);
1620 case JSON_VARIANT_ARRAY
: {
1623 n
= json_variant_elements(v
);
1628 _cleanup_free_
char *joined
= NULL
;
1629 const char *prefix2
;
1631 if (flags
& JSON_FORMAT_PRETTY
) {
1632 joined
= strjoin(strempty(prefix
), "\t");
1639 prefix2
= strempty(prefix
);
1643 for (i
= 0; i
< n
; i
++) {
1646 assert_se(e
= json_variant_by_index(v
, i
));
1649 if (flags
& JSON_FORMAT_PRETTY
)
1655 if (flags
& JSON_FORMAT_PRETTY
) {
1656 print_source(f
, e
, flags
, false);
1660 r
= json_format(f
, e
, flags
, prefix2
);
1665 if (flags
& JSON_FORMAT_PRETTY
) {
1667 print_source(f
, v
, flags
, true);
1668 fputs(strempty(prefix
), f
);
1676 case JSON_VARIANT_OBJECT
: {
1679 n
= json_variant_elements(v
);
1684 _cleanup_free_
char *joined
= NULL
;
1685 const char *prefix2
;
1687 if (flags
& JSON_FORMAT_PRETTY
) {
1688 joined
= strjoin(strempty(prefix
), "\t");
1695 prefix2
= strempty(prefix
);
1699 for (i
= 0; i
< n
; i
+= 2) {
1702 e
= json_variant_by_index(v
, i
);
1705 if (flags
& JSON_FORMAT_PRETTY
)
1711 if (flags
& JSON_FORMAT_PRETTY
) {
1712 print_source(f
, e
, flags
, false);
1716 r
= json_format(f
, e
, flags
, prefix2
);
1720 fputs(flags
& JSON_FORMAT_PRETTY
? " : " : ":", f
);
1722 r
= json_format(f
, json_variant_by_index(v
, i
+1), flags
, prefix2
);
1727 if (flags
& JSON_FORMAT_PRETTY
) {
1729 print_source(f
, v
, flags
, true);
1730 fputs(strempty(prefix
), f
);
1739 assert_not_reached("Unexpected variant type.");
1745 int json_variant_format(JsonVariant
*v
, JsonFormatFlags flags
, char **ret
) {
1746 _cleanup_free_
char *s
= NULL
;
1750 /* Returns the length of the generated string (without the terminating NUL),
1751 * or negative on error. */
1753 assert_return(v
, -EINVAL
);
1754 assert_return(ret
, -EINVAL
);
1757 _cleanup_fclose_
FILE *f
= NULL
;
1759 f
= open_memstream_unlocked(&s
, &sz
);
1763 json_variant_dump(v
, flags
, f
, NULL
);
1765 /* Add terminating 0, so that the output buffer is a valid string. */
1768 r
= fflush_and_check(f
);
1776 return (int) sz
- 1;
1779 void json_variant_dump(JsonVariant
*v
, JsonFormatFlags flags
, FILE *f
, const char *prefix
) {
1786 print_source(f
, v
, flags
, false);
1788 if (((flags
& (JSON_FORMAT_COLOR_AUTO
|JSON_FORMAT_COLOR
)) == JSON_FORMAT_COLOR_AUTO
) && colors_enabled())
1789 flags
|= JSON_FORMAT_COLOR
;
1791 if (((flags
& (JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_PRETTY
)) == JSON_FORMAT_PRETTY_AUTO
))
1792 flags
|= on_tty() ? JSON_FORMAT_PRETTY
: JSON_FORMAT_NEWLINE
;
1794 if (flags
& JSON_FORMAT_SSE
)
1796 if (flags
& JSON_FORMAT_SEQ
)
1797 fputc('\x1e', f
); /* ASCII Record Separator */
1799 json_format(f
, v
, flags
, prefix
);
1801 if (flags
& (JSON_FORMAT_PRETTY
|JSON_FORMAT_SEQ
|JSON_FORMAT_SSE
|JSON_FORMAT_NEWLINE
))
1803 if (flags
& JSON_FORMAT_SSE
)
1804 fputc('\n', f
); /* In case of SSE add a second newline */
1806 if (flags
& JSON_FORMAT_FLUSH
)
1810 int json_variant_filter(JsonVariant
**v
, char **to_remove
) {
1811 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1812 _cleanup_free_ JsonVariant
**array
= NULL
;
1813 size_t i
, n
= 0, k
= 0;
1818 if (json_variant_is_blank_object(*v
))
1820 if (!json_variant_is_object(*v
))
1823 if (strv_isempty(to_remove
))
1826 for (i
= 0; i
< json_variant_elements(*v
); i
+= 2) {
1829 p
= json_variant_by_index(*v
, i
);
1830 if (!json_variant_has_type(p
, JSON_VARIANT_STRING
))
1833 if (strv_contains(to_remove
, json_variant_string(p
))) {
1835 array
= new(JsonVariant
*, json_variant_elements(*v
) - 2);
1839 for (k
= 0; k
< i
; k
++)
1840 array
[k
] = json_variant_by_index(*v
, k
);
1846 array
[k
++] = json_variant_by_index(*v
, i
+ 1);
1853 r
= json_variant_new_object(&w
, array
, k
);
1857 json_variant_propagate_sensitive(*v
, w
);
1859 json_variant_unref(*v
);
1865 int json_variant_set_field(JsonVariant
**v
, const char *field
, JsonVariant
*value
) {
1866 _cleanup_(json_variant_unrefp
) JsonVariant
*field_variant
= NULL
, *w
= NULL
;
1867 _cleanup_free_ JsonVariant
**array
= NULL
;
1874 if (json_variant_is_blank_object(*v
)) {
1875 array
= new(JsonVariant
*, 2);
1880 if (!json_variant_is_object(*v
))
1883 for (i
= 0; i
< json_variant_elements(*v
); i
+= 2) {
1886 p
= json_variant_by_index(*v
, i
);
1887 if (!json_variant_is_string(p
))
1890 if (streq(json_variant_string(p
), field
)) {
1893 array
= new(JsonVariant
*, json_variant_elements(*v
));
1897 for (k
= 0; k
< i
; k
++)
1898 array
[k
] = json_variant_by_index(*v
, k
);
1903 array
[k
++] = json_variant_by_index(*v
, i
+ 1);
1908 array
= new(JsonVariant
*, json_variant_elements(*v
) + 2);
1912 for (k
= 0; k
< json_variant_elements(*v
); k
++)
1913 array
[k
] = json_variant_by_index(*v
, k
);
1917 r
= json_variant_new_string(&field_variant
, field
);
1921 array
[k
++] = field_variant
;
1924 r
= json_variant_new_object(&w
, array
, k
);
1928 json_variant_propagate_sensitive(*v
, w
);
1930 json_variant_unref(*v
);
1936 int json_variant_set_field_string(JsonVariant
**v
, const char *field
, const char *value
) {
1937 _cleanup_(json_variant_unrefp
) JsonVariant
*m
= NULL
;
1940 r
= json_variant_new_string(&m
, value
);
1944 return json_variant_set_field(v
, field
, m
);
1947 int json_variant_set_field_integer(JsonVariant
**v
, const char *field
, intmax_t i
) {
1948 _cleanup_(json_variant_unrefp
) JsonVariant
*m
= NULL
;
1951 r
= json_variant_new_integer(&m
, i
);
1955 return json_variant_set_field(v
, field
, m
);
1958 int json_variant_set_field_unsigned(JsonVariant
**v
, const char *field
, uintmax_t u
) {
1959 _cleanup_(json_variant_unrefp
) JsonVariant
*m
= NULL
;
1962 r
= json_variant_new_unsigned(&m
, u
);
1966 return json_variant_set_field(v
, field
, m
);
1969 int json_variant_set_field_boolean(JsonVariant
**v
, const char *field
, bool b
) {
1970 _cleanup_(json_variant_unrefp
) JsonVariant
*m
= NULL
;
1973 r
= json_variant_new_boolean(&m
, b
);
1977 return json_variant_set_field(v
, field
, m
);
1980 int json_variant_set_field_strv(JsonVariant
**v
, const char *field
, char **l
) {
1981 _cleanup_(json_variant_unrefp
) JsonVariant
*m
= NULL
;
1984 r
= json_variant_new_array_strv(&m
, l
);
1988 return json_variant_set_field(v
, field
, m
);
1991 int json_variant_merge(JsonVariant
**v
, JsonVariant
*m
) {
1992 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1993 _cleanup_free_ JsonVariant
**array
= NULL
;
1994 size_t v_elements
, m_elements
, i
, k
;
1995 bool v_blank
, m_blank
;
1998 m
= json_variant_dereference(m
);
2000 v_blank
= json_variant_is_blank_object(*v
);
2001 m_blank
= json_variant_is_blank_object(m
);
2003 if (!v_blank
&& !json_variant_is_object(*v
))
2005 if (!m_blank
&& !json_variant_is_object(m
))
2009 return 0; /* nothing to do */
2012 json_variant_unref(*v
);
2013 *v
= json_variant_ref(m
);
2017 v_elements
= json_variant_elements(*v
);
2018 m_elements
= json_variant_elements(m
);
2019 if (v_elements
> SIZE_MAX
- m_elements
) /* overflow check */
2022 array
= new(JsonVariant
*, v_elements
+ m_elements
);
2027 for (i
= 0; i
< v_elements
; i
+= 2) {
2030 u
= json_variant_by_index(*v
, i
);
2031 if (!json_variant_is_string(u
))
2034 if (json_variant_by_key(m
, json_variant_string(u
)))
2035 continue; /* skip if exists in second variant */
2038 array
[k
++] = json_variant_by_index(*v
, i
+ 1);
2041 for (i
= 0; i
< m_elements
; i
++)
2042 array
[k
++] = json_variant_by_index(m
, i
);
2044 r
= json_variant_new_object(&w
, array
, k
);
2048 json_variant_propagate_sensitive(*v
, w
);
2049 json_variant_propagate_sensitive(m
, w
);
2051 json_variant_unref(*v
);
2057 int json_variant_append_array(JsonVariant
**v
, JsonVariant
*element
) {
2058 _cleanup_(json_variant_unrefp
) JsonVariant
*nv
= NULL
;
2066 if (!*v
|| json_variant_is_null(*v
))
2068 else if (!json_variant_is_array(*v
))
2071 blank
= json_variant_elements(*v
) == 0;
2074 r
= json_variant_new_array(&nv
, (JsonVariant
*[]) { element
}, 1);
2076 _cleanup_free_ JsonVariant
**array
= NULL
;
2079 array
= new(JsonVariant
*, json_variant_elements(*v
) + 1);
2083 for (i
= 0; i
< json_variant_elements(*v
); i
++)
2084 array
[i
] = json_variant_by_index(*v
, i
);
2088 r
= json_variant_new_array(&nv
, array
, i
+ 1);
2093 json_variant_propagate_sensitive(*v
, nv
);
2095 json_variant_unref(*v
);
2101 int json_variant_strv(JsonVariant
*v
, char ***ret
) {
2109 if (!v
|| json_variant_is_null(v
)) {
2118 if (!json_variant_is_array(v
))
2121 sensitive
= v
->sensitive
;
2123 n
= json_variant_elements(v
);
2124 l
= new(char*, n
+1);
2128 for (i
= 0; i
< n
; i
++) {
2131 assert_se(e
= json_variant_by_index(v
, i
));
2132 sensitive
= sensitive
|| e
->sensitive
;
2134 if (!json_variant_is_string(e
)) {
2140 l
[i
] = strdup(json_variant_string(e
));
2161 static int json_variant_copy(JsonVariant
**nv
, JsonVariant
*v
) {
2171 /* Let's copy the simple types literally, and the larger types by references */
2172 t
= json_variant_type(v
);
2174 case JSON_VARIANT_INTEGER
:
2175 k
= sizeof(intmax_t);
2176 value
.integer
= json_variant_integer(v
);
2180 case JSON_VARIANT_UNSIGNED
:
2181 k
= sizeof(uintmax_t);
2182 value
.unsig
= json_variant_unsigned(v
);
2186 case JSON_VARIANT_REAL
:
2187 k
= sizeof(long double);
2188 value
.real
= json_variant_real(v
);
2192 case JSON_VARIANT_BOOLEAN
:
2194 value
.boolean
= json_variant_boolean(v
);
2198 case JSON_VARIANT_NULL
:
2203 case JSON_VARIANT_STRING
:
2204 source
= json_variant_string(v
);
2205 k
= strnlen(source
, INLINE_STRING_MAX
+ 1);
2206 if (k
<= INLINE_STRING_MAX
) {
2214 /* Everything else copy by reference */
2216 c
= malloc0(MAX(sizeof(JsonVariant
),
2217 offsetof(JsonVariant
, reference
) + sizeof(JsonVariant
*)));
2223 c
->is_reference
= true;
2224 c
->reference
= json_variant_ref(json_variant_formalize(v
));
2230 c
= malloc0(MAX(sizeof(JsonVariant
),
2231 offsetof(JsonVariant
, value
) + k
));
2238 memcpy_safe(&c
->value
, source
, k
);
2240 json_variant_propagate_sensitive(v
, c
);
2246 static bool json_single_ref(JsonVariant
*v
) {
2248 /* Checks whether the caller is the single owner of the object, i.e. can get away with changing it */
2250 if (!json_variant_is_regular(v
))
2254 return json_single_ref(v
->parent
);
2256 assert(v
->n_ref
> 0);
2257 return v
->n_ref
== 1;
2260 static int json_variant_set_source(JsonVariant
**v
, JsonSource
*source
, unsigned line
, unsigned column
) {
2266 /* Patch in source and line/column number. Tries to do this in-place if the caller is the sole referencer of
2267 * the object. If not, allocates a new object, possibly a surrogate for the original one */
2272 if (source
&& line
> source
->max_line
)
2273 source
->max_line
= line
;
2274 if (source
&& column
> source
->max_column
)
2275 source
->max_column
= column
;
2277 if (!json_variant_is_regular(*v
)) {
2279 if (!source
&& line
== 0 && column
== 0)
2283 if (json_source_equal((*v
)->source
, source
) &&
2284 (*v
)->line
== line
&&
2285 (*v
)->column
== column
)
2288 if (json_single_ref(*v
)) { /* Sole reference? */
2289 json_source_unref((*v
)->source
);
2290 (*v
)->source
= json_source_ref(source
);
2292 (*v
)->column
= column
;
2297 r
= json_variant_copy(&w
, *v
);
2301 assert(json_variant_is_regular(w
));
2302 assert(!w
->is_embedded
);
2303 assert(w
->n_ref
== 1);
2306 w
->source
= json_source_ref(source
);
2310 json_variant_unref(*v
);
2316 static void inc_lines_columns(unsigned *line
, unsigned *column
, const char *s
, size_t n
) {
2319 assert(s
|| n
== 0);
2325 } else if ((signed char) *s
>= 0 && *s
< 127) /* Process ASCII chars quickly */
2330 w
= utf8_encoded_valid_unichar(s
, n
);
2331 if (w
< 0) /* count invalid unichars as normal characters */
2333 else if ((size_t) w
> n
) /* never read more than the specified number of characters */
2348 static int unhex_ucs2(const char *c
, uint16_t *ret
) {
2355 aa
= unhexchar(c
[0]);
2359 bb
= unhexchar(c
[1]);
2363 cc
= unhexchar(c
[2]);
2367 dd
= unhexchar(c
[3]);
2371 x
= ((uint16_t) aa
<< 12) |
2372 ((uint16_t) bb
<< 8) |
2373 ((uint16_t) cc
<< 4) |
2384 static int json_parse_string(const char **p
, char **ret
) {
2385 _cleanup_free_
char *s
= NULL
;
2386 size_t n
= 0, allocated
= 0;
2407 /* Check for control characters 0x00..0x1f */
2408 if (*c
> 0 && *c
< ' ')
2411 /* Check for control character 0x7f */
2426 return JSON_TOKEN_STRING
;
2436 if (IN_SET(*c
, '"', '\\', '/'))
2448 else if (*c
== 'u') {
2452 r
= unhex_ucs2(c
+ 1, &x
);
2458 if (!GREEDY_REALLOC(s
, allocated
, n
+ 5))
2461 if (!utf16_is_surrogate(x
))
2462 n
+= utf8_encode_unichar(s
+ n
, (char32_t
) x
);
2463 else if (utf16_is_trailing_surrogate(x
))
2468 if (c
[0] != '\\' || c
[1] != 'u')
2471 r
= unhex_ucs2(c
+ 2, &y
);
2477 if (!utf16_is_trailing_surrogate(y
))
2480 n
+= utf8_encode_unichar(s
+ n
, utf16_surrogate_pair_to_unichar(x
, y
));
2487 if (!GREEDY_REALLOC(s
, allocated
, n
+ 2))
2495 len
= utf8_encoded_valid_unichar(c
, (size_t) -1);
2499 if (!GREEDY_REALLOC(s
, allocated
, n
+ len
+ 1))
2502 memcpy(s
+ n
, c
, len
);
2508 static int json_parse_number(const char **p
, JsonValue
*ret
) {
2509 bool negative
= false, exponent_negative
= false, is_real
= false;
2510 long double x
= 0.0, y
= 0.0, exponent
= 0.0, shift
= 1.0;
2529 if (!strchr("123456789", *c
) || *c
== 0)
2536 if (i
< INTMAX_MIN
/ 10) /* overflow */
2539 intmax_t t
= 10 * i
;
2541 if (t
< INTMAX_MIN
+ (*c
- '0')) /* overflow */
2547 if (u
> UINTMAX_MAX
/ 10) /* overflow */
2550 uintmax_t t
= 10 * u
;
2552 if (t
> UINTMAX_MAX
- (*c
- '0')) /* overflow */
2560 x
= 10.0 * x
+ (*c
- '0');
2563 } while (strchr("0123456789", *c
) && *c
!= 0);
2570 if (!strchr("0123456789", *c
) || *c
== 0)
2574 y
= 10.0 * y
+ (*c
- '0');
2575 shift
= 10.0 * shift
;
2577 } while (strchr("0123456789", *c
) && *c
!= 0);
2580 if (IN_SET(*c
, 'e', 'E')) {
2585 exponent_negative
= true;
2587 } else if (*c
== '+')
2590 if (!strchr("0123456789", *c
) || *c
== 0)
2594 exponent
= 10.0 * exponent
+ (*c
- '0');
2596 } while (strchr("0123456789", *c
) && *c
!= 0);
2602 ret
->real
= ((negative
? -1.0 : 1.0) * (x
+ (y
/ shift
))) * exp10l((exponent_negative
? -1.0 : 1.0) * exponent
);
2603 return JSON_TOKEN_REAL
;
2604 } else if (negative
) {
2606 return JSON_TOKEN_INTEGER
;
2609 return JSON_TOKEN_UNSIGNED
;
2616 JsonValue
*ret_value
,
2617 unsigned *ret_line
, /* 'ret_line' returns the line at the beginning of this token */
2618 unsigned *ret_column
,
2620 unsigned *line
, /* 'line' is used as a line state, it always reflect the line we are at after the token was read */
2623 unsigned start_line
, start_column
;
2624 const char *start
, *c
;
2644 t
= PTR_TO_INT(*state
);
2645 if (t
== STATE_NULL
) {
2651 /* Skip over the whitespace */
2652 n
= strspn(*p
, WHITESPACE
);
2653 inc_lines_columns(line
, column
, *p
, n
);
2656 /* Remember where we started processing this token */
2659 start_column
= *column
;
2663 *ret_value
= JSON_VALUE_NULL
;
2674 *state
= INT_TO_PTR(STATE_VALUE
);
2675 r
= JSON_TOKEN_OBJECT_OPEN
;
2678 } else if (*c
== '}') {
2680 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2681 r
= JSON_TOKEN_OBJECT_CLOSE
;
2684 } else if (*c
== '[') {
2686 *state
= INT_TO_PTR(STATE_VALUE
);
2687 r
= JSON_TOKEN_ARRAY_OPEN
;
2690 } else if (*c
== ']') {
2692 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2693 r
= JSON_TOKEN_ARRAY_CLOSE
;
2696 } else if (*c
== '"') {
2698 r
= json_parse_string(&c
, ret_string
);
2702 *ret_value
= JSON_VALUE_NULL
;
2703 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2706 } else if (strchr("-0123456789", *c
)) {
2708 r
= json_parse_number(&c
, ret_value
);
2713 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2716 } else if (startswith(c
, "true")) {
2718 ret_value
->boolean
= true;
2720 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2721 r
= JSON_TOKEN_BOOLEAN
;
2724 } else if (startswith(c
, "false")) {
2726 ret_value
->boolean
= false;
2728 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2729 r
= JSON_TOKEN_BOOLEAN
;
2732 } else if (startswith(c
, "null")) {
2734 *ret_value
= JSON_VALUE_NULL
;
2736 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2737 r
= JSON_TOKEN_NULL
;
2744 case STATE_VALUE_POST
:
2748 *state
= INT_TO_PTR(STATE_VALUE
);
2749 r
= JSON_TOKEN_COLON
;
2752 } else if (*c
== ',') {
2754 *state
= INT_TO_PTR(STATE_VALUE
);
2755 r
= JSON_TOKEN_COMMA
;
2758 } else if (*c
== '}') {
2760 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2761 r
= JSON_TOKEN_OBJECT_CLOSE
;
2764 } else if (*c
== ']') {
2766 *state
= INT_TO_PTR(STATE_VALUE_POST
);
2767 r
= JSON_TOKEN_ARRAY_CLOSE
;
2774 assert_not_reached("Unexpected tokenizer state");
2779 *ret_value
= JSON_VALUE_NULL
;
2782 inc_lines_columns(line
, column
, start
, c
- start
);
2785 *ret_line
= start_line
;
2786 *ret_column
= start_column
;
2791 typedef enum JsonExpect
{
2792 /* The following values are used by json_parse() */
2795 EXPECT_OBJECT_FIRST_KEY
,
2796 EXPECT_OBJECT_NEXT_KEY
,
2797 EXPECT_OBJECT_COLON
,
2798 EXPECT_OBJECT_VALUE
,
2799 EXPECT_OBJECT_COMMA
,
2800 EXPECT_ARRAY_FIRST_ELEMENT
,
2801 EXPECT_ARRAY_NEXT_ELEMENT
,
2804 /* And these are used by json_build() */
2805 EXPECT_ARRAY_ELEMENT
,
2809 typedef struct JsonStack
{
2811 JsonVariant
**elements
;
2812 size_t n_elements
, n_elements_allocated
;
2813 unsigned line_before
;
2814 unsigned column_before
;
2815 size_t n_suppress
; /* When building: if > 0, suppress this many subsequent elements. If == (size_t) -1, suppress all subsequent elements */
2818 static void json_stack_release(JsonStack
*s
) {
2821 json_variant_unref_many(s
->elements
, s
->n_elements
);
2822 s
->elements
= mfree(s
->elements
);
2825 static int json_parse_internal(
2828 JsonParseFlags flags
,
2832 bool continue_end
) {
2834 size_t n_stack
= 1, n_stack_allocated
= 0, i
;
2835 unsigned line_buffer
= 0, column_buffer
= 0;
2836 void *tokenizer_state
= NULL
;
2837 JsonStack
*stack
= NULL
;
2841 assert_return(input
, -EINVAL
);
2842 assert_return(ret
, -EINVAL
);
2846 if (!GREEDY_REALLOC(stack
, n_stack_allocated
, n_stack
))
2849 stack
[0] = (JsonStack
) {
2850 .expect
= EXPECT_TOPLEVEL
,
2854 line
= &line_buffer
;
2856 column
= &column_buffer
;
2859 _cleanup_(json_variant_unrefp
) JsonVariant
*add
= NULL
;
2860 _cleanup_free_
char *string
= NULL
;
2861 unsigned line_token
, column_token
;
2866 assert(n_stack
> 0);
2867 current
= stack
+ n_stack
- 1;
2869 if (continue_end
&& current
->expect
== EXPECT_END
)
2872 token
= json_tokenize(&p
, &string
, &value
, &line_token
, &column_token
, &tokenizer_state
, line
, column
);
2880 case JSON_TOKEN_END
:
2881 if (current
->expect
!= EXPECT_END
) {
2886 assert(current
->n_elements
== 1);
2887 assert(n_stack
== 1);
2890 case JSON_TOKEN_COLON
:
2892 if (current
->expect
!= EXPECT_OBJECT_COLON
) {
2897 current
->expect
= EXPECT_OBJECT_VALUE
;
2900 case JSON_TOKEN_COMMA
:
2902 if (current
->expect
== EXPECT_OBJECT_COMMA
)
2903 current
->expect
= EXPECT_OBJECT_NEXT_KEY
;
2904 else if (current
->expect
== EXPECT_ARRAY_COMMA
)
2905 current
->expect
= EXPECT_ARRAY_NEXT_ELEMENT
;
2913 case JSON_TOKEN_OBJECT_OPEN
:
2915 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
2920 if (!GREEDY_REALLOC(stack
, n_stack_allocated
, n_stack
+1)) {
2924 current
= stack
+ n_stack
- 1;
2926 /* Prepare the expect for when we return from the child */
2927 if (current
->expect
== EXPECT_TOPLEVEL
)
2928 current
->expect
= EXPECT_END
;
2929 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
2930 current
->expect
= EXPECT_OBJECT_COMMA
;
2932 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
2933 current
->expect
= EXPECT_ARRAY_COMMA
;
2936 stack
[n_stack
++] = (JsonStack
) {
2937 .expect
= EXPECT_OBJECT_FIRST_KEY
,
2938 .line_before
= line_token
,
2939 .column_before
= column_token
,
2942 current
= stack
+ n_stack
- 1;
2945 case JSON_TOKEN_OBJECT_CLOSE
:
2946 if (!IN_SET(current
->expect
, EXPECT_OBJECT_FIRST_KEY
, EXPECT_OBJECT_COMMA
)) {
2951 assert(n_stack
> 1);
2953 r
= json_variant_new_object(&add
, current
->elements
, current
->n_elements
);
2957 line_token
= current
->line_before
;
2958 column_token
= current
->column_before
;
2960 json_stack_release(current
);
2961 n_stack
--, current
--;
2965 case JSON_TOKEN_ARRAY_OPEN
:
2966 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
2971 if (!GREEDY_REALLOC(stack
, n_stack_allocated
, n_stack
+1)) {
2975 current
= stack
+ n_stack
- 1;
2977 /* Prepare the expect for when we return from the child */
2978 if (current
->expect
== EXPECT_TOPLEVEL
)
2979 current
->expect
= EXPECT_END
;
2980 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
2981 current
->expect
= EXPECT_OBJECT_COMMA
;
2983 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
2984 current
->expect
= EXPECT_ARRAY_COMMA
;
2987 stack
[n_stack
++] = (JsonStack
) {
2988 .expect
= EXPECT_ARRAY_FIRST_ELEMENT
,
2989 .line_before
= line_token
,
2990 .column_before
= column_token
,
2995 case JSON_TOKEN_ARRAY_CLOSE
:
2996 if (!IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_COMMA
)) {
3001 assert(n_stack
> 1);
3003 r
= json_variant_new_array(&add
, current
->elements
, current
->n_elements
);
3007 line_token
= current
->line_before
;
3008 column_token
= current
->column_before
;
3010 json_stack_release(current
);
3011 n_stack
--, current
--;
3014 case JSON_TOKEN_STRING
:
3015 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_FIRST_KEY
, EXPECT_OBJECT_NEXT_KEY
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
3020 r
= json_variant_new_string(&add
, string
);
3024 if (current
->expect
== EXPECT_TOPLEVEL
)
3025 current
->expect
= EXPECT_END
;
3026 else if (IN_SET(current
->expect
, EXPECT_OBJECT_FIRST_KEY
, EXPECT_OBJECT_NEXT_KEY
))
3027 current
->expect
= EXPECT_OBJECT_COLON
;
3028 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3029 current
->expect
= EXPECT_OBJECT_COMMA
;
3031 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
3032 current
->expect
= EXPECT_ARRAY_COMMA
;
3037 case JSON_TOKEN_REAL
:
3038 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
3043 r
= json_variant_new_real(&add
, value
.real
);
3047 if (current
->expect
== EXPECT_TOPLEVEL
)
3048 current
->expect
= EXPECT_END
;
3049 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3050 current
->expect
= EXPECT_OBJECT_COMMA
;
3052 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
3053 current
->expect
= EXPECT_ARRAY_COMMA
;
3058 case JSON_TOKEN_INTEGER
:
3059 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
3064 r
= json_variant_new_integer(&add
, value
.integer
);
3068 if (current
->expect
== EXPECT_TOPLEVEL
)
3069 current
->expect
= EXPECT_END
;
3070 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3071 current
->expect
= EXPECT_OBJECT_COMMA
;
3073 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
3074 current
->expect
= EXPECT_ARRAY_COMMA
;
3079 case JSON_TOKEN_UNSIGNED
:
3080 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
3085 r
= json_variant_new_unsigned(&add
, value
.unsig
);
3089 if (current
->expect
== EXPECT_TOPLEVEL
)
3090 current
->expect
= EXPECT_END
;
3091 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3092 current
->expect
= EXPECT_OBJECT_COMMA
;
3094 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
3095 current
->expect
= EXPECT_ARRAY_COMMA
;
3100 case JSON_TOKEN_BOOLEAN
:
3101 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
3106 r
= json_variant_new_boolean(&add
, value
.boolean
);
3110 if (current
->expect
== EXPECT_TOPLEVEL
)
3111 current
->expect
= EXPECT_END
;
3112 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3113 current
->expect
= EXPECT_OBJECT_COMMA
;
3115 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
3116 current
->expect
= EXPECT_ARRAY_COMMA
;
3121 case JSON_TOKEN_NULL
:
3122 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
)) {
3127 r
= json_variant_new_null(&add
);
3131 if (current
->expect
== EXPECT_TOPLEVEL
)
3132 current
->expect
= EXPECT_END
;
3133 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3134 current
->expect
= EXPECT_OBJECT_COMMA
;
3136 assert(IN_SET(current
->expect
, EXPECT_ARRAY_FIRST_ELEMENT
, EXPECT_ARRAY_NEXT_ELEMENT
));
3137 current
->expect
= EXPECT_ARRAY_COMMA
;
3143 assert_not_reached("Unexpected token");
3147 /* If we are asked to make this parsed object sensitive, then let's apply this
3148 * immediately after allocating each variant, so that when we abort half-way
3149 * everything we already allocated that is then freed is correctly marked. */
3150 if (FLAGS_SET(flags
, JSON_PARSE_SENSITIVE
))
3151 json_variant_sensitive(add
);
3153 (void) json_variant_set_source(&add
, source
, line_token
, column_token
);
3155 if (!GREEDY_REALLOC(current
->elements
, current
->n_elements_allocated
, current
->n_elements
+ 1)) {
3160 current
->elements
[current
->n_elements
++] = TAKE_PTR(add
);
3165 assert(n_stack
== 1);
3166 assert(stack
[0].n_elements
== 1);
3168 *ret
= json_variant_ref(stack
[0].elements
[0]);
3173 for (i
= 0; i
< n_stack
; i
++)
3174 json_stack_release(stack
+ i
);
3181 int json_parse(const char *input
, JsonParseFlags flags
, JsonVariant
**ret
, unsigned *ret_line
, unsigned *ret_column
) {
3182 return json_parse_internal(&input
, NULL
, flags
, ret
, ret_line
, ret_column
, false);
3185 int json_parse_continue(const char **p
, JsonParseFlags flags
, JsonVariant
**ret
, unsigned *ret_line
, unsigned *ret_column
) {
3186 return json_parse_internal(p
, NULL
, flags
, ret
, ret_line
, ret_column
, true);
3189 int json_parse_file_at(FILE *f
, int dir_fd
, const char *path
, JsonParseFlags flags
, JsonVariant
**ret
, unsigned *ret_line
, unsigned *ret_column
) {
3190 _cleanup_(json_source_unrefp
) JsonSource
*source
= NULL
;
3191 _cleanup_free_
char *text
= NULL
;
3196 r
= read_full_stream(f
, &text
, NULL
);
3198 r
= read_full_file_full(dir_fd
, path
, 0, NULL
, &text
, NULL
);
3205 source
= json_source_new(path
);
3211 return json_parse_internal(&p
, source
, flags
, ret
, ret_line
, ret_column
, false);
3214 int json_buildv(JsonVariant
**ret
, va_list ap
) {
3215 JsonStack
*stack
= NULL
;
3216 size_t n_stack
= 1, n_stack_allocated
= 0, i
;
3219 assert_return(ret
, -EINVAL
);
3221 if (!GREEDY_REALLOC(stack
, n_stack_allocated
, n_stack
))
3224 stack
[0] = (JsonStack
) {
3225 .expect
= EXPECT_TOPLEVEL
,
3229 _cleanup_(json_variant_unrefp
) JsonVariant
*add
= NULL
;
3230 size_t n_subtract
= 0; /* how much to subtract from current->n_suppress, i.e. how many elements would
3231 * have been added to the current variant */
3235 assert(n_stack
> 0);
3236 current
= stack
+ n_stack
- 1;
3238 if (current
->expect
== EXPECT_END
)
3241 command
= va_arg(ap
, int);
3245 case _JSON_BUILD_STRING
: {
3248 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3253 p
= va_arg(ap
, const char *);
3255 if (current
->n_suppress
== 0) {
3256 r
= json_variant_new_string(&add
, p
);
3263 if (current
->expect
== EXPECT_TOPLEVEL
)
3264 current
->expect
= EXPECT_END
;
3265 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3266 current
->expect
= EXPECT_OBJECT_KEY
;
3268 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3273 case _JSON_BUILD_INTEGER
: {
3276 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3281 j
= va_arg(ap
, intmax_t);
3283 if (current
->n_suppress
== 0) {
3284 r
= json_variant_new_integer(&add
, j
);
3291 if (current
->expect
== EXPECT_TOPLEVEL
)
3292 current
->expect
= EXPECT_END
;
3293 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3294 current
->expect
= EXPECT_OBJECT_KEY
;
3296 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3301 case _JSON_BUILD_UNSIGNED
: {
3304 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3309 j
= va_arg(ap
, uintmax_t);
3311 if (current
->n_suppress
== 0) {
3312 r
= json_variant_new_unsigned(&add
, j
);
3319 if (current
->expect
== EXPECT_TOPLEVEL
)
3320 current
->expect
= EXPECT_END
;
3321 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3322 current
->expect
= EXPECT_OBJECT_KEY
;
3324 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3329 case _JSON_BUILD_REAL
: {
3332 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3337 d
= va_arg(ap
, long double);
3339 if (current
->n_suppress
== 0) {
3340 r
= json_variant_new_real(&add
, d
);
3347 if (current
->expect
== EXPECT_TOPLEVEL
)
3348 current
->expect
= EXPECT_END
;
3349 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3350 current
->expect
= EXPECT_OBJECT_KEY
;
3352 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3357 case _JSON_BUILD_BOOLEAN
: {
3360 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3365 b
= va_arg(ap
, int);
3367 if (current
->n_suppress
== 0) {
3368 r
= json_variant_new_boolean(&add
, b
);
3375 if (current
->expect
== EXPECT_TOPLEVEL
)
3376 current
->expect
= EXPECT_END
;
3377 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3378 current
->expect
= EXPECT_OBJECT_KEY
;
3380 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3385 case _JSON_BUILD_NULL
:
3387 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3392 if (current
->n_suppress
== 0) {
3393 r
= json_variant_new_null(&add
);
3400 if (current
->expect
== EXPECT_TOPLEVEL
)
3401 current
->expect
= EXPECT_END
;
3402 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3403 current
->expect
= EXPECT_OBJECT_KEY
;
3405 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3409 case _JSON_BUILD_VARIANT
:
3411 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3416 /* Note that we don't care for current->n_suppress here, after all the variant is already
3417 * allocated anyway... */
3418 add
= va_arg(ap
, JsonVariant
*);
3420 add
= JSON_VARIANT_MAGIC_NULL
;
3422 json_variant_ref(add
);
3426 if (current
->expect
== EXPECT_TOPLEVEL
)
3427 current
->expect
= EXPECT_END
;
3428 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3429 current
->expect
= EXPECT_OBJECT_KEY
;
3431 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3435 case _JSON_BUILD_VARIANT_ARRAY
: {
3436 JsonVariant
**array
;
3439 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3444 array
= va_arg(ap
, JsonVariant
**);
3445 n
= va_arg(ap
, size_t);
3447 if (current
->n_suppress
== 0) {
3448 r
= json_variant_new_array(&add
, array
, n
);
3455 if (current
->expect
== EXPECT_TOPLEVEL
)
3456 current
->expect
= EXPECT_END
;
3457 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3458 current
->expect
= EXPECT_OBJECT_KEY
;
3460 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3465 case _JSON_BUILD_LITERAL
: {
3468 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3473 l
= va_arg(ap
, const char *);
3476 /* Note that we don't care for current->n_suppress here, we should generate parsing
3477 * errors even in suppressed object properties */
3479 r
= json_parse(l
, 0, &add
, NULL
, NULL
);
3483 add
= JSON_VARIANT_MAGIC_NULL
;
3487 if (current
->expect
== EXPECT_TOPLEVEL
)
3488 current
->expect
= EXPECT_END
;
3489 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3490 current
->expect
= EXPECT_OBJECT_KEY
;
3492 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3497 case _JSON_BUILD_ARRAY_BEGIN
:
3499 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3504 if (!GREEDY_REALLOC(stack
, n_stack_allocated
, n_stack
+1)) {
3508 current
= stack
+ n_stack
- 1;
3510 if (current
->expect
== EXPECT_TOPLEVEL
)
3511 current
->expect
= EXPECT_END
;
3512 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3513 current
->expect
= EXPECT_OBJECT_KEY
;
3515 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3517 stack
[n_stack
++] = (JsonStack
) {
3518 .expect
= EXPECT_ARRAY_ELEMENT
,
3519 .n_suppress
= current
->n_suppress
!= 0 ? (size_t) -1 : 0, /* if we shall suppress the
3520 * new array, then we should
3521 * also suppress all array
3527 case _JSON_BUILD_ARRAY_END
:
3528 if (current
->expect
!= EXPECT_ARRAY_ELEMENT
) {
3533 assert(n_stack
> 1);
3535 if (current
->n_suppress
== 0) {
3536 r
= json_variant_new_array(&add
, current
->elements
, current
->n_elements
);
3543 json_stack_release(current
);
3544 n_stack
--, current
--;
3548 case _JSON_BUILD_STRV
: {
3551 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3556 l
= va_arg(ap
, char **);
3558 if (current
->n_suppress
== 0) {
3559 r
= json_variant_new_array_strv(&add
, l
);
3566 if (current
->expect
== EXPECT_TOPLEVEL
)
3567 current
->expect
= EXPECT_END
;
3568 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3569 current
->expect
= EXPECT_OBJECT_KEY
;
3571 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3576 case _JSON_BUILD_BASE64
: {
3580 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3585 p
= va_arg(ap
, const void *);
3586 n
= va_arg(ap
, size_t);
3588 if (current
->n_suppress
== 0) {
3589 r
= json_variant_new_base64(&add
, p
, n
);
3596 if (current
->expect
== EXPECT_TOPLEVEL
)
3597 current
->expect
= EXPECT_END
;
3598 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3599 current
->expect
= EXPECT_OBJECT_KEY
;
3601 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3606 case _JSON_BUILD_ID128
: {
3609 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3614 id
= va_arg(ap
, sd_id128_t
);
3616 if (current
->n_suppress
== 0) {
3617 r
= json_variant_new_id128(&add
, id
);
3624 if (current
->expect
== EXPECT_TOPLEVEL
)
3625 current
->expect
= EXPECT_END
;
3626 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3627 current
->expect
= EXPECT_OBJECT_KEY
;
3629 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3634 case _JSON_BUILD_BYTE_ARRAY
: {
3638 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3643 array
= va_arg(ap
, const void*);
3644 n
= va_arg(ap
, size_t);
3646 if (current
->n_suppress
== 0) {
3647 r
= json_variant_new_array_bytes(&add
, array
, n
);
3654 if (current
->expect
== EXPECT_TOPLEVEL
)
3655 current
->expect
= EXPECT_END
;
3656 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3657 current
->expect
= EXPECT_OBJECT_KEY
;
3659 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3664 case _JSON_BUILD_OBJECT_BEGIN
:
3666 if (!IN_SET(current
->expect
, EXPECT_TOPLEVEL
, EXPECT_OBJECT_VALUE
, EXPECT_ARRAY_ELEMENT
)) {
3671 if (!GREEDY_REALLOC(stack
, n_stack_allocated
, n_stack
+1)) {
3675 current
= stack
+ n_stack
- 1;
3677 if (current
->expect
== EXPECT_TOPLEVEL
)
3678 current
->expect
= EXPECT_END
;
3679 else if (current
->expect
== EXPECT_OBJECT_VALUE
)
3680 current
->expect
= EXPECT_OBJECT_KEY
;
3682 assert(current
->expect
== EXPECT_ARRAY_ELEMENT
);
3684 stack
[n_stack
++] = (JsonStack
) {
3685 .expect
= EXPECT_OBJECT_KEY
,
3686 .n_suppress
= current
->n_suppress
!= 0 ? (size_t) -1 : 0, /* if we shall suppress the
3687 * new object, then we should
3688 * also suppress all object
3694 case _JSON_BUILD_OBJECT_END
:
3696 if (current
->expect
!= EXPECT_OBJECT_KEY
) {
3701 assert(n_stack
> 1);
3703 if (current
->n_suppress
== 0) {
3704 r
= json_variant_new_object(&add
, current
->elements
, current
->n_elements
);
3711 json_stack_release(current
);
3712 n_stack
--, current
--;
3716 case _JSON_BUILD_PAIR
: {
3719 if (current
->expect
!= EXPECT_OBJECT_KEY
) {
3724 n
= va_arg(ap
, const char *);
3726 if (current
->n_suppress
== 0) {
3727 r
= json_variant_new_string(&add
, n
);
3734 current
->expect
= EXPECT_OBJECT_VALUE
;
3738 case _JSON_BUILD_PAIR_CONDITION
: {
3742 if (current
->expect
!= EXPECT_OBJECT_KEY
) {
3747 b
= va_arg(ap
, int);
3748 n
= va_arg(ap
, const char *);
3750 if (b
&& current
->n_suppress
== 0) {
3751 r
= json_variant_new_string(&add
, n
);
3756 n_subtract
= 1; /* we generated one item */
3758 if (!b
&& current
->n_suppress
!= (size_t) -1)
3759 current
->n_suppress
+= 2; /* Suppress this one and the next item */
3761 current
->expect
= EXPECT_OBJECT_VALUE
;
3765 /* If a variant was generated, add it to our current variant, but only if we are not supposed to suppress additions */
3766 if (add
&& current
->n_suppress
== 0) {
3767 if (!GREEDY_REALLOC(current
->elements
, current
->n_elements_allocated
, current
->n_elements
+ 1)) {
3772 current
->elements
[current
->n_elements
++] = TAKE_PTR(add
);
3775 /* If we are supposed to suppress items, let's subtract how many items where generated from that
3776 * counter. Except if the counter is (size_t) -1, i.e. we shall suppress an infinite number of elements
3777 * on this stack level */
3778 if (current
->n_suppress
!= (size_t) -1) {
3779 if (current
->n_suppress
<= n_subtract
) /* Saturated */
3780 current
->n_suppress
= 0;
3782 current
->n_suppress
-= n_subtract
;
3787 assert(n_stack
== 1);
3788 assert(stack
[0].n_elements
== 1);
3790 *ret
= json_variant_ref(stack
[0].elements
[0]);
3794 for (i
= 0; i
< n_stack
; i
++)
3795 json_stack_release(stack
+ i
);
3802 int json_build(JsonVariant
**ret
, ...) {
3807 r
= json_buildv(ret
, ap
);
3813 int json_log_internal(
3814 JsonVariant
*variant
,
3820 const char *format
, ...) {
3824 unsigned source_line
, source_column
;
3825 char buffer
[LINE_MAX
];
3830 errno
= ERRNO_VALUE(error
);
3832 va_start(ap
, format
);
3833 (void) vsnprintf(buffer
, sizeof buffer
, format
, ap
);
3837 r
= json_variant_get_source(variant
, &source
, &source_line
, &source_column
);
3846 if (source
&& source_line
> 0 && source_column
> 0)
3847 return log_struct_internal(
3848 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD
, level
),
3851 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR
,
3852 "CONFIG_FILE=%s", source
,
3853 "CONFIG_LINE=%u", source_line
,
3854 "CONFIG_COLUMN=%u", source_column
,
3855 LOG_MESSAGE("%s:%u:%u: %s", source
, source_line
, source_column
, buffer
),
3858 return log_struct_internal(
3859 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD
, level
),
3862 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR
,
3863 LOG_MESSAGE("%s", buffer
),
3867 int json_dispatch(JsonVariant
*v
, const JsonDispatch table
[], JsonDispatchCallback bad
, JsonDispatchFlags flags
, void *userdata
) {
3868 const JsonDispatch
*p
;
3873 if (!json_variant_is_object(v
)) {
3874 json_log(v
, flags
, 0, "JSON variant is not an object.");
3876 if (flags
& JSON_PERMISSIVE
)
3882 for (p
= table
, m
= 0; p
->name
; p
++)
3885 found
= newa0(bool, m
);
3887 n
= json_variant_elements(v
);
3888 for (i
= 0; i
< n
; i
+= 2) {
3889 JsonVariant
*key
, *value
;
3891 assert_se(key
= json_variant_by_index(v
, i
));
3892 assert_se(value
= json_variant_by_index(v
, i
+1));
3894 for (p
= table
; p
->name
; p
++)
3895 if (p
->name
== POINTER_MAX
||
3896 streq_ptr(json_variant_string(key
), p
->name
))
3899 if (p
->name
) { /* Found a matching entry! :-) */
3900 JsonDispatchFlags merged_flags
;
3902 merged_flags
= flags
| p
->flags
;
3904 if (p
->type
!= _JSON_VARIANT_TYPE_INVALID
&&
3905 !json_variant_has_type(value
, p
->type
)) {
3907 json_log(value
, merged_flags
, 0,
3908 "Object field '%s' has wrong type %s, expected %s.", json_variant_string(key
),
3909 json_variant_type_to_string(json_variant_type(value
)), json_variant_type_to_string(p
->type
));
3911 if (merged_flags
& JSON_PERMISSIVE
)
3917 if (found
[p
-table
]) {
3918 json_log(value
, merged_flags
, 0, "Duplicate object field '%s'.", json_variant_string(key
));
3920 if (merged_flags
& JSON_PERMISSIVE
)
3926 found
[p
-table
] = true;
3929 r
= p
->callback(json_variant_string(key
), value
, merged_flags
, (uint8_t*) userdata
+ p
->offset
);
3931 if (merged_flags
& JSON_PERMISSIVE
)
3940 } else { /* Didn't find a matching entry! :-( */
3943 r
= bad(json_variant_string(key
), value
, flags
, userdata
);
3945 if (flags
& JSON_PERMISSIVE
)
3953 json_log(value
, flags
, 0, "Unexpected object field '%s'.", json_variant_string(key
));
3955 if (flags
& JSON_PERMISSIVE
)
3958 return -EADDRNOTAVAIL
;
3963 for (p
= table
; p
->name
; p
++) {
3964 JsonDispatchFlags merged_flags
= p
->flags
| flags
;
3966 if ((merged_flags
& JSON_MANDATORY
) && !found
[p
-table
]) {
3967 json_log(v
, merged_flags
, 0, "Missing object field '%s'.", p
->name
);
3969 if ((merged_flags
& JSON_PERMISSIVE
))
3979 int json_dispatch_boolean(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
3985 if (!json_variant_is_boolean(variant
))
3986 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a boolean.", strna(name
));
3988 *b
= json_variant_boolean(variant
);
3992 int json_dispatch_tristate(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
3998 if (json_variant_is_null(variant
)) {
4003 if (!json_variant_is_boolean(variant
))
4004 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a boolean.", strna(name
));
4006 *b
= json_variant_boolean(variant
);
4010 int json_dispatch_integer(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4011 intmax_t *i
= userdata
;
4016 if (!json_variant_is_integer(variant
))
4017 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
4019 *i
= json_variant_integer(variant
);
4023 int json_dispatch_unsigned(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4024 uintmax_t *u
= userdata
;
4029 if (!json_variant_is_unsigned(variant
))
4030 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an unsigned integer.", strna(name
));
4032 *u
= json_variant_unsigned(variant
);
4036 int json_dispatch_uint32(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4037 uint32_t *u
= userdata
;
4042 if (!json_variant_is_unsigned(variant
))
4043 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an unsigned integer.", strna(name
));
4045 if (json_variant_unsigned(variant
) > UINT32_MAX
)
4046 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "JSON field '%s' out of bounds.", strna(name
));
4048 *u
= (uint32_t) json_variant_unsigned(variant
);
4052 int json_dispatch_int32(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4053 int32_t *i
= userdata
;
4058 if (!json_variant_is_integer(variant
))
4059 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
4061 if (json_variant_integer(variant
) < INT32_MIN
|| json_variant_integer(variant
) > INT32_MAX
)
4062 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "JSON field '%s' out of bounds.", strna(name
));
4064 *i
= (int32_t) json_variant_integer(variant
);
4068 int json_dispatch_string(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4069 char **s
= userdata
;
4075 if (json_variant_is_null(variant
)) {
4080 if (!json_variant_is_string(variant
))
4081 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
4083 if ((flags
& JSON_SAFE
) && !string_is_safe(json_variant_string(variant
)))
4084 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' contains unsafe characters, refusing.", strna(name
));
4086 r
= free_and_strdup(s
, json_variant_string(variant
));
4088 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
4093 int json_dispatch_const_string(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4094 const char **s
= userdata
;
4099 if (json_variant_is_null(variant
)) {
4104 if (!json_variant_is_string(variant
))
4105 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
4107 if ((flags
& JSON_SAFE
) && !string_is_safe(json_variant_string(variant
)))
4108 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' contains unsafe characters, refusing.", strna(name
));
4110 *s
= json_variant_string(variant
);
4114 int json_dispatch_strv(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4115 _cleanup_strv_free_
char **l
= NULL
;
4116 char ***s
= userdata
;
4123 if (json_variant_is_null(variant
)) {
4128 /* Let's be flexible here: accept a single string in place of a single-item array */
4129 if (json_variant_is_string(variant
)) {
4130 if ((flags
& JSON_SAFE
) && !string_is_safe(json_variant_string(variant
)))
4131 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' contains unsafe characters, refusing.", strna(name
));
4133 l
= strv_new(json_variant_string(variant
));
4137 strv_free_and_replace(*s
, l
);
4141 if (!json_variant_is_array(variant
))
4142 return json_log(variant
, SYNTHETIC_ERRNO(EINVAL
), flags
, "JSON field '%s' is not an array.", strna(name
));
4144 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
4145 if (!json_variant_is_string(e
))
4146 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
4148 if ((flags
& JSON_SAFE
) && !string_is_safe(json_variant_string(e
)))
4149 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' contains unsafe characters, refusing.", strna(name
));
4151 r
= strv_extend(&l
, json_variant_string(e
));
4153 return json_log(e
, flags
, r
, "Failed to append array element: %m");
4156 strv_free_and_replace(*s
, l
);
4160 int json_dispatch_variant(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4161 JsonVariant
**p
= userdata
;
4166 json_variant_unref(*p
);
4167 *p
= json_variant_ref(variant
);
4172 int json_dispatch_uid_gid(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4173 uid_t
*uid
= userdata
;
4176 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
4177 assert_cc(sizeof(gid_t
) == sizeof(uint32_t));
4179 DISABLE_WARNING_TYPE_LIMITS
;
4180 assert_cc(((uid_t
) -1 < (uid_t
) 0) == ((gid_t
) -1 < (gid_t
) 0));
4183 if (json_variant_is_null(variant
)) {
4188 if (!json_variant_is_unsigned(variant
))
4189 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
4191 k
= json_variant_unsigned(variant
);
4192 if (k
> UINT32_MAX
|| !uid_is_valid(k
))
4193 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid UID/GID.", strna(name
));
4199 int json_dispatch_user_group_name(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4200 char **s
= userdata
;
4204 if (json_variant_is_null(variant
)) {
4209 if (!json_variant_is_string(variant
))
4210 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
4212 n
= json_variant_string(variant
);
4213 if (!valid_user_group_name(n
, FLAGS_SET(flags
, JSON_RELAX
) ? VALID_USER_RELAX
: 0))
4214 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid user/group name.", strna(name
));
4216 r
= free_and_strdup(s
, n
);
4218 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
4223 int json_dispatch_id128(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4224 sd_id128_t
*uuid
= userdata
;
4227 if (json_variant_is_null(variant
)) {
4228 *uuid
= SD_ID128_NULL
;
4232 if (!json_variant_is_string(variant
))
4233 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
4235 r
= sd_id128_from_string(json_variant_string(variant
), uuid
);
4237 return json_log(variant
, flags
, r
, "JSON field '%s' is not a valid UID.", strna(name
));
4242 int json_dispatch_unsupported(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
4243 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not allowed in this object.", strna(name
));
4246 static int json_cmp_strings(const void *x
, const void *y
) {
4247 JsonVariant
*const *a
= x
, *const *b
= y
;
4249 if (!json_variant_is_string(*a
) || !json_variant_is_string(*b
))
4252 return strcmp(json_variant_string(*a
), json_variant_string(*b
));
4255 int json_variant_sort(JsonVariant
**v
) {
4256 _cleanup_free_ JsonVariant
**a
= NULL
;
4257 JsonVariant
*n
= NULL
;
4263 if (json_variant_is_sorted(*v
))
4266 if (!json_variant_is_object(*v
))
4267 return -EMEDIUMTYPE
;
4269 /* Sorts they key/value pairs in an object variant */
4271 m
= json_variant_elements(*v
);
4272 a
= new(JsonVariant
*, m
);
4276 for (i
= 0; i
< m
; i
++)
4277 a
[i
] = json_variant_by_index(*v
, i
);
4279 qsort(a
, m
/2, sizeof(JsonVariant
*)*2, json_cmp_strings
);
4281 r
= json_variant_new_object(&n
, a
, m
);
4285 json_variant_propagate_sensitive(*v
, n
);
4287 if (!n
->sorted
) /* Check if this worked. This will fail if there are multiple identical keys used. */
4290 json_variant_unref(*v
);
4296 int json_variant_normalize(JsonVariant
**v
) {
4297 _cleanup_free_ JsonVariant
**a
= NULL
;
4298 JsonVariant
*n
= NULL
;
4304 if (json_variant_is_normalized(*v
))
4307 if (!json_variant_is_object(*v
) && !json_variant_is_array(*v
))
4308 return -EMEDIUMTYPE
;
4310 /* Sorts the key/value pairs in an object variant anywhere down the tree in the specified variant */
4312 m
= json_variant_elements(*v
);
4313 a
= new(JsonVariant
*, m
);
4317 for (i
= 0; i
< m
; i
++) {
4318 a
[i
] = json_variant_ref(json_variant_by_index(*v
, i
));
4320 r
= json_variant_normalize(a
+ i
);
4325 qsort(a
, m
/2, sizeof(JsonVariant
*)*2, json_cmp_strings
);
4327 if (json_variant_is_object(*v
))
4328 r
= json_variant_new_object(&n
, a
, m
);
4330 assert(json_variant_is_array(*v
));
4331 r
= json_variant_new_array(&n
, a
, m
);
4336 json_variant_propagate_sensitive(*v
, n
);
4338 if (!n
->normalized
) { /* Let's see if normalization worked. It will fail if there are multiple
4339 * identical keys used in the same object anywhere, or if there are floating
4340 * point numbers used (see below) */
4345 json_variant_unref(*v
);
4351 for (j
= 0; j
< i
; j
++)
4352 json_variant_unref(a
[j
]);
4357 bool json_variant_is_normalized(JsonVariant
*v
) {
4359 /* For now, let's consider anything containing numbers not expressible as integers as
4360 * non-normalized. That's because we cannot sensibly compare them due to accuracy issues, nor even
4361 * store them if they are too large. */
4362 if (json_variant_is_real(v
) && !json_variant_is_integer(v
) && !json_variant_is_unsigned(v
))
4365 /* The concept only applies to variants that include other variants, i.e. objects and arrays. All
4366 * others are normalized anyway. */
4367 if (!json_variant_is_object(v
) && !json_variant_is_array(v
))
4370 /* Empty objects/arrays don't include any other variant, hence are always normalized too */
4371 if (json_variant_elements(v
) == 0)
4374 return v
->normalized
; /* For everything else there's an explicit boolean we maintain */
4377 bool json_variant_is_sorted(JsonVariant
*v
) {
4379 /* Returns true if all key/value pairs of an object are properly sorted. Note that this only applies
4380 * to objects, not arrays. */
4382 if (!json_variant_is_object(v
))
4384 if (json_variant_elements(v
) <= 1)
4390 int json_variant_unbase64(JsonVariant
*v
, void **ret
, size_t *ret_size
) {
4392 if (!json_variant_is_string(v
))
4395 return unbase64mem(json_variant_string(v
), (size_t) -1, ret
, ret_size
);
4398 static const char* const json_variant_type_table
[_JSON_VARIANT_TYPE_MAX
] = {
4399 [JSON_VARIANT_STRING
] = "string",
4400 [JSON_VARIANT_INTEGER
] = "integer",
4401 [JSON_VARIANT_UNSIGNED
] = "unsigned",
4402 [JSON_VARIANT_REAL
] = "real",
4403 [JSON_VARIANT_NUMBER
] = "number",
4404 [JSON_VARIANT_BOOLEAN
] = "boolean",
4405 [JSON_VARIANT_ARRAY
] = "array",
4406 [JSON_VARIANT_OBJECT
] = "object",
4407 [JSON_VARIANT_NULL
] = "null",
4410 DEFINE_STRING_TABLE_LOOKUP(json_variant_type
, JsonVariantType
);