json.c revision fecb719ec1e1abc665f91d55adaa4951db5c1bed
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <math.h>
#include "macro.h"
#include "utf8.h"
#include "json.h"
JsonVariant *v;
if (!v)
return -ENOMEM;
*ret = v;
return 0;
}
return -ENOMEM;
return -ENOMEM;
int r;
if (r < 0)
return r;
}
}
else
return 0;
}
if (!variant)
return NULL;
return json_object_unref(variant);
return NULL;
}
if (!variant)
return NULL;
return NULL;
}
return NULL;
return NULL;
}
size_t i = 0;
JsonVariant *p = NULL;
if (!variant)
return NULL;
if (p->type == JSON_VARIANT_STRING)
free(p);
}
return NULL;
}
if (!variant)
return NULL;
return NULL;
}
}
}
}
}
}
}
return NULL;
}
const char *p = s;
if (!line)
return;
for (;;) {
const char *f;
f = memchr(p, '\n', n);
if (!f)
return;
n -= (f - p) + 1;
p = f + 1;
(*line)++;
}
}
uint16_t x;
assert(c);
if (aa < 0)
return -EINVAL;
if (bb < 0)
return -EINVAL;
if (cc < 0)
return -EINVAL;
if (dd < 0)
return -EINVAL;
if (x <= 0)
return -EINVAL;
*ret = x;
return 0;
}
static int json_parse_string(const char **p, char **ret) {
_cleanup_free_ char *s = NULL;
const char *c;
assert(p);
assert(*p);
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 = s;
s = NULL;
return JSON_STRING;
}
if (*c == '\\') {
char ch = 0;
c++;
if (*c == 0)
return -EINVAL;
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') {
uint16_t x;
int r;
r = unhex_ucs2(c + 1, &x);
if (r < 0)
return r;
c += 5;
return -ENOMEM;
if (!utf16_is_surrogate(x))
n += utf8_encode_unichar(s + n, x);
else if (utf16_is_trailing_surrogate(x))
return -EINVAL;
else {
uint16_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;
return -ENOMEM;
s[n++] = ch;
c ++;
continue;
}
if (len < 0)
return len;
return -ENOMEM;
n += len;
c += len;
}
}
intmax_t i = 0;
const char *c;
assert(p);
assert(*p);
c = *p;
if (*c == '-') {
negative = true;
c++;
}
if (*c == '0')
c++;
else {
if (!strchr("123456789", *c) || *c == 0)
return -EINVAL;
do {
if (!is_double) {
int64_t t;
t = 10 * i + (*c - '0');
if (t < i) /* overflow */
is_double = false;
else
i = t;
}
x = 10.0 * x + (*c - '0');
c++;
} while (strchr("0123456789", *c) && *c != 0);
}
if (*c == '.') {
is_double = true;
c++;
if (!strchr("0123456789", *c) || *c == 0)
return -EINVAL;
do {
y = 10.0 * y + (*c - '0');
c++;
} while (strchr("0123456789", *c) && *c != 0);
}
if (*c == 'e' || *c == 'E') {
is_double = true;
c++;
if (*c == '-') {
exponent_negative = true;
c++;
} else if (*c == '+')
c++;
if (!strchr("0123456789", *c) || *c == 0)
return -EINVAL;
do {
c++;
} while (strchr("0123456789", *c) && *c != 0);
}
*p = c;
if (is_double) {
ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10((exponent_negative ? -1.0 : 1.0) * exponent);
return JSON_REAL;
} else {
return JSON_INTEGER;
}
}
int json_tokenize(
const char **p,
char **ret_string,
union json_value *ret_value,
void **state,
unsigned *line) {
const char *c;
int t;
int r;
enum {
};
assert(p);
assert(*p);
t = PTR_TO_INT(*state);
c = *p;
if (t == STATE_NULL) {
if (line)
*line = 1;
t = STATE_VALUE;
}
for (;;) {
const char *b;
b = c + strspn(c, WHITESPACE);
if (*b == 0)
return JSON_END;
c = b;
switch (t) {
case STATE_VALUE:
if (*c == '{') {
*ret_string = NULL;
*p = c + 1;
return JSON_OBJECT_OPEN;
} else if (*c == '}') {
*ret_string = NULL;
*p = c + 1;
return JSON_OBJECT_CLOSE;
} else if (*c == '[') {
*ret_string = NULL;
*p = c + 1;
return JSON_ARRAY_OPEN;
} else if (*c == ']') {
*ret_string = NULL;
*p = c + 1;
return JSON_ARRAY_CLOSE;
} else if (*c == '"') {
r = json_parse_string(&c, ret_string);
if (r < 0)
return r;
*p = c;
return r;
} else if (strchr("-0123456789", *c)) {
r = json_parse_number(&c, ret_value);
if (r < 0)
return r;
*ret_string = NULL;
*p = c;
return r;
} else if (startswith(c, "true")) {
*ret_string = NULL;
*p = c + 4;
return JSON_BOOLEAN;
} else if (startswith(c, "false")) {
*ret_string = NULL;
*p = c + 5;
return JSON_BOOLEAN;
} else if (startswith(c, "null")) {
*ret_string = NULL;
*p = c + 4;
return JSON_NULL;
} else
return -EINVAL;
case STATE_VALUE_POST:
if (*c == ':') {
*ret_string = NULL;
*p = c + 1;
return JSON_COLON;
} else if (*c == ',') {
*ret_string = NULL;
*p = c + 1;
return JSON_COMMA;
} else if (*c == '}') {
*ret_string = NULL;
*p = c + 1;
return JSON_OBJECT_CLOSE;
} else if (*c == ']') {
*ret_string = NULL;
*p = c + 1;
return JSON_ARRAY_CLOSE;
} else
return -EINVAL;
}
}
}
}
enum {
assert(i);
int r;
if (stopper) {
goto error;
goto out;
}
goto error;
else {
state = STATE_COLON;
}
}
else if (state == STATE_COLON) {
goto error;
if (json_is_value(var))
goto error;
goto error;
state = STATE_VALUE;
}
else if (state == STATE_VALUE) {
if (!json_is_value(var)) {
r = json_variant_new(&v, type);
if (r < 0)
goto error;
r = json_scoped_parse(tokens, i, n, v);
if (r < 0)
goto error;
value = v;
}
else
goto error;
if (arr) {
if (r < 0)
goto error;
} else {
if (r < 0)
goto error;
if (r < 0)
goto error;
}
state = STATE_COMMA;
}
else if (state == STATE_COMMA) {
if (json_is_value(var))
goto error;
goto error;
}
}
return -EBADMSG;
out:
}
int r;
JsonVariant *e;
r = json_variant_new(&p, JSON_VARIANT_OBJECT);
if (r < 0)
return r;
return -EBADMSG;
if (r < 0)
return r;
*rv = p;
p = NULL;
return 0;
}
union json_value v = {};
void *json_state = NULL;
const char *p;
int t, r;
assert(n);
if (size <= 0)
return -EBADMSG;
if (!buf)
return -ENOMEM;
p = buf;
for (;;) {
if (t < 0)
return t;
else if (t == JSON_END)
break;
if (t <= JSON_ARRAY_CLOSE) {
if (r < 0)
return r;
} else {
switch (t) {
case JSON_STRING:
if (r < 0)
return r;
return -ENOMEM;
}
break;
case JSON_INTEGER:
if (r < 0)
return r;
break;
case JSON_REAL:
if (r < 0)
return r;
break;
case JSON_BOOLEAN:
if (r < 0)
return r;
break;
case JSON_NULL:
if (r < 0)
return r;
break;
}
}
return -ENOMEM;
}
*n = s;
return 0;
}
JsonVariant *v = NULL;
size_t n = 0;
int r;
if (r < 0)
return r;
r = json_parse_tokens(s, n, &v);
if (r < 0)
return r;
*rv = v;
return 0;
}