diff --git a/build/__tools__/build.sh b/build/__tools__/build.sh index 401873ee..e6c6daa3 100755 --- a/build/__tools__/build.sh +++ b/build/__tools__/build.sh @@ -42,7 +42,6 @@ while true ; do *) echo "Internal error!" ; exit 1 ;; esac done -TEMP if [ ${F_CLIENT} == "yes" ]; then CMAKE_ARGS="-DBUILD_CLIENT=1 -DBUILD_TRUSTED=0 -DBUILD_UNTRUSTED=0" diff --git a/common/interpreter/wawaka_wasm/CMakeLists.txt b/common/interpreter/wawaka_wasm/CMakeLists.txt index 6fc73083..b4fc3e4d 100644 --- a/common/interpreter/wawaka_wasm/CMakeLists.txt +++ b/common/interpreter/wawaka_wasm/CMakeLists.txt @@ -104,13 +104,18 @@ ENABLE_LANGUAGE (ASM) ################################################################# # WAMR Library ################################################################# - # include the build config template file INCLUDE (${WAMR_ROOT_DIR}/build-scripts/config_common.cmake) INCLUDE (${SHARED_DIR}/platform/${WAMR_BUILD_PLATFORM}/shared_platform.cmake) INCLUDE (${SHARED_DIR}/mem-alloc/mem_alloc.cmake) INCLUDE (${SHARED_DIR}/utils/shared_utils.cmake) + +# this picks up the libc_erro.h, this appears to be an error in WAMR in that the +# test for WASI is AFTER the attempt to include a file that is only available when +# WASI is turned on. +INCLUDE (${SHARED_DIR}/platform/common/libc-util/platform_common_libc_util.cmake) + IF (WAMR_BUILD_LIBC_BUILTIN EQUAL 1) INCLUDE (${IWASM_DIR}/libraries/libc-builtin/libc_builtin.cmake) ENDIF () @@ -126,13 +131,19 @@ ENDIF () ADD_LIBRARY (${IWASM_STATIC_NAME} ${PLATFORM_SHARED_SOURCE} + ${PLATFORM_COMMON_LIBC_UTIL_SOURCE} ${MEM_ALLOC_SHARED_SOURCE} ${UTILS_SHARED_SOURCE} ${LIBC_BUILTIN_SOURCE} ${LIBC_WASI_SOURCE} + ${WAMR_POSIX_SOURCES} ${IWASM_COMMON_SOURCE} ${IWASM_INTERP_SOURCE} - ${IWASM_COMPL_SOURCE}) + ${IWASM_COMPL_SOURCE} + # this is necessary because WAMR currently does not have a definition + # for os_is_handle_valid in the sgx_platform.c file + ${CMAKE_CURRENT_SOURCE_DIR}/wamr_fixes.c + ) TARGET_INCLUDE_DIRECTORIES(${IWASM_STATIC_NAME} PUBLIC ${SHARED_DIR}/include) TARGET_INCLUDE_DIRECTORIES(${IWASM_STATIC_NAME} PUBLIC ${IWASM_DIR}/include) diff --git a/common/interpreter/wawaka_wasm/README.md b/common/interpreter/wawaka_wasm/README.md index 5fa32786..91855386 100644 --- a/common/interpreter/wawaka_wasm/README.md +++ b/common/interpreter/wawaka_wasm/README.md @@ -30,8 +30,8 @@ enclave with Wawaka enabled, you will need to do the following: There are many toolchains that could be used to build a WASM code. By default, Wawaka contracts are compiled with the compilers provided by [WASI SDK](https://github.com/WebAssembly/wasi-sdk). To use WASI SDK, download and install the appropriate package file from -https://github.com/WebAssembly/wasi-sdk/releases (we have verified that release wasi-sdk-12 works -with WAMR version WAMR-1.1.2). +https://github.com/WebAssembly/wasi-sdk/releases (we have verified that release wasi-sdk-21 works +with WAMR version WAMR-1.3). ```bash wget -q -P /tmp https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk_12.0_amd64.deb @@ -46,14 +46,14 @@ SDK would be installed in the directory `/opt/wasi-sdk`. If wawaka is configured as the contract interpreter, the libraries implementing the WASM interpreter will be built for use with Intel SGX. The source for the WAMR interpreter is included as a submodule in the interpreters/ folder, and will -always point to the latest tagged commit that we have validated: `WAMR-1.1.2`. +always point to the latest tagged commit that we have validated: `WAMR-1.3`. If the PDO parent repo was not cloned with the `--recurse-submodules` flag, you will have to explictly pull the submodule source. ``` cd ${PDO_SOURCE_ROOT}/interpreters/wasm-micro-runtime git submodule update --init -git checkout WAMR-1.1.2 # optional +git checkout WAMR-1.3 # optional ``` The WAMR API is built during the Wawaka build, so no additional diff --git a/common/interpreter/wawaka_wasm/wamr_fixes.c b/common/interpreter/wawaka_wasm/wamr_fixes.c new file mode 100644 index 00000000..3677a0f2 --- /dev/null +++ b/common/interpreter/wawaka_wasm/wamr_fixes.c @@ -0,0 +1,8 @@ +#include "platform_internal.h" + +bool +os_is_handle_valid(os_file_handle *handle) +{ + assert(handle != NULL); + return *handle > -1; +} diff --git a/common/packages/parson/parson.cpp b/common/packages/parson/parson.cpp index fe0ed1b1..76aaa153 100644 --- a/common/packages/parson/parson.cpp +++ b/common/packages/parson/parson.cpp @@ -1,25 +1,31 @@ /* - Parson ( http://kgabis.github.com/parson/ ) - Copyright (c) 2012 - 2016 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 - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - */ + SPDX-License-Identifier: MIT + + Parson 1.5.3 (https://github.com/kgabis/parson) + Copyright (c) 2012 - 2023 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 + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +// NOTE: This file has been modified from the original to support execution +// in SGX and PDO smart contracts. + #ifdef _MSC_VER #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS @@ -28,1724 +34,2482 @@ #include "parson.h" -#ifdef __cplusplus -extern "C" { -#endif -int snprintf(char *str, size_t size, const char *format, ...); -#ifdef __cplusplus -} +#define PARSON_IMPL_VERSION_MAJOR 1 +#define PARSON_IMPL_VERSION_MINOR 5 +#define PARSON_IMPL_VERSION_PATCH 3 + +#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\ +|| (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\ +|| (PARSON_VERSION_PATCH != PARSON_IMPL_VERSION_PATCH) +#error "parson version mismatch between parson.c and parson.h" #endif +#include +#include #include #include #include #include +#include + +/* Apparently sscanf is not implemented in some "standard" libraries, so don't use it, if you + * don't have to. */ +#ifdef sscanf +#undef sscanf +#define sscanf THINK_TWICE_ABOUT_USING_SSCANF +#endif + +/* strcpy is unsafe */ +#ifdef strcpy +#undef strcpy +#endif +#define strcpy USE_MEMCPY_INSTEAD_OF_STRCPY + +#define STARTING_CAPACITY 16 +#define MAX_NESTING 2048 + +#ifndef PARSON_DEFAULT_FLOAT_FORMAT +#define PARSON_DEFAULT_FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */ +#endif + +#ifndef PARSON_NUM_BUF_SIZE +#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 */ +#endif -#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" +#ifndef PARSON_INDENT_STR +#define PARSON_INDENT_STR " " +#endif #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 #undef free +#if defined(isnan) && defined(isinf) +#define IS_NUMBER_INVALID(x) (isnan((x)) || isinf((x))) +#else +#define IS_NUMBER_INVALID(x) (((x) * 0.0) != 0.0) +#endif + +#define OBJECT_INVALID_IX ((size_t)-1) + static JSON_Malloc_Function parson_malloc = malloc; static JSON_Free_Function parson_free = free; +#ifndef COMPILE_FOR_SGX +static int parson_escape_slashes = 1; +#else +static int parson_escape_slashes = 0; +#endif + +static char *parson_float_format = NULL; + +static JSON_Number_Serialization_Function parson_number_serialization_function = NULL; + #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ +typedef int parson_bool_t; + +#define PARSON_TRUE 1 +#define PARSON_FALSE 0 + +typedef struct json_string { + char *chars; + size_t length; +} JSON_String; + /* Type definitions */ typedef union json_value_value { - char *string; - double number; - JSON_Object *object; - JSON_Array *array; - int boolean; - int null; + JSON_String string; + double number; + JSON_Object *object; + JSON_Array *array; + int boolean; + int null; } JSON_Value_Value; struct json_value_t { - JSON_Value_Type type; - JSON_Value_Value value; + JSON_Value *parent; + JSON_Value_Type type; + JSON_Value_Value value; }; struct json_object_t { - char **names; - JSON_Value **values; - size_t count; - size_t capacity; + JSON_Value *wrapping_value; + size_t *cells; + unsigned long *hashes; + char **names; + JSON_Value **values; + size_t *cell_ixs; + size_t count; + size_t item_capacity; + size_t cell_capacity; }; struct json_array_t { - JSON_Value **items; - size_t count; - size_t capacity; + JSON_Value *wrapping_value; + JSON_Value **items; + size_t count; + size_t capacity; }; /* Various */ -// SGX support static char * read_file(const char *filename); +#ifndef COMPILE_FOR_SGX +static char * read_file(const char *filename); +#endif 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 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); -static int is_decimal(const char *string, size_t length); +#ifndef COMPILE_FOR_SGX +static int parson_sprintf(char * s, const char * format, ...); +#else +#define parson_snprintf(s, size, format, ...) snprintf(s, size, format, ##__VA_ARGS__) +#endif + +static int hex_char_to_int(char c); +static JSON_Status parse_utf16_hex(const char *string, unsigned int *result); +static int num_bytes_in_utf8_sequence(unsigned char c); +static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len); +static parson_bool_t is_valid_utf8(const char *string, size_t string_len); +static parson_bool_t is_decimal(const char *string, size_t length); +static unsigned long hash_string(const char *string, size_t n); /* JSON Object */ -static JSON_Object * json_object_init(void); -static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value); -static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity); -static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n); +static JSON_Object * json_object_make(JSON_Value *wrapping_value); +static JSON_Status json_object_init(JSON_Object *object, size_t capacity); +static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys, parson_bool_t free_values); +static JSON_Status json_object_grow_and_rehash(JSON_Object *object); +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); +static JSON_Status json_object_add(JSON_Object *object, char *name, JSON_Value *value); +static JSON_Value * json_object_getn_value(const JSON_Object *object, const char *name, size_t name_len); +static JSON_Status json_object_remove_internal(JSON_Object *object, const char *name, parson_bool_t free_value); +static JSON_Status json_object_dotremove_internal(JSON_Object *object, const char *name, parson_bool_t free_value); static void json_object_free(JSON_Object *object); /* JSON Array */ -static JSON_Array * json_array_init(void); +static JSON_Array * json_array_make(JSON_Value *wrapping_value); static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value); static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity); static void json_array_free(JSON_Array *array); /* JSON Value */ -static JSON_Value * json_value_init_string_no_copy(char *string); +static JSON_Value * json_value_init_string_no_copy(char *string, size_t length); +static const JSON_String * json_value_get_string_desc(const JSON_Value *value); /* Parser */ -static void skip_quotes(const char **string); -static int parse_utf_16(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); -static JSON_Value * parse_array_value(const char **string, size_t nesting); -static JSON_Value * parse_string_value(const char **string); -static JSON_Value * parse_boolean_value(const char **string); -static JSON_Value * parse_number_value(const char **string); -static JSON_Value * parse_null_value(const char **string); -static JSON_Value * parse_value(const char **string, size_t nesting); +static JSON_Status skip_quotes(const char **string); +static JSON_Status parse_utf16(const char **unprocessed, char **processed); +static char * process_string(const char *input, size_t input_len, size_t *output_len); +static char * get_quoted_string(const char **string, size_t *output_string_len); +static JSON_Value * parse_object_value(const char **string, size_t nesting); +static JSON_Value * parse_array_value(const char **string, size_t nesting); +static JSON_Value * parse_string_value(const char **string); +static JSON_Value * parse_boolean_value(const char **string); +static JSON_Value * parse_number_value(const char **string); +static JSON_Value * parse_null_value(const char **string); +static JSON_Value * parse_value(const char **string, size_t nesting); /* Serialization */ -static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf, size_t buf_length); -static int json_serialize_string(const char *string, char *buf); -static int append_indent(char *buf, int level); -static int append_string(char *buf, const char *string); +static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, parson_bool_t is_pretty, char *num_buf); +static int json_serialize_string(const char *string, size_t len, char *buf); /* Various */ +#ifndef COMPILE_FOR_SGX +static char * read_file(const char * filename) { + FILE *fp = fopen(filename, "r"); + size_t size_to_read = 0; + size_t size_read = 0; + long pos; + char *file_contents; + if (!fp) { + return NULL; + } + fseek(fp, 0L, SEEK_END); + pos = ftell(fp); + if (pos < 0) { + fclose(fp); + return NULL; + } + size_to_read = pos; + rewind(fp); + file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1)); + if (!file_contents) { + fclose(fp); + 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[size_read] = '\0'; + return file_contents; +} +#endif + +static void remove_comments(char *string, const char *start_token, const char *end_token) { + parson_bool_t in_string = PARSON_FALSE, escaped = PARSON_FALSE; + size_t i; + char *ptr = NULL, current_char; + size_t start_token_len = strlen(start_token); + size_t end_token_len = strlen(end_token); + if (start_token_len == 0 || end_token_len == 0) { + return; + } + while ((current_char = *string) != '\0') { + if (current_char == '\\' && !escaped) { + escaped = PARSON_TRUE; + string++; + continue; + } else if (current_char == '\"' && !escaped) { + in_string = !in_string; + } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { + for(i = 0; i < start_token_len; i++) { + string[i] = ' '; + } + string = string + start_token_len; + ptr = strstr(string, end_token); + if (!ptr) { + return; + } + for (i = 0; i < (ptr - string) + end_token_len; i++) { + string[i] = ' '; + } + string = ptr + end_token_len - 1; + } + escaped = PARSON_FALSE; + string++; + } +} + static char * parson_strndup(const char *string, size_t n) { - char *output_string = (char*)parson_malloc(n + 1); - if (!output_string) - return NULL; - output_string[n] = '\0'; - strncpy(output_string, string, n); - return output_string; + /* We expect the caller has validated that 'n' fits within the input buffer. */ + char *output_string = (char*)parson_malloc(n + 1); + if (!output_string) { + return NULL; + } + output_string[n] = '\0'; + memcpy(output_string, string, n); + return output_string; } static char * parson_strdup(const char *string) { - return parson_strndup(string, strlen(string)); + return parson_strndup(string, strlen(string)); +} + +#ifndef COMPILE_FOR_SGX +static int parson_sprintf(char * s, const char * format, ...) { + int result; + va_list args; + va_start(args, format); + + #if defined(__APPLE__) && defined(__clang__) + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + #endif + result = vsprintf(s, format, args); + #if defined(__APPLE__) && defined(__clang__) + #pragma clang diagnostic pop + #endif + + va_end(args); + return result; +} +#endif + +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 JSON_Status 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 JSONFailure; + } + 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 JSONFailure; + } + *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4); + return JSONSuccess; } static int num_bytes_in_utf8_sequence(unsigned char c) { - if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { - return 0; - } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ - return 1; - } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ - return 2; - } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ - return 3; - } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ - return 4; - } - return 0; /* won't happen */ -} - -static int verify_utf8_sequence(const unsigned char *string, int *len) { - unsigned int cp = 0; - *len = num_bytes_in_utf8_sequence(string[0]); - - if (*len == 1) { - cp = string[0]; - } else if (*len == 2 && IS_CONT(string[1])) { - cp = string[0] & 0x1F; - cp = (cp << 6) | (string[1] & 0x3F); - } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { - cp = ((unsigned char)string[0]) & 0xF; - cp = (cp << 6) | (string[1] & 0x3F); - cp = (cp << 6) | (string[2] & 0x3F); - } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { - cp = string[0] & 0x7; - cp = (cp << 6) | (string[1] & 0x3F); - cp = (cp << 6) | (string[2] & 0x3F); - cp = (cp << 6) | (string[3] & 0x3F); - } else { - return 0; - } + if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { + return 0; + } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ + return 1; + } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ + return 2; + } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ + return 3; + } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ + return 4; + } + return 0; /* won't happen */ +} + +static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len) { + unsigned int cp = 0; + *len = num_bytes_in_utf8_sequence(string[0]); + + if (*len == 1) { + cp = string[0]; + } else if (*len == 2 && IS_CONT(string[1])) { + cp = string[0] & 0x1F; + cp = (cp << 6) | (string[1] & 0x3F); + } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { + cp = ((unsigned char)string[0]) & 0xF; + cp = (cp << 6) | (string[1] & 0x3F); + cp = (cp << 6) | (string[2] & 0x3F); + } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { + cp = string[0] & 0x7; + cp = (cp << 6) | (string[1] & 0x3F); + cp = (cp << 6) | (string[2] & 0x3F); + cp = (cp << 6) | (string[3] & 0x3F); + } else { + return JSONFailure; + } - /* overlong encodings */ - if ((cp < 0x80 && *len > 1) || - (cp < 0x800 && *len > 2) || - (cp < 0x10000 && *len > 3)) { - return 0; - } + /* overlong encodings */ + if ((cp < 0x80 && *len > 1) || + (cp < 0x800 && *len > 2) || + (cp < 0x10000 && *len > 3)) { + return JSONFailure; + } - /* invalid unicode */ - if (cp > 0x10FFFF) { - return 0; - } + /* invalid unicode */ + if (cp > 0x10FFFF) { + return JSONFailure; + } - /* surrogate halves */ - if (cp >= 0xD800 && cp <= 0xDFFF) { - return 0; - } + /* surrogate halves */ + if (cp >= 0xD800 && cp <= 0xDFFF) { + return JSONFailure; + } - return 1; + return JSONSuccess; } static int is_valid_utf8(const char *string, size_t string_len) { - int len = 0; - const char *string_end = string + string_len; - while (string < string_end) { - if (!verify_utf8_sequence((const unsigned char*)string, &len)) { - return 0; + int len = 0; + const char *string_end = string + string_len; + while (string < string_end) { + if (verify_utf8_sequence((const unsigned char*)string, &len) != JSONSuccess) { + return PARSON_FALSE; + } + string += len; } - string += len; - } - return 1; + return PARSON_TRUE; } -static int is_decimal(const char *string, size_t length) { - if (length > 1 && string[0] == '0' && string[1] != '.') - return 0; - if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') - return 0; - while (length--) - if (strchr("xX", string[length])) - return 0; - return 1; -} -/* Remove for SGX Support - static char * read_file(const char * filename) { - FILE *fp = fopen(filename, "r"); - size_t file_size; - long pos; - char *file_contents; - if (!fp) - return NULL; - fseek(fp, 0L, SEEK_END); - pos = ftell(fp); - if (pos < 0) { - fclose(fp); - return NULL; - } - file_size = pos; - rewind(fp); - file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 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; - } - } - fclose(fp); - file_contents[file_size] = '\0'; - return file_contents; - } - */ +static parson_bool_t is_decimal(const char *string, size_t length) { + if (length > 1 && string[0] == '0' && string[1] != '.') { + return PARSON_FALSE; + } + if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { + return PARSON_FALSE; + } + while (length--) { + if (strchr("xX", string[length])) { + return PARSON_FALSE; + } + } + return PARSON_TRUE; +} -static void remove_comments(char *string, const char *start_token, const char *end_token) { - int in_string = 0, escaped = 0; - size_t i; - char *ptr = NULL, current_char; - size_t start_token_len = strlen(start_token); - size_t end_token_len = strlen(end_token); - if (start_token_len == 0 || end_token_len == 0) - return; - while ((current_char = *string) != '\0') { - if (current_char == '\\' && !escaped) { - escaped = 1; - string++; - continue; - } else if (current_char == '\"' && !escaped) { - in_string = !in_string; - } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { - for(i = 0; i < start_token_len; i++) - string[i] = ' '; - string = string + start_token_len; - ptr = (char*)strstr(string, end_token); - if (!ptr) - return; - for (i = 0; i < (ptr - string) + end_token_len; i++) - string[i] = ' '; - string = ptr + end_token_len - 1; +static unsigned long hash_string(const char *string, size_t n) { +#ifdef PARSON_FORCE_HASH_COLLISIONS + (void)string; + (void)n; + return 0; +#else + unsigned long hash = 5381; + unsigned char c; + size_t i = 0; + for (i = 0; i < n; i++) { + c = string[i]; + if (c == '\0') { + break; + } + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ } - escaped = 0; - string++; - } + return hash; +#endif } /* JSON Object */ -static JSON_Object * json_object_init(void) { - JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); - if (!new_obj) - return NULL; - new_obj->names = (char**)NULL; - new_obj->values = (JSON_Value**)NULL; - new_obj->capacity = 0; - new_obj->count = 0; - return new_obj; +static JSON_Object * json_object_make(JSON_Value *wrapping_value) { + JSON_Status res = JSONFailure; + JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); + if (new_obj == NULL) { + return NULL; + } + new_obj->wrapping_value = wrapping_value; + res = json_object_init(new_obj, 0); + if (res != JSONSuccess) { + parson_free(new_obj); + return NULL; + } + return new_obj; } -static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { - size_t index = 0; - if (object == NULL || name == NULL || value == NULL) { - return JSONFailure; - } - if (json_object_get_value(object, name) != NULL) { - return JSONFailure; - } - 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; - } - index = object->count; - object->names[index] = parson_strdup(name); - if (object->names[index] == NULL) +static JSON_Status json_object_init(JSON_Object *object, size_t capacity) { + unsigned int i = 0; + + object->cells = NULL; + object->names = NULL; + object->values = NULL; + object->cell_ixs = NULL; + object->hashes = NULL; + + object->count = 0; + object->cell_capacity = capacity; + object->item_capacity = (unsigned int)(capacity * 7/10); + + if (capacity == 0) { + return JSONSuccess; + } + + object->cells = (size_t*)parson_malloc(object->cell_capacity * sizeof(*object->cells)); + object->names = (char**)parson_malloc(object->item_capacity * sizeof(*object->names)); + object->values = (JSON_Value**)parson_malloc(object->item_capacity * sizeof(*object->values)); + object->cell_ixs = (size_t*)parson_malloc(object->item_capacity * sizeof(*object->cell_ixs)); + object->hashes = (unsigned long*)parson_malloc(object->item_capacity * sizeof(*object->hashes)); + if (object->cells == NULL + || object->names == NULL + || object->values == NULL + || object->cell_ixs == NULL + || object->hashes == NULL) { + goto error; + } + for (i = 0; i < object->cell_capacity; i++) { + object->cells[i] = OBJECT_INVALID_IX; + } + return JSONSuccess; +error: + parson_free(object->cells); + parson_free(object->names); + parson_free(object->values); + parson_free(object->cell_ixs); + parson_free(object->hashes); return JSONFailure; - object->values[index] = value; - object->count++; - return JSONSuccess; } -static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) { - char **temp_names = NULL; - JSON_Value **temp_values = NULL; +static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys, parson_bool_t free_values) { + unsigned int i = 0; + for (i = 0; i < object->count; i++) { + if (free_keys) { + parson_free(object->names[i]); + } + if (free_values) { + json_value_free(object->values[i]); + } + } - if ((object->names == NULL && object->values != NULL) || - (object->names != NULL && object->values == NULL) || - new_capacity == 0) { - return JSONFailure; /* Shouldn't happen */ - } + object->count = 0; + object->item_capacity = 0; + object->cell_capacity = 0; + + parson_free(object->cells); + parson_free(object->names); + parson_free(object->values); + parson_free(object->cell_ixs); + parson_free(object->hashes); + + object->cells = NULL; + object->names = NULL; + object->values = NULL; + object->cell_ixs = NULL; + object->hashes = NULL; +} + +static JSON_Status json_object_grow_and_rehash(JSON_Object *object) { + JSON_Value *wrapping_value = NULL; + JSON_Object new_object; + char *key = NULL; + JSON_Value *value = NULL; + unsigned int i = 0; + size_t new_capacity = MAX(object->cell_capacity * 2, STARTING_CAPACITY); + JSON_Status res = json_object_init(&new_object, new_capacity); + if (res != JSONSuccess) { + return JSONFailure; + } - temp_names = (char**)parson_malloc(new_capacity * sizeof(char*)); - if (temp_names == NULL) - return JSONFailure; + wrapping_value = json_object_get_wrapping_value(object); + new_object.wrapping_value = wrapping_value; - temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); - if (temp_values == NULL) { - parson_free(temp_names); - return JSONFailure; - } - - if (object->names != NULL && object->values != NULL && object->count > 0) { - memcpy(temp_names, object->names, object->count * sizeof(char*)); - memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*)); - } - parson_free(object->names); - parson_free(object->values); - object->names = temp_names; - object->values = temp_values; - object->capacity = new_capacity; - return JSONSuccess; -} - -static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) { - size_t i, name_length; - for (i = 0; i < json_object_get_count(object); i++) { - name_length = strlen(object->names[i]); - if (name_length != n) - continue; - if (strncmp(object->names[i], name, n) == 0) - return object->values[i]; - } - return NULL; + for (i = 0; i < object->count; i++) { + key = object->names[i]; + value = object->values[i]; + res = json_object_add(&new_object, key, value); + if (res != JSONSuccess) { + json_object_deinit(&new_object, PARSON_FALSE, PARSON_FALSE); + return JSONFailure; + } + value->parent = wrapping_value; + } + json_object_deinit(object, PARSON_FALSE, PARSON_FALSE); + *object = new_object; + return JSONSuccess; +} + +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) { + size_t cell_ix = hash & (object->cell_capacity - 1); + size_t cell = 0; + size_t ix = 0; + unsigned int i = 0; + unsigned long hash_to_check = 0; + const char *key_to_check = NULL; + size_t key_to_check_len = 0; + + *out_found = PARSON_FALSE; + + for (i = 0; i < object->cell_capacity; i++) { + ix = (cell_ix + i) & (object->cell_capacity - 1); + cell = object->cells[ix]; + if (cell == OBJECT_INVALID_IX) { + return ix; + } + hash_to_check = object->hashes[cell]; + if (hash != hash_to_check) { + continue; + } + key_to_check = object->names[cell]; + key_to_check_len = strlen(key_to_check); + if (key_to_check_len == key_len && strncmp(key, key_to_check, key_len) == 0) { + *out_found = PARSON_TRUE; + return ix; + } + } + return OBJECT_INVALID_IX; +} + +static JSON_Status json_object_add(JSON_Object *object, char *name, JSON_Value *value) { + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell_ix = 0; + JSON_Status res = JSONFailure; + + if (!object || !name || !value) { + return JSONFailure; + } + + hash = hash_string(name, strlen(name)); + found = PARSON_FALSE; + cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + if (found) { + return JSONFailure; + } + + if (object->count >= object->item_capacity) { + res = json_object_grow_and_rehash(object); + if (res != JSONSuccess) { + return JSONFailure; + } + cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + } + + object->names[object->count] = name; + object->cells[cell_ix] = object->count; + object->values[object->count] = value; + object->cell_ixs[object->count] = cell_ix; + object->hashes[object->count] = hash; + object->count++; + value->parent = json_object_get_wrapping_value(object); + + return JSONSuccess; +} + +static JSON_Value * json_object_getn_value(const JSON_Object *object, const char *name, size_t name_len) { + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell_ix = 0; + size_t item_ix = 0; + if (!object || !name) { + return NULL; + } + hash = hash_string(name, name_len); + found = PARSON_FALSE; + cell_ix = json_object_get_cell_ix(object, name, name_len, hash, &found); + if (!found) { + return NULL; + } + item_ix = object->cells[cell_ix]; + return object->values[item_ix]; +} + +static JSON_Status json_object_remove_internal(JSON_Object *object, const char *name, parson_bool_t free_value) { + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell = 0; + size_t item_ix = 0; + size_t last_item_ix = 0; + size_t i = 0; + size_t j = 0; + size_t x = 0; + size_t k = 0; + JSON_Value *val = NULL; + + if (object == NULL) { + return JSONFailure; + } + + hash = hash_string(name, strlen(name)); + found = PARSON_FALSE; + cell = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + if (!found) { + return JSONFailure; + } + + item_ix = object->cells[cell]; + if (free_value) { + val = object->values[item_ix]; + json_value_free(val); + val = NULL; + } + + parson_free(object->names[item_ix]); + last_item_ix = object->count - 1; + if (item_ix < last_item_ix) { + object->names[item_ix] = object->names[last_item_ix]; + object->values[item_ix] = object->values[last_item_ix]; + object->cell_ixs[item_ix] = object->cell_ixs[last_item_ix]; + object->hashes[item_ix] = object->hashes[last_item_ix]; + object->cells[object->cell_ixs[item_ix]] = item_ix; + } + object->count--; + + i = cell; + j = i; + for (x = 0; x < (object->cell_capacity - 1); x++) { + j = (j + 1) & (object->cell_capacity - 1); + if (object->cells[j] == OBJECT_INVALID_IX) { + break; + } + k = object->hashes[object->cells[j]] & (object->cell_capacity - 1); + if ((j > i && (k <= i || k > j)) + || (j < i && (k <= i && k > j))) { + object->cell_ixs[object->cells[j]] = i; + object->cells[i] = object->cells[j]; + i = j; + } + } + object->cells[i] = OBJECT_INVALID_IX; + return JSONSuccess; +} + +static JSON_Status json_object_dotremove_internal(JSON_Object *object, const char *name, parson_bool_t free_value) { + JSON_Value *temp_value = NULL; + JSON_Object *temp_object = NULL; + const char *dot_pos = strchr(name, '.'); + if (!dot_pos) { + return json_object_remove_internal(object, name, free_value); + } + temp_value = json_object_getn_value(object, name, dot_pos - name); + if (json_value_get_type(temp_value) != JSONObject) { + return JSONFailure; + } + temp_object = json_value_get_object(temp_value); + return json_object_dotremove_internal(temp_object, dot_pos + 1, free_value); } static void json_object_free(JSON_Object *object) { - while(object->count--) { - parson_free(object->names[object->count]); - json_value_free(object->values[object->count]); - } - parson_free(object->names); - parson_free(object->values); - parson_free(object); + json_object_deinit(object, PARSON_TRUE, PARSON_TRUE); + parson_free(object); } /* JSON Array */ -static JSON_Array * json_array_init(void) { - JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); - if (!new_array) - return NULL; - new_array->items = (JSON_Value**)NULL; - new_array->capacity = 0; - new_array->count = 0; - return new_array; +static JSON_Array * json_array_make(JSON_Value *wrapping_value) { + JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); + if (new_array == NULL) { + return NULL; + } + new_array->wrapping_value = wrapping_value; + new_array->items = (JSON_Value**)NULL; + new_array->capacity = 0; + new_array->count = 0; + return new_array; } 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; - } - array->items[array->count] = value; - array->count++; - return JSONSuccess; + if (array->count >= array->capacity) { + size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); + if (json_array_resize(array, new_capacity) != JSONSuccess) { + return JSONFailure; + } + } + value->parent = json_array_get_wrapping_value(array); + array->items[array->count] = value; + array->count++; + return JSONSuccess; } static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) { - JSON_Value **new_items = NULL; - if (new_capacity == 0) { - return JSONFailure; - } - new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); - if (new_items == NULL) { - return JSONFailure; - } - if (array->items != NULL && array->count > 0) { - memcpy(new_items, array->items, array->count * sizeof(JSON_Value*)); - } - parson_free(array->items); - array->items = new_items; - array->capacity = new_capacity; - return JSONSuccess; + JSON_Value **new_items = NULL; + if (new_capacity == 0) { + return JSONFailure; + } + new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); + if (new_items == NULL) { + return JSONFailure; + } + if (array->items != NULL && array->count > 0) { + memcpy(new_items, array->items, array->count * sizeof(JSON_Value*)); + } + parson_free(array->items); + array->items = new_items; + array->capacity = new_capacity; + return JSONSuccess; } static void json_array_free(JSON_Array *array) { - while (array->count--) - json_value_free(array->items[array->count]); - parson_free(array->items); - parson_free(array); + size_t i; + for (i = 0; i < array->count; i++) { + json_value_free(array->items[i]); + } + parson_free(array->items); + parson_free(array); } /* JSON Value */ -static JSON_Value * json_value_init_string_no_copy(char *string) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONString; - new_value->value.string = string; - return new_value; +static JSON_Value * json_value_init_string_no_copy(char *string, size_t length) { + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONString; + new_value->value.string.chars = string; + new_value->value.string.length = length; + return new_value; } /* Parser */ -static void skip_quotes(const char **string) { - SKIP_CHAR(string); - while (**string != '\"') { - if (**string == '\0') - return; - if (**string == '\\') { - SKIP_CHAR(string); - if (**string == '\0') - return; +static JSON_Status skip_quotes(const char **string) { + if (**string != '\"') { + return JSONFailure; + } + SKIP_CHAR(string); + while (**string != '\"') { + if (**string == '\0') { + return JSONFailure; + } else if (**string == '\\') { + SKIP_CHAR(string); + if (**string == '\0') { + return JSONFailure; + } + } + SKIP_CHAR(string); } SKIP_CHAR(string); - } - SKIP_CHAR(string); + return JSONSuccess; } -static int parse_utf_16(const char **unprocessed, char **processed) { - return JSONFailure; - // SGX removed due to missing sscanf support +static JSON_Status parse_utf16(const char **unprocessed, char **processed) { + unsigned int cp, lead, trail; + char *processed_ptr = *processed; + const char *unprocessed_ptr = *unprocessed; + JSON_Status status = JSONFailure; + unprocessed_ptr++; /* skips u */ + status = parse_utf16_hex(unprocessed_ptr, &cp); + if (status != JSONSuccess) { + return JSONFailure; + } + if (cp < 0x80) { + processed_ptr[0] = (char)cp; /* 0xxxxxxx */ + } else if (cp < 0x800) { + 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[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') { + return JSONFailure; + } + status = parse_utf16_hex(unprocessed_ptr, &trail); + if (status != JSONSuccess || 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; + } + unprocessed_ptr += 3; + *processed = processed_ptr; + *unprocessed = unprocessed_ptr; + return JSONSuccess; } /* Copies and processes passed string up to supplied length. Example: "\u006Corem ipsum" -> lorem ipsum */ -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; - while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) { - if (*input_ptr == '\\') { - input_ptr++; - switch (*input_ptr) { - case '\"': *output_ptr = '\"'; break; - case '\\': *output_ptr = '\\'; break; - case '/': *output_ptr = '/'; break; - case 'b': *output_ptr = '\b'; break; - case 'f': *output_ptr = '\f'; break; - case 'n': *output_ptr = '\n'; break; - case 'r': *output_ptr = '\r'; break; - case 't': *output_ptr = '\t'; break; - case 'u': - if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) - goto error; - break; - default: - goto error; - } - } else if ((unsigned char)*input_ptr < 0x20) { - goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ - } else { - *output_ptr = *input_ptr; - } - output_ptr++; - input_ptr++; - } - *output_ptr = '\0'; - /* resize to new length */ - final_size = (size_t)(output_ptr-output) + 1; - resized_output = (char*)parson_malloc(final_size); - if (resized_output == NULL) - goto error; - memcpy(resized_output, output, final_size); - parson_free(output); - return resized_output; +static char* process_string(const char *input, size_t input_len, size_t *output_len) { + const char *input_ptr = input; + size_t initial_size = (input_len + 1) * sizeof(char); + size_t final_size = 0; + 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) < input_len) { + if (*input_ptr == '\\') { + input_ptr++; + switch (*input_ptr) { + case '\"': *output_ptr = '\"'; break; + case '\\': *output_ptr = '\\'; break; + case '/': *output_ptr = '/'; break; + case 'b': *output_ptr = '\b'; break; + case 'f': *output_ptr = '\f'; break; + case 'n': *output_ptr = '\n'; break; + case 'r': *output_ptr = '\r'; break; + case 't': *output_ptr = '\t'; break; + case 'u': + if (parse_utf16(&input_ptr, &output_ptr) != JSONSuccess) { + goto error; + } + break; + default: + goto error; + } + } else if ((unsigned char)*input_ptr < 0x20) { + goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ + } else { + *output_ptr = *input_ptr; + } + output_ptr++; + input_ptr++; + } + *output_ptr = '\0'; + /* resize to new length */ + final_size = (size_t)(output_ptr-output) + 1; + /* todo: don't resize if final_size == initial_size */ + resized_output = (char*)parson_malloc(final_size); + if (resized_output == NULL) { + goto error; + } + memcpy(resized_output, output, final_size); + *output_len = final_size - 1; + parson_free(output); + return resized_output; error: - parson_free(output); - return NULL; + parson_free(output); + return NULL; } /* Return processed contents of a string between quotes and skips passed argument to a matching quote. */ -static char * get_quoted_string(const char **string) { - const char *string_start = *string; - size_t string_len = 0; - skip_quotes(string); - string_len = *string - string_start - 2; /* length without quotes */ - return process_string(string_start + 1, string_len); +static char * get_quoted_string(const char **string, size_t *output_string_len) { + const char *string_start = *string; + size_t input_string_len = 0; + JSON_Status status = skip_quotes(string); + if (status != JSONSuccess) { + return NULL; + } + input_string_len = *string - string_start - 2; /* length without quotes */ + return process_string(string_start + 1, input_string_len, output_string_len); } static JSON_Value * parse_value(const char **string, size_t nesting) { - if (nesting > MAX_NESTING) - return NULL; - SKIP_WHITESPACES(string); - switch (**string) { - case '{': - return parse_object_value(string, nesting + 1); - case '[': - return parse_array_value(string, nesting + 1); - case '\"': - return parse_string_value(string); - case 'f': case 't': - return parse_boolean_value(string); - case '-': - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - return parse_number_value(string); - case 'n': - return parse_null_value(string); - default: - return NULL; - } + if (nesting > MAX_NESTING) { + return NULL; + } + SKIP_WHITESPACES(string); + switch (**string) { + case '{': + return parse_object_value(string, nesting + 1); + case '[': + return parse_array_value(string, nesting + 1); + case '\"': + return parse_string_value(string); + case 'f': case 't': + return parse_boolean_value(string); + case '-': + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + return parse_number_value(string); + case 'n': + return parse_null_value(string); + default: + return NULL; + } } static JSON_Value * parse_object_value(const char **string, size_t nesting) { - JSON_Value *output_value = json_value_init_object(), *new_value = NULL; - JSON_Object *output_object = json_value_get_object(output_value); - char *new_key = NULL; - if (output_value == NULL) - return NULL; - SKIP_CHAR(string); - SKIP_WHITESPACES(string); - if (**string == '}') { /* empty object */ - SKIP_CHAR(string); - return output_value; - } - while (**string != '\0') { - new_key = get_quoted_string(string); - SKIP_WHITESPACES(string); - if (new_key == NULL || **string != ':') { - json_value_free(output_value); - return NULL; + JSON_Status status = JSONFailure; + JSON_Value *output_value = NULL, *new_value = NULL; + JSON_Object *output_object = NULL; + char *new_key = NULL; + + output_value = json_value_init_object(); + if (output_value == NULL) { + return NULL; + } + if (**string != '{') { + json_value_free(output_value); + return NULL; } + output_object = json_value_get_object(output_value); SKIP_CHAR(string); - new_value = parse_value(string, nesting); - if (new_value == NULL) { - parson_free(new_key); - json_value_free(output_value); - return NULL; + SKIP_WHITESPACES(string); + if (**string == '}') { /* empty object */ + SKIP_CHAR(string); + return output_value; } - if(json_object_add(output_object, new_key, new_value) == JSONFailure) { - parson_free(new_key); - parson_free(new_value); - json_value_free(output_value); - return NULL; + while (**string != '\0') { + size_t key_len = 0; + new_key = get_quoted_string(string, &key_len); + /* We do not support key names with embedded \0 chars */ + if (!new_key) { + json_value_free(output_value); + return NULL; + } + if (key_len != strlen(new_key)) { + parson_free(new_key); + json_value_free(output_value); + return NULL; + } + SKIP_WHITESPACES(string); + if (**string != ':') { + parson_free(new_key); + json_value_free(output_value); + return NULL; + } + SKIP_CHAR(string); + new_value = parse_value(string, nesting); + if (new_value == NULL) { + parson_free(new_key); + json_value_free(output_value); + return NULL; + } + status = json_object_add(output_object, new_key, new_value); + if (status != JSONSuccess) { + parson_free(new_key); + json_value_free(new_value); + json_value_free(output_value); + return NULL; + } + SKIP_WHITESPACES(string); + if (**string != ',') { + break; + } + SKIP_CHAR(string); + SKIP_WHITESPACES(string); + if (**string == '}') { + break; + } } - parson_free(new_key); SKIP_WHITESPACES(string); - if (**string != ',') - break; + if (**string != '}') { + json_value_free(output_value); + return NULL; + } SKIP_CHAR(string); - SKIP_WHITESPACES(string); - } - SKIP_WHITESPACES(string); - if (**string != '}' || /* Trim object after parsing is over */ - json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure) { - json_value_free(output_value); - return NULL; - } - SKIP_CHAR(string); - return output_value; + return output_value; } static JSON_Value * parse_array_value(const char **string, size_t nesting) { - JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL; - JSON_Array *output_array = json_value_get_array(output_value); - if (!output_value) - return NULL; - SKIP_CHAR(string); - SKIP_WHITESPACES(string); - if (**string == ']') { /* empty array */ + JSON_Value *output_value = NULL, *new_array_value = NULL; + JSON_Array *output_array = NULL; + output_value = json_value_init_array(); + if (output_value == NULL) { + return NULL; + } + if (**string != '[') { + json_value_free(output_value); + return NULL; + } + output_array = json_value_get_array(output_value); SKIP_CHAR(string); - return output_value; - } - while (**string != '\0') { - new_array_value = parse_value(string, nesting); - if (!new_array_value) { - 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(output_value); - return NULL; + SKIP_WHITESPACES(string); + if (**string == ']') { /* empty array */ + SKIP_CHAR(string); + return output_value; + } + while (**string != '\0') { + new_array_value = parse_value(string, nesting); + if (new_array_value == NULL) { + json_value_free(output_value); + return NULL; + } + if (json_array_add(output_array, new_array_value) != JSONSuccess) { + json_value_free(new_array_value); + json_value_free(output_value); + return NULL; + } + SKIP_WHITESPACES(string); + if (**string != ',') { + break; + } + SKIP_CHAR(string); + SKIP_WHITESPACES(string); + if (**string == ']') { + break; + } } SKIP_WHITESPACES(string); - if (**string != ',') - break; + if (**string != ']' || /* Trim array after parsing is over */ + json_array_resize(output_array, json_array_get_count(output_array)) != JSONSuccess) { + json_value_free(output_value); + return NULL; + } SKIP_CHAR(string); - SKIP_WHITESPACES(string); - } - SKIP_WHITESPACES(string); - if (**string != ']' || /* Trim array after parsing is over */ - json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) { - json_value_free(output_value); - return NULL; - } - SKIP_CHAR(string); - return output_value; + return output_value; } static JSON_Value * parse_string_value(const char **string) { - JSON_Value *value = NULL; - char *new_string = get_quoted_string(string); - if (new_string == NULL) - return NULL; - value = json_value_init_string_no_copy(new_string); - if (value == NULL) { - parson_free(new_string); - return NULL; - } - return value; + JSON_Value *value = NULL; + size_t new_string_len = 0; + char *new_string = get_quoted_string(string, &new_string_len); + if (new_string == NULL) { + return NULL; + } + value = json_value_init_string_no_copy(new_string, new_string_len); + if (value == NULL) { + parson_free(new_string); + return NULL; + } + return value; } static JSON_Value * parse_boolean_value(const char **string) { - size_t true_token_size = SIZEOF_TOKEN("true"); - size_t false_token_size = SIZEOF_TOKEN("false"); - if (strncmp("true", *string, true_token_size) == 0) { - *string += true_token_size; - return json_value_init_boolean(1); - } else if (strncmp("false", *string, false_token_size) == 0) { - *string += false_token_size; - return json_value_init_boolean(0); - } - return NULL; + size_t true_token_size = SIZEOF_TOKEN("true"); + size_t false_token_size = SIZEOF_TOKEN("false"); + if (strncmp("true", *string, true_token_size) == 0) { + *string += true_token_size; + return json_value_init_boolean(1); + } else if (strncmp("false", *string, false_token_size) == 0) { + *string += false_token_size; + return json_value_init_boolean(0); + } + return NULL; } static JSON_Value * parse_number_value(const char **string) { - char *end; - double number = strtod(*string, &end); - JSON_Value *output_value; - if (is_decimal(*string, end - *string)) { + char *end; + double number = 0; + errno = 0; + number = strtod(*string, &end); + if (errno == ERANGE && (number <= -HUGE_VAL || number >= HUGE_VAL)) { + return NULL; + } + if ((errno && errno != ERANGE) || !is_decimal(*string, end - *string)) { + return NULL; + } *string = end; - output_value = json_value_init_number(number); - } else { - output_value = NULL; - } - return output_value; + return json_value_init_number(number); } static JSON_Value * parse_null_value(const char **string) { - size_t token_size = SIZEOF_TOKEN("null"); - if (strncmp("null", *string, token_size) == 0) { - *string += token_size; - return json_value_init_null(); - } - return NULL; -} + size_t token_size = SIZEOF_TOKEN("null"); + if (strncmp("null", *string, token_size) == 0) { + *string += token_size; + return json_value_init_null(); + } + return NULL; +} /* Serialization */ -#define APPEND_STRING(str) do { written = append_string(buf, (str));\ - if (written < 0) { return -1; }\ - if (buf != NULL) { buf += written; }\ - written_total += written; } while(0) -#define APPEND_INDENT(level) do { written = append_indent(buf, (level));\ - if (written < 0) { return -1; }\ - if (buf != NULL) { buf += written; }\ - written_total += written; } while(0) - -static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf, size_t buf_length) +/* APPEND_STRING() is only called on string literals. + It's a bit hacky because it makes plenty of assumptions about the external state + and should eventually be tidied up into a function (same goes for APPEND_INDENT) + */ +#define APPEND_STRING(str) do {\ + written = SIZEOF_TOKEN((str));\ + if (buf != NULL) {\ + memcpy(buf, (str), written);\ + buf[written] = '\0';\ + buf += written;\ + }\ + written_total += written;\ + } while (0) + +#define APPEND_INDENT(level) do {\ + int level_i = 0;\ + for (level_i = 0; level_i < (level); level_i++) {\ + APPEND_STRING(PARSON_INDENT_STR);\ + }\ + } while (0) + +static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, parson_bool_t is_pretty, char *num_buf) { - const char *key = NULL, *string = NULL; - JSON_Value *temp_value = NULL; - JSON_Array *array = NULL; - JSON_Object *object = NULL; - size_t i = 0, count = 0; - double num = 0.0; - int written = -1, written_total = 0; - - switch (json_value_get_type(value)) { - case JSONArray: - array = json_value_get_array(value); - count = json_array_get_count(array); - APPEND_STRING("["); - if (count > 0 && is_pretty) - APPEND_STRING("\n"); - for (i = 0; i < count; i++) { - if (is_pretty) - APPEND_INDENT(level+1); - temp_value = json_array_get_value(array, i); - written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf, buf_length-written_total); - if (written < 0) - return -1; - if (buf != NULL) - buf += written; - written_total += written; - if (i < (count - 1)) - APPEND_STRING(","); - if (is_pretty) - APPEND_STRING("\n"); - } - if (count > 0 && is_pretty) - APPEND_INDENT(level); - APPEND_STRING("]"); - return written_total; - case JSONObject: - object = json_value_get_object(value); - count = json_object_get_count(object); - APPEND_STRING("{"); - if (count > 0 && is_pretty) - APPEND_STRING("\n"); - for (i = 0; i < count; i++) { - key = json_object_get_name(object, i); - if (is_pretty) - APPEND_INDENT(level+1); - written = json_serialize_string(key, buf); - if (written < 0) - return -1; - if (buf != NULL) - buf += written; - written_total += written; - APPEND_STRING(":"); - if (is_pretty) - APPEND_STRING(" "); - temp_value = json_object_get_value(object, key); - written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf, buf_length-written_total); - if (written < 0) - return -1; - if (buf != NULL) - buf += written; - written_total += written; - if (i < (count - 1)) - APPEND_STRING(","); - if (is_pretty) - APPEND_STRING("\n"); - } - if (count > 0 && is_pretty) - APPEND_INDENT(level); - APPEND_STRING("}"); - return written_total; - case JSONString: - string = json_value_get_string(value); - written = json_serialize_string(string, buf); - if (written < 0) - return -1; - if (buf != NULL) - buf += written; - written_total += written; - return written_total; - case JSONBoolean: - if (json_value_get_boolean(value)) - APPEND_STRING("true"); - else - APPEND_STRING("false"); - return written_total; - case JSONNumber: - num = json_value_get_number(value); - if (buf != NULL) - num_buf = buf; - else - buf_length = written_total + 100; - - if (num == ((double)(int)num)) /* check if num is integer */ - { - // SGX workaround for missing - written = snprintf(num_buf, buf_length-written_total, "%d", (int)num); - } - else - written = snprintf(num_buf, buf_length-written_total, DOUBLE_SERIALIZATION_FORMAT, num); - if (written < 0) - return -1; - if (buf != NULL) - buf += written; - written_total += written; - return written_total; - case JSONNull: - APPEND_STRING("null"); - return written_total; - case JSONError: - return -1; - default: - return -1; - } -} - -static int json_serialize_string(const char *string, char *buf) { - size_t i = 0, len = strlen(string); - char c = '\0'; - int written = -1, written_total = 0; - APPEND_STRING("\""); - for (i = 0; i < len; i++) { - c = string[i]; - switch (c) { - case '\"': APPEND_STRING("\\\""); break; - case '\\': APPEND_STRING("\\\\"); break; -#if PARSON_ENCODE_SLASH - case '/': APPEND_STRING("\\/"); break; /* to make json embeddable in xml\/html */ + const char *key = NULL, *string = NULL; + JSON_Value *temp_value = NULL; + JSON_Array *array = NULL; + JSON_Object *object = NULL; + size_t i = 0, count = 0; + double num = 0.0; + int written = -1, written_total = 0; + size_t len = 0; + + switch (json_value_get_type(value)) { + case JSONArray: + array = json_value_get_array(value); + count = json_array_get_count(array); + APPEND_STRING("["); + if (count > 0 && is_pretty) { + APPEND_STRING("\n"); + } + for (i = 0; i < count; i++) { + if (is_pretty) { + APPEND_INDENT(level+1); + } + temp_value = json_array_get_value(array, i); + written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + if (i < (count - 1)) { + APPEND_STRING(","); + } + if (is_pretty) { + APPEND_STRING("\n"); + } + } + if (count > 0 && is_pretty) { + APPEND_INDENT(level); + } + APPEND_STRING("]"); + return written_total; + case JSONObject: + object = json_value_get_object(value); + count = json_object_get_count(object); + APPEND_STRING("{"); + if (count > 0 && is_pretty) { + APPEND_STRING("\n"); + } + for (i = 0; i < count; i++) { + key = json_object_get_name(object, i); + if (key == NULL) { + return -1; + } + if (is_pretty) { + APPEND_INDENT(level+1); + } + /* We do not support key names with embedded \0 chars */ + written = json_serialize_string(key, strlen(key), buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + APPEND_STRING(":"); + if (is_pretty) { + APPEND_STRING(" "); + } + temp_value = json_object_get_value_at(object, i); + written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + if (i < (count - 1)) { + APPEND_STRING(","); + } + if (is_pretty) { + APPEND_STRING("\n"); + } + } + if (count > 0 && is_pretty) { + APPEND_INDENT(level); + } + APPEND_STRING("}"); + return written_total; + case JSONString: + string = json_value_get_string(value); + if (string == NULL) { + return -1; + } + len = json_value_get_string_len(value); + written = json_serialize_string(string, len, buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + return written_total; + case JSONBoolean: + if (json_value_get_boolean(value)) { + APPEND_STRING("true"); + } else { + APPEND_STRING("false"); + } + return written_total; + case JSONNumber: + num = json_value_get_number(value); + if (buf != NULL) { + num_buf = buf; + } + if (parson_number_serialization_function) { + written = parson_number_serialization_function(num, num_buf); + } else { + const char *float_format = parson_float_format ? parson_float_format : PARSON_DEFAULT_FLOAT_FORMAT; +#ifndef COMPILE_FOR_SGX + written = parson_sprintf(num_buf, float_format, num); +#else + written = parson_snprintf(num_buf, PARSON_NUM_BUF_SIZE, float_format, num); #endif - case '\b': APPEND_STRING("\\b"); break; - case '\f': APPEND_STRING("\\f"); break; - case '\n': APPEND_STRING("\\n"); break; - case '\r': APPEND_STRING("\\r"); break; - case '\t': APPEND_STRING("\\t"); break; - default: - if (buf != NULL) { - buf[0] = c; - buf += 1; - } - written_total += 1; - break; - } - } - APPEND_STRING("\""); - return written_total; -} - -static int append_indent(char *buf, int level) { - int i; - int written = -1, written_total = 0; - for (i = 0; i < level; i++) { - APPEND_STRING(" "); - } - return written_total; -} - -static int append_string(char *buf, const char *string) { - int len = (int)strlen(string); - if (buf == NULL) { - return len; - } - // SGX work around - strncpy(buf, string, len+1); - return len; - // return sprintf(buf, "%s", string); + } + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + return written_total; + case JSONNull: + APPEND_STRING("null"); + return written_total; + case JSONError: + return -1; + default: + return -1; + } +} + +static int json_serialize_string(const char *string, size_t len, char *buf) { + size_t i = 0; + char c = '\0'; + int written = -1, written_total = 0; + APPEND_STRING("\""); + for (i = 0; i < len; i++) { + c = string[i]; + switch (c) { + case '\"': APPEND_STRING("\\\""); break; + case '\\': APPEND_STRING("\\\\"); break; + case '\b': APPEND_STRING("\\b"); break; + case '\f': APPEND_STRING("\\f"); break; + case '\n': APPEND_STRING("\\n"); break; + case '\r': APPEND_STRING("\\r"); break; + case '\t': APPEND_STRING("\\t"); break; + case '\x00': APPEND_STRING("\\u0000"); break; + case '\x01': APPEND_STRING("\\u0001"); break; + case '\x02': APPEND_STRING("\\u0002"); break; + case '\x03': APPEND_STRING("\\u0003"); break; + case '\x04': APPEND_STRING("\\u0004"); break; + case '\x05': APPEND_STRING("\\u0005"); break; + case '\x06': APPEND_STRING("\\u0006"); break; + case '\x07': APPEND_STRING("\\u0007"); break; + /* '\x08' duplicate: '\b' */ + /* '\x09' duplicate: '\t' */ + /* '\x0a' duplicate: '\n' */ + case '\x0b': APPEND_STRING("\\u000b"); break; + /* '\x0c' duplicate: '\f' */ + /* '\x0d' duplicate: '\r' */ + case '\x0e': APPEND_STRING("\\u000e"); break; + case '\x0f': APPEND_STRING("\\u000f"); break; + case '\x10': APPEND_STRING("\\u0010"); break; + case '\x11': APPEND_STRING("\\u0011"); break; + case '\x12': APPEND_STRING("\\u0012"); break; + case '\x13': APPEND_STRING("\\u0013"); break; + case '\x14': APPEND_STRING("\\u0014"); break; + case '\x15': APPEND_STRING("\\u0015"); break; + case '\x16': APPEND_STRING("\\u0016"); break; + case '\x17': APPEND_STRING("\\u0017"); break; + case '\x18': APPEND_STRING("\\u0018"); break; + case '\x19': APPEND_STRING("\\u0019"); break; + case '\x1a': APPEND_STRING("\\u001a"); break; + case '\x1b': APPEND_STRING("\\u001b"); break; + case '\x1c': APPEND_STRING("\\u001c"); break; + case '\x1d': APPEND_STRING("\\u001d"); break; + case '\x1e': APPEND_STRING("\\u001e"); break; + case '\x1f': APPEND_STRING("\\u001f"); break; + case '/': + if (parson_escape_slashes) { + APPEND_STRING("\\/"); /* to make json embeddable in xml\/html */ + } else { + APPEND_STRING("/"); + } + break; + default: + if (buf != NULL) { + buf[0] = c; + buf += 1; + } + written_total += 1; + break; + } + } + APPEND_STRING("\""); + return written_total; } #undef APPEND_STRING #undef APPEND_INDENT /* Parser API */ -/* SGX Support - JSON_Value * json_parse_file(const char *filename) { - char *file_contents = read_file(filename); - JSON_Value *output_value = NULL; - if (file_contents == NULL) - return NULL; - output_value = json_parse_string(file_contents); - parson_free(file_contents); - return output_value; - } - - JSON_Value * json_parse_file_with_comments(const char *filename) { - char *file_contents = read_file(filename); - JSON_Value *output_value = NULL; - if (file_contents == NULL) - return NULL; - output_value = json_parse_string_with_comments(file_contents); - parson_free(file_contents); - return output_value; - } - */ +#ifndef COMPILE_FOR_SGX +JSON_Value * json_parse_file(const char *filename) { + char *file_contents = read_file(filename); + JSON_Value *output_value = NULL; + if (file_contents == NULL) { + return NULL; + } + output_value = json_parse_string(file_contents); + parson_free(file_contents); + return output_value; +} +#endif + +#ifndef COMPILE_FOR_SGX +JSON_Value * json_parse_file_with_comments(const char *filename) { + char *file_contents = read_file(filename); + JSON_Value *output_value = NULL; + if (file_contents == NULL) { + return NULL; + } + output_value = json_parse_string_with_comments(file_contents); + parson_free(file_contents); + return output_value; +} +#endif + JSON_Value * json_parse_string(const char *string) { - if (string == NULL) - return NULL; - return parse_value((const char**)&string, 0); + if (string == NULL) { + return NULL; + } + if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') { + string = string + 3; /* Support for UTF-8 BOM */ + } + return parse_value((const char**)&string, 0); } JSON_Value * json_parse_string_with_comments(const char *string) { - JSON_Value *result = NULL; - char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; - string_mutable_copy = parson_strdup(string); - if (string_mutable_copy == NULL) - return NULL; - remove_comments(string_mutable_copy, "/*", "*/"); - remove_comments(string_mutable_copy, "//", "\n"); - string_mutable_copy_ptr = string_mutable_copy; - result = parse_value((const char**)&string_mutable_copy_ptr, 0); - parson_free(string_mutable_copy); - return result; + JSON_Value *result = NULL; + char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; + string_mutable_copy = parson_strdup(string); + if (string_mutable_copy == NULL) { + return NULL; + } + remove_comments(string_mutable_copy, "/*", "*/"); + remove_comments(string_mutable_copy, "//", "\n"); + string_mutable_copy_ptr = string_mutable_copy; + result = parse_value((const char**)&string_mutable_copy_ptr, 0); + parson_free(string_mutable_copy); + return result; } /* JSON Object API */ JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { - if (object == NULL || name == NULL) - return NULL; - return json_object_nget_value(object, name, strlen(name)); + if (object == NULL || name == NULL) { + return NULL; + } + return json_object_getn_value(object, name, strlen(name)); } const char * json_object_get_string(const JSON_Object *object, const char *name) { - return json_value_get_string(json_object_get_value(object, name)); + return json_value_get_string(json_object_get_value(object, name)); +} + +size_t json_object_get_string_len(const JSON_Object *object, const char *name) { + return json_value_get_string_len(json_object_get_value(object, name)); } double json_object_get_number(const JSON_Object *object, const char *name) { - return json_value_get_number(json_object_get_value(object, name)); + return json_value_get_number(json_object_get_value(object, name)); } JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) { - return json_value_get_object(json_object_get_value(object, name)); + return json_value_get_object(json_object_get_value(object, name)); } JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) { - return json_value_get_array(json_object_get_value(object, name)); + return json_value_get_array(json_object_get_value(object, name)); } int json_object_get_boolean(const JSON_Object *object, const char *name) { - return json_value_get_boolean(json_object_get_value(object, name)); + return json_value_get_boolean(json_object_get_value(object, name)); } JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { - const char *dot_position = strchr(name, '.'); - if (!dot_position) - return json_object_get_value(object, name); - object = json_value_get_object(json_object_nget_value(object, name, dot_position - name)); - return json_object_dotget_value(object, dot_position + 1); + const char *dot_position = strchr(name, '.'); + if (!dot_position) { + return json_object_get_value(object, name); + } + object = json_value_get_object(json_object_getn_value(object, name, dot_position - name)); + return json_object_dotget_value(object, dot_position + 1); } const char * json_object_dotget_string(const JSON_Object *object, const char *name) { - return json_value_get_string(json_object_dotget_value(object, name)); + return json_value_get_string(json_object_dotget_value(object, name)); +} + +size_t json_object_dotget_string_len(const JSON_Object *object, const char *name) { + return json_value_get_string_len(json_object_dotget_value(object, name)); } double json_object_dotget_number(const JSON_Object *object, const char *name) { - return json_value_get_number(json_object_dotget_value(object, name)); + return json_value_get_number(json_object_dotget_value(object, name)); } JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) { - return json_value_get_object(json_object_dotget_value(object, name)); + return json_value_get_object(json_object_dotget_value(object, name)); } JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) { - return json_value_get_array(json_object_dotget_value(object, name)); + return json_value_get_array(json_object_dotget_value(object, name)); } int json_object_dotget_boolean(const JSON_Object *object, const char *name) { - return json_value_get_boolean(json_object_dotget_value(object, name)); + return json_value_get_boolean(json_object_dotget_value(object, name)); } size_t json_object_get_count(const JSON_Object *object) { - return object ? object->count : 0; + return object ? object->count : 0; } const char * json_object_get_name(const JSON_Object *object, size_t index) { - if (object == NULL || index >= json_object_get_count(object)) - return NULL; - return object->names[index]; + if (object == NULL || index >= json_object_get_count(object)) { + return NULL; + } + return object->names[index]; } JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) { - if (object == NULL || index >= json_object_get_count(object)) - return NULL; - return object->values[index]; + if (object == NULL || index >= json_object_get_count(object)) { + return NULL; + } + return object->values[index]; +} + +JSON_Value *json_object_get_wrapping_value(const JSON_Object *object) { + if (!object) { + return NULL; + } + return object->wrapping_value; +} + +int json_object_has_value (const JSON_Object *object, const char *name) { + return json_object_get_value(object, name) != NULL; +} + +int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { + JSON_Value *val = json_object_get_value(object, name); + return val != NULL && json_value_get_type(val) == type; +} + +int json_object_dothas_value (const JSON_Object *object, const char *name) { + return json_object_dotget_value(object, name) != NULL; +} + +int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { + JSON_Value *val = json_object_dotget_value(object, name); + return val != NULL && json_value_get_type(val) == type; } /* JSON Array API */ JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { - if (array == NULL || index >= json_array_get_count(array)) - return NULL; - return array->items[index]; + if (array == NULL || index >= json_array_get_count(array)) { + return NULL; + } + return array->items[index]; } const char * json_array_get_string(const JSON_Array *array, size_t index) { - return json_value_get_string(json_array_get_value(array, index)); + return json_value_get_string(json_array_get_value(array, index)); +} + +size_t json_array_get_string_len(const JSON_Array *array, size_t index) { + return json_value_get_string_len(json_array_get_value(array, index)); } double json_array_get_number(const JSON_Array *array, size_t index) { - return json_value_get_number(json_array_get_value(array, index)); + return json_value_get_number(json_array_get_value(array, index)); } JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) { - return json_value_get_object(json_array_get_value(array, index)); + return json_value_get_object(json_array_get_value(array, index)); } JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) { - return json_value_get_array(json_array_get_value(array, index)); + return json_value_get_array(json_array_get_value(array, index)); } int json_array_get_boolean(const JSON_Array *array, size_t index) { - return json_value_get_boolean(json_array_get_value(array, index)); + return json_value_get_boolean(json_array_get_value(array, index)); } size_t json_array_get_count(const JSON_Array *array) { - return array ? array->count : 0; + return array ? array->count : 0; +} + +JSON_Value * json_array_get_wrapping_value(const JSON_Array *array) { + if (!array) { + return NULL; + } + return array->wrapping_value; } /* JSON Value API */ JSON_Value_Type json_value_get_type(const JSON_Value *value) { - return value ? value->type : JSONError; + return value ? value->type : JSONError; } JSON_Object * json_value_get_object(const JSON_Value *value) { - return json_value_get_type(value) == JSONObject ? value->value.object : NULL; + return json_value_get_type(value) == JSONObject ? value->value.object : NULL; } JSON_Array * json_value_get_array(const JSON_Value *value) { - return json_value_get_type(value) == JSONArray ? value->value.array : NULL; + return json_value_get_type(value) == JSONArray ? value->value.array : NULL; +} + +static const JSON_String * json_value_get_string_desc(const JSON_Value *value) { + return json_value_get_type(value) == JSONString ? &value->value.string : NULL; } const char * json_value_get_string(const JSON_Value *value) { - return json_value_get_type(value) == JSONString ? value->value.string : NULL; + const JSON_String *str = json_value_get_string_desc(value); + return str ? str->chars : NULL; +} + +size_t json_value_get_string_len(const JSON_Value *value) { + const JSON_String *str = json_value_get_string_desc(value); + return str ? str->length : 0; } double json_value_get_number(const JSON_Value *value) { - return json_value_get_type(value) == JSONNumber ? value->value.number : 0; + return json_value_get_type(value) == JSONNumber ? value->value.number : 0; } int json_value_get_boolean(const JSON_Value *value) { - return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1; + return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1; +} + +JSON_Value * json_value_get_parent (const JSON_Value *value) { + return value ? value->parent : NULL; } void json_value_free(JSON_Value *value) { - switch (json_value_get_type(value)) { - case JSONObject: - json_object_free(value->value.object); - break; - case JSONString: - if (value->value.string) { parson_free(value->value.string); } - break; - case JSONArray: - json_array_free(value->value.array); - break; - default: - break; - } - parson_free(value); + switch (json_value_get_type(value)) { + case JSONObject: + json_object_free(value->value.object); + break; + case JSONString: + parson_free(value->value.string.chars); + break; + case JSONArray: + json_array_free(value->value.array); + break; + default: + break; + } + parson_free(value); } JSON_Value * json_value_init_object(void) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONObject; - new_value->value.object = json_object_init(); - if (!new_value->value.object) { - parson_free(new_value); - return NULL; - } - return new_value; + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONObject; + new_value->value.object = json_object_make(new_value); + if (!new_value->value.object) { + parson_free(new_value); + return NULL; + } + return new_value; } JSON_Value * json_value_init_array(void) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONArray; - new_value->value.array = json_array_init(); - if (!new_value->value.array) { - parson_free(new_value); - return NULL; - } - return new_value; + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONArray; + new_value->value.array = json_array_make(new_value); + if (!new_value->value.array) { + parson_free(new_value); + return NULL; + } + return new_value; } JSON_Value * json_value_init_string(const char *string) { - char *copy = NULL; - JSON_Value *value; - size_t string_len = 0; - if (string == NULL) - return NULL; - string_len = strlen(string); - if (!is_valid_utf8(string, string_len)) - return NULL; - copy = parson_strndup(string, string_len); - if (copy == NULL) - return NULL; - value = json_value_init_string_no_copy(copy); - if (value == NULL) - parson_free(copy); - return value; + if (string == NULL) { + return NULL; + } + return json_value_init_string_with_len(string, strlen(string)); +} + +JSON_Value * json_value_init_string_with_len(const char *string, size_t length) { + char *copy = NULL; + JSON_Value *value; + if (string == NULL) { + return NULL; + } + if (!is_valid_utf8(string, length)) { + return NULL; + } + copy = parson_strndup(string, length); + if (copy == NULL) { + return NULL; + } + value = json_value_init_string_no_copy(copy, length); + if (value == NULL) { + parson_free(copy); + } + return value; } JSON_Value * json_value_init_number(double number) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONNumber; - new_value->value.number = number; - return new_value; + JSON_Value *new_value = NULL; + if (IS_NUMBER_INVALID(number)) { + return NULL; + } + new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (new_value == NULL) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONNumber; + new_value->value.number = number; + return new_value; } JSON_Value * json_value_init_boolean(int boolean) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONBoolean; - new_value->value.boolean = boolean ? 1 : 0; - return new_value; + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONBoolean; + new_value->value.boolean = boolean ? 1 : 0; + return new_value; } JSON_Value * json_value_init_null(void) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONNull; - return new_value; + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONNull; + return new_value; } JSON_Value * json_value_deep_copy(const JSON_Value *value) { - size_t i = 0; - JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL; - const char *temp_string = NULL, *temp_key = NULL; - char *temp_string_copy = NULL; - JSON_Array *temp_array = NULL, *temp_array_copy = NULL; - JSON_Object *temp_object = NULL, *temp_object_copy = NULL; - - switch (json_value_get_type(value)) { - case JSONArray: - temp_array = json_value_get_array(value); - return_value = json_value_init_array(); - if (return_value == NULL) - return NULL; - temp_array_copy = json_value_get_array(return_value); - for (i = 0; i < json_array_get_count(temp_array); i++) { - temp_value = json_array_get_value(temp_array, i); - temp_value_copy = json_value_deep_copy(temp_value); - if (temp_value_copy == NULL) { - json_value_free(return_value); - return NULL; - } - if (json_array_add(temp_array_copy, temp_value_copy) == JSONFailure) { - json_value_free(return_value); - json_value_free(temp_value_copy); - return NULL; - } - } - return return_value; - case JSONObject: - temp_object = json_value_get_object(value); - return_value = json_value_init_object(); - if (return_value == NULL) - return NULL; - temp_object_copy = json_value_get_object(return_value); - for (i = 0; i < json_object_get_count(temp_object); i++) { - temp_key = json_object_get_name(temp_object, i); - temp_value = json_object_get_value(temp_object, temp_key); - temp_value_copy = json_value_deep_copy(temp_value); - if (temp_value_copy == NULL) { - json_value_free(return_value); - return NULL; - } - if (json_object_add(temp_object_copy, temp_key, temp_value_copy) == JSONFailure) { - json_value_free(return_value); - json_value_free(temp_value_copy); - return NULL; - } - } - return return_value; - case JSONBoolean: - return json_value_init_boolean(json_value_get_boolean(value)); - case JSONNumber: - return json_value_init_number(json_value_get_number(value)); - case JSONString: - temp_string = json_value_get_string(value); - temp_string_copy = parson_strdup(temp_string); - if (temp_string_copy == NULL) - return NULL; - return_value = json_value_init_string_no_copy(temp_string_copy); - if (return_value == NULL) - parson_free(temp_string_copy); - return return_value; - case JSONNull: - return json_value_init_null(); - case JSONError: - return NULL; - default: - return NULL; - } + size_t i = 0; + JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL; + const JSON_String *temp_string = NULL; + const char *temp_key = NULL; + char *temp_string_copy = NULL; + JSON_Array *temp_array = NULL, *temp_array_copy = NULL; + JSON_Object *temp_object = NULL, *temp_object_copy = NULL; + JSON_Status res = JSONFailure; + char *key_copy = NULL; + + switch (json_value_get_type(value)) { + case JSONArray: + temp_array = json_value_get_array(value); + return_value = json_value_init_array(); + if (return_value == NULL) { + return NULL; + } + temp_array_copy = json_value_get_array(return_value); + for (i = 0; i < json_array_get_count(temp_array); i++) { + temp_value = json_array_get_value(temp_array, i); + temp_value_copy = json_value_deep_copy(temp_value); + if (temp_value_copy == NULL) { + json_value_free(return_value); + return NULL; + } + if (json_array_add(temp_array_copy, temp_value_copy) != JSONSuccess) { + json_value_free(return_value); + json_value_free(temp_value_copy); + return NULL; + } + } + return return_value; + case JSONObject: + temp_object = json_value_get_object(value); + return_value = json_value_init_object(); + if (!return_value) { + return NULL; + } + temp_object_copy = json_value_get_object(return_value); + for (i = 0; i < json_object_get_count(temp_object); i++) { + temp_key = json_object_get_name(temp_object, i); + temp_value = json_object_get_value(temp_object, temp_key); + temp_value_copy = json_value_deep_copy(temp_value); + if (!temp_value_copy) { + json_value_free(return_value); + return NULL; + } + key_copy = parson_strdup(temp_key); + if (!key_copy) { + json_value_free(temp_value_copy); + json_value_free(return_value); + return NULL; + } + res = json_object_add(temp_object_copy, key_copy, temp_value_copy); + if (res != JSONSuccess) { + parson_free(key_copy); + json_value_free(temp_value_copy); + json_value_free(return_value); + return NULL; + } + } + return return_value; + case JSONBoolean: + return json_value_init_boolean(json_value_get_boolean(value)); + case JSONNumber: + return json_value_init_number(json_value_get_number(value)); + case JSONString: + temp_string = json_value_get_string_desc(value); + if (temp_string == NULL) { + return NULL; + } + temp_string_copy = parson_strndup(temp_string->chars, temp_string->length); + if (temp_string_copy == NULL) { + return NULL; + } + return_value = json_value_init_string_no_copy(temp_string_copy, temp_string->length); + if (return_value == NULL) { + parson_free(temp_string_copy); + } + return return_value; + case JSONNull: + return json_value_init_null(); + case JSONError: + return NULL; + default: + return NULL; + } } size_t json_serialization_size(const JSON_Value *value) { - char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ - int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf, 1100); - return res < 0 ? 0 : (size_t)(res + 1); + char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ + int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_FALSE, num_buf); + return res < 0 ? 0 : (size_t)(res) + 1; } JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { - int written = -1; - size_t needed_size_in_bytes = json_serialization_size(value); - if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { - return JSONFailure; - } - written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL, needed_size_in_bytes); - if (written < 0) - return JSONFailure; - return JSONSuccess; -} -/* removed for sgx support - JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) { - JSON_Status return_code = JSONSuccess; - FILE *fp = NULL; - char *serialized_string = json_serialize_to_string(value); - if (serialized_string == NULL) { - return JSONFailure; - } - fp = fopen (filename, "w"); - if (fp == NULL) { - json_free_serialized_string(serialized_string); - return JSONFailure; - } - if (fputs(serialized_string, fp) == EOF) { - return_code = JSONFailure; - } - if (fclose(fp) == EOF) { - return_code = JSONFailure; - } - json_free_serialized_string(serialized_string); - return return_code; - } - */ + int written = -1; + size_t needed_size_in_bytes = json_serialization_size(value); + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { + return JSONFailure; + } + written = json_serialize_to_buffer_r(value, buf, 0, PARSON_FALSE, NULL); + if (written < 0) { + return JSONFailure; + } + return JSONSuccess; +} + +#ifndef COMPILE_FOR_SGX +JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) { + JSON_Status return_code = JSONSuccess; + FILE *fp = NULL; + char *serialized_string = json_serialize_to_string(value); + if (serialized_string == NULL) { + return JSONFailure; + } + fp = fopen(filename, "w"); + if (fp == NULL) { + json_free_serialized_string(serialized_string); + return JSONFailure; + } + if (fputs(serialized_string, fp) == EOF) { + return_code = JSONFailure; + } + if (fclose(fp) == EOF) { + return_code = JSONFailure; + } + json_free_serialized_string(serialized_string); + return return_code; +} +#endif char * json_serialize_to_string(const JSON_Value *value) { - JSON_Status serialization_result = JSONFailure; - size_t buf_size_bytes = json_serialization_size(value); - char *buf = NULL; - if (buf_size_bytes == 0) { - return NULL; - } - buf = (char*)parson_malloc(buf_size_bytes); - if (buf == NULL) - return NULL; - serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); - if (serialization_result == JSONFailure) { - json_free_serialized_string(buf); - return NULL; - } - return buf; + JSON_Status serialization_result = JSONFailure; + size_t buf_size_bytes = json_serialization_size(value); + char *buf = NULL; + if (buf_size_bytes == 0) { + return NULL; + } + buf = (char*)parson_malloc(buf_size_bytes); + if (buf == NULL) { + return NULL; + } + serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); + if (serialization_result != JSONSuccess) { + json_free_serialized_string(buf); + return NULL; + } + return buf; } size_t json_serialization_size_pretty(const JSON_Value *value) { - char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ - int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf, 1100); - return res < 0 ? 0 : (size_t)(res + 1); + char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ + int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_TRUE, num_buf); + return res < 0 ? 0 : (size_t)(res) + 1; } JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { - int written = -1; - size_t needed_size_in_bytes = json_serialization_size_pretty(value); - if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) - return JSONFailure; - written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL, needed_size_in_bytes); - if (written < 0) - return JSONFailure; - return JSONSuccess; -} -/* SGX Support - JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) { - JSON_Status return_code = JSONSuccess; - FILE *fp = NULL; - char *serialized_string = json_serialize_to_string_pretty(value); - if (serialized_string == NULL) { - return JSONFailure; - } - fp = fopen (filename, "w"); - if (fp == NULL) { - json_free_serialized_string(serialized_string); - return JSONFailure; - } - if (fputs(serialized_string, fp) == EOF) { - return_code = JSONFailure; - } - if (fclose(fp) == EOF) { - return_code = JSONFailure; - } - json_free_serialized_string(serialized_string); - return return_code; - } - */ + int written = -1; + size_t needed_size_in_bytes = json_serialization_size_pretty(value); + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { + return JSONFailure; + } + written = json_serialize_to_buffer_r(value, buf, 0, PARSON_TRUE, NULL); + if (written < 0) { + return JSONFailure; + } + return JSONSuccess; +} + +#ifndef COMPILE_FOR_SGX +JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) { + JSON_Status return_code = JSONSuccess; + FILE *fp = NULL; + char *serialized_string = json_serialize_to_string_pretty(value); + if (serialized_string == NULL) { + return JSONFailure; + } + fp = fopen(filename, "w"); + if (fp == NULL) { + json_free_serialized_string(serialized_string); + return JSONFailure; + } + if (fputs(serialized_string, fp) == EOF) { + return_code = JSONFailure; + } + if (fclose(fp) == EOF) { + return_code = JSONFailure; + } + json_free_serialized_string(serialized_string); + return return_code; +} +#endif char * json_serialize_to_string_pretty(const JSON_Value *value) { - JSON_Status serialization_result = JSONFailure; - size_t buf_size_bytes = json_serialization_size_pretty(value); - char *buf = NULL; - if (buf_size_bytes == 0) { - return NULL; - } - buf = (char*)parson_malloc(buf_size_bytes); - if (buf == NULL) - return NULL; - serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); - if (serialization_result == JSONFailure) { - json_free_serialized_string(buf); - return NULL; - } - return buf; + JSON_Status serialization_result = JSONFailure; + size_t buf_size_bytes = json_serialization_size_pretty(value); + char *buf = NULL; + if (buf_size_bytes == 0) { + return NULL; + } + buf = (char*)parson_malloc(buf_size_bytes); + if (buf == NULL) { + return NULL; + } + serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); + if (serialization_result != JSONSuccess) { + json_free_serialized_string(buf); + return NULL; + } + return buf; } void json_free_serialized_string(char *string) { - parson_free(string); + parson_free(string); } JSON_Status json_array_remove(JSON_Array *array, size_t ix) { - JSON_Value *temp_value = NULL; - size_t last_element_ix = 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; + size_t to_move_bytes = 0; + if (array == NULL || ix >= json_array_get_count(array)) { + return JSONFailure; } - array->items[ix] = temp_value; - } - array->count -= 1; - return JSONSuccess; + json_value_free(json_array_get_value(array, ix)); + 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; } JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) { - if (array == NULL || value == NULL || ix >= json_array_get_count(array)) { - return JSONFailure; - } - json_value_free(json_array_get_value(array, ix)); - array->items[ix] = value; - return JSONSuccess; + if (array == NULL || value == NULL || value->parent != NULL || ix >= json_array_get_count(array)) { + return JSONFailure; + } + json_value_free(json_array_get_value(array, ix)); + value->parent = json_array_get_wrapping_value(array); + array->items[ix] = value; + return JSONSuccess; } JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { - JSON_Value *value = json_value_init_string(string); - if (value == NULL) - return JSONFailure; - if (json_array_replace_value(array, i, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_string(string); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, const char *string, size_t len) { + JSON_Value *value = json_value_init_string_with_len(string, len); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { - JSON_Value *value = json_value_init_number(number); - if (value == NULL) - return JSONFailure; - if (json_array_replace_value(array, i, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_number(number); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { - JSON_Value *value = json_value_init_boolean(boolean); - if (value == NULL) - return JSONFailure; - if (json_array_replace_value(array, i, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { - JSON_Value *value = json_value_init_null(); - if (value == NULL) - return JSONFailure; - if (json_array_replace_value(array, i, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_null(); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_clear(JSON_Array *array) { - size_t i = 0; - if (array == NULL) - return JSONFailure; - for (i = 0; i < json_array_get_count(array); i++) { - json_value_free(json_array_get_value(array, i)); - } - array->count = 0; - return JSONSuccess; + size_t i = 0; + if (array == NULL) { + return JSONFailure; + } + for (i = 0; i < json_array_get_count(array); i++) { + json_value_free(json_array_get_value(array, i)); + } + array->count = 0; + return JSONSuccess; } JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) { - if (array == NULL || value == NULL) - return JSONFailure; - return json_array_add(array, value); + if (array == NULL || value == NULL || value->parent != NULL) { + return JSONFailure; + } + return json_array_add(array, value); } JSON_Status json_array_append_string(JSON_Array *array, const char *string) { - JSON_Value *value = json_value_init_string(string); - if (value == NULL) - return JSONFailure; - if (json_array_append_value(array, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_string(string); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_string_with_len(JSON_Array *array, const char *string, size_t len) { + JSON_Value *value = json_value_init_string_with_len(string, len); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_append_number(JSON_Array *array, double number) { - JSON_Value *value = json_value_init_number(number); - if (value == NULL) - return JSONFailure; - if (json_array_append_value(array, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_number(number); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { - JSON_Value *value = json_value_init_boolean(boolean); - if (value == NULL) - return JSONFailure; - if (json_array_append_value(array, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_array_append_null(JSON_Array *array) { - JSON_Value *value = json_value_init_null(); - if (value == NULL) - return JSONFailure; - if (json_array_append_value(array, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_null(); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) { - size_t i = 0; - JSON_Value *old_value; - if (object == NULL || name == NULL || value == NULL) - return JSONFailure; - old_value = json_object_get_value(object, name); - if (old_value != NULL) { /* free and overwrite old value */ - json_value_free(old_value); - for (i = 0; i < json_object_get_count(object); i++) { - if (strcmp(object->names[i], name) == 0) { - object->values[i] = value; + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell_ix = 0; + size_t item_ix = 0; + JSON_Value *old_value = NULL; + char *key_copy = NULL; + + if (!object || !name || !value || value->parent) { + return JSONFailure; + } + hash = hash_string(name, strlen(name)); + found = PARSON_FALSE; + cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + if (found) { + item_ix = object->cells[cell_ix]; + old_value = object->values[item_ix]; + json_value_free(old_value); + object->values[item_ix] = value; + value->parent = json_object_get_wrapping_value(object); return JSONSuccess; - } } - } - /* add new key value pair */ - return json_object_add(object, name, value); + if (object->count >= object->item_capacity) { + JSON_Status res = json_object_grow_and_rehash(object); + if (res != JSONSuccess) { + return JSONFailure; + } + cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + } + key_copy = parson_strdup(name); + if (!key_copy) { + return JSONFailure; + } + object->names[object->count] = key_copy; + object->cells[cell_ix] = object->count; + object->values[object->count] = value; + object->cell_ixs[object->count] = cell_ix; + object->hashes[object->count] = hash; + object->count++; + value->parent = json_object_get_wrapping_value(object); + return JSONSuccess; } JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) { - return json_object_set_value(object, name, json_value_init_string(string)); + JSON_Value *value = json_value_init_string(string); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; +} + +JSON_Status json_object_set_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len) { + JSON_Value *value = json_value_init_string_with_len(string, len); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; } JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) { - return json_object_set_value(object, name, json_value_init_number(number)); + JSON_Value *value = json_value_init_number(number); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; } JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) { - return json_object_set_value(object, name, json_value_init_boolean(boolean)); + JSON_Value *value = json_value_init_boolean(boolean); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; } JSON_Status json_object_set_null(JSON_Object *object, const char *name) { - return json_object_set_value(object, name, json_value_init_null()); + JSON_Value *value = json_value_init_null(); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; } JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) { - const char *dot_pos = NULL; - char *current_name = NULL; - JSON_Object *temp_obj = NULL; - JSON_Value *new_value = NULL; - if (value == NULL || name == NULL || value == NULL) - return JSONFailure; - dot_pos = strchr(name, '.'); - if (dot_pos == NULL) { - return json_object_set_value(object, name, value); - } else { - current_name = parson_strndup(name, dot_pos - name); - temp_obj = json_object_get_object(object, current_name); - if (temp_obj == NULL) { - new_value = json_value_init_object(); - if (new_value == NULL) { - parson_free(current_name); - return JSONFailure; - } - if (json_object_add(object, current_name, new_value) == JSONFailure) { + const char *dot_pos = NULL; + JSON_Value *temp_value = NULL, *new_value = NULL; + JSON_Object *temp_object = NULL, *new_object = NULL; + JSON_Status status = JSONFailure; + size_t name_len = 0; + char *name_copy = NULL; + + if (object == NULL || name == NULL || value == NULL) { + return JSONFailure; + } + dot_pos = strchr(name, '.'); + if (dot_pos == NULL) { + return json_object_set_value(object, name, value); + } + name_len = dot_pos - name; + temp_value = json_object_getn_value(object, name, name_len); + if (temp_value) { + /* Don't overwrite existing non-object (unlike json_object_set_value, but it shouldn't be changed at this point) */ + if (json_value_get_type(temp_value) != JSONObject) { + return JSONFailure; + } + temp_object = json_value_get_object(temp_value); + return json_object_dotset_value(temp_object, dot_pos + 1, value); + } + new_value = json_value_init_object(); + if (new_value == NULL) { + return JSONFailure; + } + new_object = json_value_get_object(new_value); + status = json_object_dotset_value(new_object, dot_pos + 1, value); + if (status != JSONSuccess) { + json_value_free(new_value); + return JSONFailure; + } + name_copy = parson_strndup(name, name_len); + if (!name_copy) { + json_object_dotremove_internal(new_object, dot_pos + 1, 0); json_value_free(new_value); - parson_free(current_name); return JSONFailure; - } - temp_obj = json_object_get_object(object, current_name); } - parson_free(current_name); - return json_object_dotset_value(temp_obj, dot_pos + 1, value); - } + status = json_object_add(object, name_copy, new_value); + if (status != JSONSuccess) { + parson_free(name_copy); + json_object_dotremove_internal(new_object, dot_pos + 1, 0); + json_value_free(new_value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { - JSON_Value *value = json_value_init_string(string); - if (value == NULL) - return JSONFailure; - if (json_object_dotset_value(object, name, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_string(string); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len) { + JSON_Value *value = json_value_init_string_with_len(string, len); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { - JSON_Value *value = json_value_init_number(number); - if (value == NULL) - return JSONFailure; - if (json_object_dotset_value(object, name, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_number(number); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { - JSON_Value *value = json_value_init_boolean(boolean); - if (value == NULL) - return JSONFailure; - if (json_object_dotset_value(object, name, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { - JSON_Value *value = json_value_init_null(); - if (value == NULL) - return JSONFailure; - if (json_object_dotset_value(object, name, value) == JSONFailure) { - json_value_free(value); - return JSONFailure; - } - return JSONSuccess; + JSON_Value *value = json_value_init_null(); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; } JSON_Status json_object_remove(JSON_Object *object, const char *name) { - size_t i = 0, last_item_index = 0; - if (object == NULL || json_object_get_value(object, name) == NULL) - return JSONFailure; - last_item_index = json_object_get_count(object) - 1; - for (i = 0; i < json_object_get_count(object); i++) { - if (strcmp(object->names[i], name) == 0) { - parson_free(object->names[i]); - json_value_free(object->values[i]); - if (i != last_item_index) { /* Replace key value pair with one from the end */ - object->names[i] = object->names[last_item_index]; - object->values[i] = object->values[last_item_index]; - } - object->count -= 1; - return JSONSuccess; - } - } - return JSONFailure; /* No execution path should end here */ + return json_object_remove_internal(object, name, PARSON_TRUE); } JSON_Status json_object_dotremove(JSON_Object *object, const char *name) { - const char *dot_pos = strchr(name, '.'); - char *current_name = NULL; - JSON_Object *temp_obj = NULL; - if (dot_pos == NULL) { - return json_object_remove(object, name); - } else { - current_name = parson_strndup(name, dot_pos - name); - temp_obj = json_object_get_object(object, 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); - } + return json_object_dotremove_internal(object, name, PARSON_TRUE); } JSON_Status json_object_clear(JSON_Object *object) { - size_t i = 0; - if (object == NULL) { - return JSONFailure; - } - for (i = 0; i < json_object_get_count(object); i++) { - parson_free(object->names[i]); - json_value_free(object->values[i]); - } - object->count = 0; - return JSONSuccess; + size_t i = 0; + if (object == NULL) { + return JSONFailure; + } + for (i = 0; i < json_object_get_count(object); i++) { + parson_free(object->names[i]); + object->names[i] = NULL; + + json_value_free(object->values[i]); + object->values[i] = NULL; + } + object->count = 0; + for (i = 0; i < object->cell_capacity; i++) { + object->cells[i] = OBJECT_INVALID_IX; + } + return JSONSuccess; } JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { - JSON_Value *temp_schema_value = NULL, *temp_value = NULL; - JSON_Array *schema_array = NULL, *value_array = NULL; - JSON_Object *schema_object = NULL, *value_object = NULL; - JSON_Value_Type schema_type = JSONError, value_type = JSONError; - const char *key = NULL; - size_t i = 0, count = 0; - if (schema == NULL || value == NULL) - return JSONFailure; - schema_type = json_value_get_type(schema); - value_type = json_value_get_type(value); - if (schema_type != value_type && schema_type != JSONNull) /* null represents all values */ - return JSONFailure; - switch (schema_type) { - case JSONArray: - schema_array = json_value_get_array(schema); - value_array = json_value_get_array(value); - count = json_array_get_count(schema_array); - if (count == 0) - return JSONSuccess; /* Empty array allows all types */ - /* Get first value from array, rest is ignored */ - temp_schema_value = json_array_get_value(schema_array, 0); - for (i = 0; i < json_array_get_count(value_array); i++) { - temp_value = json_array_get_value(value_array, i); - if (json_validate(temp_schema_value, temp_value) == JSONFailure) { - return JSONFailure; - } - } - return JSONSuccess; - case JSONObject: - schema_object = json_value_get_object(schema); - value_object = json_value_get_object(value); - count = json_object_get_count(schema_object); - if (count == 0) - return JSONSuccess; /* Empty object allows all objects */ - else if (json_object_get_count(value_object) < count) - return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ - for (i = 0; i < count; i++) { - key = json_object_get_name(schema_object, i); - temp_schema_value = json_object_get_value(schema_object, key); - temp_value = json_object_get_value(value_object, key); - if (temp_value == NULL) - return JSONFailure; - if (json_validate(temp_schema_value, temp_value) == JSONFailure) - return JSONFailure; - } - return JSONSuccess; - case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: - return JSONSuccess; /* equality already tested before switch */ - case JSONError: default: - return JSONFailure; - } -} - -JSON_Status 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; - const char *key = NULL; - size_t a_count = 0, b_count = 0, i = 0; - JSON_Value_Type a_type, b_type; - a_type = json_value_get_type(a); - b_type = json_value_get_type(b); - if (a_type != b_type) { - return 0; - } - switch (a_type) { - case JSONArray: - a_array = json_value_get_array(a); - b_array = json_value_get_array(b); - a_count = json_array_get_count(a_array); - b_count = json_array_get_count(b_array); - if (a_count != b_count) { - return 0; - } - for (i = 0; i < a_count; i++) { - if (!json_value_equals(json_array_get_value(a_array, i), - json_array_get_value(b_array, i))) { - return 0; - } - } - return 1; - case JSONObject: - a_object = json_value_get_object(a); - b_object = json_value_get_object(b); - a_count = json_object_get_count(a_object); - b_count = json_object_get_count(b_object); - if (a_count != b_count) { - return 0; - } - for (i = 0; i < a_count; i++) { - key = json_object_get_name(a_object, i); - if (!json_value_equals(json_object_get_value(a_object, key), - json_object_get_value(b_object, key))) { - return 0; - } - } - return 1; - case JSONString: - a_string = json_value_get_string(a); - b_string = json_value_get_string(b); - return strcmp(a_string, b_string) == 0; - case JSONBoolean: - return json_value_get_boolean(a) == json_value_get_boolean(b); - case JSONNumber: - return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */ - case JSONError: - return 1; - case JSONNull: - return 1; - default: - return 1; - } + JSON_Value *temp_schema_value = NULL, *temp_value = NULL; + JSON_Array *schema_array = NULL, *value_array = NULL; + JSON_Object *schema_object = NULL, *value_object = NULL; + JSON_Value_Type schema_type = JSONError, value_type = JSONError; + const char *key = NULL; + size_t i = 0, count = 0; + if (schema == NULL || value == NULL) { + return JSONFailure; + } + schema_type = json_value_get_type(schema); + value_type = json_value_get_type(value); + if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */ + return JSONFailure; + } + switch (schema_type) { + case JSONArray: + schema_array = json_value_get_array(schema); + value_array = json_value_get_array(value); + count = json_array_get_count(schema_array); + if (count == 0) { + return JSONSuccess; /* Empty array allows all types */ + } + /* Get first value from array, rest is ignored */ + temp_schema_value = json_array_get_value(schema_array, 0); + for (i = 0; i < json_array_get_count(value_array); i++) { + temp_value = json_array_get_value(value_array, i); + if (json_validate(temp_schema_value, temp_value) != JSONSuccess) { + return JSONFailure; + } + } + return JSONSuccess; + case JSONObject: + schema_object = json_value_get_object(schema); + value_object = json_value_get_object(value); + count = json_object_get_count(schema_object); + if (count == 0) { + return JSONSuccess; /* Empty object allows all objects */ + } else if (json_object_get_count(value_object) < count) { + return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ + } + for (i = 0; i < count; i++) { + key = json_object_get_name(schema_object, i); + temp_schema_value = json_object_get_value(schema_object, key); + temp_value = json_object_get_value(value_object, key); + if (temp_value == NULL) { + return JSONFailure; + } + if (json_validate(temp_schema_value, temp_value) != JSONSuccess) { + return JSONFailure; + } + } + return JSONSuccess; + case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: + return JSONSuccess; /* equality already tested before switch */ + case JSONError: default: + return JSONFailure; + } +} + +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 JSON_String *a_string = NULL, *b_string = NULL; + const char *key = NULL; + size_t a_count = 0, b_count = 0, i = 0; + JSON_Value_Type a_type, b_type; + a_type = json_value_get_type(a); + b_type = json_value_get_type(b); + if (a_type != b_type) { + return PARSON_FALSE; + } + switch (a_type) { + case JSONArray: + a_array = json_value_get_array(a); + b_array = json_value_get_array(b); + a_count = json_array_get_count(a_array); + b_count = json_array_get_count(b_array); + if (a_count != b_count) { + return PARSON_FALSE; + } + for (i = 0; i < a_count; i++) { + if (!json_value_equals(json_array_get_value(a_array, i), + json_array_get_value(b_array, i))) { + return PARSON_FALSE; + } + } + return PARSON_TRUE; + case JSONObject: + a_object = json_value_get_object(a); + b_object = json_value_get_object(b); + a_count = json_object_get_count(a_object); + b_count = json_object_get_count(b_object); + if (a_count != b_count) { + return PARSON_FALSE; + } + for (i = 0; i < a_count; i++) { + key = json_object_get_name(a_object, i); + if (!json_value_equals(json_object_get_value(a_object, key), + json_object_get_value(b_object, key))) { + return PARSON_FALSE; + } + } + return PARSON_TRUE; + case JSONString: + a_string = json_value_get_string_desc(a); + b_string = json_value_get_string_desc(b); + if (a_string == NULL || b_string == NULL) { + return PARSON_FALSE; /* shouldn't happen */ + } + return a_string->length == b_string->length && + memcmp(a_string->chars, b_string->chars, a_string->length) == 0; + case JSONBoolean: + return json_value_get_boolean(a) == json_value_get_boolean(b); + case JSONNumber: + return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */ + case JSONError: + return PARSON_TRUE; + case JSONNull: + return PARSON_TRUE; + default: + return PARSON_TRUE; + } } JSON_Value_Type json_type(const JSON_Value *value) { - return json_value_get_type(value); + return json_value_get_type(value); } JSON_Object * json_object (const JSON_Value *value) { - return json_value_get_object(value); + return json_value_get_object(value); } -JSON_Array * json_array (const JSON_Value *value) { - return json_value_get_array(value); +JSON_Array * json_array(const JSON_Value *value) { + return json_value_get_array(value); } -const char * json_string (const JSON_Value *value) { - return json_value_get_string(value); +const char * json_string(const JSON_Value *value) { + return json_value_get_string(value); } -double json_number (const JSON_Value *value) { - return json_value_get_number(value); +size_t json_string_len(const JSON_Value *value) { + return json_value_get_string_len(value); +} + +double json_number(const JSON_Value *value) { + return json_value_get_number(value); } int json_boolean(const JSON_Value *value) { - return json_value_get_boolean(value); + return json_value_get_boolean(value); } void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) { - parson_malloc = malloc_fun; - parson_free = free_fun; + parson_malloc = malloc_fun; + parson_free = free_fun; +} + +void json_set_escape_slashes(int escape_slashes) { + parson_escape_slashes = escape_slashes; +} + +void json_set_float_serialization_format(const char *format) { + if (parson_float_format) { + parson_free(parson_float_format); + parson_float_format = NULL; + } + if (!format) { + parson_float_format = NULL; + return; + } + parson_float_format = parson_strdup(format); +} + +void json_set_number_serialization_function(JSON_Number_Serialization_Function func) { + parson_number_serialization_function = func; } diff --git a/common/packages/parson/parson.h b/common/packages/parson/parson.h index fe177d54..9f6e7538 100644 --- a/common/packages/parson/parson.h +++ b/common/packages/parson/parson.h @@ -1,42 +1,58 @@ /* - Parson ( http://kgabis.github.com/parson/ ) - Copyright (c) 2012 - 2016 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 - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - */ + SPDX-License-Identifier: MIT + + Parson 1.5.3 (https://github.com/kgabis/parson) + Copyright (c) 2012 - 2023 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 + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +// NOTE: This file has been modified from the original to support execution +// in SGX and PDO smart contracts. #ifndef parson_parson_h #define parson_parson_h +#define COMPILE_FOR_SGX 1 + #ifdef __cplusplus extern "C" { #endif +#if 0 +} /* unconfuse xcode */ +#endif + +#define PARSON_VERSION_MAJOR 1 +#define PARSON_VERSION_MINOR 5 +#define PARSON_VERSION_PATCH 3 + +#define PARSON_VERSION_STRING "1.5.3" #include /* size_t */ - /* Types and enums */ - typedef struct json_object_t JSON_Object; - typedef struct json_array_t JSON_Array; - typedef struct json_value_t JSON_Value; +/* Types and enums */ +typedef struct json_object_t JSON_Object; +typedef struct json_array_t JSON_Array; +typedef struct json_value_t JSON_Value; - enum json_value_type { +enum json_value_type { JSONError = -1, JSONNull = 1, JSONString = 2, @@ -44,177 +60,225 @@ extern "C" JSONObject = 4, JSONArray = 5, JSONBoolean = 6 - }; - typedef int JSON_Value_Type; +}; +typedef int JSON_Value_Type; - enum json_result_t { +enum json_result_t { JSONSuccess = 0, JSONFailure = -1 - }; - typedef int JSON_Status; - - typedef void * (*JSON_Malloc_Function)(size_t); - typedef void (*JSON_Free_Function)(void *); - - /* Call only once, before calling any other function from parson API. If not called, malloc and free - from stdlib will be used for all allocations */ - void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); - - /* Parses first JSON value in a file, returns NULL in case of error */ - JSON_Value * json_parse_file(const char *filename); - - /* Parses first JSON value in a file and ignores comments (/ * * / and //), - returns NULL in case of error */ - JSON_Value * json_parse_file_with_comments(const char *filename); - - /* Parses first JSON value in a string, returns NULL in case of error */ - JSON_Value * json_parse_string(const char *string); - - /* Parses first JSON value in a string and ignores comments (/ * * / and //), - returns NULL in case of error */ - JSON_Value * json_parse_string_with_comments(const char *string); - - /* Serialization */ - size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ - JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); - JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); - char * json_serialize_to_string(const JSON_Value *value); - - /* Pretty serialization */ - size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ - JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); - JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); - char * json_serialize_to_string_pretty(const JSON_Value *value); - - void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ - - /* Comparing */ - int json_value_equals(const JSON_Value *a, const JSON_Value *b); - - /* Validation - This is *NOT* JSON Schema. It validates json by checking if object have identically - named fields with matching types. - For example schema {"name":"", "age":0} will validate - {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, - but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. - In case of arrays, only first value in schema is checked against all values in tested array. - Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, - null validates values of every type. - */ - JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); - - /* - * JSON Object - */ - JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); - const char * json_object_get_string (const JSON_Object *object, const char *name); - JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); - JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); - double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ - int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ - - /* dotget functions enable addressing values with dot notation in nested objects, - just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). - Because valid names in JSON can contain dots, some values may be inaccessible - this way. */ - JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); - const char * json_object_dotget_string (const JSON_Object *object, const char *name); - JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); - JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); - double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ - int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ - - /* Functions to get available names */ - size_t json_object_get_count (const JSON_Object *object); - const char * json_object_get_name (const JSON_Object *object, size_t index); - JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index); - - /* Creates new name-value pair or frees and replaces old value with a new one. - * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ - JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); - JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); - JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); - JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); - JSON_Status json_object_set_null(JSON_Object *object, const char *name); - - /* Works like dotget functions, but creates whole hierarchy if necessary. - * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ - JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); - JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); - JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); - JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); - JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); - - /* Frees and removes name-value pair */ - JSON_Status json_object_remove(JSON_Object *object, const char *name); - - /* Works like dotget function, but removes name-value pair only on exact match. */ - JSON_Status json_object_dotremove(JSON_Object *object, const char *key); - - /* Removes all name-value pairs in object */ - JSON_Status json_object_clear(JSON_Object *object); - - /* - *JSON Array - */ - JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); - const char * json_array_get_string (const JSON_Array *array, size_t index); - JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); - JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); - double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ - int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ - size_t json_array_get_count (const JSON_Array *array); - - /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. - * Order of values in array may change during execution. */ - JSON_Status json_array_remove(JSON_Array *array, size_t i); - - /* Frees and removes from array value at given index and replaces it with given one. - * Does nothing and returns JSONFailure if index doesn't exist. - * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ - JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); - JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); - JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); - JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); - JSON_Status json_array_replace_null(JSON_Array *array, size_t i); - - /* Frees and removes all values from array */ - JSON_Status json_array_clear(JSON_Array *array); - - /* Appends new value at the end of array. - * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ - JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); - JSON_Status json_array_append_string(JSON_Array *array, const char *string); - JSON_Status json_array_append_number(JSON_Array *array, double number); - JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); - JSON_Status json_array_append_null(JSON_Array *array); - - /* - *JSON Value - */ - JSON_Value * json_value_init_object (void); - JSON_Value * json_value_init_array (void); - JSON_Value * json_value_init_string (const char *string); /* copies passed string */ - JSON_Value * json_value_init_number (double number); - JSON_Value * json_value_init_boolean(int boolean); - JSON_Value * json_value_init_null (void); - JSON_Value * json_value_deep_copy (const JSON_Value *value); - void json_value_free (JSON_Value *value); - - JSON_Value_Type json_value_get_type (const JSON_Value *value); - JSON_Object * json_value_get_object (const JSON_Value *value); - JSON_Array * json_value_get_array (const JSON_Value *value); - const char * json_value_get_string (const JSON_Value *value); - double json_value_get_number (const JSON_Value *value); - int json_value_get_boolean(const JSON_Value *value); - - /* Same as above, but shorter */ - JSON_Value_Type json_type (const JSON_Value *value); - JSON_Object * json_object (const JSON_Value *value); - JSON_Array * json_array (const JSON_Value *value); - const char * json_string (const JSON_Value *value); - double json_number (const JSON_Value *value); - int json_boolean(const JSON_Value *value); +}; +typedef int JSON_Status; + +typedef void * (*JSON_Malloc_Function)(size_t); +typedef void (*JSON_Free_Function)(void *); + +/* A function used for serializing numbers (see json_set_number_serialization_function). + If 'buf' is null then it should return number of bytes that would've been written + (but not more than PARSON_NUM_BUF_SIZE). +*/ +typedef int (*JSON_Number_Serialization_Function)(double num, char *buf); + +/* Call only once, before calling any other function from parson API. If not called, malloc and free + from stdlib will be used for all allocations */ +void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); + +/* Sets if slashes should be escaped or not when serializing JSON. By default slashes are escaped. + This function sets a global setting and is not thread safe. */ +void json_set_escape_slashes(int escape_slashes); + +/* Sets float format used for serialization of numbers. + Make sure it can't serialize to a string longer than PARSON_NUM_BUF_SIZE. + If format is null then the default format is used. */ +void json_set_float_serialization_format(const char *format); + +/* Sets a function that will be used for serialization of numbers. + If function is null then the default serialization function is used. */ +void json_set_number_serialization_function(JSON_Number_Serialization_Function fun); + +/* Parses first JSON value in a file, returns NULL in case of error */ +#ifndef COMPILE_FOR_SGX +JSON_Value * json_parse_file(const char *filename); +#endif + +/* Parses first JSON value in a file and ignores comments (/ * * / and //), + returns NULL in case of error */ +#ifndef COMPILE_FOR_SGX +JSON_Value * json_parse_file_with_comments(const char *filename); +#endif + +/* Parses first JSON value in a string, returns NULL in case of error */ +JSON_Value * json_parse_string(const char *string); + +/* Parses first JSON value in a string and ignores comments (/ * * / and //), + returns NULL in case of error */ +JSON_Value * json_parse_string_with_comments(const char *string); + +/* Serialization */ +size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ +JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); +#ifndef COMPILE_FOR_SGX +JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); +#endif +char * json_serialize_to_string(const JSON_Value *value); + +/* Pretty serialization */ +size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ +JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); +#ifndef COMPILE_FOR_SGX +JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); +#endif +char * json_serialize_to_string_pretty(const JSON_Value *value); + +void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ + +/* Comparing */ +int json_value_equals(const JSON_Value *a, const JSON_Value *b); + +/* Validation + This is *NOT* JSON Schema. It validates json by checking if object have identically + named fields with matching types. + For example schema {"name":"", "age":0} will validate + {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, + but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. + In case of arrays, only first value in schema is checked against all values in tested array. + Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, + null validates values of every type. + */ +JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); + +/* + * JSON Object + */ +JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); +const char * json_object_get_string (const JSON_Object *object, const char *name); +size_t json_object_get_string_len(const JSON_Object *object, const char *name); /* doesn't account for last null character */ +JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); +JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); +double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ +int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ + +/* dotget functions enable addressing values with dot notation in nested objects, + just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). + Because valid names in JSON can contain dots, some values may be inaccessible + this way. */ +JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); +const char * json_object_dotget_string (const JSON_Object *object, const char *name); +size_t json_object_dotget_string_len(const JSON_Object *object, const char *name); /* doesn't account for last null character */ +JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); +JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); +double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ +int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ + +/* Functions to get available names */ +size_t json_object_get_count (const JSON_Object *object); +const char * json_object_get_name (const JSON_Object *object, size_t index); +JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index); +JSON_Value * json_object_get_wrapping_value(const JSON_Object *object); + +/* Functions to check if object has a value with a specific name. Returned value is 1 if object has + * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */ +int json_object_has_value (const JSON_Object *object, const char *name); +int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); + +int json_object_dothas_value (const JSON_Object *object, const char *name); +int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); + +/* Creates new name-value pair or frees and replaces old value with a new one. + * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); +JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); +JSON_Status json_object_set_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len); /* length shouldn't include last null character */ +JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); +JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); +JSON_Status json_object_set_null(JSON_Object *object, const char *name); + +/* Works like dotget functions, but creates whole hierarchy if necessary. + * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); +JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); +JSON_Status json_object_dotset_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len); /* length shouldn't include last null character */ +JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); +JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); +JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); + +/* Frees and removes name-value pair */ +JSON_Status json_object_remove(JSON_Object *object, const char *name); + +/* Works like dotget function, but removes name-value pair only on exact match. */ +JSON_Status json_object_dotremove(JSON_Object *object, const char *key); + +/* Removes all name-value pairs in object */ +JSON_Status json_object_clear(JSON_Object *object); + +/* + *JSON Array + */ +JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); +const char * json_array_get_string (const JSON_Array *array, size_t index); +size_t json_array_get_string_len(const JSON_Array *array, size_t index); /* doesn't account for last null character */ +JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); +JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); +double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ +int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ +size_t json_array_get_count (const JSON_Array *array); +JSON_Value * json_array_get_wrapping_value(const JSON_Array *array); + +/* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. + * Order of values in array may change during execution. */ +JSON_Status json_array_remove(JSON_Array *array, size_t i); + +/* Frees and removes from array value at given index and replaces it with given one. + * Does nothing and returns JSONFailure if index doesn't exist. + * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); +JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); +JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, const char *string, size_t len); /* length shouldn't include last null character */ +JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); +JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); +JSON_Status json_array_replace_null(JSON_Array *array, size_t i); + +/* Frees and removes all values from array */ +JSON_Status json_array_clear(JSON_Array *array); + +/* Appends new value at the end of array. + * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); +JSON_Status json_array_append_string(JSON_Array *array, const char *string); +JSON_Status json_array_append_string_with_len(JSON_Array *array, const char *string, size_t len); /* length shouldn't include last null character */ +JSON_Status json_array_append_number(JSON_Array *array, double number); +JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); +JSON_Status json_array_append_null(JSON_Array *array); + +/* + *JSON Value + */ +JSON_Value * json_value_init_object (void); +JSON_Value * json_value_init_array (void); +JSON_Value * json_value_init_string (const char *string); /* copies passed string */ +JSON_Value * json_value_init_string_with_len(const char *string, size_t length); /* copies passed string, length shouldn't include last null character */ +JSON_Value * json_value_init_number (double number); +JSON_Value * json_value_init_boolean(int boolean); +JSON_Value * json_value_init_null (void); +JSON_Value * json_value_deep_copy (const JSON_Value *value); +void json_value_free (JSON_Value *value); + +JSON_Value_Type json_value_get_type (const JSON_Value *value); +JSON_Object * json_value_get_object (const JSON_Value *value); +JSON_Array * json_value_get_array (const JSON_Value *value); +const char * json_value_get_string (const JSON_Value *value); +size_t json_value_get_string_len(const JSON_Value *value); /* doesn't account for last null character */ +double json_value_get_number (const JSON_Value *value); +int json_value_get_boolean(const JSON_Value *value); +JSON_Value * json_value_get_parent (const JSON_Value *value); + +/* Same as above, but shorter */ +JSON_Value_Type json_type (const JSON_Value *value); +JSON_Object * json_object (const JSON_Value *value); +JSON_Array * json_array (const JSON_Value *value); +const char * json_string (const JSON_Value *value); +size_t json_string_len(const JSON_Value *value); /* doesn't account for last null character */ +double json_number (const JSON_Value *value); +int json_boolean(const JSON_Value *value); #ifdef __cplusplus } diff --git a/contracts/wawaka/common/Util.cpp b/contracts/wawaka/common/Util.cpp index 175e7b02..698046a7 100644 --- a/contracts/wawaka/common/Util.cpp +++ b/contracts/wawaka/common/Util.cpp @@ -18,12 +18,20 @@ #include "Types.h" #include "Util.h" +#include "WasmExtensions.h" #ifdef USE_WASI_SDK #include -// this function does not appear to be defined in the wasi-sdk -// lib c++. we need +// ----------------------------------------------------------------- +// these functions do not appear to be defined in the wasi-sdk +// lib c++. an error is generally raised about the failure to link +// an import function. for ones with obvious implements like standard +// new and delete, we provide viable implementation. for others that +// are not supported, a message will be generated and the application +// will abort. +// ----------------------------------------------------------------- + std::new_handler std::get_new_handler() _NOEXCEPT { return NULL; @@ -39,11 +47,38 @@ void * operator new[](size_t sz) throw(std::bad_alloc) return malloc(sz); } +void * operator new(size_t sz, std::align_val_t v) +{ + CONTRACT_SAFE_LOG(4, "attempt to invoke unsupported aligned allocation"); + std::abort(); +} + void operator delete(void *ptr) _NOEXCEPT { free(ptr); } +void operator delete(void *ptr, std::align_val_t) _NOEXCEPT +{ + CONTRACT_SAFE_LOG(4, "attempt to invoke unsupported aligned deallocation"); + std::abort(); +} + +#include +int vfprintf(FILE *__restrict, const char *__restrict, __isoc_va_list) +{ + CONTRACT_SAFE_LOG(4, "attempt to invoke unsupported vfprintf"); + std::abort(); + + return -1; +} + +extern "C" void __cxa_pure_virtual(void) +{ + CONTRACT_SAFE_LOG(4, "missing pure virtual function"); + std::abort(); +} + #endif /* ----------------------------------------------------------------- * diff --git a/docker/pdo_base.dockerfile b/docker/pdo_base.dockerfile index 0abac789..6ecf456e 100644 --- a/docker/pdo_base.dockerfile +++ b/docker/pdo_base.dockerfile @@ -68,7 +68,8 @@ RUN apt-get update \ # ----------------------------------------------------------------- # Install WASI toolkit # ----------------------------------------------------------------- -ARG WASI_VERSION=12 +##ARG WASI_VERSION=12 +ARG WASI_VERSION=21 ARG WASI_PACKAGE="wasi-sdk_${WASI_VERSION}.0_amd64.deb" WORKDIR /tmp diff --git a/interpreters/wasm-micro-runtime b/interpreters/wasm-micro-runtime index fb8727ba..78be2215 160000 --- a/interpreters/wasm-micro-runtime +++ b/interpreters/wasm-micro-runtime @@ -1 +1 @@ -Subproject commit fb8727ba683b3969cc6075356bf109defefe6003 +Subproject commit 78be22159489c691f9162d4f09d9e5307fbb0851