diff --git a/compat/jansson/CMakeLists.txt b/compat/jansson/CMakeLists.txt index 9a73da225..7bd74c671 100644 --- a/compat/jansson/CMakeLists.txt +++ b/compat/jansson/CMakeLists.txt @@ -6,6 +6,8 @@ add_definitions(-DHAVE_CONFIG_H) # Add the lib sources. file(GLOB JANSSON_SRC *.c) +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os") + set(JANSSON_HDR_PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/hashtable.h ${CMAKE_CURRENT_SOURCE_DIR}/jansson_private.h diff --git a/compat/jansson/dump.c b/compat/jansson/dump.c index 6b1aabd45..a23fabb7c 100644 --- a/compat/jansson/dump.c +++ b/compat/jansson/dump.c @@ -9,13 +9,17 @@ #define _GNU_SOURCE #endif +#include "jansson_private.h" + #include #include #include #include +#ifdef HAVE_UNISTD_H +#include +#endif #include "jansson.h" -#include "jansson_private.h" #include "strbuffer.h" #include "utf.h" @@ -25,11 +29,28 @@ #define FLAGS_TO_INDENT(f) ((f) & 0x1F) #define FLAGS_TO_PRECISION(f) (((f) >> 11) & 0x1F) +struct buffer { + const size_t size; + size_t used; + char *data; +}; + static int dump_to_strbuffer(const char *buffer, size_t size, void *data) { return strbuffer_append_bytes((strbuffer_t *)data, buffer, size); } +static int dump_to_buffer(const char *buffer, size_t size, void *data) +{ + struct buffer *buf = (struct buffer *)data; + + if(buf->used + size <= buf->size) + memcpy(&buf->data[buf->used], buffer, size); + + buf->used += size; + return 0; +} + static int dump_to_file(const char *buffer, size_t size, void *data) { FILE *dest = (FILE *)data; @@ -38,6 +59,16 @@ static int dump_to_file(const char *buffer, size_t size, void *data) return 0; } +static int dump_to_fd(const char *buffer, size_t size, void *data) +{ + int *dest = (int *)data; +#ifdef HAVE_UNISTD_H + if(write(*dest, buffer, size) == (ssize_t)size) + return 0; +#endif + return -1; +} + /* 32 spaces (the maximum indentation size) */ static const char whitespace[] = " "; @@ -168,6 +199,10 @@ static int compare_keys(const void *key1, const void *key2) static int do_dump(const json_t *json, size_t flags, int depth, json_dump_callback_t dump, void *data) { + int embed = flags & JSON_EMBED; + + flags &= ~JSON_EMBED; + if(!json) return -1; @@ -227,11 +262,11 @@ static int do_dump(const json_t *json, size_t flags, int depth, n = json_array_size(json); - if(dump("[", 1, data)) + if(!embed && dump("[", 1, data)) goto array_error; if(n == 0) { array->visited = 0; - return dump("]", 1, data); + return embed ? 0 : dump("]", 1, data); } if(dump_indent(flags, depth + 1, 0, dump, data)) goto array_error; @@ -255,7 +290,7 @@ static int do_dump(const json_t *json, size_t flags, int depth, } array->visited = 0; - return dump("]", 1, data); + return embed ? 0 : dump("]", 1, data); array_error: array->visited = 0; @@ -286,11 +321,11 @@ static int do_dump(const json_t *json, size_t flags, int depth, iter = json_object_iter((json_t *)json); - if(dump("{", 1, data)) + if(!embed && dump("{", 1, data)) goto object_error; if(!iter) { object->visited = 0; - return dump("}", 1, data); + return embed ? 0 : dump("}", 1, data); } if(dump_indent(flags, depth + 1, 0, dump, data)) goto object_error; @@ -386,7 +421,7 @@ static int do_dump(const json_t *json, size_t flags, int depth, } object->visited = 0; - return dump("}", 1, data); + return embed ? 0 : dump("}", 1, data); object_error: object->visited = 0; @@ -416,11 +451,26 @@ char *json_dumps(const json_t *json, size_t flags) return result; } +size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags) +{ + struct buffer buf = { size, 0, buffer }; + + if(json_dump_callback(json, dump_to_buffer, (void *)&buf, flags)) + return 0; + + return buf.used; +} + int json_dumpf(const json_t *json, FILE *output, size_t flags) { return json_dump_callback(json, dump_to_file, (void *)output, flags); } +int json_dumpfd(const json_t *json, int output, size_t flags) +{ + return json_dump_callback(json, dump_to_fd, (void *)&output, flags); +} + int json_dump_file(const json_t *json, const char *path, size_t flags) { int result; diff --git a/compat/jansson/hashtable_seed.c b/compat/jansson/hashtable_seed.c index 751e0e326..8aed54068 100644 --- a/compat/jansson/hashtable_seed.c +++ b/compat/jansson/hashtable_seed.c @@ -168,12 +168,12 @@ static uint32_t generate_seed() { int done = 0; #if !defined(_WIN32) && defined(USE_URANDOM) - if (!done && seed_from_urandom(&seed) == 0) + if (seed_from_urandom(&seed) == 0) done = 1; #endif #if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) - if (!done && seed_from_windows_cryptoapi(&seed) == 0) + if (seed_from_windows_cryptoapi(&seed) == 0) done = 1; #endif diff --git a/compat/jansson/jansson.h b/compat/jansson/jansson.h index 591f2a944..a5927bd63 100644 --- a/compat/jansson/jansson.h +++ b/compat/jansson/jansson.h @@ -21,11 +21,11 @@ extern "C" { /* version */ #define JANSSON_MAJOR_VERSION 2 -#define JANSSON_MINOR_VERSION 9 +#define JANSSON_MINOR_VERSION 10 #define JANSSON_MICRO_VERSION 0 /* Micro version is omitted if it's 0 */ -#define JANSSON_VERSION "2.9" +#define JANSSON_VERSION "2.10" /* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */ @@ -273,6 +273,7 @@ typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data); json_t *json_loads(const char *input, size_t flags, json_error_t *error); json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error); json_t *json_loadf(FILE *input, size_t flags, json_error_t *error); +json_t *json_loadfd(int input, size_t flags, json_error_t *error); json_t *json_load_file(const char *path, size_t flags, json_error_t *error); json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error); @@ -288,11 +289,14 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla #define JSON_ENCODE_ANY 0x200 #define JSON_ESCAPE_SLASH 0x400 #define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11) +#define JSON_EMBED 0x10000 typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); char *json_dumps(const json_t *json, size_t flags); +size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags); int json_dumpf(const json_t *json, FILE *output, size_t flags); +int json_dumpfd(const json_t *json, int output, size_t flags); int json_dump_file(const json_t *json, const char *path, size_t flags); int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags); diff --git a/compat/jansson/jansson_private.h b/compat/jansson/jansson_private.h index 4a4927bb2..5ed961582 100644 --- a/compat/jansson/jansson_private.h +++ b/compat/jansson/jansson_private.h @@ -8,6 +8,7 @@ #ifndef JANSSON_PRIVATE_H #define JANSSON_PRIVATE_H +#include "jansson_private_config.h" #include #include "jansson.h" #include "hashtable.h" diff --git a/compat/jansson/load.c b/compat/jansson/load.c index 7a8f96ecb..c212489a1 100644 --- a/compat/jansson/load.c +++ b/compat/jansson/load.c @@ -9,15 +9,19 @@ #define _GNU_SOURCE #endif +#include "jansson_private.h" + #include #include #include #include #include #include +#ifdef HAVE_UNISTD_H +#include +#endif #include "jansson.h" -#include "jansson_private.h" #include "strbuffer.h" #include "utf.h" @@ -340,7 +344,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error) /* control character */ lex_unget_unsave(lex, c); if(c == '\n') - error_set(error, lex, "unexpected newline", c); + error_set(error, lex, "unexpected newline"); else error_set(error, lex, "control character 0x%x", c); goto out; @@ -914,7 +918,7 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error) typedef struct { const char *data; - int pos; + size_t pos; } string_data_t; static int string_get(void *data) @@ -1028,6 +1032,45 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) return result; } +static int fd_get_func(int *fd) +{ + uint8_t c; +#ifdef HAVE_UNISTD_H + if (read(*fd, &c, 1) == 1) + return c; +#endif + return EOF; +} + +json_t *json_loadfd(int input, size_t flags, json_error_t *error) +{ + lex_t lex; + const char *source; + json_t *result; + +#ifdef HAVE_UNISTD_H + if(input == STDIN_FILENO) + source = ""; + else +#endif + source = ""; + + jsonp_error_init(error, source); + + if (input < 0) { + error_set(error, NULL, "wrong arguments"); + return NULL; + } + + if(lex_init(&lex, (get_func)fd_get_func, flags, &input)) + return NULL; + + result = parse_json(&lex, flags, error); + + lex_close(&lex); + return result; +} + json_t *json_load_file(const char *path, size_t flags, json_error_t *error) { json_t *result; diff --git a/version.h b/version.h index 330d96975..a17b3301f 100644 --- a/version.h +++ b/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "Monero (XMR) CPU miner" -#define APP_VERSION "0.8.3" +#define APP_VERSION "0.8.3-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com"