Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file json.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic JSON format parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <ctype.h> |
| 17 | #include <errno.h> |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 19 | #include <string.h> |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include "common.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 23 | #include "in_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 24 | #include "json.h" |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 25 | |
| 26 | #define JSON_PUSH_STATUS_RET(CTX, STATUS) \ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 27 | LY_CHECK_RET(ly_set_add(&CTX->status, (void*)STATUS, 1, NULL)) |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 28 | |
| 29 | #define JSON_POP_STATUS_RET(CTX) \ |
| 30 | assert(CTX->status.count); CTX->status.count--; |
| 31 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 32 | const char * |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 33 | lyjson_token2str(enum LYJSON_PARSER_STATUS status) |
| 34 | { |
| 35 | switch (status) { |
| 36 | case LYJSON_ERROR: |
| 37 | return "error"; |
| 38 | case LYJSON_ROOT: |
| 39 | return "document root"; |
| 40 | case LYJSON_FALSE: |
| 41 | return "false"; |
| 42 | case LYJSON_TRUE: |
| 43 | return "true"; |
| 44 | case LYJSON_NULL: |
| 45 | return "null"; |
| 46 | case LYJSON_OBJECT: |
| 47 | return "object"; |
| 48 | case LYJSON_OBJECT_CLOSED: |
| 49 | return "object closed"; |
| 50 | case LYJSON_OBJECT_EMPTY: |
| 51 | return "empty object"; |
| 52 | case LYJSON_ARRAY: |
| 53 | return "array"; |
| 54 | case LYJSON_ARRAY_CLOSED: |
| 55 | return "array closed"; |
| 56 | case LYJSON_ARRAY_EMPTY: |
| 57 | return "empty array"; |
| 58 | case LYJSON_NUMBER: |
| 59 | return "number"; |
| 60 | case LYJSON_STRING: |
| 61 | return "string"; |
| 62 | case LYJSON_END: |
| 63 | return "end of input"; |
| 64 | } |
| 65 | |
| 66 | return ""; |
| 67 | } |
| 68 | |
| 69 | static LY_ERR |
| 70 | skip_ws(struct lyjson_ctx *jsonctx) |
| 71 | { |
| 72 | /* skip leading whitespaces */ |
| 73 | while (*jsonctx->in->current != '\0' && is_jsonws(*jsonctx->in->current)) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 74 | ly_in_skip(jsonctx->in, 1); |
| 75 | } |
| 76 | if (*jsonctx->in->current == '\0') { |
| 77 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_END); |
| 78 | } |
| 79 | |
| 80 | return LY_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * @brief Set value corresponding to the current context's status |
| 85 | */ |
| 86 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 87 | lyjson_ctx_set_value(struct lyjson_ctx *jsonctx, const char *value, size_t value_len, ly_bool dynamic) |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 88 | { |
| 89 | assert(jsonctx); |
| 90 | |
| 91 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 92 | free((char *)jsonctx->value); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 93 | } |
| 94 | jsonctx->value = value; |
| 95 | jsonctx->value_len = value_len; |
| 96 | jsonctx->dynamic = dynamic; |
| 97 | } |
| 98 | |
| 99 | static LY_ERR |
| 100 | lyjson_check_next(struct lyjson_ctx *jsonctx) |
| 101 | { |
| 102 | if (jsonctx->status.count == 1) { |
| 103 | /* top level value (JSON-text), ws expected */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 104 | if ((*jsonctx->in->current == '\0') || is_jsonws(*jsonctx->in->current)) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 105 | return LY_SUCCESS; |
| 106 | } |
| 107 | } else if (lyjson_ctx_status(jsonctx, 1) == LYJSON_OBJECT) { |
| 108 | LY_CHECK_RET(skip_ws(jsonctx)); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 109 | if ((*jsonctx->in->current == ',') || (*jsonctx->in->current == '}')) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 110 | return LY_SUCCESS; |
| 111 | } |
| 112 | } else if (lyjson_ctx_status(jsonctx, 1) == LYJSON_ARRAY) { |
| 113 | LY_CHECK_RET(skip_ws(jsonctx)); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 114 | if ((*jsonctx->in->current == ',') || (*jsonctx->in->current == ']')) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 115 | return LY_SUCCESS; |
| 116 | } |
| 117 | } else { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 118 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 119 | "Unexpected character \"%c\" after JSON %s.", *jsonctx->in->current, lyjson_token2str(lyjson_ctx_status(jsonctx, 0))); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return LY_EVALID; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Input is expected to start after the opening quotation-mark. |
| 127 | * When succeeds, input is moved after the closing quotation-mark. |
| 128 | */ |
| 129 | static LY_ERR |
| 130 | lyjson_string_(struct lyjson_ctx *jsonctx) |
| 131 | { |
| 132 | #define BUFSIZE 24 |
| 133 | #define BUFSIZE_STEP 128 |
| 134 | |
| 135 | const char *in = jsonctx->in->current, *start; |
| 136 | char *buf = NULL; |
| 137 | size_t offset; /* read offset in input buffer */ |
| 138 | size_t len; /* length of the output string (write offset in output buffer) */ |
| 139 | size_t size = 0; /* size of the output buffer */ |
| 140 | size_t u; |
| 141 | uint64_t start_line; |
| 142 | |
| 143 | assert(jsonctx); |
| 144 | |
| 145 | /* init */ |
| 146 | start = in; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 147 | start_line = jsonctx->in->line; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 148 | offset = len = 0; |
| 149 | |
| 150 | /* parse */ |
| 151 | while (in[offset]) { |
| 152 | if (in[offset] == '\\') { |
| 153 | /* escape sequence */ |
| 154 | size_t slash = offset; |
| 155 | uint32_t value; |
| 156 | uint8_t i = 1; |
| 157 | |
| 158 | if (!buf) { |
| 159 | /* prepare output buffer */ |
| 160 | buf = malloc(BUFSIZE); |
| 161 | LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM); |
| 162 | size = BUFSIZE; |
| 163 | } |
| 164 | |
| 165 | /* allocate enough for the offset and next character, |
| 166 | * we will need 4 bytes at most since we support only the predefined |
| 167 | * (one-char) entities and character references */ |
| 168 | if (len + offset + 4 >= size) { |
| 169 | buf = ly_realloc(buf, size + BUFSIZE_STEP); |
| 170 | LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM); |
| 171 | size += BUFSIZE_STEP; |
| 172 | } |
| 173 | |
| 174 | if (offset) { |
| 175 | /* store what we have so far */ |
| 176 | memcpy(&buf[len], in, offset); |
| 177 | len += offset; |
| 178 | in += offset; |
| 179 | offset = 0; |
| 180 | } |
| 181 | |
| 182 | switch (in[++offset]) { |
| 183 | case '"': |
| 184 | /* quotation mark */ |
| 185 | value = 0x22; |
| 186 | break; |
| 187 | case '\\': |
| 188 | /* reverse solidus */ |
| 189 | value = 0x5c; |
| 190 | break; |
| 191 | case '/': |
| 192 | /* solidus */ |
| 193 | value = 0x2f; |
| 194 | break; |
| 195 | case 'b': |
| 196 | /* backspace */ |
| 197 | value = 0x08; |
| 198 | break; |
| 199 | case 'f': |
| 200 | /* form feed */ |
| 201 | value = 0x0c; |
| 202 | break; |
| 203 | case 'n': |
| 204 | /* line feed */ |
| 205 | value = 0x0a; |
| 206 | break; |
| 207 | case 'r': |
| 208 | /* carriage return */ |
| 209 | value = 0x0d; |
| 210 | break; |
| 211 | case 't': |
| 212 | /* tab */ |
| 213 | value = 0x09; |
| 214 | break; |
| 215 | case 'u': |
| 216 | /* Basic Multilingual Plane character \uXXXX */ |
| 217 | offset++; |
| 218 | for (value = i = 0; i < 4; i++) { |
Juraj Vijtiuk | 2b94e4b | 2020-11-16 23:52:07 +0100 | [diff] [blame] | 219 | if (!in[offset + i]) { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 220 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, |
Radek IÅ¡a | 14b357e | 2020-11-26 17:55:11 +0100 | [diff] [blame] | 221 | "Invalid basic multilingual plane character \"%s\".", &in[slash]); |
Juraj Vijtiuk | 2b94e4b | 2020-11-16 23:52:07 +0100 | [diff] [blame] | 222 | goto error; |
| 223 | } else if (isdigit(in[offset + i])) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 224 | u = (in[offset + i] - '0'); |
| 225 | } else if (in[offset + i] > 'F') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 226 | u = LY_BASE_DEC + (in[offset + i] - 'a'); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 227 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 228 | u = LY_BASE_DEC + (in[offset + i] - 'A'); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 229 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 230 | value = (LY_BASE_HEX * value) + u; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 231 | } |
| 232 | break; |
| 233 | default: |
| 234 | /* invalid escape sequence */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 235 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 236 | "Invalid character escape sequence \\%c.", in[offset]); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 237 | goto error; |
| 238 | |
| 239 | } |
| 240 | |
| 241 | offset += i; /* add read escaped characters */ |
| 242 | LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], value, &u), |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 243 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 244 | "Invalid character reference \"%.*s\" (0x%08x).", offset - slash, &in[slash], value), |
| 245 | error); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 246 | len += u; /* update number of bytes in buffer */ |
| 247 | in += offset; /* move the input by the processed bytes stored in the buffer ... */ |
| 248 | offset = 0; /* ... and reset the offset index for future moving data into buffer */ |
| 249 | |
| 250 | } else if (in[offset] == '"') { |
| 251 | /* end of string */ |
| 252 | if (buf) { |
| 253 | /* realloc exact size string */ |
| 254 | buf = ly_realloc(buf, len + offset + 1); |
| 255 | LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM); |
| 256 | size = len + offset + 1; |
| 257 | memcpy(&buf[len], in, offset); |
| 258 | |
| 259 | /* set terminating NULL byte */ |
| 260 | buf[len + offset] = '\0'; |
| 261 | } |
| 262 | len += offset; |
| 263 | ++offset; |
| 264 | in += offset; |
| 265 | goto success; |
| 266 | } else { |
| 267 | /* get it as UTF-8 character for check */ |
| 268 | const char *c = &in[offset]; |
| 269 | uint32_t code = 0; |
| 270 | size_t code_len = 0; |
| 271 | |
| 272 | LY_CHECK_ERR_GOTO(ly_getutf8(&c, &code, &code_len), |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 273 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_INCHAR, in[offset]), error); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 274 | |
| 275 | LY_CHECK_ERR_GOTO(!is_jsonstrchar(code), |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 276 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 277 | "Invalid character in JSON string \"%.*s\" (0x%08x).", &in[offset] - start + code_len, start, code), |
| 278 | error); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 279 | |
| 280 | /* character is ok, continue */ |
| 281 | offset += code_len; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* EOF reached before endchar */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 286 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_EOF); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 287 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &start_line, LYVE_SYNTAX, "Missing quotation-mark at the end of a JSON string."); |
| 288 | |
| 289 | error: |
| 290 | free(buf); |
| 291 | return LY_EVALID; |
| 292 | |
| 293 | success: |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 294 | jsonctx->in->current = in; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 295 | if (buf) { |
| 296 | lyjson_ctx_set_value(jsonctx, buf, len, 1); |
| 297 | } else { |
| 298 | lyjson_ctx_set_value(jsonctx, start, len, 0); |
| 299 | } |
| 300 | |
| 301 | return LY_SUCCESS; |
| 302 | |
| 303 | #undef BUFSIZE |
| 304 | #undef BUFSIZE_STEP |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * |
| 309 | * Wrapper around lyjson_string_() adding LYJSON_STRING status into context to allow using lyjson_string_() for parsing object's name. |
| 310 | */ |
| 311 | static LY_ERR |
| 312 | lyjson_string(struct lyjson_ctx *jsonctx) |
| 313 | { |
| 314 | LY_CHECK_RET(lyjson_string_(jsonctx)); |
| 315 | |
| 316 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_STRING); |
| 317 | LY_CHECK_RET(lyjson_check_next(jsonctx)); |
| 318 | |
| 319 | return LY_SUCCESS; |
| 320 | } |
| 321 | |
| 322 | static LY_ERR |
| 323 | lyjson_number(struct lyjson_ctx *jsonctx) |
| 324 | { |
| 325 | size_t offset = 0, exponent = 0; |
| 326 | const char *in = jsonctx->in->current; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 327 | uint8_t minus = 0; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 328 | |
| 329 | if (in[offset] == '-') { |
| 330 | ++offset; |
| 331 | minus = 1; |
| 332 | } |
| 333 | |
| 334 | if (in[offset] == '0') { |
| 335 | ++offset; |
| 336 | } else if (isdigit(in[offset])) { |
| 337 | ++offset; |
| 338 | while (isdigit(in[offset])) { |
| 339 | ++offset; |
| 340 | } |
| 341 | } else { |
| 342 | invalid_character: |
| 343 | if (in[offset]) { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 344 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, "Invalid character in JSON Number value (\"%c\").", in[offset]); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 345 | } else { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 346 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_EOF); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 347 | } |
| 348 | return LY_EVALID; |
| 349 | } |
| 350 | |
| 351 | if (in[offset] == '.') { |
| 352 | ++offset; |
| 353 | if (!isdigit(in[offset])) { |
| 354 | goto invalid_character; |
| 355 | } |
| 356 | while (isdigit(in[offset])) { |
| 357 | ++offset; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if ((in[offset] == 'e') || (in[offset] == 'E')) { |
| 362 | exponent = offset++; |
| 363 | if ((in[offset] == '+') || (in[offset] == '-')) { |
| 364 | ++offset; |
| 365 | } |
| 366 | if (!isdigit(in[offset])) { |
| 367 | goto invalid_character; |
| 368 | } |
| 369 | while (isdigit(in[offset])) { |
| 370 | ++offset; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (exponent) { |
| 375 | /* convert JSON number with exponent into the representation used by YANG */ |
| 376 | long int e_val; |
| 377 | char *ptr, *dec_point, *num; |
| 378 | const char *e_ptr = &in[exponent + 1]; |
| 379 | size_t num_len, i; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 380 | int64_t dp_position; /* final position of the deciaml point */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 381 | |
| 382 | errno = 0; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 383 | e_val = strtol(e_ptr, &ptr, LY_BASE_DEC); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 384 | if (errno) { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 385 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SEMANTICS, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 386 | "Exponent out-of-bounds in a JSON Number value (%.*s).", offset - minus - (e_ptr - in), e_ptr); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 387 | return LY_EVALID; |
| 388 | } |
| 389 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 390 | dec_point = ly_strnchr(in, '.', exponent); |
| 391 | if (!dec_point) { |
| 392 | /* value is integer, we are just ... */ |
| 393 | if (e_val >= 0) { |
| 394 | /* adding zeros at the end */ |
| 395 | num_len = exponent + e_val; |
| 396 | dp_position = num_len; /* decimal point is behind the actual value */ |
| 397 | } else if ((size_t)labs(e_val) < exponent) { |
| 398 | /* adding decimal point between the integer's digits */ |
| 399 | num_len = exponent + 1; |
| 400 | dp_position = exponent + e_val; |
| 401 | } else { |
| 402 | /* adding decimal point before the integer with adding leading zero(s) */ |
| 403 | num_len = labs(e_val) + 2; |
| 404 | dp_position = exponent + e_val; |
| 405 | } |
| 406 | dp_position -= minus; |
| 407 | } else { |
| 408 | /* value is decimal, we are moving the decimal point */ |
| 409 | dp_position = dec_point - in + e_val - minus; |
| 410 | if (dp_position > (ssize_t)exponent) { |
| 411 | /* moving decimal point after the decimal value make the integer result */ |
| 412 | num_len = dp_position; |
| 413 | } else if (dp_position < 0) { |
| 414 | /* moving decimal point before the decimal value requires additional zero(s) |
| 415 | * (decimal point is already count in exponent value) */ |
| 416 | num_len = exponent + labs(dp_position) + 1; |
| 417 | } else { |
| 418 | /* moving decimal point just inside the decimal value does not make any change in length */ |
| 419 | num_len = exponent; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* allocate buffer for the result (add terminating NULL-byte */ |
| 424 | num = malloc(num_len + 1); |
| 425 | LY_CHECK_ERR_RET(!num, LOGMEM(jsonctx->ctx), LY_EMEM); |
| 426 | |
| 427 | /* compose the resulting vlaue */ |
| 428 | i = 0; |
| 429 | if (minus) { |
| 430 | num[i++] = '-'; |
| 431 | } |
| 432 | /* add leading zeros */ |
| 433 | if (dp_position <= 0) { |
| 434 | num[i++] = '0'; |
| 435 | num[i++] = '.'; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 436 | for ( ; dp_position; dp_position++) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 437 | num[i++] = '0'; |
| 438 | } |
| 439 | } |
| 440 | /* copy the value */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 441 | ly_bool dp_placed; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 442 | size_t j; |
| 443 | for (dp_placed = dp_position ? 0 : 1, j = minus; j < exponent; j++) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 444 | if (in[j] == '.') { |
| 445 | continue; |
| 446 | } |
| 447 | if (!dp_placed) { |
| 448 | if (!dp_position) { |
| 449 | num[i++] = '.'; |
| 450 | dp_placed = 1; |
| 451 | } else { |
| 452 | dp_position--; |
| 453 | if (in[j] == '0') { |
| 454 | num_len--; |
| 455 | continue; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | num[i++] = in[j]; |
| 461 | } |
| 462 | /* trailing zeros */ |
| 463 | while (dp_position--) { |
| 464 | num[i++] = '0'; |
| 465 | } |
| 466 | /* terminating NULL byte */ |
| 467 | num[i] = '\0'; |
| 468 | |
| 469 | /* store the modified number */ |
| 470 | lyjson_ctx_set_value(jsonctx, num, num_len, 1); |
| 471 | } else { |
| 472 | /* store the number */ |
| 473 | lyjson_ctx_set_value(jsonctx, jsonctx->in->current, offset, 0); |
| 474 | } |
| 475 | ly_in_skip(jsonctx->in, offset); |
| 476 | |
| 477 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_NUMBER); |
| 478 | LY_CHECK_RET(lyjson_check_next(jsonctx)); |
| 479 | |
| 480 | return LY_SUCCESS; |
| 481 | } |
| 482 | |
| 483 | static LY_ERR |
| 484 | lyjson_object_name(struct lyjson_ctx *jsonctx) |
| 485 | { |
| 486 | if (*jsonctx->in->current != '"') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 487 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 488 | jsonctx->in->current, "a JSON object's member"); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 489 | return LY_EVALID; |
| 490 | } |
| 491 | ly_in_skip(jsonctx->in, 1); |
| 492 | |
| 493 | LY_CHECK_RET(lyjson_string_(jsonctx)); |
| 494 | LY_CHECK_RET(skip_ws(jsonctx)); |
Michal Vasko | 08dc70b | 2020-10-07 13:58:47 +0200 | [diff] [blame] | 495 | if (*jsonctx->in->current != ':') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 496 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_INSTREXP, |
Michal Vasko | 08dc70b | 2020-10-07 13:58:47 +0200 | [diff] [blame] | 497 | LY_VCODE_INSTREXP_len(jsonctx->in->current), jsonctx->in->current, "a JSON object's name-separator ':'"); |
| 498 | return LY_EVALID; |
| 499 | } |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 500 | ly_in_skip(jsonctx->in, 1); |
| 501 | LY_CHECK_RET(skip_ws(jsonctx)); |
| 502 | |
| 503 | return LY_SUCCESS; |
| 504 | } |
| 505 | |
| 506 | static LY_ERR |
| 507 | lyjson_object(struct lyjson_ctx *jsonctx) |
| 508 | { |
| 509 | LY_CHECK_RET(skip_ws(jsonctx)); |
| 510 | |
| 511 | if (*jsonctx->in->current == '}') { |
| 512 | /* empty object */ |
| 513 | ly_in_skip(jsonctx->in, 1); |
| 514 | lyjson_ctx_set_value(jsonctx, NULL, 0, 0); |
| 515 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_OBJECT_EMPTY); |
| 516 | return LY_SUCCESS; |
| 517 | } |
| 518 | |
| 519 | LY_CHECK_RET(lyjson_object_name(jsonctx)); |
| 520 | |
| 521 | /* output data are set by lyjson_string_() */ |
| 522 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_OBJECT); |
| 523 | |
| 524 | return LY_SUCCESS; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * @brief Process JSON array envelope |
| 529 | * |
| 530 | * |
| 531 | * |
| 532 | * @param[in] jsonctx JSON parser context |
| 533 | * @return LY_SUCCESS or LY_EMEM |
| 534 | */ |
| 535 | static LY_ERR |
| 536 | lyjson_array(struct lyjson_ctx *jsonctx) |
| 537 | { |
| 538 | LY_CHECK_RET(skip_ws(jsonctx)); |
| 539 | |
| 540 | if (*jsonctx->in->current == ']') { |
| 541 | /* empty array */ |
| 542 | ly_in_skip(jsonctx->in, 1); |
| 543 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_ARRAY_EMPTY); |
| 544 | } else { |
| 545 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_ARRAY); |
| 546 | } |
| 547 | |
| 548 | /* erase previous values, array has no value on its own */ |
| 549 | lyjson_ctx_set_value(jsonctx, NULL, 0, 0); |
| 550 | |
| 551 | return LY_SUCCESS; |
| 552 | } |
| 553 | |
| 554 | static LY_ERR |
| 555 | lyjson_value(struct lyjson_ctx *jsonctx) |
| 556 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 557 | if (jsonctx->status.count && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 558 | return LY_SUCCESS; |
| 559 | } |
| 560 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 561 | if ((*jsonctx->in->current == 'f') && !strncmp(jsonctx->in->current, "false", ly_strlen_const("false"))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 562 | /* false */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 563 | lyjson_ctx_set_value(jsonctx, jsonctx->in->current, ly_strlen_const("false"), 0); |
| 564 | ly_in_skip(jsonctx->in, ly_strlen_const("false")); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 565 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_FALSE); |
| 566 | LY_CHECK_RET(lyjson_check_next(jsonctx)); |
| 567 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 568 | } else if ((*jsonctx->in->current == 't') && !strncmp(jsonctx->in->current, "true", ly_strlen_const("true"))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 569 | /* true */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 570 | lyjson_ctx_set_value(jsonctx, jsonctx->in->current, ly_strlen_const("true"), 0); |
| 571 | ly_in_skip(jsonctx->in, ly_strlen_const("true")); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 572 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_TRUE); |
| 573 | LY_CHECK_RET(lyjson_check_next(jsonctx)); |
| 574 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 575 | } else if ((*jsonctx->in->current == 'n') && !strncmp(jsonctx->in->current, "null", ly_strlen_const("null"))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 576 | /* none */ |
Radek Krejci | 201963a | 2020-12-03 11:43:40 +0100 | [diff] [blame] | 577 | lyjson_ctx_set_value(jsonctx, "", 0, 0); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 578 | ly_in_skip(jsonctx->in, ly_strlen_const("null")); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 579 | JSON_PUSH_STATUS_RET(jsonctx, LYJSON_NULL); |
| 580 | LY_CHECK_RET(lyjson_check_next(jsonctx)); |
| 581 | |
| 582 | } else if (*jsonctx->in->current == '"') { |
| 583 | /* string */ |
| 584 | ly_in_skip(jsonctx->in, 1); |
| 585 | LY_CHECK_RET(lyjson_string(jsonctx)); |
| 586 | |
| 587 | } else if (*jsonctx->in->current == '[') { |
| 588 | /* array */ |
| 589 | ly_in_skip(jsonctx->in, 1); |
| 590 | LY_CHECK_RET(lyjson_array(jsonctx)); |
| 591 | |
| 592 | } else if (*jsonctx->in->current == '{') { |
| 593 | /* object */ |
| 594 | ly_in_skip(jsonctx->in, 1); |
| 595 | LY_CHECK_RET(lyjson_object(jsonctx)); |
| 596 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 597 | } else if ((*jsonctx->in->current == '-') || ((*jsonctx->in->current >= '0') && (*jsonctx->in->current <= '9'))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 598 | /* number */ |
| 599 | LY_CHECK_RET(lyjson_number(jsonctx)); |
| 600 | |
| 601 | } else { |
| 602 | /* unexpected value */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 603 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 604 | jsonctx->in->current, "a JSON value"); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 605 | return LY_EVALID; |
| 606 | } |
| 607 | |
| 608 | return LY_SUCCESS; |
| 609 | } |
| 610 | |
| 611 | LY_ERR |
| 612 | lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **jsonctx_p) |
| 613 | { |
| 614 | LY_ERR ret = LY_SUCCESS; |
| 615 | struct lyjson_ctx *jsonctx; |
| 616 | |
| 617 | assert(ctx); |
| 618 | assert(in); |
| 619 | assert(jsonctx_p); |
| 620 | |
| 621 | /* new context */ |
| 622 | jsonctx = calloc(1, sizeof *jsonctx); |
| 623 | LY_CHECK_ERR_RET(!jsonctx, LOGMEM(ctx), LY_EMEM); |
| 624 | jsonctx->ctx = ctx; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 625 | jsonctx->in = in; |
| 626 | |
| 627 | /* parse JSON value, if any */ |
| 628 | LY_CHECK_GOTO(ret = skip_ws(jsonctx), cleanup); |
| 629 | if (lyjson_ctx_status(jsonctx, 0) == LYJSON_END) { |
| 630 | /* empty data input */ |
| 631 | goto cleanup; |
| 632 | } |
| 633 | |
| 634 | ret = lyjson_value(jsonctx); |
| 635 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 636 | if ((jsonctx->status.count > 1) && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 637 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_EOF); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 638 | ret = LY_EVALID; |
| 639 | } |
| 640 | |
| 641 | cleanup: |
| 642 | if (ret) { |
| 643 | lyjson_ctx_free(jsonctx); |
| 644 | } else { |
| 645 | *jsonctx_p = jsonctx; |
| 646 | } |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | void |
| 651 | lyjson_ctx_backup(struct lyjson_ctx *jsonctx) |
| 652 | { |
| 653 | if (jsonctx->backup.dynamic) { |
| 654 | free((char *)jsonctx->backup.value); |
| 655 | } |
| 656 | jsonctx->backup.status = lyjson_ctx_status(jsonctx, 0); |
| 657 | jsonctx->backup.status_count = jsonctx->status.count; |
| 658 | jsonctx->backup.value = jsonctx->value; |
| 659 | jsonctx->backup.value_len = jsonctx->value_len; |
| 660 | jsonctx->backup.input = jsonctx->in->current; |
| 661 | jsonctx->backup.dynamic = jsonctx->dynamic; |
| 662 | jsonctx->dynamic = 0; |
| 663 | } |
| 664 | |
| 665 | void |
| 666 | lyjson_ctx_restore(struct lyjson_ctx *jsonctx) |
| 667 | { |
| 668 | if (jsonctx->dynamic) { |
| 669 | free((char *)jsonctx->value); |
| 670 | } |
| 671 | jsonctx->status.count = jsonctx->backup.status_count; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 672 | jsonctx->status.objs[jsonctx->backup.status_count - 1] = (void *)jsonctx->backup.status; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 673 | jsonctx->value = jsonctx->backup.value; |
| 674 | jsonctx->value_len = jsonctx->backup.value_len; |
| 675 | jsonctx->in->current = jsonctx->backup.input; |
| 676 | jsonctx->dynamic = jsonctx->backup.dynamic; |
| 677 | jsonctx->backup.dynamic = 0; |
| 678 | } |
| 679 | |
| 680 | LY_ERR |
| 681 | lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status) |
| 682 | { |
| 683 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 684 | ly_bool toplevel = 0; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 685 | enum LYJSON_PARSER_STATUS prev; |
| 686 | |
| 687 | assert(jsonctx); |
| 688 | |
| 689 | prev = lyjson_ctx_status(jsonctx, 0); |
| 690 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 691 | if ((prev == LYJSON_OBJECT) || (prev == LYJSON_ARRAY)) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 692 | /* get value for the object's member OR the first value in the array */ |
| 693 | ret = lyjson_value(jsonctx); |
| 694 | goto result; |
| 695 | } else { |
| 696 | /* the previous token is closed and should be completely processed */ |
| 697 | JSON_POP_STATUS_RET(jsonctx); |
| 698 | prev = lyjson_ctx_status(jsonctx, 0); |
| 699 | } |
| 700 | |
| 701 | if (!jsonctx->status.count) { |
| 702 | /* we are done with the top level value */ |
| 703 | toplevel = 1; |
| 704 | } |
| 705 | LY_CHECK_RET(skip_ws(jsonctx)); |
| 706 | if (toplevel && !jsonctx->status.count) { |
| 707 | /* EOF expected, but there are some data after the top level token */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 708 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LYVE_SYNTAX, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 709 | "Expecting end-of-input, but some data follows the top level JSON value."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 710 | return LY_EVALID; |
| 711 | } |
| 712 | |
| 713 | if (toplevel) { |
| 714 | /* we are done */ |
| 715 | return LY_SUCCESS; |
| 716 | } |
| 717 | |
| 718 | /* continue with the next token */ |
| 719 | assert(prev == LYJSON_OBJECT || prev == LYJSON_ARRAY); |
| 720 | |
| 721 | if (*jsonctx->in->current == ',') { |
| 722 | /* sibling item in the ... */ |
| 723 | ly_in_skip(jsonctx->in, 1); |
| 724 | LY_CHECK_RET(skip_ws(jsonctx)); |
| 725 | |
| 726 | if (prev == LYJSON_OBJECT) { |
| 727 | /* ... object - get another object's member */ |
| 728 | ret = lyjson_object_name(jsonctx); |
| 729 | } else { /* LYJSON_ARRAY */ |
| 730 | /* ... array - get another complete value */ |
| 731 | ret = lyjson_value(jsonctx); |
| 732 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 733 | } else if (((prev == LYJSON_OBJECT) && (*jsonctx->in->current == '}')) || ((prev == LYJSON_ARRAY) && (*jsonctx->in->current == ']'))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 734 | ly_in_skip(jsonctx->in, 1); |
| 735 | JSON_POP_STATUS_RET(jsonctx); |
| 736 | JSON_PUSH_STATUS_RET(jsonctx, prev + 1); |
| 737 | } else { |
| 738 | /* unexpected value */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 739 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 740 | jsonctx->in->current, prev == LYJSON_ARRAY ? "another JSON value in array" : "another JSON object's member"); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 741 | return LY_EVALID; |
| 742 | } |
| 743 | |
| 744 | result: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 745 | if ((ret == LY_SUCCESS) && (jsonctx->status.count > 1) && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 746 | LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->in->line, LY_VCODE_EOF); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 747 | ret = LY_EVALID; |
| 748 | } |
| 749 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 750 | if ((ret == LY_SUCCESS) && status) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 751 | *status = lyjson_ctx_status(jsonctx, 0); |
| 752 | } |
| 753 | |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | enum LYJSON_PARSER_STATUS |
| 758 | lyjson_ctx_status(struct lyjson_ctx *jsonctx, uint32_t index) |
| 759 | { |
| 760 | assert(jsonctx); |
| 761 | |
| 762 | if (jsonctx->status.count < index) { |
| 763 | return LYJSON_ERROR; |
| 764 | } else if (jsonctx->status.count == index) { |
| 765 | return LYJSON_ROOT; |
| 766 | } else { |
Michal Vasko | 2791572 | 2020-08-31 14:54:42 +0200 | [diff] [blame] | 767 | return (enum LYJSON_PARSER_STATUS)(uintptr_t)jsonctx->status.objs[jsonctx->status.count - (index + 1)]; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | void |
| 772 | lyjson_ctx_free(struct lyjson_ctx *jsonctx) |
| 773 | { |
| 774 | if (!jsonctx) { |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | if (jsonctx->dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 779 | free((char *)jsonctx->value); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 780 | } |
| 781 | if (jsonctx->backup.dynamic) { |
| 782 | free((char *)jsonctx->backup.value); |
| 783 | } |
| 784 | |
| 785 | ly_set_erase(&jsonctx->status, NULL); |
| 786 | |
| 787 | free(jsonctx); |
| 788 | } |