value.c (jansson-2.13.1.tar.bz2) | : | value.c (jansson-2.14.tar.bz2) | ||
---|---|---|---|---|
skipping to change at line 47 | skipping to change at line 47 | |||
static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); } | static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); } | |||
#endif | #endif | |||
json_t *do_deep_copy(const json_t *json, hashtable_t *parents); | json_t *do_deep_copy(const json_t *json, hashtable_t *parents); | |||
static JSON_INLINE void json_init(json_t *json, json_type type) { | static JSON_INLINE void json_init(json_t *json, json_type type) { | |||
json->type = type; | json->type = type; | |||
json->refcount = 1; | json->refcount = 1; | |||
} | } | |||
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, | int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t | |||
size_t key_size) { | key_size, | |||
snprintf(key, key_size, "%p", json); | size_t *key_len_out) { | |||
if (hashtable_get(parents, key)) | size_t key_len = snprintf(key, key_size, "%p", json); | |||
if (key_len_out) | ||||
*key_len_out = key_len; | ||||
if (hashtable_get(parents, key, key_len)) | ||||
return -1; | return -1; | |||
return hashtable_set(parents, key, json_null()); | return hashtable_set(parents, key, key_len, json_null()); | |||
} | } | |||
/*** object ***/ | /*** object ***/ | |||
extern volatile uint32_t hashtable_seed; | extern volatile uint32_t hashtable_seed; | |||
json_t *json_object(void) { | json_t *json_object(void) { | |||
json_object_t *object = jsonp_malloc(sizeof(json_object_t)); | json_object_t *object = jsonp_malloc(sizeof(json_object_t)); | |||
if (!object) | if (!object) | |||
return NULL; | return NULL; | |||
skipping to change at line 96 | skipping to change at line 100 | |||
json_object_t *object; | json_object_t *object; | |||
if (!json_is_object(json)) | if (!json_is_object(json)) | |||
return 0; | return 0; | |||
object = json_to_object(json); | object = json_to_object(json); | |||
return object->hashtable.size; | return object->hashtable.size; | |||
} | } | |||
json_t *json_object_get(const json_t *json, const char *key) { | json_t *json_object_get(const json_t *json, const char *key) { | |||
if (!key) | ||||
return NULL; | ||||
return json_object_getn(json, key, strlen(key)); | ||||
} | ||||
json_t *json_object_getn(const json_t *json, const char *key, size_t key_len) { | ||||
json_object_t *object; | json_object_t *object; | |||
if (!key || !json_is_object(json)) | if (!key || !json_is_object(json)) | |||
return NULL; | return NULL; | |||
object = json_to_object(json); | object = json_to_object(json); | |||
return hashtable_get(&object->hashtable, key); | return hashtable_get(&object->hashtable, key, key_len); | |||
} | } | |||
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { | int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { | |||
if (!key) { | ||||
json_decref(value); | ||||
return -1; | ||||
} | ||||
return json_object_setn_new_nocheck(json, key, strlen(key), value); | ||||
} | ||||
int json_object_setn_new_nocheck(json_t *json, const char *key, size_t key_len, | ||||
json_t *value) { | ||||
json_object_t *object; | json_object_t *object; | |||
if (!value) | if (!value) | |||
return -1; | return -1; | |||
if (!key || !json_is_object(json) || json == value) { | if (!key || !json_is_object(json) || json == value) { | |||
json_decref(value); | json_decref(value); | |||
return -1; | return -1; | |||
} | } | |||
object = json_to_object(json); | object = json_to_object(json); | |||
if (hashtable_set(&object->hashtable, key, value)) { | if (hashtable_set(&object->hashtable, key, key_len, value)) { | |||
json_decref(value); | json_decref(value); | |||
return -1; | return -1; | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int json_object_set_new(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) { | |||
json_decref(value); | ||||
return -1; | ||||
} | ||||
return json_object_setn_new(json, key, strlen(key), value); | ||||
} | ||||
int json_object_setn_new(json_t *json, const char *key, size_t key_len, json_t * | ||||
value) { | ||||
if (!key || !utf8_check_string(key, key_len)) { | ||||
json_decref(value); | json_decref(value); | |||
return -1; | return -1; | |||
} | } | |||
return json_object_set_new_nocheck(json, key, value); | return json_object_setn_new_nocheck(json, key, key_len, value); | |||
} | } | |||
int json_object_del(json_t *json, const char *key) { | int json_object_del(json_t *json, const char *key) { | |||
if (!key) | ||||
return -1; | ||||
return json_object_deln(json, key, strlen(key)); | ||||
} | ||||
int json_object_deln(json_t *json, const char *key, size_t key_len) { | ||||
json_object_t *object; | json_object_t *object; | |||
if (!key || !json_is_object(json)) | if (!key || !json_is_object(json)) | |||
return -1; | return -1; | |||
object = json_to_object(json); | object = json_to_object(json); | |||
return hashtable_del(&object->hashtable, key); | return hashtable_del(&object->hashtable, key, key_len); | |||
} | } | |||
int json_object_clear(json_t *json) { | int json_object_clear(json_t *json) { | |||
json_object_t *object; | json_object_t *object; | |||
if (!json_is_object(json)) | if (!json_is_object(json)) | |||
return -1; | return -1; | |||
object = json_to_object(json); | object = json_to_object(json); | |||
hashtable_clear(&object->hashtable); | hashtable_clear(&object->hashtable); | |||
skipping to change at line 173 | skipping to change at line 209 | |||
json_object_foreach(other, key, value) { | json_object_foreach(other, key, value) { | |||
if (json_object_set_nocheck(object, key, value)) | if (json_object_set_nocheck(object, key, value)) | |||
return -1; | return -1; | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int json_object_update_existing(json_t *object, json_t *other) { | int json_object_update_existing(json_t *object, json_t *other) { | |||
const char *key; | const char *key; | |||
size_t key_len; | ||||
json_t *value; | json_t *value; | |||
if (!json_is_object(object) || !json_is_object(other)) | if (!json_is_object(object) || !json_is_object(other)) | |||
return -1; | return -1; | |||
json_object_foreach(other, key, value) { | json_object_keylen_foreach(other, key, key_len, value) { | |||
if (json_object_get(object, key)) | if (json_object_getn(object, key, key_len)) | |||
json_object_set_nocheck(object, key, value); | json_object_setn_nocheck(object, key, key_len, value); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int json_object_update_missing(json_t *object, json_t *other) { | int json_object_update_missing(json_t *object, json_t *other) { | |||
const char *key; | const char *key; | |||
json_t *value; | json_t *value; | |||
if (!json_is_object(object) || !json_is_object(other)) | if (!json_is_object(object) || !json_is_object(other)) | |||
skipping to change at line 203 | skipping to change at line 240 | |||
json_object_foreach(other, key, value) { | 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); | json_object_set_nocheck(object, key, value); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *paren ts) { | int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *paren ts) { | |||
const char *key; | const char *key; | |||
size_t key_len; | ||||
json_t *value; | json_t *value; | |||
char loop_key[LOOP_KEY_LEN]; | char loop_key[LOOP_KEY_LEN]; | |||
int res = 0; | int res = 0; | |||
size_t loop_key_len; | ||||
if (!json_is_object(object) || !json_is_object(other)) | if (!json_is_object(object) || !json_is_object(other)) | |||
return -1; | return -1; | |||
if (jsonp_loop_check(parents, other, loop_key, sizeof(loop_key))) | if (jsonp_loop_check(parents, other, loop_key, sizeof(loop_key), &loop_key_l en)) | |||
return -1; | return -1; | |||
json_object_foreach(other, key, value) { | json_object_keylen_foreach(other, key, key_len, value) { | |||
json_t *v = json_object_get(object, key); | json_t *v = json_object_get(object, key); | |||
if (json_is_object(v) && json_is_object(value)) { | if (json_is_object(v) && json_is_object(value)) { | |||
if (do_object_update_recursive(v, value, parents)) { | if (do_object_update_recursive(v, value, parents)) { | |||
res = -1; | res = -1; | |||
break; | break; | |||
} | } | |||
} else { | } else { | |||
if (json_object_set_nocheck(object, key, value)) { | if (json_object_setn_nocheck(object, key, key_len, value)) { | |||
res = -1; | res = -1; | |||
break; | break; | |||
} | } | |||
} | } | |||
} | } | |||
hashtable_del(parents, loop_key); | hashtable_del(parents, loop_key, loop_key_len); | |||
return res; | return res; | |||
} | } | |||
int json_object_update_recursive(json_t *object, json_t *other) { | int json_object_update_recursive(json_t *object, json_t *other) { | |||
int res; | int res; | |||
hashtable_t parents_set; | hashtable_t parents_set; | |||
if (hashtable_init(&parents_set)) | if (hashtable_init(&parents_set)) | |||
return -1; | return -1; | |||
skipping to change at line 263 | skipping to change at line 302 | |||
return hashtable_iter(&object->hashtable); | return hashtable_iter(&object->hashtable); | |||
} | } | |||
void *json_object_iter_at(json_t *json, const char *key) { | void *json_object_iter_at(json_t *json, const char *key) { | |||
json_object_t *object; | json_object_t *object; | |||
if (!key || !json_is_object(json)) | if (!key || !json_is_object(json)) | |||
return NULL; | return NULL; | |||
object = json_to_object(json); | object = json_to_object(json); | |||
return hashtable_iter_at(&object->hashtable, key); | return hashtable_iter_at(&object->hashtable, key, strlen(key)); | |||
} | } | |||
void *json_object_iter_next(json_t *json, void *iter) { | void *json_object_iter_next(json_t *json, void *iter) { | |||
json_object_t *object; | json_object_t *object; | |||
if (!json_is_object(json) || iter == NULL) | if (!json_is_object(json) || iter == NULL) | |||
return NULL; | return NULL; | |||
object = json_to_object(json); | object = json_to_object(json); | |||
return hashtable_iter_next(&object->hashtable, iter); | return hashtable_iter_next(&object->hashtable, iter); | |||
} | } | |||
const char *json_object_iter_key(void *iter) { | const char *json_object_iter_key(void *iter) { | |||
if (!iter) | if (!iter) | |||
return NULL; | return NULL; | |||
return hashtable_iter_key(iter); | return hashtable_iter_key(iter); | |||
} | } | |||
size_t json_object_iter_key_len(void *iter) { | ||||
if (!iter) | ||||
return 0; | ||||
return hashtable_iter_key_len(iter); | ||||
} | ||||
json_t *json_object_iter_value(void *iter) { | json_t *json_object_iter_value(void *iter) { | |||
if (!iter) | if (!iter) | |||
return NULL; | return NULL; | |||
return (json_t *)hashtable_iter_value(iter); | return (json_t *)hashtable_iter_value(iter); | |||
} | } | |||
int json_object_iter_set_new(json_t *json, void *iter, json_t *value) { | 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); | json_decref(value); | |||
skipping to change at line 343 | skipping to change at line 389 | |||
json_object_foreach(object, key, value) json_object_set_nocheck(result, key, value); | json_object_foreach(object, key, value) json_object_set_nocheck(result, key, value); | |||
return result; | return result; | |||
} | } | |||
static json_t *json_object_deep_copy(const json_t *object, hashtable_t *parents) { | static json_t *json_object_deep_copy(const json_t *object, hashtable_t *parents) { | |||
json_t *result; | json_t *result; | |||
void *iter; | void *iter; | |||
char loop_key[LOOP_KEY_LEN]; | char loop_key[LOOP_KEY_LEN]; | |||
size_t loop_key_len; | ||||
if (jsonp_loop_check(parents, object, loop_key, sizeof(loop_key))) | if (jsonp_loop_check(parents, object, loop_key, sizeof(loop_key), &loop_key_ len)) | |||
return NULL; | return NULL; | |||
result = json_object(); | result = json_object(); | |||
if (!result) | if (!result) | |||
goto out; | goto out; | |||
/* Cannot use json_object_foreach because object has to be cast | /* Cannot use json_object_foreach because object has to be cast | |||
non-const */ | non-const */ | |||
iter = json_object_iter((json_t *)object); | iter = json_object_iter((json_t *)object); | |||
while (iter) { | while (iter) { | |||
skipping to change at line 369 | skipping to change at line 416 | |||
if (json_object_set_new_nocheck(result, key, do_deep_copy(value, parents ))) { | if (json_object_set_new_nocheck(result, key, do_deep_copy(value, parents ))) { | |||
json_decref(result); | json_decref(result); | |||
result = NULL; | result = NULL; | |||
break; | break; | |||
} | } | |||
iter = json_object_iter_next((json_t *)object, iter); | iter = json_object_iter_next((json_t *)object, iter); | |||
} | } | |||
out: | out: | |||
hashtable_del(parents, loop_key); | hashtable_del(parents, loop_key, loop_key_len); | |||
return result; | return result; | |||
} | } | |||
/*** array ***/ | /*** array ***/ | |||
json_t *json_array(void) { | json_t *json_array(void) { | |||
json_array_t *array = jsonp_malloc(sizeof(json_array_t)); | json_array_t *array = jsonp_malloc(sizeof(json_array_t)); | |||
if (!array) | if (!array) | |||
return NULL; | return NULL; | |||
skipping to change at line 636 | skipping to change at line 683 | |||
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)); | json_array_append(result, json_array_get(array, i)); | |||
return result; | return result; | |||
} | } | |||
static json_t *json_array_deep_copy(const json_t *array, hashtable_t *parents) { | static json_t *json_array_deep_copy(const json_t *array, hashtable_t *parents) { | |||
json_t *result; | json_t *result; | |||
size_t i; | size_t i; | |||
char loop_key[LOOP_KEY_LEN]; | char loop_key[LOOP_KEY_LEN]; | |||
size_t loop_key_len; | ||||
if (jsonp_loop_check(parents, array, loop_key, sizeof(loop_key))) | if (jsonp_loop_check(parents, array, loop_key, sizeof(loop_key), &loop_key_l en)) | |||
return NULL; | return NULL; | |||
result = json_array(); | result = json_array(); | |||
if (!result) | if (!result) | |||
goto out; | goto out; | |||
for (i = 0; i < json_array_size(array); i++) { | for (i = 0; i < json_array_size(array); i++) { | |||
if (json_array_append_new(result, | if (json_array_append_new(result, | |||
do_deep_copy(json_array_get(array, i), parents ))) { | do_deep_copy(json_array_get(array, i), parents ))) { | |||
json_decref(result); | json_decref(result); | |||
result = NULL; | result = NULL; | |||
break; | break; | |||
} | } | |||
} | } | |||
out: | out: | |||
hashtable_del(parents, loop_key); | hashtable_del(parents, loop_key, loop_key_len); | |||
return result; | return result; | |||
} | } | |||
/*** string ***/ | /*** string ***/ | |||
static json_t *string_create(const char *value, size_t len, int own) { | static json_t *string_create(const char *value, size_t len, int own) { | |||
char *v; | char *v; | |||
json_string_t *string; | json_string_t *string; | |||
skipping to change at line 800 | skipping to change at line 848 | |||
} | } | |||
json_t *json_vsprintf(const char *fmt, va_list ap) { | json_t *json_vsprintf(const char *fmt, va_list ap) { | |||
json_t *json = NULL; | json_t *json = NULL; | |||
int length; | int length; | |||
char *buf; | char *buf; | |||
va_list aq; | va_list aq; | |||
va_copy(aq, ap); | va_copy(aq, ap); | |||
length = vsnprintf(NULL, 0, fmt, ap); | length = vsnprintf(NULL, 0, fmt, ap); | |||
if (length < 0) | ||||
goto out; | ||||
if (length == 0) { | if (length == 0) { | |||
json = json_string(""); | json = json_string(""); | |||
goto out; | goto out; | |||
} | } | |||
buf = jsonp_malloc(length + 1); | buf = jsonp_malloc((size_t)length + 1); | |||
if (!buf) | if (!buf) | |||
goto out; | goto out; | |||
vsnprintf(buf, length + 1, fmt, aq); | vsnprintf(buf, (size_t)length + 1, fmt, aq); | |||
if (!utf8_check_string(buf, length)) { | if (!utf8_check_string(buf, length)) { | |||
jsonp_free(buf); | jsonp_free(buf); | |||
goto out; | goto out; | |||
} | } | |||
json = jsonp_stringn_nocheck_own(buf, length); | json = jsonp_stringn_nocheck_own(buf, length); | |||
out: | out: | |||
va_end(aq); | va_end(aq); | |||
return json; | return json; | |||
End of changes. 29 change blocks. | ||||
24 lines changed or deleted | 76 lines changed or added |