aboutsummaryrefslogtreecommitdiff
path: root/lib/parson
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parson')
-rw-r--r--lib/parson/parson.c194
-rw-r--r--lib/parson/parson.h2
2 files changed, 111 insertions, 85 deletions
diff --git a/lib/parson/parson.c b/lib/parson/parson.c
index b914ae8..583524b 100644
--- a/lib/parson/parson.c
+++ b/lib/parson/parson.c
@@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
- Copyright (c) 2012 - 2016 Krzysztof Gabis
+ Copyright (c) 2012 - 2017 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -35,15 +35,17 @@
#include <math.h>
#include <errno.h>
-#define STARTING_CAPACITY 15
-#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
-#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */
-#define MAX_NESTING 19
-#define DOUBLE_SERIALIZATION_FORMAT "%f"
+/* Apparently sscanf is not implemented in some "standard" libraries, so don't use it, if you
+ * don't have to. */
+#define sscanf THINK_TWICE_ABOUT_USING_SSCANF
+
+#define STARTING_CAPACITY 16
+#define MAX_NESTING 2048
+#define FLOAT_FORMAT "%1.17g"
#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
#define SKIP_CHAR(str) ((*str)++)
-#define SKIP_WHITESPACES(str) while (isspace(**str)) { SKIP_CHAR(str); }
+#define SKIP_WHITESPACES(str) while (isspace((unsigned char)(**str))) { SKIP_CHAR(str); }
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#undef malloc
@@ -90,7 +92,8 @@ static char * read_file(const char *filename);
static void remove_comments(char *string, const char *start_token, const char *end_token);
static char * parson_strndup(const char *string, size_t n);
static char * parson_strdup(const char *string);
-static int is_utf16_hex(const unsigned char *string);
+static int hex_char_to_int(char c);
+static int parse_utf16_hex(const char *string, unsigned int *result);
static int num_bytes_in_utf8_sequence(unsigned char c);
static int verify_utf8_sequence(const unsigned char *string, int *len);
static int is_valid_utf8(const char *string, size_t string_len);
@@ -114,7 +117,7 @@ static JSON_Value * json_value_init_string_no_copy(char *string);
/* Parser */
static JSON_Status skip_quotes(const char **string);
-static int parse_utf_16(const char **unprocessed, char **processed);
+static int parse_utf16(const char **unprocessed, char **processed);
static char * process_string(const char *input, size_t len);
static char * get_quoted_string(const char **string);
static JSON_Value * parse_object_value(const char **string, size_t nesting);
@@ -146,8 +149,31 @@ static char * parson_strdup(const char *string) {
return parson_strndup(string, strlen(string));
}
-static int is_utf16_hex(const unsigned char *s) {
- return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]);
+static int hex_char_to_int(char c) {
+ if (c >= '0' && c <= '9') {
+ return c - '0';
+ } else if (c >= 'a' && c <= 'f') {
+ return c - 'a' + 10;
+ } else if (c >= 'A' && c <= 'F') {
+ return c - 'A' + 10;
+ }
+ return -1;
+}
+
+static int parse_utf16_hex(const char *s, unsigned int *result) {
+ int x1, x2, x3, x4;
+ if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') {
+ return 0;
+ }
+ x1 = hex_char_to_int(s[0]);
+ x2 = hex_char_to_int(s[1]);
+ x3 = hex_char_to_int(s[2]);
+ x4 = hex_char_to_int(s[3]);
+ if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) {
+ return 0;
+ }
+ *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4);
+ return 1;
}
static int num_bytes_in_utf8_sequence(unsigned char c) {
@@ -236,7 +262,8 @@ static int is_decimal(const char *string, size_t length) {
static char * read_file(const char * filename) {
FILE *fp = fopen(filename, "r");
- size_t file_size;
+ size_t size_to_read = 0;
+ size_t size_read = 0;
long pos;
char *file_contents;
if (!fp) {
@@ -248,22 +275,21 @@ static char * read_file(const char * filename) {
fclose(fp);
return NULL;
}
- file_size = pos;
+ size_to_read = pos;
rewind(fp);
- file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1));
+ file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1));
if (!file_contents) {
fclose(fp);
return NULL;
}
- if (fread(file_contents, file_size, 1, fp) < 1) {
- if (ferror(fp)) {
- fclose(fp);
- parson_free(file_contents);
- return NULL;
- }
+ size_read = fread(file_contents, 1, size_to_read, fp);
+ if (size_read == 0 || ferror(fp)) {
+ fclose(fp);
+ parson_free(file_contents);
+ return NULL;
}
fclose(fp);
- file_contents[file_size] = '\0';
+ file_contents[size_read] = '\0';
return file_contents;
}
@@ -326,9 +352,6 @@ static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_V
}
if (object->count >= object->capacity) {
size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
- if (new_capacity > OBJECT_MAX_CAPACITY) {
- return JSONFailure;
- }
if (json_object_resize(object, new_capacity) == JSONFailure) {
return JSONFailure;
}
@@ -389,9 +412,10 @@ static JSON_Value * json_object_nget_value(const JSON_Object *object, const char
}
static void json_object_free(JSON_Object *object) {
- while(object->count--) {
- parson_free(object->names[object->count]);
- json_value_free(object->values[object->count]);
+ size_t i;
+ for (i = 0; i < object->count; i++) {
+ parson_free(object->names[i]);
+ json_value_free(object->values[i]);
}
parson_free(object->names);
parson_free(object->values);
@@ -414,9 +438,6 @@ static JSON_Array * json_array_init(JSON_Value *wrapping_value) {
static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
if (array->count >= array->capacity) {
size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
- if (new_capacity > ARRAY_MAX_CAPACITY) {
- return JSONFailure;
- }
if (json_array_resize(array, new_capacity) == JSONFailure) {
return JSONFailure;
}
@@ -446,8 +467,9 @@ static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) {
}
static void json_array_free(JSON_Array *array) {
- while (array->count--) {
- json_value_free(array->items[array->count]);
+ size_t i;
+ for (i = 0; i < array->count; i++) {
+ json_value_free(array->items[i]);
}
parson_free(array->items);
parson_free(array);
@@ -486,37 +508,43 @@ static JSON_Status skip_quotes(const char **string) {
return JSONSuccess;
}
-static int parse_utf_16(const char **unprocessed, char **processed) {
+static int parse_utf16(const char **unprocessed, char **processed) {
unsigned int cp, lead, trail;
+ int parse_succeeded = 0;
char *processed_ptr = *processed;
const char *unprocessed_ptr = *unprocessed;
unprocessed_ptr++; /* skips u */
- if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) {
+ parse_succeeded = parse_utf16_hex(unprocessed_ptr, &cp);
+ if (!parse_succeeded) {
return JSONFailure;
}
if (cp < 0x80) {
- *processed_ptr = cp; /* 0xxxxxxx */
+ processed_ptr[0] = (char)cp; /* 0xxxxxxx */
} else if (cp < 0x800) {
- *processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
- *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
+ processed_ptr[1] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr += 1;
} else if (cp < 0xD800 || cp > 0xDFFF) {
- *processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
- *processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
- *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
+ processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr[2] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr += 2;
} else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
lead = cp;
unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
- if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */
- !is_utf16_hex((const unsigned char*)unprocessed_ptr) ||
- sscanf(unprocessed_ptr, "%4x", &trail) == EOF ||
- trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
- return JSONFailure;
+ if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') {
+ return JSONFailure;
}
- cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000;
- *processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
- *processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
- *processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
- *processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */
+ parse_succeeded = parse_utf16_hex(unprocessed_ptr, &trail);
+ if (!parse_succeeded || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
+ return JSONFailure;
+ }
+ cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + 0x010000;
+ processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
+ processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
+ processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
+ processed_ptr[3] = (((cp) & 0x3F) | 0x80); /* 10xxxxxx */
+ processed_ptr += 3;
} else { /* trail surrogate before lead surrogate */
return JSONFailure;
}
@@ -533,9 +561,12 @@ static char* process_string(const char *input, size_t len) {
const char *input_ptr = input;
size_t initial_size = (len + 1) * sizeof(char);
size_t final_size = 0;
- char *output = (char*)parson_malloc(initial_size);
- char *output_ptr = output;
- char *resized_output = NULL;
+ char *output = NULL, *output_ptr = NULL, *resized_output = NULL;
+ output = (char*)parson_malloc(initial_size);
+ if (output == NULL) {
+ goto error;
+ }
+ output_ptr = output;
while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) {
if (*input_ptr == '\\') {
input_ptr++;
@@ -549,7 +580,7 @@ static char* process_string(const char *input, size_t len) {
case 'r': *output_ptr = '\r'; break;
case 't': *output_ptr = '\t'; break;
case 'u':
- if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) {
+ if (parse_utf16(&input_ptr, &output_ptr) == JSONFailure) {
goto error;
}
break;
@@ -633,8 +664,13 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) {
}
while (**string != '\0') {
new_key = get_quoted_string(string);
+ if (new_key == NULL) {
+ json_value_free(output_value);
+ return NULL;
+ }
SKIP_WHITESPACES(string);
- if (new_key == NULL || **string != ':') {
+ if (**string != ':') {
+ parson_free(new_key);
json_value_free(output_value);
return NULL;
}
@@ -645,9 +681,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) {
json_value_free(output_value);
return NULL;
}
- if(json_object_add(output_object, new_key, new_value) == JSONFailure) {
+ if (json_object_add(output_object, new_key, new_value) == JSONFailure) {
parson_free(new_key);
- parson_free(new_value);
+ json_value_free(new_value);
json_value_free(output_value);
return NULL;
}
@@ -683,12 +719,12 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) {
}
while (**string != '\0') {
new_array_value = parse_value(string, nesting);
- if (!new_array_value) {
+ if (new_array_value == NULL) {
json_value_free(output_value);
return NULL;
}
if (json_array_add(output_array, new_array_value) == JSONFailure) {
- parson_free(new_array_value);
+ json_value_free(new_array_value);
json_value_free(output_value);
return NULL;
}
@@ -885,13 +921,7 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
if (buf != NULL) {
num_buf = buf;
}
- if (num == ((double)(int)num)) { /* check if num is integer */
- written = sprintf(num_buf, "%d", (int)num);
- } else if (num == ((double)(unsigned int)num)) {
- written = sprintf(num_buf, "%u", (unsigned int)num);
- } else {
- written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
- }
+ written = sprintf(num_buf, FLOAT_FORMAT, num);
if (written < 0) {
return -1;
}
@@ -1271,8 +1301,12 @@ JSON_Value * json_value_init_string(const char *string) {
}
JSON_Value * json_value_init_number(double number) {
- JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
- if (!new_value) {
+ JSON_Value *new_value = NULL;
+ if ((number * 0.0) != 0.0) { /* nan and inf test */
+ return NULL;
+ }
+ new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
+ if (new_value == NULL) {
return NULL;
}
new_value->parent = NULL;
@@ -1407,7 +1441,7 @@ JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename
if (serialized_string == NULL) {
return JSONFailure;
}
- fp = fopen (filename, "w");
+ fp = fopen(filename, "w");
if (fp == NULL) {
json_free_serialized_string(serialized_string);
return JSONFailure;
@@ -1467,7 +1501,7 @@ JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *f
if (serialized_string == NULL) {
return JSONFailure;
}
- fp = fopen (filename, "w");
+ fp = fopen(filename, "w");
if (fp == NULL) {
json_free_serialized_string(serialized_string);
return JSONFailure;
@@ -1506,20 +1540,13 @@ void json_free_serialized_string(char *string) {
}
JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
- JSON_Value *temp_value = NULL;
- size_t last_element_ix = 0;
+ size_t to_move_bytes = 0;
if (array == NULL || ix >= json_array_get_count(array)) {
return JSONFailure;
}
- last_element_ix = json_array_get_count(array) - 1;
json_value_free(json_array_get_value(array, ix));
- if (ix != last_element_ix) { /* Replace value with one from the end of array */
- temp_value = json_array_get_value(array, last_element_ix);
- if (temp_value == NULL) {
- return JSONFailure;
- }
- array->items[ix] = temp_value;
- }
+ to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value*);
+ memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
array->count -= 1;
return JSONSuccess;
}
@@ -1691,7 +1718,7 @@ JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON
char *current_name = NULL;
JSON_Object *temp_obj = NULL;
JSON_Value *new_value = NULL;
- if (value == NULL || name == NULL || value == NULL) {
+ if (object == NULL || name == NULL || value == NULL) {
return JSONFailure;
}
dot_pos = strchr(name, '.');
@@ -1796,11 +1823,10 @@ JSON_Status json_object_dotremove(JSON_Object *object, const char *name) {
} else {
current_name = parson_strndup(name, dot_pos - name);
temp_obj = json_object_get_object(object, current_name);
+ parson_free(current_name);
if (temp_obj == NULL) {
- parson_free(current_name);
return JSONFailure;
}
- parson_free(current_name);
return json_object_dotremove(temp_obj, dot_pos + 1);
}
}
@@ -1878,7 +1904,7 @@ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) {
}
}
-JSON_Status json_value_equals(const JSON_Value *a, const JSON_Value *b) {
+int json_value_equals(const JSON_Value *a, const JSON_Value *b) {
JSON_Object *a_object = NULL, *b_object = NULL;
JSON_Array *a_array = NULL, *b_array = NULL;
const char *a_string = NULL, *b_string = NULL;
diff --git a/lib/parson/parson.h b/lib/parson/parson.h
index e4cd21e..6438c93 100644
--- a/lib/parson/parson.h
+++ b/lib/parson/parson.h
@@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
- Copyright (c) 2012 - 2016 Krzysztof Gabis
+ Copyright (c) 2012 - 2017 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal