From e8c79795231aa9c2060d287fdb2eb3e6acc043c7 Mon Sep 17 00:00:00 2001 From: SHADOW Date: Fri, 20 Oct 2023 11:23:28 +0800 Subject: [PATCH 1/3] feat: update cjson --- include/sparrow/json.h | 570 +++++---- src/json.c | 2704 +++++++++++++++++++++++++++++++++++++++ src/json/LICENSE | 202 --- src/json/json_reader.cc | 840 ------------ src/json/json_writer.cc | 331 ----- 5 files changed, 3035 insertions(+), 1612 deletions(-) create mode 100644 src/json.c delete mode 100644 src/json/LICENSE delete mode 100644 src/json/json_reader.cc delete mode 100644 src/json/json_writer.cc diff --git a/include/sparrow/json.h b/include/sparrow/json.h index 71a16ed..1574da3 100644 --- a/include/sparrow/json.h +++ b/include/sparrow/json.h @@ -1,242 +1,334 @@ /* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef SPARROW_JSON_H_ -#define SPARROW_JSON_H_ + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + 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. +*/ + +#ifndef cJSON__h +#define cJSON__h + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(__WINDOWS__) && \ + (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#define __WINDOWS__ +#endif + +#ifdef __WINDOWS__ + +/* When compiling for windows, we specify a specific calling convention to avoid issues where we are +being called from a project with a different default calling convention. For windows you have 3 +define options: + +CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols +CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) +CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + +For *nix builds that support visibility attribute, you can define similar behavior by + +setting default visibility to hidden by adding +-fvisibility=hidden (for gcc) +or +-xldscope=hidden (for sun cc) +to CFLAGS + +then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS +does + +*/ + +#define CJSON_CDECL __cdecl +#define CJSON_STDCALL __stdcall + +/* export symbols by default, this is necessary for copy pasting the C and header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_EXPORT_SYMBOLS +#endif + +#if defined(CJSON_HIDE_SYMBOLS) +#define CJSON_PUBLIC(type) type CJSON_STDCALL +#elif defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL +#elif defined(CJSON_IMPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL +#endif +#else /* !__WINDOWS__ */ +#define CJSON_CDECL +#define CJSON_STDCALL + +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && \ + defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#else +#define CJSON_PUBLIC(type) type +#endif +#endif + +/* project version */ +#define CJSON_VERSION_MAJOR 1 +#define CJSON_VERSION_MINOR 7 +#define CJSON_VERSION_PATCH 15 #include -#include -#include - -#include -#include -#include - -namespace sparrow { - -// A JSON value, which can be any one of object, array, string, -// number, true, false, or null. -class Json { -public: - // TODO(roth): Currently, numbers are stored internally as strings, - // which makes the API a bit cumbersome to use. When we have time, - // consider whether there's a better alternative (e.g., maybe storing - // each numeric type as the native C++ type and automatically converting - // to string as needed). - enum class Type { JSON_NULL, JSON_TRUE, JSON_FALSE, NUMBER, STRING, OBJECT, ARRAY }; - - using Object = std::map; - using Array = std::vector; - - // Parses JSON string from json_str. On error, sets *error. - static inline Json Parse(const std::string &json_str, std::string *error) { - return Parse(json_str.data(), json_str.size(), error); - } - - // Parses JSON string from buff. On error, sets *error. - static Json Parse(const char *buff, size_t size, std::string *error); - - Json() = default; - - // Copyable. - Json(const Json &other) { CopyFrom(other); } - Json &operator=(const Json &other) { - CopyFrom(other); - return *this; - } - - // Moveable. - Json(Json &&other) { MoveFrom(std::move(other)); } - Json &operator=(Json &&other) { - MoveFrom(std::move(other)); - return *this; - } - - // Construct from copying a string. - // If is_number is true, the type will be NUMBER instead of STRING. - Json(const std::string &string, bool is_number = false) - : type_(is_number ? Type::NUMBER : Type::STRING) - , string_value_(string) {} - Json &operator=(const std::string &string) { - type_ = Type::STRING; - string_value_ = string; - return *this; - } - - // Same thing for C-style strings, both const and mutable. - Json(const char *string, bool is_number = false) - : Json(std::string(string), is_number) {} - Json &operator=(const char *string) { - *this = std::string(string); - return *this; - } - Json(char *string, bool is_number = false) - : Json(std::string(string), is_number) {} - Json &operator=(char *string) { - *this = std::string(string); - return *this; - } - - // Construct by moving a string. - Json(std::string &&string) - : type_(Type::STRING) - , string_value_(std::move(string)) {} - Json &operator=(std::string &&string) { - type_ = Type::STRING; - string_value_ = std::move(string); - return *this; - } - - // Construct from bool. - Json(bool b) - : type_(b ? Type::JSON_TRUE : Type::JSON_FALSE) {} - Json &operator=(bool b) { - type_ = b ? Type::JSON_TRUE : Type::JSON_FALSE; - return *this; - } - - // Construct from any numeric type. - template - Json(NumericType number) - : type_(Type::NUMBER) - , string_value_(std::to_string(number)) {} - template - Json &operator=(NumericType number) { - type_ = Type::NUMBER; - string_value_ = std::to_string(number); - return *this; - } - - // Construct by copying object. - Json(const Object &object) - : type_(Type::OBJECT) - , object_value_(object) {} - Json &operator=(const Object &object) { - type_ = Type::OBJECT; - object_value_ = object; - return *this; - } - - // Construct by moving object. - Json(Object &&object) - : type_(Type::OBJECT) - , object_value_(std::move(object)) {} - Json &operator=(Object &&object) { - type_ = Type::OBJECT; - object_value_ = std::move(object); - return *this; - } - - // Construct by copying array. - Json(const Array &array) - : type_(Type::ARRAY) - , array_value_(array) {} - Json &operator=(const Array &array) { - type_ = Type::ARRAY; - array_value_ = array; - return *this; - } - - // Construct by moving array. - Json(Array &&array) - : type_(Type::ARRAY) - , array_value_(std::move(array)) {} - Json &operator=(Array &&array) { - type_ = Type::ARRAY; - array_value_ = std::move(array); - return *this; - } - - // Dumps JSON from value to string form. - std::string Dump(int indent = 0) const; - - // Accessor methods. - Type type() const { return type_; } - const std::string &string_value() const { return string_value_; } - std::string *mutable_string_value() { return &string_value_; } - const Object &object_value() const { return object_value_; } - Object *mutable_object() { return &object_value_; } - const Array &array_value() const { return array_value_; } - Array *mutable_array() { return &array_value_; } - - bool operator==(const Json &other) const { - if (type_ != other.type_) return false; - switch (type_) { - case Type::NUMBER: - case Type::STRING: - if (string_value_ != other.string_value_) return false; - break; - case Type::OBJECT: - if (object_value_ != other.object_value_) return false; - break; - case Type::ARRAY: - if (array_value_ != other.array_value_) return false; - break; - default: - break; - } - return true; - } - - bool operator!=(const Json &other) const { return !(*this == other); } - -private: - void CopyFrom(const Json &other) { - type_ = other.type_; - switch (type_) { - case Type::NUMBER: - case Type::STRING: - string_value_ = other.string_value_; - break; - case Type::OBJECT: - object_value_ = other.object_value_; - break; - case Type::ARRAY: - array_value_ = other.array_value_; - break; - default: - break; - } - } - - void MoveFrom(Json &&other) { - type_ = other.type_; - other.type_ = Type::JSON_NULL; - switch (type_) { - case Type::NUMBER: - case Type::STRING: - string_value_ = std::move(other.string_value_); - break; - case Type::OBJECT: - object_value_ = std::move(other.object_value_); - break; - case Type::ARRAY: - array_value_ = std::move(other.array_value_); - break; - default: - break; - } - } - - Type type_ = Type::JSON_NULL; - std::string string_value_; - Object object_value_; - Array array_value_; -}; -} // namespace sparrow -#endif /* SPARROW_JSON_H_ */ + +/* cJSON Types: */ +#define cJSON_Invalid (0) +#define cJSON_Boolean (1 << 0) +#define cJSON_NULL (1 << 1) +#define cJSON_Integer (1 << 2) +#define cJSON_Number (1 << 3) +#define cJSON_String (1 << 4) +#define cJSON_Array (1 << 5) +#define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) /* raw json */ + +#define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON { + /* next/prev allow you to walk array/object chains. Alternatively, use + * GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the + * array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* The item's value, if type==cJSON_Integer, cJSON_Boolean */ + long long valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an + * object. */ + char *string; +} cJSON; + +typedef struct cJSON_Hooks { + /* malloc/free are CDECL on Windows regardless of the default calling convention of the + * compiler, so ensure the hooks allow passing those functions directly. */ + void *(CJSON_CDECL *malloc_fn)(size_t sz); + void(CJSON_CDECL *free_fn)(void *ptr); +} cJSON_Hooks; + +typedef int cJSON_bool; + +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_NESTING_LIMIT +#define CJSON_NESTING_LIMIT 1000 +#endif + +/* returns the version of cJSON as a string */ +CJSON_PUBLIC(const char *) cJSON_Version(void); + +/* Supply malloc, realloc and free functions to cJSON */ +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks); + +/* Memory Management: the caller is always responsible to free the results from all variants of + * cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or + * cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full + * responsibility of the buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve + * the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a + * pointer to the error so will match cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) +cJSON_ParseWithOpts(const char *value, const char **return_parse_end, + cJSON_bool require_null_terminated); +CJSON_PUBLIC(cJSON *) +cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, + cJSON_bool require_null_terminated); + +/* Render a cJSON entity to text for transfer/storage. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. */ +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. + * guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with given length. + * Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe + * allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) +cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Delete a cJSON entity and all subentities. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); + +/* Returns the number of items in an array (or object). */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); +/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); +/* Get item "string" from object. Case insensitive. */ +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *const object, const char *const string); +CJSON_PUBLIC(cJSON *) +cJSON_GetObjectItemCaseSensitive(const cJSON *const object, const char *const string); +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to + * look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when + * cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); + +/* Check item type and return its value */ +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON *const item); +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON *const item); +CJSON_PUBLIC(long long) cJSON_GetIntegerValue(const cJSON *const item); + +/* These functions check the type of an item */ +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBoolean(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsInteger(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item); + +/* These calls create a cJSON item of the appropriate type. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateBoolean(cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) cJSON_CreateInteger(long long num); +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); +/* raw json */ +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); + +/* Create a string where valuestring references a string so + * it will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); +/* Create an object/array that only references it's elements so + * they will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); + +/* These utilities create an Array of count items. + * The parameter count cannot be greater than the number of elements in the number array, otherwise + * array access will be out of bounds.*/ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntegerArray(const long long *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely + * survive the cJSON object. WARNING: When this function was used, make sure to always check that + * (item->type & cJSON_StringIsConst) is zero before writing to `item->string` */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing + * cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) +cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detach items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON *const item); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); + +/* Update array items. */ +CJSON_PUBLIC(cJSON_bool) +cJSON_InsertItemInArray(cJSON *array, int which, + cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemViaPointer(cJSON *const parent, cJSON *const item, cJSON *replacement); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem); + +/* Duplicate a cJSON item */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will + * need to be released. With recurse!=0, it will duplicate any children connected to the item. + * The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will + * be considered unequal. case_sensitive determines if object keys are treated case sensitive (1) or + * case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) +cJSON_Compare(const cJSON *const a, const cJSON *const b, const cJSON_bool case_sensitive); + +/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable address area. */ +CJSON_PUBLIC(void) cJSON_Minify(char *json); + +/* Helper functions for creating and adding items to an object at the same time. + * They return the added item or NULL on failure. */ +CJSON_PUBLIC(cJSON *) cJSON_AddNullToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) cJSON_AddTrueToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) cJSON_AddFalseToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) +cJSON_AddBoolToObject(cJSON *const object, const char *const name, const cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) +cJSON_AddIntegerToObject(cJSON *const object, const char *const name, const long long number); +CJSON_PUBLIC(cJSON *) +cJSON_AddNumberToObject(cJSON *const object, const char *const name, const double number); +CJSON_PUBLIC(cJSON *) +cJSON_AddStringToObject(cJSON *const object, const char *const name, const char *const string); +CJSON_PUBLIC(cJSON *) +cJSON_AddRawToObject(cJSON *const object, const char *const name, const char *const raw); +CJSON_PUBLIC(cJSON *) cJSON_AddObjectToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) cJSON_AddArrayToObject(cJSON *const object, const char *const name); + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntegerValue(object, number) \ + ((object != NULL) ? (object)->valueint = (number) : (number)) +/* helper for the cJSON_SetNumberValue macro */ +#define cJSON_SetNumberValue(object, number) \ + ((object != NULL) ? (object)->valuedouble = (number) : (number)) +/* Change the valuestring of a cJSON_String object, only takes effect when type of object is + * cJSON_String */ +CJSON_PUBLIC(char *) cJSON_SetValuestring(cJSON *object, const char *valuestring); + +/* Macro for iterating over an array or object */ +#define cJSON_ArrayForEach(element, array) \ + for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \ + element = element->next) + +/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +CJSON_PUBLIC(void *) cJSON_malloc(size_t size); +CJSON_PUBLIC(void) cJSON_free(void *object); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/json.c b/src/json.c new file mode 100644 index 0000000..98c94dd --- /dev/null +++ b/src/json.c @@ -0,0 +1,2704 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + 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. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning(push) +/* disable warning about single line comments in system headers */ +#pragma warning(disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_LOCALES +#include +#endif + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif + +#include "sparrow/cJSON15.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h + */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#ifdef _WIN32 +#define NAN sqrt(-1.0) +#else +#define NAN 0.0 / 0.0 +#endif +#endif + +typedef struct { + const unsigned char *json; + size_t position; +} error; +static error global_error = {NULL, 0}; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) { + return (const char *)(global_error.json + global_error.position); +} + +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON *const item) { + if (!cJSON_IsString(item)) { + return NULL; + } + + return item->valuestring; +} + +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON *const item) { + if (!cJSON_IsNumber(item)) { + return (double)NAN; + } + + return item->valuedouble; +} + +CJSON_PUBLIC(long long) cJSON_GetIntegerValue(const cJSON *const item) { + if (!cJSON_IsInteger(item)) { + return 0; + } + + return item->valueint; +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 15) +#error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#endif + +CJSON_PUBLIC(const char *) cJSON_Version(void) { + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) { + if ((string1 == NULL) || (string2 == NULL)) { + return 1; + } + + if (string1 == string2) { + return 0; + } + + for (; tolower(*string1) == tolower(*string2); (void)string1++, string2++) { + if (*string1 == '\0') { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks { + void *(CJSON_CDECL *allocate)(size_t size); + void(CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); +} internal_hooks; + +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void *CJSON_CDECL internal_malloc(size_t size) { return malloc(size); } +static void CJSON_CDECL internal_free(void *pointer) { free(pointer); } +static void *CJSON_CDECL internal_realloc(void *pointer, size_t size) { + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + +static internal_hooks global_hooks = {internal_malloc, internal_free, internal_realloc}; + +static unsigned char *cJSON_strdup(const unsigned char *string, const internal_hooks *const hooks) { + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) { + return NULL; + } + + length = strlen((const char *)string) + sizeof(""); + copy = (unsigned char *)hooks->allocate(length); + if (copy == NULL) { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks) { + if (hooks == NULL) { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks *const hooks) { + cJSON *node = (cJSON *)hooks->allocate(sizeof(cJSON)); + if (node) { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) { + cJSON *next = NULL; + while (item != NULL) { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) { + global_hooks.deallocate(item->valuestring); + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) { + global_hooks.deallocate(item->string); + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) { +#ifdef ENABLE_LOCALES + struct lconv *lconv = localeconv(); + return (unsigned char)lconv->decimal_point[0]; +#else + return '.'; +#endif +} + +typedef struct { + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) \ + ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON *const item, parse_buffer *const input_buffer) { + + unsigned char *after_end = NULL; + unsigned char number_c_string[64]; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + int find_number_e = 0; + int find_number_dot = 0; + if ((input_buffer == NULL) || (input_buffer->content == NULL)) { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input + */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) { + switch (buffer_at_offset(input_buffer)[i]) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + find_number_e = 1; + break; + + case '.': + number_c_string[i] = decimal_point; + find_number_dot = 1; + break; + + default: + goto loop_end; + } + } +loop_end: + number_c_string[i] = '\0'; + + if (find_number_dot == 1 || find_number_e == 1) { + item->valuedouble = strtod((const char *)number_c_string, (char **)&after_end); + item->valueint = 0; + item->type = cJSON_Number; + } else { + item->valueint = strtoll((const char *)number_c_string, (char **)&after_end, 10); + item->valuedouble = 0; + item->type = cJSON_Integer; + } + + if (number_c_string == after_end) { + return false; /* parse_error */ + } + + input_buffer->offset += (size_t)(after_end - number_c_string); + return true; +} + +CJSON_PUBLIC(char *) cJSON_SetValuestring(cJSON *object, const char *valuestring) { + char *copy = NULL; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring + */ + if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference)) { + return NULL; + } + if (strlen(valuestring) <= strlen(object->valuestring)) { + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char *)cJSON_strdup((const unsigned char *)valuestring, &global_hooks); + if (copy == NULL) { + return NULL; + } + if (object->valuestring != NULL) { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + +typedef struct { + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char *ensure(printbuffer *const p, size_t needed) { + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) { + newsize = INT_MAX; + } else { + return NULL; + } + } else { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) { + /* reallocate with realloc if available */ + newbuffer = (unsigned char *)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } else { + /* otherwise reallocate manually */ + newbuffer = (unsigned char *)p->hooks.allocate(newsize); + if (!newbuffer) { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + + memcpy(newbuffer, p->buffer, p->offset + 1); + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer *const buffer) { + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char *)buffer_pointer); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) { + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + +/* Render the integer nicely from the given item into a string. */ +static cJSON_bool print_integer(const cJSON *const item, printbuffer *const output_buffer) { + unsigned char *output_pointer = NULL; + int length = 0; + int i = 0; + unsigned char number_buffer[32] = {0}; + + if (output_buffer == NULL) { + return false; + } + + length = snprintf((char *)number_buffer, sizeof(number_buffer), "%" PRId64, item->valueint); + if (length < 0 || length > (int)(sizeof(number_buffer) - 1)) { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) { + return false; + } + + /* copy the printed number to the output */ + for (i = 0; i < length; i++) { + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON *const item, printbuffer *const output_buffer) { + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) { + + length = sprintf((char *)number_buffer, "null"); + + } else if (d == (double)item->valueint) { + + length = sprintf((char *)number_buffer, "%" PRId64, item->valueint); + + } else { + + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char *)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char *)number_buffer, "%lg", &test) != 1) || + !compare_double((double)test, d)) { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char *)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) { + if (number_buffer[i] == decimal_point) { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char *const input) { + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) { + h += (unsigned int)input[i] - '0'; + } else if ((input[i] >= 'A') && (input[i] <= 'F')) { + h += (unsigned int)10 + input[i] - 'A'; + } else if ((input[i] >= 'a') && (input[i] <= 'f')) { + h += (unsigned int)10 + input[i] - 'a'; + } else /* invalid */ + { + return 0; + } + + if (i < 3) { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char *const input_pointer, + const unsigned char *const input_end, + unsigned char **output_pointer) { + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) { + /* invalid second half of the surrogate pair */ + goto fail; + } + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } else { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } else if (codepoint < 0x800) { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } else if (codepoint < 0x10000) { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } else if (codepoint <= 0x10FFFF) { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } else { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } else { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON *const item, parse_buffer *const input_buffer) { + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && + (*input_end != '\"')) { + /* is escape sequence */ + if (input_end[0] == '\\') { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || + (*input_end != '\"')) { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t)(input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char *)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) { + if (*input_pointer != '\\') { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) { + goto fail; + } + + switch (input_pointer[1]) { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char *)output; + + input_buffer->offset = (size_t)(input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) { + input_buffer->hooks.deallocate(output); + } + + if (input_pointer != NULL) { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char *const input, + printbuffer *const output_buffer) { + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) { + return false; + } + + /* empty string */ + if (input == NULL) { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) { + return false; + } + strcpy((char *)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) { + switch (*input_pointer) { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) { + /* normal character, copy */ + *output_pointer = *input_pointer; + } else { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char *)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON *const item, printbuffer *const p) { + return print_string_ptr((unsigned char *)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON *const item, parse_buffer *const input_buffer); +static cJSON_bool print_value(const cJSON *const item, printbuffer *const output_buffer); +static cJSON_bool parse_array(cJSON *const item, parse_buffer *const input_buffer); +static cJSON_bool print_array(const cJSON *const item, printbuffer *const output_buffer); +static cJSON_bool parse_object(cJSON *const item, parse_buffer *const input_buffer); +static cJSON_bool print_object(const cJSON *const item, printbuffer *const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer *const buffer) { + if ((buffer == NULL) || (buffer->content == NULL)) { + return NULL; + } + + if (cannot_access_at_index(buffer, 0)) { + return buffer; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) { + buffer->offset++; + } + + if (buffer->offset == buffer->length) { + buffer->offset--; + } + + return buffer; +} + +/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ +static parse_buffer *skip_utf8_bom(parse_buffer *const buffer) { + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) { + return NULL; + } + + if (can_access_at_index(buffer, 4) && + (strncmp((const char *)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) { + buffer->offset += 3; + } + + return buffer; +} + +CJSON_PUBLIC(cJSON *) +cJSON_ParseWithOpts(const char *value, const char **return_parse_end, + cJSON_bool require_null_terminated) { + size_t buffer_length; + + if (NULL == value) { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, + require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) +cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, + cJSON_bool require_null_terminated) { + parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}}; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL || 0 == buffer_length) { + goto fail; + } + + buffer.content = (const unsigned char *)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null + * terminator */ + if (require_null_terminated) { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') { + goto fail; + } + } + if (return_parse_end) { + *return_parse_end = (const char *)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) { + cJSON_Delete(item); + } + + if (value != NULL) { + error local_error; + local_error.json = (const unsigned char *)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) { + local_error.position = buffer.offset; + } else if (buffer.length > 0) { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) { + *return_parse_end = (const char *)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) { return cJSON_ParseWithOpts(value, 0, 0); } + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) { + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) + +static unsigned char *print(const cJSON *const item, cJSON_bool format, + const internal_hooks *const hooks) { + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char *)hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) { + printed = (unsigned char *)hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char *)hooks->allocate(buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + } + + return printed; + +fail: + if (buffer->buffer != NULL) { + hooks->deallocate(buffer->buffer); + } + + if (printed != NULL) { + hooks->deallocate(printed); + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) { + return (char *)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) { + return (char *)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) { + printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; + + if (prebuffer < 0) { + return NULL; + } + + p.buffer = (unsigned char *)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) { + global_hooks.deallocate(p.buffer); + return NULL; + } + + return (char *)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) +cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) { + printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; + + if ((length < 0) || (buffer == NULL)) { + return false; + } + + p.buffer = (unsigned char *)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON *const item, parse_buffer *const input_buffer) { + if ((input_buffer == NULL) || (input_buffer->content == NULL)) { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && + (strncmp((const char *)buffer_at_offset(input_buffer), "null", 4) == 0)) { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && + (strncmp((const char *)buffer_at_offset(input_buffer), "false", 5) == 0)) { + item->type = cJSON_Boolean; + item->valueint = 0; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && + (strncmp((const char *)buffer_at_offset(input_buffer), "true", 4) == 0)) { + item->type = cJSON_Boolean; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || + ((buffer_at_offset(input_buffer)[0] >= '0') && + (buffer_at_offset(input_buffer)[0] <= '9')))) { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) { + return parse_object(item, input_buffer); + } + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON *const item, printbuffer *const output_buffer) { + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) { + return false; + } + + switch ((item->type) & 0xFF) { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) { + return false; + } + strcpy((char *)output, "null"); + return true; + + case cJSON_Boolean: + if (item->valueint == 1) { + output = ensure(output_buffer, 5); + if (output == NULL) { + return false; + } + strcpy((char *)output, "true"); + } else { + output = ensure(output_buffer, 6); + if (output == NULL) { + return false; + } + strcpy((char *)output, "false"); + } + + return true; + + case cJSON_Integer: + return print_integer(item, output_buffer); + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: { + size_t raw_length = 0; + if (item->valuestring == NULL) { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON *const item, parse_buffer *const input_buffer) { + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) { + /* start the linked list */ + current_item = head = new_item; + } else { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON *const item, printbuffer *const output_buffer) { + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) { + if (!print_value(current_element, output_buffer)) { + return false; + } + update_offset(output_buffer); + if (current_element->next) { + length = (size_t)(output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) { + return false; + } + *output_pointer++ = ','; + if (output_buffer->format) { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON *const item, parse_buffer *const input_buffer) { + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) { + /* start the linked list */ + current_item = head = new_item; + } else { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON *const item, printbuffer *const output_buffer) { + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) { + return false; + } + + /* Compose the output: */ + length = (size_t)(output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) { + if (output_buffer->format) { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) { + return false; + } + for (i = 0; i < output_buffer->depth; i++) { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char *)current_item->string, output_buffer)) { + return false; + } + update_offset(output_buffer); + + length = (size_t)(output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) { + return false; + } + if (current_item->next) { + *output_pointer++ = ','; + } + + if (output_buffer->format) { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) { + return false; + } + if (output_buffer->format) { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) { + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) { + return 0; + } + + child = array->child; + + while (child != NULL) { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON *get_array_item(const cJSON *array, size_t index) { + cJSON *current_child = NULL; + + if (array == NULL) { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) { + if (index < 0) { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON *const object, const char *const name, + const cJSON_bool case_sensitive) { + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) { + return NULL; + } + + current_element = object->child; + if (case_sensitive) { + while ((current_element != NULL) && (current_element->string != NULL) && + (strcmp(name, current_element->string) != 0)) { + current_element = current_element->next; + } + } else { + while ((current_element != NULL) && + (case_insensitive_strcmp((const unsigned char *)name, + (const unsigned char *)(current_element->string)) != 0)) { + current_element = current_element->next; + } + } + + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *const object, const char *const string) { + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) +cJSON_GetObjectItemCaseSensitive(const cJSON *const object, const char *const string) { + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) { + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) { + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks *const hooks) { + cJSON *reference = NULL; + if (item == NULL) { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) { + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } else { + /* append to the end */ + if (child->prev) { + suffix_object(child->prev, item); + array->child->prev = item; + } + } + + return true; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) { + return add_item_to_array(array, item); +} + +#if defined(__clang__) || \ + (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) +#pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif +/* helper function to cast away const */ +static void *cast_away_const(const void *string) { return (void *)string; } +#if defined(__clang__) || \ + (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) +#pragma GCC diagnostic pop +#endif + +static cJSON_bool add_item_to_object(cJSON *const object, const char *const string, + cJSON *const item, const internal_hooks *const hooks, + const cJSON_bool constant_key) { + char *new_key = NULL; + int new_type = cJSON_Invalid; + + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) { + return false; + } + + if (constant_key) { + new_key = (char *)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } else { + new_key = (char *)cJSON_strdup((const unsigned char *)string, hooks); + if (new_key == NULL) { + return false; + } + + new_type = item->type & ~cJSON_StringIsConst; + } + + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) { + hooks->deallocate(item->string); + } + + item->string = new_key; + item->type = new_type; + + return add_item_to_array(object, item); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) { + return add_item_to_object(object, string, item, &global_hooks, false); +} + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) { + return add_item_to_object(object, string, item, &global_hooks, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) { + if (array == NULL) { + return false; + } + + return add_item_to_array(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON_bool) +cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) { + if ((object == NULL) || (string == NULL)) { + return false; + } + + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, + false); +} + +CJSON_PUBLIC(cJSON *) cJSON_AddNullToObject(cJSON *const object, const char *const name) { + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) { + return null; + } + + cJSON_Delete(null); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_AddTrueToObject(cJSON *const object, const char *const name) { + cJSON *true_item = cJSON_CreateBooleanean(1); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) { + return true_item; + } + + cJSON_Delete(true_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_AddFalseToObject(cJSON *const object, const char *const name) { + cJSON *false_item = cJSON_CreateBooleanean(0); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) { + return false_item; + } + + cJSON_Delete(false_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) +cJSON_AddBoolToObject(cJSON *const object, const char *const name, const cJSON_bool boolean) { + cJSON *bool_item = cJSON_CreateBoolean(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) { + return bool_item; + } + + cJSON_Delete(bool_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) +cJSON_AddIntegerToObject(cJSON *const object, const char *const name, const long long number) { + cJSON *number_item = cJSON_CreateInteger(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) +cJSON_AddNumberToObject(cJSON *const object, const char *const name, const double number) { + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) +cJSON_AddStringToObject(cJSON *const object, const char *const name, const char *const string) { + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) { + return string_item; + } + + cJSON_Delete(string_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) +cJSON_AddRawToObject(cJSON *const object, const char *const name, const char *const raw) { + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) { + return raw_item; + } + + cJSON_Delete(raw_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_AddObjectToObject(cJSON *const object, const char *const name) { + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) { + return object_item; + } + + cJSON_Delete(object_item); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_AddArrayToObject(cJSON *const object, const char *const name) { + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) { + return array; + } + + cJSON_Delete(array); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON *const item) { + if ((parent == NULL) || (item == NULL)) { + return NULL; + } + + if (item != parent->child) { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) { + /* first element */ + parent->child = item->next; + } else if (item->next == NULL) { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) { + if (which < 0) { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) { + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) { + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) { + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) { + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) { + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { + cJSON *after_inserted = NULL; + + if (which < 0) { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) { + return add_item_to_array(array, newitem); + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) { + array->child = newitem; + } else { + newitem->prev->next = newitem; + } + return true; +} + +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemViaPointer(cJSON *const parent, cJSON *const item, cJSON *replacement) { + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) { + return false; + } + + if (replacement == item) { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) { + replacement->next->prev = replacement; + } + if (parent->child == item) { + if (parent->child->prev == parent->child) { + replacement->prev = replacement; + } + parent->child = replacement; + } else { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { + if (which < 0) { + return false; + } + + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); +} + +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, + cJSON_bool case_sensitive) { + if ((replacement == NULL) || (string == NULL)) { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) { + cJSON_free(replacement->string); + } + replacement->string = (char *)cJSON_strdup((const unsigned char *)string, &global_hooks); + if (replacement->string == NULL) { + return false; + } + replacement->type &= ~cJSON_StringIsConst; + + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), + replacement); +} + +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { + return replace_item_in_object(object, string, newitem, false); +} + +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) { + return replace_item_in_object(object, string, newitem, true); +} + +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_NULL; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateBooleanean(cJSON_bool boolean) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_Boolean; + item->valueint = boolean; + item->valuedouble = 0; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateInteger(long long num) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_Integer; + item->valueint = num; + item->valuedouble = 0; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_Number; + item->valueint = 0; + item->valuedouble = num; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_String; + item->valuestring = (char *)cJSON_strdup((const unsigned char *)string, &global_hooks); + if (!item->valuestring) { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char *)cast_away_const(string); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON *)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON *)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_Raw; + item->valuestring = (char *)cJSON_strdup((const unsigned char *)raw, &global_hooks); + if (!item->valuestring) { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_Array; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) { + item->type = cJSON_Object; + } + + return item; +} + +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntegerArray(const long long *numbers, int count) { + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) { + n = cJSON_CreateInteger(numbers[i]); + if (!n) { + cJSON_Delete(a); + return NULL; + } + if (!i) { + a->child = n; + } else { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) { + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) { + n = cJSON_CreateNumber((double)numbers[i]); + if (!n) { + cJSON_Delete(a); + return NULL; + } + if (!i) { + a->child = n; + } else { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) { + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) { + n = cJSON_CreateNumber(numbers[i]); + if (!n) { + cJSON_Delete(a); + return NULL; + } + if (!i) { + a->child = n; + } else { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) { + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) { + n = cJSON_CreateString(strings[i]); + if (!n) { + cJSON_Delete(a); + return NULL; + } + if (!i) { + a->child = n; + } else { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +/* Duplication */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) { + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) { + newitem->valuestring = + (char *)cJSON_strdup((unsigned char *)item->valuestring, &global_hooks); + if (!newitem->valuestring) { + goto fail; + } + } + if (item->string) { + newitem->string = (item->type & cJSON_StringIsConst) + ? item->string + : (char *)cJSON_strdup((unsigned char *)item->string, &global_hooks); + if (!newitem->string) { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) { + newchild = cJSON_Duplicate( + child, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) { + goto fail; + } + if (next != NULL) { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } else { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + if (newitem && newitem->child) { + newitem->child->prev = newchild; + } + + return newitem; + +fail: + if (newitem != NULL) { + cJSON_Delete(newitem); + } + + return NULL; +} + +static void skip_oneline_comment(char **input) { + *input += static_strlen("//"); + + for (; (*input)[0] != '\0'; ++(*input)) { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } +} + +static void skip_multiline_comment(char **input) { + *input += static_strlen("/*"); + + for (; (*input)[0] != '\0'; ++(*input)) { + if (((*input)[0] == '*') && ((*input)[1] == '/')) { + *input += static_strlen("*/"); + return; + } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } +} + +CJSON_PUBLIC(void) cJSON_Minify(char *json) { + char *into = json; + + if (json == NULL) { + return; + } + + while (json[0] != '\0') { + switch (json[0]) { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') { + skip_oneline_comment(&json); + } else if (json[1] == '*') { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char **)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsBoolean(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Boolean; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInteger(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Integer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item) { + if (item == NULL) { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} + +CJSON_PUBLIC(cJSON_bool) +cJSON_Compare(const cJSON *const a, const cJSON *const b, const cJSON_bool case_sensitive) { + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) { + case cJSON_Boolean: + case cJSON_NULL: + case cJSON_Integer: + case cJSON_Number: + case cJSON_String: + case cJSON_Array: + case cJSON_Object: + case cJSON_Raw: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) { + return true; + } + + switch (a->type & 0xFF) { + /* in these cases and equal type is enough */ + case cJSON_NULL: + return true; + + case cJSON_Boolean: + case cJSON_Integer: + if (a->valueint == b->valueint) { + return true; + } + return false; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) { + return true; + } + + return false; + + case cJSON_Array: { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) { + return false; + } + } + + return true; + } + + default: + return false; + } +} + +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) { return global_hooks.allocate(size); } + +CJSON_PUBLIC(void) cJSON_free(void *object) { global_hooks.deallocate(object); } diff --git a/src/json/LICENSE b/src/json/LICENSE deleted file mode 100644 index d645695..0000000 --- a/src/json/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/src/json/json_reader.cc b/src/json/json_reader.cc deleted file mode 100644 index 1a3c238..0000000 --- a/src/json/json_reader.cc +++ /dev/null @@ -1,840 +0,0 @@ -/* - * - * Copyright 2015-2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "sparrow/json.h" - -#include -#include -#include - -#include - -#include "sparrow/strings.h" - -#define GRPC_JSON_MAX_DEPTH 255 -#define GRPC_JSON_MAX_ERRORS 16 -#define GPR_UNREACHABLE_CODE(x) x - -namespace sparrow { - -namespace { - -class JsonReader { -public: - static std::string Parse(const char *input, size_t input_size, Json *output); - -private: - enum class Status { - GRPC_JSON_DONE, /* The parser finished successfully. */ - GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */ - GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */ - }; - - enum class State { - GRPC_JSON_STATE_OBJECT_KEY_BEGIN, - GRPC_JSON_STATE_OBJECT_KEY_STRING, - GRPC_JSON_STATE_OBJECT_KEY_END, - GRPC_JSON_STATE_VALUE_BEGIN, - GRPC_JSON_STATE_VALUE_STRING, - GRPC_JSON_STATE_STRING_ESCAPE, - GRPC_JSON_STATE_STRING_ESCAPE_U1, - GRPC_JSON_STATE_STRING_ESCAPE_U2, - GRPC_JSON_STATE_STRING_ESCAPE_U3, - GRPC_JSON_STATE_STRING_ESCAPE_U4, - GRPC_JSON_STATE_VALUE_NUMBER, - GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL, - GRPC_JSON_STATE_VALUE_NUMBER_ZERO, - GRPC_JSON_STATE_VALUE_NUMBER_DOT, - GRPC_JSON_STATE_VALUE_NUMBER_E, - GRPC_JSON_STATE_VALUE_NUMBER_EPM, - GRPC_JSON_STATE_VALUE_TRUE_R, - GRPC_JSON_STATE_VALUE_TRUE_U, - GRPC_JSON_STATE_VALUE_TRUE_E, - GRPC_JSON_STATE_VALUE_FALSE_A, - GRPC_JSON_STATE_VALUE_FALSE_L, - GRPC_JSON_STATE_VALUE_FALSE_S, - GRPC_JSON_STATE_VALUE_FALSE_E, - GRPC_JSON_STATE_VALUE_NULL_U, - GRPC_JSON_STATE_VALUE_NULL_L1, - GRPC_JSON_STATE_VALUE_NULL_L2, - GRPC_JSON_STATE_VALUE_END, - GRPC_JSON_STATE_END - }; - - /* The first non-unicode value is 0x110000. But let's pick - * a value high enough to start our error codes from. These - * values are safe to return from the read_char function. - */ - static constexpr uint32_t GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0; - - explicit JsonReader(const char *data, size_t size) - : original_input_(reinterpret_cast(data)) - , input_(original_input_) - , remaining_input_(size) {} - - Status Run(); - uint32_t ReadChar(); - bool IsComplete(); - - size_t CurrentIndex() const { return input_ - original_input_ - 1; } - - void StringAddChar(uint32_t c); - void StringAddUtf32(uint32_t c); - - Json *CreateAndLinkValue(); - bool StartContainer(Json::Type type); - void EndContainer(); - void SetKey(); - void SetString(); - bool SetNumber(); - void SetTrue(); - void SetFalse(); - void SetNull(); - - const uint8_t *original_input_; - const uint8_t *input_; - size_t remaining_input_; - - State state_ = State::GRPC_JSON_STATE_VALUE_BEGIN; - bool escaped_string_was_key_ = false; - bool container_just_begun_ = false; - uint16_t unicode_char_ = 0; - uint16_t unicode_high_surrogate_ = 0; - std::vector errors_; - bool truncated_errors_ = false; - - Json root_value_; - std::vector stack_; - - std::string key_; - std::string string_; -}; - -void JsonReader::StringAddChar(uint32_t c) { string_.push_back(static_cast(c)); } - -void JsonReader::StringAddUtf32(uint32_t c) { - if (c <= 0x7f) { - StringAddChar(c); - } else if (c <= 0x7ff) { - uint32_t b1 = 0xc0 | ((c >> 6) & 0x1f); - uint32_t b2 = 0x80 | (c & 0x3f); - StringAddChar(b1); - StringAddChar(b2); - } else if (c <= 0xffff) { - uint32_t b1 = 0xe0 | ((c >> 12) & 0x0f); - uint32_t b2 = 0x80 | ((c >> 6) & 0x3f); - uint32_t b3 = 0x80 | (c & 0x3f); - StringAddChar(b1); - StringAddChar(b2); - StringAddChar(b3); - } else if (c <= 0x1fffff) { - uint32_t b1 = 0xf0 | ((c >> 18) & 0x07); - uint32_t b2 = 0x80 | ((c >> 12) & 0x3f); - uint32_t b3 = 0x80 | ((c >> 6) & 0x3f); - uint32_t b4 = 0x80 | (c & 0x3f); - StringAddChar(b1); - StringAddChar(b2); - StringAddChar(b3); - StringAddChar(b4); - } -} - -uint32_t JsonReader::ReadChar() { - if (remaining_input_ == 0) return GRPC_JSON_READ_CHAR_EOF; - const uint32_t r = *input_++; - --remaining_input_; - if (r == 0) { - remaining_input_ = 0; - return GRPC_JSON_READ_CHAR_EOF; - } - return r; -} - -Json *JsonReader::CreateAndLinkValue() { - Json *value; - if (stack_.empty()) { - value = &root_value_; - } else { - Json *parent = stack_.back(); - if (parent->type() == Json::Type::OBJECT) { - if (parent->object_value().find(key_) != parent->object_value().end()) { - if (errors_.size() == GRPC_JSON_MAX_ERRORS) { - truncated_errors_ = true; - } else { - errors_.push_back( - Asprintf("duplicate key \"%s\" at index %" PRIuPTR, key_, CurrentIndex())); - } - } - value = &(*parent->mutable_object())[std::move(key_)]; - } else { - assert(parent->type() == Json::Type::ARRAY); - parent->mutable_array()->emplace_back(); - value = &parent->mutable_array()->back(); - } - } - return value; -} - -bool JsonReader::StartContainer(Json::Type type) { - if (stack_.size() == GRPC_JSON_MAX_DEPTH) { - if (errors_.size() == GRPC_JSON_MAX_ERRORS) { - truncated_errors_ = true; - } else { - errors_.push_back(Asprintf("exceeded max stack depth (%d) at index %" PRIuPTR, - GRPC_JSON_MAX_DEPTH, CurrentIndex())); - } - return false; - } - Json *value = CreateAndLinkValue(); - if (type == Json::Type::OBJECT) { - *value = Json::Object(); - } else { - assert(type == Json::Type::ARRAY); - *value = Json::Array(); - } - stack_.push_back(value); - return true; -} - -void JsonReader::EndContainer() { - assert(!stack_.empty()); - stack_.pop_back(); -} - -void JsonReader::SetKey() { - key_ = std::move(string_); - string_.clear(); -} - -void JsonReader::SetString() { - Json *value = CreateAndLinkValue(); - *value = std::move(string_); - string_.clear(); -} - -bool JsonReader::SetNumber() { - Json *value = CreateAndLinkValue(); - *value = Json(string_, /*is_number=*/true); - string_.clear(); - return true; -} - -void JsonReader::SetTrue() { - Json *value = CreateAndLinkValue(); - *value = true; - string_.clear(); -} - -void JsonReader::SetFalse() { - Json *value = CreateAndLinkValue(); - *value = false; - string_.clear(); -} - -void JsonReader::SetNull() { CreateAndLinkValue(); } - -bool JsonReader::IsComplete() { - return (stack_.empty() && - (state_ == State::GRPC_JSON_STATE_END || state_ == State::GRPC_JSON_STATE_VALUE_END)); -} - -/* Call this function to start parsing the input. It will return the following: - * . GRPC_JSON_DONE if the input got eof, and the parsing finished - * successfully. - * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid. - * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid - * internal state. - */ -JsonReader::Status JsonReader::Run() { - uint32_t c; - - /* This state-machine is a strict implementation of ECMA-404 */ - while (true) { - c = ReadChar(); - switch (c) { - /* Let's process the error case first. */ - case GRPC_JSON_READ_CHAR_EOF: - if (IsComplete()) { - return Status::GRPC_JSON_DONE; - } else { - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - /* Processing whitespaces. */ - case ' ': - case '\t': - case '\n': - case '\r': - switch (state_) { - case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN: - case State::GRPC_JSON_STATE_OBJECT_KEY_END: - case State::GRPC_JSON_STATE_VALUE_BEGIN: - case State::GRPC_JSON_STATE_VALUE_END: - case State::GRPC_JSON_STATE_END: - break; - - case State::GRPC_JSON_STATE_OBJECT_KEY_STRING: - case State::GRPC_JSON_STATE_VALUE_STRING: - if (c != ' ') return Status::GRPC_JSON_PARSE_ERROR; - if (unicode_high_surrogate_ != 0) { - return Status::GRPC_JSON_PARSE_ERROR; - } - StringAddChar(c); - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER: - case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL: - case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO: - case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM: - if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_END; - break; - - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - /* Value, object or array terminations. */ - case ',': - case '}': - case ']': - switch (state_) { - case State::GRPC_JSON_STATE_OBJECT_KEY_STRING: - case State::GRPC_JSON_STATE_VALUE_STRING: - if (unicode_high_surrogate_ != 0) { - return Status::GRPC_JSON_PARSE_ERROR; - } - StringAddChar(c); - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER: - case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL: - case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO: - case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM: - if (stack_.empty()) { - return Status::GRPC_JSON_PARSE_ERROR; - } else if (c == '}' && stack_.back()->type() != Json::Type::OBJECT) { - return Status::GRPC_JSON_PARSE_ERROR; - return Status::GRPC_JSON_PARSE_ERROR; - } else if (c == ']' && stack_.back()->type() != Json::Type::ARRAY) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_END; - /* The missing break here is intentional. */ - /* fallthrough */ - - case State::GRPC_JSON_STATE_VALUE_END: - case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN: - case State::GRPC_JSON_STATE_VALUE_BEGIN: - if (c == ',') { - if (state_ != State::GRPC_JSON_STATE_VALUE_END) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (!stack_.empty() && stack_.back()->type() == Json::Type::OBJECT) { - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN; - } else if (!stack_.empty() && stack_.back()->type() == Json::Type::ARRAY) { - state_ = State::GRPC_JSON_STATE_VALUE_BEGIN; - } else { - return Status::GRPC_JSON_PARSE_ERROR; - } - } else { - if (stack_.empty()) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (c == '}' && stack_.back()->type() != Json::Type::OBJECT) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (c == '}' && state_ == State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN && - !container_just_begun_) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (c == ']' && stack_.back()->type() != Json::Type::ARRAY) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (c == ']' && state_ == State::GRPC_JSON_STATE_VALUE_BEGIN && - !container_just_begun_) { - return Status::GRPC_JSON_PARSE_ERROR; - } - state_ = State::GRPC_JSON_STATE_VALUE_END; - EndContainer(); - if (stack_.empty()) { - state_ = State::GRPC_JSON_STATE_END; - } - } - break; - - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - /* In-string escaping. */ - case '\\': - switch (state_) { - case State::GRPC_JSON_STATE_OBJECT_KEY_STRING: - escaped_string_was_key_ = true; - state_ = State::GRPC_JSON_STATE_STRING_ESCAPE; - break; - - case State::GRPC_JSON_STATE_VALUE_STRING: - escaped_string_was_key_ = false; - state_ = State::GRPC_JSON_STATE_STRING_ESCAPE; - break; - - /* This is the \\ case. */ - case State::GRPC_JSON_STATE_STRING_ESCAPE: - if (unicode_high_surrogate_ != 0) return Status::GRPC_JSON_PARSE_ERROR; - StringAddChar('\\'); - if (escaped_string_was_key_) { - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING; - } else { - state_ = State::GRPC_JSON_STATE_VALUE_STRING; - } - break; - - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - default: - container_just_begun_ = false; - switch (state_) { - case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN: - if (c != '"') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING; - break; - - case State::GRPC_JSON_STATE_OBJECT_KEY_STRING: - if (unicode_high_surrogate_ != 0) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (c == '"') { - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_END; - SetKey(); - } else { - if (c < 32) return Status::GRPC_JSON_PARSE_ERROR; - StringAddChar(c); - } - break; - - case State::GRPC_JSON_STATE_VALUE_STRING: - if (unicode_high_surrogate_ != 0) { - return Status::GRPC_JSON_PARSE_ERROR; - } - if (c == '"') { - state_ = State::GRPC_JSON_STATE_VALUE_END; - SetString(); - } else { - if (c < 32) return Status::GRPC_JSON_PARSE_ERROR; - StringAddChar(c); - } - break; - - case State::GRPC_JSON_STATE_OBJECT_KEY_END: - if (c != ':') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_BEGIN; - break; - - case State::GRPC_JSON_STATE_VALUE_BEGIN: - switch (c) { - case 't': - state_ = State::GRPC_JSON_STATE_VALUE_TRUE_R; - break; - - case 'f': - state_ = State::GRPC_JSON_STATE_VALUE_FALSE_A; - break; - - case 'n': - state_ = State::GRPC_JSON_STATE_VALUE_NULL_U; - break; - - case '"': - state_ = State::GRPC_JSON_STATE_VALUE_STRING; - break; - - case '0': - StringAddChar(c); - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO; - break; - - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '-': - StringAddChar(c); - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER; - break; - - case '{': - container_just_begun_ = true; - if (!StartContainer(Json::Type::OBJECT)) { - return Status::GRPC_JSON_PARSE_ERROR; - } - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN; - break; - - case '[': - container_just_begun_ = true; - if (!StartContainer(Json::Type::ARRAY)) { - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_STRING_ESCAPE: - if (escaped_string_was_key_) { - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING; - } else { - state_ = State::GRPC_JSON_STATE_VALUE_STRING; - } - if (unicode_high_surrogate_ && c != 'u') { - return Status::GRPC_JSON_PARSE_ERROR; - } - switch (c) { - case '"': - case '/': - StringAddChar(c); - break; - case 'b': - StringAddChar('\b'); - break; - case 'f': - StringAddChar('\f'); - break; - case 'n': - StringAddChar('\n'); - break; - case 'r': - StringAddChar('\r'); - break; - case 't': - StringAddChar('\t'); - break; - case 'u': - state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U1; - unicode_char_ = 0; - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_STRING_ESCAPE_U1: - case State::GRPC_JSON_STATE_STRING_ESCAPE_U2: - case State::GRPC_JSON_STATE_STRING_ESCAPE_U3: - case State::GRPC_JSON_STATE_STRING_ESCAPE_U4: - if ((c >= '0') && (c <= '9')) { - c -= '0'; - } else if ((c >= 'A') && (c <= 'F')) { - c -= 'A' - 10; - } else if ((c >= 'a') && (c <= 'f')) { - c -= 'a' - 10; - } else { - return Status::GRPC_JSON_PARSE_ERROR; - } - unicode_char_ = static_cast(unicode_char_ << 4); - unicode_char_ = static_cast(unicode_char_ | c); - - switch (state_) { - case State::GRPC_JSON_STATE_STRING_ESCAPE_U1: - state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U2; - break; - case State::GRPC_JSON_STATE_STRING_ESCAPE_U2: - state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U3; - break; - case State::GRPC_JSON_STATE_STRING_ESCAPE_U3: - state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U4; - break; - case State::GRPC_JSON_STATE_STRING_ESCAPE_U4: - /* See grpc_json_writer_escape_string to have a description - * of what's going on here. - */ - if ((unicode_char_ & 0xfc00) == 0xd800) { - /* high surrogate utf-16 */ - if (unicode_high_surrogate_ != 0) return Status::GRPC_JSON_PARSE_ERROR; - unicode_high_surrogate_ = unicode_char_; - } else if ((unicode_char_ & 0xfc00) == 0xdc00) { - /* low surrogate utf-16 */ - uint32_t utf32; - if (unicode_high_surrogate_ == 0) return Status::GRPC_JSON_PARSE_ERROR; - utf32 = 0x10000; - utf32 += static_cast((unicode_high_surrogate_ - 0xd800) * 0x400); - utf32 += static_cast(unicode_char_ - 0xdc00); - StringAddUtf32(utf32); - unicode_high_surrogate_ = 0; - } else { - /* anything else */ - if (unicode_high_surrogate_ != 0) return Status::GRPC_JSON_PARSE_ERROR; - StringAddUtf32(unicode_char_); - } - if (escaped_string_was_key_) { - state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING; - } else { - state_ = State::GRPC_JSON_STATE_VALUE_STRING; - } - break; - default: - GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR); - } - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER: - StringAddChar(c); - switch (c) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - break; - case 'e': - case 'E': - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E; - break; - case '.': - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT; - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL: - StringAddChar(c); - switch (c) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - break; - case 'e': - case 'E': - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E; - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO: - if (c != '.') return Status::GRPC_JSON_PARSE_ERROR; - StringAddChar(c); - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT; - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER_DOT: - StringAddChar(c); - switch (c) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL; - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER_E: - StringAddChar(c); - switch (c) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '+': - case '-': - state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_EPM; - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM: - StringAddChar(c); - switch (c) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - break; - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_VALUE_TRUE_R: - if (c != 'r') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_TRUE_U; - break; - - case State::GRPC_JSON_STATE_VALUE_TRUE_U: - if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_TRUE_E; - break; - - case State::GRPC_JSON_STATE_VALUE_TRUE_E: - if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR; - SetTrue(); - state_ = State::GRPC_JSON_STATE_VALUE_END; - break; - - case State::GRPC_JSON_STATE_VALUE_FALSE_A: - if (c != 'a') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_FALSE_L; - break; - - case State::GRPC_JSON_STATE_VALUE_FALSE_L: - if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_FALSE_S; - break; - - case State::GRPC_JSON_STATE_VALUE_FALSE_S: - if (c != 's') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_FALSE_E; - break; - - case State::GRPC_JSON_STATE_VALUE_FALSE_E: - if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR; - SetFalse(); - state_ = State::GRPC_JSON_STATE_VALUE_END; - break; - - case State::GRPC_JSON_STATE_VALUE_NULL_U: - if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_NULL_L1; - break; - - case State::GRPC_JSON_STATE_VALUE_NULL_L1: - if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR; - state_ = State::GRPC_JSON_STATE_VALUE_NULL_L2; - break; - - case State::GRPC_JSON_STATE_VALUE_NULL_L2: - if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR; - SetNull(); - state_ = State::GRPC_JSON_STATE_VALUE_END; - break; - - /* All of the VALUE_END cases are handled in the specialized case - * above. */ - case State::GRPC_JSON_STATE_VALUE_END: - switch (c) { - case ',': - case '}': - case ']': - GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR); - break; - - default: - return Status::GRPC_JSON_PARSE_ERROR; - } - break; - - case State::GRPC_JSON_STATE_END: - return Status::GRPC_JSON_PARSE_ERROR; - } - } - } - - GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR); -} - -std::string JsonReader::Parse(const char *buff, size_t size, Json *output) { - JsonReader reader(buff, size); - Status status = reader.Run(); - if (reader.truncated_errors_) { - reader.errors_.push_back( - std::string("too many errors encountered during JSON parsing -- fix reported " - "errors and try again to see additional errors")); - } - if (status == Status::GRPC_JSON_INTERNAL_ERROR) { - reader.errors_.push_back( - StrCat("internal error in JSON parser at index ", reader.CurrentIndex())); - } else if (status == Status::GRPC_JSON_PARSE_ERROR) { - reader.errors_.push_back(StrCat("JSON parse error at index ", reader.CurrentIndex())); - } - if (!reader.errors_.empty()) { - std::string res("JSON parsing failed:"); - for (const std::string &e : reader.errors_) { - res.append(e); - } - return res; - } - *output = std::move(reader.root_value_); - return std::string("No Error"); -} - -} // namespace - -Json Json::Parse(const char *buff, size_t size, std::string *error) { - Json value; - *error = JsonReader::Parse(buff, size, &value); - return value; -} - -#undef GRPC_JSON_MAX_DEPTH -#undef GRPC_JSON_MAX_ERRORS -#undef GPR_UNREACHABLE_CODE - -} // namespace sparrow diff --git a/src/json/json_writer.cc b/src/json/json_writer.cc deleted file mode 100644 index 2cc5362..0000000 --- a/src/json/json_writer.cc +++ /dev/null @@ -1,331 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "sparrow/json.h" - -#include -#include -#include - -#define GPR_UNREACHABLE_CODE(x) x - -namespace sparrow { - -namespace { - -/* The idea of the writer is basically symmetrical of the reader. While the - * reader emits various calls to your code, the writer takes basically the - * same calls and emit json out of it. It doesn't try to make any check on - * the order of the calls you do on it. Meaning you can theorically force - * it to generate invalid json. - * - * Also, unlike the reader, the writer expects UTF-8 encoded input strings. - * These strings will be UTF-8 validated, and any invalid character will - * cut the conversion short, before any invalid UTF-8 sequence, thus forming - * a valid UTF-8 string overall. - */ -class JsonWriter { -public: - static std::string Dump(const Json &value, int indent); - -private: - explicit JsonWriter(int indent) - : indent_(indent) {} - - void OutputCheck(size_t needed); - void OutputChar(char c); - void OutputString(const char *data, size_t size); - inline void OutputString(const std::string &str) { OutputString(str.data(), str.size()); } - void OutputIndent(); - void ValueEnd(); - void EscapeUtf16(uint16_t utf16); - void EscapeString(const std::string &string); - void ContainerBegins(Json::Type type); - void ContainerEnds(Json::Type type); - void ObjectKey(const std::string &string); - void ValueRaw(const std::string &string); - void ValueString(const std::string &string); - - void DumpObject(const Json::Object &object); - void DumpArray(const Json::Array &array); - void DumpValue(const Json &value); - - int indent_; - int depth_ = 0; - bool container_empty_ = true; - bool got_key_ = false; - std::string output_; -}; - -/* This function checks if there's enough space left in the output buffer, - * and will enlarge it if necessary. We're only allocating chunks of 256 - * bytes at a time (or multiples thereof). - */ -void JsonWriter::OutputCheck(size_t needed) { - size_t free_space = output_.capacity() - output_.size(); - if (free_space >= needed) return; - needed -= free_space; - /* Round up by 256 bytes. */ - needed = (needed + 0xff) & ~0xffU; - output_.reserve(output_.capacity() + needed); -} - -void JsonWriter::OutputChar(char c) { - OutputCheck(1); - output_.push_back(c); -} - -void JsonWriter::OutputString(const char *data, size_t size) { - OutputCheck(size); - output_.append(data, size); -} - -void JsonWriter::OutputIndent() { - static const char spacesstr[] = " " - " " - " " - " "; - unsigned spaces = static_cast(depth_ * indent_); - if (indent_ == 0) return; - if (got_key_) { - OutputChar(' '); - return; - } - while (spaces >= (sizeof(spacesstr) - 1)) { - OutputString(spacesstr, sizeof(spacesstr) - 1); - spaces -= static_cast(sizeof(spacesstr) - 1); - } - if (spaces == 0) return; - OutputString(spacesstr + sizeof(spacesstr) - 1 - spaces, spaces); -} - -void JsonWriter::ValueEnd() { - if (container_empty_) { - container_empty_ = false; - if (indent_ == 0 || depth_ == 0) return; - OutputChar('\n'); - } else { - OutputChar(','); - if (indent_ == 0) return; - OutputChar('\n'); - } -} - -void JsonWriter::EscapeUtf16(uint16_t utf16) { - static const char hex[] = "0123456789abcdef"; - OutputString("\\u", 2); - OutputChar(hex[(utf16 >> 12) & 0x0f]); - OutputChar(hex[(utf16 >> 8) & 0x0f]); - OutputChar(hex[(utf16 >> 4) & 0x0f]); - OutputChar(hex[(utf16)&0x0f]); -} - -void JsonWriter::EscapeString(const std::string &string) { - OutputChar('"'); - for (size_t idx = 0; idx < string.size(); ++idx) { - uint8_t c = static_cast(string[idx]); - if (c == 0) { - break; - } else if (c >= 32 && c <= 126) { - if (c == '\\' || c == '"') OutputChar('\\'); - OutputChar(static_cast(c)); - } else if (c < 32 || c == 127) { - switch (c) { - case '\b': - OutputString("\\b", 2); - break; - case '\f': - OutputString("\\f", 2); - break; - case '\n': - OutputString("\\n", 2); - break; - case '\r': - OutputString("\\r", 2); - break; - case '\t': - OutputString("\\t", 2); - break; - default: - EscapeUtf16(c); - break; - } - } else { - uint32_t utf32 = 0; - int extra = 0; - int i; - int valid = 1; - if ((c & 0xe0) == 0xc0) { - utf32 = c & 0x1f; - extra = 1; - } else if ((c & 0xf0) == 0xe0) { - utf32 = c & 0x0f; - extra = 2; - } else if ((c & 0xf8) == 0xf0) { - utf32 = c & 0x07; - extra = 3; - } else { - break; - } - for (i = 0; i < extra; i++) { - utf32 <<= 6; - ++idx; - /* Breaks out and bail if we hit the end of the string. */ - if (idx == string.size()) { - valid = 0; - break; - } - c = static_cast(string[idx]); - /* Breaks out and bail on any invalid UTF-8 sequence, including \0. */ - if ((c & 0xc0) != 0x80) { - valid = 0; - break; - } - utf32 |= c & 0x3f; - } - if (!valid) break; - /* The range 0xd800 - 0xdfff is reserved by the surrogates ad vitam. - * Any other range is technically reserved for future usage, so if we - * don't want the software to break in the future, we have to allow - * anything else. The first non-unicode character is 0x110000. */ - if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) || (utf32 >= 0x110000)) break; - if (utf32 >= 0x10000) { - /* If utf32 contains a character that is above 0xffff, it needs to be - * broken down into a utf-16 surrogate pair. A surrogate pair is first - * a high surrogate, followed by a low surrogate. Each surrogate holds - * 10 bits of usable data, thus allowing a total of 20 bits of data. - * The high surrogate marker is 0xd800, while the low surrogate marker - * is 0xdc00. The low 10 bits of each will be the usable data. - * - * After re-combining the 20 bits of data, one has to add 0x10000 to - * the resulting value, in order to obtain the original character. - * This is obviously because the range 0x0000 - 0xffff can be written - * without any special trick. - * - * Since 0x10ffff is the highest allowed character, we're working in - * the range 0x00000 - 0xfffff after we decrement it by 0x10000. - * That range is exactly 20 bits. - */ - utf32 -= 0x10000; - EscapeUtf16(static_cast(0xd800 | (utf32 >> 10))); - EscapeUtf16(static_cast(0xdc00 | (utf32 & 0x3ff))); - } else { - EscapeUtf16(static_cast(utf32)); - } - } - } - OutputChar('"'); -} - -void JsonWriter::ContainerBegins(Json::Type type) { - if (!got_key_) ValueEnd(); - OutputIndent(); - OutputChar(type == Json::Type::OBJECT ? '{' : '['); - container_empty_ = true; - got_key_ = false; - depth_++; -} - -void JsonWriter::ContainerEnds(Json::Type type) { - if (indent_ && !container_empty_) OutputChar('\n'); - depth_--; - if (!container_empty_) OutputIndent(); - OutputChar(type == Json::Type::OBJECT ? '}' : ']'); - container_empty_ = false; - got_key_ = false; -} - -void JsonWriter::ObjectKey(const std::string &string) { - ValueEnd(); - OutputIndent(); - EscapeString(string); - OutputChar(':'); - got_key_ = true; -} - -void JsonWriter::ValueRaw(const std::string &string) { - if (!got_key_) ValueEnd(); - OutputIndent(); - OutputString(string); - got_key_ = false; -} - -void JsonWriter::ValueString(const std::string &string) { - if (!got_key_) ValueEnd(); - OutputIndent(); - EscapeString(string); - got_key_ = false; -} - -void JsonWriter::DumpObject(const Json::Object &object) { - ContainerBegins(Json::Type::OBJECT); - for (const auto &p : object) { - ObjectKey(p.first.data()); - DumpValue(p.second); - } - ContainerEnds(Json::Type::OBJECT); -} - -void JsonWriter::DumpArray(const Json::Array &array) { - ContainerBegins(Json::Type::ARRAY); - for (const auto &v : array) { - DumpValue(v); - } - ContainerEnds(Json::Type::ARRAY); -} - -void JsonWriter::DumpValue(const Json &value) { - switch (value.type()) { - case Json::Type::OBJECT: - DumpObject(value.object_value()); - break; - case Json::Type::ARRAY: - DumpArray(value.array_value()); - break; - case Json::Type::STRING: - ValueString(value.string_value()); - break; - case Json::Type::NUMBER: - ValueRaw(value.string_value()); - break; - case Json::Type::JSON_TRUE: - ValueRaw(std::string("true", 4)); - break; - case Json::Type::JSON_FALSE: - ValueRaw(std::string("false", 5)); - break; - case Json::Type::JSON_NULL: - ValueRaw(std::string("null", 4)); - break; - default: - GPR_UNREACHABLE_CODE(abort()); - } -} - -std::string JsonWriter::Dump(const Json &value, int indent) { - JsonWriter writer(indent); - writer.DumpValue(value); - return std::move(writer.output_); -} - -} // namespace - -std::string Json::Dump(int indent) const { return JsonWriter::Dump(*this, indent); } - -#undef GPR_UNREACHABLE_CODE -} // namespace sparrow -- Gitee From 0e53d6a1b69a6376222f366811e466ec40af5ddf Mon Sep 17 00:00:00 2001 From: SHADOW Date: Fri, 20 Oct 2023 14:20:46 +0800 Subject: [PATCH 2/3] feat: add json impl --- include/sparrow/json.h | 59 +++--------------------------------- include/sparrow/sha.h | 69 ------------------------------------------ src/json.c | 51 +++++-------------------------- 3 files changed, 12 insertions(+), 167 deletions(-) delete mode 100644 include/sparrow/sha.h diff --git a/include/sparrow/json.h b/include/sparrow/json.h index 1574da3..62faff4 100644 --- a/include/sparrow/json.h +++ b/include/sparrow/json.h @@ -27,60 +27,9 @@ extern "C" { #endif -#if !defined(__WINDOWS__) && \ - (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) -#define __WINDOWS__ -#endif - -#ifdef __WINDOWS__ - -/* When compiling for windows, we specify a specific calling convention to avoid issues where we are -being called from a project with a different default calling convention. For windows you have 3 -define options: - -CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols -CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) -CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol +#include "sparrow/export.h" -For *nix builds that support visibility attribute, you can define similar behavior by - -setting default visibility to hidden by adding --fvisibility=hidden (for gcc) -or --xldscope=hidden (for sun cc) -to CFLAGS - -then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS -does - -*/ - -#define CJSON_CDECL __cdecl -#define CJSON_STDCALL __stdcall - -/* export symbols by default, this is necessary for copy pasting the C and header file */ -#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) -#define CJSON_EXPORT_SYMBOLS -#endif - -#if defined(CJSON_HIDE_SYMBOLS) -#define CJSON_PUBLIC(type) type CJSON_STDCALL -#elif defined(CJSON_EXPORT_SYMBOLS) -#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL -#elif defined(CJSON_IMPORT_SYMBOLS) -#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL -#endif -#else /* !__WINDOWS__ */ -#define CJSON_CDECL -#define CJSON_STDCALL - -#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && \ - defined(CJSON_API_VISIBILITY) -#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type -#else -#define CJSON_PUBLIC(type) type -#endif -#endif +#define CJSON_PUBLIC(type) SPARROW_API type /* project version */ #define CJSON_VERSION_MAJOR 1 @@ -131,8 +80,8 @@ typedef struct cJSON { typedef struct cJSON_Hooks { /* malloc/free are CDECL on Windows regardless of the default calling convention of the * compiler, so ensure the hooks allow passing those functions directly. */ - void *(CJSON_CDECL *malloc_fn)(size_t sz); - void(CJSON_CDECL *free_fn)(void *ptr); + void *(*malloc_fn)(size_t sz); + void (*free_fn)(void *ptr); } cJSON_Hooks; typedef int cJSON_bool; diff --git a/include/sparrow/sha.h b/include/sparrow/sha.h deleted file mode 100644 index d0be403..0000000 --- a/include/sparrow/sha.h +++ /dev/null @@ -1,69 +0,0 @@ -// -// Copyright 2022 The Sparrow Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#ifndef SPARROW_SHA_H_ -#define SPARROW_SHA_H_ - -#include -#include - -namespace sparrow { - -class SHA256 final { -public: - static const int OUTPUT_SIZE = 32; - - SHA256(); - ~SHA256(); - - SHA256(const SHA256 &) = delete; - SHA256 &operator=(const SHA256 &) = delete; - - SHA256 &Write(const void *data, size_t len); - - void Finalize(char hash[OUTPUT_SIZE]); - std::string Finalize(); - - SHA256 &Reset(); - -private: - void *ctx_; -}; - -class SHA1 final { -public: - static const int OUTPUT_SIZE = 20; - - SHA1(); - ~SHA1(); - - SHA1(const SHA1 &) = delete; - SHA1 &operator=(const SHA1 &) = delete; - - SHA1 &Write(const void *data, size_t len); - - void Finalize(char hash[OUTPUT_SIZE]); - std::string Finalize(); - - SHA1 &Reset(); - -private: - void *impl_; -}; - -} // namespace sparrow - -#endif // SPARROW_SHA_H_ diff --git a/src/json.c b/src/json.c index 98c94dd..b452dde 100644 --- a/src/json.c +++ b/src/json.c @@ -22,20 +22,7 @@ /* cJSON */ /* JSON parser in C. */ - -/* disable warnings about old C89 functions in MSVC */ -#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) -#define _CRT_SECURE_NO_DEPRECATE -#endif - -#ifdef __GNUC__ -#pragma GCC visibility push(default) -#endif -#if defined(_MSC_VER) -#pragma warning(push) -/* disable warning about single line comments in system headers */ -#pragma warning(disable : 4001) -#endif +#include "sparrow/json.h" #include #include @@ -46,19 +33,6 @@ #include #include -#ifdef ENABLE_LOCALES -#include -#endif - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif -#ifdef __GNUC__ -#pragma GCC visibility pop -#endif - -#include "sparrow/cJSON15.h" - /* define our own boolean type */ #ifdef true #undef true @@ -153,18 +127,16 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned } typedef struct internal_hooks { - void *(CJSON_CDECL *allocate)(size_t size); - void(CJSON_CDECL *deallocate)(void *pointer); - void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); + void *(*allocate)(size_t size); + void (*deallocate)(void *pointer); + void *(*reallocate)(void *pointer, size_t size); } internal_hooks; #if defined(_MSC_VER) /* work around MSVC error C2322: '...' address of dllimport '...' is not static */ -static void *CJSON_CDECL internal_malloc(size_t size) { return malloc(size); } -static void CJSON_CDECL internal_free(void *pointer) { free(pointer); } -static void *CJSON_CDECL internal_realloc(void *pointer, size_t size) { - return realloc(pointer, size); -} +static void *internal_malloc(size_t size) { return malloc(size); } +static void internal_free(void *pointer) { free(pointer); } +static void *internal_realloc(void *pointer, size_t size) { return realloc(pointer, size); } #else #define internal_malloc malloc #define internal_free free @@ -250,14 +222,7 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) { } /* get the decimal point character of the current locale */ -static unsigned char get_decimal_point(void) { -#ifdef ENABLE_LOCALES - struct lconv *lconv = localeconv(); - return (unsigned char)lconv->decimal_point[0]; -#else - return '.'; -#endif -} +static unsigned char get_decimal_point(void) { return '.'; } typedef struct { const unsigned char *content; -- Gitee From 201b6b517221ebba15a56aac4ee6a7966e3db0f0 Mon Sep 17 00:00:00 2001 From: SHADOW Date: Fri, 20 Oct 2023 15:14:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 149 ++++++++++++++++++++++++++++++++++ .editorconfig | 26 ++++++ .vscode/c_cpp_properties.json | 18 ++++ .vscode/settings.json | 4 + include/sparrow/hash.h | 33 ++++++-- include/sparrow/text_reader.h | 119 ++++++++++++++++++++------- src/os/text_reader.cc | 127 ----------------------------- 7 files changed, 314 insertions(+), 162 deletions(-) create mode 100644 .clang-format create mode 100644 .editorconfig create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json delete mode 100644 src/os/text_reader.cc diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..d3c22dd --- /dev/null +++ b/.clang-format @@ -0,0 +1,149 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: true +AlignConsecutiveAssignments: false +AlignConsecutiveBitFields: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: true +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 100 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + - Regex: '.*' + Priority: 1 + SortPriority: 0 +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +ReflowComments: true +SortIncludes: false +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Latest +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE +... + diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..70c5a1a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,26 @@ +root = true +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[Makefile] +indent_style = tab +indent_size = 4 +insert_final_newline = false +trim_trailing_whitespace = false + +[*.ssh] +indent_style = tab +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.bat] +end_of_line = crlf diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..39ef7af --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/third_party/googletest-1.10.0/googletest/include", + "${workspaceFolder}/include" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c11", + "cppStandard": "c++11", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..415d02e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "editor.fontSize": 17, + "editor.formatOnSave": true +} diff --git a/include/sparrow/hash.h b/include/sparrow/hash.h index 242ffc7..56b7f29 100644 --- a/include/sparrow/hash.h +++ b/include/sparrow/hash.h @@ -1,5 +1,5 @@ // -// Copyright 2022 The Sparrow Authors. +// Copyright 2023 The Sparrow Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,17 +14,36 @@ // limitations under the License. // -#ifndef SPARROW_HASH_H_ -#define SPARROW_HASH_H_ +#ifndef SPARROW_INCLUDE_HASH_H_ +#define SPARROW_INCLUDE_HASH_H_ #include #include +#include +#include +#include + +#include "sparrow/hash/murmur_hash.h" namespace sparrow { /* compute the hash of key (length len) */ -uint32_t grpc_hash(const void *key, size_t len, uint32_t seed); -uint32_t leveldb_hash(const char *data, size_t n, uint32_t seed); - +inline uint32_t hash32(const void *key, int32_t len, uint32_t seed = 0) { + return murmur_hash_32(key, len, seed); +} +inline uint32_t hash32(const char *data, uint32_t seed = 0) { + return murmur_hash_32(data, strlen(data), seed); +} +inline uint32_t hash32(const std::string &s, uint32_t seed = 0) { + return murmur_hash_32(s.data(), s.size(), seed); +} +template +inline uint32_t hash32(const char (&buff)[SIZE], uint32_t seed = 0) { + return murmur_hash_32(&buff[0], SIZE, seed); +} +template +inline uint32_t hash32(const std::array &buff, uint32_t seed = 0) { + return murmur_hash_32(buff.data(), SIZE, seed); +} } // namespace sparrow -#endif /* SPARROW_HASH_H_ */ +#endif /* SPARROW_INCLUDE_HASH_H_ */ diff --git a/include/sparrow/text_reader.h b/include/sparrow/text_reader.h index da923fe..c59fee2 100644 --- a/include/sparrow/text_reader.h +++ b/include/sparrow/text_reader.h @@ -36,8 +36,8 @@ * } * */ -#ifndef SPARROW_TEXT_READER_H_ -#define SPARROW_TEXT_READER_H_ +#ifndef SPARROW_INCLUDE_TEXT_READER_H_ +#define SPARROW_INCLUDE_TEXT_READER_H_ #include #include @@ -63,11 +63,12 @@ class ALineOfData final { friend class TextFileReader; public: - ALineOfData(/* args */); - ~ALineOfData(); + ALineOfData(/* args */) + : line_(0) {} + ~ALineOfData() {} - uint32_t Line() const; - const std::string &Data() const; + uint32_t Line() const { return line_; } + const std::string &Data() const { return data_; } private: uint32_t line_; @@ -77,20 +78,44 @@ private: class LineIterator final { public: - LineIterator(/* args */); - LineIterator(internal::NextLineInterface *reader, bool eof); - LineIterator(internal::NextLineInterface *reader, const ALineOfData &data, bool eof); - - ~LineIterator(); - - bool operator==(const LineIterator &oth); + LineIterator(/* args */) + : reader_(nullptr) + , end_of_file_(true) {} + LineIterator(internal::NextLineInterface *reader, bool eof) + : reader_(reader) + , end_of_file_(eof) {} + LineIterator(internal::NextLineInterface *reader, const ALineOfData &data, bool eof) + : reader_(reader) + , end_of_file_(eof) + , one_line_data_(data) {} + + ~LineIterator() {} + + bool operator==(const LineIterator &oth) { + if (reader_ == oth.reader_ && end_of_file_ == oth.end_of_file_ && + one_line_data_.line_ == oth.one_line_data_.line_) { + return true; + } + return false; + } bool operator!=(const LineIterator &oth) { return !this->operator==(oth); } - LineIterator &operator++(); - const LineIterator operator++(int); - - ALineOfData &operator*(); - ALineOfData *operator->(); + LineIterator &operator++() { + if (reader_) { + end_of_file_ = !reader_->NextLine(&one_line_data_.data_, &one_line_data_.line_); + } + return *this; + } + const LineIterator operator++(int) { + LineIterator ret = *this; + if (reader_) { + end_of_file_ = !reader_->NextLine(&one_line_data_.data_, &one_line_data_.line_); + } + return ret; + } + + ALineOfData &operator*() { return one_line_data_; } + ALineOfData *operator->() { return &one_line_data_; } private: internal::NextLineInterface *reader_; @@ -102,18 +127,38 @@ class TextFileReader final : public internal::NextLineInterface { friend class LineIterator; public: - TextFileReader() = default; + TextFileReader() = default; ~TextFileReader() = default; TextFileReader(const TextFileReader &) = delete; TextFileReader &operator=(const TextFileReader &) = delete; - bool open(const std::string &path); - LineIterator begin(); - LineIterator end(); + bool open(const std::string &path) { + stream_.open(path); + return stream_.is_open(); + } + LineIterator begin() { + ALineOfData data; + bool eof = !NextLine(&data.data_, &data.line_); + return LineIterator(this, data, eof); + } + LineIterator end() { return LineIterator(this, true); } private: - bool NextLine(std::string *data, uint32_t *line) override; + bool NextLine(std::string *data, uint32_t *line) override { + if (!stream_.is_open() || stream_.eof()) { + data->clear(); + *line = 0; + return false; + } + + (*line)++; + + std::string temp; + std::getline(stream_, temp); + data->swap(temp); + return true; + } std::ifstream stream_; }; @@ -121,19 +166,37 @@ class TextDataReader final : public internal::NextLineInterface { friend class LineIterator; public: - TextDataReader(const char *data, size_t len); + TextDataReader(const char *data, size_t len) { stream_ << std::string(data, len); } ~TextDataReader() = default; TextDataReader(const TextDataReader &) = delete; TextDataReader &operator=(const TextDataReader &) = delete; - LineIterator begin(); - LineIterator end(); + LineIterator begin() { + ALineOfData data; + bool eof = !NextLine(&data.data_, &data.line_); + return LineIterator(this, data, eof); + } + LineIterator end() { return LineIterator(this, true); } private: - bool NextLine(std::string *data, uint32_t *line) override; + bool NextLine(std::string *data, uint32_t *line) override { + if (stream_.eof()) { + data->clear(); + *line = 0; + return false; + } + + (*line)++; + + std::string temp; + std::getline(stream_, temp); + data->swap(temp); + + return true; + } std::stringstream stream_; }; } // namespace sparrow -#endif // SPARROW_TEXT_READER_H_ +#endif // SPARROW_INCLUDE_TEXT_READER_H_ diff --git a/src/os/text_reader.cc b/src/os/text_reader.cc deleted file mode 100644 index ed652f2..0000000 --- a/src/os/text_reader.cc +++ /dev/null @@ -1,127 +0,0 @@ -// -// Copyright 2022 The Sparrow Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#include "sparrow/text_reader.h" - -namespace sparrow { - -ALineOfData::ALineOfData(/* args */) - : line_(0) {} - -ALineOfData::~ALineOfData() {} - -uint32_t ALineOfData::Line() const { return line_; } - -const std::string &ALineOfData::Data() const { return data_; } - -LineIterator::LineIterator(/* args */) - : reader_(nullptr) - , end_of_file_(true) {} - -LineIterator::LineIterator(internal::NextLineInterface *reader, bool eof) - : reader_(reader) - , end_of_file_(eof) {} - -LineIterator::LineIterator(internal::NextLineInterface *reader, const ALineOfData &data, bool eof) - : reader_(reader) - , end_of_file_(eof) - , one_line_data_(data) {} - -LineIterator::~LineIterator() {} - -bool LineIterator::operator==(const LineIterator &oth) { - if (reader_ == oth.reader_ && end_of_file_ == oth.end_of_file_ && - one_line_data_.line_ == oth.one_line_data_.line_) { - return true; - } - return false; -} - -LineIterator &LineIterator::operator++() { - if (reader_) { - end_of_file_ = !reader_->NextLine(&one_line_data_.data_, &one_line_data_.line_); - } - return *this; -} - -const LineIterator LineIterator::operator++(int) { - LineIterator ret = *this; - if (reader_) { - end_of_file_ = !reader_->NextLine(&one_line_data_.data_, &one_line_data_.line_); - } - return ret; -} - -ALineOfData &LineIterator::operator*() { return one_line_data_; } - -ALineOfData *LineIterator::operator->() { return &one_line_data_; } - -// --------------------------------------------- - -bool TextFileReader::open(const std::string &path) { - stream_.open(path); - return stream_.is_open(); -} - -LineIterator TextFileReader::begin() { - ALineOfData data; - bool eof = !NextLine(&data.data_, &data.line_); - return LineIterator(this, data, eof); -} - -LineIterator TextFileReader::end() { return LineIterator(this, true); } - -bool TextFileReader::NextLine(std::string *data, uint32_t *line) { - if (!stream_.is_open() || stream_.eof()) { - data->clear(); - *line = 0; - return false; - } - - (*line)++; - - std::string temp; - std::getline(stream_, temp); - data->swap(temp); - return true; -} - -TextDataReader::TextDataReader(const char *data, size_t len) { stream_ << std::string(data, len); } - -LineIterator TextDataReader::begin() { - ALineOfData data; - bool eof = !NextLine(&data.data_, &data.line_); - return LineIterator(this, data, eof); -} - -LineIterator TextDataReader::end() { return LineIterator(this, true); } - -bool TextDataReader::NextLine(std::string *data, uint32_t *line) { - if (stream_.eof()) { - data->clear(); - *line = 0; - return false; - } - - (*line)++; - - std::string temp; - std::getline(stream_, temp); - data->swap(temp); - - return true; -} -} // namespace sparrow -- Gitee