1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
36 static void inc_lines(unsigned *line, const char *s, size_t n) {
45 f = memchr(p, '\n', n);
55 static int unhex_ucs2(const char *c, uint16_t *ret) {
78 x = ((uint16_t) aa << 12) |
79 ((uint16_t) bb << 8) |
80 ((uint16_t) cc << 4) |
91 static int json_parse_string(const char **p, char **ret) {
92 _cleanup_free_ char *s = NULL;
93 size_t n = 0, allocated = 0;
114 /* Check for control characters 0x00..0x1f */
115 if (*c > 0 && *c < ' ')
118 /* Check for control character 0x7f */
144 if (IN_SET(*c, '"', '\\', '/'))
156 else if (*c == 'u') {
160 r = unhex_ucs2(c + 1, &x);
166 if (!GREEDY_REALLOC(s, allocated, n + 4))
169 if (!utf16_is_surrogate(x))
170 n += utf8_encode_unichar(s + n, x);
171 else if (utf16_is_trailing_surrogate(x))
176 if (c[0] != '\\' || c[1] != 'u')
179 r = unhex_ucs2(c + 2, &y);
185 if (!utf16_is_trailing_surrogate(y))
188 n += utf8_encode_unichar(s + n, utf16_surrogate_pair_to_unichar(x, y));
195 if (!GREEDY_REALLOC(s, allocated, n + 2))
203 len = utf8_encoded_valid_unichar(c);
207 if (!GREEDY_REALLOC(s, allocated, n + len + 1))
210 memcpy(s + n, c, len);
216 static int json_parse_number(const char **p, union json_value *ret) {
217 bool negative = false, exponent_negative = false, is_double = false;
218 double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
236 if (!strchr("123456789", *c) || *c == 0)
243 t = 10 * i + (*c - '0');
244 if (t < i) /* overflow */
250 x = 10.0 * x + (*c - '0');
252 } while (strchr("0123456789", *c) && *c != 0);
259 if (!strchr("0123456789", *c) || *c == 0)
263 y = 10.0 * y + (*c - '0');
264 shift = 10.0 * shift;
266 } while (strchr("0123456789", *c) && *c != 0);
269 if (*c == 'e' || *c == 'E') {
274 exponent_negative = true;
276 } else if (*c == '+')
279 if (!strchr("0123456789", *c) || *c == 0)
283 exponent = 10.0 * exponent + (*c - '0');
285 } while (strchr("0123456789", *c) && *c != 0);
294 ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10((exponent_negative ? -1.0 : 1.0) * exponent);
297 ret->integer = negative ? -i : i;
305 union json_value *ret_value,
319 t = PTR_TO_INT(*state);
322 if (t == STATE_NULL) {
331 b = c + strspn(c, WHITESPACE);
335 inc_lines(line, c, b - c);
344 *ret_value = JSON_VALUE_NULL;
346 *state = INT_TO_PTR(STATE_VALUE);
347 return JSON_OBJECT_OPEN;
349 } else if (*c == '}') {
351 *ret_value = JSON_VALUE_NULL;
353 *state = INT_TO_PTR(STATE_VALUE_POST);
354 return JSON_OBJECT_CLOSE;
356 } else if (*c == '[') {
358 *ret_value = JSON_VALUE_NULL;
360 *state = INT_TO_PTR(STATE_VALUE);
361 return JSON_ARRAY_OPEN;
363 } else if (*c == ']') {
365 *ret_value = JSON_VALUE_NULL;
367 *state = INT_TO_PTR(STATE_VALUE_POST);
368 return JSON_ARRAY_CLOSE;
370 } else if (*c == '"') {
371 r = json_parse_string(&c, ret_string);
375 *ret_value = JSON_VALUE_NULL;
377 *state = INT_TO_PTR(STATE_VALUE_POST);
380 } else if (strchr("-0123456789", *c)) {
381 r = json_parse_number(&c, ret_value);
387 *state = INT_TO_PTR(STATE_VALUE_POST);
390 } else if (startswith(c, "true")) {
392 ret_value->boolean = true;
394 *state = INT_TO_PTR(STATE_VALUE_POST);
397 } else if (startswith(c, "false")) {
399 ret_value->boolean = false;
401 *state = INT_TO_PTR(STATE_VALUE_POST);
404 } else if (startswith(c, "null")) {
406 *ret_value = JSON_VALUE_NULL;
408 *state = INT_TO_PTR(STATE_VALUE_POST);
414 case STATE_VALUE_POST:
418 *ret_value = JSON_VALUE_NULL;
420 *state = INT_TO_PTR(STATE_VALUE);
422 } else if (*c == ',') {
424 *ret_value = JSON_VALUE_NULL;
426 *state = INT_TO_PTR(STATE_VALUE);
428 } else if (*c == '}') {
430 *ret_value = JSON_VALUE_NULL;
432 *state = INT_TO_PTR(STATE_VALUE_POST);
433 return JSON_OBJECT_CLOSE;
434 } else if (*c == ']') {
436 *ret_value = JSON_VALUE_NULL;
438 *state = INT_TO_PTR(STATE_VALUE_POST);
439 return JSON_ARRAY_CLOSE;