HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-json/sd-json.c
Git blob: dea16611a22e2975d723e14e171d670c48aede44
Lines 2001–3000 of 6,031
for (size_t i = 0; i < sd_json_variant_elements(*v); i += 2) {
sd_json_variant *p;
p = sd_json_variant_by_index(*v, i);
if (!sd_json_variant_is_string(p))
return -EINVAL;
if (streq(sd_json_variant_string(p), field)) {
if (!array) {
array = new(sd_json_variant*, sd_json_variant_elements(*v));
if (!array)
return -ENOMEM;
for (k = 0; k < i; k++)
array[k] = sd_json_variant_by_index(*v, k);
}
} else if (array) {
array[k++] = p;
array[k++] = sd_json_variant_by_index(*v, i + 1);
}
}
if (!array) {
array = new(sd_json_variant*, sd_json_variant_elements(*v) + 2);
if (!array)
return -ENOMEM;
for (k = 0; k < sd_json_variant_elements(*v); k++)
array[k] = sd_json_variant_by_index(*v, k);
}
}
r = sd_json_variant_new_string(&field_variant, field);
if (r < 0)
return r;
array[k++] = field_variant;
array[k++] = value;
r = sd_json_variant_new_object(&w, array, k);
if (r < 0)
return r;
json_variant_propagate_sensitive(*v, w);
json_variant_unref_and_replace(*v, w);
return 1;
}
_public_ int sd_json_variant_set_fieldb(sd_json_variant **v, const char *field, ...) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
va_list ap;
int r;
va_start(ap, field);
r = sd_json_buildv(&w, ap);
va_end(ap);
if (r < 0)
return r;
return sd_json_variant_set_field(v, field, w);
}
_public_ int sd_json_variant_set_field_string(sd_json_variant **v, const char *field, const char *value) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
int r;
r = sd_json_variant_new_string(&m, value);
if (r < 0)
return r;
return sd_json_variant_set_field(v, field, m);
}
_public_ int sd_json_variant_set_field_id128(sd_json_variant **v, const char *field, sd_id128_t value) {
return sd_json_variant_set_field_string(v, field, SD_ID128_TO_STRING(value));
}
_public_ int sd_json_variant_set_field_uuid(sd_json_variant **v, const char *field, sd_id128_t value) {
return sd_json_variant_set_field_string(v, field, SD_ID128_TO_UUID_STRING(value));
}
_public_ int sd_json_variant_set_field_integer(sd_json_variant **v, const char *field, int64_t value) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
int r;
r = sd_json_variant_new_integer(&m, value);
if (r < 0)
return r;
return sd_json_variant_set_field(v, field, m);
}
_public_ int sd_json_variant_set_field_unsigned(sd_json_variant **v, const char *field, uint64_t value) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
int r;
r = sd_json_variant_new_unsigned(&m, value);
if (r < 0)
return r;
return sd_json_variant_set_field(v, field, m);
}
_public_ int sd_json_variant_set_field_boolean(sd_json_variant **v, const char *field, int b) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
int r;
r = sd_json_variant_new_boolean(&m, b);
if (r < 0)
return r;
return sd_json_variant_set_field(v, field, m);
}
_public_ int sd_json_variant_set_field_strv(sd_json_variant **v, const char *field, char **l) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
int r;
r = sd_json_variant_new_array_strv(&m, l);
if (r < 0)
return r;
return sd_json_variant_set_field(v, field, m);
}
_public_ int sd_json_variant_merge_object(sd_json_variant **v, sd_json_variant *m) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
_cleanup_free_ sd_json_variant **array = NULL;
size_t v_elements, m_elements, k;
bool v_blank, m_blank;
int r;
m = json_variant_dereference(m);
v_blank = sd_json_variant_is_blank_object(*v);
m_blank = sd_json_variant_is_blank_object(m);
if (!v_blank && !sd_json_variant_is_object(*v))
return -EINVAL;
if (!m_blank && !sd_json_variant_is_object(m))
return -EINVAL;
if (m_blank)
return 0; /* nothing to do */
if (v_blank) {
JSON_VARIANT_REPLACE(*v, sd_json_variant_ref(m));
return 1;
}
v_elements = sd_json_variant_elements(*v);
m_elements = sd_json_variant_elements(m);
if (v_elements > SIZE_MAX - m_elements) /* overflow check */
return -ENOMEM;
array = new(sd_json_variant*, v_elements + m_elements);
if (!array)
return -ENOMEM;
k = 0;
for (size_t i = 0; i < v_elements; i += 2) {
sd_json_variant *u;
u = sd_json_variant_by_index(*v, i);
if (!sd_json_variant_is_string(u))
return -EINVAL;
if (sd_json_variant_by_key(m, sd_json_variant_string(u)))
continue; /* skip if exists in second variant */
array[k++] = u;
array[k++] = sd_json_variant_by_index(*v, i + 1);
}
for (size_t i = 0; i < m_elements; i++)
array[k++] = sd_json_variant_by_index(m, i);
r = sd_json_variant_new_object(&w, array, k);
if (r < 0)
return r;
json_variant_propagate_sensitive(*v, w);
json_variant_propagate_sensitive(m, w);
json_variant_unref_and_replace(*v, w);
return 1;
}
_public_ int sd_json_variant_merge_objectb(sd_json_variant **v, ...) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
va_list ap;
int r;
va_start(ap, v);
r = sd_json_buildv(&w, ap);
va_end(ap);
if (r < 0)
return r;
return sd_json_variant_merge_object(v, w);
}
_public_ int sd_json_variant_append_array(sd_json_variant **v, sd_json_variant *element) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *nv = NULL;
bool blank;
int r;
assert_return(v, -EINVAL);
assert_return(element, -EINVAL);
if (!*v || sd_json_variant_is_null(*v))
blank = true;
else if (sd_json_variant_is_array(*v))
blank = sd_json_variant_elements(*v) == 0;
else
return -EINVAL;
if (blank) {
r = sd_json_variant_new_array(&nv, (sd_json_variant*[]) { element }, 1);
if (r < 0)
return r;
} else if (json_variant_n_ref(*v) == 1) {
/* Let's bump the reference count on element. We can't do the realloc if we're appending *v
* to itself, or one of the objects embedded in *v to *v. If the reference count grows, we
* need to fall back to the other method below. */
_unused_ _cleanup_(sd_json_variant_unrefp) sd_json_variant *dummy = sd_json_variant_ref(element);
if (json_variant_n_ref(*v) == 1) {
/* We hold the only reference. Let's mutate the object. */
size_t size = sd_json_variant_elements(*v);
void *old = *v;
if (!GREEDY_REALLOC(*v, size + 1 + 1))
return -ENOMEM;
if (old != *v)
/* Readjust the parent pointers to the new address */
for (size_t i = 0; i < size; i++)
(*v)[1 + i].parent = *v;
return json_variant_array_put_element(*v, element);
}
}
if (!blank) {
size_t size = sd_json_variant_elements(*v);
_cleanup_free_ sd_json_variant **array = new(sd_json_variant*, size + 1);
if (!array)
return -ENOMEM;
for (size_t i = 0; i < size; i++)
array[i] = sd_json_variant_by_index(*v, i);
array[size] = element;
r = sd_json_variant_new_array(&nv, array, size + 1);
if (r < 0)
return r;
}
json_variant_propagate_sensitive(*v, nv);
return json_variant_unref_and_replace(*v, nv);
}
_public_ int sd_json_variant_append_arrayb(sd_json_variant **v, ...) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
va_list ap;
int r;
va_start(ap, v);
r = sd_json_buildv(&w, ap);
va_end(ap);
if (r < 0)
return r;
return sd_json_variant_append_array(v, w);
}
_public_ sd_json_variant *sd_json_variant_find(sd_json_variant *haystack, sd_json_variant *needle) {
sd_json_variant *i;
/* Find a json object in an array. Returns NULL if not found, or if the array is not actually an array. */
JSON_VARIANT_ARRAY_FOREACH(i, haystack)
if (sd_json_variant_equal(i, needle))
return i;
return NULL;
}
_public_ int sd_json_variant_append_array_nodup(sd_json_variant **v, sd_json_variant *element) {
assert_return(v, -EINVAL);
if (sd_json_variant_find(*v, element))
return 0;
return sd_json_variant_append_array(v, element);
}
_public_ int sd_json_variant_strv(sd_json_variant *v, char ***ret) {
char **l = NULL;
bool sensitive;
int r;
assert_return(ret, -EINVAL);
if (!v || sd_json_variant_is_null(v)) {
l = new0(char*, 1);
if (!l)
return -ENOMEM;
*ret = l;
return 0;
}
if (!sd_json_variant_is_array(v))
return -EINVAL;
sensitive = sd_json_variant_is_sensitive(v);
size_t n = sd_json_variant_elements(v);
l = new(char*, n+1);
if (!l)
return -ENOMEM;
for (size_t i = 0; i < n; i++) {
sd_json_variant *e;
assert_se(e = sd_json_variant_by_index(v, i));
sensitive = sensitive || sd_json_variant_is_sensitive(e);
if (!sd_json_variant_is_string(e)) {
l[i] = NULL;
r = -EINVAL;
goto fail;
}
l[i] = strdup(sd_json_variant_string(e));
if (!l[i]) {
r = -ENOMEM;
goto fail;
}
}
l[n] = NULL;
*ret = TAKE_PTR(l);
return 0;
fail:
if (sensitive)
strv_free_erase(l);
else
strv_free(l);
return r;
}
static int json_variant_copy(sd_json_variant **nv, sd_json_variant *v) {
sd_json_variant_type_t t;
sd_json_variant *c;
JsonValue value;
const void *source;
size_t k;
assert(nv);
assert(v);
/* Let's copy the simple types literally, and the larger types by references */
t = sd_json_variant_type(v);
switch (t) {
case SD_JSON_VARIANT_INTEGER:
k = sizeof(int64_t);
value.integer = sd_json_variant_integer(v);
source = &value;
break;
case SD_JSON_VARIANT_UNSIGNED:
k = sizeof(uint64_t);
value.unsig = sd_json_variant_unsigned(v);
source = &value;
break;
case SD_JSON_VARIANT_REAL:
k = sizeof(double);
value.real = sd_json_variant_real(v);
source = &value;
break;
case SD_JSON_VARIANT_BOOLEAN:
k = sizeof(bool);
value.boolean = sd_json_variant_boolean(v);
source = &value;
break;
case SD_JSON_VARIANT_NULL:
k = 0;
source = NULL;
break;
case SD_JSON_VARIANT_STRING:
source = sd_json_variant_string(v);
k = strnlen(source, INLINE_STRING_MAX + 1);
if (k <= INLINE_STRING_MAX) {
k++;
break;
}
_fallthrough_;
default:
/* Everything else copy by reference */
c = malloc0(MAX(sizeof(sd_json_variant),
offsetof(sd_json_variant, reference) + sizeof(sd_json_variant*)));
if (!c)
return -ENOMEM;
c->n_ref = 1;
c->type = t;
c->is_reference = true;
c->reference = sd_json_variant_ref(json_variant_formalize(v));
*nv = c;
return 0;
}
c = malloc0(MAX(sizeof(sd_json_variant),
offsetof(sd_json_variant, value) + k));
if (!c)
return -ENOMEM;
c->n_ref = 1;
c->type = t;
memcpy_safe(&c->value, source, k);
json_variant_propagate_sensitive(v, c);
*nv = c;
return 0;
}
static bool json_single_ref(sd_json_variant *v) {
/* Checks whether the caller is the single owner of the object, i.e. can get away with changing it */
if (!json_variant_is_regular(v))
return false;
if (v->is_embedded)
return json_single_ref(v->parent);
assert(v->n_ref > 0);
return v->n_ref == 1;
}
static int json_variant_set_source(sd_json_variant **v, JsonSource *source, unsigned line, unsigned column) {
sd_json_variant *w;
int r;
assert(v);
/* Patch in source and line/column number. Tries to do this in-place if the caller is the sole
* referencer of the object. If not, allocates a new object, possibly a surrogate for the original
* one */
if (!*v)
return 0;
if (source && line > source->max_line)
source->max_line = line;
if (source && column > source->max_column)
source->max_column = column;
if (!json_variant_is_regular(*v)) {
if (!source && line == 0 && column == 0)
return 0;
} else {
if (json_source_equal((*v)->source, source) &&
(*v)->line == line &&
(*v)->column == column)
return 0;
if (json_single_ref(*v)) { /* Sole reference? */
json_source_unref((*v)->source);
(*v)->source = json_source_ref(source);
(*v)->line = line;
(*v)->column = column;
return 1;
}
}
r = json_variant_copy(&w, *v);
if (r < 0)
return r;
assert(json_variant_is_regular(w));
assert(!w->is_embedded);
assert(w->n_ref == 1);
assert(!w->source);
w->source = json_source_ref(source);
w->line = line;
w->column = column;
json_variant_unref_and_replace(*v, w);
return 1;
}
static void inc_lines_columns(unsigned *line, unsigned *column, const char *s, size_t n) {
assert(line);
assert(column);
assert(s || n == 0);
while (n > 0) {
if (*s == '\n') {
(*line)++;
*column = 1;
} else if ((signed char) *s >= 0 && *s < 127) /* Process ASCII chars quickly */
(*column)++;
else {
int w;
w = utf8_encoded_valid_unichar(s, n);
if (w < 0) /* count invalid unichars as normal characters */
w = 1;
else if ((size_t) w > n) /* never read more than the specified number of characters */
w = (int) n;
(*column)++;
s += w;
n -= w;
continue;
}
s++;
n--;
}
}
static int unhex_ucs2(const char *c, uint16_t *ret) {
int aa, bb, cc, dd;
uint16_t x;
assert(c);
assert(ret);
aa = unhexchar(c[0]);
if (aa < 0)
return -EINVAL;
bb = unhexchar(c[1]);
if (bb < 0)
return -EINVAL;
cc = unhexchar(c[2]);
if (cc < 0)
return -EINVAL;
dd = unhexchar(c[3]);
if (dd < 0)
return -EINVAL;
x = ((uint16_t) aa << 12) |
((uint16_t) bb << 8) |
((uint16_t) cc << 4) |
((uint16_t) dd);
if (x <= 0)
return -EINVAL;
*ret = x;
return 0;
}
static int json_parse_string(const char **p, char **ret) {
_cleanup_free_ char *s = NULL;
size_t n = 0;
const char *c;
assert(p);
assert(*p);
assert(ret);
c = *p;
if (*c != '"')
return -EINVAL;
c++;
for (;;) {
int len;
/* Check for EOF */
if (*c == 0)
return -EINVAL;
/* Check for control characters 0x00..0x1f */
if (*c > 0 && *c < ' ')
return -EINVAL;
/* Check for control character 0x7f */
if (*c == 0x7f)
return -EINVAL;
if (*c == '"') {
if (!s) {
s = strdup("");
if (!s)
return -ENOMEM;
} else
s[n] = 0;
*p = c + 1;
*ret = TAKE_PTR(s);
return JSON_TOKEN_STRING;
}
if (*c == '\\') {
char ch = 0;
c++;
if (*c == 0)
return -EINVAL;
if (IN_SET(*c, '"', '\\', '/'))
ch = *c;
else if (*c == 'b')
ch = '\b';
else if (*c == 'f')
ch = '\f';
else if (*c == 'n')
ch = '\n';
else if (*c == 'r')
ch = '\r';
else if (*c == 't')
ch = '\t';
else if (*c == 'u') {
char16_t x;
int r;
r = unhex_ucs2(c + 1, &x);
if (r < 0)
return r;
c += 5;
if (!GREEDY_REALLOC(s, n + 5))
return -ENOMEM;
if (!utf16_is_surrogate(x))
n += utf8_encode_unichar(s + n, (char32_t) x);
else if (utf16_is_trailing_surrogate(x))
return -EINVAL;
else {
char16_t y;
if (c[0] != '\\' || c[1] != 'u')
return -EINVAL;
r = unhex_ucs2(c + 2, &y);
if (r < 0)
return r;
c += 6;
if (!utf16_is_trailing_surrogate(y))
return -EINVAL;
n += utf8_encode_unichar(s + n, utf16_surrogate_pair_to_unichar(x, y));
}
continue;
} else
return -EINVAL;
if (!GREEDY_REALLOC(s, n + 2))
return -ENOMEM;
s[n++] = ch;
c++;
continue;
}
len = utf8_encoded_valid_unichar(c, SIZE_MAX);
if (len < 0)
return len;
if (!GREEDY_REALLOC(s, n + len + 1))
return -ENOMEM;
memcpy(s + n, c, len);
n += len;
c += len;
}
}
static int json_parse_number(const char **p, JsonValue *ret) {
bool negative = false, exponent_negative = false, is_real = false;
double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
int64_t i = 0;
uint64_t u = 0;
const char *c;
assert(p);
assert(*p);
assert(ret);
c = *p;
if (*c == '-') {
negative = true;
c++;
}
if (*c == '0')
c++;
else {
if (!strchr("123456789", *c) || *c == 0)
return -EINVAL;
do {
if (!is_real) {
if (negative) {
if (i < INT64_MIN / 10) /* overflow */
is_real = true;
else {
int64_t t = 10 * i;
if (t < INT64_MIN + (*c - '0')) /* overflow */
is_real = true;
else
i = t - (*c - '0');
}
} else {
if (u > UINT64_MAX / 10) /* overflow */
is_real = true;
else {
uint64_t t = 10 * u;
if (t > UINT64_MAX - (*c - '0')) /* overflow */
is_real = true;
else
u = t + (*c - '0');
}
}
}
x = 10.0 * x + (*c - '0');
c++;
} while (strchr(DIGITS, *c) && *c != 0);
}
if (*c == '.') {
is_real = true;
c++;
if (!strchr(DIGITS, *c) || *c == 0)
return -EINVAL;
do {
y = 10.0 * y + (*c - '0');
shift = 10.0 * shift;
c++;
} while (strchr(DIGITS, *c) && *c != 0);
}
if (IN_SET(*c, 'e', 'E')) {
is_real = true;
c++;
if (*c == '-') {
exponent_negative = true;
c++;
} else if (*c == '+')
c++;
if (!strchr(DIGITS, *c) || *c == 0)
return -EINVAL;
do {
exponent = 10.0 * exponent + (*c - '0');
c++;
} while (strchr(DIGITS, *c) && *c != 0);
}
*p = c;
if (is_real) {
/* Clamp before casting to int — a JSON input with an absurdly large exponent could
* otherwise trigger undefined behaviour in the double→int conversion. xexp10i()
* itself saturates anything beyond ~10^308, so clamping at INT_MAX is harmless. */
int e = exponent > (double) INT_MAX ? INT_MAX : (int) exponent;
ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * xexp10i(exponent_negative ? -e : e);
return JSON_TOKEN_REAL;
} else if (negative) {
ret->integer = i;
return JSON_TOKEN_INTEGER;
} else {
ret->unsig = u;
return JSON_TOKEN_UNSIGNED;
}
}
int json_tokenize(
const char **p,
char **ret_string,
JsonValue *ret_value,
unsigned *ret_line, /* 'reterr_line' returns the line at the beginning of this token */
unsigned *ret_column,
void **state,
unsigned *line, /* 'line' is used as a line state, it always reflect the line we are at after the token was read */
unsigned *column) {
unsigned start_line, start_column;
const char *start, *c;
size_t n;
int t, r;
enum {
STATE_NULL,
STATE_VALUE,
STATE_VALUE_POST,
};
assert(p);
assert(*p);
assert(ret_string);
assert(ret_value);
assert(ret_line);
assert(ret_column);
assert(line);
assert(column);
assert(state);
t = PTR_TO_INT(*state);
if (t == STATE_NULL) {
*line = 1;
*column = 1;
t = STATE_VALUE;
}
/* Skip over the whitespace */
n = strspn(*p, WHITESPACE);
inc_lines_columns(line, column, *p, n);
c = *p + n;
/* Remember where we started processing this token */
start = c;
start_line = *line;
start_column = *column;
if (*c == 0) {
*ret_string = NULL;
*ret_value = JSON_VALUE_NULL;
r = JSON_TOKEN_END;
goto finish;
}
switch (t) {
case STATE_VALUE:
if (*c == '{') {
c++;
*state = INT_TO_PTR(STATE_VALUE);
r = JSON_TOKEN_OBJECT_OPEN;
goto null_return;
} else if (*c == '}') {
c++;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_OBJECT_CLOSE;
goto null_return;
} else if (*c == '[') {
c++;
*state = INT_TO_PTR(STATE_VALUE);
r = JSON_TOKEN_ARRAY_OPEN;
goto null_return;
} else if (*c == ']') {
c++;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_ARRAY_CLOSE;
goto null_return;
} else if (*c == '"') {
r = json_parse_string(&c, ret_string);
if (r < 0)
return r;
*ret_value = JSON_VALUE_NULL;
*state = INT_TO_PTR(STATE_VALUE_POST);
goto finish;
} else if (strchr("-" DIGITS, *c)) {
r = json_parse_number(&c, ret_value);
if (r < 0)
return r;
*ret_string = NULL;
*state = INT_TO_PTR(STATE_VALUE_POST);
goto finish;
} else if (startswith(c, "true")) {
*ret_string = NULL;
ret_value->boolean = true;
c += 4;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_BOOLEAN;
goto finish;
} else if (startswith(c, "false")) {
*ret_string = NULL;
ret_value->boolean = false;
c += 5;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_BOOLEAN;
goto finish;
} else if (startswith(c, "null")) {
*ret_string = NULL;
*ret_value = JSON_VALUE_NULL;
c += 4;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_NULL;
goto finish;
}
return -EINVAL;
case STATE_VALUE_POST:
if (*c == ':') {
c++;
*state = INT_TO_PTR(STATE_VALUE);
r = JSON_TOKEN_COLON;
goto null_return;
} else if (*c == ',') {
c++;
*state = INT_TO_PTR(STATE_VALUE);
r = JSON_TOKEN_COMMA;
goto null_return;
} else if (*c == '}') {
c++;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_OBJECT_CLOSE;
goto null_return;
} else if (*c == ']') {
c++;
*state = INT_TO_PTR(STATE_VALUE_POST);
r = JSON_TOKEN_ARRAY_CLOSE;
goto null_return;
}
return -EINVAL;
default:
assert_not_reached();
}
null_return:
*ret_string = NULL;
*ret_value = JSON_VALUE_NULL;
finish:
inc_lines_columns(line, column, start, c - start);
*p = c;
*ret_line = start_line;
*ret_column = start_column;
return r;
}
typedef enum JsonExpect {
/* The following values are used by sd_json_parse() */
EXPECT_TOPLEVEL,