Debian Codegraph @

internal/warpc/deps/parson/parson.c

raw
1 /*
2 SPDX-License-Identifier: MIT
3
4 Parson 1.5.3 (https://github.com/kgabis/parson)
5 Copyright (c) 2012 - 2023 Krzysztof Gabis
6 Commit: ba29f4eda9ea7703a9f6a9cf2b0532a2605723c3
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 */
26 #ifdef _MSC_VER
27 #ifndef _CRT_SECURE_NO_WARNINGS
28 #define _CRT_SECURE_NO_WARNINGS
29 #endif /* _CRT_SECURE_NO_WARNINGS */
30 #endif /* _MSC_VER */
31
32 #include "parson.h"
33
34 #define PARSON_IMPL_VERSION_MAJOR 1
35 #define PARSON_IMPL_VERSION_MINOR 5
36 #define PARSON_IMPL_VERSION_PATCH 3
37
38 #if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\
39 || (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\
40 || (PARSON_VERSION_PATCH != PARSON_IMPL_VERSION_PATCH)
41 #error "parson version mismatch between parson.c and parson.h"
42 #endif
43
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <math.h>
50 #include <errno.h>
51
52 /* Apparently sscanf is not implemented in some "standard" libraries, so don't use it, if you
53 * don't have to. */
54 #ifdef sscanf
55 #undef sscanf
56 #define sscanf THINK_TWICE_ABOUT_USING_SSCANF
57 #endif
58
59 /* strcpy is unsafe */
60 #ifdef strcpy
61 #undef strcpy
62 #endif
63 #define strcpy USE_MEMCPY_INSTEAD_OF_STRCPY
64
65 #define STARTING_CAPACITY 16
66 #define MAX_NESTING 2048
67
68 #ifndef PARSON_DEFAULT_FLOAT_FORMAT
69 #define PARSON_DEFAULT_FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */
70 #endif
71
72 #ifndef PARSON_NUM_BUF_SIZE
73 #define PARSON_NUM_BUF_SIZE 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so let's be paranoid and use 64 */
74 #endif
75
76 #ifndef PARSON_INDENT_STR
77 #define PARSON_INDENT_STR " "
78 #endif
79
80 #define SIZEOF_TOKEN(a) (sizeof(a) - 1)
81 #define SKIP_CHAR(str) ((*str)++)
82 #define SKIP_WHITESPACES(str) while (isspace((unsigned char)(**str))) { SKIP_CHAR(str); }
83 #define MAX(a, b) ((a) > (b) ? (a) : (b))
84
85 #undef malloc
86 #undef free
87
88 #if defined(isnan) && defined(isinf)
89 #define IS_NUMBER_INVALID(x) (isnan((x)) || isinf((x)))
90 #else
91 #define IS_NUMBER_INVALID(x) (((x) * 0.0) != 0.0)
92 #endif
93
94 #define OBJECT_INVALID_IX ((size_t)-1)
95
96 static JSON_Malloc_Function parson_malloc = malloc;
97 static JSON_Free_Function parson_free = free;
98
99 static int parson_escape_slashes = 1;
100
101 static char *parson_float_format = NULL;
102
103 static JSON_Number_Serialization_Function parson_number_serialization_function = NULL;
104
105 #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */
106
107 typedef int parson_bool_t;
108
109 #define PARSON_TRUE 1
110 #define PARSON_FALSE 0
111
112 typedef struct json_string {
113 char *chars;
114 size_t length;
115 } JSON_String;
116
117 /* Type definitions */
118 typedef union json_value_value {
119 JSON_String string;
120 double number;
121 JSON_Object *object;
122 JSON_Array *array;
123 int boolean;
124 int null;
125 } JSON_Value_Value;
126
127 struct json_value_t {
128 JSON_Value *parent;
129 JSON_Value_Type type;
130 JSON_Value_Value value;
131 };
132
133 struct json_object_t {
134 JSON_Value *wrapping_value;
135 size_t *cells;
136 unsigned long *hashes;
137 char **names;
138 JSON_Value **values;
139 size_t *cell_ixs;
140 size_t count;
141 size_t item_capacity;
142 size_t cell_capacity;
143 };
144
145 struct json_array_t {
146 JSON_Value *wrapping_value;
147 JSON_Value **items;
148 size_t count;
149 size_t capacity;
150 };
151
152 /* Various */
153 static char * read_file(const char *filename);
154 static void remove_comments(char *string, const char *start_token, const char *end_token);
155 static char * parson_strndup(const char *string, size_t n);
156 static char * parson_strdup(const char *string);
157 static int parson_sprintf(char * s, const char * format, ...);
158
159 static int hex_char_to_int(char c);
160 static JSON_Status parse_utf16_hex(const char *string, unsigned int *result);
161 static int num_bytes_in_utf8_sequence(unsigned char c);
162 static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len);
163 static parson_bool_t is_valid_utf8(const char *string, size_t string_len);
164 static parson_bool_t is_decimal(const char *string, size_t length);
165 static unsigned long hash_string(const char *string, size_t n);
166
167 /* JSON Object */
168 static JSON_Object * json_object_make(JSON_Value *wrapping_value);
169 static JSON_Status json_object_init(JSON_Object *object, size_t capacity);
170 static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys, parson_bool_t free_values);
171 static JSON_Status json_object_grow_and_rehash(JSON_Object *object);
172 static size_t json_object_get_cell_ix(const JSON_Object *object, const char *key, size_t key_len, unsigned long hash, parson_bool_t *out_found);
173 static JSON_Status json_object_add(JSON_Object *object, char *name, JSON_Value *value);
174 static JSON_Value * json_object_getn_value(const JSON_Object *object, const char *name, size_t name_len);
175 static JSON_Status json_object_remove_internal(JSON_Object *object, const char *name, parson_bool_t free_value);
176 static JSON_Status json_object_dotremove_internal(JSON_Object *object, const char *name, parson_bool_t free_value);
177 static void json_object_free(JSON_Object *object);
178
179 /* JSON Array */
180 static JSON_Array * json_array_make(JSON_Value *wrapping_value);
181 static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value);
182 static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity);
183 static void json_array_free(JSON_Array *array);
184
185 /* JSON Value */
186 static JSON_Value * json_value_init_string_no_copy(char *string, size_t length);
187 static const JSON_String * json_value_get_string_desc(const JSON_Value *value);
188
189 /* Parser */
190 static JSON_Status skip_quotes(const char **string);
191 static JSON_Status parse_utf16(const char **unprocessed, char **processed);
192 static char * process_string(const char *input, size_t input_len, size_t *output_len);
193 static char * get_quoted_string(const char **string, size_t *output_string_len);
194 static JSON_Value * parse_object_value(const char **string, size_t nesting);
195 static JSON_Value * parse_array_value(const char **string, size_t nesting);
196 static JSON_Value * parse_string_value(const char **string);
197 static JSON_Value * parse_boolean_value(const char **string);
198 static JSON_Value * parse_number_value(const char **string);
199 static JSON_Value * parse_null_value(const char **string);
200 static JSON_Value * parse_value(const char **string, size_t nesting);
201
202 /* Serialization */
203 static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, parson_bool_t is_pretty, char *num_buf);
204 static int json_serialize_string(const char *string, size_t len, char *buf);
205
206 /* Various */
207 static char * read_file(const char * filename) {
208 FILE *fp = fopen(filename, "r");
209 size_t size_to_read = 0;
210 size_t size_read = 0;
211 long pos;
212 char *file_contents;
213 if (!fp) {
214 return NULL;
215 }
216 fseek(fp, 0L, SEEK_END);
217 pos = ftell(fp);
218 if (pos < 0) {
219 fclose(fp);
220 return NULL;
221 }
222 size_to_read = pos;
223 rewind(fp);
224 file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1));
225 if (!file_contents) {
226 fclose(fp);
227 return NULL;
228 }
229 size_read = fread(file_contents, 1, size_to_read, fp);
230 if (size_read == 0 || ferror(fp)) {
231 fclose(fp);
232 parson_free(file_contents);
233 return NULL;
234 }
235 fclose(fp);
236 file_contents[size_read] = '\0';
237 return file_contents;
238 }
239
240 static void remove_comments(char *string, const char *start_token, const char *end_token) {
241 parson_bool_t in_string = PARSON_FALSE, escaped = PARSON_FALSE;
242 size_t i;
243 char *ptr = NULL, current_char;
244 size_t start_token_len = strlen(start_token);
245 size_t end_token_len = strlen(end_token);
246 if (start_token_len == 0 || end_token_len == 0) {
247 return;
248 }
249 while ((current_char = *string) != '\0') {
250 if (current_char == '\\' && !escaped) {
251 escaped = PARSON_TRUE;
252 string++;
253 continue;
254 } else if (current_char == '\"' && !escaped) {
255 in_string = !in_string;
256 } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) {
257 for(i = 0; i < start_token_len; i++) {
258 string[i] = ' ';
259 }
260 string = string + start_token_len;
261 ptr = strstr(string, end_token);
262 if (!ptr) {
263 return;
264 }
265 for (i = 0; i < (ptr - string) + end_token_len; i++) {
266 string[i] = ' ';
267 }
268 string = ptr + end_token_len - 1;
269 }
270 escaped = PARSON_FALSE;
271 string++;
272 }
273 }
274
275 static char * parson_strndup(const char *string, size_t n) {
276 /* We expect the caller has validated that 'n' fits within the input buffer. */
277 char *output_string = (char*)parson_malloc(n + 1);
278 if (!output_string) {
279 return NULL;
280 }
281 output_string[n] = '\0';
282 memcpy(output_string, string, n);
283 return output_string;
284 }
285
286 static char * parson_strdup(const char *string) {
287 return parson_strndup(string, strlen(string));
288 }
289
290 static int parson_sprintf(char * s, const char * format, ...) {
291 int result;
292 va_list args;
293 va_start(args, format);
294
295 #if defined(__APPLE__) && defined(__clang__)
296 #pragma clang diagnostic push
297 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
298 #endif
299 result = vsprintf(s, format, args);
300 #if defined(__APPLE__) && defined(__clang__)
301 #pragma clang diagnostic pop
302 #endif
303
304 va_end(args);
305 return result;
306
307 }
308
309 static int hex_char_to_int(char c) {
310 if (c >= '0' && c <= '9') {
311 return c - '0';
312 } else if (c >= 'a' && c <= 'f') {
313 return c - 'a' + 10;
314 } else if (c >= 'A' && c <= 'F') {
315 return c - 'A' + 10;
316 }
317 return -1;
318 }
319
320 static JSON_Status parse_utf16_hex(const char *s, unsigned int *result) {
321 int x1, x2, x3, x4;
322 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') {
323 return JSONFailure;
324 }
325 x1 = hex_char_to_int(s[0]);
326 x2 = hex_char_to_int(s[1]);
327 x3 = hex_char_to_int(s[2]);
328 x4 = hex_char_to_int(s[3]);
329 if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) {
330 return JSONFailure;
331 }
332 *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4);
333 return JSONSuccess;
334 }
335
336 static int num_bytes_in_utf8_sequence(unsigned char c) {
337 if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) {
338 return 0;
339 } else if ((c & 0x80) == 0) { /* 0xxxxxxx */
340 return 1;
341 } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */
342 return 2;
343 } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */
344 return 3;
345 } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */
346 return 4;
347 }
348 return 0; /* won't happen */
349 }
350
351 static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len) {
352 unsigned int cp = 0;
353 *len = num_bytes_in_utf8_sequence(string[0]);
354
355 if (*len == 1) {
356 cp = string[0];
357 } else if (*len == 2 && IS_CONT(string[1])) {
358 cp = string[0] & 0x1F;
359 cp = (cp << 6) | (string[1] & 0x3F);
360 } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) {
361 cp = ((unsigned char)string[0]) & 0xF;
362 cp = (cp << 6) | (string[1] & 0x3F);
363 cp = (cp << 6) | (string[2] & 0x3F);
364 } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) {
365 cp = string[0] & 0x7;
366 cp = (cp << 6) | (string[1] & 0x3F);
367 cp = (cp << 6) | (string[2] & 0x3F);
368 cp = (cp << 6) | (string[3] & 0x3F);
369 } else {
370 return JSONFailure;
371 }
372
373 /* overlong encodings */
374 if ((cp < 0x80 && *len > 1) ||
375 (cp < 0x800 && *len > 2) ||
376 (cp < 0x10000 && *len > 3)) {
377 return JSONFailure;
378 }
379
380 /* invalid unicode */
381 if (cp > 0x10FFFF) {
382 return JSONFailure;
383 }
384
385 /* surrogate halves */
386 if (cp >= 0xD800 && cp <= 0xDFFF) {
387 return JSONFailure;
388 }
389
390 return JSONSuccess;
391 }
392
393 static int is_valid_utf8(const char *string, size_t string_len) {
394 int len = 0;
395 const char *string_end = string + string_len;
396 while (string < string_end) {
397 if (verify_utf8_sequence((const unsigned char*)string, &len) != JSONSuccess) {
398 return PARSON_FALSE;
399 }
400 string += len;
401 }
402 return PARSON_TRUE;
403 }
404
405 static parson_bool_t is_decimal(const char *string, size_t length) {
406 if (length > 1 && string[0] == '0' && string[1] != '.') {
407 return PARSON_FALSE;
408 }
409 if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') {
410 return PARSON_FALSE;
411 }
412 while (length--) {
413 if (strchr("xX", string[length])) {
414 return PARSON_FALSE;
415 }
416 }
417 return PARSON_TRUE;
418 }
419
420 static unsigned long hash_string(const char *string, size_t n) {
421 #ifdef PARSON_FORCE_HASH_COLLISIONS
422 (void)string;
423 (void)n;
424 return 0;
425 #else
426 unsigned long hash = 5381;
427 unsigned char c;
428 size_t i = 0;
429 for (i = 0; i < n; i++) {
430 c = string[i];
431 if (c == '\0') {
432 break;
433 }
434 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
435 }
436 return hash;
437 #endif
438 }
439
440 /* JSON Object */
441 static JSON_Object * json_object_make(JSON_Value *wrapping_value) {
442 JSON_Status res = JSONFailure;
443 JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
444 if (new_obj == NULL) {
445 return NULL;
446 }
447 new_obj->wrapping_value = wrapping_value;
448 res = json_object_init(new_obj, 0);
449 if (res != JSONSuccess) {
450 parson_free(new_obj);
451 return NULL;
452 }
453 return new_obj;
454 }
455
456 static JSON_Status json_object_init(JSON_Object *object, size_t capacity) {
457 unsigned int i = 0;
458
459 object->cells = NULL;
460 object->names = NULL;
461 object->values = NULL;
462 object->cell_ixs = NULL;
463 object->hashes = NULL;
464
465 object->count = 0;
466 object->cell_capacity = capacity;
467 object->item_capacity = (unsigned int)(capacity * 7/10);
468
469 if (capacity == 0) {
470 return JSONSuccess;
471 }
472
473 object->cells = (size_t*)parson_malloc(object->cell_capacity * sizeof(*object->cells));
474 object->names = (char**)parson_malloc(object->item_capacity * sizeof(*object->names));
475 object->values = (JSON_Value**)parson_malloc(object->item_capacity * sizeof(*object->values));
476 object->cell_ixs = (size_t*)parson_malloc(object->item_capacity * sizeof(*object->cell_ixs));
477 object->hashes = (unsigned long*)parson_malloc(object->item_capacity * sizeof(*object->hashes));
478 if (object->cells == NULL
479 || object->names == NULL
480 || object->values == NULL
481 || object->cell_ixs == NULL
482 || object->hashes == NULL) {
483 goto error;
484 }
485 for (i = 0; i < object->cell_capacity; i++) {
486 object->cells[i] = OBJECT_INVALID_IX;
487 }
488 return JSONSuccess;
489 error:
490 parson_free(object->cells);
491 parson_free(object->names);
492 parson_free(object->values);
493 parson_free(object->cell_ixs);
494 parson_free(object->hashes);
495 return JSONFailure;
496 }
497
498 static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys, parson_bool_t free_values) {
499 unsigned int i = 0;
500 for (i = 0; i < object->count; i++) {
501 if (free_keys) {
502 parson_free(object->names[i]);
503 }
504 if (free_values) {
505 json_value_free(object->values[i]);
506 }
507 }
508
509 object->count = 0;
510 object->item_capacity = 0;
511 object->cell_capacity = 0;
512
513 parson_free(object->cells);
514 parson_free(object->names);
515 parson_free(object->values);
516 parson_free(object->cell_ixs);
517 parson_free(object->hashes);
518
519 object->cells = NULL;
520 object->names = NULL;
521 object->values = NULL;
522 object->cell_ixs = NULL;
523 object->hashes = NULL;
524 }
525
526 static JSON_Status json_object_grow_and_rehash(JSON_Object *object) {
527 JSON_Value *wrapping_value = NULL;
528 JSON_Object new_object;
529 char *key = NULL;
530 JSON_Value *value = NULL;
531 unsigned int i = 0;
532 size_t new_capacity = MAX(object->cell_capacity * 2, STARTING_CAPACITY);
533 JSON_Status res = json_object_init(&new_object, new_capacity);
534 if (res != JSONSuccess) {
535 return JSONFailure;
536 }
537
538 wrapping_value = json_object_get_wrapping_value(object);
539 new_object.wrapping_value = wrapping_value;
540
541 for (i = 0; i < object->count; i++) {
542 key = object->names[i];
543 value = object->values[i];
544 res = json_object_add(&new_object, key, value);
545 if (res != JSONSuccess) {
546 json_object_deinit(&new_object, PARSON_FALSE, PARSON_FALSE);
547 return JSONFailure;
548 }
549 value->parent = wrapping_value;
550 }
551 json_object_deinit(object, PARSON_FALSE, PARSON_FALSE);
552 *object = new_object;
553 return JSONSuccess;
554 }
555
556 static size_t json_object_get_cell_ix(const JSON_Object *object, const char *key, size_t key_len, unsigned long hash, parson_bool_t *out_found) {
557 size_t cell_ix = hash & (object->cell_capacity - 1);
558 size_t cell = 0;
559 size_t ix = 0;
560 unsigned int i = 0;
561 unsigned long hash_to_check = 0;
562 const char *key_to_check = NULL;
563 size_t key_to_check_len = 0;
564
565 *out_found = PARSON_FALSE;
566
567 for (i = 0; i < object->cell_capacity; i++) {
568 ix = (cell_ix + i) & (object->cell_capacity - 1);
569 cell = object->cells[ix];
570 if (cell == OBJECT_INVALID_IX) {
571 return ix;
572 }
573 hash_to_check = object->hashes[cell];
574 if (hash != hash_to_check) {
575 continue;
576 }
577 key_to_check = object->names[cell];
578 key_to_check_len = strlen(key_to_check);
579 if (key_to_check_len == key_len && strncmp(key, key_to_check, key_len) == 0) {
580 *out_found = PARSON_TRUE;
581 return ix;
582 }
583 }
584 return OBJECT_INVALID_IX;
585 }
586
587 static JSON_Status json_object_add(JSON_Object *object, char *name, JSON_Value *value) {
588 unsigned long hash = 0;
589 parson_bool_t found = PARSON_FALSE;
590 size_t cell_ix = 0;
591 JSON_Status res = JSONFailure;
592
593 if (!object || !name || !value) {
594 return JSONFailure;
595 }
596
597 hash = hash_string(name, strlen(name));
598 found = PARSON_FALSE;
599 cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
600 if (found) {
601 return JSONFailure;
602 }
603
604 if (object->count >= object->item_capacity) {
605 res = json_object_grow_and_rehash(object);
606 if (res != JSONSuccess) {
607 return JSONFailure;
608 }
609 cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
610 }
611
612 object->names[object->count] = name;
613 object->cells[cell_ix] = object->count;
614 object->values[object->count] = value;
615 object->cell_ixs[object->count] = cell_ix;
616 object->hashes[object->count] = hash;
617 object->count++;
618 value->parent = json_object_get_wrapping_value(object);
619
620 return JSONSuccess;
621 }
622
623 static JSON_Value * json_object_getn_value(const JSON_Object *object, const char *name, size_t name_len) {
624 unsigned long hash = 0;
625 parson_bool_t found = PARSON_FALSE;
626 size_t cell_ix = 0;
627 size_t item_ix = 0;
628 if (!object || !name) {
629 return NULL;
630 }
631 hash = hash_string(name, name_len);
632 found = PARSON_FALSE;
633 cell_ix = json_object_get_cell_ix(object, name, name_len, hash, &found);
634 if (!found) {
635 return NULL;
636 }
637 item_ix = object->cells[cell_ix];
638 return object->values[item_ix];
639 }
640
641 static JSON_Status json_object_remove_internal(JSON_Object *object, const char *name, parson_bool_t free_value) {
642 unsigned long hash = 0;
643 parson_bool_t found = PARSON_FALSE;
644 size_t cell = 0;
645 size_t item_ix = 0;
646 size_t last_item_ix = 0;
647 size_t i = 0;
648 size_t j = 0;
649 size_t x = 0;
650 size_t k = 0;
651 JSON_Value *val = NULL;
652
653 if (object == NULL) {
654 return JSONFailure;
655 }
656
657 hash = hash_string(name, strlen(name));
658 found = PARSON_FALSE;
659 cell = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
660 if (!found) {
661 return JSONFailure;
662 }
663
664 item_ix = object->cells[cell];
665 if (free_value) {
666 val = object->values[item_ix];
667 json_value_free(val);
668 val = NULL;
669 }
670
671 parson_free(object->names[item_ix]);
672 last_item_ix = object->count - 1;
673 if (item_ix < last_item_ix) {
674 object->names[item_ix] = object->names[last_item_ix];
675 object->values[item_ix] = object->values[last_item_ix];
676 object->cell_ixs[item_ix] = object->cell_ixs[last_item_ix];
677 object->hashes[item_ix] = object->hashes[last_item_ix];
678 object->cells[object->cell_ixs[item_ix]] = item_ix;
679 }
680 object->count--;
681
682 i = cell;
683 j = i;
684 for (x = 0; x < (object->cell_capacity - 1); x++) {
685 j = (j + 1) & (object->cell_capacity - 1);
686 if (object->cells[j] == OBJECT_INVALID_IX) {
687 break;
688 }
689 k = object->hashes[object->cells[j]] & (object->cell_capacity - 1);
690 if ((j > i && (k <= i || k > j))
691 || (j < i && (k <= i && k > j))) {
692 object->cell_ixs[object->cells[j]] = i;
693 object->cells[i] = object->cells[j];
694 i = j;
695 }
696 }
697 object->cells[i] = OBJECT_INVALID_IX;
698 return JSONSuccess;
699 }
700
701 static JSON_Status json_object_dotremove_internal(JSON_Object *object, const char *name, parson_bool_t free_value) {
702 JSON_Value *temp_value = NULL;
703 JSON_Object *temp_object = NULL;
704 const char *dot_pos = strchr(name, '.');
705 if (!dot_pos) {
706 return json_object_remove_internal(object, name, free_value);
707 }
708 temp_value = json_object_getn_value(object, name, dot_pos - name);
709 if (json_value_get_type(temp_value) != JSONObject) {
710 return JSONFailure;
711 }
712 temp_object = json_value_get_object(temp_value);
713 return json_object_dotremove_internal(temp_object, dot_pos + 1, free_value);
714 }
715
716 static void json_object_free(JSON_Object *object) {
717 json_object_deinit(object, PARSON_TRUE, PARSON_TRUE);
718 parson_free(object);
719 }
720
721 /* JSON Array */
722 static JSON_Array * json_array_make(JSON_Value *wrapping_value) {
723 JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
724 if (new_array == NULL) {
725 return NULL;
726 }
727 new_array->wrapping_value = wrapping_value;
728 new_array->items = (JSON_Value**)NULL;
729 new_array->capacity = 0;
730 new_array->count = 0;
731 return new_array;
732 }
733
734 static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
735 if (array->count >= array->capacity) {
736 size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
737 if (json_array_resize(array, new_capacity) != JSONSuccess) {
738 return JSONFailure;
739 }
740 }
741 value->parent = json_array_get_wrapping_value(array);
742 array->items[array->count] = value;
743 array->count++;
744 return JSONSuccess;
745 }
746
747 static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) {
748 JSON_Value **new_items = NULL;
749 if (new_capacity == 0) {
750 return JSONFailure;
751 }
752 new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
753 if (new_items == NULL) {
754 return JSONFailure;
755 }
756 if (array->items != NULL && array->count > 0) {
757 memcpy(new_items, array->items, array->count * sizeof(JSON_Value*));
758 }
759 parson_free(array->items);
760 array->items = new_items;
761 array->capacity = new_capacity;
762 return JSONSuccess;
763 }
764
765 static void json_array_free(JSON_Array *array) {
766 size_t i;
767 for (i = 0; i < array->count; i++) {
768 json_value_free(array->items[i]);
769 }
770 parson_free(array->items);
771 parson_free(array);
772 }
773
774 /* JSON Value */
775 static JSON_Value * json_value_init_string_no_copy(char *string, size_t length) {
776 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
777 if (!new_value) {
778 return NULL;
779 }
780 new_value->parent = NULL;
781 new_value->type = JSONString;
782 new_value->value.string.chars = string;
783 new_value->value.string.length = length;
784 return new_value;
785 }
786
787 /* Parser */
788 static JSON_Status skip_quotes(const char **string) {
789 if (**string != '\"') {
790 return JSONFailure;
791 }
792 SKIP_CHAR(string);
793 while (**string != '\"') {
794 if (**string == '\0') {
795 return JSONFailure;
796 } else if (**string == '\\') {
797 SKIP_CHAR(string);
798 if (**string == '\0') {
799 return JSONFailure;
800 }
801 }
802 SKIP_CHAR(string);
803 }
804 SKIP_CHAR(string);
805 return JSONSuccess;
806 }
807
808 static JSON_Status parse_utf16(const char **unprocessed, char **processed) {
809 unsigned int cp, lead, trail;
810 char *processed_ptr = *processed;
811 const char *unprocessed_ptr = *unprocessed;
812 JSON_Status status = JSONFailure;
813 unprocessed_ptr++; /* skips u */
814 status = parse_utf16_hex(unprocessed_ptr, &cp);
815 if (status != JSONSuccess) {
816 return JSONFailure;
817 }
818 if (cp < 0x80) {
819 processed_ptr[0] = (char)cp; /* 0xxxxxxx */
820 } else if (cp < 0x800) {
821 processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
822 processed_ptr[1] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
823 processed_ptr += 1;
824 } else if (cp < 0xD800 || cp > 0xDFFF) {
825 processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
826 processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
827 processed_ptr[2] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
828 processed_ptr += 2;
829 } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
830 lead = cp;
831 unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
832 if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') {
833 return JSONFailure;
834 }
835 status = parse_utf16_hex(unprocessed_ptr, &trail);
836 if (status != JSONSuccess || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
837 return JSONFailure;
838 }
839 cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + 0x010000;
840 processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
841 processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
842 processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
843 processed_ptr[3] = (((cp) & 0x3F) | 0x80); /* 10xxxxxx */
844 processed_ptr += 3;
845 } else { /* trail surrogate before lead surrogate */
846 return JSONFailure;
847 }
848 unprocessed_ptr += 3;
849 *processed = processed_ptr;
850 *unprocessed = unprocessed_ptr;
851 return JSONSuccess;
852 }
853
854
855 /* Copies and processes passed string up to supplied length.
856 Example: "\u006Corem ipsum" -> lorem ipsum */
857 static char* process_string(const char *input, size_t input_len, size_t *output_len) {
858 const char *input_ptr = input;
859 size_t initial_size = (input_len + 1) * sizeof(char);
860 size_t final_size = 0;
861 char *output = NULL, *output_ptr = NULL, *resized_output = NULL;
862 output = (char*)parson_malloc(initial_size);
863 if (output == NULL) {
864 goto error;
865 }
866 output_ptr = output;
867 while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < input_len) {
868 if (*input_ptr == '\\') {
869 input_ptr++;
870 switch (*input_ptr) {
871 case '\"': *output_ptr = '\"'; break;
872 case '\\': *output_ptr = '\\'; break;
873 case '/': *output_ptr = '/'; break;
874 case 'b': *output_ptr = '\b'; break;
875 case 'f': *output_ptr = '\f'; break;
876 case 'n': *output_ptr = '\n'; break;
877 case 'r': *output_ptr = '\r'; break;
878 case 't': *output_ptr = '\t'; break;
879 case 'u':
880 if (parse_utf16(&input_ptr, &output_ptr) != JSONSuccess) {
881 goto error;
882 }
883 break;
884 default:
885 goto error;
886 }
887 } else if ((unsigned char)*input_ptr < 0x20) {
888 goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */
889 } else {
890 *output_ptr = *input_ptr;
891 }
892 output_ptr++;
893 input_ptr++;
894 }
895 *output_ptr = '\0';
896 /* resize to new length */
897 final_size = (size_t)(output_ptr-output) + 1;
898 /* todo: don't resize if final_size == initial_size */
899 resized_output = (char*)parson_malloc(final_size);
900 if (resized_output == NULL) {
901 goto error;
902 }
903 memcpy(resized_output, output, final_size);
904 *output_len = final_size - 1;
905 parson_free(output);
906 return resized_output;
907 error:
908 parson_free(output);
909 return NULL;
910 }
911
912 /* Return processed contents of a string between quotes and
913 skips passed argument to a matching quote. */
914 static char * get_quoted_string(const char **string, size_t *output_string_len) {
915 const char *string_start = *string;
916 size_t input_string_len = 0;
917 JSON_Status status = skip_quotes(string);
918 if (status != JSONSuccess) {
919 return NULL;
920 }
921 input_string_len = *string - string_start - 2; /* length without quotes */
922 return process_string(string_start + 1, input_string_len, output_string_len);
923 }
924
925 static JSON_Value * parse_value(const char **string, size_t nesting) {
926 if (nesting > MAX_NESTING) {
927 return NULL;
928 }
929 SKIP_WHITESPACES(string);
930 switch (**string) {
931 case '{':
932 return parse_object_value(string, nesting + 1);
933 case '[':
934 return parse_array_value(string, nesting + 1);
935 case '\"':
936 return parse_string_value(string);
937 case 'f': case 't':
938 return parse_boolean_value(string);
939 case '-':
940 case '0': case '1': case '2': case '3': case '4':
941 case '5': case '6': case '7': case '8': case '9':
942 return parse_number_value(string);
943 case 'n':
944 return parse_null_value(string);
945 default:
946 return NULL;
947 }
948 }
949
950 static JSON_Value * parse_object_value(const char **string, size_t nesting) {
951 JSON_Status status = JSONFailure;
952 JSON_Value *output_value = NULL, *new_value = NULL;
953 JSON_Object *output_object = NULL;
954 char *new_key = NULL;
955
956 output_value = json_value_init_object();
957 if (output_value == NULL) {
958 return NULL;
959 }
960 if (**string != '{') {
961 json_value_free(output_value);
962 return NULL;
963 }
964 output_object = json_value_get_object(output_value);
965 SKIP_CHAR(string);
966 SKIP_WHITESPACES(string);
967 if (**string == '}') { /* empty object */
968 SKIP_CHAR(string);
969 return output_value;
970 }
971 while (**string != '\0') {
972 size_t key_len = 0;
973 new_key = get_quoted_string(string, &key_len);
974 /* We do not support key names with embedded \0 chars */
975 if (!new_key) {
976 json_value_free(output_value);
977 return NULL;
978 }
979 if (key_len != strlen(new_key)) {
980 parson_free(new_key);
981 json_value_free(output_value);
982 return NULL;
983 }
984 SKIP_WHITESPACES(string);
985 if (**string != ':') {
986 parson_free(new_key);
987 json_value_free(output_value);
988 return NULL;
989 }
990 SKIP_CHAR(string);
991 new_value = parse_value(string, nesting);
992 if (new_value == NULL) {
993 parson_free(new_key);
994 json_value_free(output_value);
995 return NULL;
996 }
997 status = json_object_add(output_object, new_key, new_value);
998 if (status != JSONSuccess) {
999 parson_free(new_key);
1000 json_value_free(new_value);
1001 json_value_free(output_value);
1002 return NULL;
1003 }
1004 SKIP_WHITESPACES(string);
1005 if (**string != ',') {
1006 break;
1007 }
1008 SKIP_CHAR(string);
1009 SKIP_WHITESPACES(string);
1010 if (**string == '}') {
1011 break;
1012 }
1013 }
1014 SKIP_WHITESPACES(string);
1015 if (**string != '}') {
1016 json_value_free(output_value);
1017 return NULL;
1018 }
1019 SKIP_CHAR(string);
1020 return output_value;
1021 }
1022
1023 static JSON_Value * parse_array_value(const char **string, size_t nesting) {
1024 JSON_Value *output_value = NULL, *new_array_value = NULL;
1025 JSON_Array *output_array = NULL;
1026 output_value = json_value_init_array();
1027 if (output_value == NULL) {
1028 return NULL;
1029 }
1030 if (**string != '[') {
1031 json_value_free(output_value);
1032 return NULL;
1033 }
1034 output_array = json_value_get_array(output_value);
1035 SKIP_CHAR(string);
1036 SKIP_WHITESPACES(string);
1037 if (**string == ']') { /* empty array */
1038 SKIP_CHAR(string);
1039 return output_value;
1040 }
1041 while (**string != '\0') {
1042 new_array_value = parse_value(string, nesting);
1043 if (new_array_value == NULL) {
1044 json_value_free(output_value);
1045 return NULL;
1046 }
1047 if (json_array_add(output_array, new_array_value) != JSONSuccess) {
1048 json_value_free(new_array_value);
1049 json_value_free(output_value);
1050 return NULL;
1051 }
1052 SKIP_WHITESPACES(string);
1053 if (**string != ',') {
1054 break;
1055 }
1056 SKIP_CHAR(string);
1057 SKIP_WHITESPACES(string);
1058 if (**string == ']') {
1059 break;
1060 }
1061 }
1062 SKIP_WHITESPACES(string);
1063 if (**string != ']' || /* Trim array after parsing is over */
1064 json_array_resize(output_array, json_array_get_count(output_array)) != JSONSuccess) {
1065 json_value_free(output_value);
1066 return NULL;
1067 }
1068 SKIP_CHAR(string);
1069 return output_value;
1070 }
1071
1072 static JSON_Value * parse_string_value(const char **string) {
1073 JSON_Value *value = NULL;
1074 size_t new_string_len = 0;
1075 char *new_string = get_quoted_string(string, &new_string_len);
1076 if (new_string == NULL) {
1077 return NULL;
1078 }
1079 value = json_value_init_string_no_copy(new_string, new_string_len);
1080 if (value == NULL) {
1081 parson_free(new_string);
1082 return NULL;
1083 }
1084 return value;
1085 }
1086
1087 static JSON_Value * parse_boolean_value(const char **string) {
1088 size_t true_token_size = SIZEOF_TOKEN("true");
1089 size_t false_token_size = SIZEOF_TOKEN("false");
1090 if (strncmp("true", *string, true_token_size) == 0) {
1091 *string += true_token_size;
1092 return json_value_init_boolean(1);
1093 } else if (strncmp("false", *string, false_token_size) == 0) {
1094 *string += false_token_size;
1095 return json_value_init_boolean(0);
1096 }
1097 return NULL;
1098 }
1099
1100 static JSON_Value * parse_number_value(const char **string) {
1101 char *end;
1102 double number = 0;
1103 errno = 0;
1104 number = strtod(*string, &end);
1105 if (errno == ERANGE && (number <= -HUGE_VAL || number >= HUGE_VAL)) {
1106 return NULL;
1107 }
1108 if ((errno && errno != ERANGE) || !is_decimal(*string, end - *string)) {
1109 return NULL;
1110 }
1111 *string = end;
1112 return json_value_init_number(number);
1113 }
1114
1115 static JSON_Value * parse_null_value(const char **string) {
1116 size_t token_size = SIZEOF_TOKEN("null");
1117 if (strncmp("null", *string, token_size) == 0) {
1118 *string += token_size;
1119 return json_value_init_null();
1120 }
1121 return NULL;
1122 }
1123
1124 /* Serialization */
1125
1126 /* APPEND_STRING() is only called on string literals.
1127 It's a bit hacky because it makes plenty of assumptions about the external state
1128 and should eventually be tidied up into a function (same goes for APPEND_INDENT)
1129 */
1130 #define APPEND_STRING(str) do {\
1131 written = SIZEOF_TOKEN((str));\
1132 if (buf != NULL) {\
1133 memcpy(buf, (str), written);\
1134 buf[written] = '\0';\
1135 buf += written;\
1136 }\
1137 written_total += written;\
1138 } while (0)
1139
1140 #define APPEND_INDENT(level) do {\
1141 int level_i = 0;\
1142 for (level_i = 0; level_i < (level); level_i++) {\
1143 APPEND_STRING(PARSON_INDENT_STR);\
1144 }\
1145 } while (0)
1146
1147 static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, parson_bool_t is_pretty, char *num_buf)
1148 {
1149 const char *key = NULL, *string = NULL;
1150 JSON_Value *temp_value = NULL;
1151 JSON_Array *array = NULL;
1152 JSON_Object *object = NULL;
1153 size_t i = 0, count = 0;
1154 double num = 0.0;
1155 int written = -1, written_total = 0;
1156 size_t len = 0;
1157
1158 switch (json_value_get_type(value)) {
1159 case JSONArray:
1160 array = json_value_get_array(value);
1161 count = json_array_get_count(array);
1162 APPEND_STRING("[");
1163 if (count > 0 && is_pretty) {
1164 APPEND_STRING("\n");
1165 }
1166 for (i = 0; i < count; i++) {
1167 if (is_pretty) {
1168 APPEND_INDENT(level+1);
1169 }
1170 temp_value = json_array_get_value(array, i);
1171 written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
1172 if (written < 0) {
1173 return -1;
1174 }
1175 if (buf != NULL) {
1176 buf += written;
1177 }
1178 written_total += written;
1179 if (i < (count - 1)) {
1180 APPEND_STRING(",");
1181 }
1182 if (is_pretty) {
1183 APPEND_STRING("\n");
1184 }
1185 }
1186 if (count > 0 && is_pretty) {
1187 APPEND_INDENT(level);
1188 }
1189 APPEND_STRING("]");
1190 return written_total;
1191 case JSONObject:
1192 object = json_value_get_object(value);
1193 count = json_object_get_count(object);
1194 APPEND_STRING("{");
1195 if (count > 0 && is_pretty) {
1196 APPEND_STRING("\n");
1197 }
1198 for (i = 0; i < count; i++) {
1199 key = json_object_get_name(object, i);
1200 if (key == NULL) {
1201 return -1;
1202 }
1203 if (is_pretty) {
1204 APPEND_INDENT(level+1);
1205 }
1206 /* We do not support key names with embedded \0 chars */
1207 written = json_serialize_string(key, strlen(key), buf);
1208 if (written < 0) {
1209 return -1;
1210 }
1211 if (buf != NULL) {
1212 buf += written;
1213 }
1214 written_total += written;
1215 APPEND_STRING(":");
1216 if (is_pretty) {
1217 APPEND_STRING(" ");
1218 }
1219 temp_value = json_object_get_value_at(object, i);
1220 written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
1221 if (written < 0) {
1222 return -1;
1223 }
1224 if (buf != NULL) {
1225 buf += written;
1226 }
1227 written_total += written;
1228 if (i < (count - 1)) {
1229 APPEND_STRING(",");
1230 }
1231 if (is_pretty) {
1232 APPEND_STRING("\n");
1233 }
1234 }
1235 if (count > 0 && is_pretty) {
1236 APPEND_INDENT(level);
1237 }
1238 APPEND_STRING("}");
1239 return written_total;
1240 case JSONString:
1241 string = json_value_get_string(value);
1242 if (string == NULL) {
1243 return -1;
1244 }
1245 len = json_value_get_string_len(value);
1246 written = json_serialize_string(string, len, buf);
1247 if (written < 0) {
1248 return -1;
1249 }
1250 if (buf != NULL) {
1251 buf += written;
1252 }
1253 written_total += written;
1254 return written_total;
1255 case JSONBoolean:
1256 if (json_value_get_boolean(value)) {
1257 APPEND_STRING("true");
1258 } else {
1259 APPEND_STRING("false");
1260 }
1261 return written_total;
1262 case JSONNumber:
1263 num = json_value_get_number(value);
1264 if (buf != NULL) {
1265 num_buf = buf;
1266 }
1267 if (parson_number_serialization_function) {
1268 written = parson_number_serialization_function(num, num_buf);
1269 } else {
1270 const char *float_format = parson_float_format ? parson_float_format : PARSON_DEFAULT_FLOAT_FORMAT;
1271 written = parson_sprintf(num_buf, float_format, num);
1272 }
1273 if (written < 0) {
1274 return -1;
1275 }
1276 if (buf != NULL) {
1277 buf += written;
1278 }
1279 written_total += written;
1280 return written_total;
1281 case JSONNull:
1282 APPEND_STRING("null");
1283 return written_total;
1284 case JSONError:
1285 return -1;
1286 default:
1287 return -1;
1288 }
1289 }
1290
1291 static int json_serialize_string(const char *string, size_t len, char *buf) {
1292 size_t i = 0;
1293 char c = '\0';
1294 int written = -1, written_total = 0;
1295 APPEND_STRING("\"");
1296 for (i = 0; i < len; i++) {
1297 c = string[i];
1298 switch (c) {
1299 case '\"': APPEND_STRING("\\\""); break;
1300 case '\\': APPEND_STRING("\\\\"); break;
1301 case '\b': APPEND_STRING("\\b"); break;
1302 case '\f': APPEND_STRING("\\f"); break;
1303 case '\n': APPEND_STRING("\\n"); break;
1304 case '\r': APPEND_STRING("\\r"); break;
1305 case '\t': APPEND_STRING("\\t"); break;
1306 case '\x00': APPEND_STRING("\\u0000"); break;
1307 case '\x01': APPEND_STRING("\\u0001"); break;
1308 case '\x02': APPEND_STRING("\\u0002"); break;
1309 case '\x03': APPEND_STRING("\\u0003"); break;
1310 case '\x04': APPEND_STRING("\\u0004"); break;
1311 case '\x05': APPEND_STRING("\\u0005"); break;
1312 case '\x06': APPEND_STRING("\\u0006"); break;
1313 case '\x07': APPEND_STRING("\\u0007"); break;
1314 /* '\x08' duplicate: '\b' */
1315 /* '\x09' duplicate: '\t' */
1316 /* '\x0a' duplicate: '\n' */
1317 case '\x0b': APPEND_STRING("\\u000b"); break;
1318 /* '\x0c' duplicate: '\f' */
1319 /* '\x0d' duplicate: '\r' */
1320 case '\x0e': APPEND_STRING("\\u000e"); break;
1321 case '\x0f': APPEND_STRING("\\u000f"); break;
1322 case '\x10': APPEND_STRING("\\u0010"); break;
1323 case '\x11': APPEND_STRING("\\u0011"); break;
1324 case '\x12': APPEND_STRING("\\u0012"); break;
1325 case '\x13': APPEND_STRING("\\u0013"); break;
1326 case '\x14': APPEND_STRING("\\u0014"); break;
1327 case '\x15': APPEND_STRING("\\u0015"); break;
1328 case '\x16': APPEND_STRING("\\u0016"); break;
1329 case '\x17': APPEND_STRING("\\u0017"); break;
1330 case '\x18': APPEND_STRING("\\u0018"); break;
1331 case '\x19': APPEND_STRING("\\u0019"); break;
1332 case '\x1a': APPEND_STRING("\\u001a"); break;
1333 case '\x1b': APPEND_STRING("\\u001b"); break;
1334 case '\x1c': APPEND_STRING("\\u001c"); break;
1335 case '\x1d': APPEND_STRING("\\u001d"); break;
1336 case '\x1e': APPEND_STRING("\\u001e"); break;
1337 case '\x1f': APPEND_STRING("\\u001f"); break;
1338 case '/':
1339 if (parson_escape_slashes) {
1340 APPEND_STRING("\\/"); /* to make json embeddable in xml\/html */
1341 } else {
1342 APPEND_STRING("/");
1343 }
1344 break;
1345 default:
1346 if (buf != NULL) {
1347 buf[0] = c;
1348 buf += 1;
1349 }
1350 written_total += 1;
1351 break;
1352 }
1353 }
1354 APPEND_STRING("\"");
1355 return written_total;
1356 }
1357
1358 #undef APPEND_STRING
1359 #undef APPEND_INDENT
1360
1361 /* Parser API */
1362 JSON_Value * json_parse_file(const char *filename) {
1363 char *file_contents = read_file(filename);
1364 JSON_Value *output_value = NULL;
1365 if (file_contents == NULL) {
1366 return NULL;
1367 }
1368 output_value = json_parse_string(file_contents);
1369 parson_free(file_contents);
1370 return output_value;
1371 }
1372
1373 JSON_Value * json_parse_file_with_comments(const char *filename) {
1374 char *file_contents = read_file(filename);
1375 JSON_Value *output_value = NULL;
1376 if (file_contents == NULL) {
1377 return NULL;
1378 }
1379 output_value = json_parse_string_with_comments(file_contents);
1380 parson_free(file_contents);
1381 return output_value;
1382 }
1383
1384 JSON_Value * json_parse_string(const char *string) {
1385 if (string == NULL) {
1386 return NULL;
1387 }
1388 if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') {
1389 string = string + 3; /* Support for UTF-8 BOM */
1390 }
1391 return parse_value((const char**)&string, 0);
1392 }
1393
1394 JSON_Value * json_parse_string_with_comments(const char *string) {
1395 JSON_Value *result = NULL;
1396 char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
1397 string_mutable_copy = parson_strdup(string);
1398 if (string_mutable_copy == NULL) {
1399 return NULL;
1400 }
1401 remove_comments(string_mutable_copy, "/*", "*/");
1402 remove_comments(string_mutable_copy, "//", "\n");
1403 string_mutable_copy_ptr = string_mutable_copy;
1404 result = parse_value((const char**)&string_mutable_copy_ptr, 0);
1405 parson_free(string_mutable_copy);
1406 return result;
1407 }
1408
1409 /* JSON Object API */
1410
1411 JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
1412 if (object == NULL || name == NULL) {
1413 return NULL;
1414 }
1415 return json_object_getn_value(object, name, strlen(name));
1416 }
1417
1418 const char * json_object_get_string(const JSON_Object *object, const char *name) {
1419 return json_value_get_string(json_object_get_value(object, name));
1420 }
1421
1422 size_t json_object_get_string_len(const JSON_Object *object, const char *name) {
1423 return json_value_get_string_len(json_object_get_value(object, name));
1424 }
1425
1426 double json_object_get_number(const JSON_Object *object, const char *name) {
1427 return json_value_get_number(json_object_get_value(object, name));
1428 }
1429
1430 JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) {
1431 return json_value_get_object(json_object_get_value(object, name));
1432 }
1433
1434 JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) {
1435 return json_value_get_array(json_object_get_value(object, name));
1436 }
1437
1438 int json_object_get_boolean(const JSON_Object *object, const char *name) {
1439 return json_value_get_boolean(json_object_get_value(object, name));
1440 }
1441
1442 JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
1443 const char *dot_position = strchr(name, '.');
1444 if (!dot_position) {
1445 return json_object_get_value(object, name);
1446 }
1447 object = json_value_get_object(json_object_getn_value(object, name, dot_position - name));
1448 return json_object_dotget_value(object, dot_position + 1);
1449 }
1450
1451 const char * json_object_dotget_string(const JSON_Object *object, const char *name) {
1452 return json_value_get_string(json_object_dotget_value(object, name));
1453 }
1454
1455 size_t json_object_dotget_string_len(const JSON_Object *object, const char *name) {
1456 return json_value_get_string_len(json_object_dotget_value(object, name));
1457 }
1458
1459 double json_object_dotget_number(const JSON_Object *object, const char *name) {
1460 return json_value_get_number(json_object_dotget_value(object, name));
1461 }
1462
1463 JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) {
1464 return json_value_get_object(json_object_dotget_value(object, name));
1465 }
1466
1467 JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) {
1468 return json_value_get_array(json_object_dotget_value(object, name));
1469 }
1470
1471 int json_object_dotget_boolean(const JSON_Object *object, const char *name) {
1472 return json_value_get_boolean(json_object_dotget_value(object, name));
1473 }
1474
1475 size_t json_object_get_count(const JSON_Object *object) {
1476 return object ? object->count : 0;
1477 }
1478
1479 const char * json_object_get_name(const JSON_Object *object, size_t index) {
1480 if (object == NULL || index >= json_object_get_count(object)) {
1481 return NULL;
1482 }
1483 return object->names[index];
1484 }
1485
1486 JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) {
1487 if (object == NULL || index >= json_object_get_count(object)) {
1488 return NULL;
1489 }
1490 return object->values[index];
1491 }
1492
1493 JSON_Value *json_object_get_wrapping_value(const JSON_Object *object) {
1494 if (!object) {
1495 return NULL;
1496 }
1497 return object->wrapping_value;
1498 }
1499
1500 int json_object_has_value (const JSON_Object *object, const char *name) {
1501 return json_object_get_value(object, name) != NULL;
1502 }
1503
1504 int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) {
1505 JSON_Value *val = json_object_get_value(object, name);
1506 return val != NULL && json_value_get_type(val) == type;
1507 }
1508
1509 int json_object_dothas_value (const JSON_Object *object, const char *name) {
1510 return json_object_dotget_value(object, name) != NULL;
1511 }
1512
1513 int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) {
1514 JSON_Value *val = json_object_dotget_value(object, name);
1515 return val != NULL && json_value_get_type(val) == type;
1516 }
1517
1518 /* JSON Array API */
1519 JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
1520 if (array == NULL || index >= json_array_get_count(array)) {
1521 return NULL;
1522 }
1523 return array->items[index];
1524 }
1525
1526 const char * json_array_get_string(const JSON_Array *array, size_t index) {
1527 return json_value_get_string(json_array_get_value(array, index));
1528 }
1529
1530 size_t json_array_get_string_len(const JSON_Array *array, size_t index) {
1531 return json_value_get_string_len(json_array_get_value(array, index));
1532 }
1533
1534 double json_array_get_number(const JSON_Array *array, size_t index) {
1535 return json_value_get_number(json_array_get_value(array, index));
1536 }
1537
1538 JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) {
1539 return json_value_get_object(json_array_get_value(array, index));
1540 }
1541
1542 JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) {
1543 return json_value_get_array(json_array_get_value(array, index));
1544 }
1545
1546 int json_array_get_boolean(const JSON_Array *array, size_t index) {
1547 return json_value_get_boolean(json_array_get_value(array, index));
1548 }
1549
1550 size_t json_array_get_count(const JSON_Array *array) {
1551 return array ? array->count : 0;
1552 }
1553
1554 JSON_Value * json_array_get_wrapping_value(const JSON_Array *array) {
1555 if (!array) {
1556 return NULL;
1557 }
1558 return array->wrapping_value;
1559 }
1560
1561 /* JSON Value API */
1562 JSON_Value_Type json_value_get_type(const JSON_Value *value) {
1563 return value ? value->type : JSONError;
1564 }
1565
1566 JSON_Object * json_value_get_object(const JSON_Value *value) {
1567 return json_value_get_type(value) == JSONObject ? value->value.object : NULL;
1568 }
1569
1570 JSON_Array * json_value_get_array(const JSON_Value *value) {
1571 return json_value_get_type(value) == JSONArray ? value->value.array : NULL;
1572 }
1573
1574 static const JSON_String * json_value_get_string_desc(const JSON_Value *value) {
1575 return json_value_get_type(value) == JSONString ? &value->value.string : NULL;
1576 }
1577
1578 const char * json_value_get_string(const JSON_Value *value) {
1579 const JSON_String *str = json_value_get_string_desc(value);
1580 return str ? str->chars : NULL;
1581 }
1582
1583 size_t json_value_get_string_len(const JSON_Value *value) {
1584 const JSON_String *str = json_value_get_string_desc(value);
1585 return str ? str->length : 0;
1586 }
1587
1588 double json_value_get_number(const JSON_Value *value) {
1589 return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
1590 }
1591
1592 int json_value_get_boolean(const JSON_Value *value) {
1593 return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1;
1594 }
1595
1596 JSON_Value * json_value_get_parent (const JSON_Value *value) {
1597 return value ? value->parent : NULL;
1598 }
1599
1600 void json_value_free(JSON_Value *value) {
1601 switch (json_value_get_type(value)) {
1602 case JSONObject:
1603 json_object_free(value->value.object);
1604 break;
1605 case JSONString:
1606 parson_free(value->value.string.chars);
1607 break;
1608 case JSONArray:
1609 json_array_free(value->value.array);
1610 break;
1611 default:
1612 break;
1613 }
1614 parson_free(value);
1615 }
1616
1617 JSON_Value * json_value_init_object(void) {
1618 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
1619 if (!new_value) {
1620 return NULL;
1621 }
1622 new_value->parent = NULL;
1623 new_value->type = JSONObject;
1624 new_value->value.object = json_object_make(new_value);
1625 if (!new_value->value.object) {
1626 parson_free(new_value);
1627 return NULL;
1628 }
1629 return new_value;
1630 }
1631
1632 JSON_Value * json_value_init_array(void) {
1633 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
1634 if (!new_value) {
1635 return NULL;
1636 }
1637 new_value->parent = NULL;
1638 new_value->type = JSONArray;
1639 new_value->value.array = json_array_make(new_value);
1640 if (!new_value->value.array) {
1641 parson_free(new_value);
1642 return NULL;
1643 }
1644 return new_value;
1645 }
1646
1647 JSON_Value * json_value_init_string(const char *string) {
1648 if (string == NULL) {
1649 return NULL;
1650 }
1651 return json_value_init_string_with_len(string, strlen(string));
1652 }
1653
1654 JSON_Value * json_value_init_string_with_len(const char *string, size_t length) {
1655 char *copy = NULL;
1656 JSON_Value *value;
1657 if (string == NULL) {
1658 return NULL;
1659 }
1660 if (!is_valid_utf8(string, length)) {
1661 return NULL;
1662 }
1663 copy = parson_strndup(string, length);
1664 if (copy == NULL) {
1665 return NULL;
1666 }
1667 value = json_value_init_string_no_copy(copy, length);
1668 if (value == NULL) {
1669 parson_free(copy);
1670 }
1671 return value;
1672 }
1673
1674 JSON_Value * json_value_init_number(double number) {
1675 JSON_Value *new_value = NULL;
1676 if (IS_NUMBER_INVALID(number)) {
1677 return NULL;
1678 }
1679 new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
1680 if (new_value == NULL) {
1681 return NULL;
1682 }
1683 new_value->parent = NULL;
1684 new_value->type = JSONNumber;
1685 new_value->value.number = number;
1686 return new_value;
1687 }
1688
1689 JSON_Value * json_value_init_boolean(int boolean) {
1690 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
1691 if (!new_value) {
1692 return NULL;
1693 }
1694 new_value->parent = NULL;
1695 new_value->type = JSONBoolean;
1696 new_value->value.boolean = boolean ? 1 : 0;
1697 return new_value;
1698 }
1699
1700 JSON_Value * json_value_init_null(void) {
1701 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
1702 if (!new_value) {
1703 return NULL;
1704 }
1705 new_value->parent = NULL;
1706 new_value->type = JSONNull;
1707 return new_value;
1708 }
1709
1710 JSON_Value * json_value_deep_copy(const JSON_Value *value) {
1711 size_t i = 0;
1712 JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL;
1713 const JSON_String *temp_string = NULL;
1714 const char *temp_key = NULL;
1715 char *temp_string_copy = NULL;
1716 JSON_Array *temp_array = NULL, *temp_array_copy = NULL;
1717 JSON_Object *temp_object = NULL, *temp_object_copy = NULL;
1718 JSON_Status res = JSONFailure;
1719 char *key_copy = NULL;
1720
1721 switch (json_value_get_type(value)) {
1722 case JSONArray:
1723 temp_array = json_value_get_array(value);
1724 return_value = json_value_init_array();
1725 if (return_value == NULL) {
1726 return NULL;
1727 }
1728 temp_array_copy = json_value_get_array(return_value);
1729 for (i = 0; i < json_array_get_count(temp_array); i++) {
1730 temp_value = json_array_get_value(temp_array, i);
1731 temp_value_copy = json_value_deep_copy(temp_value);
1732 if (temp_value_copy == NULL) {
1733 json_value_free(return_value);
1734 return NULL;
1735 }
1736 if (json_array_add(temp_array_copy, temp_value_copy) != JSONSuccess) {
1737 json_value_free(return_value);
1738 json_value_free(temp_value_copy);
1739 return NULL;
1740 }
1741 }
1742 return return_value;
1743 case JSONObject:
1744 temp_object = json_value_get_object(value);
1745 return_value = json_value_init_object();
1746 if (!return_value) {
1747 return NULL;
1748 }
1749 temp_object_copy = json_value_get_object(return_value);
1750 for (i = 0; i < json_object_get_count(temp_object); i++) {
1751 temp_key = json_object_get_name(temp_object, i);
1752 temp_value = json_object_get_value(temp_object, temp_key);
1753 temp_value_copy = json_value_deep_copy(temp_value);
1754 if (!temp_value_copy) {
1755 json_value_free(return_value);
1756 return NULL;
1757 }
1758 key_copy = parson_strdup(temp_key);
1759 if (!key_copy) {
1760 json_value_free(temp_value_copy);
1761 json_value_free(return_value);
1762 return NULL;
1763 }
1764 res = json_object_add(temp_object_copy, key_copy, temp_value_copy);
1765 if (res != JSONSuccess) {
1766 parson_free(key_copy);
1767 json_value_free(temp_value_copy);
1768 json_value_free(return_value);
1769 return NULL;
1770 }
1771 }
1772 return return_value;
1773 case JSONBoolean:
1774 return json_value_init_boolean(json_value_get_boolean(value));
1775 case JSONNumber:
1776 return json_value_init_number(json_value_get_number(value));
1777 case JSONString:
1778 temp_string = json_value_get_string_desc(value);
1779 if (temp_string == NULL) {
1780 return NULL;
1781 }
1782 temp_string_copy = parson_strndup(temp_string->chars, temp_string->length);
1783 if (temp_string_copy == NULL) {
1784 return NULL;
1785 }
1786 return_value = json_value_init_string_no_copy(temp_string_copy, temp_string->length);
1787 if (return_value == NULL) {
1788 parson_free(temp_string_copy);
1789 }
1790 return return_value;
1791 case JSONNull:
1792 return json_value_init_null();
1793 case JSONError:
1794 return NULL;
1795 default:
1796 return NULL;
1797 }
1798 }
1799
1800 size_t json_serialization_size(const JSON_Value *value) {
1801 char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
1802 int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_FALSE, num_buf);
1803 return res < 0 ? 0 : (size_t)(res) + 1;
1804 }
1805
1806 JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
1807 int written = -1;
1808 size_t needed_size_in_bytes = json_serialization_size(value);
1809 if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
1810 return JSONFailure;
1811 }
1812 written = json_serialize_to_buffer_r(value, buf, 0, PARSON_FALSE, NULL);
1813 if (written < 0) {
1814 return JSONFailure;
1815 }
1816 return JSONSuccess;
1817 }
1818
1819 JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) {
1820 JSON_Status return_code = JSONSuccess;
1821 FILE *fp = NULL;
1822 char *serialized_string = json_serialize_to_string(value);
1823 if (serialized_string == NULL) {
1824 return JSONFailure;
1825 }
1826 fp = fopen(filename, "w");
1827 if (fp == NULL) {
1828 json_free_serialized_string(serialized_string);
1829 return JSONFailure;
1830 }
1831 if (fputs(serialized_string, fp) == EOF) {
1832 return_code = JSONFailure;
1833 }
1834 if (fclose(fp) == EOF) {
1835 return_code = JSONFailure;
1836 }
1837 json_free_serialized_string(serialized_string);
1838 return return_code;
1839 }
1840
1841 char * json_serialize_to_string(const JSON_Value *value) {
1842 JSON_Status serialization_result = JSONFailure;
1843 size_t buf_size_bytes = json_serialization_size(value);
1844 char *buf = NULL;
1845 if (buf_size_bytes == 0) {
1846 return NULL;
1847 }
1848 buf = (char*)parson_malloc(buf_size_bytes);
1849 if (buf == NULL) {
1850 return NULL;
1851 }
1852 serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
1853 if (serialization_result != JSONSuccess) {
1854 json_free_serialized_string(buf);
1855 return NULL;
1856 }
1857 return buf;
1858 }
1859
1860 size_t json_serialization_size_pretty(const JSON_Value *value) {
1861 char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
1862 int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_TRUE, num_buf);
1863 return res < 0 ? 0 : (size_t)(res) + 1;
1864 }
1865
1866 JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
1867 int written = -1;
1868 size_t needed_size_in_bytes = json_serialization_size_pretty(value);
1869 if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
1870 return JSONFailure;
1871 }
1872 written = json_serialize_to_buffer_r(value, buf, 0, PARSON_TRUE, NULL);
1873 if (written < 0) {
1874 return JSONFailure;
1875 }
1876 return JSONSuccess;
1877 }
1878
1879 JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) {
1880 JSON_Status return_code = JSONSuccess;
1881 FILE *fp = NULL;
1882 char *serialized_string = json_serialize_to_string_pretty(value);
1883 if (serialized_string == NULL) {
1884 return JSONFailure;
1885 }
1886 fp = fopen(filename, "w");
1887 if (fp == NULL) {
1888 json_free_serialized_string(serialized_string);
1889 return JSONFailure;
1890 }
1891 if (fputs(serialized_string, fp) == EOF) {
1892 return_code = JSONFailure;
1893 }
1894 if (fclose(fp) == EOF) {
1895 return_code = JSONFailure;
1896 }
1897 json_free_serialized_string(serialized_string);
1898 return return_code;
1899 }
1900
1901 char * json_serialize_to_string_pretty(const JSON_Value *value) {
1902 JSON_Status serialization_result = JSONFailure;
1903 size_t buf_size_bytes = json_serialization_size_pretty(value);
1904 char *buf = NULL;
1905 if (buf_size_bytes == 0) {
1906 return NULL;
1907 }
1908 buf = (char*)parson_malloc(buf_size_bytes);
1909 if (buf == NULL) {
1910 return NULL;
1911 }
1912 serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
1913 if (serialization_result != JSONSuccess) {
1914 json_free_serialized_string(buf);
1915 return NULL;
1916 }
1917 return buf;
1918 }
1919
1920 void json_free_serialized_string(char *string) {
1921 parson_free(string);
1922 }
1923
1924 JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
1925 size_t to_move_bytes = 0;
1926 if (array == NULL || ix >= json_array_get_count(array)) {
1927 return JSONFailure;
1928 }
1929 json_value_free(json_array_get_value(array, ix));
1930 to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value*);
1931 memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
1932 array->count -= 1;
1933 return JSONSuccess;
1934 }
1935
1936 JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) {
1937 if (array == NULL || value == NULL || value->parent != NULL || ix >= json_array_get_count(array)) {
1938 return JSONFailure;
1939 }
1940 json_value_free(json_array_get_value(array, ix));
1941 value->parent = json_array_get_wrapping_value(array);
1942 array->items[ix] = value;
1943 return JSONSuccess;
1944 }
1945
1946 JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) {
1947 JSON_Value *value = json_value_init_string(string);
1948 if (value == NULL) {
1949 return JSONFailure;
1950 }
1951 if (json_array_replace_value(array, i, value) != JSONSuccess) {
1952 json_value_free(value);
1953 return JSONFailure;
1954 }
1955 return JSONSuccess;
1956 }
1957
1958 JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, const char *string, size_t len) {
1959 JSON_Value *value = json_value_init_string_with_len(string, len);
1960 if (value == NULL) {
1961 return JSONFailure;
1962 }
1963 if (json_array_replace_value(array, i, value) != JSONSuccess) {
1964 json_value_free(value);
1965 return JSONFailure;
1966 }
1967 return JSONSuccess;
1968 }
1969
1970 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) {
1971 JSON_Value *value = json_value_init_number(number);
1972 if (value == NULL) {
1973 return JSONFailure;
1974 }
1975 if (json_array_replace_value(array, i, value) != JSONSuccess) {
1976 json_value_free(value);
1977 return JSONFailure;
1978 }
1979 return JSONSuccess;
1980 }
1981
1982 JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) {
1983 JSON_Value *value = json_value_init_boolean(boolean);
1984 if (value == NULL) {
1985 return JSONFailure;
1986 }
1987 if (json_array_replace_value(array, i, value) != JSONSuccess) {
1988 json_value_free(value);
1989 return JSONFailure;
1990 }
1991 return JSONSuccess;
1992 }
1993
1994 JSON_Status json_array_replace_null(JSON_Array *array, size_t i) {
1995 JSON_Value *value = json_value_init_null();
1996 if (value == NULL) {
1997 return JSONFailure;
1998 }
1999 if (json_array_replace_value(array, i, value) != JSONSuccess) {
2000 json_value_free(value);
2001 return JSONFailure;
2002 }
2003 return JSONSuccess;
2004 }
2005
2006 JSON_Status json_array_clear(JSON_Array *array) {
2007 size_t i = 0;
2008 if (array == NULL) {
2009 return JSONFailure;
2010 }
2011 for (i = 0; i < json_array_get_count(array); i++) {
2012 json_value_free(json_array_get_value(array, i));
2013 }
2014 array->count = 0;
2015 return JSONSuccess;
2016 }
2017
2018 JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) {
2019 if (array == NULL || value == NULL || value->parent != NULL) {
2020 return JSONFailure;
2021 }
2022 return json_array_add(array, value);
2023 }
2024
2025 JSON_Status json_array_append_string(JSON_Array *array, const char *string) {
2026 JSON_Value *value = json_value_init_string(string);
2027 if (value == NULL) {
2028 return JSONFailure;
2029 }
2030 if (json_array_append_value(array, value) != JSONSuccess) {
2031 json_value_free(value);
2032 return JSONFailure;
2033 }
2034 return JSONSuccess;
2035 }
2036
2037 JSON_Status json_array_append_string_with_len(JSON_Array *array, const char *string, size_t len) {
2038 JSON_Value *value = json_value_init_string_with_len(string, len);
2039 if (value == NULL) {
2040 return JSONFailure;
2041 }
2042 if (json_array_append_value(array, value) != JSONSuccess) {
2043 json_value_free(value);
2044 return JSONFailure;
2045 }
2046 return JSONSuccess;
2047 }
2048
2049 JSON_Status json_array_append_number(JSON_Array *array, double number) {
2050 JSON_Value *value = json_value_init_number(number);
2051 if (value == NULL) {
2052 return JSONFailure;
2053 }
2054 if (json_array_append_value(array, value) != JSONSuccess) {
2055 json_value_free(value);
2056 return JSONFailure;
2057 }
2058 return JSONSuccess;
2059 }
2060
2061 JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) {
2062 JSON_Value *value = json_value_init_boolean(boolean);
2063 if (value == NULL) {
2064 return JSONFailure;
2065 }
2066 if (json_array_append_value(array, value) != JSONSuccess) {
2067 json_value_free(value);
2068 return JSONFailure;
2069 }
2070 return JSONSuccess;
2071 }
2072
2073 JSON_Status json_array_append_null(JSON_Array *array) {
2074 JSON_Value *value = json_value_init_null();
2075 if (value == NULL) {
2076 return JSONFailure;
2077 }
2078 if (json_array_append_value(array, value) != JSONSuccess) {
2079 json_value_free(value);
2080 return JSONFailure;
2081 }
2082 return JSONSuccess;
2083 }
2084
2085 JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
2086 unsigned long hash = 0;
2087 parson_bool_t found = PARSON_FALSE;
2088 size_t cell_ix = 0;
2089 size_t item_ix = 0;
2090 JSON_Value *old_value = NULL;
2091 char *key_copy = NULL;
2092
2093 if (!object || !name || !value || value->parent) {
2094 return JSONFailure;
2095 }
2096 hash = hash_string(name, strlen(name));
2097 found = PARSON_FALSE;
2098 cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
2099 if (found) {
2100 item_ix = object->cells[cell_ix];
2101 old_value = object->values[item_ix];
2102 json_value_free(old_value);
2103 object->values[item_ix] = value;
2104 value->parent = json_object_get_wrapping_value(object);
2105 return JSONSuccess;
2106 }
2107 if (object->count >= object->item_capacity) {
2108 JSON_Status res = json_object_grow_and_rehash(object);
2109 if (res != JSONSuccess) {
2110 return JSONFailure;
2111 }
2112 cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
2113 }
2114 key_copy = parson_strdup(name);
2115 if (!key_copy) {
2116 return JSONFailure;
2117 }
2118 object->names[object->count] = key_copy;
2119 object->cells[cell_ix] = object->count;
2120 object->values[object->count] = value;
2121 object->cell_ixs[object->count] = cell_ix;
2122 object->hashes[object->count] = hash;
2123 object->count++;
2124 value->parent = json_object_get_wrapping_value(object);
2125 return JSONSuccess;
2126 }
2127
2128 JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) {
2129 JSON_Value *value = json_value_init_string(string);
2130 JSON_Status status = json_object_set_value(object, name, value);
2131 if (status != JSONSuccess) {
2132 json_value_free(value);
2133 }
2134 return status;
2135 }
2136
2137 JSON_Status json_object_set_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len) {
2138 JSON_Value *value = json_value_init_string_with_len(string, len);
2139 JSON_Status status = json_object_set_value(object, name, value);
2140 if (status != JSONSuccess) {
2141 json_value_free(value);
2142 }
2143 return status;
2144 }
2145
2146 JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) {
2147 JSON_Value *value = json_value_init_number(number);
2148 JSON_Status status = json_object_set_value(object, name, value);
2149 if (status != JSONSuccess) {
2150 json_value_free(value);
2151 }
2152 return status;
2153 }
2154
2155 JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) {
2156 JSON_Value *value = json_value_init_boolean(boolean);
2157 JSON_Status status = json_object_set_value(object, name, value);
2158 if (status != JSONSuccess) {
2159 json_value_free(value);
2160 }
2161 return status;
2162 }
2163
2164 JSON_Status json_object_set_null(JSON_Object *object, const char *name) {
2165 JSON_Value *value = json_value_init_null();
2166 JSON_Status status = json_object_set_value(object, name, value);
2167 if (status != JSONSuccess) {
2168 json_value_free(value);
2169 }
2170 return status;
2171 }
2172
2173 JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) {
2174 const char *dot_pos = NULL;
2175 JSON_Value *temp_value = NULL, *new_value = NULL;
2176 JSON_Object *temp_object = NULL, *new_object = NULL;
2177 JSON_Status status = JSONFailure;
2178 size_t name_len = 0;
2179 char *name_copy = NULL;
2180
2181 if (object == NULL || name == NULL || value == NULL) {
2182 return JSONFailure;
2183 }
2184 dot_pos = strchr(name, '.');
2185 if (dot_pos == NULL) {
2186 return json_object_set_value(object, name, value);
2187 }
2188 name_len = dot_pos - name;
2189 temp_value = json_object_getn_value(object, name, name_len);
2190 if (temp_value) {
2191 /* Don't overwrite existing non-object (unlike json_object_set_value, but it shouldn't be changed at this point) */
2192 if (json_value_get_type(temp_value) != JSONObject) {
2193 return JSONFailure;
2194 }
2195 temp_object = json_value_get_object(temp_value);
2196 return json_object_dotset_value(temp_object, dot_pos + 1, value);
2197 }
2198 new_value = json_value_init_object();
2199 if (new_value == NULL) {
2200 return JSONFailure;
2201 }
2202 new_object = json_value_get_object(new_value);
2203 status = json_object_dotset_value(new_object, dot_pos + 1, value);
2204 if (status != JSONSuccess) {
2205 json_value_free(new_value);
2206 return JSONFailure;
2207 }
2208 name_copy = parson_strndup(name, name_len);
2209 if (!name_copy) {
2210 json_object_dotremove_internal(new_object, dot_pos + 1, 0);
2211 json_value_free(new_value);
2212 return JSONFailure;
2213 }
2214 status = json_object_add(object, name_copy, new_value);
2215 if (status != JSONSuccess) {
2216 parson_free(name_copy);
2217 json_object_dotremove_internal(new_object, dot_pos + 1, 0);
2218 json_value_free(new_value);
2219 return JSONFailure;
2220 }
2221 return JSONSuccess;
2222 }
2223
2224 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) {
2225 JSON_Value *value = json_value_init_string(string);
2226 if (value == NULL) {
2227 return JSONFailure;
2228 }
2229 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2230 json_value_free(value);
2231 return JSONFailure;
2232 }
2233 return JSONSuccess;
2234 }
2235
2236 JSON_Status json_object_dotset_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len) {
2237 JSON_Value *value = json_value_init_string_with_len(string, len);
2238 if (value == NULL) {
2239 return JSONFailure;
2240 }
2241 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2242 json_value_free(value);
2243 return JSONFailure;
2244 }
2245 return JSONSuccess;
2246 }
2247
2248 JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) {
2249 JSON_Value *value = json_value_init_number(number);
2250 if (value == NULL) {
2251 return JSONFailure;
2252 }
2253 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2254 json_value_free(value);
2255 return JSONFailure;
2256 }
2257 return JSONSuccess;
2258 }
2259
2260 JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) {
2261 JSON_Value *value = json_value_init_boolean(boolean);
2262 if (value == NULL) {
2263 return JSONFailure;
2264 }
2265 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2266 json_value_free(value);
2267 return JSONFailure;
2268 }
2269 return JSONSuccess;
2270 }
2271
2272 JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) {
2273 JSON_Value *value = json_value_init_null();
2274 if (value == NULL) {
2275 return JSONFailure;
2276 }
2277 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2278 json_value_free(value);
2279 return JSONFailure;
2280 }
2281 return JSONSuccess;
2282 }
2283
2284 JSON_Status json_object_remove(JSON_Object *object, const char *name) {
2285 return json_object_remove_internal(object, name, PARSON_TRUE);
2286 }
2287
2288 JSON_Status json_object_dotremove(JSON_Object *object, const char *name) {
2289 return json_object_dotremove_internal(object, name, PARSON_TRUE);
2290 }
2291
2292 JSON_Status json_object_clear(JSON_Object *object) {
2293 size_t i = 0;
2294 if (object == NULL) {
2295 return JSONFailure;
2296 }
2297 for (i = 0; i < json_object_get_count(object); i++) {
2298 parson_free(object->names[i]);
2299 object->names[i] = NULL;
2300
2301 json_value_free(object->values[i]);
2302 object->values[i] = NULL;
2303 }
2304 object->count = 0;
2305 for (i = 0; i < object->cell_capacity; i++) {
2306 object->cells[i] = OBJECT_INVALID_IX;
2307 }
2308 return JSONSuccess;
2309 }
2310
2311 JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) {
2312 JSON_Value *temp_schema_value = NULL, *temp_value = NULL;
2313 JSON_Array *schema_array = NULL, *value_array = NULL;
2314 JSON_Object *schema_object = NULL, *value_object = NULL;
2315 JSON_Value_Type schema_type = JSONError, value_type = JSONError;
2316 const char *key = NULL;
2317 size_t i = 0, count = 0;
2318 if (schema == NULL || value == NULL) {
2319 return JSONFailure;
2320 }
2321 schema_type = json_value_get_type(schema);
2322 value_type = json_value_get_type(value);
2323 if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */
2324 return JSONFailure;
2325 }
2326 switch (schema_type) {
2327 case JSONArray:
2328 schema_array = json_value_get_array(schema);
2329 value_array = json_value_get_array(value);
2330 count = json_array_get_count(schema_array);
2331 if (count == 0) {
2332 return JSONSuccess; /* Empty array allows all types */
2333 }
2334 /* Get first value from array, rest is ignored */
2335 temp_schema_value = json_array_get_value(schema_array, 0);
2336 for (i = 0; i < json_array_get_count(value_array); i++) {
2337 temp_value = json_array_get_value(value_array, i);
2338 if (json_validate(temp_schema_value, temp_value) != JSONSuccess) {
2339 return JSONFailure;
2340 }
2341 }
2342 return JSONSuccess;
2343 case JSONObject:
2344 schema_object = json_value_get_object(schema);
2345 value_object = json_value_get_object(value);
2346 count = json_object_get_count(schema_object);
2347 if (count == 0) {
2348 return JSONSuccess; /* Empty object allows all objects */
2349 } else if (json_object_get_count(value_object) < count) {
2350 return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */
2351 }
2352 for (i = 0; i < count; i++) {
2353 key = json_object_get_name(schema_object, i);
2354 temp_schema_value = json_object_get_value(schema_object, key);
2355 temp_value = json_object_get_value(value_object, key);
2356 if (temp_value == NULL) {
2357 return JSONFailure;
2358 }
2359 if (json_validate(temp_schema_value, temp_value) != JSONSuccess) {
2360 return JSONFailure;
2361 }
2362 }
2363 return JSONSuccess;
2364 case JSONString: case JSONNumber: case JSONBoolean: case JSONNull:
2365 return JSONSuccess; /* equality already tested before switch */
2366 case JSONError: default:
2367 return JSONFailure;
2368 }
2369 }
2370
2371 int json_value_equals(const JSON_Value *a, const JSON_Value *b) {
2372 JSON_Object *a_object = NULL, *b_object = NULL;
2373 JSON_Array *a_array = NULL, *b_array = NULL;
2374 const JSON_String *a_string = NULL, *b_string = NULL;
2375 const char *key = NULL;
2376 size_t a_count = 0, b_count = 0, i = 0;
2377 JSON_Value_Type a_type, b_type;
2378 a_type = json_value_get_type(a);
2379 b_type = json_value_get_type(b);
2380 if (a_type != b_type) {
2381 return PARSON_FALSE;
2382 }
2383 switch (a_type) {
2384 case JSONArray:
2385 a_array = json_value_get_array(a);
2386 b_array = json_value_get_array(b);
2387 a_count = json_array_get_count(a_array);
2388 b_count = json_array_get_count(b_array);
2389 if (a_count != b_count) {
2390 return PARSON_FALSE;
2391 }
2392 for (i = 0; i < a_count; i++) {
2393 if (!json_value_equals(json_array_get_value(a_array, i),
2394 json_array_get_value(b_array, i))) {
2395 return PARSON_FALSE;
2396 }
2397 }
2398 return PARSON_TRUE;
2399 case JSONObject:
2400 a_object = json_value_get_object(a);
2401 b_object = json_value_get_object(b);
2402 a_count = json_object_get_count(a_object);
2403 b_count = json_object_get_count(b_object);
2404 if (a_count != b_count) {
2405 return PARSON_FALSE;
2406 }
2407 for (i = 0; i < a_count; i++) {
2408 key = json_object_get_name(a_object, i);
2409 if (!json_value_equals(json_object_get_value(a_object, key),
2410 json_object_get_value(b_object, key))) {
2411 return PARSON_FALSE;
2412 }
2413 }
2414 return PARSON_TRUE;
2415 case JSONString:
2416 a_string = json_value_get_string_desc(a);
2417 b_string = json_value_get_string_desc(b);
2418 if (a_string == NULL || b_string == NULL) {
2419 return PARSON_FALSE; /* shouldn't happen */
2420 }
2421 return a_string->length == b_string->length &&
2422 memcmp(a_string->chars, b_string->chars, a_string->length) == 0;
2423 case JSONBoolean:
2424 return json_value_get_boolean(a) == json_value_get_boolean(b);
2425 case JSONNumber:
2426 return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */
2427 case JSONError:
2428 return PARSON_TRUE;
2429 case JSONNull:
2430 return PARSON_TRUE;
2431 default:
2432 return PARSON_TRUE;
2433 }
2434 }
2435
2436 JSON_Value_Type json_type(const JSON_Value *value) {
2437 return json_value_get_type(value);
2438 }
2439
2440 JSON_Object * json_object (const JSON_Value *value) {
2441 return json_value_get_object(value);
2442 }
2443
2444 JSON_Array * json_array(const JSON_Value *value) {
2445 return json_value_get_array(value);
2446 }
2447
2448 const char * json_string(const JSON_Value *value) {
2449 return json_value_get_string(value);
2450 }
2451
2452 size_t json_string_len(const JSON_Value *value) {
2453 return json_value_get_string_len(value);
2454 }
2455
2456 double json_number(const JSON_Value *value) {
2457 return json_value_get_number(value);
2458 }
2459
2460 int json_boolean(const JSON_Value *value) {
2461 return json_value_get_boolean(value);
2462 }
2463
2464 void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) {
2465 parson_malloc = malloc_fun;
2466 parson_free = free_fun;
2467 }
2468
2469 void json_set_escape_slashes(int escape_slashes) {
2470 parson_escape_slashes = escape_slashes;
2471 }
2472
2473 void json_set_float_serialization_format(const char *format) {
2474 if (parson_float_format) {
2475 parson_free(parson_float_format);
2476 parson_float_format = NULL;
2477 }
2478 if (!format) {
2479 parson_float_format = NULL;
2480 return;
2481 }
2482 parson_float_format = parson_strdup(format);
2483 }
2484
2485 void json_set_number_serialization_function(JSON_Number_Serialization_Function func) {
2486 parson_number_serialization_function = func;
2487 }