diff --git a/src/jansson-2.10/src/dump.c b/src/jansson-2.10/src/dump.c index a23fabb7..ad1977a1 100644 --- a/src/jansson-2.10/src/dump.c +++ b/src/jansson-2.10/src/dump.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2009-2016 Petri Lehtinen + * Copyright (c) 2015,2017 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -116,7 +117,7 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v while(end < lim) { - end = utf8_iterate(pos, lim - pos, &codepoint); + end = utf8_iterate(pos, lim - pos, &codepoint, flags & JSON_NO_UTF8); if(!end) return -1; @@ -444,10 +445,11 @@ char *json_dumps(const json_t *json, size_t flags) if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) result = NULL; + else if (flags & JSON_EOL) + result = jsonp_eolstrsteal(&strbuff); else - result = jsonp_strdup(strbuffer_value(&strbuff)); + result = jsonp_strsteal(&strbuff); - strbuffer_close(&strbuff); return result; } diff --git a/src/jansson-2.10/src/jansson.h b/src/jansson-2.10/src/jansson.h index a5927bd6..97f7dcf9 100644 --- a/src/jansson-2.10/src/jansson.h +++ b/src/jansson-2.10/src/jansson.h @@ -290,6 +290,8 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla #define JSON_ESCAPE_SLASH 0x400 #define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11) #define JSON_EMBED 0x10000 +#define JSON_NO_UTF8 0x20000 +#define JSON_EOL 0x40000 typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); diff --git a/src/jansson-2.10/src/jansson_private.h b/src/jansson-2.10/src/jansson_private.h index 5ed96158..f6480d66 100644 --- a/src/jansson-2.10/src/jansson_private.h +++ b/src/jansson-2.10/src/jansson_private.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2009-2016 Petri Lehtinen + * Copyright (c) 2015,2017 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -86,9 +87,14 @@ int jsonp_dtostr(char *buffer, size_t size, double value, int prec); /* Wrappers for custom memory functions */ void* jsonp_malloc(size_t size); void jsonp_free(void *ptr); +void jsonp_free(void *ptr); +void _jsonp_free(void **ptr); +#define jsonp_free(ptr) _jsonp_free((void *)&(ptr)) + char *jsonp_strndup(const char *str, size_t length); char *jsonp_strdup(const char *str); -char *jsonp_strndup(const char *str, size_t len); +char *jsonp_strsteal(strbuffer_t *strbuff); +char *jsonp_eolstrsteal(strbuffer_t *strbuff); /* Windows compatibility */ diff --git a/src/jansson-2.10/src/lookup3.h b/src/jansson-2.10/src/lookup3.h index 522a41ae..632141b1 100644 --- a/src/jansson-2.10/src/lookup3.h +++ b/src/jansson-2.10/src/lookup3.h @@ -284,8 +284,6 @@ static uint32_t hashlittle(const void *key, size_t length, uint32_t initval) case 0 : return c; } -#endif /* !valgrind */ - } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ const uint8_t *k8; diff --git a/src/jansson-2.10/src/memory.c b/src/jansson-2.10/src/memory.c index a2be5d23..ae9680ea 100644 --- a/src/jansson-2.10/src/memory.c +++ b/src/jansson-2.10/src/memory.c @@ -1,6 +1,7 @@ /* * Copyright (c) 2009-2016 Petri Lehtinen * Copyright (c) 2011-2012 Basile Starynkevitch + * Copyright (c) 2015,2017 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. @@ -28,12 +29,13 @@ void *jsonp_malloc(size_t size) return (*do_malloc)(size); } -void jsonp_free(void *ptr) +void _jsonp_free(void **ptr) { - if(!ptr) + if(!*ptr) return; - (*do_free)(ptr); + (*do_free)(*ptr); + *ptr = NULL; } char *jsonp_strdup(const char *str) @@ -54,6 +56,24 @@ char *jsonp_strndup(const char *str, size_t len) return new_str; } +char *jsonp_strsteal(strbuffer_t *strbuff) +{ + size_t len = strbuff->length + 1; + char *ret = realloc(strbuff->value, len); + + return ret; +} + +char *jsonp_eolstrsteal(strbuffer_t *strbuff) +{ + size_t len = strbuff->length + 2; + char *ret = realloc(strbuff->value, len); + + ret[strbuff->length] = '\n'; + ret[strbuff->length + 1] = '\0'; + return ret; +} + void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) { do_malloc = malloc_fn; diff --git a/src/jansson-2.10/src/strbuffer.c b/src/jansson-2.10/src/strbuffer.c index 5e8c0039..36d548c1 100644 --- a/src/jansson-2.10/src/strbuffer.c +++ b/src/jansson-2.10/src/strbuffer.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2009-2016 Petri Lehtinen + * Copyright (c) 2015,2017 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -11,10 +12,11 @@ #include #include +#include #include "jansson_private.h" #include "strbuffer.h" -#define STRBUFFER_MIN_SIZE 16 +#define STRBUFFER_MIN_SIZE 4096 #define STRBUFFER_FACTOR 2 #define STRBUFFER_SIZE_MAX ((size_t)-1) @@ -67,8 +69,10 @@ int strbuffer_append_byte(strbuffer_t *strbuff, char byte) int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) { - if(size >= strbuff->size - strbuff->length) + /* Leave room for EOL and NULL bytes */ + if(size + 2 > strbuff->size - strbuff->length) { + int backoff = 1; size_t new_size; char *new_value; @@ -81,13 +85,14 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) new_size = max(strbuff->size * STRBUFFER_FACTOR, strbuff->length + size + 1); - new_value = jsonp_malloc(new_size); - if(!new_value) - return -1; - - memcpy(new_value, strbuff->value, strbuff->length); + while (42) { + new_value = realloc(strbuff->value, new_size); + if (new_value) + break; + usleep(backoff * 1000); + backoff <<= 1; + } - jsonp_free(strbuff->value); strbuff->value = new_value; strbuff->size = new_size; } diff --git a/src/jansson-2.10/src/utf.c b/src/jansson-2.10/src/utf.c index be966cbd..99ba9f0e 100644 --- a/src/jansson-2.10/src/utf.c +++ b/src/jansson-2.10/src/utf.c @@ -136,17 +136,19 @@ size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint) return 1; } -const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint) +const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint, int noutf8) { - size_t count; + size_t count = 1; int32_t value; if(!bufsize) return buffer; - count = utf8_check_first(buffer[0]); - if(count <= 0) - return NULL; + if (!noutf8) { + count = utf8_check_first(buffer[0]); + if(count <= 0) + return NULL; + } if(count == 1) value = (unsigned char)buffer[0]; diff --git a/src/jansson-2.10/src/utf.h b/src/jansson-2.10/src/utf.h index e182df78..54d00781 100644 --- a/src/jansson-2.10/src/utf.h +++ b/src/jansson-2.10/src/utf.h @@ -20,7 +20,7 @@ int utf8_encode(int32_t codepoint, char *buffer, size_t *size); size_t utf8_check_first(char byte); size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint); -const char *utf8_iterate(const char *buffer, size_t size, int32_t *codepoint); +const char *utf8_iterate(const char *buffer, size_t size, int32_t *codepoint, int noutf8); int utf8_check_string(const char *string, size_t length);