Run clang-format on *.c, *.h

This commit is contained in:
Petri Lehtinen 2019-10-17 09:08:51 +03:00
parent 7dc463ee4e
commit 79fe8c3435
39 changed files with 2508 additions and 2913 deletions

View File

@ -8,8 +8,8 @@
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include <curl/curl.h>
#include <jansson.h>
#define BUFFER_SIZE (256 * 1024) /* 256 KB */
@ -18,8 +18,7 @@
/* Return the offset of the first newline in text or the length of
text if there's no newline */
static int newline_offset(const char *text)
{
static int newline_offset(const char *text) {
const char *newline = strchr(text, '\n');
if (!newline)
return strlen(text);
@ -27,18 +26,16 @@ static int newline_offset(const char *text)
return (int)(newline - text);
}
struct write_result
{
struct write_result {
char *data;
int pos;
};
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
static size_t write_response(void *ptr, size_t size, size_t nmemb,
void *stream) {
struct write_result *result = (struct write_result *)stream;
if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
{
if (result->pos + size * nmemb >= BUFFER_SIZE - 1) {
fprintf(stderr, "error: too small buffer\n");
return 0;
}
@ -49,8 +46,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
return size * nmemb;
}
static char *request(const char *url)
{
static char *request(const char *url) {
CURL *curl = NULL;
CURLcode status;
struct curl_slist *headers = NULL;
@ -66,10 +62,7 @@ static char *request(const char *url)
if (!data)
goto error;
struct write_result write_result = {
.data = data,
.pos = 0
};
struct write_result write_result = {.data = data, .pos = 0};
curl_easy_setopt(curl, CURLOPT_URL, url);
@ -81,16 +74,14 @@ static char *request(const char *url)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
status = curl_easy_perform(curl);
if(status != 0)
{
if (status != 0) {
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
goto error;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200)
{
if (code != 200) {
fprintf(stderr, "error: server responded with code %ld\n", code);
goto error;
}
@ -115,8 +106,7 @@ error:
return NULL;
}
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
size_t i;
char *text;
char url[URL_SIZE];
@ -124,8 +114,7 @@ int main(int argc, char *argv[])
json_t *root;
json_error_t error;
if(argc != 3)
{
if (argc != 3) {
fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
return 2;
@ -140,60 +129,55 @@ int main(int argc, char *argv[])
root = json_loads(text, 0, &error);
free(text);
if(!root)
{
if (!root) {
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return 1;
}
if(!json_is_array(root))
{
if (!json_is_array(root)) {
fprintf(stderr, "error: root is not an array\n");
json_decref(root);
return 1;
}
for(i = 0; i < json_array_size(root); i++)
{
for (i = 0; i < json_array_size(root); i++) {
json_t *data, *sha, *commit, *message;
const char *message_text;
data = json_array_get(root, i);
if(!json_is_object(data))
{
fprintf(stderr, "error: commit data %d is not an object\n", (int)(i + 1));
if (!json_is_object(data)) {
fprintf(stderr, "error: commit data %d is not an object\n",
(int)(i + 1));
json_decref(root);
return 1;
}
sha = json_object_get(data, "sha");
if(!json_is_string(sha))
{
fprintf(stderr, "error: commit %d: sha is not a string\n", (int)(i + 1));
if (!json_is_string(sha)) {
fprintf(stderr, "error: commit %d: sha is not a string\n",
(int)(i + 1));
return 1;
}
commit = json_object_get(data, "commit");
if(!json_is_object(commit))
{
fprintf(stderr, "error: commit %d: commit is not an object\n", (int)(i + 1));
if (!json_is_object(commit)) {
fprintf(stderr, "error: commit %d: commit is not an object\n",
(int)(i + 1));
json_decref(root);
return 1;
}
message = json_object_get(commit, "message");
if(!json_is_string(message))
{
fprintf(stderr, "error: commit %d: message is not a string\n", (int)(i + 1));
if (!json_is_string(message)) {
fprintf(stderr, "error: commit %d: message is not a string\n",
(int)(i + 1));
json_decref(root);
return 1;
}
message_text = json_string_value(message);
printf("%.8s %.*s\n",
json_string_value(sha),
newline_offset(message_text),
message_text);
printf("%.8s %.*s\n", json_string_value(sha),
newline_offset(message_text), message_text);
}
json_decref(root);

View File

@ -22,9 +22,9 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <jansson.h>
#include <stdio.h>
#include <stdlib.h>
#include <jansson.h>
/* forward refs */
void print_json(json_t *root);
@ -40,49 +40,32 @@ void print_json_true(json_t *element, int indent);
void print_json_false(json_t *element, int indent);
void print_json_null(json_t *element, int indent);
void print_json(json_t *root) {
print_json_aux(root, 0);
}
void print_json(json_t *root) { print_json_aux(root, 0); }
void print_json_aux(json_t *element, int indent) {
switch (json_typeof(element)) {
case JSON_OBJECT:
print_json_object(element, indent);
break;
case JSON_ARRAY:
print_json_array(element, indent);
break;
case JSON_STRING:
print_json_string(element, indent);
break;
case JSON_INTEGER:
print_json_integer(element, indent);
break;
case JSON_REAL:
print_json_real(element, indent);
break;
case JSON_TRUE:
print_json_true(element, indent);
break;
case JSON_FALSE:
print_json_false(element, indent);
break;
case JSON_NULL:
print_json_null(element, indent);
break;
case JSON_OBJECT: print_json_object(element, indent); break;
case JSON_ARRAY: print_json_array(element, indent); break;
case JSON_STRING: print_json_string(element, indent); break;
case JSON_INTEGER: print_json_integer(element, indent); break;
case JSON_REAL: print_json_real(element, indent); break;
case JSON_TRUE: print_json_true(element, indent); break;
case JSON_FALSE: print_json_false(element, indent); break;
case JSON_NULL: print_json_null(element, indent); break;
default:
fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
fprintf(stderr, "unrecognized JSON type %d\n",
json_typeof(element));
}
}
void print_json_indent(int indent) {
int i;
for (i = 0; i < indent; i++) { putchar(' '); }
for (i = 0; i < indent; i++) {
putchar(' ');
}
}
const char *json_plural(int count) {
return count == 1 ? "" : "s";
}
const char *json_plural(int count) { return count == 1 ? "" : "s"; }
void print_json_object(json_t *element, int indent) {
size_t size;
@ -98,7 +81,6 @@ void print_json_object(json_t *element, int indent) {
printf("JSON Key: \"%s\"\n", key);
print_json_aux(value, indent + 2);
}
}
void print_json_array(json_t *element, int indent) {
@ -119,7 +101,8 @@ void print_json_string(json_t *element, int indent) {
void print_json_integer(json_t *element, int indent) {
print_json_indent(indent);
printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element));
printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n",
json_integer_value(element));
}
void print_json_real(json_t *element, int indent) {

View File

@ -11,10 +11,10 @@
#include "jansson_private.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -35,13 +35,11 @@ struct buffer {
char *data;
};
static int dump_to_strbuffer(const char *buffer, size_t size, void *data)
{
static int dump_to_strbuffer(const char *buffer, size_t size, void *data) {
return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
}
static int dump_to_buffer(const char *buffer, size_t size, void *data)
{
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)
@ -51,16 +49,14 @@ static int dump_to_buffer(const char *buffer, size_t size, void *data)
return 0;
}
static int dump_to_file(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)
return -1;
return 0;
}
static int dump_to_fd(const char *buffer, size_t size, void *data)
{
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)
@ -72,34 +68,33 @@ static int dump_to_fd(const char *buffer, size_t size, void *data)
/* 32 spaces (the maximum indentation size) */
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)
{
unsigned int ws_count = FLAGS_TO_INDENT(flags), n_spaces = depth * ws_count;
static int dump_indent(size_t flags, int depth, int space,
json_dump_callback_t dump, void *data) {
if (FLAGS_TO_INDENT(flags) > 0) {
unsigned int ws_count = FLAGS_TO_INDENT(flags),
n_spaces = depth * ws_count;
if (dump("\n", 1, data))
return -1;
while(n_spaces > 0)
{
int cur_n = n_spaces < sizeof whitespace - 1 ? n_spaces : sizeof whitespace - 1;
while (n_spaces > 0) {
int cur_n = n_spaces < sizeof whitespace - 1
? n_spaces
: sizeof whitespace - 1;
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;
}
static int dump_string(const char *str, size_t len, json_dump_callback_t dump, void *data, size_t flags)
{
static int dump_string(const char *str, size_t len, json_dump_callback_t dump,
void *data, size_t flags) {
const char *pos, *end, *lim;
int32_t codepoint = 0;
@ -108,14 +103,12 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
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)
return -1;
@ -145,8 +138,7 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
/* handle \, /, ", and control codes */
length = 2;
switch(codepoint)
{
switch (codepoint) {
case '\\': text = "\\\\"; break;
case '\"': text = "\\\""; break;
case '\b': text = "\\b"; break;
@ -155,25 +147,24 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
case '\r': text = "\\r"; break;
case '\t': text = "\\t"; break;
case '/': text = "\\/"; break;
default:
{
default: {
/* codepoint is in BMP */
if(codepoint < 0x10000)
{
snprintf(seq, sizeof(seq), "\\u%04X", (unsigned int)codepoint);
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;
first = 0xD800 | ((codepoint & 0xffc00) >> 10);
last = 0xDC00 | (codepoint & 0x003ff);
snprintf(seq, sizeof(seq), "\\u%04X\\u%04X", (unsigned int)first, (unsigned int)last);
snprintf(seq, sizeof(seq), "\\u%04X\\u%04X",
(unsigned int)first, (unsigned int)last);
length = 12;
}
@ -191,14 +182,13 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
return dump("\"", 1, data);
}
static int compare_keys(const void *key1, const void *key2)
{
static int compare_keys(const void *key1, const void *key2) {
return strcmp(*(const char **)key1, *(const char **)key2);
}
static int do_dump(const json_t *json, size_t flags, int depth,
hashtable_t *parents, json_dump_callback_t dump, void *data)
{
hashtable_t *parents, json_dump_callback_t dump,
void *data) {
int embed = flags & JSON_EMBED;
flags &= ~JSON_EMBED;
@ -207,31 +197,25 @@ static int do_dump(const json_t *json, size_t flags, int depth,
return -1;
switch (json_typeof(json)) {
case JSON_NULL:
return dump("null", 4, data);
case JSON_NULL: return dump("null", 4, data);
case JSON_TRUE:
return dump("true", 4, data);
case JSON_TRUE: return dump("true", 4, data);
case JSON_FALSE:
return dump("false", 5, data);
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));
"%" JSON_INTEGER_FORMAT, json_integer_value(json));
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);
@ -245,13 +229,14 @@ 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);
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. */
/* Space for "0x", double the sizeof a pointer for the hex and a
* terminator. */
char key[2 + (sizeof(json) * 2) + 1];
/* detect circular references */
@ -270,18 +255,15 @@ static int do_dump(const json_t *json, size_t flags, int depth,
return -1;
for (i = 0; i < n; ++i) {
if(do_dump(json_array_get(json, i), flags, depth + 1,
parents, dump, data))
if (do_dump(json_array_get(json, i), flags, depth + 1, parents,
dump, data))
return -1;
if(i < n - 1)
{
if (i < n - 1) {
if (dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
return -1;
}
else
{
} else {
if (dump_indent(flags, depth, 0, dump, data))
return -1;
}
@ -291,8 +273,7 @@ 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;
@ -301,8 +282,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
if (flags & JSON_COMPACT) {
separator = ":";
separator_length = 1;
}
else {
} else {
separator = ": ";
separator_length = 2;
}
@ -322,8 +302,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
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;
@ -333,8 +312,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
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++;
@ -343,8 +321,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;
@ -354,25 +331,19 @@ static int do_dump(const json_t *json, size_t flags, int depth,
dump_string(key, strlen(key), dump, data, flags);
if (dump(separator, separator_length, data) ||
do_dump(value, flags, depth + 1, parents, dump, data))
{
do_dump(value, flags, depth + 1, parents, dump, data)) {
jsonp_free(keys);
return -1;
}
if(i < size - 1)
{
if (i < size - 1) {
if (dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, 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;
}
@ -380,13 +351,10 @@ 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);
@ -396,14 +364,11 @@ static int do_dump(const json_t *json, size_t flags, int depth,
parents, dump, data))
return -1;
if(next)
{
if (next) {
if (dump(",", 1, data) ||
dump_indent(flags, depth + 1, 1, dump, data))
return -1;
}
else
{
} else {
if (dump_indent(flags, depth, 0, dump, data))
return -1;
}
@ -422,8 +387,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
}
}
char *json_dumps(const json_t *json, size_t flags)
{
char *json_dumps(const json_t *json, size_t flags) {
strbuffer_t strbuff;
char *result;
@ -439,8 +403,7 @@ char *json_dumps(const json_t *json, size_t flags)
return result;
}
size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags)
{
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))
@ -449,18 +412,15 @@ size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags)
return buf.used;
}
int json_dumpf(const json_t *json, FILE *output, size_t flags)
{
int json_dumpf(const json_t *json, FILE *output, size_t flags) {
return json_dump_callback(json, dump_to_file, (void *)output, flags);
}
int json_dumpfd(const json_t *json, int output, size_t flags)
{
int json_dumpfd(const json_t *json, int output, size_t flags) {
return json_dump_callback(json, dump_to_fd, (void *)&output, flags);
}
int json_dump_file(const json_t *json, const char *path, size_t flags)
{
int json_dump_file(const json_t *json, const char *path, size_t flags) {
int result;
FILE *output = fopen(path, "w");
@ -475,8 +435,8 @@ int json_dump_file(const json_t *json, const char *path, size_t flags)
return result;
}
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
{
int json_dump_callback(const json_t *json, json_dump_callback_t callback,
void *data, size_t flags) {
int res;
hashtable_t parents_set;

View File

@ -1,10 +1,8 @@
#include <string.h>
#include "jansson_private.h"
#include <string.h>
void jsonp_error_init(json_error_t *error, const char *source)
{
if(error)
{
void jsonp_error_init(json_error_t *error, const char *source) {
if (error) {
error->text[0] = '\0';
error->line = -1;
error->column = -1;
@ -16,8 +14,7 @@ void jsonp_error_init(json_error_t *error, const char *source)
}
}
void jsonp_error_set_source(json_error_t *error, const char *source)
{
void jsonp_error_set_source(json_error_t *error, const char *source) {
size_t length;
if (!error || !source)
@ -33,10 +30,8 @@ void jsonp_error_set_source(json_error_t *error, const char *source)
}
}
void jsonp_error_set(json_error_t *error, int line, int column,
size_t position, enum json_error_code code,
const char *msg, ...)
{
void jsonp_error_set(json_error_t *error, int line, int column, size_t position,
enum json_error_code code, const char *msg, ...) {
va_list ap;
va_start(ap, msg);
@ -46,8 +41,7 @@ void jsonp_error_set(json_error_t *error, int line, int column,
void jsonp_error_vset(json_error_t *error, int line, int column,
size_t position, enum json_error_code code,
const char *msg, va_list ap)
{
const char *msg, va_list ap) {
if (!error)
return;

View File

@ -16,9 +16,9 @@
#include <stdint.h>
#endif
#include <jansson_config.h> /* for JSON_INLINE */
#include "jansson_private.h" /* for container_of() */
#include "hashtable.h"
#include "jansson_private.h" /* for container_of() */
#include <jansson_config.h> /* for JSON_INLINE */
#ifndef INITIAL_HASHTABLE_ORDER
#define INITIAL_HASHTABLE_ORDER 3
@ -37,49 +37,41 @@ extern volatile uint32_t hashtable_seed;
#define ordered_list_to_pair(list_) container_of(list_, pair_t, ordered_list)
#define hash_str(key) ((size_t)hashlittle((key), strlen(key), hashtable_seed))
static JSON_INLINE void list_init(list_t *list)
{
static JSON_INLINE void list_init(list_t *list) {
list->next = list;
list->prev = list;
}
static JSON_INLINE void list_insert(list_t *list, list_t *node)
{
static JSON_INLINE void list_insert(list_t *list, list_t *node) {
node->next = list;
node->prev = list->prev;
list->prev->next = node;
list->prev = node;
}
static JSON_INLINE void list_remove(list_t *list)
{
static JSON_INLINE void list_remove(list_t *list) {
list->prev->next = list->next;
list->next->prev = list->prev;
}
static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
{
static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable,
bucket_t *bucket) {
return bucket->first == &hashtable->list && bucket->first == bucket->last;
}
static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
list_t *list)
{
if(bucket_is_empty(hashtable, bucket))
{
list_t *list) {
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;
}
}
static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
const char *key, size_t hash)
{
const char *key, size_t hash) {
list_t *list;
pair_t *pair;
@ -87,8 +79,7 @@ static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
return NULL;
list = bucket->first;
while(1)
{
while (1) {
pair = list_to_pair(list);
if (pair->hash == hash && strcmp(pair->key, key) == 0)
return pair;
@ -103,9 +94,8 @@ static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
}
/* returns 0 on success, -1 if key was not found */
static int hashtable_do_del(hashtable_t *hashtable,
const char *key, size_t hash)
{
static int hashtable_do_del(hashtable_t *hashtable, const char *key,
size_t hash) {
pair_t *pair;
bucket_t *bucket;
size_t index;
@ -136,13 +126,11 @@ static int hashtable_do_del(hashtable_t *hashtable,
return 0;
}
static void hashtable_do_clear(hashtable_t *hashtable)
{
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);
@ -150,8 +138,7 @@ static void hashtable_do_clear(hashtable_t *hashtable)
}
}
static int hashtable_do_rehash(hashtable_t *hashtable)
{
static int hashtable_do_rehash(hashtable_t *hashtable) {
list_t *list, *next;
pair_t *pair;
size_t i, index, new_size, new_order;
@ -168,8 +155,7 @@ static int hashtable_do_rehash(hashtable_t *hashtable)
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;
}
@ -187,22 +173,20 @@ static int hashtable_do_rehash(hashtable_t *hashtable)
return 0;
}
int hashtable_init(hashtable_t *hashtable)
{
int hashtable_init(hashtable_t *hashtable) {
size_t i;
hashtable->size = 0;
hashtable->order = INITIAL_HASHTABLE_ORDER;
hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t));
hashtable->buckets =
jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t));
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;
}
@ -210,14 +194,12 @@ int hashtable_init(hashtable_t *hashtable)
return 0;
}
void hashtable_close(hashtable_t *hashtable)
{
void hashtable_close(hashtable_t *hashtable) {
hashtable_do_clear(hashtable);
jsonp_free(hashtable->buckets);
}
int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value)
{
int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value) {
pair_t *pair;
bucket_t *bucket;
size_t hash, index;
@ -232,13 +214,10 @@ 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. */
@ -267,8 +246,7 @@ int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value)
return 0;
}
void *hashtable_get(hashtable_t *hashtable, const char *key)
{
void *hashtable_get(hashtable_t *hashtable, const char *key) {
pair_t *pair;
size_t hash;
bucket_t *bucket;
@ -283,20 +261,17 @@ void *hashtable_get(hashtable_t *hashtable, const char *key)
return pair->value;
}
int hashtable_del(hashtable_t *hashtable, const char *key)
{
int hashtable_del(hashtable_t *hashtable, const char *key) {
size_t hash = hash_str(key);
return hashtable_do_del(hashtable, key, hash);
}
void hashtable_clear(hashtable_t *hashtable)
{
void hashtable_clear(hashtable_t *hashtable) {
size_t i;
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;
}
@ -306,13 +281,11 @@ void hashtable_clear(hashtable_t *hashtable)
hashtable->size = 0;
}
void *hashtable_iter(hashtable_t *hashtable)
{
void *hashtable_iter(hashtable_t *hashtable) {
return hashtable_iter_next(hashtable, &hashtable->ordered_list);
}
void *hashtable_iter_at(hashtable_t *hashtable, const char *key)
{
void *hashtable_iter_at(hashtable_t *hashtable, const char *key) {
pair_t *pair;
size_t hash;
bucket_t *bucket;
@ -327,28 +300,24 @@ void *hashtable_iter_at(hashtable_t *hashtable, const char *key)
return &pair->ordered_list;
}
void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
{
void *hashtable_iter_next(hashtable_t *hashtable, void *iter) {
list_t *list = (list_t *)iter;
if (list->next == &hashtable->ordered_list)
return NULL;
return list->next;
}
void *hashtable_iter_key(void *iter)
{
void *hashtable_iter_key(void *iter) {
pair_t *pair = ordered_list_to_pair((list_t *)iter);
return pair->key;
}
void *hashtable_iter_value(void *iter)
{
void *hashtable_iter_value(void *iter) {
pair_t *pair = ordered_list_to_pair((list_t *)iter);
return pair->value;
}
void hashtable_iter_set(void *iter, json_t *value)
{
void hashtable_iter_set(void *iter, json_t *value) {
pair_t *pair = ordered_list_to_pair((list_t *)iter);
json_decref(pair->value);

View File

@ -8,8 +8,8 @@
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <stdlib.h>
#include "jansson.h"
#include <stdlib.h>
struct hashtable_list {
struct hashtable_list *prev;
@ -40,11 +40,9 @@ typedef struct hashtable {
struct hashtable_list ordered_list;
} hashtable_t;
#define hashtable_key_to_iter(key_) \
(&(container_of(key_, struct hashtable_pair, key)->ordered_list))
/**
* hashtable_init - Initialize a hashtable object
*

View File

@ -44,7 +44,6 @@
#include "jansson.h"
static uint32_t buf_to_uint32(char *data) {
size_t i;
uint32_t result = 0;
@ -55,8 +54,6 @@ static uint32_t buf_to_uint32(char *data) {
return result;
}
/* /dev/urandom */
#if !defined(_WIN32) && defined(USE_URANDOM)
static int seed_from_urandom(uint32_t *seed) {
@ -97,12 +94,15 @@ static int seed_from_urandom(uint32_t *seed) {
#if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI)
#include <wincrypt.h>
typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv, LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags);
typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer);
typedef BOOL(WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,
LPCSTR pszContainer,
LPCSTR pszProvider, DWORD dwProvType,
DWORD dwFlags);
typedef BOOL(WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,
BYTE *pbBuffer);
typedef BOOL(WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV hProv, DWORD dwFlags);
static int seed_from_windows_cryptoapi(uint32_t *seed)
{
static int seed_from_windows_cryptoapi(uint32_t *seed) {
HINSTANCE hAdvAPI32 = NULL;
CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
CRYPTGENRANDOM pCryptGenRandom = NULL;
@ -115,19 +115,23 @@ static int seed_from_windows_cryptoapi(uint32_t *seed)
if (hAdvAPI32 == NULL)
return 1;
pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA");
pCryptAcquireContext =
(CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA");
if (!pCryptAcquireContext)
return 1;
pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, "CryptGenRandom");
pCryptGenRandom =
(CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, "CryptGenRandom");
if (!pCryptGenRandom)
return 1;
pCryptReleaseContext = (CRYPTRELEASECONTEXT)GetProcAddress(hAdvAPI32, "CryptReleaseContext");
pCryptReleaseContext =
(CRYPTRELEASECONTEXT)GetProcAddress(hAdvAPI32, "CryptReleaseContext");
if (!pCryptReleaseContext)
return 1;
if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
return 1;
ok = pCryptGenRandom(hCryptProv, sizeof(uint32_t), data);
@ -190,10 +194,10 @@ static uint32_t generate_seed() {
return seed;
}
volatile uint32_t hashtable_seed = 0;
#if defined(HAVE_ATOMIC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32))
#if defined(HAVE_ATOMIC_BUILTINS) && \
(defined(HAVE_SCHED_YIELD) || !defined(_WIN32))
static volatile char seed_initialized = 0;
void json_object_seed(size_t seed) {
@ -216,7 +220,8 @@ void json_object_seed(size_t seed) {
}
}
}
#elif defined(HAVE_SYNC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32))
#elif defined(HAVE_SYNC_BUILTINS) && \
(defined(HAVE_SCHED_YIELD) || !defined(_WIN32))
void json_object_seed(size_t seed) {
uint32_t new_seed = (uint32_t)seed;

View File

@ -8,9 +8,9 @@
#ifndef JANSSON_H
#define JANSSON_H
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> /* for size_t */
#include <stdarg.h>
#include "jansson_config.h"
@ -29,8 +29,8 @@ extern "C" {
/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \
(JANSSON_MINOR_VERSION << 8) | \
#define JANSSON_VERSION_HEX \
((JANSSON_MAJOR_VERSION << 16) | (JANSSON_MINOR_VERSION << 8) | \
(JANSSON_MICRO_VERSION << 0))
/* If __atomic or __sync builtins are available the library is thread
@ -107,8 +107,10 @@ json_t *json_null(void);
/* do not call JSON_INTERNAL_INCREF or JSON_INTERNAL_DECREF directly */
#if JSON_HAVE_ATOMIC_BUILTINS
#define JSON_INTERNAL_INCREF(json) __atomic_add_fetch(&json->refcount, 1, __ATOMIC_ACQUIRE)
#define JSON_INTERNAL_DECREF(json) __atomic_sub_fetch(&json->refcount, 1, __ATOMIC_RELEASE)
#define JSON_INTERNAL_INCREF(json) \
__atomic_add_fetch(&json->refcount, 1, __ATOMIC_ACQUIRE)
#define JSON_INTERNAL_DECREF(json) \
__atomic_sub_fetch(&json->refcount, 1, __ATOMIC_RELEASE)
#elif JSON_HAVE_SYNC_BUILTINS
#define JSON_INTERNAL_INCREF(json) __sync_add_and_fetch(&json->refcount, 1)
#define JSON_INTERNAL_DECREF(json) __sync_sub_and_fetch(&json->refcount, 1)
@ -117,9 +119,7 @@ json_t *json_null(void);
#define JSON_INTERNAL_DECREF(json) (--json->refcount)
#endif
static JSON_INLINE
json_t *json_incref(json_t *json)
{
static JSON_INLINE json_t *json_incref(json_t *json) {
if (json && json->refcount != (size_t)-1)
JSON_INTERNAL_INCREF(json);
return json;
@ -128,17 +128,13 @@ json_t *json_incref(json_t *json)
/* do not call json_delete directly */
void json_delete(json_t *json);
static JSON_INLINE
void json_decref(json_t *json)
{
static JSON_INLINE void json_decref(json_t *json) {
if (json && json->refcount != (size_t)-1 && JSON_INTERNAL_DECREF(json) == 0)
json_delete(json);
}
#if defined(__GNUC__) || defined(__clang__)
static JSON_INLINE
void json_decrefp(json_t **json)
{
static JSON_INLINE void json_decrefp(json_t **json) {
if (json) {
json_decref(*json);
*json = NULL;
@ -148,7 +144,6 @@ void json_decrefp(json_t **json)
#define json_auto_t json_t __attribute__((cleanup(json_decrefp)))
#endif
/* error reporting */
#define JSON_ERROR_TEXT_LENGTH 160
@ -191,7 +186,8 @@ static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) {
void json_object_seed(size_t seed);
size_t json_object_size(const json_t *object);
json_t *json_object_get(const json_t *object, const char *key) JANSSON_ATTRS((warn_unused_result));
json_t *json_object_get(const json_t *object, const char *key)
JANSSON_ATTRS((warn_unused_result));
int json_object_set_new(json_t *object, const char *key, json_t *value);
int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
int json_object_del(json_t *object, const char *key);
@ -210,65 +206,60 @@ int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
#define json_object_foreach(object, key, value) \
for (key = json_object_iter_key(json_object_iter(object)); \
key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
key = json_object_iter_key(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( \
json_object_iter_next(object, json_object_key_to_iter(key))))
#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)); \
key && (value = json_object_iter_value(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)))
#define json_array_foreach(array, index, value) \
for(index = 0; \
index < json_array_size(array) && (value = json_array_get(array, index)); \
for (index = 0; index < json_array_size(array) && \
(value = json_array_get(array, index)); \
index++)
static JSON_INLINE
int json_object_set(json_t *object, const char *key, json_t *value)
{
static JSON_INLINE int json_object_set(json_t *object, const char *key,
json_t *value) {
return json_object_set_new(object, key, json_incref(value));
}
static JSON_INLINE
int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
{
static JSON_INLINE int json_object_set_nocheck(json_t *object, const char *key,
json_t *value) {
return json_object_set_new_nocheck(object, key, json_incref(value));
}
static JSON_INLINE
int json_object_iter_set(json_t *object, void *iter, json_t *value)
{
static JSON_INLINE int json_object_iter_set(json_t *object, void *iter,
json_t *value) {
return json_object_iter_set_new(object, iter, json_incref(value));
}
static JSON_INLINE
int json_object_update_new(json_t *object, json_t *other)
{
static JSON_INLINE int json_object_update_new(json_t *object, json_t *other) {
int ret = json_object_update(object, other);
json_decref(other);
return ret;
}
static JSON_INLINE
int json_object_update_existing_new(json_t *object, json_t *other)
{
static JSON_INLINE int json_object_update_existing_new(json_t *object, json_t *other) {
int ret = json_object_update_existing(object, other);
json_decref(other);
return ret;
}
static JSON_INLINE
int json_object_update_missing_new(json_t *object, json_t *other)
{
static JSON_INLINE int json_object_update_missing_new(json_t *object, json_t *other) {
int ret = json_object_update_missing(object, other);
json_decref(other);
return ret;
}
size_t json_array_size(const json_t *array);
json_t *json_array_get(const json_t *array, size_t index) JANSSON_ATTRS((warn_unused_result));
json_t *json_array_get(const json_t *array, size_t index)
JANSSON_ATTRS((warn_unused_result));
int json_array_set_new(json_t *array, size_t index, json_t *value);
int json_array_append_new(json_t *array, json_t *value);
int json_array_insert_new(json_t *array, size_t index, json_t *value);
@ -276,21 +267,17 @@ int json_array_remove(json_t *array, size_t index);
int json_array_clear(json_t *array);
int json_array_extend(json_t *array, json_t *other);
static JSON_INLINE
int json_array_set(json_t *array, size_t ind, json_t *value)
{
static JSON_INLINE int json_array_set(json_t *array, size_t ind,
json_t *value) {
return json_array_set_new(array, ind, json_incref(value));
}
static JSON_INLINE
int json_array_append(json_t *array, json_t *value)
{
static JSON_INLINE int json_array_append(json_t *array, json_t *value) {
return json_array_append_new(array, json_incref(value));
}
static JSON_INLINE
int json_array_insert(json_t *array, size_t ind, json_t *value)
{
static JSON_INLINE int json_array_insert(json_t *array, size_t ind,
json_t *value) {
return json_array_insert_new(array, ind, json_incref(value));
}
@ -310,33 +297,36 @@ int json_real_set(json_t *real, double value);
/* pack, unpack */
json_t *json_pack(const char *fmt, ...) JANSSON_ATTRS((warn_unused_result));
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...) JANSSON_ATTRS((warn_unused_result));
json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap) JANSSON_ATTRS((warn_unused_result));
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
JANSSON_ATTRS((warn_unused_result));
json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt,
va_list ap) JANSSON_ATTRS((warn_unused_result));
#define JSON_VALIDATE_ONLY 0x1
#define JSON_STRICT 0x2
int json_unpack(json_t *root, const char *fmt, ...);
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags,
const char *fmt, ...);
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
const char *fmt, va_list ap);
/* sprintf */
json_t *json_sprintf(const char *fmt, ...) JANSSON_ATTRS((warn_unused_result, format(printf, 1, 2)));
json_t *json_vsprintf(const char *fmt, va_list ap) JANSSON_ATTRS((warn_unused_result, format(printf, 1, 0)));
json_t *json_sprintf(const char *fmt, ...)
JANSSON_ATTRS((warn_unused_result, format(printf, 1, 2)));
json_t *json_vsprintf(const char *fmt, va_list ap)
JANSSON_ATTRS((warn_unused_result, format(printf, 1, 0)));
/* equality */
int json_equal(const json_t *value1, const json_t *value2);
/* copying */
json_t *json_copy(json_t *value) JANSSON_ATTRS((warn_unused_result));
json_t *json_deep_copy(const json_t *value) JANSSON_ATTRS((warn_unused_result));
/* decoding */
#define JSON_REJECT_DUPLICATES 0x1
@ -347,13 +337,19 @@ json_t *json_deep_copy(const json_t *value) JANSSON_ATTRS((warn_unused_result));
typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
json_t *json_loads(const char *input, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_loadfd(int input, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_load_file(const char *path, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_loads(const char *input, size_t flags, json_error_t *error)
JANSSON_ATTRS((warn_unused_result));
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags,
json_error_t *error) JANSSON_ATTRS((warn_unused_result));
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
JANSSON_ATTRS((warn_unused_result));
json_t *json_loadfd(int input, size_t flags, json_error_t *error)
JANSSON_ATTRS((warn_unused_result));
json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
JANSSON_ATTRS((warn_unused_result));
json_t *json_load_callback(json_load_callback_t callback, void *data,
size_t flags, json_error_t *error)
JANSSON_ATTRS((warn_unused_result));
/* encoding */
@ -368,14 +364,17 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla
#define JSON_REAL_PRECISION(n) (((n)&0x1F) << 11)
#define JSON_EMBED 0x10000
typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
typedef int (*json_dump_callback_t)(const char *buffer, size_t size,
void *data);
char *json_dumps(const json_t *json, size_t flags) JANSSON_ATTRS((warn_unused_result));
char *json_dumps(const json_t *json, size_t flags)
JANSSON_ATTRS((warn_unused_result));
size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags);
int json_dumpf(const json_t *json, FILE *output, size_t flags);
int json_dumpfd(const json_t *json, int output, size_t flags);
int json_dump_file(const json_t *json, const char *path, size_t flags);
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
int json_dump_callback(const json_t *json, json_dump_callback_t callback,
void *data, size_t flags);
/* custom memory allocation */

View File

@ -8,11 +8,11 @@
#ifndef JANSSON_PRIVATE_H
#define JANSSON_PRIVATE_H
#include "jansson_private_config.h"
#include <stddef.h>
#include "jansson.h"
#include "hashtable.h"
#include "jansson.h"
#include "jansson_private_config.h"
#include "strbuffer.h"
#include <stddef.h>
#define container_of(ptr_, type_, member_) \
((type_ *)((char *)ptr_ - offsetof(type_, member_)))
@ -72,9 +72,8 @@ json_t *jsonp_stringn_nocheck_own(const char *value, size_t len);
/* Error message formatting */
void jsonp_error_init(json_error_t *error, const char *source);
void jsonp_error_set_source(json_error_t *error, const char *source);
void jsonp_error_set(json_error_t *error, int line, int column,
size_t position, enum json_error_code code,
const char *msg, ...);
void jsonp_error_set(json_error_t *error, int line, int column, size_t position,
enum json_error_code code, const char *msg, ...);
void jsonp_error_vset(json_error_t *error, int line, int column,
size_t position, enum json_error_code code,
const char *msg, va_list ap);
@ -86,23 +85,27 @@ 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_free(void *ptr);
char *jsonp_strndup(const char *str, size_t length) JANSSON_ATTRS((warn_unused_result));
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));
char *jsonp_strndup(const char *str, size_t len) JANSSON_ATTRS((warn_unused_result));
char *jsonp_strndup(const char *str, size_t len)
JANSSON_ATTRS((warn_unused_result));
/* Circular reference check*/
/* Space for "0x", double the sizeof a pointer for the hex and a terminator. */
#define LOOP_KEY_LEN (2 + (sizeof(json_t *) * 2) + 1)
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size);
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key,
size_t key_size);
/* Windows compatibility */
#if defined(_WIN32) || defined(WIN32)
#if defined(_MSC_VER) /* MS compiller */
# if (_MSC_VER < 1900) && !defined(snprintf) /* snprintf not defined yet & not introduced */
#if (_MSC_VER < 1900) && \
!defined(snprintf) /* snprintf not defined yet & not introduced */
#define snprintf _snprintf
#endif
# if (_MSC_VER < 1500) && !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */
#if (_MSC_VER < 1500) && \
!defined(vsnprintf) /* vsnprintf not defined yet & not introduced */
#define vsnprintf(b, c, f, a) _vsnprintf(b, c, f, a)
#endif
#else /* Other Windows compiller, old definition */

View File

@ -11,12 +11,12 @@
#include "jansson_private.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -80,13 +80,10 @@ typedef struct {
#define stream_to_lex(stream) container_of(stream, lex_t, stream)
/*** error reporting ***/
static void error_set(json_error_t *error, const lex_t *lex,
enum json_error_code code,
const char *msg, ...)
{
enum json_error_code code, const char *msg, ...) {
va_list ap;
char msg_text[JSON_ERROR_TEXT_LENGTH];
char msg_with_context[JSON_ERROR_TEXT_LENGTH];
@ -103,25 +100,21 @@ 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 (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
{
} else {
if (code == json_error_invalid_syntax) {
/* More specific error code for premature end of file. */
code = json_error_premature_end_of_input;
@ -129,8 +122,7 @@ static void error_set(json_error_t *error, const lex_t *lex,
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';
@ -142,12 +134,9 @@ static void error_set(json_error_t *error, const lex_t *lex,
jsonp_error_set(error, line, col, pos, code, "%s", result);
}
/*** lexical analyzer ***/
static void
stream_init(stream_t *stream, get_func get, void *data)
{
static void stream_init(stream_t *stream, get_func get, void *data) {
stream->get = get;
stream->data = data;
stream->buffer[0] = '\0';
@ -159,15 +148,13 @@ stream_init(stream_t *stream, get_func get, void *data)
stream->position = 0;
}
static int stream_get(stream_t *stream, json_error_t *error)
{
static int stream_get(stream_t *stream, json_error_t *error) {
int c;
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) {
stream->state = STREAM_STATE_EOF;
@ -177,8 +164,7 @@ 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;
@ -195,8 +181,7 @@ static int stream_get(stream_t *stream, json_error_t *error)
goto out;
stream->buffer[count] = '\0';
}
else
} else
stream->buffer[1] = '\0';
}
@ -207,8 +192,7 @@ static int stream_get(stream_t *stream, json_error_t *error)
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++;
@ -218,12 +202,12 @@ static int stream_get(stream_t *stream, json_error_t *error)
out:
stream->state = STREAM_STATE_ERROR;
error_set(error, stream_to_lex(stream), json_error_invalid_utf8, "unable to decode byte 0x%x", c);
error_set(error, stream_to_lex(stream), json_error_invalid_utf8,
"unable to decode byte 0x%x", c);
return STREAM_STATE_ERROR;
}
static void stream_unget(stream_t *stream, int c)
{
static void stream_unget(stream_t *stream, int c) {
if (c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR)
return;
@ -231,8 +215,7 @@ static void stream_unget(stream_t *stream, int c)
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);
@ -240,32 +223,24 @@ static void stream_unget(stream_t *stream, int c)
assert(stream->buffer[stream->buffer_pos] == c);
}
static int lex_get(lex_t *lex, json_error_t *error)
{
static int lex_get(lex_t *lex, json_error_t *error) {
return stream_get(&lex->stream, error);
}
static void lex_save(lex_t *lex, int c)
{
static void lex_save(lex_t *lex, int c) {
strbuffer_append_byte(&lex->saved_text, c);
}
static int lex_get_save(lex_t *lex, json_error_t *error)
{
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)
lex_save(lex, c);
return c;
}
static void lex_unget(lex_t *lex, int c)
{
stream_unget(&lex->stream, c);
}
static void lex_unget(lex_t *lex, int c) { stream_unget(&lex->stream, c); }
static void lex_unget_unsave(lex_t *lex, int c)
{
static void lex_unget_unsave(lex_t *lex, int c) {
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
@ -283,26 +258,22 @@ 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')
{
static void lex_save_cached(lex_t *lex) {
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++;
}
}
static void lex_free_string(lex_t *lex)
{
static void lex_free_string(lex_t *lex) {
jsonp_free(lex->value.string.val);
lex->value.string.val = NULL;
lex->value.string.len = 0;
}
/* assumes that str points to 'u' plus at least 4 valid hex digits */
static int32_t decode_unicode_escape(const char *str)
{
static int32_t decode_unicode_escape(const char *str) {
int i;
int32_t value = 0;
@ -324,8 +295,7 @@ static int32_t decode_unicode_escape(const char *str)
return value;
}
static void lex_scan_string(lex_t *lex, json_error_t *error)
{
static void lex_scan_string(lex_t *lex, json_error_t *error) {
int c;
const char *p;
char *t;
@ -341,7 +311,8 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
goto out;
else if (c == STREAM_STATE_EOF) {
error_set(error, lex, json_error_premature_end_of_input, "premature end of input");
error_set(error, lex, json_error_premature_end_of_input,
"premature end of input");
goto out;
}
@ -349,9 +320,11 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
/* control character */
lex_unget_unsave(lex, c);
if (c == '\n')
error_set(error, lex, json_error_invalid_syntax, "unexpected newline");
error_set(error, lex, json_error_invalid_syntax,
"unexpected newline");
else
error_set(error, lex, json_error_invalid_syntax, "control character 0x%x", c);
error_set(error, lex, json_error_invalid_syntax,
"control character 0x%x", c);
goto out;
}
@ -361,21 +334,21 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
c = lex_get_save(lex, error);
for (i = 0; i < 4; i++) {
if (!l_isxdigit(c)) {
error_set(error, lex, json_error_invalid_syntax, "invalid escape");
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' ||
} 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");
error_set(error, lex, json_error_invalid_syntax,
"invalid escape");
goto out;
}
}
else
} else
c = lex_get_save(lex, error);
}
@ -405,7 +378,8 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
value = decode_unicode_escape(p);
if (value < 0) {
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode escape '%.6s'", p - 1);
error_set(error, lex, json_error_invalid_syntax,
"invalid Unicode escape '%.6s'", p - 1);
goto out;
}
p += 5;
@ -415,47 +389,43 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
if (*p == '\\' && *(p + 1) == 'u') {
int32_t value2 = decode_unicode_escape(++p);
if (value2 < 0) {
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode escape '%.6s'", p - 1);
error_set(error, lex, json_error_invalid_syntax,
"invalid Unicode escape '%.6s'", p - 1);
goto out;
}
p += 5;
if (0xDC00 <= value2 && value2 <= 0xDFFF) {
/* valid second surrogate */
value =
((value - 0xD800) << 10) +
(value2 - 0xDC00) +
0x10000;
}
else {
value = ((value - 0xD800) << 10) +
(value2 - 0xDC00) + 0x10000;
} else {
/* invalid second surrogate */
error_set(error, lex,
json_error_invalid_syntax,
"invalid Unicode '\\u%04X\\u%04X'",
value, value2);
error_set(error, lex, json_error_invalid_syntax,
"invalid Unicode '\\u%04X\\u%04X'", value,
value2);
goto out;
}
}
else {
} else {
/* no second surrogate */
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode '\\u%04X'",
value);
error_set(error, lex, json_error_invalid_syntax,
"invalid Unicode '\\u%04X'", value);
goto out;
}
}
else if(0xDC00 <= value && value <= 0xDFFF) {
error_set(error, lex, json_error_invalid_syntax, "invalid Unicode '\\u%04X'", value);
} 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))
assert(0);
t += length;
}
else {
} else {
switch (*p) {
case '"': case '\\': case '/':
*t = *p; break;
case '"':
case '\\':
case '/': *t = *p; break;
case 'b': *t = '\b'; break;
case 'f': *t = '\f'; break;
case 'n': *t = '\n'; break;
@ -466,8 +436,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
t++;
p++;
}
}
else
} else
*(t++) = *(p++);
}
*t = '\0';
@ -491,8 +460,7 @@ out:
#endif
#endif
static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
{
static int lex_scan_number(lex_t *lex, int c, json_error_t *error) {
const char *saved_text;
char *end;
double doubleval;
@ -508,20 +476,17 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
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 {
} 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);
@ -532,9 +497,11 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
intval = json_strtoint(saved_text, &end, 10);
if (errno == ERANGE) {
if (intval < 0)
error_set(error, lex, json_error_numeric_overflow, "too big negative integer");
error_set(error, lex, json_error_numeric_overflow,
"too big negative integer");
else
error_set(error, lex, json_error_numeric_overflow, "too big integer");
error_set(error, lex, json_error_numeric_overflow,
"too big integer");
goto out;
}
@ -576,7 +543,8 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
lex_unget_unsave(lex, c);
if (jsonp_strtod(&lex->saved_text, &doubleval)) {
error_set(error, lex, json_error_numeric_overflow, "real number overflow");
error_set(error, lex, json_error_numeric_overflow,
"real number overflow");
goto out;
}
@ -588,8 +556,7 @@ out:
return -1;
}
static int lex_scan(lex_t *lex, json_error_t *error)
{
static int lex_scan(lex_t *lex, json_error_t *error) {
int c;
strbuffer_clear(&lex->saved_text);
@ -656,8 +623,7 @@ out:
return lex->token;
}
static char *lex_steal_string(lex_t *lex, size_t *out_len)
{
static char *lex_steal_string(lex_t *lex, size_t *out_len) {
char *result = NULL;
if (lex->token == TOKEN_STRING) {
result = lex->value.string.val;
@ -668,8 +634,7 @@ static char *lex_steal_string(lex_t *lex, size_t *out_len)
return result;
}
static int lex_init(lex_t *lex, get_func get, size_t flags, void *data)
{
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))
return -1;
@ -679,20 +644,17 @@ static int lex_init(lex_t *lex, get_func get, size_t flags, void *data)
return 0;
}
static void lex_close(lex_t *lex)
{
static void lex_close(lex_t *lex) {
if (lex->token == TOKEN_STRING)
lex_free_string(lex);
strbuffer_close(&lex->saved_text);
}
/*** parser ***/
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)
{
static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error) {
json_t *object = json_object();
if (!object)
return NULL;
@ -707,7 +669,8 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
json_t *value;
if (lex->token != TOKEN_STRING) {
error_set(error, lex, json_error_invalid_syntax, "string or '}' expected");
error_set(error, lex, json_error_invalid_syntax,
"string or '}' expected");
goto error;
}
@ -716,14 +679,16 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
return NULL;
if (memchr(key, '\0', len)) {
jsonp_free(key);
error_set(error, lex, json_error_null_byte_in_key, "NUL byte in object key not supported");
error_set(error, lex, json_error_null_byte_in_key,
"NUL byte in object key not supported");
goto error;
}
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");
error_set(error, lex, json_error_duplicate_key,
"duplicate object key");
goto error;
}
}
@ -768,8 +733,7 @@ error:
return NULL;
}
static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error)
{
static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error) {
json_t *array = json_array();
if (!array)
return NULL;
@ -806,13 +770,13 @@ error:
return NULL;
}
static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
{
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) {
error_set(error, lex, json_error_stack_overflow, "maximum parsing depth reached");
error_set(error, lex, json_error_stack_overflow,
"maximum parsing depth reached");
return NULL;
}
@ -823,7 +787,8 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
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");
error_set(error, lex, json_error_null_character,
"\\u0000 is not allowed without JSON_ALLOW_NUL");
return NULL;
}
}
@ -844,32 +809,23 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
break;
}
case TOKEN_TRUE:
json = json_true();
break;
case TOKEN_TRUE: json = json_true(); break;
case TOKEN_FALSE:
json = json_false();
break;
case TOKEN_FALSE: json = json_false(); break;
case TOKEN_NULL:
json = json_null();
break;
case TOKEN_NULL: json = json_null(); break;
case '{':
json = parse_object(lex, flags, error);
break;
case '{': json = parse_object(lex, flags, error); break;
case '[':
json = parse_array(lex, flags, error);
break;
case '[': json = parse_array(lex, flags, error); break;
case TOKEN_INVALID:
error_set(error, lex, json_error_invalid_syntax, "invalid token");
return NULL;
default:
error_set(error, lex, json_error_invalid_syntax, "unexpected token");
error_set(error, lex, json_error_invalid_syntax,
"unexpected token");
return NULL;
}
@ -880,8 +836,7 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
return json;
}
static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
{
static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error) {
json_t *result;
lex->depth = 0;
@ -889,7 +844,8 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
lex_scan(lex, error);
if (!(flags & JSON_DECODE_ANY)) {
if (lex->token != '[' && lex->token != '{') {
error_set(error, lex, json_error_invalid_syntax, "'[' or '{' expected");
error_set(error, lex, json_error_invalid_syntax,
"'[' or '{' expected");
return NULL;
}
}
@ -901,7 +857,8 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
if (!(flags & JSON_DISABLE_EOF_CHECK)) {
lex_scan(lex, error);
if (lex->token != TOKEN_EOF) {
error_set(error, lex, json_error_end_of_input_expected, "end of file expected");
error_set(error, lex, json_error_end_of_input_expected,
"end of file expected");
json_decref(result);
return NULL;
}
@ -915,28 +872,24 @@ 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;
static int string_get(void *data)
{
static int string_get(void *data) {
char c;
string_data_t *stream = (string_data_t *)data;
c = stream->data[stream->pos];
if (c == '\0')
return EOF;
else
{
else {
stream->pos++;
return (unsigned char)c;
}
}
json_t *json_loads(const char *string, size_t flags, json_error_t *error)
{
json_t *json_loads(const char *string, size_t flags, json_error_t *error) {
lex_t lex;
json_t *result;
string_data_t stream_data;
@ -960,15 +913,13 @@ 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;
} buffer_data_t;
static int buffer_get(void *data)
{
static int buffer_get(void *data) {
char c;
buffer_data_t *stream = data;
if (stream->pos >= stream->len)
@ -979,8 +930,8 @@ static int buffer_get(void *data)
return (unsigned char)c;
}
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error)
{
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags,
json_error_t *error) {
lex_t lex;
json_t *result;
buffer_data_t stream_data;
@ -1005,8 +956,7 @@ json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t
return result;
}
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) {
lex_t lex;
const char *source;
json_t *result;
@ -1032,8 +982,7 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
return result;
}
static int fd_get_func(int *fd)
{
static int fd_get_func(int *fd) {
#ifdef HAVE_UNISTD_H
uint8_t c;
if (read(*fd, &c, 1) == 1)
@ -1042,8 +991,7 @@ static int fd_get_func(int *fd)
return EOF;
}
json_t *json_loadfd(int input, size_t flags, json_error_t *error)
{
json_t *json_loadfd(int input, size_t flags, json_error_t *error) {
lex_t lex;
const char *source;
json_t *result;
@ -1071,8 +1019,7 @@ json_t *json_loadfd(int input, size_t flags, json_error_t *error)
return result;
}
json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
{
json_t *json_load_file(const char *path, size_t flags, json_error_t *error) {
json_t *result;
FILE *fp;
@ -1084,10 +1031,9 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
}
fp = fopen(path, "rb");
if(!fp)
{
error_set(error, NULL, json_error_cannot_open_file, "unable to open %s: %s",
path, strerror(errno));
if (!fp) {
error_set(error, NULL, json_error_cannot_open_file,
"unable to open %s: %s", path, strerror(errno));
return NULL;
}
@ -1099,8 +1045,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;
@ -1108,8 +1053,7 @@ typedef struct
void *arg;
} callback_data_t;
static int callback_get(void *data)
{
static int callback_get(void *data) {
char c;
callback_data_t *stream = data;
@ -1125,8 +1069,8 @@ static int callback_get(void *data)
return (unsigned char)c;
}
json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flags, json_error_t *error)
{
json_t *json_load_callback(json_load_callback_t callback, void *arg,
size_t flags, json_error_t *error) {
lex_t lex;
json_t *result;

View File

@ -1,3 +1,4 @@
// clang-format off
/*
-------------------------------------------------------------------------------
lookup3.c, by Bob Jenkins, May 2006, Public Domain.

View File

@ -20,29 +20,23 @@
static json_malloc_t do_malloc = malloc;
static json_free_t do_free = free;
void *jsonp_malloc(size_t size)
{
void *jsonp_malloc(size_t size) {
if (!size)
return NULL;
return (*do_malloc)(size);
}
void jsonp_free(void *ptr)
{
void jsonp_free(void *ptr) {
if (!ptr)
return;
(*do_free)(ptr);
}
char *jsonp_strdup(const char *str)
{
return jsonp_strndup(str, strlen(str));
}
char *jsonp_strdup(const char *str) { return jsonp_strndup(str, strlen(str)); }
char *jsonp_strndup(const char *str, size_t len)
{
char *jsonp_strndup(const char *str, size_t len) {
char *new_str;
new_str = jsonp_malloc(len + 1);
@ -54,14 +48,12 @@ char *jsonp_strndup(const char *str, size_t len)
return new_str;
}
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
{
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) {
do_malloc = malloc_fn;
do_free = free_fn;
}
void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn)
{
void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn) {
if (malloc_fn)
*malloc_fn = do_malloc;
if (free_fn)

View File

@ -6,10 +6,10 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include "jansson.h"
#include "jansson_private.h"
#include "utf.h"
#include <string.h>
typedef struct {
int line;
@ -34,24 +34,15 @@ typedef struct {
#define token(scanner) ((scanner)->token.token)
static const char * const type_names[] = {
"object",
"array",
"string",
"integer",
"real",
"true",
"false",
"null"
};
static const char *const type_names[] = {"object", "array", "string", "integer",
"real", "true", "false", "null"};
#define type_name(x) type_names[json_typeof(x)]
static const char unpack_value_starters[] = "{[siIbfFOon";
static void scanner_init(scanner_t *s, json_error_t *error,
size_t flags, const char *fmt)
{
static void scanner_init(scanner_t *s, json_error_t *error, size_t flags,
const char *fmt) {
s->error = error;
s->flags = flags;
s->fmt = s->start = fmt;
@ -64,8 +55,7 @@ static void scanner_init(scanner_t *s, json_error_t *error,
s->has_error = 0;
}
static void next_token(scanner_t *s)
{
static void next_token(scanner_t *s) {
const char *t;
s->prev_token = s->token;
@ -87,8 +77,7 @@ static void next_token(scanner_t *s)
if (*t == '\n') {
s->line++;
s->column = 1;
}
else
} else
s->column++;
s->pos++;
@ -100,19 +89,18 @@ static void next_token(scanner_t *s)
s->token.column = s->column;
s->token.pos = s->pos;
if (*t) t++;
if (*t)
t++;
s->fmt = t;
}
static void prev_token(scanner_t *s)
{
static void prev_token(scanner_t *s) {
s->next_token = s->token;
s->token = s->prev_token;
}
static void set_error(scanner_t *s, const char *source, enum json_error_code code,
const char *fmt, ...)
{
static void set_error(scanner_t *s, const char *source,
enum json_error_code code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
@ -126,12 +114,10 @@ static void set_error(scanner_t *s, const char *source, enum json_error_code cod
static json_t *pack(scanner_t *s, va_list *ap);
/* ours will be set to 1 if jsonp_free() must be called for the result
afterwards */
static char *read_string(scanner_t *s, va_list *ap,
const char *purpose, size_t *out_len, int *ours, int optional)
{
static char *read_string(scanner_t *s, va_list *ap, const char *purpose,
size_t *out_len, int *ours, int optional) {
char t;
strbuffer_t strbuff;
const char *str;
@ -148,7 +134,8 @@ static char *read_string(scanner_t *s, va_list *ap,
if (!str) {
if (!optional) {
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
set_error(s, "<args>", json_error_null_value, "NULL %s",
purpose);
s->has_error = 1;
}
return NULL;
@ -157,7 +144,8 @@ static char *read_string(scanner_t *s, va_list *ap,
length = strlen(str);
if (!utf8_check_string(str, length)) {
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s",
purpose);
s->has_error = 1;
return NULL;
}
@ -165,7 +153,8 @@ static char *read_string(scanner_t *s, va_list *ap,
*out_len = length;
return (char *)str;
} else if (optional) {
set_error(s, "<format>", json_error_invalid_format, "Cannot use '%c' on optional strings", t);
set_error(s, "<format>", json_error_invalid_format,
"Cannot use '%c' on optional strings", t);
s->has_error = 1;
return NULL;
@ -187,17 +176,17 @@ static char *read_string(scanner_t *s, va_list *ap,
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) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
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;
}
@ -214,7 +203,8 @@ static char *read_string(scanner_t *s, va_list *ap,
}
if (!utf8_check_string(strbuff.value, strbuff.length)) {
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s",
purpose);
strbuffer_close(&strbuff);
s->has_error = 1;
return NULL;
@ -225,8 +215,7 @@ static char *read_string(scanner_t *s, va_list *ap,
return strbuffer_steal_value(&strbuff);
}
static json_t *pack_object(scanner_t *s, va_list *ap)
{
static json_t *pack_object(scanner_t *s, va_list *ap) {
json_t *object = json_object();
next_token(s);
@ -238,12 +227,14 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
char valueOptional;
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
set_error(s, "<format>", json_error_invalid_format,
"Unexpected end of format string");
goto error;
}
if (token(s) != 's') {
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
set_error(s, "<format>", json_error_invalid_format,
"Expected format 's', got '%c'", token(s));
goto error;
}
@ -261,7 +252,8 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
jsonp_free(key);
if (valueOptional != '*') {
set_error(s, "<args>", json_error_null_value, "NULL object value");
set_error(s, "<args>", json_error_null_value,
"NULL object value");
s->has_error = 1;
}
@ -273,7 +265,8 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
json_decref(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);
set_error(s, "<internal>", json_error_out_of_memory,
"Unable to add key \"%s\"", key);
s->has_error = 1;
}
@ -291,8 +284,7 @@ error:
return NULL;
}
static json_t *pack_array(scanner_t *s, va_list *ap)
{
static json_t *pack_array(scanner_t *s, va_list *ap) {
json_t *array = json_array();
next_token(s);
@ -301,7 +293,8 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
char valueOptional;
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
set_error(s, "<format>", json_error_invalid_format,
"Unexpected end of format string");
/* Format string errors are unrecoverable. */
goto error;
}
@ -324,7 +317,8 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
json_decref(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");
set_error(s, "<internal>", json_error_out_of_memory,
"Unable to append to array");
s->has_error = 1;
}
@ -339,8 +333,7 @@ error:
return NULL;
}
static json_t *pack_string(scanner_t *s, va_list *ap)
{
static json_t *pack_string(scanner_t *s, va_list *ap) {
char *str;
char t;
size_t len;
@ -369,8 +362,7 @@ static json_t *pack_string(scanner_t *s, va_list *ap)
return json_stringn_nocheck(str, len);
}
static json_t *pack_object_inter(scanner_t *s, va_list *ap, int need_incref)
{
static json_t *pack_object_inter(scanner_t *s, va_list *ap, int need_incref) {
json_t *json;
char ntoken;
@ -386,12 +378,9 @@ static json_t *pack_object_inter(scanner_t *s, va_list *ap, int need_incref)
return need_incref ? json_incref(json) : json;
switch (ntoken) {
case '?':
return json_null();
case '*':
return NULL;
default:
break;
case '?': return json_null();
case '*': return NULL;
default: break;
}
set_error(s, "<args>", json_error_null_value, "NULL object");
@ -399,8 +388,7 @@ static json_t *pack_object_inter(scanner_t *s, va_list *ap, int need_incref)
return NULL;
}
static json_t *pack_integer(scanner_t *s, json_int_t value)
{
static json_t *pack_integer(scanner_t *s, json_int_t value) {
json_t *json = json_integer(value);
if (!json) {
@ -411,8 +399,7 @@ static json_t *pack_integer(scanner_t *s, json_int_t value)
return json;
}
static json_t *pack_real(scanner_t *s, double value)
{
static json_t *pack_real(scanner_t *s, double value) {
/* Allocate without setting value so we can identify OOM error. */
json_t *json = json_real(0.0);
@ -426,7 +413,8 @@ static json_t *pack_real(scanner_t *s, double value)
if (json_real_set(json, value)) {
json_decref(json);
set_error(s, "<args>", json_error_numeric_overflow, "Invalid floating point value");
set_error(s, "<args>", json_error_numeric_overflow,
"Invalid floating point value");
s->has_error = 1;
return NULL;
@ -435,20 +423,15 @@ static json_t *pack_real(scanner_t *s, double value)
return json;
}
static json_t *pack(scanner_t *s, va_list *ap)
{
static json_t *pack(scanner_t *s, va_list *ap) {
switch (token(s)) {
case '{':
return pack_object(s, ap);
case '{': return pack_object(s, ap);
case '[':
return pack_array(s, ap);
case '[': return pack_array(s, ap);
case 's': /* string */
return pack_string(s, ap);
case 's': /* string */ return pack_string(s, ap);
case 'n': /* null */
return json_null();
case 'n': /* null */ return json_null();
case 'b': /* boolean */
return va_arg(*ap, int) ? json_true() : json_false();
@ -459,8 +442,7 @@ static json_t *pack(scanner_t *s, va_list *ap)
case 'I': /* integer from json_int_t */
return pack_integer(s, va_arg(*ap, json_int_t));
case 'f': /* real */
return pack_real(s, va_arg(*ap, double));
case 'f': /* real */ return pack_real(s, va_arg(*ap, double));
case 'O': /* a json_t object; increments refcount */
return pack_object_inter(s, ap, 1);
@ -469,8 +451,8 @@ static json_t *pack(scanner_t *s, va_list *ap)
return pack_object_inter(s, ap, 0);
default:
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
token(s));
set_error(s, "<format>", json_error_invalid_format,
"Unexpected format character '%c'", token(s));
s->has_error = 1;
return NULL;
}
@ -478,8 +460,7 @@ static json_t *pack(scanner_t *s, va_list *ap)
static int unpack(scanner_t *s, json_t *root, va_list *ap);
static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
{
static int unpack_object(scanner_t *s, json_t *root, va_list *ap) {
int ret = -1;
int strict = 0;
int gotopt = 0;
@ -497,8 +478,8 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
}
if (root && !json_is_object(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected object, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected object, got %s", type_name(root));
goto out;
}
next_token(s);
@ -509,13 +490,15 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
int opt = 0;
if (strict != 0) {
set_error(s, "<format>", json_error_invalid_format, "Expected '}' after '%c', got '%c'",
set_error(s, "<format>", json_error_invalid_format,
"Expected '}' after '%c', got '%c'",
(strict == 1 ? '!' : '*'), token(s));
goto out;
}
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
set_error(s, "<format>", json_error_invalid_format,
"Unexpected end of format string");
goto out;
}
@ -526,7 +509,8 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
}
if (token(s) != 's') {
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
set_error(s, "<format>", json_error_invalid_format,
"Expected format 's', got '%c'", token(s));
goto out;
}
@ -546,11 +530,11 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
if (!root) {
/* skipping */
value = NULL;
}
else {
} else {
value = json_object_get(root, key);
if (!value && !opt) {
set_error(s, "<validation>", json_error_item_not_found, "Object item not found: %s", key);
set_error(s, "<validation>", json_error_item_not_found,
"Object item not found: %s", key);
goto out;
}
}
@ -583,19 +567,21 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
if (keys_res == 1) {
keys_res = strbuffer_init(&unrecognized_keys);
} else if (!keys_res) {
keys_res = strbuffer_append_bytes(&unrecognized_keys, ", ", 2);
keys_res =
strbuffer_append_bytes(&unrecognized_keys, ", ", 2);
}
if (!keys_res)
keys_res = strbuffer_append_bytes(&unrecognized_keys, key, strlen(key));
keys_res = strbuffer_append_bytes(&unrecognized_keys,
key, strlen(key));
}
}
}
if (unpacked) {
set_error(s, "<validation>", json_error_end_of_input_expected,
"%li object item(s) left unpacked: %s",
unpacked,
keys_res ? "<unknown>" : strbuffer_value(&unrecognized_keys));
"%li object item(s) left unpacked: %s", unpacked,
keys_res ? "<unknown>"
: strbuffer_value(&unrecognized_keys));
strbuffer_close(&unrecognized_keys);
goto out;
}
@ -608,13 +594,13 @@ out:
return ret;
}
static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
{
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)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected array, got %s", type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected array, got %s", type_name(root));
return -1;
}
next_token(s);
@ -623,14 +609,15 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
json_t *value;
if (strict != 0) {
set_error(s, "<format>", json_error_invalid_format, "Expected ']' after '%c', got '%c'",
(strict == 1 ? '!' : '*'),
token(s));
set_error(s, "<format>", json_error_invalid_format,
"Expected ']' after '%c', got '%c'",
(strict == 1 ? '!' : '*'), token(s));
return -1;
}
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
set_error(s, "<format>", json_error_invalid_format,
"Unexpected end of format string");
return -1;
}
@ -641,20 +628,19 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
}
if (!strchr(unpack_value_starters, token(s))) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
token(s));
set_error(s, "<format>", json_error_invalid_format,
"Unexpected format character '%c'", token(s));
return -1;
}
if (!root) {
/* skipping */
value = NULL;
}
else {
} else {
value = json_array_get(root, i);
if (!value) {
set_error(s, "<validation>", json_error_index_out_of_range, "Array index %lu out of range",
(unsigned long)i);
set_error(s, "<validation>", json_error_index_out_of_range,
"Array index %lu out of range", (unsigned long)i);
return -1;
}
}
@ -671,27 +657,24 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
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);
set_error(s, "<validation>", json_error_end_of_input_expected,
"%li array item(s) left unpacked", diff);
return -1;
}
return 0;
}
static int unpack(scanner_t *s, json_t *root, va_list *ap)
{
switch(token(s))
{
case '{':
return unpack_object(s, root, ap);
static int unpack(scanner_t *s, json_t *root, va_list *ap) {
switch (token(s)) {
case '{': return unpack_object(s, root, ap);
case '[':
return unpack_array(s, root, ap);
case '[': return unpack_array(s, root, ap);
case 's':
if (root && !json_is_string(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected string, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected string, got %s", type_name(root));
return -1;
}
@ -701,7 +684,8 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
str_target = va_arg(*ap, const char **);
if (!str_target) {
set_error(s, "<args>", json_error_null_value, "NULL string argument");
set_error(s, "<args>", json_error_null_value,
"NULL string argument");
return -1;
}
@ -710,11 +694,11 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
if (token(s) == '%') {
len_target = va_arg(*ap, size_t *);
if (!len_target) {
set_error(s, "<args>", json_error_null_value, "NULL string length argument");
set_error(s, "<args>", json_error_null_value,
"NULL string length argument");
return -1;
}
}
else
} else
prev_token(s);
if (root) {
@ -727,8 +711,8 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'i':
if (root && !json_is_integer(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected integer, got %s", type_name(root));
return -1;
}
@ -742,8 +726,8 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'I':
if (root && !json_is_integer(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected integer, got %s", type_name(root));
return -1;
}
@ -757,8 +741,8 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'b':
if (root && !json_is_boolean(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected true or false, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected true or false, got %s", type_name(root));
return -1;
}
@ -772,8 +756,8 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'f':
if (root && !json_is_real(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected real, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected real, got %s", type_name(root));
return -1;
}
@ -787,8 +771,8 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'F':
if (root && !json_is_number(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected real or integer, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected real or integer, got %s", type_name(root));
return -1;
}
@ -817,29 +801,29 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'n':
/* Never assign, just validate */
if (root && !json_is_null(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected null, got %s",
type_name(root));
set_error(s, "<validation>", json_error_wrong_type,
"Expected null, got %s", type_name(root));
return -1;
}
return 0;
default:
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
token(s));
set_error(s, "<format>", json_error_invalid_format,
"Unexpected format character '%c'", token(s));
return -1;
}
}
json_t *json_vpack_ex(json_error_t *error, size_t flags,
const char *fmt, va_list ap)
{
json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt,
va_list ap) {
scanner_t s;
va_list ap_copy;
json_t *value;
if (!fmt || !*fmt) {
jsonp_error_init(error, "<format>");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument,
"NULL or empty format string");
return NULL;
}
jsonp_error_init(error, NULL);
@ -858,15 +842,15 @@ json_t *json_vpack_ex(json_error_t *error, size_t flags,
next_token(&s);
if (token(&s)) {
json_decref(value);
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
set_error(&s, "<format>", json_error_invalid_format,
"Garbage after format string");
return NULL;
}
return value;
}
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
{
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...) {
json_t *value;
va_list ap;
@ -877,8 +861,7 @@ json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
return value;
}
json_t *json_pack(const char *fmt, ...)
{
json_t *json_pack(const char *fmt, ...) {
json_t *value;
va_list ap;
@ -890,20 +873,21 @@ json_t *json_pack(const char *fmt, ...)
}
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
const char *fmt, va_list ap)
{
const char *fmt, va_list ap) {
scanner_t s;
va_list ap_copy;
if (!root) {
jsonp_error_init(error, "<root>");
jsonp_error_set(error, -1, -1, 0, json_error_null_value, "NULL root value");
jsonp_error_set(error, -1, -1, 0, json_error_null_value,
"NULL root value");
return -1;
}
if (!fmt || !*fmt) {
jsonp_error_init(error, "<format>");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument,
"NULL or empty format string");
return -1;
}
jsonp_error_init(error, NULL);
@ -920,15 +904,16 @@ int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
next_token(&s);
if (token(&s)) {
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
set_error(&s, "<format>", json_error_invalid_format,
"Garbage after format string");
return -1;
}
return 0;
}
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
{
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags,
const char *fmt, ...) {
int ret;
va_list ap;
@ -939,8 +924,7 @@ int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *
return ret;
}
int json_unpack(json_t *root, const char *fmt, ...)
{
int json_unpack(json_t *root, const char *fmt, ...) {
int ret;
va_list ap;

View File

@ -9,17 +9,16 @@
#define _GNU_SOURCE
#endif
#include "strbuffer.h"
#include "jansson_private.h"
#include <stdlib.h>
#include <string.h>
#include "jansson_private.h"
#include "strbuffer.h"
#define STRBUFFER_MIN_SIZE 16
#define STRBUFFER_FACTOR 2
#define STRBUFFER_SIZE_MAX ((size_t)-1)
int strbuffer_init(strbuffer_t *strbuff)
{
int strbuffer_init(strbuffer_t *strbuff) {
strbuff->size = STRBUFFER_MIN_SIZE;
strbuff->length = 0;
@ -32,8 +31,7 @@ int strbuffer_init(strbuffer_t *strbuff)
return 0;
}
void strbuffer_close(strbuffer_t *strbuff)
{
void strbuffer_close(strbuffer_t *strbuff) {
if (strbuff->value)
jsonp_free(strbuff->value);
@ -42,44 +40,39 @@ void strbuffer_close(strbuffer_t *strbuff)
strbuff->value = NULL;
}
void strbuffer_clear(strbuffer_t *strbuff)
{
void strbuffer_clear(strbuffer_t *strbuff) {
strbuff->length = 0;
strbuff->value[0] = '\0';
}
const char *strbuffer_value(const strbuffer_t *strbuff)
{
const char *strbuffer_value(const strbuffer_t *strbuff) {
return strbuff->value;
}
char *strbuffer_steal_value(strbuffer_t *strbuff)
{
char *strbuffer_steal_value(strbuffer_t *strbuff) {
char *result = strbuff->value;
strbuff->value = NULL;
return result;
}
int strbuffer_append_byte(strbuffer_t *strbuff, char byte)
{
int strbuffer_append_byte(strbuffer_t *strbuff, char byte) {
return strbuffer_append_bytes(strbuff, &byte, 1);
}
int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
{
if(size >= strbuff->size - strbuff->length)
{
int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data,
size_t size) {
if (size >= strbuff->size - strbuff->length) {
size_t new_size;
char *new_value;
/* avoid integer overflow */
if (strbuff->size > STRBUFFER_SIZE_MAX / STRBUFFER_FACTOR
|| size > STRBUFFER_SIZE_MAX - 1
|| strbuff->length > STRBUFFER_SIZE_MAX - 1 - size)
if (strbuff->size > STRBUFFER_SIZE_MAX / STRBUFFER_FACTOR ||
size > STRBUFFER_SIZE_MAX - 1 ||
strbuff->length > STRBUFFER_SIZE_MAX - 1 - size)
return -1;
new_size = max(strbuff->size * STRBUFFER_FACTOR,
strbuff->length + size + 1);
new_size =
max(strbuff->size * STRBUFFER_FACTOR, strbuff->length + size + 1);
new_value = jsonp_malloc(new_size);
if (!new_value)
@ -99,13 +92,11 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
return 0;
}
char strbuffer_pop(strbuffer_t *strbuff)
{
char strbuffer_pop(strbuffer_t *strbuff) {
if (strbuff->length > 0) {
char c = strbuff->value[--strbuff->length];
strbuff->value[strbuff->length] = '\0';
return c;
}
else
} else
return '\0';
}

View File

@ -8,6 +8,7 @@
#ifndef STRBUFFER_H
#define STRBUFFER_H
#include "jansson.h"
#include <stdlib.h>
typedef struct {

View File

@ -1,8 +1,8 @@
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "jansson_private.h"
#include "strbuffer.h"
@ -24,8 +24,7 @@
this way. Multi-threaded programs should use uselocale() instead.
*/
static void to_locale(strbuffer_t *strbuffer)
{
static void to_locale(strbuffer_t *strbuffer) {
const char *point;
char *pos;
@ -40,8 +39,7 @@ static void to_locale(strbuffer_t *strbuffer)
*pos = *point;
}
static void from_locale(char *buffer)
{
static void from_locale(char *buffer) {
const char *point;
char *pos;
@ -57,8 +55,7 @@ static void from_locale(char *buffer)
}
#endif
int jsonp_strtod(strbuffer_t *strbuffer, double *out)
{
int jsonp_strtod(strbuffer_t *strbuffer, double *out) {
double value;
char *end;
@ -79,8 +76,7 @@ int jsonp_strtod(strbuffer_t *strbuffer, double *out)
return 0;
}
int jsonp_dtostr(char *buffer, size_t size, double value, int precision)
{
int jsonp_dtostr(char *buffer, size_t size, double value, int precision) {
int ret;
char *start, *end;
size_t length;
@ -102,9 +98,7 @@ 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 (strchr(buffer, '.') == NULL && strchr(buffer, 'e') == NULL) {
if (length + 3 >= size) {
/* No space to append ".0" */
return -1;

View File

@ -5,47 +5,37 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include "utf.h"
#include <string.h>
int utf8_encode(int32_t codepoint, char *buffer, size_t *size)
{
int utf8_encode(int32_t codepoint, char *buffer, size_t *size) {
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;
}
size_t utf8_check_first(char byte)
{
size_t utf8_check_first(char byte) {
unsigned char u = (unsigned char)byte;
if (u < 0x80)
@ -55,12 +45,10 @@ size_t utf8_check_first(char byte)
/* 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;
}
@ -68,41 +56,31 @@ size_t utf8_check_first(char byte)
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;
}
}
size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint)
{
size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint) {
size_t i;
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) {
@ -123,8 +101,7 @@ size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint)
return 0;
}
else if((size == 2 && value < 0x80) ||
(size == 3 && value < 0x800) ||
else if ((size == 2 && value < 0x80) || (size == 3 && value < 0x800) ||
(size == 4 && value < 0x10000)) {
/* overlong encoding */
return 0;
@ -136,8 +113,8 @@ size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint)
return 1;
}
const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint)
{
const char *utf8_iterate(const char *buffer, size_t bufsize,
int32_t *codepoint) {
size_t count;
int32_t value;
@ -150,8 +127,7 @@ const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint)
if (count == 1)
value = (unsigned char)buffer[0];
else
{
else {
if (count > bufsize || !utf8_check_full(buffer, count, &value))
return NULL;
}
@ -162,17 +138,14 @@ const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint)
return buffer + count;
}
int utf8_check_string(const char *string, size_t length)
{
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)
return 0;
else if(count > 1)
{
else if (count > 1) {
if (count > length - i)
return 0;

View File

@ -12,6 +12,7 @@
#include <jansson_private_config.h>
#endif
#include <stddef.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

View File

@ -13,17 +13,17 @@
#include <jansson_private_config.h>
#endif
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include "jansson.h"
#include "hashtable.h"
#include "jansson.h"
#include "jansson_private.h"
#include "utf.h"
@ -39,14 +39,13 @@ static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); }
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->refcount = 1;
}
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size)
{
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key,
size_t key_size) {
snprintf(key, key_size, "%p", json);
if (hashtable_get(parents, key))
return -1;
@ -58,8 +57,7 @@ int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t
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));
if (!object)
return NULL;
@ -71,8 +69,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,14 +77,12 @@ json_t *json_object(void)
return &object->json;
}
static void json_delete_object(json_object_t *object)
{
static void json_delete_object(json_object_t *object) {
hashtable_close(&object->hashtable);
jsonp_free(object);
}
size_t json_object_size(const json_t *json)
{
size_t json_object_size(const json_t *json) {
json_object_t *object;
if (!json_is_object(json))
@ -97,8 +92,7 @@ size_t json_object_size(const json_t *json)
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) {
json_object_t *object;
if (!key || !json_is_object(json))
@ -108,22 +102,19 @@ json_t *json_object_get(const json_t *json, const char *key)
return hashtable_get(&object->hashtable, key);
}
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) {
json_object_t *object;
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;
}
@ -131,10 +122,8 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
return 0;
}
int json_object_set_new(json_t *json, const char *key, json_t *value)
{
if(!key || !utf8_check_string(key, strlen(key)))
{
int json_object_set_new(json_t *json, const char *key, json_t *value) {
if (!key || !utf8_check_string(key, strlen(key))) {
json_decref(value);
return -1;
}
@ -142,8 +131,7 @@ int json_object_set_new(json_t *json, const char *key, json_t *value)
return json_object_set_new_nocheck(json, key, value);
}
int json_object_del(json_t *json, const char *key)
{
int json_object_del(json_t *json, const char *key) {
json_object_t *object;
if (!key || !json_is_object(json))
@ -153,8 +141,7 @@ int json_object_del(json_t *json, const char *key)
return hashtable_del(&object->hashtable, key);
}
int json_object_clear(json_t *json)
{
int json_object_clear(json_t *json) {
json_object_t *object;
if (!json_is_object(json))
@ -166,8 +153,7 @@ int json_object_clear(json_t *json)
return 0;
}
int json_object_update(json_t *object, json_t *other)
{
int json_object_update(json_t *object, json_t *other) {
const char *key;
json_t *value;
@ -182,8 +168,7 @@ int json_object_update(json_t *object, json_t *other)
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;
json_t *value;
@ -198,8 +183,7 @@ int json_object_update_existing(json_t *object, json_t *other)
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;
json_t *value;
@ -214,8 +198,8 @@ int json_object_update_missing(json_t *object, json_t *other)
return 0;
}
int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *parents)
{
int do_object_update_recursive(json_t *object, json_t *other,
hashtable_t *parents) {
const char *key;
json_t *value;
char loop_key[LOOP_KEY_LEN];
@ -230,18 +214,13 @@ int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *paren
json_object_foreach(other, key, value) {
json_t *v = json_object_get(object, key);
if(json_is_object(v) && json_is_object(value))
{
if(do_object_update_recursive(v, value, parents))
{
if (json_is_object(v) && json_is_object(value)) {
if (do_object_update_recursive(v, value, parents)) {
res = -1;
break;
}
}
else
{
if(json_object_set_nocheck(object, key, value))
{
} else {
if (json_object_set_nocheck(object, key, value)) {
res = -1;
break;
}
@ -253,8 +232,7 @@ int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *paren
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;
hashtable_t parents_set;
@ -266,8 +244,7 @@ int json_object_update_recursive(json_t *object, json_t *other)
return res;
}
void *json_object_iter(json_t *json)
{
void *json_object_iter(json_t *json) {
json_object_t *object;
if (!json_is_object(json))
@ -277,8 +254,7 @@ void *json_object_iter(json_t *json)
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;
if (!key || !json_is_object(json))
@ -288,8 +264,7 @@ void *json_object_iter_at(json_t *json, const char *key)
return hashtable_iter_at(&object->hashtable, 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;
if (!json_is_object(json) || iter == NULL)
@ -299,26 +274,22 @@ void *json_object_iter_next(json_t *json, void *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)
return NULL;
return hashtable_iter_key(iter);
}
json_t *json_object_iter_value(void *iter)
{
json_t *json_object_iter_value(void *iter) {
if (!iter)
return NULL;
return (json_t *)hashtable_iter_value(iter);
}
int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
{
if(!json_is_object(json) || !iter || !value)
{
int json_object_iter_set_new(json_t *json, void *iter, json_t *value) {
if (!json_is_object(json) || !iter || !value) {
json_decref(value);
return -1;
}
@ -327,16 +298,14 @@ int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
return 0;
}
void *json_object_key_to_iter(const char *key)
{
void *json_object_key_to_iter(const char *key) {
if (!key)
return NULL;
return hashtable_key_to_iter(key);
}
static int json_object_equal(const json_t *object1, const json_t *object2)
{
static int json_object_equal(const json_t *object1, const json_t *object2) {
const char *key;
const json_t *value1, *value2;
@ -353,8 +322,7 @@ static int json_object_equal(const json_t *object1, const json_t *object2)
return 1;
}
static json_t *json_object_copy(json_t *object)
{
static json_t *json_object_copy(json_t *object) {
json_t *result;
const char *key;
@ -370,8 +338,8 @@ static json_t *json_object_copy(json_t *object)
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;
void *iter;
char loop_key[LOOP_KEY_LEN];
@ -392,8 +360,8 @@ static json_t *json_object_deep_copy(const json_t *object, hashtable_t *parents)
key = json_object_iter_key(iter);
value = json_object_iter_value(iter);
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);
result = NULL;
break;
@ -407,11 +375,9 @@ out:
return result;
}
/*** array ***/
json_t *json_array(void)
{
json_t *json_array(void) {
json_array_t *array = jsonp_malloc(sizeof(json_array_t));
if (!array)
return NULL;
@ -429,8 +395,7 @@ json_t *json_array(void)
return &array->json;
}
static void json_delete_array(json_array_t *array)
{
static void json_delete_array(json_array_t *array) {
size_t i;
for (i = 0; i < array->entries; i++)
@ -440,16 +405,14 @@ static void json_delete_array(json_array_t *array)
jsonp_free(array);
}
size_t json_array_size(const json_t *json)
{
size_t json_array_size(const json_t *json) {
if (!json_is_array(json))
return 0;
return json_to_array(json)->entries;
}
json_t *json_array_get(const json_t *json, size_t index)
{
json_t *json_array_get(const json_t *json, size_t index) {
json_array_t *array;
if (!json_is_array(json))
return NULL;
@ -461,22 +424,19 @@ json_t *json_array_get(const json_t *json, size_t index)
return array->table[index];
}
int json_array_set_new(json_t *json, size_t index, json_t *value)
{
int json_array_set_new(json_t *json, size_t index, json_t *value) {
json_array_t *array;
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;
}
@ -487,23 +447,17 @@ int json_array_set_new(json_t *json, size_t index, json_t *value)
return 0;
}
static void array_move(json_array_t *array, size_t dest,
size_t src, size_t count)
{
static void array_move(json_array_t *array, size_t dest, size_t src,
size_t count) {
memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
}
static void array_copy(json_t **dest, size_t dpos,
json_t **src, size_t spos,
size_t count)
{
static void array_copy(json_t **dest, size_t dpos, json_t **src, size_t spos,
size_t count) {
memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
}
static json_t **json_array_grow(json_array_t *array,
size_t amount,
int copy)
{
static json_t **json_array_grow(json_array_t *array, size_t amount, int copy) {
size_t new_size;
json_t **old_table, **new_table;
@ -529,15 +483,13 @@ static json_t **json_array_grow(json_array_t *array,
return old_table;
}
int json_array_append_new(json_t *json, json_t *value)
{
int json_array_append_new(json_t *json, json_t *value) {
json_array_t *array;
if (!value)
return -1;
if(!json_is_array(json) || json == value)
{
if (!json_is_array(json) || json == value) {
json_decref(value);
return -1;
}
@ -554,8 +506,7 @@ int json_array_append_new(json_t *json, json_t *value)
return 0;
}
int json_array_insert_new(json_t *json, size_t index, json_t *value)
{
int json_array_insert_new(json_t *json, size_t index, json_t *value) {
json_array_t *array;
json_t **old_table;
@ -584,8 +535,7 @@ int json_array_insert_new(json_t *json, size_t index, json_t *value)
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;
@ -594,8 +544,7 @@ int json_array_insert_new(json_t *json, size_t index, json_t *value)
return 0;
}
int json_array_remove(json_t *json, size_t index)
{
int json_array_remove(json_t *json, size_t index) {
json_array_t *array;
if (!json_is_array(json))
@ -616,8 +565,7 @@ int json_array_remove(json_t *json, size_t index)
return 0;
}
int json_array_clear(json_t *json)
{
int json_array_clear(json_t *json) {
json_array_t *array;
size_t i;
@ -632,8 +580,7 @@ int json_array_clear(json_t *json)
return 0;
}
int json_array_extend(json_t *json, json_t *other_json)
{
int json_array_extend(json_t *json, json_t *other_json) {
json_array_t *array, *other;
size_t i;
@ -654,16 +601,14 @@ int json_array_extend(json_t *json, json_t *other_json)
return 0;
}
static int json_array_equal(const json_t *array1, const json_t *array2)
{
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))
return 0;
for(i = 0; i < size; i++)
{
for (i = 0; i < size; i++) {
json_t *value1, *value2;
value1 = json_array_get(array1, i);
@ -676,8 +621,7 @@ static int json_array_equal(const json_t *array1, const json_t *array2)
return 1;
}
static json_t *json_array_copy(json_t *array)
{
static json_t *json_array_copy(json_t *array) {
json_t *result;
size_t i;
@ -691,8 +635,7 @@ static json_t *json_array_copy(json_t *array)
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;
size_t i;
char loop_key[LOOP_KEY_LEN];
@ -704,10 +647,9 @@ static json_t *json_array_deep_copy(const json_t *array, hashtable_t *parents)
if (!result)
goto out;
for(i = 0; i < json_array_size(array); i++)
{
if (json_array_append_new(result, do_deep_copy(json_array_get(array, i), parents)))
{
for (i = 0; i < json_array_size(array); i++) {
if (json_array_append_new(
result, do_deep_copy(json_array_get(array, i), parents))) {
json_decref(result);
result = NULL;
break;
@ -722,8 +664,7 @@ out:
/*** 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;
json_string_t *string;
@ -750,67 +691,58 @@ static json_t *string_create(const char *value, size_t len, int own)
return &string->json;
}
json_t *json_string_nocheck(const char *value)
{
json_t *json_string_nocheck(const char *value) {
if (!value)
return NULL;
return string_create(value, strlen(value), 0);
}
json_t *json_stringn_nocheck(const char *value, size_t len)
{
json_t *json_stringn_nocheck(const char *value, size_t len) {
return string_create(value, len, 0);
}
/* this is private; "steal" is not a public API concept */
json_t *jsonp_stringn_nocheck_own(const char *value, size_t len)
{
json_t *jsonp_stringn_nocheck_own(const char *value, size_t len) {
return string_create(value, len, 1);
}
json_t *json_string(const char *value)
{
json_t *json_string(const char *value) {
if (!value)
return NULL;
return json_stringn(value, strlen(value));
}
json_t *json_stringn(const char *value, size_t len)
{
json_t *json_stringn(const char *value, size_t len) {
if (!value || !utf8_check_string(value, len))
return NULL;
return json_stringn_nocheck(value, len);
}
const char *json_string_value(const json_t *json)
{
const char *json_string_value(const json_t *json) {
if (!json_is_string(json))
return NULL;
return json_to_string(json)->value;
}
size_t json_string_length(const json_t *json)
{
size_t json_string_length(const json_t *json) {
if (!json_is_string(json))
return 0;
return json_to_string(json)->length;
}
int json_string_set_nocheck(json_t *json, const char *value)
{
int json_string_set_nocheck(json_t *json, const char *value) {
if (!value)
return -1;
return json_string_setn_nocheck(json, value, strlen(value));
}
int json_string_setn_nocheck(json_t *json, const char *value, size_t len)
{
int json_string_setn_nocheck(json_t *json, const char *value, size_t len) {
char *dup;
json_string_t *string;
@ -829,39 +761,35 @@ int json_string_setn_nocheck(json_t *json, const char *value, size_t len)
return 0;
}
int json_string_set(json_t *json, const char *value)
{
int json_string_set(json_t *json, const char *value) {
if (!value)
return -1;
return json_string_setn(json, value, strlen(value));
}
int json_string_setn(json_t *json, const char *value, size_t len)
{
int json_string_setn(json_t *json, const char *value, size_t len) {
if (!value || !utf8_check_string(value, len))
return -1;
return json_string_setn_nocheck(json, value, len);
}
static void json_delete_string(json_string_t *string)
{
static void json_delete_string(json_string_t *string) {
jsonp_free(string->value);
jsonp_free(string);
}
static int json_string_equal(const json_t *string1, const json_t *string2)
{
static int json_string_equal(const json_t *string1, const json_t *string2) {
json_string_t *s1, *s2;
s1 = json_to_string(string1);
s2 = json_to_string(string2);
return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length);
return s1->length == s2->length &&
!memcmp(s1->value, s2->value, s1->length);
}
static json_t *json_string_copy(const json_t *string)
{
static json_t *json_string_copy(const json_t *string) {
json_string_t *s;
s = json_to_string(string);
@ -909,11 +837,9 @@ json_t *json_sprintf(const char *fmt, ...) {
return result;
}
/*** integer ***/
json_t *json_integer(json_int_t value)
{
json_t *json_integer(json_int_t value) {
json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
if (!integer)
return NULL;
@ -923,16 +849,14 @@ json_t *json_integer(json_int_t value)
return &integer->json;
}
json_int_t json_integer_value(const json_t *json)
{
json_int_t json_integer_value(const json_t *json) {
if (!json_is_integer(json))
return 0;
return json_to_integer(json)->value;
}
int json_integer_set(json_t *json, json_int_t value)
{
int json_integer_set(json_t *json, json_int_t value) {
if (!json_is_integer(json))
return -1;
@ -941,26 +865,21 @@ int json_integer_set(json_t *json, json_int_t value)
return 0;
}
static void json_delete_integer(json_integer_t *integer)
{
static void json_delete_integer(json_integer_t *integer) {
jsonp_free(integer);
}
static int json_integer_equal(const json_t *integer1, const json_t *integer2)
{
static int json_integer_equal(const json_t *integer1, const json_t *integer2) {
return json_integer_value(integer1) == json_integer_value(integer2);
}
static json_t *json_integer_copy(const json_t *integer)
{
static json_t *json_integer_copy(const json_t *integer) {
return json_integer(json_integer_value(integer));
}
/*** real ***/
json_t *json_real(double value)
{
json_t *json_real(double value) {
json_real_t *real;
if (isnan(value) || isinf(value))
@ -975,16 +894,14 @@ json_t *json_real(double value)
return &real->json;
}
double json_real_value(const json_t *json)
{
double json_real_value(const json_t *json) {
if (!json_is_real(json))
return 0;
return json_to_real(json)->value;
}
int json_real_set(json_t *json, double value)
{
int json_real_set(json_t *json, double value) {
if (!json_is_real(json) || isnan(value) || isinf(value))
return -1;
@ -993,26 +910,19 @@ int json_real_set(json_t *json, double value)
return 0;
}
static void json_delete_real(json_real_t *real)
{
jsonp_free(real);
}
static void json_delete_real(json_real_t *real) { jsonp_free(real); }
static int json_real_equal(const json_t *real1, const json_t *real2)
{
static int json_real_equal(const json_t *real1, const json_t *real2) {
return json_real_value(real1) == json_real_value(real2);
}
static json_t *json_real_copy(const json_t *real)
{
static json_t *json_real_copy(const json_t *real) {
return json_real(json_real_value(real));
}
/*** number ***/
double json_number_value(const json_t *json)
{
double json_number_value(const json_t *json) {
if (json_is_integer(json))
return (double)json_integer_value(json);
else if (json_is_real(json))
@ -1021,65 +931,44 @@ double json_number_value(const json_t *json)
return 0.0;
}
/*** simple values ***/
json_t *json_true(void)
{
json_t *json_true(void) {
static json_t the_true = {JSON_TRUE, (size_t)-1};
return &the_true;
}
json_t *json_false(void)
{
json_t *json_false(void) {
static json_t the_false = {JSON_FALSE, (size_t)-1};
return &the_false;
}
json_t *json_null(void)
{
json_t *json_null(void) {
static json_t the_null = {JSON_NULL, (size_t)-1};
return &the_null;
}
/*** deletion ***/
void json_delete(json_t *json)
{
void json_delete(json_t *json) {
if (!json)
return;
switch (json_typeof(json)) {
case JSON_OBJECT:
json_delete_object(json_to_object(json));
break;
case JSON_ARRAY:
json_delete_array(json_to_array(json));
break;
case JSON_STRING:
json_delete_string(json_to_string(json));
break;
case JSON_INTEGER:
json_delete_integer(json_to_integer(json));
break;
case JSON_REAL:
json_delete_real(json_to_real(json));
break;
default:
return;
case JSON_OBJECT: json_delete_object(json_to_object(json)); break;
case JSON_ARRAY: json_delete_array(json_to_array(json)); break;
case JSON_STRING: json_delete_string(json_to_string(json)); break;
case JSON_INTEGER: json_delete_integer(json_to_integer(json)); break;
case JSON_REAL: json_delete_real(json_to_real(json)); break;
default: return;
}
/* json_delete is not called for true, false or null */
}
/*** equality ***/
int json_equal(const json_t *json1, const json_t *json2)
{
int json_equal(const json_t *json1, const json_t *json2) {
if (!json1 || !json2)
return 0;
@ -1091,51 +980,35 @@ int json_equal(const json_t *json1, const json_t *json2)
return 1;
switch (json_typeof(json1)) {
case JSON_OBJECT:
return json_object_equal(json1, json2);
case JSON_ARRAY:
return json_array_equal(json1, json2);
case JSON_STRING:
return json_string_equal(json1, json2);
case JSON_INTEGER:
return json_integer_equal(json1, json2);
case JSON_REAL:
return json_real_equal(json1, json2);
default:
return 0;
case JSON_OBJECT: return json_object_equal(json1, json2);
case JSON_ARRAY: return json_array_equal(json1, json2);
case JSON_STRING: return json_string_equal(json1, json2);
case JSON_INTEGER: return json_integer_equal(json1, json2);
case JSON_REAL: return json_real_equal(json1, json2);
default: return 0;
}
}
/*** copying ***/
json_t *json_copy(json_t *json)
{
json_t *json_copy(json_t *json) {
if (!json)
return NULL;
switch (json_typeof(json)) {
case JSON_OBJECT:
return json_object_copy(json);
case JSON_ARRAY:
return json_array_copy(json);
case JSON_STRING:
return json_string_copy(json);
case JSON_INTEGER:
return json_integer_copy(json);
case JSON_REAL:
return json_real_copy(json);
case JSON_OBJECT: return json_object_copy(json);
case JSON_ARRAY: return json_array_copy(json);
case JSON_STRING: return json_string_copy(json);
case JSON_INTEGER: return json_integer_copy(json);
case JSON_REAL: return json_real_copy(json);
case JSON_TRUE:
case JSON_FALSE:
case JSON_NULL:
return json;
default:
return NULL;
case JSON_NULL: return json;
default: return NULL;
}
}
json_t *json_deep_copy(const json_t *json)
{
json_t *json_deep_copy(const json_t *json) {
json_t *res;
hashtable_t parents_set;
@ -1147,29 +1020,22 @@ json_t *json_deep_copy(const json_t *json)
return res;
}
json_t *do_deep_copy(const json_t *json, hashtable_t *parents)
{
json_t *do_deep_copy(const json_t *json, hashtable_t *parents) {
if (!json)
return NULL;
switch (json_typeof(json)) {
case JSON_OBJECT:
return json_object_deep_copy(json, parents);
case JSON_OBJECT: return json_object_deep_copy(json, parents);
case JSON_ARRAY:
return json_array_deep_copy(json, parents);
/* 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:
return json_integer_copy(json);
case JSON_REAL:
return json_real_copy(json);
case JSON_STRING: return json_string_copy(json);
case JSON_INTEGER: return json_integer_copy(json);
case JSON_REAL: return json_real_copy(json);
case JSON_TRUE:
case JSON_FALSE:
case JSON_NULL:
return (json_t *)json;
default:
return NULL;
case JSON_NULL: return (json_t *)json;
default: return NULL;
}
}

View File

@ -11,13 +11,9 @@
#include "jansson.h"
const char *jansson_version_str(void)
{
return JANSSON_VERSION;
}
const char *jansson_version_str(void) { return JANSSON_VERSION; }
int jansson_version_cmp(int major, int minor, int micro)
{
int jansson_version_cmp(int major, int minor, int micro) {
int diff;
if ((diff = JANSSON_MAJOR_VERSION - major)) {

View File

@ -9,26 +9,25 @@
#include <jansson_private_config.h>
#endif
#include <ctype.h>
#include <jansson.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <jansson.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#if _WIN32
#include <io.h> /* for _setmode() */
#include <fcntl.h> /* for _O_BINARY */
#include <io.h> /* for _setmode() */
static const char dir_sep = '\\';
#else
static const char dir_sep = '/';
#endif
struct config {
int indent;
int compact;
@ -47,8 +46,7 @@ struct config {
/* Return a pointer to the first non-whitespace character of str.
Modifies str so that all trailing whitespace characters are
replaced by '\0'. */
static const char *strip(char *str)
{
static const char *strip(char *str) {
size_t length;
char *result = str;
while (*result && l_isspace(*result))
@ -64,9 +62,7 @@ static const char *strip(char *str)
return result;
}
static char *loadfile(FILE *file)
{
static char *loadfile(FILE *file) {
long fsize, ret;
char *buf;
@ -83,9 +79,7 @@ static char *loadfile(FILE *file)
return buf;
}
static void read_conf(FILE *conffile)
{
static void read_conf(FILE *conffile) {
char *buffer, *line, *val;
buffer = loadfile(conffile);
@ -124,9 +118,7 @@ static void read_conf(FILE *conffile)
free(buffer);
}
static int cmpfile(const char *str, const char *path, const char *fname)
{
static int cmpfile(const char *str, const char *path, const char *fname) {
char filename[1024], *buffer;
int ret;
FILE *file;
@ -156,8 +148,7 @@ static int cmpfile(const char *str, const char *path, const char *fname)
return ret;
}
int use_conf(char *test_path)
{
int use_conf(char *test_path) {
int ret;
size_t flags = 0;
char filename[1024], errstr[1024];
@ -216,16 +207,14 @@ int use_conf(char *test_path)
buffer = loadfile(infile);
json = json_loads(strip(buffer), 0, &error);
free(buffer);
}
else
} else
json = json_loadf(infile, 0, &error);
fclose(infile);
if (!json) {
sprintf(errstr, "%d %d %d\n%s\n",
error.line, error.column, error.position,
error.text);
sprintf(errstr, "%d %d %d\n%s\n", error.line, error.column,
error.position, error.text);
ret = cmpfile(errstr, test_path, "error");
return ret;
@ -239,8 +228,7 @@ int use_conf(char *test_path)
return ret;
}
static int getenv_int(const char *name)
{
static int getenv_int(const char *name) {
char *value, *end;
long result;
@ -255,8 +243,7 @@ static int getenv_int(const char *name)
return (int)result;
}
int use_env()
{
int use_env() {
int indent, precision;
size_t flags = 0;
json_t *json;
@ -329,13 +316,11 @@ int use_env()
json = json_loads(strip(buffer), 0, &error);
free(buffer);
}
else
} else
json = json_loadf(stdin, 0, &error);
if (!json) {
fprintf(stderr, "%d %d %d\n%s\n",
error.line, error.column,
fprintf(stderr, "%d %d %d\n%s\n", error.line, error.column,
error.position, error.text);
return 1;
}
@ -346,8 +331,7 @@ int use_env()
return 0;
}
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
int i;
char *test_path = NULL;
@ -370,8 +354,7 @@ int main(int argc, char *argv[])
if (conf.use_env)
return use_env();
else
{
else {
if (!test_path)
goto usage;

View File

@ -5,11 +5,10 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <jansson.h>
#include "util.h"
#include <jansson.h>
static void test_misc(void)
{
static void test_misc(void) {
json_t *array, *five, *seven, *value;
size_t i;
@ -114,8 +113,7 @@ static void test_misc(void)
json_decref(array);
}
static void test_insert(void)
{
static void test_insert(void) {
json_t *array, *five, *seven, *eleven, *value;
int i;
@ -129,11 +127,9 @@ static void test_insert(void)
if (!five || !seven || !eleven)
fail("unable to create integer");
if (!json_array_insert(array, 1, five))
fail("able to insert value out of bounds");
if (json_array_insert(array, 0, five))
fail("unable to insert value in an empty array");
@ -143,7 +139,6 @@ static void test_insert(void)
if (json_array_size(array) != 1)
fail("array size is invalid after insertion");
if (json_array_insert(array, 1, seven))
fail("unable to insert value at the end of an array");
@ -156,7 +151,6 @@ static void test_insert(void)
if (json_array_size(array) != 2)
fail("array size is invalid after insertion");
if (json_array_insert(array, 1, eleven))
fail("unable to insert value in the middle of an array");
@ -172,7 +166,6 @@ static void test_insert(void)
if (json_array_size(array) != 3)
fail("array size is invalid after insertion");
if (json_array_insert_new(array, 2, json_integer(123)))
fail("unable to insert value in the middle of an array");
@ -183,7 +176,6 @@ static void test_insert(void)
if (json_array_size(array) != 4)
fail("array size is invalid after insertion");
for (i = 0; i < 20; i++) {
if (json_array_insert(array, 0, seven))
fail("unable to insert value at the beginning of an array");
@ -203,8 +195,7 @@ static void test_insert(void)
json_decref(array);
}
static void test_remove(void)
{
static void test_remove(void) {
json_t *array, *five, *seven;
int i;
@ -219,11 +210,9 @@ static void test_remove(void)
if (!seven)
fail("unable to create integer");
if (!json_array_remove(array, 0))
fail("able to remove an unexisting index");
if (json_array_append(array, five))
fail("unable to append");
@ -236,11 +225,8 @@ static void test_remove(void)
if (json_array_size(array) != 0)
fail("array size is invalid after removing");
if(json_array_append(array, five) ||
json_array_append(array, seven) ||
json_array_append(array, five) ||
json_array_append(array, seven))
if (json_array_append(array, five) || json_array_append(array, seven) ||
json_array_append(array, five) || json_array_append(array, seven))
fail("unable to append");
if (json_array_remove(array, 2))
@ -249,8 +235,7 @@ static void test_remove(void)
if (json_array_size(array) != 3)
fail("array size is invalid after removing");
if(json_array_get(array, 0) != five ||
json_array_get(array, 1) != seven ||
if (json_array_get(array, 0) != five || json_array_get(array, 1) != seven ||
json_array_get(array, 2) != seven)
fail("remove works incorrectly");
@ -272,8 +257,7 @@ static void test_remove(void)
json_decref(array);
}
static void test_clear(void)
{
static void test_clear(void) {
json_t *array, *five, *seven;
int i;
@ -309,8 +293,7 @@ static void test_clear(void)
json_decref(array);
}
static void test_extend(void)
{
static void test_extend(void) {
json_t *array1, *array2, *five, *seven;
int i;
@ -354,8 +337,7 @@ static void test_extend(void)
json_decref(array2);
}
static void test_circular()
{
static void test_circular() {
json_t *array1, *array2;
/* the simple cases are checked */
@ -378,7 +360,6 @@ static void test_circular()
json_decref(array1);
/* create circular references */
array1 = json_array();
@ -386,8 +367,7 @@ static void test_circular()
if (!array1 || !array2)
fail("unable to create array");
if(json_array_append(array1, array2) ||
json_array_append(array2, array1))
if (json_array_append(array1, array2) || json_array_append(array2, array1))
fail("unable to append");
/* circularity is detected when dumping */
@ -400,8 +380,7 @@ static void test_circular()
json_decref(array1);
}
static void test_array_foreach()
{
static void test_array_foreach() {
size_t index;
json_t *array1, *array2, *value;
@ -419,8 +398,7 @@ static void test_array_foreach()
json_decref(array2);
}
static void test_bad_args(void)
{
static void test_bad_args(void) {
json_t *arr = json_array();
json_t *num = json_integer(1);
@ -475,13 +453,17 @@ static void test_bad_args(void)
fail("json_array_insert_new did not return error for value == array");
if (!json_array_extend(NULL, arr))
fail("json_array_extend did not return error for first argument non-array");
fail("json_array_extend did not return error for first argument "
"non-array");
if (!json_array_extend(num, arr))
fail("json_array_extend did not return error for first argument non-array");
fail("json_array_extend did not return error for first argument "
"non-array");
if (!json_array_extend(arr, NULL))
fail("json_array_extend did not return error for second argument non-array");
fail("json_array_extend did not return error for second argument "
"non-array");
if (!json_array_extend(arr, num))
fail("json_array_extend did not return error for second argument non-array");
fail("json_array_extend did not return error for second argument "
"non-array");
if (num->refcount != 1)
fail("unexpected reference count on num");
@ -492,8 +474,7 @@ static void test_bad_args(void)
json_decref(arr);
}
static void run_tests()
{
static void run_tests() {
test_misc();
test_insert();
test_remove();

View File

@ -2,17 +2,16 @@
#define _GNU_SOURCE
#endif
#include "util.h"
#include <jansson.h>
#include <stdio.h>
#include <string.h>
#include <jansson.h>
#include "util.h"
static int chaos_pos = 0;
static int chaos_fail = 0;
#define CHAOS_MAX_FAILURE 100
void *chaos_malloc(size_t size)
{
void *chaos_malloc(size_t size) {
if (chaos_pos == chaos_fail)
return NULL;
@ -21,10 +20,7 @@ void *chaos_malloc(size_t size)
return malloc(size);
}
void chaos_free(void *obj)
{
free(obj);
}
void chaos_free(void *obj) { free(obj); }
/* Test all potential allocation failures. */
#define chaos_loop(condition, code, cleanup) \
@ -33,8 +29,7 @@ void chaos_free(void *obj)
while (condition) { \
if (chaos_fail > CHAOS_MAX_FAILURE) \
fail("too many chaos failures"); \
code \
chaos_pos = 0; \
code chaos_pos = 0; \
chaos_fail++; \
} \
cleanup \
@ -43,18 +38,19 @@ void chaos_free(void *obj)
#define chaos_loop_new_value(json, initcall) \
chaos_loop(!json, json = initcall;, json_decref(json); json = NULL;)
int test_unpack()
{
int test_unpack() {
int ret = -1;
int v1;
int v2;
json_error_t error;
json_t *root = json_pack("{s:i, s:i, s:i, s:i}", "n1", 1, "n2", 2, "n3", 3, "n4", 4);
json_t *root =
json_pack("{s:i, s:i, s:i, s:i}", "n1", 1, "n2", 2, "n3", 3, "n4", 4);
if (!root)
return -1;
if (!json_unpack_ex(root, &error, JSON_STRICT, "{s:i, s:i}", "n1", &v1, "n2", &v2))
if (!json_unpack_ex(root, &error, JSON_STRICT, "{s:i, s:i}", "n1", &v1,
"n2", &v2))
fail("Unexpected success");
if (json_error_code(&error) != json_error_end_of_input_expected) {
@ -74,8 +70,7 @@ out:
return ret;
}
int dump_chaos_callback(const char *buffer, size_t size, void *data)
{
int dump_chaos_callback(const char *buffer, size_t size, void *data) {
json_t *obj = json_object();
(void)buffer;
@ -90,8 +85,7 @@ int dump_chaos_callback(const char *buffer, size_t size, void *data)
return 0;
}
static void test_chaos()
{
static void test_chaos() {
json_malloc_t orig_malloc;
json_free_t orig_free;
json_t *json = NULL;
@ -102,9 +96,8 @@ static void test_chaos()
json_t *intnum = json_integer(1);
json_t *dblnum = json_real(0.5);
char *dumptxt = NULL;
json_t *dumpobj = json_pack("{s:[iiis], s:s}",
"key1", 1, 2, 3, "txt",
"key2", "v2");
json_t *dumpobj =
json_pack("{s:[iiis], s:s}", "key1", 1, 2, 3, "txt", "key2", "v2");
int keyno;
if (!obj || !arr1 || !arr2 || !txt || !intnum || !dblnum || !dumpobj)
@ -119,16 +112,22 @@ static void test_chaos()
chaos_loop_new_value(json, json_pack("[s*,s*]", "v1", "v2"));
chaos_loop_new_value(json, json_pack("o", json_incref(txt)));
chaos_loop_new_value(json, json_pack("O", txt));
chaos_loop_new_value(json, json_pack("s++", "a",
"long string to force realloc",
"another long string to force yet another reallocation of the string because "
chaos_loop_new_value(json,
json_pack("s++", "a", "long string to force realloc",
"another long string to force yet another "
"reallocation of the string because "
"that's what we are testing."));
chaos_loop(test_unpack(), , );
chaos_loop(json_dump_callback(dumpobj, dump_chaos_callback, NULL, JSON_INDENT(1)),,);
chaos_loop(json_dump_callback(dumpobj, dump_chaos_callback, NULL, JSON_INDENT(1) | JSON_SORT_KEYS),,);
chaos_loop(!dumptxt, dumptxt = json_dumps(dumpobj, JSON_COMPACT);, free(dumptxt); dumptxt = NULL;);
chaos_loop(
json_dump_callback(dumpobj, dump_chaos_callback, NULL, JSON_INDENT(1)),
, );
chaos_loop(json_dump_callback(dumpobj, dump_chaos_callback, NULL,
JSON_INDENT(1) | JSON_SORT_KEYS),
, );
chaos_loop(!dumptxt, dumptxt = json_dumps(dumpobj, JSON_COMPACT);
, free(dumptxt); dumptxt = NULL;);
chaos_loop_new_value(json, json_copy(obj));
chaos_loop_new_value(json, json_deep_copy(obj));
@ -142,7 +141,8 @@ static void test_chaos()
#define JSON_LOAD_TXT "{\"n\":[1,2,3,4,5,6,7,8,9,10]}"
chaos_loop_new_value(json, json_loads(JSON_LOAD_TXT, 0, NULL));
chaos_loop_new_value(json, json_loadb(JSON_LOAD_TXT, strlen(JSON_LOAD_TXT), 0, NULL));
chaos_loop_new_value(
json, json_loadb(JSON_LOAD_TXT, strlen(JSON_LOAD_TXT), 0, NULL));
chaos_loop_new_value(json, json_sprintf("%s", "string"));
@ -152,7 +152,8 @@ static void test_chaos()
char testkey[10];
snprintf(testkey, sizeof(testkey), "test%d", keyno);
chaos_loop(json_object_set_new_nocheck(obj, testkey, json_object()),,);
chaos_loop(json_object_set_new_nocheck(obj, testkey, json_object()),
, );
#endif
chaos_loop(json_array_append_new(arr1, json_null()), , );
chaos_loop(json_array_insert_new(arr2, 0, json_null()), , );
@ -171,7 +172,4 @@ static void test_chaos()
json_decref(dumpobj);
}
static void run_tests()
{
test_chaos();
}
static void run_tests() { test_chaos(); }

View File

@ -5,12 +5,11 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include <jansson.h>
#include "util.h"
#include <jansson.h>
#include <string.h>
static void test_copy_simple(void)
{
static void test_copy_simple(void) {
json_t *value, *copy;
if (json_copy(NULL))
@ -89,8 +88,7 @@ static void test_copy_simple(void)
json_decref(copy);
}
static void test_deep_copy_simple(void)
{
static void test_deep_copy_simple(void) {
json_t *value, *copy;
if (json_deep_copy(NULL))
@ -169,8 +167,7 @@ static void test_deep_copy_simple(void)
json_decref(copy);
}
static void test_copy_array(void)
{
static void test_copy_array(void) {
const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
json_t *array, *copy;
@ -188,8 +185,7 @@ static void test_copy_array(void)
if (!json_equal(copy, array))
fail("copying an array produces an inequal copy");
for(i = 0; i < json_array_size(copy); i++)
{
for (i = 0; i < json_array_size(copy); i++) {
if (json_array_get(array, i) != json_array_get(copy, i))
fail("copying an array modifies its elements");
}
@ -198,8 +194,7 @@ static void test_copy_array(void)
json_decref(copy);
}
static void test_deep_copy_array(void)
{
static void test_deep_copy_array(void) {
const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
json_t *array, *copy;
@ -217,8 +212,7 @@ static void test_deep_copy_array(void)
if (!json_equal(copy, array))
fail("deep copying an array produces an inequal copy");
for(i = 0; i < json_array_size(copy); i++)
{
for (i = 0; i < json_array_size(copy); i++) {
if (json_array_get(array, i) == json_array_get(copy, i))
fail("deep copying an array doesn't copy its elements");
}
@ -227,8 +221,7 @@ static void test_deep_copy_array(void)
json_decref(copy);
}
static void test_copy_object(void)
{
static void test_copy_object(void) {
const char *json_object_text =
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
@ -252,8 +245,7 @@ static void test_copy_object(void)
i = 0;
iter = json_object_iter(object);
while(iter)
{
while (iter) {
const char *key;
json_t *value1, *value2;
@ -275,8 +267,7 @@ static void test_copy_object(void)
json_decref(copy);
}
static void test_deep_copy_object(void)
{
static void test_deep_copy_object(void) {
const char *json_object_text =
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
@ -300,8 +291,7 @@ static void test_deep_copy_object(void)
i = 0;
iter = json_object_iter(object);
while(iter)
{
while (iter) {
const char *key;
json_t *value1, *value2;
@ -323,8 +313,7 @@ static void test_deep_copy_object(void)
json_decref(copy);
}
static void test_deep_copy_circular_references(void)
{
static void test_deep_copy_circular_references(void) {
/* Construct a JSON object/array with a circular reference:
object: {"a": {"b": {"c": <circular reference to $.a>}}}
@ -375,8 +364,7 @@ static void test_deep_copy_circular_references(void)
json_decref(json);
}
static void run_tests()
{
static void run_tests() {
test_copy_simple();
test_deep_copy_simple();
test_copy_array();

View File

@ -18,16 +18,14 @@
#define pipe(fds) _pipe(fds, 1024, _O_BINARY)
#endif
static int encode_null_callback(const char *buffer, size_t size, void *data)
{
static int encode_null_callback(const char *buffer, size_t size, void *data) {
(void)buffer;
(void)size;
(void)data;
return 0;
}
static void encode_null()
{
static void encode_null() {
if (json_dumps(NULL, JSON_ENCODE_ANY) != NULL)
fail("json_dumps didn't fail for NULL");
@ -44,13 +42,12 @@ static void encode_null()
/* Don't test json_dump_file to avoid creating a file */
if(json_dump_callback(NULL, encode_null_callback, NULL, JSON_ENCODE_ANY) != -1)
if (json_dump_callback(NULL, encode_null_callback, NULL, JSON_ENCODE_ANY) !=
-1)
fail("json_dump_callback didn't fail for NULL");
}
static void encode_twice()
{
static void encode_twice() {
/* Encode an empty object/array, add an item, encode again */
json_t *json;
@ -85,8 +82,7 @@ static void encode_twice()
json_decref(json);
}
static void circular_references()
{
static void circular_references() {
/* Construct a JSON object/array with a circular reference:
object: {"a": {"b": {"c": <circular reference to $.a>}}}
@ -135,8 +131,7 @@ static void circular_references()
json_decref(json);
}
static void encode_other_than_array_or_object()
{
static void encode_other_than_array_or_object() {
/* Encoding anything other than array or object should only
* succeed if the JSON_ENCODE_ANY flag is used */
@ -172,36 +167,36 @@ static void encode_other_than_array_or_object()
free(result);
json_decref(json);
}
static void escape_slashes()
{
static void escape_slashes() {
/* Test dump escaping slashes */
json_t *json;
char *result;
json = json_object();
json_object_set_new(json, "url", json_string("https://github.com/akheron/jansson"));
json_object_set_new(json, "url",
json_string("https://github.com/akheron/jansson"));
result = json_dumps(json, 0);
if(!result || strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}"))
if (!result ||
strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}"))
fail("json_dumps failed to not escape slashes");
free(result);
result = json_dumps(json, JSON_ESCAPE_SLASH);
if(!result || strcmp(result, "{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}"))
if (!result ||
strcmp(result,
"{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}"))
fail("json_dumps failed to escape slashes");
free(result);
json_decref(json);
}
static void encode_nul_byte()
{
static void encode_nul_byte() {
json_t *json;
char *result;
@ -214,8 +209,7 @@ static void encode_nul_byte()
json_decref(json);
}
static void dump_file()
{
static void dump_file() {
json_t *json;
int result;
@ -232,8 +226,7 @@ static void dump_file()
remove("json_dump_file.json");
}
static void dumpb()
{
static void dumpb() {
char buf[2];
json_t *obj;
size_t size;
@ -254,8 +247,7 @@ static void dumpb()
json_decref(obj);
}
static void dumpfd()
{
static void dumpfd() {
#ifdef HAVE_UNISTD_H
int fds[2] = {-1, -1};
json_t *a, *b;
@ -282,15 +274,9 @@ static void dumpfd()
#endif
}
static void embed()
{
static const char *plains[] = {
"{\"bar\":[],\"foo\":{}}",
"[[],{}]",
"{}",
"[]",
NULL
};
static void embed() {
static const char *plains[] = {"{\"bar\":[],\"foo\":{}}", "[[],{}]", "{}",
"[]", NULL};
size_t i;
@ -315,8 +301,7 @@ static void embed()
}
}
static void run_tests()
{
static void run_tests() {
encode_null();
encode_twice();
circular_references();

View File

@ -5,10 +5,10 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <jansson.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include <jansson.h>
#include <stdlib.h>
#include <string.h>
struct my_sink {
char *buf;
@ -26,11 +26,11 @@ static int my_writer(const char *buffer, size_t len, void *data) {
return 0;
}
static void run_tests()
{
static void run_tests() {
struct my_sink s;
json_t *json;
const char str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
const char str[] =
"[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
char *dumped_to_string;
json = json_loads(str, 0, NULL);
@ -64,7 +64,8 @@ static void run_tests()
json_decref(json);
free(dumped_to_string);
free(s.buf);
fail("json_dump_callback and json_dumps did not produce identical output");
fail("json_dump_callback and json_dumps did not produce identical "
"output");
}
s.off = 1;
@ -72,7 +73,8 @@ static void run_tests()
json_decref(json);
free(dumped_to_string);
free(s.buf);
fail("json_dump_callback succeeded on a short buffer when it should have failed");
fail("json_dump_callback succeeded on a short buffer when it should "
"have failed");
}
json_decref(json);

View File

@ -5,11 +5,10 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <jansson.h>
#include "util.h"
#include <jansson.h>
static void test_equal_simple()
{
static void test_equal_simple() {
json_t *value1, *value2;
if (json_equal(NULL, NULL))
@ -86,8 +85,7 @@ static void test_equal_simple()
json_decref(value2);
}
static void test_equal_array()
{
static void test_equal_array() {
json_t *array1, *array2;
array1 = json_array();
@ -119,8 +117,7 @@ static void test_equal_array()
json_decref(array2);
}
static void test_equal_object()
{
static void test_equal_object() {
json_t *object1, *object2;
object1 = json_object();
@ -157,8 +154,7 @@ static void test_equal_object()
json_decref(object2);
}
static void test_equal_complex()
{
static void test_equal_complex() {
json_t *value1, *value2, *value3;
const char *complex_json =
@ -182,13 +178,15 @@ static void test_equal_complex()
if (!json_equal(value1, value2))
fail("json_equal fails for two equal objects");
json_array_set_new(json_object_get(json_object_get(value2, "object"),
"array-in-object"), 1, json_false());
json_array_set_new(
json_object_get(json_object_get(value2, "object"), "array-in-object"),
1, json_false());
if (json_equal(value1, value2))
fail("json_equal fails for two inequal objects");
json_object_set_new(json_object_get(json_object_get(value3, "object"),
"object-in-object"), "foo", json_string("baz"));
json_object_set_new(
json_object_get(json_object_get(value3, "object"), "object-in-object"),
"foo", json_string("baz"));
if (json_equal(value1, value3))
fail("json_equal fails for two inequal objects");
@ -197,8 +195,7 @@ static void test_equal_complex()
json_decref(value3);
}
static void run_tests()
{
static void run_tests() {
test_equal_simple();
test_equal_array();
test_equal_object();

View File

@ -5,12 +5,11 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "util.h"
#include <jansson.h>
#include <string.h>
#include "util.h"
static void file_not_found()
{
static void file_not_found() {
json_t *json;
json_error_t error;
char *pos;
@ -30,7 +29,8 @@ static void file_not_found()
*pos = '\0';
if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json") != 0)
if (strcmp(error.text, "unable to open /path/to/nonexistent/file.json") !=
0)
fail("json_load_file returned an invalid error message");
if (json_error_code(&error) != json_error_cannot_open_file)
fail("json_load_file returned an invalid error code");
@ -40,7 +40,9 @@ static void very_long_file_name() {
json_t *json;
json_error_t error;
json = json_load_file("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0, &error);
json = json_load_file("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
0, &error);
if (json)
fail("json_load_file returned non-NULL for a nonexistent file");
if (error.line != -1)
@ -52,17 +54,16 @@ static void very_long_file_name() {
fail("error code was set incorrectly");
}
static void reject_duplicates()
{
static void reject_duplicates() {
json_error_t error;
if (json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
fail("json_loads did not detect a duplicate key");
check_error(json_error_duplicate_key, "duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
check_error(json_error_duplicate_key, "duplicate object key near '\"foo\"'",
"<string>", 1, 16, 16);
}
static void disable_eof_check()
{
static void disable_eof_check() {
json_error_t error;
json_t *json;
@ -70,7 +71,8 @@ static void disable_eof_check()
if (json_loads(text, 0, &error))
fail("json_loads did not detect garbage after JSON text");
check_error(json_error_end_of_input_expected, "end of file expected near 'garbage'", "<string>", 1, 18, 18);
check_error(json_error_end_of_input_expected,
"end of file expected near 'garbage'", "<string>", 1, 18, 18);
json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
if (!json)
@ -79,8 +81,7 @@ static void disable_eof_check()
json_decref(json);
}
static void decode_any()
{
static void decode_any() {
json_t *json;
json_error_t error;
@ -105,8 +106,7 @@ static void decode_any()
json_decref(json);
}
static void decode_int_as_real()
{
static void decode_int_as_real() {
json_t *json;
json_error_t error;
@ -129,7 +129,8 @@ static void decode_int_as_real()
json = json_loads(imprecise, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY,
&error);
if (!json || !json_is_real(json) || expected != (json_int_t)json_real_value(json))
if (!json || !json_is_real(json) ||
expected != (json_int_t)json_real_value(json))
fail("json_load decode int as real failed - expected imprecision");
json_decref(json);
#endif
@ -145,11 +146,9 @@ static void decode_int_as_real()
json_error_code(&error) != json_error_numeric_overflow)
fail("json_load decode int as real failed - expected overflow");
json_decref(json);
}
static void allow_nul()
{
static void allow_nul() {
const char *text = "\"nul byte \\u0000 in string\"";
const char *expected = "nul byte \0 in string";
size_t len = 20;
@ -168,8 +167,7 @@ static void allow_nul()
json_decref(json);
}
static void load_wrong_args()
{
static void load_wrong_args() {
json_t *json;
json_error_t error;
@ -194,8 +192,7 @@ static void load_wrong_args()
fail("json_load_file should return NULL if the first argument is NULL");
}
static void position()
{
static void position() {
json_t *json;
size_t flags = JSON_DISABLE_EOF_CHECK;
json_error_t error;
@ -211,8 +208,7 @@ static void position()
json_decref(json);
}
static void error_code()
{
static void error_code() {
json_error_t error;
json_t *json = json_loads("[123] garbage", 0, &error);
if (json != NULL)
@ -231,8 +227,7 @@ static void error_code()
fail("json_loads returned incorrect error code");
}
static void run_tests()
{
static void run_tests() {
file_not_found();
very_long_file_name();
reject_duplicates();

View File

@ -5,10 +5,10 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <jansson.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include <jansson.h>
#include <stdlib.h>
#include <string.h>
struct my_source {
const char *buf;
@ -16,10 +16,10 @@ struct my_source {
size_t cap;
};
static const char my_str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
static const char my_str[] =
"[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
static size_t greedy_reader(void *buf, size_t buflen, void *arg)
{
static size_t greedy_reader(void *buf, size_t buflen, void *arg) {
struct my_source *s = arg;
if (buflen > s->cap - s->off)
buflen = s->cap - s->off;
@ -32,8 +32,7 @@ static size_t greedy_reader(void *buf, size_t buflen, void *arg)
}
}
static void run_tests()
{
static void run_tests() {
struct my_source s;
json_t *json;
json_error_t error;
@ -55,21 +54,25 @@ static void run_tests()
json = json_load_callback(greedy_reader, &s, 0, &error);
if (json) {
json_decref(json);
fail("json_load_callback should have failed on an incomplete stream, but it didn't");
fail("json_load_callback should have failed on an incomplete stream, "
"but it didn't");
}
if (strcmp(error.source, "<callback>") != 0) {
fail("json_load_callback returned an invalid error source");
}
if (strcmp(error.text, "']' expected near end of file") != 0) {
fail("json_load_callback returned an invalid error message for an unclosed top-level array");
fail("json_load_callback returned an invalid error message for an "
"unclosed top-level array");
}
json = json_load_callback(NULL, NULL, 0, &error);
if (json) {
json_decref(json);
fail("json_load_callback should have failed on NULL load callback, but it didn't");
fail("json_load_callback should have failed on NULL load callback, but "
"it didn't");
}
if (strcmp(error.text, "wrong arguments") != 0) {
fail("json_load_callback returned an invalid error message for a NULL load callback");
fail("json_load_callback returned an invalid error message for a NULL "
"load callback");
}
}

View File

@ -5,12 +5,11 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "util.h"
#include <jansson.h>
#include <string.h>
#include "util.h"
static void run_tests()
{
static void run_tests() {
json_t *json;
json_error_t error;
const char str[] = "[\"A\", {\"B\": \"C\"}, 1, 2, 3]garbage";
@ -25,12 +24,14 @@ static void run_tests()
json = json_loadb(str, len - 1, 0, &error);
if (json) {
json_decref(json);
fail("json_loadb should have failed on an incomplete buffer, but it didn't");
fail("json_loadb should have failed on an incomplete buffer, but it "
"didn't");
}
if (error.line != 1) {
fail("json_loadb returned an invalid line number on fail");
}
if (strcmp(error.text, "']' expected near end of file") != 0) {
fail("json_loadb returned an invalid error message for an unclosed top-level array");
fail("json_loadb returned an invalid error message for an unclosed "
"top-level array");
}
}

View File

@ -1,5 +1,5 @@
#include <string.h>
#include <jansson.h>
#include <string.h>
#include "util.h"
@ -8,29 +8,21 @@ static int free_called = 0;
static size_t malloc_used = 0;
/* helpers */
static void create_and_free_complex_object()
{
static void create_and_free_complex_object() {
json_t *obj;
obj = json_pack("{s:i,s:n,s:b,s:b,s:{s:s},s:[i,i,i]}",
"foo", 42,
"bar",
"baz", 1,
"qux", 0,
"alice", "bar", "baz",
"bob", 9, 8, 7);
obj = json_pack("{s:i,s:n,s:b,s:b,s:{s:s},s:[i,i,i]}", "foo", 42, "bar",
"baz", 1, "qux", 0, "alice", "bar", "baz", "bob", 9, 8, 7);
json_decref(obj);
}
static void create_and_free_object_with_oom()
{
static void create_and_free_object_with_oom() {
int i;
char key[4];
json_t *obj = json_object();
for (i = 0; i < 10; i++)
{
for (i = 0; i < 10; i++) {
snprintf(key, sizeof key, "%d", i);
json_object_set_new(obj, key, json_integer(i));
}
@ -38,20 +30,17 @@ static void create_and_free_object_with_oom()
json_decref(obj);
}
static void *my_malloc(size_t size)
{
static void *my_malloc(size_t size) {
malloc_called = 1;
return malloc(size);
}
static void my_free(void *ptr)
{
static void my_free(void *ptr) {
free_called = 1;
free(ptr);
}
static void test_simple()
{
static void test_simple() {
json_malloc_t mfunc = NULL;
json_free_t ffunc = NULL;
@ -59,14 +48,12 @@ static void test_simple()
json_get_alloc_funcs(&mfunc, &ffunc);
create_and_free_complex_object();
if (malloc_called != 1 || free_called != 1
|| mfunc != my_malloc || ffunc != my_free)
if (malloc_called != 1 || free_called != 1 || mfunc != my_malloc ||
ffunc != my_free)
fail("Custom allocation failed");
}
static void *oom_malloc(size_t size)
{
static void *oom_malloc(size_t size) {
if (malloc_used + size > 800)
return NULL;
@ -74,14 +61,12 @@ static void *oom_malloc(size_t size)
return malloc(size);
}
static void oom_free(void *ptr)
{
static void oom_free(void *ptr) {
free_called++;
free(ptr);
}
static void test_oom()
{
static void test_oom() {
free_called = 0;
json_set_alloc_funcs(oom_malloc, oom_free);
create_and_free_object_with_oom();
@ -90,23 +75,20 @@ static void test_oom()
fail("Allocation with OOM failed");
}
/*
Test the secure memory functions code given in the API reference
documentation, but by using plain memset instead of
guaranteed_memset().
*/
static void *secure_malloc(size_t size)
{
static void *secure_malloc(size_t size) {
/* Store the memory area size in the beginning of the block */
void *ptr = malloc(size + 8);
*((size_t *)ptr) = size;
return (char *)ptr + 8;
}
static void secure_free(void *ptr)
{
static void secure_free(void *ptr) {
size_t size;
ptr = (char *)ptr - 8;
@ -116,20 +98,17 @@ static void secure_free(void *ptr)
free(ptr);
}
static void test_secure_funcs(void)
{
static void test_secure_funcs(void) {
json_set_alloc_funcs(secure_malloc, secure_free);
create_and_free_complex_object();
}
static void test_bad_args(void)
{
static void test_bad_args(void) {
/* The result of this test is not crashing. */
json_get_alloc_funcs(NULL, NULL);
}
static void run_tests()
{
static void run_tests() {
test_simple();
test_secure_funcs();
test_oom();

View File

@ -5,9 +5,9 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <math.h>
#include <jansson.h>
#include "util.h"
#include <jansson.h>
#include <math.h>
#ifdef INFINITY
// This test triggers "warning C4756: overflow in constant arithmetic"
@ -17,8 +17,7 @@
#pragma warning(push)
#pragma warning(disable : 4756)
#endif
static void test_inifity()
{
static void test_inifity() {
json_t *real = json_real(INFINITY);
if (real != NULL)
fail("could construct a real from Inf");
@ -37,8 +36,7 @@ static void test_inifity()
}
#endif // INFINITY
static void test_bad_args(void)
{
static void test_bad_args(void) {
json_t *txt = json_string("test");
if (json_integer_value(NULL) != 0)
@ -72,8 +70,7 @@ static void test_bad_args(void)
json_decref(txt);
}
static void run_tests()
{
static void run_tests() {
json_t *integer, *real;
json_int_t i;
double d;

View File

@ -5,12 +5,11 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "util.h"
#include <jansson.h>
#include <string.h>
#include "util.h"
static void test_clear()
{
static void test_clear() {
json_t *object, *ten;
object = json_object();
@ -24,8 +23,7 @@ static void test_clear()
if (json_object_set(object, "a", ten) ||
json_object_set(object, "b", ten) ||
json_object_set(object, "c", ten) ||
json_object_set(object, "d", ten) ||
json_object_set(object, "e", ten))
json_object_set(object, "d", ten) || json_object_set(object, "e", ten))
fail("unable to set value");
if (json_object_size(object) != 5)
@ -40,8 +38,7 @@ static void test_clear()
json_decref(object);
}
static void test_update()
{
static void test_update() {
json_t *object, *other, *nine, *ten;
object = json_object();
@ -55,7 +52,6 @@ static void test_update()
if (!nine || !ten)
fail("unable to create integer");
/* update an empty object with an empty object */
if (json_object_update(object, other))
@ -67,13 +63,10 @@ static void test_update()
if (json_object_size(other) != 0)
fail("invalid size for updater after update");
/* update an empty object with a nonempty object */
if(json_object_set(other, "a", ten) ||
json_object_set(other, "b", ten) ||
json_object_set(other, "c", ten) ||
json_object_set(other, "d", ten) ||
if (json_object_set(other, "a", ten) || json_object_set(other, "b", ten) ||
json_object_set(other, "c", ten) || json_object_set(other, "d", ten) ||
json_object_set(other, "e", ten))
fail("unable to set value");
@ -90,7 +83,6 @@ static void test_update()
json_object_get(object, "e") != ten)
fail("update works incorrectly");
/* perform the same update again */
if (json_object_update(object, other))
@ -106,7 +98,6 @@ static void test_update()
json_object_get(object, "e") != ten)
fail("update works incorrectly");
/* update a nonempty object with a nonempty object with both old
and new keys */
@ -116,8 +107,7 @@ static void test_update()
if (json_object_set(other, "a", nine) ||
json_object_set(other, "b", nine) ||
json_object_set(other, "f", nine) ||
json_object_set(other, "g", nine) ||
json_object_set(other, "h", nine))
json_object_set(other, "g", nine) || json_object_set(other, "h", nine))
fail("unable to set value");
if (json_object_update(object, other))
@ -137,25 +127,21 @@ static void test_update()
if (json_object_clear(object))
fail("clear failed");
if(json_object_set(object, "a", ten) ||
json_object_set(object, "b", ten) ||
json_object_set(object, "c", ten) ||
json_object_set(object, "d", ten) ||
if (json_object_set(object, "a", ten) || json_object_set(object, "b", ten) ||
json_object_set(object, "c", ten) || json_object_set(object, "d", ten) ||
json_object_set(object, "e", ten))
fail("unable to set value");
if(json_object_update_new(object, json_pack("{s:O, s:O, s:O}", "b", nine, "f", nine, "g", nine)))
if (json_object_update_new(
object, json_pack("{s:O, s:O, s:O}", "b", nine, "f", nine, "g", nine)))
fail("unable to update_new a nonempty object");
if (json_object_size(object) != 7)
fail("invalid size after update_new");
if(json_object_get(object, "a") != ten ||
json_object_get(object, "b") != nine ||
json_object_get(object, "c") != ten ||
json_object_get(object, "d") != ten ||
json_object_get(object, "e") != ten ||
json_object_get(object, "f") != nine ||
if (json_object_get(object, "a") != ten || json_object_get(object, "b") != nine ||
json_object_get(object, "c") != ten || json_object_get(object, "d") != ten ||
json_object_get(object, "e") != ten || json_object_get(object, "f") != nine ||
json_object_get(object, "g") != nine)
fail("update_new works incorrectly");
@ -165,8 +151,7 @@ static void test_update()
json_decref(object);
}
static void test_set_many_keys()
{
static void test_set_many_keys() {
json_t *object, *value;
const char *keys = "abcdefghijklmnopqrstuvwxyz";
char buf[2];
@ -191,8 +176,7 @@ static void test_set_many_keys()
json_decref(value);
}
static void test_conditional_updates()
{
static void test_conditional_updates() {
json_t *object, *other;
object = json_pack("{sisi}", "foo", 1, "bar", 2);
@ -270,8 +254,7 @@ static void test_conditional_updates()
json_decref(other);
}
static void test_recursive_updates()
{
static void test_recursive_updates() {
json_t *invalid, *object, *other, *barBefore, *barAfter;
invalid = json_integer(42);
@ -318,7 +301,8 @@ static void test_recursive_updates()
if (!json_object_get(object, "foo"))
fail("json_object_update_recursive removed existing key");
if(json_integer_value(json_object_get(json_object_get(object, "bar"), "baz")) != 3)
if (json_integer_value(
json_object_get(json_object_get(object, "bar"), "baz")) != 3)
fail("json_object_update_recursive failed to update nested value");
barAfter = json_object_get(object, "bar");
@ -334,14 +318,14 @@ static void test_recursive_updates()
/* check circular reference */
object = json_pack("{s{s{s{si}}}}", "foo", "bar", "baz", "xxx", 2);
other = json_pack("{s{s{si}}}", "foo", "bar", "baz", 2);
json_object_set(json_object_get(json_object_get(other, "foo"), "bar"), "baz",
json_object_get(other, "foo"));
json_object_set(json_object_get(json_object_get(other, "foo"), "bar"),
"baz", json_object_get(other, "foo"));
if (!json_object_update_recursive(object, other))
fail("json_object_update_recursive update a circular reference!");
json_object_set_new(json_object_get(json_object_get(other, "foo"), "bar"), "baz",
json_integer(1));
json_object_set_new(json_object_get(json_object_get(other, "foo"), "bar"),
"baz", json_integer(1));
if (json_object_update_recursive(object, other))
fail("json_object_update_recursive failed!");
@ -350,8 +334,7 @@ static void test_recursive_updates()
json_decref(other);
}
static void test_circular()
{
static void test_circular() {
json_t *object1, *object2;
object1 = json_object();
@ -378,8 +361,7 @@ static void test_circular()
json_decref(object1);
}
static void test_set_nocheck()
{
static void test_set_nocheck() {
json_t *object, *string;
object = json_object();
@ -416,8 +398,7 @@ static void test_set_nocheck()
json_decref(object);
}
static void test_iterators()
{
static void test_iterators() {
json_t *object, *foo, *bar, *baz;
void *iter;
@ -438,8 +419,7 @@ static void test_iterators()
fail("able to increment a NULL iterator");
if (json_object_set(object, "a", foo) ||
json_object_set(object, "b", bar) ||
json_object_set(object, "c", baz))
json_object_set(object, "b", bar) || json_object_set(object, "c", baz))
fail("unable to populate object");
iter = json_object_iter(object);
@ -497,8 +477,7 @@ static void test_iterators()
json_decref(baz);
}
static void test_misc()
{
static void test_misc() {
json_t *object, *string, *other_string, *value;
object = json_object();
@ -571,7 +550,6 @@ static void test_misc()
if (json_object_del(object, "lp"))
fail("unable to delete an existing key");
/* add many keys to initiate rehashing */
if (json_object_set(object, "a", string))
@ -592,7 +570,6 @@ static void test_misc()
if (json_object_set(object, "e", string))
fail("unable to set value");
if (json_object_set_new(object, "foo", json_integer(123)))
fail("unable to set new value");
@ -611,12 +588,12 @@ static void test_misc()
json_decref(object);
}
static void test_preserve_order()
{
static void test_preserve_order() {
json_t *object;
char *result;
const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": 3, \"sit amet\": 5, \"helicopter\": 7}";
const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": "
"3, \"sit amet\": 5, \"helicopter\": 7}";
object = json_object();
@ -646,8 +623,7 @@ static void test_preserve_order()
json_decref(object);
}
static void test_object_foreach()
{
static void test_object_foreach() {
const char *key;
json_t *object1, *object2, *value;
@ -664,8 +640,7 @@ static void test_object_foreach()
json_decref(object2);
}
static void test_object_foreach_safe()
{
static void test_object_foreach_safe() {
const char *key;
void *tmp;
json_t *object, *value;
@ -682,8 +657,7 @@ static void test_object_foreach_safe()
json_decref(object);
}
static void test_bad_args(void)
{
static void test_bad_args(void) {
json_t *obj = json_object();
json_t *num = json_integer(1);
void *iter;
@ -711,11 +685,14 @@ static void test_bad_args(void)
fail("json_object_get with NULL key returned non-NULL");
if (!json_object_set_new_nocheck(NULL, "test", json_null()))
fail("json_object_set_new_nocheck with non-object argument did not return error");
fail("json_object_set_new_nocheck with non-object argument did not "
"return error");
if (!json_object_set_new_nocheck(num, "test", json_null()))
fail("json_object_set_new_nocheck with non-object argument did not return error");
fail("json_object_set_new_nocheck with non-object argument did not "
"return error");
if (!json_object_set_new_nocheck(obj, "test", json_incref(obj)))
fail("json_object_set_new_nocheck with object == value did not return error");
fail("json_object_set_new_nocheck with object == value did not return "
"error");
if (!json_object_set_new_nocheck(obj, NULL, json_object()))
fail("json_object_set_new_nocheck with NULL key did not return error");
@ -732,31 +709,43 @@ static void test_bad_args(void)
fail("json_object_clear with non-object argument did not return error");
if (!json_object_update(NULL, obj))
fail("json_object_update with non-object first argument did not return error");
fail("json_object_update with non-object first argument did not return "
"error");
if (!json_object_update(num, obj))
fail("json_object_update with non-object first argument did not return error");
fail("json_object_update with non-object first argument did not return "
"error");
if (!json_object_update(obj, NULL))
fail("json_object_update with non-object second argument did not return error");
fail("json_object_update with non-object second argument did not "
"return error");
if (!json_object_update(obj, num))
fail("json_object_update with non-object second argument did not return error");
fail("json_object_update with non-object second argument did not "
"return error");
if (!json_object_update_existing(NULL, obj))
fail("json_object_update_existing with non-object first argument did not return error");
fail("json_object_update_existing with non-object first argument did "
"not return error");
if (!json_object_update_existing(num, obj))
fail("json_object_update_existing with non-object first argument did not return error");
fail("json_object_update_existing with non-object first argument did "
"not return error");
if (!json_object_update_existing(obj, NULL))
fail("json_object_update_existing with non-object second argument did not return error");
fail("json_object_update_existing with non-object second argument did "
"not return error");
if (!json_object_update_existing(obj, num))
fail("json_object_update_existing with non-object second argument did not return error");
fail("json_object_update_existing with non-object second argument did "
"not return error");
if (!json_object_update_missing(NULL, obj))
fail("json_object_update_missing with non-object first argument did not return error");
fail("json_object_update_missing with non-object first argument did "
"not return error");
if (!json_object_update_missing(num, obj))
fail("json_object_update_missing with non-object first argument did not return error");
fail("json_object_update_missing with non-object first argument did "
"not return error");
if (!json_object_update_missing(obj, NULL))
fail("json_object_update_missing with non-object second argument did not return error");
fail("json_object_update_missing with non-object second argument did "
"not return error");
if (!json_object_update_missing(obj, num))
fail("json_object_update_missing with non-object second argument did not return error");
fail("json_object_update_missing with non-object second argument did "
"not return error");
if (json_object_iter(NULL) != NULL)
fail("json_object_iter with non-object argument returned non-NULL");
@ -773,7 +762,8 @@ static void test_bad_args(void)
if (json_object_iter_next(obj, NULL) != NULL)
fail("json_object_iter_next with NULL iter returned non-NULL");
if (json_object_iter_next(num, iter) != NULL)
fail("json_object_iter_next with non-object argument returned non-NULL");
fail(
"json_object_iter_next with non-object argument returned non-NULL");
if (json_object_iter_key(NULL) != NULL)
fail("json_object_iter_key with NULL iter returned non-NULL");
@ -785,9 +775,11 @@ static void test_bad_args(void)
fail("json_object_iter_value with NULL iter returned non-NULL");
if (!json_object_iter_set_new(NULL, iter, json_incref(num)))
fail("json_object_iter_set_new with non-object argument did not return error");
fail("json_object_iter_set_new with non-object argument did not return "
"error");
if (!json_object_iter_set_new(num, iter, json_incref(num)))
fail("json_object_iter_set_new with non-object argument did not return error");
fail("json_object_iter_set_new with non-object argument did not return "
"error");
if (!json_object_iter_set_new(obj, NULL, json_incref(num)))
fail("json_object_iter_set_new with NULL iter did not return error");
if (!json_object_iter_set_new(obj, iter, NULL))
@ -803,8 +795,7 @@ static void test_bad_args(void)
json_decref(num);
}
static void run_tests()
{
static void run_tests() {
test_misc();
test_clear();
test_update();

View File

@ -12,11 +12,11 @@
#include <jansson_config.h>
#include <string.h>
#include <jansson.h>
#include <stdio.h>
#include <math.h>
#include "util.h"
#include <jansson.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#ifdef INFINITY
// This test triggers "warning C4756: overflow in constant arithmetic"
@ -26,21 +26,23 @@
#pragma warning(push)
#pragma warning(disable : 4756)
#endif
static void test_inifity()
{
static void test_inifity() {
json_error_t error;
if (json_pack_ex(&error, 0, "f", INFINITY))
fail("json_pack infinity incorrectly succeeded");
check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 1, 1);
check_error(json_error_numeric_overflow, "Invalid floating point value",
"<args>", 1, 1, 1);
if (json_pack_ex(&error, 0, "[f]", INFINITY))
fail("json_pack infinity array element incorrectly succeeded");
check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 2, 2);
check_error(json_error_numeric_overflow, "Invalid floating point value",
"<args>", 1, 2, 2);
if (json_pack_ex(&error, 0, "{s:f}", "key", INFINITY))
fail("json_pack infinity object value incorrectly succeeded");
check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 4, 4);
check_error(json_error_numeric_overflow, "Invalid floating point value",
"<args>", 1, 4, 4);
#ifdef _MSC_VER
#pragma warning(pop)
@ -48,8 +50,7 @@ static void test_inifity()
}
#endif // INFINITY
static void run_tests()
{
static void run_tests() {
json_t *value;
int i;
char buffer[4] = {'t', 'e', 's', 't'};
@ -133,12 +134,14 @@ static void run_tests()
/* nullable string concatenation */
if (json_pack_ex(&error, 0, "s?+", "test", "ing"))
fail("json_pack failed to catch invalid format 's?+'");
check_error(json_error_invalid_format, "Cannot use '+' on optional strings", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Cannot use '+' on optional strings",
"<format>", 1, 2, 2);
/* nullable string with integer length */
if (json_pack_ex(&error, 0, "s?#", "test", 4))
fail("json_pack failed to catch invalid format 's?#'");
check_error(json_error_invalid_format, "Cannot use '#' on optional strings", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Cannot use '#' on optional strings",
"<format>", 1, 2, 2);
/* string and length (int) */
value = json_pack("s#", "test asdf", 4);
@ -196,7 +199,8 @@ static void run_tests()
if (!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
fail("json_pack string concatenation and length (size_t) failed");
if (value->refcount != (size_t)1)
fail("json_pack string concatenation and length (size_t) refcount failed");
fail("json_pack string concatenation and length (size_t) refcount "
"failed");
json_decref(value);
/* empty object */
@ -253,7 +257,8 @@ static void run_tests()
if (!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack incref'd nullable object (defined case) failed");
if (value->refcount != (size_t)2)
fail("json_pack incref'd nullable object (defined case) refcount failed");
fail("json_pack incref'd nullable object (defined case) refcount "
"failed");
json_decref(value);
json_decref(value);
@ -295,7 +300,8 @@ static void run_tests()
if (json_pack_ex(&error, 0, "{s:i*}", "a", 1))
fail("json_pack object optional invalid incorrectly succeeded");
check_error(json_error_invalid_format, "Expected format 's', got '*'", "<format>", 1, 5, 5);
check_error(json_error_invalid_format, "Expected format 's', got '*'",
"<format>", 1, 5, 5);
value = json_pack("{s:s*,s:o*,s:O*}", "a", NULL, "b", NULL, "c", NULL);
if (!json_is_object(value) || json_object_size(value) != 0)
@ -304,22 +310,24 @@ static void run_tests()
value = json_pack("{s:s*}", "key", "\xff\xff");
if (value)
fail("json_pack object optional with invalid UTF-8 incorrectly succeeded");
fail("json_pack object optional with invalid UTF-8 incorrectly "
"succeeded");
if (json_pack_ex(&error, 0, "{s: s*#}", "key", "test", 1))
fail("json_pack failed to catch invalid format 's*#'");
check_error(json_error_invalid_format, "Cannot use '#' on optional strings", "<format>", 1, 6, 6);
check_error(json_error_invalid_format, "Cannot use '#' on optional strings",
"<format>", 1, 6, 6);
if (json_pack_ex(&error, 0, "{s: s*+}", "key", "test", "ing"))
fail("json_pack failed to catch invalid format 's*+'");
check_error(json_error_invalid_format, "Cannot use '+' on optional strings", "<format>", 1, 6, 6);
check_error(json_error_invalid_format, "Cannot use '+' on optional strings",
"<format>", 1, 6, 6);
/* simple array */
value = json_pack("[i,i,i]", 0, 1, 2);
if (!json_is_array(value) || json_array_size(value) != 3)
fail("json_pack object failed");
for(i=0; i<3; i++)
{
for (i = 0; i < 3; i++) {
if (!json_is_integer(json_array_get(value, i)) ||
json_integer_value(json_array_get(value, i)) != i)
@ -334,7 +342,8 @@ static void run_tests()
if (json_pack_ex(&error, 0, "[i*]", 1))
fail("json_pack array optional invalid incorrectly succeeded");
check_error(json_error_invalid_format, "Unexpected format character '*'", "<format>", 1, 3, 3);
check_error(json_error_invalid_format, "Unexpected format character '*'",
"<format>", 1, 3, 3);
value = json_pack("[**]", NULL);
if (value)
@ -348,15 +357,18 @@ static void run_tests()
/* Invalid float values */
if (json_pack_ex(&error, 0, "f", NAN))
fail("json_pack NAN incorrectly succeeded");
check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 1, 1);
check_error(json_error_numeric_overflow, "Invalid floating point value",
"<args>", 1, 1, 1);
if (json_pack_ex(&error, 0, "[f]", NAN))
fail("json_pack NAN array element incorrectly succeeded");
check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 2, 2);
check_error(json_error_numeric_overflow, "Invalid floating point value",
"<args>", 1, 2, 2);
if (json_pack_ex(&error, 0, "{s:f}", "key", NAN))
fail("json_pack NAN object value incorrectly succeeded");
check_error(json_error_numeric_overflow, "Invalid floating point value", "<args>", 1, 4, 4);
check_error(json_error_numeric_overflow, "Invalid floating point value",
"<args>", 1, 4, 4);
#endif
#ifdef INFINITY
@ -388,35 +400,42 @@ static void run_tests()
/* newline in format string */
if (json_pack_ex(&error, 0, "{\n\n1"))
fail("json_pack failed to catch invalid format '1'");
check_error(json_error_invalid_format, "Expected format 's', got '1'", "<format>", 3, 1, 4);
check_error(json_error_invalid_format, "Expected format 's', got '1'",
"<format>", 3, 1, 4);
/* mismatched open/close array/object */
if (json_pack_ex(&error, 0, "[}"))
fail("json_pack failed to catch mismatched '}'");
check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected format character '}'",
"<format>", 1, 2, 2);
if (json_pack_ex(&error, 0, "{]"))
fail("json_pack failed to catch mismatched ']'");
check_error(json_error_invalid_format, "Expected format 's', got ']'", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Expected format 's', got ']'",
"<format>", 1, 2, 2);
/* missing close array */
if (json_pack_ex(&error, 0, "["))
fail("json_pack failed to catch missing ']'");
check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected end of format string",
"<format>", 1, 2, 2);
/* missing close object */
if (json_pack_ex(&error, 0, "{"))
fail("json_pack failed to catch missing '}'");
check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected end of format string",
"<format>", 1, 2, 2);
/* garbage after format string */
if (json_pack_ex(&error, 0, "[i]a", 42))
fail("json_pack failed to catch garbage after format string");
check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 4, 4);
check_error(json_error_invalid_format, "Garbage after format string",
"<format>", 1, 4, 4);
if (json_pack_ex(&error, 0, "ia", 42))
fail("json_pack failed to catch garbage after format string");
check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Garbage after format string",
"<format>", 1, 2, 2);
/* NULL string */
if (json_pack_ex(&error, 0, "s", NULL))
@ -426,17 +445,20 @@ static void run_tests()
/* + on its own */
if (json_pack_ex(&error, 0, "+", NULL))
fail("json_pack failed to a lone +");
check_error(json_error_invalid_format, "Unexpected format character '+'", "<format>", 1, 1, 1);
check_error(json_error_invalid_format, "Unexpected format character '+'",
"<format>", 1, 1, 1);
/* Empty format */
if (json_pack_ex(&error, 0, ""))
fail("json_pack failed to catch empty format string");
check_error(json_error_invalid_argument, "NULL or empty format string", "<format>", -1, -1, 0);
check_error(json_error_invalid_argument, "NULL or empty format string",
"<format>", -1, -1, 0);
/* NULL format */
if (json_pack_ex(&error, 0, NULL))
fail("json_pack failed to catch NULL format string");
check_error(json_error_invalid_argument, "NULL or empty format string", "<format>", -1, -1, 0);
check_error(json_error_invalid_argument, "NULL or empty format string",
"<format>", -1, -1, 0);
/* NULL key */
if (json_pack_ex(&error, 0, "{s:i}", NULL, 1))
@ -445,7 +467,8 @@ static void run_tests()
/* NULL value followed by object still steals the object's ref */
value = json_incref(json_object());
if(json_pack_ex(&error, 0, "{s:s,s:o}", "badnull", NULL, "dontleak", value))
if (json_pack_ex(&error, 0, "{s:s,s:o}", "badnull", NULL, "dontleak",
value))
fail("json_pack failed to catch NULL value");
check_error(json_error_null_value, "NULL string", "<args>", 1, 4, 4);
if (value->refcount != (size_t)1)
@ -455,42 +478,52 @@ static void run_tests()
/* More complicated checks for row/columns */
if (json_pack_ex(&error, 0, "{ {}: s }", "foo"))
fail("json_pack failed to catch object as key");
check_error(json_error_invalid_format, "Expected format 's', got '{'", "<format>", 1, 3, 3);
check_error(json_error_invalid_format, "Expected format 's', got '{'",
"<format>", 1, 3, 3);
/* Complex object */
if (json_pack_ex(&error, 0, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
fail("json_pack failed to catch missing ]");
check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 19, 19);
check_error(json_error_invalid_format, "Unexpected format character '}'",
"<format>", 1, 19, 19);
/* Complex array */
if (json_pack_ex(&error, 0, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
fail("json_pack failed to catch extra }");
check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 21, 21);
check_error(json_error_invalid_format, "Unexpected format character '}'",
"<format>", 1, 21, 21);
/* Invalid UTF-8 in object key */
if (json_pack_ex(&error, 0, "{s:i}", "\xff\xff", 42))
fail("json_pack failed to catch invalid UTF-8 in an object key");
check_error(json_error_invalid_utf8, "Invalid UTF-8 object key", "<args>", 1, 2, 2);
check_error(json_error_invalid_utf8, "Invalid UTF-8 object key", "<args>",
1, 2, 2);
/* Invalid UTF-8 in a string */
if (json_pack_ex(&error, 0, "{s:s}", "foo", "\xff\xff"))
fail("json_pack failed to catch invalid UTF-8 in a string");
check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 4, 4);
check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 4,
4);
/* Invalid UTF-8 in an optional '?' string */
if (json_pack_ex(&error, 0, "{s:s?}", "foo", "\xff\xff"))
fail("json_pack failed to catch invalid UTF-8 in an optional '?' string");
check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 5, 5);
fail("json_pack failed to catch invalid UTF-8 in an optional '?' "
"string");
check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 5,
5);
/* Invalid UTF-8 in an optional '*' string */
if (json_pack_ex(&error, 0, "{s:s*}", "foo", "\xff\xff"))
fail("json_pack failed to catch invalid UTF-8 in an optional '*' string");
check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 5, 5);
fail("json_pack failed to catch invalid UTF-8 in an optional '*' "
"string");
check_error(json_error_invalid_utf8, "Invalid UTF-8 string", "<args>", 1, 5,
5);
/* Invalid UTF-8 in a concatenated key */
if (json_pack_ex(&error, 0, "{s+:i}", "\xff\xff", "concat", 42))
fail("json_pack failed to catch invalid UTF-8 in an object key");
check_error(json_error_invalid_utf8, "Invalid UTF-8 object key", "<args>", 1, 3, 3);
check_error(json_error_invalid_utf8, "Invalid UTF-8 object key", "<args>",
1, 3, 3);
if (json_pack_ex(&error, 0, "{s:o}", "foo", NULL))
fail("json_pack failed to catch nullable object");
@ -506,11 +539,13 @@ static void run_tests()
if (json_pack_ex(&error, 0, "[1s", "Hi"))
fail("json_pack failed to catch invalid format");
check_error(json_error_invalid_format, "Unexpected format character '1'", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected format character '1'",
"<format>", 1, 2, 2);
if (json_pack_ex(&error, 0, "[1s+", "Hi", "ya"))
fail("json_pack failed to catch invalid format");
check_error(json_error_invalid_format, "Unexpected format character '1'", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected format character '1'",
"<format>", 1, 2, 2);
if (json_pack_ex(&error, 0, "[so]", NULL, json_object()))
fail("json_pack failed to catch NULL value");

View File

@ -5,12 +5,11 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include <jansson.h>
#include "util.h"
#include <jansson.h>
#include <string.h>
static void test_bad_args(void)
{
static void test_bad_args(void) {
json_t *num = json_integer(1);
json_t *txt = json_string("test");
@ -60,8 +59,7 @@ static void test_bad_args(void)
}
/* Call the simple functions not covered by other tests of the public API */
static void run_tests()
{
static void run_tests() {
json_t *value;
value = json_boolean(1);
@ -81,7 +79,6 @@ static void run_tests()
fail("json_boolean_value failed");
json_decref(value);
value = json_integer(1);
if (json_typeof(value) != JSON_INTEGER)
fail("json_typeof failed");
@ -118,7 +115,6 @@ static void run_tests()
json_decref(value);
value = json_string("foo");
if (!value)
fail("json_string failed");
@ -194,7 +190,6 @@ static void run_tests()
json_decref(value);
value = json_integer(123);
if (!value)
fail("json_integer failed");

View File

@ -1,7 +1,6 @@
#include <string.h>
#include <jansson.h>
#include "util.h"
#include <jansson.h>
#include <string.h>
static void test_sprintf() {
json_t *s = json_sprintf("foo bar %d", 42);
@ -27,8 +26,4 @@ static void test_sprintf() {
fail("json_sprintf unexpected success with invalid UTF");
}
static void run_tests()
{
test_sprintf();
}
static void run_tests() { test_sprintf(); }

View File

@ -6,13 +6,12 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include "util.h"
#include <jansson.h>
#include <stdio.h>
#include "util.h"
#include <string.h>
static void run_tests()
{
static void run_tests() {
json_t *j, *j2;
int i1, i2, i3;
json_int_t I1;
@ -144,7 +143,8 @@ static void run_tests()
j = json_integer(42);
if (!json_unpack_ex(j, &error, 0, "z"))
fail("json_unpack succeeded with invalid format character");
check_error(json_error_invalid_format, "Unexpected format character 'z'", "<format>", 1, 1, 1);
check_error(json_error_invalid_format, "Unexpected format character 'z'",
"<format>", 1, 1, 1);
if (!json_unpack_ex(NULL, &error, 0, "[i]"))
fail("json_unpack succeeded with NULL root");
@ -155,54 +155,62 @@ static void run_tests()
j = json_pack("[]");
if (!json_unpack_ex(j, &error, 0, "[}"))
fail("json_unpack failed to catch mismatched ']'");
check_error(json_error_invalid_format, "Unexpected format character '}'", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected format character '}'",
"<format>", 1, 2, 2);
json_decref(j);
j = json_pack("{}");
if (!json_unpack_ex(j, &error, 0, "{]"))
fail("json_unpack failed to catch mismatched '}'");
check_error(json_error_invalid_format, "Expected format 's', got ']'", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Expected format 's', got ']'",
"<format>", 1, 2, 2);
json_decref(j);
/* missing close array */
j = json_pack("[]");
if (!json_unpack_ex(j, &error, 0, "["))
fail("json_unpack failed to catch missing ']'");
check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected end of format string",
"<format>", 1, 2, 2);
json_decref(j);
/* missing close object */
j = json_pack("{}");
if (!json_unpack_ex(j, &error, 0, "{"))
fail("json_unpack failed to catch missing '}'");
check_error(json_error_invalid_format, "Unexpected end of format string", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Unexpected end of format string",
"<format>", 1, 2, 2);
json_decref(j);
/* garbage after format string */
j = json_pack("[i]", 42);
if (!json_unpack_ex(j, &error, 0, "[i]a", &i1))
fail("json_unpack failed to catch garbage after format string");
check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 4, 4);
check_error(json_error_invalid_format, "Garbage after format string",
"<format>", 1, 4, 4);
json_decref(j);
j = json_integer(12345);
if (!json_unpack_ex(j, &error, 0, "ia", &i1))
fail("json_unpack failed to catch garbage after format string");
check_error(json_error_invalid_format, "Garbage after format string", "<format>", 1, 2, 2);
check_error(json_error_invalid_format, "Garbage after format string",
"<format>", 1, 2, 2);
json_decref(j);
/* NULL format string */
j = json_pack("[]");
if (!json_unpack_ex(j, &error, 0, NULL))
fail("json_unpack failed to catch null format string");
check_error(json_error_invalid_argument, "NULL or empty format string", "<format>", -1, -1, 0);
check_error(json_error_invalid_argument, "NULL or empty format string",
"<format>", -1, -1, 0);
json_decref(j);
/* NULL string pointer */
j = json_string("foobie");
if (!json_unpack_ex(j, &error, 0, "s", NULL))
fail("json_unpack failed to catch null string pointer");
check_error(json_error_null_value, "NULL string argument", "<args>", 1, 1, 1);
check_error(json_error_null_value, "NULL string argument", "<args>", 1, 1,
1);
json_decref(j);
/* invalid types */
@ -210,39 +218,48 @@ static void run_tests()
j2 = json_string("foo");
if (!json_unpack_ex(j, &error, 0, "s"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected string, got integer", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected string, got integer",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j, &error, 0, "n"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected null, got integer", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected null, got integer",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j, &error, 0, "b"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected true or false, got integer", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected true or false, got integer",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j2, &error, 0, "i"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected integer, got string", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected integer, got string",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j2, &error, 0, "I"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected integer, got string", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected integer, got string",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j, &error, 0, "f"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected real, got integer", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected real, got integer",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j2, &error, 0, "F"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected real or integer, got string", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected real or integer, got string",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j, &error, 0, "[i]"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected array, got integer", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected array, got integer",
"<validation>", 1, 1, 1);
if (!json_unpack_ex(j, &error, 0, "{si}", "foo"))
fail("json_unpack failed to catch invalid type");
check_error(json_error_wrong_type, "Expected object, got integer", "<validation>", 1, 1, 1);
check_error(json_error_wrong_type, "Expected object, got integer",
"<validation>", 1, 1, 1);
json_decref(j);
json_decref(j2);
@ -251,7 +268,8 @@ static void run_tests()
j = json_pack("[i]", 1);
if (!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
fail("json_unpack failed to catch index out of array bounds");
check_error(json_error_index_out_of_range, "Array index 1 out of range", "<validation>", 1, 3, 3);
check_error(json_error_index_out_of_range, "Array index 1 out of range",
"<validation>", 1, 3, 3);
json_decref(j);
/* NULL object key */
@ -265,7 +283,8 @@ static void run_tests()
j = json_pack("{si}", "foo", 42);
if (!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
fail("json_unpack failed to catch null string pointer");
check_error(json_error_item_not_found, "Object item not found: baz", "<validation>", 1, 3, 3);
check_error(json_error_item_not_found, "Object item not found: baz",
"<validation>", 1, 3, 3);
json_decref(j);
/*
@ -281,14 +300,16 @@ static void run_tests()
j = json_pack("[iii]", 1, 2, 3);
if (!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
fail("json_unpack array with strict validation failed");
check_error(json_error_end_of_input_expected, "1 array item(s) left unpacked", "<validation>", 1, 5, 5);
check_error(json_error_end_of_input_expected,
"1 array item(s) left unpacked", "<validation>", 1, 5, 5);
json_decref(j);
/* Like above, but with JSON_STRICT instead of '!' format */
j = json_pack("[iii]", 1, 2, 3);
if (!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
fail("json_unpack array with strict validation failed");
check_error(json_error_end_of_input_expected, "1 array item(s) left unpacked", "<validation>", 1, 4, 4);
check_error(json_error_end_of_input_expected,
"1 array item(s) left unpacked", "<validation>", 1, 4, 4);
json_decref(j);
j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
@ -304,9 +325,9 @@ static void run_tests()
{
const char *possible_errors[] = {
"2 object item(s) left unpacked: baz, quux",
"2 object item(s) left unpacked: quux, baz"
};
check_errors(json_error_end_of_input_expected, possible_errors, 2, "<validation>", 1, 10, 10);
"2 object item(s) left unpacked: quux, baz"};
check_errors(json_error_end_of_input_expected, possible_errors, 2,
"<validation>", 1, 10, 10);
}
json_decref(j);
@ -320,35 +341,41 @@ static void run_tests()
j = json_pack("[ii]", 1, 2);
if (!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
fail("json_unpack failed to catch ! in the middle of an array");
check_error(json_error_invalid_format, "Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
check_error(json_error_invalid_format, "Expected ']' after '!', got 'i'",
"<format>", 1, 4, 4);
if (!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
fail("json_unpack failed to catch * in the middle of an array");
check_error(json_error_invalid_format, "Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
check_error(json_error_invalid_format, "Expected ']' after '*', got 'i'",
"<format>", 1, 4, 4);
json_decref(j);
j = json_pack("{sssi}", "foo", "bar", "baz", 42);
if (!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
fail("json_unpack failed to catch ! in the middle of an object");
check_error(json_error_invalid_format, "Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
check_error(json_error_invalid_format, "Expected '}' after '!', got 's'",
"<format>", 1, 5, 5);
if (!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
fail("json_unpack failed to catch ! in the middle of an object");
check_error(json_error_invalid_format, "Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
check_error(json_error_invalid_format, "Expected '}' after '*', got 's'",
"<format>", 1, 5, 5);
json_decref(j);
/* Error in nested object */
j = json_pack("{s{snsn}}", "foo", "bar", "baz");
if (!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
fail("json_unpack nested object with strict validation failed");
check_error(json_error_end_of_input_expected, "1 object item(s) left unpacked: baz", "<validation>", 1, 7, 7);
check_error(json_error_end_of_input_expected,
"1 object item(s) left unpacked: baz", "<validation>", 1, 7, 7);
json_decref(j);
/* Error in nested array */
j = json_pack("[[ii]]", 1, 2);
if (!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
fail("json_unpack nested array with strict validation failed");
check_error(json_error_end_of_input_expected, "1 array item(s) left unpacked", "<validation>", 1, 5, 5);
check_error(json_error_end_of_input_expected,
"1 array item(s) left unpacked", "<validation>", 1, 5, 5);
json_decref(j);
/* Optional values */
@ -370,9 +397,8 @@ static void run_tests()
j = json_object();
i1 = i2 = i3 = 0;
if(json_unpack(j, "{s?[ii]s?{s{si}}}",
"foo", &i1, &i2,
"bar", "baz", "quux", &i3))
if (json_unpack(j, "{s?[ii]s?{s{si}}}", "foo", &i1, &i2, "bar", "baz",
"quux", &i3))
fail("json_unpack failed for complex optional values");
if (i1 != 0 || i2 != 0 || i3 != 0)
fail("json_unpack unexpectedly unpacked something");
@ -400,7 +426,9 @@ static void run_tests()
j = json_pack("{sisi}", "foo", 42, "baz", 43);
i1 = i2 = i3 = 0;
if (!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2))
fail("json_unpack failed for optional values with strict mode and compensation");
check_error(json_error_end_of_input_expected, "1 object item(s) left unpacked: baz", "<validation>", 1, 8, 8);
fail("json_unpack failed for optional values with strict mode and "
"compensation");
check_error(json_error_end_of_input_expected,
"1 object item(s) left unpacked: baz", "<validation>", 1, 8, 8);
json_decref(j);
}

View File

@ -5,20 +5,19 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include <jansson.h>
#include "util.h"
#include <jansson.h>
#include <string.h>
static void test_version_str(void)
{
static void test_version_str(void) {
if (strcmp(jansson_version_str(), JANSSON_VERSION)) {
fail("jansson_version_str returned invalid version string");
}
}
static void test_version_cmp()
{
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION, JANSSON_MICRO_VERSION)) {
static void test_version_cmp() {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION,
JANSSON_MICRO_VERSION)) {
fail("jansson_version_cmp equality check failed");
}
@ -27,32 +26,37 @@ static void test_version_cmp()
}
if (JANSSON_MINOR_VERSION) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION - 1, JANSSON_MICRO_VERSION) <= 0) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION,
JANSSON_MINOR_VERSION - 1,
JANSSON_MICRO_VERSION) <= 0) {
fail("jansson_version_cmp less than check failed");
}
}
if (JANSSON_MICRO_VERSION) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION, JANSSON_MICRO_VERSION - 1) <= 0) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION,
JANSSON_MICRO_VERSION - 1) <= 0) {
fail("jansson_version_cmp less than check failed");
}
}
if (jansson_version_cmp(JANSSON_MAJOR_VERSION + 1, JANSSON_MINOR_VERSION, JANSSON_MICRO_VERSION) >= 0) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION + 1, JANSSON_MINOR_VERSION,
JANSSON_MICRO_VERSION) >= 0) {
fail("jansson_version_cmp greater than check failed");
}
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION + 1, JANSSON_MICRO_VERSION) >= 0) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION + 1,
JANSSON_MICRO_VERSION) >= 0) {
fail("jansson_version_cmp greater than check failed");
}
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION, JANSSON_MICRO_VERSION + 1) >= 0) {
if (jansson_version_cmp(JANSSON_MAJOR_VERSION, JANSSON_MINOR_VERSION,
JANSSON_MICRO_VERSION + 1) >= 0) {
fail("jansson_version_cmp greater than check failed");
}
}
static void run_tests()
{
static void run_tests() {
test_version_str();
test_version_cmp();
}

View File

@ -30,14 +30,13 @@
} while (0)
/* Assumes json_error_t error */
#define check_errors(code_, texts_, num_, source_, \
line_, column_, position_) \
#define check_errors(code_, texts_, num_, source_, line_, column_, position_) \
do { \
int i_, found_ = 0; \
if (json_error_code(&error) != code_) { \
failhdr; \
fprintf(stderr, "code: %d != %d\n", \
json_error_code(&error), code_); \
fprintf(stderr, "code: %d != %d\n", json_error_code(&error), \
code_); \
exit(1); \
} \
for (i_ = 0; i_ < num_; i_++) { \
@ -49,7 +48,8 @@
if (!found_) { \
failhdr; \
if (num_ == 1) { \
fprintf(stderr, "text: \"%s\" != \"%s\"\n", error.text, texts_[0]); \
fprintf(stderr, "text: \"%s\" != \"%s\"\n", error.text, \
texts_[0]); \
} else { \
fprintf(stderr, "text: \"%s\" does not match\n", error.text); \
} \
@ -58,7 +58,8 @@
if (strcmp(error.source, source_) != 0) { \
failhdr; \
\
fprintf(stderr, "source: \"%s\" != \"%s\"\n", error.source, source_); \
fprintf(stderr, "source: \"%s\" != \"%s\"\n", error.source, \
source_); \
exit(1); \
} \
if (error.line != line_) { \
@ -73,17 +74,16 @@
} \
if (error.position != position_) { \
failhdr; \
fprintf(stderr, "position: %d != %d\n", error.position, position_); \
fprintf(stderr, "position: %d != %d\n", error.position, \
position_); \
exit(1); \
} \
} while (0)
/* Assumes json_error_t error */
#define check_error(code_, text_, source_, line_, column_, position_) \
check_errors(code_, &text_, 1, source_, line_, column_, position_)
static void run_tests();
int main() {