Amit Pundir | aab5ced | 2020-04-16 10:02:40 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018-2019, Linaro Ltd. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software without |
| 17 | * specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | #include <sys/stat.h> |
| 32 | #include <ctype.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | #include "json.h" |
| 41 | |
| 42 | static const char *input_buf; |
| 43 | static int input_pos; |
| 44 | static int input_len; |
| 45 | |
| 46 | static int json_parse_array(struct json_value *array); |
| 47 | static int json_parse_object(struct json_value *object); |
| 48 | static int json_parse_property(struct json_value *value); |
| 49 | |
| 50 | static int input(void) |
| 51 | { |
| 52 | if (input_pos >= input_len) |
| 53 | return 0; |
| 54 | |
| 55 | return input_buf[input_pos++]; |
| 56 | } |
| 57 | |
| 58 | static void unput(void) |
| 59 | { |
| 60 | input_pos--; |
| 61 | } |
| 62 | |
| 63 | static void json_skip_whitespace(void) |
| 64 | { |
| 65 | int ch; |
| 66 | |
| 67 | while ((ch = input()) && isspace(ch)) |
| 68 | ; |
| 69 | unput(); |
| 70 | } |
| 71 | |
| 72 | static int json_parse_string(struct json_value *value) |
| 73 | { |
| 74 | char buf[128]; |
| 75 | char *b = buf; |
| 76 | int ch; |
| 77 | |
| 78 | ch = input(); |
| 79 | if (ch != '"') { |
| 80 | unput(); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | while ((ch = input()) && ch != '"' && b - buf < sizeof(buf) - 1) |
| 85 | *b++ = ch; |
| 86 | *b = '\0'; |
| 87 | |
| 88 | if (!ch) |
| 89 | return -1; |
| 90 | |
| 91 | value->type = JSON_TYPE_STRING; |
| 92 | value->u.string = strdup(buf); |
| 93 | |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static int json_parse_number(struct json_value *value) |
| 98 | { |
| 99 | char buf[20]; |
| 100 | char *b = buf; |
| 101 | int ch; |
| 102 | |
| 103 | while ((ch = input()) && isdigit(ch) && b - buf < sizeof(buf) - 1) |
| 104 | *b++ = ch; |
| 105 | *b = '\0'; |
| 106 | unput(); |
| 107 | |
| 108 | if (b == buf) |
| 109 | return 0; |
| 110 | |
| 111 | value->type = JSON_TYPE_NUMBER; |
| 112 | value->u.number = strtod(buf, NULL); |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | static int json_parse_keyword(struct json_value *value) |
| 118 | { |
| 119 | const char *match; |
| 120 | const char *m; |
| 121 | int ch; |
| 122 | |
| 123 | ch = input(); |
| 124 | switch (ch) { |
| 125 | case 't': |
| 126 | match = "true"; |
| 127 | value->type = JSON_TYPE_TRUE; |
| 128 | break; |
| 129 | case 'f': |
| 130 | match = "false"; |
| 131 | value->type = JSON_TYPE_FALSE; |
| 132 | break; |
| 133 | case 'n': |
| 134 | match = "null"; |
| 135 | value->type = JSON_TYPE_NULL; |
| 136 | break; |
| 137 | default: |
| 138 | unput(); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | m = match; |
| 143 | while (*m && *m++ == ch) |
| 144 | ch = input(); |
| 145 | unput(); |
| 146 | |
| 147 | return *m == '\0' ? 1 : -1; |
| 148 | } |
| 149 | |
| 150 | static int json_parse_value(struct json_value *value) |
| 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | json_skip_whitespace(); |
| 155 | |
| 156 | ret = json_parse_object(value); |
| 157 | if (ret) |
| 158 | goto out; |
| 159 | |
| 160 | ret = json_parse_array(value); |
| 161 | if (ret) |
| 162 | goto out; |
| 163 | |
| 164 | ret = json_parse_string(value); |
| 165 | if (ret) |
| 166 | goto out; |
| 167 | |
| 168 | ret = json_parse_number(value); |
| 169 | if (ret) |
| 170 | goto out; |
| 171 | |
| 172 | ret = json_parse_keyword(value); |
| 173 | if (ret) |
| 174 | goto out; |
| 175 | |
| 176 | fprintf(stderr, "unable to match a value\n"); |
| 177 | return -1; |
| 178 | |
| 179 | out: |
| 180 | json_skip_whitespace(); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int json_parse_array(struct json_value *array) |
| 185 | { |
| 186 | struct json_value *value; |
| 187 | struct json_value *last = NULL; |
| 188 | int ret; |
| 189 | int ch; |
| 190 | |
| 191 | ch = input(); |
| 192 | if (ch != '[') { |
| 193 | unput(); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | array->type = JSON_TYPE_ARRAY; |
| 198 | do { |
| 199 | value = calloc(1, sizeof(*value)); |
| 200 | if (!value) |
| 201 | return -1; |
| 202 | |
| 203 | ret = json_parse_value(value); |
| 204 | if (ret <= 0) { |
| 205 | free(value); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | if (!array->u.value) |
| 210 | array->u.value = value; |
| 211 | if (last) |
| 212 | last->next = value; |
| 213 | last = value; |
| 214 | |
| 215 | ch = input(); |
| 216 | if (ch == ']') { |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | } while (ch == ','); |
| 221 | |
| 222 | fprintf(stderr, "expected ',' got '%c'\n", ch); |
| 223 | |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | static int json_parse_object(struct json_value *object) |
| 228 | { |
| 229 | struct json_value *value; |
| 230 | struct json_value *last = NULL; |
| 231 | int ret; |
| 232 | int ch; |
| 233 | |
| 234 | ch = input(); |
| 235 | if (ch != '{') { |
| 236 | unput(); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | object->type = JSON_TYPE_OBJECT; |
| 241 | |
| 242 | do { |
| 243 | value = calloc(1, sizeof(*value)); |
| 244 | if (!value) |
| 245 | return -1; |
| 246 | |
| 247 | ret = json_parse_property(value); |
| 248 | if (ret <= 0) { |
| 249 | free(value); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | if (!object->u.value) |
| 254 | object->u.value = value; |
| 255 | if (last) |
| 256 | last->next = value; |
| 257 | last = value; |
| 258 | |
| 259 | ch = input(); |
| 260 | if (ch == '}') { |
| 261 | return 1; |
| 262 | } |
| 263 | } while (ch == ','); |
| 264 | |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | static int json_parse_property(struct json_value *value) |
| 269 | { |
| 270 | struct json_value key; |
| 271 | int ret; |
| 272 | int ch; |
| 273 | |
| 274 | json_skip_whitespace(); |
| 275 | |
| 276 | ret = json_parse_string(&key); |
| 277 | if (ret <= 0) |
| 278 | return -1; |
| 279 | |
| 280 | value->key = key.u.string; |
| 281 | |
| 282 | json_skip_whitespace(); |
| 283 | |
| 284 | ch = input(); |
| 285 | if (ch != ':') |
| 286 | return -1; |
| 287 | |
| 288 | ret = json_parse_value(value); |
| 289 | if (ret <= 0) |
| 290 | return -1; |
| 291 | |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | struct json_value *json_parse(const char *json) |
| 296 | { |
| 297 | struct json_value *root; |
| 298 | int ret; |
| 299 | |
| 300 | input_buf = json; |
| 301 | input_pos = 0; |
| 302 | input_len = strlen(input_buf); |
| 303 | |
| 304 | root = calloc(1, sizeof(*root)); |
| 305 | if (!root) |
| 306 | return NULL; |
| 307 | |
| 308 | ret = json_parse_value(root); |
| 309 | if (ret != 1) { |
| 310 | free(root); |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | return root; |
| 315 | } |
| 316 | |
| 317 | struct json_value *json_parse_file(const char *file) |
| 318 | { |
| 319 | struct json_value *root; |
| 320 | struct stat sb; |
| 321 | int ret; |
| 322 | int fd; |
| 323 | |
| 324 | fd = open(file, O_RDONLY); |
| 325 | if (fd < 0) { |
| 326 | fprintf(stderr, "failed to open %s: %s\n", file, strerror(errno)); |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | ret = fstat(fd, &sb); |
| 331 | if (ret < 0) |
| 332 | return NULL; |
| 333 | |
| 334 | input_pos = 0; |
| 335 | input_len = sb.st_size; |
| 336 | input_buf = malloc(sb.st_size); |
| 337 | |
| 338 | ret = read(fd, (char *)input_buf, input_len); |
| 339 | |
| 340 | close(fd); |
| 341 | |
| 342 | if (ret != input_len) { |
| 343 | fprintf(stderr, "failed to read %d bytes form %s\n", input_len, file); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | root = calloc(1, sizeof(*root)); |
| 348 | if (!root) |
| 349 | return NULL; |
| 350 | |
| 351 | ret = json_parse_value(root); |
| 352 | if (ret != 1) { |
| 353 | json_free(root); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | return root; |
| 358 | } |
| 359 | |
| 360 | struct json_value *json_get_child(struct json_value *object, const char *key) |
| 361 | { |
| 362 | struct json_value *it; |
| 363 | |
| 364 | if(object->type != JSON_TYPE_OBJECT) |
| 365 | return NULL; |
| 366 | |
| 367 | for (it = object->u.value; it; it = it->next) { |
| 368 | if (!strcmp(it->key, key)) |
| 369 | return it; |
| 370 | } |
| 371 | |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | int json_count_children(struct json_value *array) |
| 376 | { |
| 377 | struct json_value *it; |
| 378 | int count = 0; |
| 379 | |
| 380 | if (!array || array->type != JSON_TYPE_ARRAY) |
| 381 | return -1; |
| 382 | |
| 383 | for (it = array->u.value; it; it = it->next) |
| 384 | count++; |
| 385 | |
| 386 | return count; |
| 387 | } |
| 388 | |
| 389 | int json_get_number(struct json_value *object, const char *key, double *number) |
| 390 | { |
| 391 | struct json_value *it; |
| 392 | |
| 393 | if (!object || object->type != JSON_TYPE_OBJECT) |
| 394 | return -1; |
| 395 | |
| 396 | for (it = object->u.value; it; it = it->next) { |
| 397 | if (!strcmp(it->key, key)) { |
| 398 | if (it->type != JSON_TYPE_NUMBER) |
| 399 | return -1; |
| 400 | |
| 401 | *number = it->u.number; |
| 402 | return 0; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | const char *json_get_string(struct json_value *object, const char *key) |
| 410 | { |
| 411 | struct json_value *it; |
| 412 | |
| 413 | if (!object || object->type != JSON_TYPE_OBJECT) |
| 414 | return NULL; |
| 415 | |
| 416 | for (it = object->u.value; it; it = it->next) { |
| 417 | if (!strcmp(it->key, key)) { |
| 418 | if (it->type != JSON_TYPE_STRING) |
| 419 | return NULL; |
| 420 | |
| 421 | return it->u.string; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | void json_free(struct json_value *value) |
| 429 | { |
| 430 | struct json_value *next; |
| 431 | struct json_value *it; |
| 432 | |
| 433 | free((char *)value->key); |
| 434 | |
| 435 | switch (value->type) { |
| 436 | case JSON_TYPE_OBJECT: |
| 437 | case JSON_TYPE_ARRAY: |
| 438 | it = value->u.value; |
| 439 | while (it) { |
| 440 | next = it->next; |
| 441 | json_free(it); |
| 442 | it = next; |
| 443 | } |
| 444 | break; |
| 445 | case JSON_TYPE_STRING: |
| 446 | free((char *)value->u.string); |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | free(value); |
| 451 | } |