]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/json-internal.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 /* This header should include all prototypes only the JSON parser itself and
8 * its tests need access to. Normal code consuming the JSON parser should not
9 * interface with this. */
11 typedef union JsonValue
{
12 /* Encodes a simple value. On x86-64 this structure is 16 bytes wide (as long double is 128bit). */
19 /* Let's protect us against accidental structure size changes on our most relevant arch */
21 assert_cc(sizeof(JsonValue
) == 16U);
24 #define JSON_VALUE_NULL ((JsonValue) {})
26 /* We use fake JsonVariant objects for some special values, in order to avoid memory allocations for them. Note that
27 * effectively this means that there are multiple ways to encode the same objects: via these magic values or as
28 * properly allocated JsonVariant. We convert between both on-the-fly as necessary. */
29 #define JSON_VARIANT_MAGIC_TRUE ((JsonVariant*) 1)
30 #define JSON_VARIANT_MAGIC_FALSE ((JsonVariant*) 2)
31 #define JSON_VARIANT_MAGIC_NULL ((JsonVariant*) 3)
32 #define JSON_VARIANT_MAGIC_ZERO_INTEGER ((JsonVariant*) 4)
33 #define JSON_VARIANT_MAGIC_ZERO_UNSIGNED ((JsonVariant*) 5)
34 #define JSON_VARIANT_MAGIC_ZERO_REAL ((JsonVariant*) 6)
35 #define JSON_VARIANT_MAGIC_EMPTY_STRING ((JsonVariant*) 7)
36 #define JSON_VARIANT_MAGIC_EMPTY_ARRAY ((JsonVariant*) 8)
37 #define JSON_VARIANT_MAGIC_EMPTY_OBJECT ((JsonVariant*) 9)
38 #define _JSON_VARIANT_MAGIC_MAX ((JsonVariant*) 10)
40 /* This is only safe as long as we don't define more than 4K magic pointers, i.e. the page size of the simplest
41 * architectures we support. That's because we rely on the fact that malloc() will never allocate from the first memory
42 * page, as it is a faulting page for catching NULL pointer dereferences. */
43 assert_cc((uintptr_t) _JSON_VARIANT_MAGIC_MAX
< 4096U);
45 enum { /* JSON tokens */
49 JSON_TOKEN_OBJECT_OPEN
,
50 JSON_TOKEN_OBJECT_CLOSE
,
51 JSON_TOKEN_ARRAY_OPEN
,
52 JSON_TOKEN_ARRAY_CLOSE
,
60 _JSON_TOKEN_INVALID
= -1,
63 int json_tokenize(const char **p
, char **ret_string
, JsonValue
*ret_value
, unsigned *ret_line
, unsigned *ret_column
, void **state
, unsigned *line
, unsigned *column
);