make style

This commit is contained in:
Philippe Teuwen 2019-03-10 00:00:59 +01:00
commit 0373696662
483 changed files with 56514 additions and 52451 deletions

View file

@ -44,7 +44,7 @@ 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)
if (buf->used + size <= buf->size)
memcpy(&buf->data[buf->used], buffer, size);
buf->used += size;
@ -54,7 +54,7 @@ static int dump_to_buffer(const char *buffer, size_t size, void *data)
static int dump_to_file(const char *buffer, size_t size, void *data)
{
FILE *dest = (FILE *)data;
if(fwrite(buffer, size, 1, dest) != 1)
if (fwrite(buffer, size, 1, dest) != 1)
return -1;
return 0;
}
@ -63,7 +63,7 @@ static int dump_to_fd(const char *buffer, size_t size, void *data)
{
#ifdef HAVE_UNISTD_H
int *dest = (int *)data;
if(write(*dest, buffer, size) == (ssize_t)size)
if (write(*dest, buffer, size) == (ssize_t)size)
return 0;
#endif
return -1;
@ -74,25 +74,21 @@ static const char whitespace[] = " ";
static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data)
{
if(FLAGS_TO_INDENT(flags) > 0)
{
if (FLAGS_TO_INDENT(flags) > 0) {
unsigned int ws_count = FLAGS_TO_INDENT(flags), n_spaces = depth * ws_count;
if(dump("\n", 1, data))
if (dump("\n", 1, data))
return -1;
while(n_spaces > 0)
{
while (n_spaces > 0) {
int cur_n = n_spaces < sizeof whitespace - 1 ? n_spaces : sizeof whitespace - 1;
if(dump(whitespace, cur_n, data))
if (dump(whitespace, cur_n, data))
return -1;
n_spaces -= cur_n;
}
}
else if(space && !(flags & JSON_COMPACT))
{
} else if (space && !(flags & JSON_COMPACT)) {
return dump(" ", 1, data);
}
return 0;
@ -103,70 +99,80 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
const char *pos, *end, *lim;
int32_t codepoint = 0;
if(dump("\"", 1, data))
if (dump("\"", 1, data))
return -1;
end = pos = str;
lim = str + len;
while(1)
{
while (1) {
const char *text;
char seq[13];
int length;
while(end < lim)
{
while (end < lim) {
end = utf8_iterate(pos, lim - pos, &codepoint);
if(!end)
if (!end)
return -1;
/* mandatory escape or control char */
if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
if (codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
break;
/* slash */
if((flags & JSON_ESCAPE_SLASH) && codepoint == '/')
if ((flags & JSON_ESCAPE_SLASH) && codepoint == '/')
break;
/* non-ASCII */
if((flags & JSON_ENSURE_ASCII) && codepoint > 0x7F)
if ((flags & JSON_ENSURE_ASCII) && codepoint > 0x7F)
break;
pos = end;
}
if(pos != str) {
if(dump(str, pos - str, data))
if (pos != str) {
if (dump(str, pos - str, data))
return -1;
}
if(end == pos)
if (end == pos)
break;
/* handle \, /, ", and control codes */
length = 2;
switch(codepoint)
{
case '\\': text = "\\\\"; break;
case '\"': text = "\\\""; break;
case '\b': text = "\\b"; break;
case '\f': text = "\\f"; break;
case '\n': text = "\\n"; break;
case '\r': text = "\\r"; break;
case '\t': text = "\\t"; break;
case '/': text = "\\/"; break;
default:
{
switch (codepoint) {
case '\\':
text = "\\\\";
break;
case '\"':
text = "\\\"";
break;
case '\b':
text = "\\b";
break;
case '\f':
text = "\\f";
break;
case '\n':
text = "\\n";
break;
case '\r':
text = "\\r";
break;
case '\t':
text = "\\t";
break;
case '/':
text = "\\/";
break;
default: {
/* codepoint is in BMP */
if(codepoint < 0x10000)
{
if (codepoint < 0x10000) {
snprintf(seq, sizeof(seq), "\\u%04X", (unsigned int)codepoint);
length = 6;
}
/* not in BMP -> construct a UTF-16 surrogate pair */
else
{
else {
int32_t first, last;
codepoint -= 0x10000;
@ -182,7 +188,7 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
}
}
if(dump(text, length, data))
if (dump(text, length, data))
return -1;
str = pos = end;
@ -212,10 +218,10 @@ static int do_dump(const json_t *json, size_t flags, int depth,
flags &= ~JSON_EMBED;
if(!json)
if (!json)
return -1;
switch(json_typeof(json)) {
switch (json_typeof(json)) {
case JSON_NULL:
return dump("null", 4, data);
@ -225,29 +231,27 @@ static int do_dump(const json_t *json, size_t flags, int depth,
case JSON_FALSE:
return dump("false", 5, data);
case JSON_INTEGER:
{
case JSON_INTEGER: {
char buffer[MAX_INTEGER_STR_LENGTH];
int size;
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH,
"%" JSON_INTEGER_FORMAT,
json_integer_value(json));
if(size < 0 || size >= MAX_INTEGER_STR_LENGTH)
if (size < 0 || size >= MAX_INTEGER_STR_LENGTH)
return -1;
return dump(buffer, size, data);
}
case JSON_REAL:
{
case JSON_REAL: {
char buffer[MAX_REAL_STR_LENGTH];
int size;
double value = json_real_value(json);
size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value,
FLAGS_TO_PRECISION(flags));
if(size < 0)
if (size < 0)
return -1;
return dump(buffer, size, data);
@ -256,8 +260,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
case JSON_STRING:
return dump_string(json_string_value(json), json_string_length(json), dump, data, flags);
case JSON_ARRAY:
{
case JSON_ARRAY: {
size_t n;
size_t i;
/* Space for "0x", double the sizeof a pointer for the hex and a terminator. */
@ -269,29 +272,26 @@ static int do_dump(const json_t *json, size_t flags, int depth,
n = json_array_size(json);
if(!embed && dump("[", 1, data))
if (!embed && dump("[", 1, data))
return -1;
if(n == 0) {
if (n == 0) {
hashtable_del(parents, key);
return embed ? 0 : dump("]", 1, data);
}
if(dump_indent(flags, depth + 1, 0, dump, data))
if (dump_indent(flags, depth + 1, 0, dump, data))
return -1;
for(i = 0; i < n; ++i) {
if(do_dump(json_array_get(json, i), flags, depth + 1,
parents, dump, data))
for (i = 0; i < n; ++i) {
if (do_dump(json_array_get(json, i), flags, depth + 1,
parents, dump, data))
return -1;
if(i < n - 1)
{
if(dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
if (i < n - 1) {
if (dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
return -1;
}
else
{
if(dump_indent(flags, depth, 0, dump, data))
} else {
if (dump_indent(flags, depth, 0, dump, data))
return -1;
}
}
@ -300,19 +300,17 @@ static int do_dump(const json_t *json, size_t flags, int depth,
return embed ? 0 : dump("]", 1, data);
}
case JSON_OBJECT:
{
case JSON_OBJECT: {
void *iter;
const char *separator;
int separator_length;
/* Space for "0x", double the sizeof a pointer for the hex and a terminator. */
char loop_key[2 + (sizeof(json) * 2) + 1];
if(flags & JSON_COMPACT) {
if (flags & JSON_COMPACT) {
separator = ":";
separator_length = 1;
}
else {
} else {
separator = ": ";
separator_length = 2;
}
@ -323,28 +321,26 @@ static int do_dump(const json_t *json, size_t flags, int depth,
iter = json_object_iter((json_t *)json);
if(!embed && dump("{", 1, data))
if (!embed && dump("{", 1, data))
return -1;
if(!iter) {
if (!iter) {
hashtable_del(parents, loop_key);
return embed ? 0 : dump("}", 1, data);
}
if(dump_indent(flags, depth + 1, 0, dump, data))
if (dump_indent(flags, depth + 1, 0, dump, data))
return -1;
if(flags & JSON_SORT_KEYS)
{
if (flags & JSON_SORT_KEYS) {
const char **keys;
size_t size, i;
size = json_object_size(json);
keys = jsonp_malloc(size * sizeof(const char *));
if(!keys)
if (!keys)
return -1;
i = 0;
while(iter)
{
while (iter) {
keys[i] = json_object_iter_key(iter);
iter = json_object_iter_next((json_t *)json, iter);
i++;
@ -353,8 +349,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
qsort(keys, size, sizeof(const char *), compare_keys);
for(i = 0; i < size; i++)
{
for (i = 0; i < size; i++) {
const char *key;
json_t *value;
@ -363,26 +358,20 @@ static int do_dump(const json_t *json, size_t flags, int depth,
assert(value);
dump_string(key, strlen(key), dump, data, flags);
if(dump(separator, separator_length, data) ||
do_dump(value, flags, depth + 1, parents, dump, data))
{
if (dump(separator, separator_length, data) ||
do_dump(value, flags, depth + 1, parents, dump, data)) {
jsonp_free(keys);
return -1;
}
if(i < size - 1)
{
if(dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
{
if (i < size - 1) {
if (dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data)) {
jsonp_free(keys);
return -1;
}
}
else
{
if(dump_indent(flags, depth, 0, dump, data))
{
} else {
if (dump_indent(flags, depth, 0, dump, data)) {
jsonp_free(keys);
return -1;
}
@ -390,31 +379,25 @@ static int do_dump(const json_t *json, size_t flags, int depth,
}
jsonp_free(keys);
}
else
{
} else {
/* Don't sort keys */
while(iter)
{
while (iter) {
void *next = json_object_iter_next((json_t *)json, iter);
const char *key = json_object_iter_key(iter);
dump_string(key, strlen(key), dump, data, flags);
if(dump(separator, separator_length, data) ||
do_dump(json_object_iter_value(iter), flags, depth + 1,
parents, dump, data))
if (dump(separator, separator_length, data) ||
do_dump(json_object_iter_value(iter), flags, depth + 1,
parents, dump, data))
return -1;
if(next)
{
if(dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
if (next) {
if (dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
return -1;
}
else
{
if(dump_indent(flags, depth, 0, dump, data))
} else {
if (dump_indent(flags, depth, 0, dump, data))
return -1;
}
@ -437,10 +420,10 @@ char *json_dumps(const json_t *json, size_t flags)
strbuffer_t strbuff;
char *result;
if(strbuffer_init(&strbuff))
if (strbuffer_init(&strbuff))
return NULL;
if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
if (json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
result = NULL;
else
result = jsonp_strdup(strbuffer_value(&strbuff));
@ -453,7 +436,7 @@ 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))
if (json_dump_callback(json, dump_to_buffer, (void *)&buf, flags))
return 0;
return buf.used;
@ -474,12 +457,12 @@ int json_dump_file(const json_t *json, const char *path, size_t flags)
int result;
FILE *output = fopen(path, "w");
if(!output)
if (!output)
return -1;
result = json_dumpf(json, output, flags);
if(fclose(output) != 0)
if (fclose(output) != 0)
return -1;
return result;
@ -490,9 +473,9 @@ int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *
int res;
hashtable_t parents_set;
if(!(flags & JSON_ENCODE_ANY)) {
if(!json_is_array(json) && !json_is_object(json))
return -1;
if (!(flags & JSON_ENCODE_ANY)) {
if (!json_is_array(json) && !json_is_object(json))
return -1;
}
if (hashtable_init(&parents_set))

View file

@ -8,7 +8,7 @@ void jsonp_error_init(json_error_t *error, const char *source)
error->line = -1;
error->column = -1;
error->position = 0;
if(source)
if (source)
jsonp_error_set_source(error, source);
else
error->source[0] = '\0';

View file

@ -65,13 +65,10 @@ static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
list_t *list)
{
if(bucket_is_empty(hashtable, bucket))
{
if (bucket_is_empty(hashtable, bucket)) {
list_insert(&hashtable->list, list);
bucket->first = bucket->last = list;
}
else
{
} else {
list_insert(bucket->first, list);
bucket->first = list;
}
@ -83,17 +80,16 @@ static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
list_t *list;
pair_t *pair;
if(bucket_is_empty(hashtable, bucket))
if (bucket_is_empty(hashtable, bucket))
return NULL;
list = bucket->first;
while(1)
{
while (1) {
pair = list_to_pair(list);
if(pair->hash == hash && strcmp(pair->key, key) == 0)
if (pair->hash == hash && strcmp(pair->key, key) == 0)
return pair;
if(list == bucket->last)
if (list == bucket->last)
break;
list = list->next;
@ -114,16 +110,16 @@ static int hashtable_do_del(hashtable_t *hashtable,
bucket = &hashtable->buckets[index];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
if(!pair)
if (!pair)
return -1;
if(&pair->list == bucket->first && &pair->list == bucket->last)
if (&pair->list == bucket->first && &pair->list == bucket->last)
bucket->first = bucket->last = &hashtable->list;
else if(&pair->list == bucket->first)
else if (&pair->list == bucket->first)
bucket->first = pair->list.next;
else if(&pair->list == bucket->last)
else if (&pair->list == bucket->last)
bucket->last = pair->list.prev;
list_remove(&pair->list);
@ -141,8 +137,7 @@ static void hashtable_do_clear(hashtable_t *hashtable)
list_t *list, *next;
pair_t *pair;
for(list = hashtable->list.next; list != &hashtable->list; list = next)
{
for (list = hashtable->list.next; list != &hashtable->list; list = next) {
next = list->next;
pair = list_to_pair(list);
json_decref(pair->value);
@ -161,23 +156,22 @@ static int hashtable_do_rehash(hashtable_t *hashtable)
new_size = hashsize(new_order);
new_buckets = jsonp_malloc(new_size * sizeof(bucket_t));
if(!new_buckets)
if (!new_buckets)
return -1;
jsonp_free(hashtable->buckets);
hashtable->buckets = new_buckets;
hashtable->order = new_order;
for(i = 0; i < hashsize(hashtable->order); i++)
{
for (i = 0; i < hashsize(hashtable->order); i++) {
hashtable->buckets[i].first = hashtable->buckets[i].last =
&hashtable->list;
&hashtable->list;
}
list = hashtable->list.next;
list_init(&hashtable->list);
for(; list != &hashtable->list; list = next) {
for (; list != &hashtable->list; list = next) {
next = list->next;
pair = list_to_pair(list);
index = pair->hash % new_size;
@ -195,16 +189,15 @@ int hashtable_init(hashtable_t *hashtable)
hashtable->size = 0;
hashtable->order = INITIAL_HASHTABLE_ORDER;
hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t));
if(!hashtable->buckets)
if (!hashtable->buckets)
return -1;
list_init(&hashtable->list);
list_init(&hashtable->ordered_list);
for(i = 0; i < hashsize(hashtable->order); i++)
{
for (i = 0; i < hashsize(hashtable->order); i++) {
hashtable->buckets[i].first = hashtable->buckets[i].last =
&hashtable->list;
&hashtable->list;
}
return 0;
@ -223,8 +216,8 @@ int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value)
size_t hash, index;
/* rehash if the load ratio exceeds 1 */
if(hashtable->size >= hashsize(hashtable->order))
if(hashtable_do_rehash(hashtable))
if (hashtable->size >= hashsize(hashtable->order))
if (hashtable_do_rehash(hashtable))
return -1;
hash = hash_str(key);
@ -232,25 +225,22 @@ int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value)
bucket = &hashtable->buckets[index];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
if(pair)
{
if (pair) {
json_decref(pair->value);
pair->value = value;
}
else
{
} else {
/* offsetof(...) returns the size of pair_t without the last,
flexible member. This way, the correct amount is
allocated. */
size_t len = strlen(key);
if(len >= (size_t)-1 - offsetof(pair_t, key)) {
if (len >= (size_t) -1 - offsetof(pair_t, key)) {
/* Avoid an overflow if the key is very long */
return -1;
}
pair = jsonp_malloc(offsetof(pair_t, key) + len + 1);
if(!pair)
if (!pair)
return -1;
pair->hash = hash;
@ -277,7 +267,7 @@ void *hashtable_get(hashtable_t *hashtable, const char *key)
bucket = &hashtable->buckets[hash & hashmask(hashtable->order)];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
if(!pair)
if (!pair)
return NULL;
return pair->value;
@ -295,10 +285,9 @@ void hashtable_clear(hashtable_t *hashtable)
hashtable_do_clear(hashtable);
for(i = 0; i < hashsize(hashtable->order); i++)
{
for (i = 0; i < hashsize(hashtable->order); i++) {
hashtable->buckets[i].first = hashtable->buckets[i].last =
&hashtable->list;
&hashtable->list;
}
list_init(&hashtable->list);
@ -321,7 +310,7 @@ void *hashtable_iter_at(hashtable_t *hashtable, const char *key)
bucket = &hashtable->buckets[hash & hashmask(hashtable->order)];
pair = hashtable_find_pair(hashtable, bucket, key, hash);
if(!pair)
if (!pair)
return NULL;
return &pair->ordered_list;
@ -330,7 +319,7 @@ void *hashtable_iter_at(hashtable_t *hashtable, const char *key)
void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
{
list_t *list = (list_t *)iter;
if(list->next == &hashtable->ordered_list)
if (list->next == &hashtable->ordered_list)
return NULL;
return list->next;
}

View file

@ -45,7 +45,8 @@
#include "jansson.h"
static uint32_t buf_to_uint32(char *data) {
static uint32_t buf_to_uint32(char *data)
{
size_t i;
uint32_t result = 0;
@ -59,7 +60,8 @@ static uint32_t buf_to_uint32(char *data) {
/* /dev/urandom */
#if !defined(_WIN32) && defined(USE_URANDOM)
static int seed_from_urandom(uint32_t *seed) {
static int seed_from_urandom(uint32_t *seed)
{
/* Use unbuffered I/O if we have open(), close() and read(). Otherwise
fall back to fopen() */
@ -112,7 +114,7 @@ static int seed_from_windows_cryptoapi(uint32_t *seed)
int ok;
hAdvAPI32 = GetModuleHandle(TEXT("advapi32.dll"));
if(hAdvAPI32 == NULL)
if (hAdvAPI32 == NULL)
return 1;
pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA");
@ -142,7 +144,8 @@ static int seed_from_windows_cryptoapi(uint32_t *seed)
#endif
/* gettimeofday() and getpid() */
static int seed_from_timestamp_and_pid(uint32_t *seed) {
static int seed_from_timestamp_and_pid(uint32_t *seed)
{
#ifdef HAVE_GETTIMEOFDAY
/* XOR of seconds and microseconds */
struct timeval tv;
@ -163,7 +166,8 @@ static int seed_from_timestamp_and_pid(uint32_t *seed) {
return 0;
}
static uint32_t generate_seed() {
static uint32_t generate_seed()
{
uint32_t seed = 0;
int done = 0;
@ -196,7 +200,8 @@ volatile uint32_t hashtable_seed = 0;
#if defined(HAVE_ATOMIC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32))
static volatile char seed_initialized = 0;
void json_object_seed(size_t seed) {
void json_object_seed(size_t seed)
{
uint32_t new_seed = (uint32_t)seed;
if (hashtable_seed == 0) {
@ -212,12 +217,13 @@ void json_object_seed(size_t seed) {
#ifdef HAVE_SCHED_YIELD
sched_yield();
#endif
} while(__atomic_load_n(&hashtable_seed, __ATOMIC_ACQUIRE) == 0);
} while (__atomic_load_n(&hashtable_seed, __ATOMIC_ACQUIRE) == 0);
}
}
}
#elif defined(HAVE_SYNC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32))
void json_object_seed(size_t seed) {
void json_object_seed(size_t seed)
{
uint32_t new_seed = (uint32_t)seed;
if (hashtable_seed == 0) {
@ -239,12 +245,13 @@ void json_object_seed(size_t seed) {
sched_yield();
#endif
}
} while(hashtable_seed == 0);
} while (hashtable_seed == 0);
}
}
#elif defined(_WIN32)
static long seed_initialized = 0;
void json_object_seed(size_t seed) {
void json_object_seed(size_t seed)
{
uint32_t new_seed = (uint32_t)seed;
if (hashtable_seed == 0) {
@ -264,7 +271,8 @@ void json_object_seed(size_t seed) {
}
#else
/* Fall back to a thread-unsafe version */
void json_object_seed(size_t seed) {
void json_object_seed(size_t seed)
{
uint32_t new_seed = (uint32_t)seed;
if (hashtable_seed == 0) {

View file

@ -120,7 +120,7 @@ json_t *json_null(void);
static JSON_INLINE
json_t *json_incref(json_t *json)
{
if(json && json->refcount != (size_t)-1)
if (json && json->refcount != (size_t) -1)
JSON_INTERNAL_INCREF(json);
return json;
}
@ -131,7 +131,7 @@ void json_delete(json_t *json);
static JSON_INLINE
void json_decref(json_t *json)
{
if(json && json->refcount != (size_t)-1 && JSON_INTERNAL_DECREF(json) == 0)
if (json && json->refcount != (size_t) -1 && JSON_INTERNAL_DECREF(json) == 0)
json_delete(json);
}
@ -139,9 +139,9 @@ void json_decref(json_t *json)
static JSON_INLINE
void json_decrefp(json_t **json)
{
if(json) {
if (json) {
json_decref(*json);
*json = NULL;
*json = NULL;
}
}
@ -183,7 +183,8 @@ enum json_error_code {
json_error_index_out_of_range
};
static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) {
static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e)
{
return (enum json_error_code)e->text[JSON_ERROR_TEXT_LENGTH - 1];
}
@ -214,10 +215,10 @@ int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
#define json_object_foreach_safe(object, n, key, value) \
for(key = json_object_iter_key(json_object_iter(object)), \
n = json_object_iter_next(object, json_object_key_to_iter(key)); \
n = json_object_iter_next(object, json_object_key_to_iter(key)); \
key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
key = json_object_iter_key(n), \
n = json_object_iter_next(object, json_object_key_to_iter(key)))
n = json_object_iter_next(object, json_object_key_to_iter(key)))
#define json_array_foreach(array, index, value) \
for(index = 0; \

View file

@ -87,7 +87,7 @@ int jsonp_strtod(strbuffer_t *strbuffer, double *out);
int jsonp_dtostr(char *buffer, size_t size, double value, int prec);
/* Wrappers for custom memory functions */
void* jsonp_malloc(size_t size) JANSSON_ATTRS(warn_unused_result);
void *jsonp_malloc(size_t size) JANSSON_ATTRS(warn_unused_result);
void jsonp_free(void *ptr);
char *jsonp_strndup(const char *str, size_t length) JANSSON_ATTRS(warn_unused_result);
char *jsonp_strdup(const char *str) JANSSON_ATTRS(warn_unused_result);

View file

@ -95,7 +95,7 @@ static void error_set(json_error_t *error, const lex_t *lex,
size_t pos = 0;
const char *result = msg_text;
if(!error)
if (!error)
return;
va_start(ap, msg);
@ -103,33 +103,28 @@ static void error_set(json_error_t *error, const lex_t *lex,
msg_text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
va_end(ap);
if(lex)
{
if (lex) {
const char *saved_text = strbuffer_value(&lex->saved_text);
line = lex->stream.line;
col = lex->stream.column;
pos = lex->stream.position;
if(saved_text && saved_text[0])
{
if(lex->saved_text.length <= 20) {
if (saved_text && saved_text[0]) {
if (lex->saved_text.length <= 20) {
snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, "%s near '%s'", msg_text, saved_text);
msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
result = msg_with_context;
}
}
else
{
if(code == json_error_invalid_syntax) {
} else {
if (code == json_error_invalid_syntax) {
/* More specific error code for premature end of file. */
code = json_error_premature_end_of_input;
}
if(lex->stream.state == STREAM_STATE_ERROR) {
if (lex->stream.state == STREAM_STATE_ERROR) {
/* No context for UTF-8 decoding errors */
result = msg_text;
}
else {
} else {
snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, "%s near end of file", msg_text);
msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
result = msg_with_context;
@ -161,13 +156,12 @@ static int stream_get(stream_t *stream, json_error_t *error)
{
int c;
if(stream->state != STREAM_STATE_OK)
if (stream->state != STREAM_STATE_OK)
return stream->state;
if(!stream->buffer[stream->buffer_pos])
{
if (!stream->buffer[stream->buffer_pos]) {
c = stream->get(stream->data);
if(c == EOF) {
if (c == EOF) {
stream->state = STREAM_STATE_EOF;
return STREAM_STATE_EOF;
}
@ -175,38 +169,35 @@ static int stream_get(stream_t *stream, json_error_t *error)
stream->buffer[0] = c;
stream->buffer_pos = 0;
if(0x80 <= c && c <= 0xFF)
{
if (0x80 <= c && c <= 0xFF) {
/* multi-byte UTF-8 sequence */
size_t i, count;
count = utf8_check_first(c);
if(!count)
if (!count)
goto out;
assert(count >= 2);
for(i = 1; i < count; i++)
for (i = 1; i < count; i++)
stream->buffer[i] = stream->get(stream->data);
if(!utf8_check_full(stream->buffer, count, NULL))
if (!utf8_check_full(stream->buffer, count, NULL))
goto out;
stream->buffer[count] = '\0';
}
else
} else
stream->buffer[1] = '\0';
}
c = stream->buffer[stream->buffer_pos++];
stream->position++;
if(c == '\n') {
if (c == '\n') {
stream->line++;
stream->last_column = stream->column;
stream->column = 0;
}
else if(utf8_check_first(c)) {
} else if (utf8_check_first(c)) {
/* track the Unicode character column, so increment only if
this is the first character of a UTF-8 sequence */
stream->column++;
@ -222,15 +213,14 @@ out:
static void stream_unget(stream_t *stream, int c)
{
if(c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR)
if (c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR)
return;
stream->position--;
if(c == '\n') {
if (c == '\n') {
stream->line--;
stream->column = stream->last_column;
}
else if(utf8_check_first(c))
} else if (utf8_check_first(c))
stream->column--;
assert(stream->buffer_pos > 0);
@ -252,7 +242,7 @@ static void lex_save(lex_t *lex, int c)
static int lex_get_save(lex_t *lex, json_error_t *error)
{
int c = stream_get(&lex->stream, error);
if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR)
if (c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR)
lex_save(lex, c);
return c;
}
@ -264,18 +254,18 @@ static void lex_unget(lex_t *lex, int c)
static void lex_unget_unsave(lex_t *lex, int c)
{
if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) {
if (c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) {
/* Since we treat warnings as errors, when assertions are turned
* off the "d" variable would be set but never used. Which is
* treated as an error by GCC.
*/
#ifndef NDEBUG
#ifndef NDEBUG
char d;
#endif
#endif
stream_unget(&lex->stream, c);
#ifndef NDEBUG
#ifndef NDEBUG
d =
#endif
#endif
strbuffer_pop(&lex->saved_text);
assert(c == d);
}
@ -283,8 +273,7 @@ static void lex_unget_unsave(lex_t *lex, int c)
static void lex_save_cached(lex_t *lex)
{
while(lex->stream.buffer[lex->stream.buffer_pos] != '\0')
{
while (lex->stream.buffer[lex->stream.buffer_pos] != '\0') {
lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]);
lex->stream.buffer_pos++;
lex->stream.position++;
@ -306,14 +295,14 @@ static int32_t decode_unicode_escape(const char *str)
assert(str[0] == 'u');
for(i = 1; i <= 4; i++) {
for (i = 1; i <= 4; i++) {
char c = str[i];
value <<= 4;
if(l_isdigit(c))
if (l_isdigit(c))
value += c - '0';
else if(l_islower(c))
else if (l_islower(c))
value += c - 'a' + 10;
else if(l_isupper(c))
else if (l_isupper(c))
value += c - 'A' + 10;
else
return -1;
@ -334,46 +323,44 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
c = lex_get_save(lex, error);
while(c != '"') {
if(c == STREAM_STATE_ERROR)
while (c != '"') {
if (c == STREAM_STATE_ERROR)
goto out;
else if(c == STREAM_STATE_EOF) {
else if (c == STREAM_STATE_EOF) {
error_set(error, lex, json_error_premature_end_of_input, "premature end of input");
goto out;
}
else if(0 <= c && c <= 0x1F) {
else if (0 <= c && c <= 0x1F) {
/* control character */
lex_unget_unsave(lex, c);
if(c == '\n')
if (c == '\n')
error_set(error, lex, json_error_invalid_syntax, "unexpected newline");
else
error_set(error, lex, json_error_invalid_syntax, "control character 0x%x", c);
goto out;
}
else if(c == '\\') {
else if (c == '\\') {
c = lex_get_save(lex, error);
if(c == 'u') {
if (c == 'u') {
c = lex_get_save(lex, error);
for(i = 0; i < 4; i++) {
if(!l_isxdigit(c)) {
for (i = 0; i < 4; i++) {
if (!l_isxdigit(c)) {
error_set(error, lex, json_error_invalid_syntax, "invalid escape");
goto out;
}
c = lex_get_save(lex, error);
}
}
else if(c == '"' || c == '\\' || c == '/' || c == 'b' ||
c == 'f' || c == 'n' || c == 'r' || c == 't')
} else if (c == '"' || c == '\\' || c == '/' || c == 'b' ||
c == 'f' || c == 'n' || c == 'r' || c == 't')
c = lex_get_save(lex, error);
else {
error_set(error, lex, json_error_invalid_syntax, "invalid escape");
goto out;
}
}
else
} else
c = lex_get_save(lex, error);
}
@ -385,7 +372,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
are converted to 4 bytes
*/
t = jsonp_malloc(lex->saved_text.length + 1);
if(!t) {
if (!t) {
/* this is not very nice, since TOKEN_INVALID is returned */
goto out;
}
@ -394,38 +381,37 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
/* + 1 to skip the " */
p = strbuffer_value(&lex->saved_text) + 1;
while(*p != '"') {
if(*p == '\\') {
while (*p != '"') {
if (*p == '\\') {
p++;
if(*p == 'u') {
if (*p == 'u') {
size_t length;
int32_t value;
value = decode_unicode_escape(p);
if(value < 0) {
if (value < 0) {
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode escape '%.6s'", p - 1);
goto out;
}
p += 5;
if(0xD800 <= value && value <= 0xDBFF) {
if (0xD800 <= value && value <= 0xDBFF) {
/* surrogate pair */
if(*p == '\\' && *(p + 1) == 'u') {
if (*p == '\\' && *(p + 1) == 'u') {
int32_t value2 = decode_unicode_escape(++p);
if(value2 < 0) {
if (value2 < 0) {
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode escape '%.6s'", p - 1);
goto out;
}
p += 5;
if(0xDC00 <= value2 && value2 <= 0xDFFF) {
if (0xDC00 <= value2 && value2 <= 0xDFFF) {
/* valid second surrogate */
value =
((value - 0xD800) << 10) +
(value2 - 0xDC00) +
0x10000;
}
else {
} else {
/* invalid second surrogate */
error_set(error, lex,
json_error_invalid_syntax,
@ -433,39 +419,49 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
value, value2);
goto out;
}
}
else {
} else {
/* no second surrogate */
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode '\\u%04X'",
value);
goto out;
}
}
else if(0xDC00 <= value && value <= 0xDFFF) {
} else if (0xDC00 <= value && value <= 0xDFFF) {
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode '\\u%04X'", value);
goto out;
}
if(utf8_encode(value, t, &length))
if (utf8_encode(value, t, &length))
assert(0);
t += length;
}
else {
switch(*p) {
case '"': case '\\': case '/':
*t = *p; break;
case 'b': *t = '\b'; break;
case 'f': *t = '\f'; break;
case 'n': *t = '\n'; break;
case 'r': *t = '\r'; break;
case 't': *t = '\t'; break;
default: assert(0);
} else {
switch (*p) {
case '"':
case '\\':
case '/':
*t = *p;
break;
case 'b':
*t = '\b';
break;
case 'f':
*t = '\f';
break;
case 'n':
*t = '\n';
break;
case 'r':
*t = '\r';
break;
case 't':
*t = '\t';
break;
default:
assert(0);
}
t++;
p++;
}
}
else
} else
*(t++) = *(p++);
}
*t = '\0';
@ -497,29 +493,26 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
lex->token = TOKEN_INVALID;
if(c == '-')
if (c == '-')
c = lex_get_save(lex, error);
if(c == '0') {
if (c == '0') {
c = lex_get_save(lex, error);
if(l_isdigit(c)) {
if (l_isdigit(c)) {
lex_unget_unsave(lex, c);
goto out;
}
}
else if(l_isdigit(c)) {
} else if (l_isdigit(c)) {
do
c = lex_get_save(lex, error);
while(l_isdigit(c));
}
else {
while (l_isdigit(c));
} else {
lex_unget_unsave(lex, c);
goto out;
}
if(!(lex->flags & JSON_DECODE_INT_AS_REAL) &&
c != '.' && c != 'E' && c != 'e')
{
if (!(lex->flags & JSON_DECODE_INT_AS_REAL) &&
c != '.' && c != 'E' && c != 'e') {
json_int_t intval;
lex_unget_unsave(lex, c);
@ -528,8 +521,8 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
errno = 0;
intval = json_strtoint(saved_text, &end, 10);
if(errno == ERANGE) {
if(intval < 0)
if (errno == ERANGE) {
if (intval < 0)
error_set(error, lex, json_error_numeric_overflow, "too big negative integer");
else
error_set(error, lex, json_error_numeric_overflow, "too big integer");
@ -543,9 +536,9 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
return 0;
}
if(c == '.') {
if (c == '.') {
c = lex_get(lex, error);
if(!l_isdigit(c)) {
if (!l_isdigit(c)) {
lex_unget(lex, c);
goto out;
}
@ -553,27 +546,27 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
do
c = lex_get_save(lex, error);
while(l_isdigit(c));
while (l_isdigit(c));
}
if(c == 'E' || c == 'e') {
if (c == 'E' || c == 'e') {
c = lex_get_save(lex, error);
if(c == '+' || c == '-')
if (c == '+' || c == '-')
c = lex_get_save(lex, error);
if(!l_isdigit(c)) {
if (!l_isdigit(c)) {
lex_unget_unsave(lex, c);
goto out;
}
do
c = lex_get_save(lex, error);
while(l_isdigit(c));
while (l_isdigit(c));
}
lex_unget_unsave(lex, c);
if(jsonp_strtod(&lex->saved_text, &doubleval)) {
if (jsonp_strtod(&lex->saved_text, &doubleval)) {
error_set(error, lex, json_error_numeric_overflow, "real number overflow");
goto out;
}
@ -592,52 +585,52 @@ static int lex_scan(lex_t *lex, json_error_t *error)
strbuffer_clear(&lex->saved_text);
if(lex->token == TOKEN_STRING)
if (lex->token == TOKEN_STRING)
lex_free_string(lex);
do
c = lex_get(lex, error);
while(c == ' ' || c == '\t' || c == '\n' || c == '\r');
while (c == ' ' || c == '\t' || c == '\n' || c == '\r');
if(c == STREAM_STATE_EOF) {
if (c == STREAM_STATE_EOF) {
lex->token = TOKEN_EOF;
goto out;
}
if(c == STREAM_STATE_ERROR) {
if (c == STREAM_STATE_ERROR) {
lex->token = TOKEN_INVALID;
goto out;
}
lex_save(lex, c);
if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
if (c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
lex->token = c;
else if(c == '"')
else if (c == '"')
lex_scan_string(lex, error);
else if(l_isdigit(c) || c == '-') {
if(lex_scan_number(lex, c, error))
else if (l_isdigit(c) || c == '-') {
if (lex_scan_number(lex, c, error))
goto out;
}
else if(l_isalpha(c)) {
else if (l_isalpha(c)) {
/* eat up the whole identifier for clearer error messages */
const char *saved_text;
do
c = lex_get_save(lex, error);
while(l_isalpha(c));
while (l_isalpha(c));
lex_unget_unsave(lex, c);
saved_text = strbuffer_value(&lex->saved_text);
if(strcmp(saved_text, "true") == 0)
if (strcmp(saved_text, "true") == 0)
lex->token = TOKEN_TRUE;
else if(strcmp(saved_text, "false") == 0)
else if (strcmp(saved_text, "false") == 0)
lex->token = TOKEN_FALSE;
else if(strcmp(saved_text, "null") == 0)
else if (strcmp(saved_text, "null") == 0)
lex->token = TOKEN_NULL;
else
lex->token = TOKEN_INVALID;
@ -657,7 +650,7 @@ out:
static char *lex_steal_string(lex_t *lex, size_t *out_len)
{
char *result = NULL;
if(lex->token == TOKEN_STRING) {
if (lex->token == TOKEN_STRING) {
result = lex->value.string.val;
*out_len = lex->value.string.len;
lex->value.string.val = NULL;
@ -669,7 +662,7 @@ static char *lex_steal_string(lex_t *lex, size_t *out_len)
static int lex_init(lex_t *lex, get_func get, size_t flags, void *data)
{
stream_init(&lex->stream, get, data);
if(strbuffer_init(&lex->saved_text))
if (strbuffer_init(&lex->saved_text))
return -1;
lex->flags = flags;
@ -679,7 +672,7 @@ static int lex_init(lex_t *lex, get_func get, size_t flags, void *data)
static void lex_close(lex_t *lex)
{
if(lex->token == TOKEN_STRING)
if (lex->token == TOKEN_STRING)
lex_free_string(lex);
strbuffer_close(&lex->saved_text);
}
@ -692,25 +685,25 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error);
static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
{
json_t *object = json_object();
if(!object)
if (!object)
return NULL;
lex_scan(lex, error);
if(lex->token == '}')
if (lex->token == '}')
return object;
while(1) {
while (1) {
char *key;
size_t len;
json_t *value;
if(lex->token != TOKEN_STRING) {
if (lex->token != TOKEN_STRING) {
error_set(error, lex, json_error_invalid_syntax, "string or '}' expected");
goto error;
}
key = lex_steal_string(lex, &len);
if(!key)
if (!key)
return NULL;
if (memchr(key, '\0', len)) {
jsonp_free(key);
@ -718,8 +711,8 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
goto error;
}
if(flags & JSON_REJECT_DUPLICATES) {
if(json_object_get(object, key)) {
if (flags & JSON_REJECT_DUPLICATES) {
if (json_object_get(object, key)) {
jsonp_free(key);
error_set(error, lex, json_error_duplicate_key, "duplicate object key");
goto error;
@ -727,7 +720,7 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
}
lex_scan(lex, error);
if(lex->token != ':') {
if (lex->token != ':') {
jsonp_free(key);
error_set(error, lex, json_error_invalid_syntax, "':' expected");
goto error;
@ -735,12 +728,12 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
lex_scan(lex, error);
value = parse_value(lex, flags, error);
if(!value) {
if (!value) {
jsonp_free(key);
goto error;
}
if(json_object_set_new_nocheck(object, key, value)) {
if (json_object_set_new_nocheck(object, key, value)) {
jsonp_free(key);
goto error;
}
@ -748,13 +741,13 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
jsonp_free(key);
lex_scan(lex, error);
if(lex->token != ',')
if (lex->token != ',')
break;
lex_scan(lex, error);
}
if(lex->token != '}') {
if (lex->token != '}') {
error_set(error, lex, json_error_invalid_syntax, "'}' expected");
goto error;
}
@ -769,30 +762,30 @@ error:
static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error)
{
json_t *array = json_array();
if(!array)
if (!array)
return NULL;
lex_scan(lex, error);
if(lex->token == ']')
if (lex->token == ']')
return array;
while(lex->token) {
while (lex->token) {
json_t *elem = parse_value(lex, flags, error);
if(!elem)
if (!elem)
goto error;
if(json_array_append_new(array, elem)) {
if (json_array_append_new(array, elem)) {
goto error;
}
lex_scan(lex, error);
if(lex->token != ',')
if (lex->token != ',')
break;
lex_scan(lex, error);
}
if(lex->token != ']') {
if (lex->token != ']') {
error_set(error, lex, json_error_invalid_syntax, "']' expected");
goto error;
}
@ -809,18 +802,18 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
json_t *json;
lex->depth++;
if(lex->depth > JSON_PARSER_MAX_DEPTH) {
if (lex->depth > JSON_PARSER_MAX_DEPTH) {
error_set(error, lex, json_error_stack_overflow, "maximum parsing depth reached");
return NULL;
}
switch(lex->token) {
switch (lex->token) {
case TOKEN_STRING: {
const char *value = lex->value.string.val;
size_t len = lex->value.string.len;
if(!(flags & JSON_ALLOW_NUL)) {
if(memchr(value, '\0', len)) {
if (!(flags & JSON_ALLOW_NUL)) {
if (memchr(value, '\0', len)) {
error_set(error, lex, json_error_null_character, "\\u0000 is not allowed without JSON_ALLOW_NUL");
return NULL;
}
@ -871,7 +864,7 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
return NULL;
}
if(!json)
if (!json)
return NULL;
lex->depth--;
@ -885,27 +878,27 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
lex->depth = 0;
lex_scan(lex, error);
if(!(flags & JSON_DECODE_ANY)) {
if(lex->token != '[' && lex->token != '{') {
if (!(flags & JSON_DECODE_ANY)) {
if (lex->token != '[' && lex->token != '{') {
error_set(error, lex, json_error_invalid_syntax, "'[' or '{' expected");
return NULL;
}
}
result = parse_value(lex, flags, error);
if(!result)
if (!result)
return NULL;
if(!(flags & JSON_DISABLE_EOF_CHECK)) {
if (!(flags & JSON_DISABLE_EOF_CHECK)) {
lex_scan(lex, error);
if(lex->token != TOKEN_EOF) {
if (lex->token != TOKEN_EOF) {
error_set(error, lex, json_error_end_of_input_expected, "end of file expected");
json_decref(result);
return NULL;
}
}
if(error) {
if (error) {
/* Save the position even though there was no error */
error->position = (int)lex->stream.position;
}
@ -913,8 +906,7 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
return result;
}
typedef struct
{
typedef struct {
const char *data;
size_t pos;
} string_data_t;
@ -924,10 +916,9 @@ static int string_get(void *data)
char c;
string_data_t *stream = (string_data_t *)data;
c = stream->data[stream->pos];
if(c == '\0')
if (c == '\0')
return EOF;
else
{
else {
stream->pos++;
return (unsigned char)c;
}
@ -949,7 +940,7 @@ json_t *json_loads(const char *string, size_t flags, json_error_t *error)
stream_data.data = string;
stream_data.pos = 0;
if(lex_init(&lex, string_get, flags, (void *)&stream_data))
if (lex_init(&lex, string_get, flags, (void *)&stream_data))
return NULL;
result = parse_json(&lex, flags, error);
@ -958,8 +949,7 @@ json_t *json_loads(const char *string, size_t flags, json_error_t *error)
return result;
}
typedef struct
{
typedef struct {
const char *data;
size_t len;
size_t pos;
@ -969,8 +959,8 @@ static int buffer_get(void *data)
{
char c;
buffer_data_t *stream = data;
if(stream->pos >= stream->len)
return EOF;
if (stream->pos >= stream->len)
return EOF;
c = stream->data[stream->pos];
stream->pos++;
@ -994,7 +984,7 @@ json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t
stream_data.pos = 0;
stream_data.len = buflen;
if(lex_init(&lex, buffer_get, flags, (void *)&stream_data))
if (lex_init(&lex, buffer_get, flags, (void *)&stream_data))
return NULL;
result = parse_json(&lex, flags, error);
@ -1009,7 +999,7 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
const char *source;
json_t *result;
if(input == stdin)
if (input == stdin)
source = "<stdin>";
else
source = "<stream>";
@ -1021,7 +1011,7 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
return NULL;
}
if(lex_init(&lex, (get_func)fgetc, flags, input))
if (lex_init(&lex, (get_func)fgetc, flags, input))
return NULL;
result = parse_json(&lex, flags, error);
@ -1047,7 +1037,7 @@ json_t *json_loadfd(int input, size_t flags, json_error_t *error)
json_t *result;
#ifdef HAVE_UNISTD_H
if(input == STDIN_FILENO)
if (input == STDIN_FILENO)
source = "<stdin>";
else
#endif
@ -1060,7 +1050,7 @@ json_t *json_loadfd(int input, size_t flags, json_error_t *error)
return NULL;
}
if(lex_init(&lex, (get_func)fd_get_func, flags, &input))
if (lex_init(&lex, (get_func)fd_get_func, flags, &input))
return NULL;
result = parse_json(&lex, flags, error);
@ -1082,8 +1072,7 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
}
fp = fopen(path, "rb");
if(!fp)
{
if (!fp) {
error_set(error, NULL, json_error_cannot_open_file, "unable to open %s: %s",
path, strerror(errno));
return NULL;
@ -1097,8 +1086,7 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
#define MAX_BUF_LEN 1024
typedef struct
{
typedef struct {
char data[MAX_BUF_LEN];
size_t len;
size_t pos;
@ -1111,10 +1099,10 @@ static int callback_get(void *data)
char c;
callback_data_t *stream = data;
if(stream->pos >= stream->len) {
if (stream->pos >= stream->len) {
stream->pos = 0;
stream->len = stream->callback(stream->data, MAX_BUF_LEN, stream->arg);
if(stream->len == 0 || stream->len == (size_t)-1)
if (stream->len == 0 || stream->len == (size_t) -1)
return EOF;
}
@ -1141,7 +1129,7 @@ json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flag
return NULL;
}
if(lex_init(&lex, (get_func)callback_get, flags, &stream_data))
if (lex_init(&lex, (get_func)callback_get, flags, &stream_data))
return NULL;
result = parse_json(&lex, flags, error);

View file

@ -121,14 +121,14 @@ rotates.
-------------------------------------------------------------------------------
*/
#define mix(a,b,c) \
{ \
a -= c; a ^= rot(c, 4); c += b; \
b -= a; b ^= rot(a, 6); a += c; \
c -= b; c ^= rot(b, 8); b += a; \
a -= c; a ^= rot(c,16); c += b; \
b -= a; b ^= rot(a,19); a += c; \
c -= b; c ^= rot(b, 4); b += a; \
}
{ \
a -= c; a ^= rot(c, 4); c += b; \
b -= a; b ^= rot(a, 6); a += c; \
c -= b; c ^= rot(b, 8); b += a; \
a -= c; a ^= rot(c,16); c += b; \
b -= a; b ^= rot(a,19); a += c; \
c -= b; c ^= rot(b, 4); b += a; \
}
/*
-------------------------------------------------------------------------------
@ -156,15 +156,15 @@ and these came close:
-------------------------------------------------------------------------------
*/
#define final(a,b,c) \
{ \
c ^= b; c -= rot(b,14); \
a ^= c; a -= rot(c,11); \
b ^= a; b -= rot(a,25); \
c ^= b; c -= rot(b,16); \
a ^= c; a -= rot(c,4); \
b ^= a; b -= rot(a,14); \
c ^= b; c -= rot(b,24); \
}
{ \
c ^= b; c -= rot(b,14); \
a ^= c; a -= rot(c,11); \
b ^= a; b -= rot(a,25); \
c ^= b; c -= rot(b,16); \
a ^= c; a -= rot(c,4); \
b ^= a; b -= rot(a,14); \
c ^= b; c -= rot(b,24); \
}
/*
-------------------------------------------------------------------------------
@ -195,17 +195,17 @@ acceptable. Do NOT use for cryptographic purposes.
static uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
{
uint32_t a,b,c; /* internal state */
union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
uint32_t a, b, c; /* internal state */
union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
/* Set up the internal state */
a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
/* Set up the internal state */
a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
u.ptr = key;
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
u.ptr = key;
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
/* Detect Valgrind or AddressSanitizer */
/* Detect Valgrind or AddressSanitizer */
#ifdef VALGRIND
# define NO_MASKING_TRICK 1
#else
@ -221,161 +221,237 @@ static uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
#endif
#ifdef NO_MASKING_TRICK
const uint8_t *k8;
const uint8_t *k8;
#endif
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
while (length > 12)
{
a += k[0];
b += k[1];
c += k[2];
mix(a,b,c);
length -= 12;
k += 3;
}
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
while (length > 12) {
a += k[0];
b += k[1];
c += k[2];
mix(a, b, c);
length -= 12;
k += 3;
}
/*----------------------------- handle the last (probably partial) block */
/*
* "k[2]&0xffffff" actually reads beyond the end of the string, but
* then masks off the part it's not allowed to read. Because the
* string is aligned, the masked-off tail is in the same word as the
* rest of the string. Every machine with memory protection I've seen
* does it on word boundaries, so is OK with this. But VALGRIND will
* still catch it and complain. The masking trick does make the hash
* noticably faster for short strings (like English words).
*/
/*----------------------------- handle the last (probably partial) block */
/*
* "k[2]&0xffffff" actually reads beyond the end of the string, but
* then masks off the part it's not allowed to read. Because the
* string is aligned, the masked-off tail is in the same word as the
* rest of the string. Every machine with memory protection I've seen
* does it on word boundaries, so is OK with this. But VALGRIND will
* still catch it and complain. The masking trick does make the hash
* noticably faster for short strings (like English words).
*/
#ifndef NO_MASKING_TRICK
switch(length)
{
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
case 8 : b+=k[1]; a+=k[0]; break;
case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
case 6 : b+=k[1]&0xffff; a+=k[0]; break;
case 5 : b+=k[1]&0xff; a+=k[0]; break;
case 4 : a+=k[0]; break;
case 3 : a+=k[0]&0xffffff; break;
case 2 : a+=k[0]&0xffff; break;
case 1 : a+=k[0]&0xff; break;
case 0 : return c; /* zero length strings require no mixing */
}
switch (length) {
case 12:
c += k[2];
b += k[1];
a += k[0];
break;
case 11:
c += k[2] & 0xffffff;
b += k[1];
a += k[0];
break;
case 10:
c += k[2] & 0xffff;
b += k[1];
a += k[0];
break;
case 9 :
c += k[2] & 0xff;
b += k[1];
a += k[0];
break;
case 8 :
b += k[1];
a += k[0];
break;
case 7 :
b += k[1] & 0xffffff;
a += k[0];
break;
case 6 :
b += k[1] & 0xffff;
a += k[0];
break;
case 5 :
b += k[1] & 0xff;
a += k[0];
break;
case 4 :
a += k[0];
break;
case 3 :
a += k[0] & 0xffffff;
break;
case 2 :
a += k[0] & 0xffff;
break;
case 1 :
a += k[0] & 0xff;
break;
case 0 :
return c; /* zero length strings require no mixing */
}
#else /* make valgrind happy */
k8 = (const uint8_t *)k;
switch(length)
{
case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
case 9 : c+=k8[8]; /* fall through */
case 8 : b+=k[1]; a+=k[0]; break;
case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
case 5 : b+=k8[4]; /* fall through */
case 4 : a+=k[0]; break;
case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
case 1 : a+=k8[0]; break;
case 0 : return c;
}
k8 = (const uint8_t *)k;
switch (length) {
case 12:
c += k[2];
b += k[1];
a += k[0];
break;
case 11:
c += ((uint32_t)k8[10]) << 16; /* fall through */
case 10:
c += ((uint32_t)k8[9]) << 8; /* fall through */
case 9 :
c += k8[8]; /* fall through */
case 8 :
b += k[1];
a += k[0];
break;
case 7 :
b += ((uint32_t)k8[6]) << 16; /* fall through */
case 6 :
b += ((uint32_t)k8[5]) << 8; /* fall through */
case 5 :
b += k8[4]; /* fall through */
case 4 :
a += k[0];
break;
case 3 :
a += ((uint32_t)k8[2]) << 16; /* fall through */
case 2 :
a += ((uint32_t)k8[1]) << 8; /* fall through */
case 1 :
a += k8[0];
break;
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;
} 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;
/*--------------- all but last block: aligned reads and different mixing */
while (length > 12)
{
a += k[0] + (((uint32_t)k[1])<<16);
b += k[2] + (((uint32_t)k[3])<<16);
c += k[4] + (((uint32_t)k[5])<<16);
mix(a,b,c);
length -= 12;
k += 6;
/*--------------- all but last block: aligned reads and different mixing */
while (length > 12) {
a += k[0] + (((uint32_t)k[1]) << 16);
b += k[2] + (((uint32_t)k[3]) << 16);
c += k[4] + (((uint32_t)k[5]) << 16);
mix(a, b, c);
length -= 12;
k += 6;
}
/*----------------------------- handle the last (probably partial) block */
k8 = (const uint8_t *)k;
switch (length) {
case 12:
c += k[4] + (((uint32_t)k[5]) << 16);
b += k[2] + (((uint32_t)k[3]) << 16);
a += k[0] + (((uint32_t)k[1]) << 16);
break;
case 11:
c += ((uint32_t)k8[10]) << 16; /* fall through */
case 10:
c += k[4];
b += k[2] + (((uint32_t)k[3]) << 16);
a += k[0] + (((uint32_t)k[1]) << 16);
break;
case 9 :
c += k8[8]; /* fall through */
case 8 :
b += k[2] + (((uint32_t)k[3]) << 16);
a += k[0] + (((uint32_t)k[1]) << 16);
break;
case 7 :
b += ((uint32_t)k8[6]) << 16; /* fall through */
case 6 :
b += k[2];
a += k[0] + (((uint32_t)k[1]) << 16);
break;
case 5 :
b += k8[4]; /* fall through */
case 4 :
a += k[0] + (((uint32_t)k[1]) << 16);
break;
case 3 :
a += ((uint32_t)k8[2]) << 16; /* fall through */
case 2 :
a += k[0];
break;
case 1 :
a += k8[0];
break;
case 0 :
return c; /* zero length requires no mixing */
}
} else { /* need to read the key one byte at a time */
const uint8_t *k = (const uint8_t *)key;
/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
while (length > 12) {
a += k[0];
a += ((uint32_t)k[1]) << 8;
a += ((uint32_t)k[2]) << 16;
a += ((uint32_t)k[3]) << 24;
b += k[4];
b += ((uint32_t)k[5]) << 8;
b += ((uint32_t)k[6]) << 16;
b += ((uint32_t)k[7]) << 24;
c += k[8];
c += ((uint32_t)k[9]) << 8;
c += ((uint32_t)k[10]) << 16;
c += ((uint32_t)k[11]) << 24;
mix(a, b, c);
length -= 12;
k += 12;
}
/*-------------------------------- last block: affect all 32 bits of (c) */
switch (length) { /* all the case statements fall through */
case 12:
c += ((uint32_t)k[11]) << 24; /* fall through */
case 11:
c += ((uint32_t)k[10]) << 16; /* fall through */
case 10:
c += ((uint32_t)k[9]) << 8; /* fall through */
case 9 :
c += k[8]; /* fall through */
case 8 :
b += ((uint32_t)k[7]) << 24; /* fall through */
case 7 :
b += ((uint32_t)k[6]) << 16; /* fall through */
case 6 :
b += ((uint32_t)k[5]) << 8; /* fall through */
case 5 :
b += k[4]; /* fall through */
case 4 :
a += ((uint32_t)k[3]) << 24; /* fall through */
case 3 :
a += ((uint32_t)k[2]) << 16; /* fall through */
case 2 :
a += ((uint32_t)k[1]) << 8; /* fall through */
case 1 :
a += k[0];
break;
case 0 :
return c;
}
}
/*----------------------------- handle the last (probably partial) block */
k8 = (const uint8_t *)k;
switch(length)
{
case 12: c+=k[4]+(((uint32_t)k[5])<<16);
b+=k[2]+(((uint32_t)k[3])<<16);
a+=k[0]+(((uint32_t)k[1])<<16);
break;
case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
case 10: c+=k[4];
b+=k[2]+(((uint32_t)k[3])<<16);
a+=k[0]+(((uint32_t)k[1])<<16);
break;
case 9 : c+=k8[8]; /* fall through */
case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
a+=k[0]+(((uint32_t)k[1])<<16);
break;
case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
case 6 : b+=k[2];
a+=k[0]+(((uint32_t)k[1])<<16);
break;
case 5 : b+=k8[4]; /* fall through */
case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
break;
case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
case 2 : a+=k[0];
break;
case 1 : a+=k8[0];
break;
case 0 : return c; /* zero length requires no mixing */
}
} else { /* need to read the key one byte at a time */
const uint8_t *k = (const uint8_t *)key;
/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
while (length > 12)
{
a += k[0];
a += ((uint32_t)k[1])<<8;
a += ((uint32_t)k[2])<<16;
a += ((uint32_t)k[3])<<24;
b += k[4];
b += ((uint32_t)k[5])<<8;
b += ((uint32_t)k[6])<<16;
b += ((uint32_t)k[7])<<24;
c += k[8];
c += ((uint32_t)k[9])<<8;
c += ((uint32_t)k[10])<<16;
c += ((uint32_t)k[11])<<24;
mix(a,b,c);
length -= 12;
k += 12;
}
/*-------------------------------- last block: affect all 32 bits of (c) */
switch(length) /* all the case statements fall through */
{
case 12: c+=((uint32_t)k[11])<<24; /* fall through */
case 11: c+=((uint32_t)k[10])<<16; /* fall through */
case 10: c+=((uint32_t)k[9])<<8; /* fall through */
case 9 : c+=k[8]; /* fall through */
case 8 : b+=((uint32_t)k[7])<<24; /* fall through */
case 7 : b+=((uint32_t)k[6])<<16; /* fall through */
case 6 : b+=((uint32_t)k[5])<<8; /* fall through */
case 5 : b+=k[4]; /* fall through */
case 4 : a+=((uint32_t)k[3])<<24; /* fall through */
case 3 : a+=((uint32_t)k[2])<<16; /* fall through */
case 2 : a+=((uint32_t)k[1])<<8; /* fall through */
case 1 : a+=k[0];
break;
case 0 : return c;
}
}
final(a,b,c);
return c;
final(a, b, c);
return c;
}

View file

@ -22,7 +22,7 @@ static json_free_t do_free = free;
void *jsonp_malloc(size_t size)
{
if(!size)
if (!size)
return NULL;
return (*do_malloc)(size);
@ -30,7 +30,7 @@ void *jsonp_malloc(size_t size)
void jsonp_free(void *ptr)
{
if(!ptr)
if (!ptr)
return;
(*do_free)(ptr);
@ -46,7 +46,7 @@ char *jsonp_strndup(const char *str, size_t len)
char *new_str;
new_str = jsonp_malloc(len + 1);
if(!new_str)
if (!new_str)
return NULL;
memcpy(new_str, str, len);

View file

@ -34,7 +34,7 @@ typedef struct {
#define token(scanner) ((scanner)->token.token)
static const char * const type_names[] = {
static const char *const type_names[] = {
"object",
"array",
"string",
@ -69,7 +69,7 @@ static void next_token(scanner_t *s)
const char *t;
s->prev_token = s->token;
if(s->next_token.line) {
if (s->next_token.line) {
s->token = s->next_token;
s->next_token.line = 0;
return;
@ -83,12 +83,11 @@ static void next_token(scanner_t *s)
s->pos++;
/* skip space and ignored chars */
while(*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
if(*t == '\n') {
while (*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
if (*t == '\n') {
s->line++;
s->column = 1;
}
else
} else
s->column++;
s->pos++;
@ -142,11 +141,11 @@ static char *read_string(scanner_t *s, va_list *ap,
prev_token(s);
*ours = 0;
if(t != '#' && t != '%' && t != '+') {
if (t != '#' && t != '%' && t != '+') {
/* Optimize the simple case */
str = va_arg(*ap, const char *);
if(!str) {
if (!str) {
if (!optional) {
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
s->has_error = 1;
@ -156,7 +155,7 @@ static char *read_string(scanner_t *s, va_list *ap,
length = strlen(str);
if(!utf8_check_string(str, length)) {
if (!utf8_check_string(str, length)) {
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
s->has_error = 1;
return NULL;
@ -171,49 +170,47 @@ static char *read_string(scanner_t *s, va_list *ap,
return NULL;
}
if(strbuffer_init(&strbuff)) {
if (strbuffer_init(&strbuff)) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
s->has_error = 1;
}
while(1) {
while (1) {
str = va_arg(*ap, const char *);
if(!str) {
if (!str) {
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
s->has_error = 1;
}
next_token(s);
if(token(s) == '#') {
if (token(s) == '#') {
length = va_arg(*ap, int);
}
else if(token(s) == '%') {
} else if (token(s) == '%') {
length = va_arg(*ap, size_t);
}
else {
} else {
prev_token(s);
length = s->has_error ? 0 : strlen(str);
}
if(!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) {
if (!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
s->has_error = 1;
}
next_token(s);
if(token(s) != '+') {
if (token(s) != '+') {
prev_token(s);
break;
}
}
if(s->has_error) {
if (s->has_error) {
strbuffer_close(&strbuff);
return NULL;
}
if(!utf8_check_string(strbuff.value, strbuff.length)) {
if (!utf8_check_string(strbuff.value, strbuff.length)) {
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
strbuffer_close(&strbuff);
s->has_error = 1;
@ -230,19 +227,19 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
json_t *object = json_object();
next_token(s);
while(token(s) != '}') {
while (token(s) != '}') {
char *key;
size_t len;
int ours;
json_t *value;
char valueOptional;
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
goto error;
}
if(token(s) != 's') {
if (token(s) != 's') {
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
goto error;
}
@ -256,11 +253,11 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
prev_token(s);
value = pack(s, ap);
if(!value) {
if(ours)
if (!value) {
if (ours)
jsonp_free(key);
if(valueOptional != '*') {
if (valueOptional != '*') {
set_error(s, "<args>", json_error_null_value, "NULL object value");
s->has_error = 1;
}
@ -269,21 +266,21 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
continue;
}
if(s->has_error)
if (s->has_error)
json_decref(value);
if(!s->has_error && json_object_set_new_nocheck(object, key, value)) {
if (!s->has_error && json_object_set_new_nocheck(object, key, value)) {
set_error(s, "<internal>", json_error_out_of_memory, "Unable to add key \"%s\"", key);
s->has_error = 1;
}
if(ours)
if (ours)
jsonp_free(key);
next_token(s);
}
if(!s->has_error)
if (!s->has_error)
return object;
error:
@ -296,11 +293,11 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
json_t *array = json_array();
next_token(s);
while(token(s) != ']') {
while (token(s) != ']') {
json_t *value;
char valueOptional;
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
/* Format string errors are unrecoverable. */
goto error;
@ -311,8 +308,8 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
prev_token(s);
value = pack(s, ap);
if(!value) {
if(valueOptional != '*') {
if (!value) {
if (valueOptional != '*') {
s->has_error = 1;
}
@ -320,10 +317,10 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
continue;
}
if(s->has_error)
if (s->has_error)
json_decref(value);
if(!s->has_error && json_array_append_new(array, value)) {
if (!s->has_error && json_array_append_new(array, value)) {
set_error(s, "<internal>", json_error_out_of_memory, "Unable to append to array");
s->has_error = 1;
}
@ -331,7 +328,7 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
next_token(s);
}
if(!s->has_error)
if (!s->has_error)
return array;
error:
@ -437,7 +434,7 @@ static json_t *pack_real(scanner_t *s, double value)
static json_t *pack(scanner_t *s, va_list *ap)
{
switch(token(s)) {
switch (token(s)) {
case '{':
return pack_object(s, ap);
@ -491,81 +488,80 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
*/
hashtable_t key_set;
if(hashtable_init(&key_set)) {
if (hashtable_init(&key_set)) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
return -1;
}
if(root && !json_is_object(root)) {
if (root && !json_is_object(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected object, got %s",
type_name(root));
goto out;
}
next_token(s);
while(token(s) != '}') {
while (token(s) != '}') {
const char *key;
json_t *value;
int opt = 0;
if(strict != 0) {
if (strict != 0) {
set_error(s, "<format>", json_error_invalid_format, "Expected '}' after '%c', got '%c'",
(strict == 1 ? '!' : '*'), token(s));
goto out;
}
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
goto out;
}
if(token(s) == '!' || token(s) == '*') {
if (token(s) == '!' || token(s) == '*') {
strict = (token(s) == '!' ? 1 : -1);
next_token(s);
continue;
}
if(token(s) != 's') {
if (token(s) != 's') {
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
goto out;
}
key = va_arg(*ap, const char *);
if(!key) {
if (!key) {
set_error(s, "<args>", json_error_null_value, "NULL object key");
goto out;
}
next_token(s);
if(token(s) == '?') {
if (token(s) == '?') {
opt = gotopt = 1;
next_token(s);
}
if(!root) {
if (!root) {
/* skipping */
value = NULL;
}
else {
} else {
value = json_object_get(root, key);
if(!value && !opt) {
if (!value && !opt) {
set_error(s, "<validation>", json_error_item_not_found, "Object item not found: %s", key);
goto out;
}
}
if(unpack(s, value, ap))
if (unpack(s, value, ap))
goto out;
hashtable_set(&key_set, key, json_null());
next_token(s);
}
if(strict == 0 && (s->flags & JSON_STRICT))
if (strict == 0 && (s->flags & JSON_STRICT))
strict = 1;
if(root && strict == 1) {
if (root && strict == 1) {
/* We need to check that all non optional items have been parsed */
const char *key;
/* keys_res is 1 for uninitialized, 0 for success, -1 for error. */
@ -576,7 +572,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
if (gotopt || json_object_size(root) != key_set.size) {
json_object_foreach(root, key, value) {
if(!hashtable_get(&key_set, key)) {
if (!hashtable_get(&key_set, key)) {
unpacked++;
/* Save unrecognized keys for the error message */
@ -613,63 +609,62 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
size_t i = 0;
int strict = 0;
if(root && !json_is_array(root)) {
if (root && !json_is_array(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected array, got %s", type_name(root));
return -1;
}
next_token(s);
while(token(s) != ']') {
while (token(s) != ']') {
json_t *value;
if(strict != 0) {
if (strict != 0) {
set_error(s, "<format>", json_error_invalid_format, "Expected ']' after '%c', got '%c'",
(strict == 1 ? '!' : '*'),
token(s));
return -1;
}
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
return -1;
}
if(token(s) == '!' || token(s) == '*') {
if (token(s) == '!' || token(s) == '*') {
strict = (token(s) == '!' ? 1 : -1);
next_token(s);
continue;
}
if(!strchr(unpack_value_starters, token(s))) {
if (!strchr(unpack_value_starters, token(s))) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
token(s));
return -1;
}
if(!root) {
if (!root) {
/* skipping */
value = NULL;
}
else {
} else {
value = json_array_get(root, i);
if(!value) {
if (!value) {
set_error(s, "<validation>", json_error_index_out_of_range, "Array index %lu out of range",
(unsigned long)i);
return -1;
}
}
if(unpack(s, value, ap))
if (unpack(s, value, ap))
return -1;
next_token(s);
i++;
}
if(strict == 0 && (s->flags & JSON_STRICT))
if (strict == 0 && (s->flags & JSON_STRICT))
strict = 1;
if(root && strict == 1 && i != json_array_size(root)) {
if (root && strict == 1 && i != json_array_size(root)) {
long diff = (long)json_array_size(root) - (long)i;
set_error(s, "<validation>", json_error_end_of_input_expected, "%li array item(s) left unpacked", diff);
return -1;
@ -680,8 +675,7 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
static int unpack(scanner_t *s, json_t *root, va_list *ap)
{
switch(token(s))
{
switch (token(s)) {
case '{':
return unpack_object(s, root, ap);
@ -689,126 +683,125 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
return unpack_array(s, root, ap);
case 's':
if(root && !json_is_string(root)) {
if (root && !json_is_string(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected string, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
if (!(s->flags & JSON_VALIDATE_ONLY)) {
const char **str_target;
size_t *len_target = NULL;
str_target = va_arg(*ap, const char **);
if(!str_target) {
if (!str_target) {
set_error(s, "<args>", json_error_null_value, "NULL string argument");
return -1;
}
next_token(s);
if(token(s) == '%') {
if (token(s) == '%') {
len_target = va_arg(*ap, size_t *);
if(!len_target) {
if (!len_target) {
set_error(s, "<args>", json_error_null_value, "NULL string length argument");
return -1;
}
}
else
} else
prev_token(s);
if(root) {
if (root) {
*str_target = json_string_value(root);
if(len_target)
if (len_target)
*len_target = json_string_length(root);
}
}
return 0;
case 'i':
if(root && !json_is_integer(root)) {
if (root && !json_is_integer(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int *);
if (root)
*target = (int)json_integer_value(root);
}
return 0;
case 'I':
if(root && !json_is_integer(root)) {
if (root && !json_is_integer(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
json_int_t *target = va_arg(*ap, json_int_t*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
json_int_t *target = va_arg(*ap, json_int_t *);
if (root)
*target = json_integer_value(root);
}
return 0;
case 'b':
if(root && !json_is_boolean(root)) {
if (root && !json_is_boolean(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected true or false, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int *);
if (root)
*target = json_is_true(root);
}
return 0;
case 'f':
if(root && !json_is_real(root)) {
if (root && !json_is_real(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected real, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double *);
if (root)
*target = json_real_value(root);
}
return 0;
case 'F':
if(root && !json_is_number(root)) {
if (root && !json_is_number(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected real or integer, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double *);
if (root)
*target = json_number_value(root);
}
return 0;
case 'O':
if(root && !(s->flags & JSON_VALIDATE_ONLY))
if (root && !(s->flags & JSON_VALIDATE_ONLY))
json_incref(root);
/* Fall through */
/* Fall through */
case 'o':
if(!(s->flags & JSON_VALIDATE_ONLY)) {
json_t **target = va_arg(*ap, json_t**);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
json_t **target = va_arg(*ap, json_t **);
if (root)
*target = root;
}
@ -816,7 +809,7 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'n':
/* Never assign, just validate */
if(root && !json_is_null(root)) {
if (root && !json_is_null(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected null, got %s",
type_name(root));
return -1;
@ -837,7 +830,7 @@ json_t *json_vpack_ex(json_error_t *error, size_t flags,
va_list ap_copy;
json_t *value;
if(!fmt || !*fmt) {
if (!fmt || !*fmt) {
jsonp_error_init(error, "<format>");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
return NULL;
@ -852,11 +845,11 @@ json_t *json_vpack_ex(json_error_t *error, size_t flags,
va_end(ap_copy);
/* This will cover all situations where s.has_error is true */
if(!value)
if (!value)
return NULL;
next_token(&s);
if(token(&s)) {
if (token(&s)) {
json_decref(value);
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
return NULL;
@ -895,13 +888,13 @@ int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
scanner_t s;
va_list ap_copy;
if(!root) {
if (!root) {
jsonp_error_init(error, "<root>");
jsonp_error_set(error, -1, -1, 0, json_error_null_value, "NULL root value");
return -1;
}
if(!fmt || !*fmt) {
if (!fmt || !*fmt) {
jsonp_error_init(error, "<format>");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
return -1;
@ -912,14 +905,14 @@ int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
next_token(&s);
va_copy(ap_copy, ap);
if(unpack(&s, root, &ap_copy)) {
if (unpack(&s, root, &ap_copy)) {
va_end(ap_copy);
return -1;
}
va_end(ap_copy);
next_token(&s);
if(token(&s)) {
if (token(&s)) {
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
return -1;
}

View file

@ -31,8 +31,7 @@ json_t *json_path_get(const json_t *json, const char *path)
token = NULL;
expect = path_delims;
while (peek && *peek && cursor)
{
while (peek && *peek && cursor) {
char *last_peek = peek;
peek = strpbrk(peek, expect);
if (peek) {
@ -72,7 +71,7 @@ fail:
int json_path_set_new(json_t *json, const char *path, json_t *value, size_t flags, json_error_t *error)
{
static const char root_chr = '$', array_open = '[', object_delim = '.';
static const char * const path_delims = ".[", *array_close = "]";
static const char *const path_delims = ".[", *array_close = "]";
json_t *cursor, *parent = NULL;
char *token, *buf = NULL, *peek, delim = '\0';
@ -98,8 +97,7 @@ int json_path_set_new(json_t *json, const char *path, json_t *value, size_t flag
token = NULL;
expect = path_delims;
while (peek && *peek && cursor)
{
while (peek && *peek && cursor) {
char *last_peek = peek;
peek = strpbrk(last_peek, expect);

View file

@ -24,7 +24,7 @@ int strbuffer_init(strbuffer_t *strbuff)
strbuff->length = 0;
strbuff->value = jsonp_malloc(strbuff->size);
if(!strbuff->value)
if (!strbuff->value)
return -1;
/* initialize to empty */
@ -34,7 +34,7 @@ int strbuffer_init(strbuffer_t *strbuff)
void strbuffer_close(strbuffer_t *strbuff)
{
if(strbuff->value)
if (strbuff->value)
jsonp_free(strbuff->value);
strbuff->size = 0;
@ -67,8 +67,7 @@ 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)
{
if (size >= strbuff->size - strbuff->length) {
size_t new_size;
char *new_value;
@ -82,7 +81,7 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
strbuff->length + size + 1);
new_value = jsonp_malloc(new_size);
if(!new_value)
if (!new_value)
return -1;
memcpy(new_value, strbuff->value, strbuff->length);
@ -101,11 +100,10 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
char strbuffer_pop(strbuffer_t *strbuff)
{
if(strbuff->length > 0) {
if (strbuff->length > 0) {
char c = strbuff->value[--strbuff->length];
strbuff->value[strbuff->length] = '\0';
return c;
}
else
} else
return '\0';
}

View file

@ -37,13 +37,13 @@ static void to_locale(strbuffer_t *strbuffer)
char *pos;
point = localeconv()->decimal_point;
if(*point == '.') {
if (*point == '.') {
/* No conversion needed */
return;
}
pos = strchr(strbuffer->value, '.');
if(pos)
if (pos)
*pos = *point;
}
@ -53,13 +53,13 @@ static void from_locale(char *buffer)
char *pos;
point = localeconv()->decimal_point;
if(*point == '.') {
if (*point == '.') {
/* No conversion needed */
return;
}
pos = strchr(buffer, *point);
if(pos)
if (pos)
*pos = '.';
}
#endif
@ -77,7 +77,7 @@ int jsonp_strtod(strbuffer_t *strbuffer, double *out)
value = strtod(strbuffer->value, &end);
assert(end == strbuffer->value + strbuffer->length);
if((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) {
if ((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) {
/* Overflow */
return -1;
}
@ -96,11 +96,11 @@ int jsonp_dtostr(char *buffer, size_t size, double value, int precision)
precision = 17;
ret = snprintf(buffer, size, "%.*g", precision, value);
if(ret < 0)
if (ret < 0)
return -1;
length = (size_t)ret;
if(length >= size)
if (length >= size)
return -1;
#if JSON_HAVE_LOCALECONV
@ -109,10 +109,9 @@ int jsonp_dtostr(char *buffer, size_t size, double value, int precision)
/* Make sure there's a dot or 'e' in the output. Otherwise
a real is converted to an integer when decoding */
if(strchr(buffer, '.') == NULL &&
strchr(buffer, 'e') == NULL)
{
if(length + 3 >= size) {
if (strchr(buffer, '.') == NULL &&
strchr(buffer, 'e') == NULL) {
if (length + 3 >= size) {
/* No space to append ".0" */
return -1;
}
@ -125,17 +124,17 @@ int jsonp_dtostr(char *buffer, size_t size, double value, int precision)
/* Remove leading '+' from positive exponent. Also remove leading
zeros from exponents (added by some printf() implementations) */
start = strchr(buffer, 'e');
if(start) {
if (start) {
start++;
end = start + 1;
if(*start == '-')
if (*start == '-')
start++;
while(*end == '0')
while (*end == '0')
end++;
if(end != start) {
if (end != start) {
memmove(start, end, length - (size_t)(end - buffer));
length -= (size_t)(end - start);
}

View file

@ -10,35 +10,27 @@
int utf8_encode(int32_t codepoint, char *buffer, size_t *size)
{
if(codepoint < 0)
if (codepoint < 0)
return -1;
else if(codepoint < 0x80)
{
else if (codepoint < 0x80) {
buffer[0] = (char)codepoint;
*size = 1;
}
else if(codepoint < 0x800)
{
} else if (codepoint < 0x800) {
buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);
buffer[1] = 0x80 + ((codepoint & 0x03F));
*size = 2;
}
else if(codepoint < 0x10000)
{
} else if (codepoint < 0x10000) {
buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);
buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);
buffer[2] = 0x80 + ((codepoint & 0x003F));
*size = 3;
}
else if(codepoint <= 0x10FFFF)
{
} else if (codepoint <= 0x10FFFF) {
buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);
buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);
buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);
buffer[3] = 0x80 + ((codepoint & 0x00003F));
*size = 4;
}
else
} else
return -1;
return 0;
@ -48,32 +40,28 @@ size_t utf8_check_first(char byte)
{
unsigned char u = (unsigned char)byte;
if(u < 0x80)
if (u < 0x80)
return 1;
if(0x80 <= u && u <= 0xBF) {
if (0x80 <= u && u <= 0xBF) {
/* second, third or fourth byte of a multi-byte
sequence, i.e. a "continuation byte" */
return 0;
}
else if(u == 0xC0 || u == 0xC1) {
} else if (u == 0xC0 || u == 0xC1) {
/* overlong encoding of an ASCII byte */
return 0;
}
else if(0xC2 <= u && u <= 0xDF) {
} else if (0xC2 <= u && u <= 0xDF) {
/* 2-byte sequence */
return 2;
}
else if(0xE0 <= u && u <= 0xEF) {
else if (0xE0 <= u && u <= 0xEF) {
/* 3-byte sequence */
return 3;
}
else if(0xF0 <= u && u <= 0xF4) {
} else if (0xF0 <= u && u <= 0xF4) {
/* 4-byte sequence */
return 4;
}
else { /* u >= 0xF5 */
} else { /* u >= 0xF5 */
/* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
UTF-8 */
return 0;
@ -86,26 +74,19 @@ size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint)
int32_t value = 0;
unsigned char u = (unsigned char)buffer[0];
if(size == 2)
{
if (size == 2) {
value = u & 0x1F;
}
else if(size == 3)
{
} else if (size == 3) {
value = u & 0xF;
}
else if(size == 4)
{
} else if (size == 4) {
value = u & 0x7;
}
else
} else
return 0;
for(i = 1; i < size; i++)
{
for (i = 1; i < size; i++) {
u = (unsigned char)buffer[i];
if(u < 0x80 || u > 0xBF) {
if (u < 0x80 || u > 0xBF) {
/* not a continuation byte */
return 0;
}
@ -113,24 +94,24 @@ size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint)
value = (value << 6) + (u & 0x3F);
}
if(value > 0x10FFFF) {
if (value > 0x10FFFF) {
/* not in Unicode range */
return 0;
}
else if(0xD800 <= value && value <= 0xDFFF) {
else if (0xD800 <= value && value <= 0xDFFF) {
/* invalid code point (UTF-16 surrogate halves) */
return 0;
}
else if((size == 2 && value < 0x80) ||
(size == 3 && value < 0x800) ||
(size == 4 && value < 0x10000)) {
else if ((size == 2 && value < 0x80) ||
(size == 3 && value < 0x800) ||
(size == 4 && value < 0x10000)) {
/* overlong encoding */
return 0;
}
if(codepoint)
if (codepoint)
*codepoint = value;
return 1;
@ -141,22 +122,21 @@ const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint)
size_t count;
int32_t value;
if(!bufsize)
if (!bufsize)
return buffer;
count = utf8_check_first(buffer[0]);
if(count <= 0)
if (count <= 0)
return NULL;
if(count == 1)
if (count == 1)
value = (unsigned char)buffer[0];
else
{
if(count > bufsize || !utf8_check_full(buffer, count, &value))
else {
if (count > bufsize || !utf8_check_full(buffer, count, &value))
return NULL;
}
if(codepoint)
if (codepoint)
*codepoint = value;
return buffer + count;
@ -166,17 +146,15 @@ int utf8_check_string(const char *string, size_t length)
{
size_t i;
for(i = 0; i < length; i++)
{
for (i = 0; i < length; i++) {
size_t count = utf8_check_first(string[i]);
if(count == 0)
if (count == 0)
return 0;
else if(count > 1)
{
if(count > length - i)
else if (count > 1) {
if (count > length - i)
return 0;
if(!utf8_check_full(&string[i], count, NULL))
if (!utf8_check_full(&string[i], count, NULL))
return 0;
i += count - 1;

View file

@ -51,7 +51,7 @@ extern volatile uint32_t hashtable_seed;
json_t *json_object(void)
{
json_object_t *object = jsonp_malloc(sizeof(json_object_t));
if(!object)
if (!object)
return NULL;
if (!hashtable_seed) {
@ -61,8 +61,7 @@ json_t *json_object(void)
json_init(&object->json, JSON_OBJECT);
if(hashtable_init(&object->hashtable))
{
if (hashtable_init(&object->hashtable)) {
jsonp_free(object);
return NULL;
}
@ -80,7 +79,7 @@ size_t json_object_size(const json_t *json)
{
json_object_t *object;
if(!json_is_object(json))
if (!json_is_object(json))
return 0;
object = json_to_object(json);
@ -91,7 +90,7 @@ json_t *json_object_get(const json_t *json, const char *key)
{
json_object_t *object;
if(!key || !json_is_object(json))
if (!key || !json_is_object(json))
return NULL;
object = json_to_object(json);
@ -102,18 +101,16 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
{
json_object_t *object;
if(!value)
if (!value)
return -1;
if(!key || !json_is_object(json) || json == value)
{
if (!key || !json_is_object(json) || json == value) {
json_decref(value);
return -1;
}
object = json_to_object(json);
if(hashtable_set(&object->hashtable, key, value))
{
if (hashtable_set(&object->hashtable, key, value)) {
json_decref(value);
return -1;
}
@ -123,8 +120,7 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
int json_object_set_new(json_t *json, const char *key, json_t *value)
{
if(!key || !utf8_check_string(key, strlen(key)))
{
if (!key || !utf8_check_string(key, strlen(key))) {
json_decref(value);
return -1;
}
@ -136,7 +132,7 @@ int json_object_del(json_t *json, const char *key)
{
json_object_t *object;
if(!key || !json_is_object(json))
if (!key || !json_is_object(json))
return -1;
object = json_to_object(json);
@ -147,7 +143,7 @@ int json_object_clear(json_t *json)
{
json_object_t *object;
if(!json_is_object(json))
if (!json_is_object(json))
return -1;
object = json_to_object(json);
@ -161,11 +157,11 @@ int json_object_update(json_t *object, json_t *other)
const char *key;
json_t *value;
if(!json_is_object(object) || !json_is_object(other))
if (!json_is_object(object) || !json_is_object(other))
return -1;
json_object_foreach(other, key, value) {
if(json_object_set_nocheck(object, key, value))
if (json_object_set_nocheck(object, key, value))
return -1;
}
@ -177,11 +173,11 @@ int json_object_update_existing(json_t *object, json_t *other)
const char *key;
json_t *value;
if(!json_is_object(object) || !json_is_object(other))
if (!json_is_object(object) || !json_is_object(other))
return -1;
json_object_foreach(other, key, value) {
if(json_object_get(object, key))
if (json_object_get(object, key))
json_object_set_nocheck(object, key, value);
}
@ -193,11 +189,11 @@ int json_object_update_missing(json_t *object, json_t *other)
const char *key;
json_t *value;
if(!json_is_object(object) || !json_is_object(other))
if (!json_is_object(object) || !json_is_object(other))
return -1;
json_object_foreach(other, key, value) {
if(!json_object_get(object, key))
if (!json_object_get(object, key))
json_object_set_nocheck(object, key, value);
}
@ -208,7 +204,7 @@ void *json_object_iter(json_t *json)
{
json_object_t *object;
if(!json_is_object(json))
if (!json_is_object(json))
return NULL;
object = json_to_object(json);
@ -219,7 +215,7 @@ void *json_object_iter_at(json_t *json, const char *key)
{
json_object_t *object;
if(!key || !json_is_object(json))
if (!key || !json_is_object(json))
return NULL;
object = json_to_object(json);
@ -230,7 +226,7 @@ void *json_object_iter_next(json_t *json, void *iter)
{
json_object_t *object;
if(!json_is_object(json) || iter == NULL)
if (!json_is_object(json) || iter == NULL)
return NULL;
object = json_to_object(json);
@ -239,7 +235,7 @@ void *json_object_iter_next(json_t *json, void *iter)
const char *json_object_iter_key(void *iter)
{
if(!iter)
if (!iter)
return NULL;
return hashtable_iter_key(iter);
@ -247,7 +243,7 @@ const char *json_object_iter_key(void *iter)
json_t *json_object_iter_value(void *iter)
{
if(!iter)
if (!iter)
return NULL;
return (json_t *)hashtable_iter_value(iter);
@ -255,8 +251,7 @@ json_t *json_object_iter_value(void *iter)
int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
{
if(!json_is_object(json) || !iter || !value)
{
if (!json_is_object(json) || !iter || !value) {
json_decref(value);
return -1;
}
@ -267,7 +262,7 @@ int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
void *json_object_key_to_iter(const char *key)
{
if(!key)
if (!key)
return NULL;
return hashtable_key_to_iter(key);
@ -278,13 +273,13 @@ static int json_object_equal(const json_t *object1, const json_t *object2)
const char *key;
const json_t *value1, *value2;
if(json_object_size(object1) != json_object_size(object2))
if (json_object_size(object1) != json_object_size(object2))
return 0;
json_object_foreach((json_t *)object1, key, value1) {
value2 = json_object_get(object2, key);
if(!json_equal(value1, value2))
if (!json_equal(value1, value2))
return 0;
}
@ -299,11 +294,11 @@ static json_t *json_object_copy(json_t *object)
json_t *value;
result = json_object();
if(!result)
if (!result)
return NULL;
json_object_foreach(object, key, value)
json_object_set_nocheck(result, key, value);
json_object_set_nocheck(result, key, value);
return result;
}
@ -314,13 +309,13 @@ static json_t *json_object_deep_copy(const json_t *object)
void *iter;
result = json_object();
if(!result)
if (!result)
return NULL;
/* Cannot use json_object_foreach because object has to be cast
non-const */
iter = json_object_iter((json_t *)object);
while(iter) {
while (iter) {
const char *key;
const json_t *value;
key = json_object_iter_key(iter);
@ -339,7 +334,7 @@ static json_t *json_object_deep_copy(const json_t *object)
json_t *json_array(void)
{
json_array_t *array = jsonp_malloc(sizeof(json_array_t));
if(!array)
if (!array)
return NULL;
json_init(&array->json, JSON_ARRAY);
@ -347,7 +342,7 @@ json_t *json_array(void)
array->size = 8;
array->table = jsonp_malloc(array->size * sizeof(json_t *));
if(!array->table) {
if (!array->table) {
jsonp_free(array);
return NULL;
}
@ -359,7 +354,7 @@ static void json_delete_array(json_array_t *array)
{
size_t i;
for(i = 0; i < array->entries; i++)
for (i = 0; i < array->entries; i++)
json_decref(array->table[i]);
jsonp_free(array->table);
@ -368,7 +363,7 @@ static void json_delete_array(json_array_t *array)
size_t json_array_size(const json_t *json)
{
if(!json_is_array(json))
if (!json_is_array(json))
return 0;
return json_to_array(json)->entries;
@ -377,11 +372,11 @@ size_t json_array_size(const json_t *json)
json_t *json_array_get(const json_t *json, size_t index)
{
json_array_t *array;
if(!json_is_array(json))
if (!json_is_array(json))
return NULL;
array = json_to_array(json);
if(index >= array->entries)
if (index >= array->entries)
return NULL;
return array->table[index];
@ -391,18 +386,16 @@ int json_array_set_new(json_t *json, size_t index, json_t *value)
{
json_array_t *array;
if(!value)
if (!value)
return -1;
if(!json_is_array(json) || json == value)
{
if (!json_is_array(json) || json == value) {
json_decref(value);
return -1;
}
array = json_to_array(json);
if(index >= array->entries)
{
if (index >= array->entries) {
json_decref(value);
return -1;
}
@ -433,20 +426,20 @@ static json_t **json_array_grow(json_array_t *array,
size_t new_size;
json_t **old_table, **new_table;
if(array->entries + amount <= array->size)
if (array->entries + amount <= array->size)
return array->table;
old_table = array->table;
new_size = max(array->size + amount, array->size * 2);
new_table = jsonp_malloc(new_size * sizeof(json_t *));
if(!new_table)
if (!new_table)
return NULL;
array->size = new_size;
array->table = new_table;
if(copy) {
if (copy) {
array_copy(array->table, 0, old_table, 0, array->entries);
jsonp_free(old_table);
return array->table;
@ -459,17 +452,16 @@ int json_array_append_new(json_t *json, json_t *value)
{
json_array_t *array;
if(!value)
if (!value)
return -1;
if(!json_is_array(json) || json == value)
{
if (!json_is_array(json) || json == value) {
json_decref(value);
return -1;
}
array = json_to_array(json);
if(!json_array_grow(array, 1, 1)) {
if (!json_array_grow(array, 1, 1)) {
json_decref(value);
return -1;
}
@ -485,33 +477,32 @@ int json_array_insert_new(json_t *json, size_t index, json_t *value)
json_array_t *array;
json_t **old_table;
if(!value)
if (!value)
return -1;
if(!json_is_array(json) || json == value) {
if (!json_is_array(json) || json == value) {
json_decref(value);
return -1;
}
array = json_to_array(json);
if(index > array->entries) {
if (index > array->entries) {
json_decref(value);
return -1;
}
old_table = json_array_grow(array, 1, 0);
if(!old_table) {
if (!old_table) {
json_decref(value);
return -1;
}
if(old_table != array->table) {
if (old_table != array->table) {
array_copy(array->table, 0, old_table, 0, index);
array_copy(array->table, index + 1, old_table, index,
array->entries - index);
jsonp_free(old_table);
}
else
} else
array_move(array, index + 1, index, array->entries - index);
array->table[index] = value;
@ -524,17 +515,17 @@ int json_array_remove(json_t *json, size_t index)
{
json_array_t *array;
if(!json_is_array(json))
if (!json_is_array(json))
return -1;
array = json_to_array(json);
if(index >= array->entries)
if (index >= array->entries)
return -1;
json_decref(array->table[index]);
/* If we're removing the last element, nothing has to be moved */
if(index < array->entries - 1)
if (index < array->entries - 1)
array_move(array, index, index + 1, array->entries - index - 1);
array->entries--;
@ -547,11 +538,11 @@ int json_array_clear(json_t *json)
json_array_t *array;
size_t i;
if(!json_is_array(json))
if (!json_is_array(json))
return -1;
array = json_to_array(json);
for(i = 0; i < array->entries; i++)
for (i = 0; i < array->entries; i++)
json_decref(array->table[i]);
array->entries = 0;
@ -563,15 +554,15 @@ int json_array_extend(json_t *json, json_t *other_json)
json_array_t *array, *other;
size_t i;
if(!json_is_array(json) || !json_is_array(other_json))
if (!json_is_array(json) || !json_is_array(other_json))
return -1;
array = json_to_array(json);
other = json_to_array(other_json);
if(!json_array_grow(array, other->entries, 1))
if (!json_array_grow(array, other->entries, 1))
return -1;
for(i = 0; i < other->entries; i++)
for (i = 0; i < other->entries; i++)
json_incref(other->table[i]);
array_copy(array->table, array->entries, other->table, 0, other->entries);
@ -585,17 +576,16 @@ static int json_array_equal(const json_t *array1, const json_t *array2)
size_t i, size;
size = json_array_size(array1);
if(size != json_array_size(array2))
if (size != json_array_size(array2))
return 0;
for(i = 0; i < size; i++)
{
for (i = 0; i < size; i++) {
json_t *value1, *value2;
value1 = json_array_get(array1, i);
value2 = json_array_get(array2, i);
if(!json_equal(value1, value2))
if (!json_equal(value1, value2))
return 0;
}
@ -608,10 +598,10 @@ static json_t *json_array_copy(json_t *array)
size_t i;
result = json_array();
if(!result)
if (!result)
return NULL;
for(i = 0; i < json_array_size(array); i++)
for (i = 0; i < json_array_size(array); i++)
json_array_append(result, json_array_get(array, i));
return result;
@ -623,10 +613,10 @@ static json_t *json_array_deep_copy(const json_t *array)
size_t i;
result = json_array();
if(!result)
if (!result)
return NULL;
for(i = 0; i < json_array_size(array); i++)
for (i = 0; i < json_array_size(array); i++)
json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
return result;
@ -639,19 +629,19 @@ static json_t *string_create(const char *value, size_t len, int own)
char *v;
json_string_t *string;
if(!value)
if (!value)
return NULL;
if(own)
if (own)
v = (char *)value;
else {
v = jsonp_strndup(value, len);
if(!v)
if (!v)
return NULL;
}
string = jsonp_malloc(sizeof(json_string_t));
if(!string) {
if (!string) {
jsonp_free(v);
return NULL;
}
@ -664,7 +654,7 @@ static json_t *string_create(const char *value, size_t len, int own)
json_t *json_string_nocheck(const char *value)
{
if(!value)
if (!value)
return NULL;
return string_create(value, strlen(value), 0);
@ -683,7 +673,7 @@ json_t *jsonp_stringn_nocheck_own(const char *value, size_t len)
json_t *json_string(const char *value)
{
if(!value)
if (!value)
return NULL;
return json_stringn(value, strlen(value));
@ -691,7 +681,7 @@ json_t *json_string(const char *value)
json_t *json_stringn(const char *value, size_t len)
{
if(!value || !utf8_check_string(value, len))
if (!value || !utf8_check_string(value, len))
return NULL;
return json_stringn_nocheck(value, len);
@ -699,7 +689,7 @@ json_t *json_stringn(const char *value, size_t len)
const char *json_string_value(const json_t *json)
{
if(!json_is_string(json))
if (!json_is_string(json))
return NULL;
return json_to_string(json)->value;
@ -707,7 +697,7 @@ const char *json_string_value(const json_t *json)
size_t json_string_length(const json_t *json)
{
if(!json_is_string(json))
if (!json_is_string(json))
return 0;
return json_to_string(json)->length;
@ -715,7 +705,7 @@ size_t json_string_length(const json_t *json)
int json_string_set_nocheck(json_t *json, const char *value)
{
if(!value)
if (!value)
return -1;
return json_string_setn_nocheck(json, value, strlen(value));
@ -726,11 +716,11 @@ int json_string_setn_nocheck(json_t *json, const char *value, size_t len)
char *dup;
json_string_t *string;
if(!json_is_string(json) || !value)
if (!json_is_string(json) || !value)
return -1;
dup = jsonp_strndup(value, len);
if(!dup)
if (!dup)
return -1;
string = json_to_string(json);
@ -743,7 +733,7 @@ int json_string_setn_nocheck(json_t *json, const char *value, size_t len)
int json_string_set(json_t *json, const char *value)
{
if(!value)
if (!value)
return -1;
return json_string_setn(json, value, strlen(value));
@ -751,7 +741,7 @@ int json_string_set(json_t *json, const char *value)
int json_string_setn(json_t *json, const char *value, size_t len)
{
if(!value || !utf8_check_string(value, len))
if (!value || !utf8_check_string(value, len))
return -1;
return json_string_setn_nocheck(json, value, len);
@ -780,7 +770,8 @@ static json_t *json_string_copy(const json_t *string)
return json_stringn_nocheck(s->value, s->length);
}
json_t *json_vsprintf(const char *fmt, va_list ap) {
json_t *json_vsprintf(const char *fmt, va_list ap)
{
json_t *json = NULL;
int length;
char *buf;
@ -810,7 +801,8 @@ out:
return json;
}
json_t *json_sprintf(const char *fmt, ...) {
json_t *json_sprintf(const char *fmt, ...)
{
json_t *result;
va_list ap;
@ -827,7 +819,7 @@ json_t *json_sprintf(const char *fmt, ...) {
json_t *json_integer(json_int_t value)
{
json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
if(!integer)
if (!integer)
return NULL;
json_init(&integer->json, JSON_INTEGER);
@ -837,7 +829,7 @@ json_t *json_integer(json_int_t value)
json_int_t json_integer_value(const json_t *json)
{
if(!json_is_integer(json))
if (!json_is_integer(json))
return 0;
return json_to_integer(json)->value;
@ -845,7 +837,7 @@ json_int_t json_integer_value(const json_t *json)
int json_integer_set(json_t *json, json_int_t value)
{
if(!json_is_integer(json))
if (!json_is_integer(json))
return -1;
json_to_integer(json)->value = value;
@ -875,11 +867,11 @@ json_t *json_real(double value)
{
json_real_t *real;
if(isnan(value) || isinf(value))
if (isnan(value) || isinf(value))
return NULL;
real = jsonp_malloc(sizeof(json_real_t));
if(!real)
if (!real)
return NULL;
json_init(&real->json, JSON_REAL);
@ -889,7 +881,7 @@ json_t *json_real(double value)
double json_real_value(const json_t *json)
{
if(!json_is_real(json))
if (!json_is_real(json))
return 0;
return json_to_real(json)->value;
@ -897,7 +889,7 @@ double json_real_value(const json_t *json)
int json_real_set(json_t *json, double value)
{
if(!json_is_real(json) || isnan(value) || isinf(value))
if (!json_is_real(json) || isnan(value) || isinf(value))
return -1;
json_to_real(json)->value = value;
@ -925,9 +917,9 @@ static json_t *json_real_copy(const json_t *real)
double json_number_value(const json_t *json)
{
if(json_is_integer(json))
if (json_is_integer(json))
return (double)json_integer_value(json);
else if(json_is_real(json))
else if (json_is_real(json))
return json_real_value(json);
else
return 0.0;
@ -938,21 +930,21 @@ double json_number_value(const json_t *json)
json_t *json_true(void)
{
static json_t the_true = {JSON_TRUE, (size_t)-1};
static json_t the_true = {JSON_TRUE, (size_t) -1};
return &the_true;
}
json_t *json_false(void)
{
static json_t the_false = {JSON_FALSE, (size_t)-1};
static json_t the_false = {JSON_FALSE, (size_t) -1};
return &the_false;
}
json_t *json_null(void)
{
static json_t the_null = {JSON_NULL, (size_t)-1};
static json_t the_null = {JSON_NULL, (size_t) -1};
return &the_null;
}
@ -964,7 +956,7 @@ void json_delete(json_t *json)
if (!json)
return;
switch(json_typeof(json)) {
switch (json_typeof(json)) {
case JSON_OBJECT:
json_delete_object(json_to_object(json));
break;
@ -992,17 +984,17 @@ void json_delete(json_t *json)
int json_equal(const json_t *json1, const json_t *json2)
{
if(!json1 || !json2)
if (!json1 || !json2)
return 0;
if(json_typeof(json1) != json_typeof(json2))
if (json_typeof(json1) != json_typeof(json2))
return 0;
/* this covers true, false and null as they are singletons */
if(json1 == json2)
if (json1 == json2)
return 1;
switch(json_typeof(json1)) {
switch (json_typeof(json1)) {
case JSON_OBJECT:
return json_object_equal(json1, json2);
case JSON_ARRAY:
@ -1023,10 +1015,10 @@ int json_equal(const json_t *json1, const json_t *json2)
json_t *json_copy(json_t *json)
{
if(!json)
if (!json)
return NULL;
switch(json_typeof(json)) {
switch (json_typeof(json)) {
case JSON_OBJECT:
return json_object_copy(json);
case JSON_ARRAY:
@ -1048,16 +1040,16 @@ json_t *json_copy(json_t *json)
json_t *json_deep_copy(const json_t *json)
{
if(!json)
if (!json)
return NULL;
switch(json_typeof(json)) {
switch (json_typeof(json)) {
case JSON_OBJECT:
return json_object_deep_copy(json);
case JSON_ARRAY:
return json_array_deep_copy(json);
/* for the rest of the types, deep copying doesn't differ from
shallow copying */
/* for the rest of the types, deep copying doesn't differ from
shallow copying */
case JSON_STRING:
return json_string_copy(json);
case JSON_INTEGER: