Browse Source

Minor changes in C source code

tags/json-c-0.13-20171207
Nicola Spanti (RyDroid) 9 years ago
parent
commit
f40b08d8f0
2 changed files with 69 additions and 54 deletions
  1. +30
    -23
      json_object.c
  2. +39
    -31
      json_tokener.c

+ 30
- 23
json_object.c View File

@@ -84,8 +84,10 @@ static void json_object_fini(void)
json_object_table->count); json_object_table->count);
lh_foreach(json_object_table, ent) lh_foreach(json_object_table, ent)
{ {
struct json_object* obj = (struct json_object*)lh_entry_v(ent);
MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
struct json_object* obj =
(struct json_object*) lh_entry_v(ent);
MC_DEBUG("\t%s:%p\n",
json_type_to_name(obj->o_type), obj);
} }
} }
} }
@@ -147,7 +149,9 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
if(c < ' ') if(c < ' ')
{ {
if(pos - start_offset > 0) if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
printbuf_memappend(pb,
str + start_offset,
pos - start_offset);
sprintbuf(pb, "\\u00%c%c", sprintbuf(pb, "\\u00%c%c",
json_hex_chars[c >> 4], json_hex_chars[c >> 4],
json_hex_chars[c & 0xf]); json_hex_chars[c & 0xf]);
@@ -168,7 +172,7 @@ extern struct json_object* json_object_get(struct json_object *jso)
{ {
if (jso) if (jso)
jso->_ref_count++; jso->_ref_count++;
return jso;
return NULL;
} }


int json_object_put(struct json_object *jso) int json_object_put(struct json_object *jso)
@@ -338,7 +342,7 @@ static void indent(struct printbuf *pb, int level, int flags)
static int json_object_object_to_json_string(struct json_object* jso, static int json_object_object_to_json_string(struct json_object* jso,
struct printbuf *pb, struct printbuf *pb,
int level, int level,
int flags)
int flags)
{ {
int had_children = 0; int had_children = 0;
struct json_object_iter iter; struct json_object_iter iter;
@@ -403,7 +407,7 @@ struct json_object* json_object_new_object(void)
jso->_delete = &json_object_object_delete; jso->_delete = &json_object_object_delete;
jso->_to_json_string = &json_object_object_to_json_string; jso->_to_json_string = &json_object_object_to_json_string;
jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
&json_object_lh_entry_free);
&json_object_lh_entry_free);
if (!jso->o.c_object) if (!jso->o.c_object)
{ {
json_object_generic_delete(jso); json_object_generic_delete(jso);
@@ -437,7 +441,8 @@ void json_object_object_add_ex(struct json_object* jso,
struct lh_entry *existing_entry; struct lh_entry *existing_entry;
const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key); const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL : existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash);
lh_table_lookup_entry_w_hash(jso->o.c_object,
(const void *)key, hash);
if (!existing_entry) if (!existing_entry)
{ {
const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
@@ -445,21 +450,22 @@ void json_object_object_add_ex(struct json_object* jso,
lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts); lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
return; return;
} }
existing_value = (json_object *)lh_entry_v(existing_entry);
existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value) if (existing_value)
json_object_put(existing_value); json_object_put(existing_value);
existing_entry->v = val; existing_entry->v = val;
} }


int json_object_object_add(struct json_object* jso, const char *key, int json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val)
struct json_object *val)
{ {
// We lookup the entry and replace the value, rather than just deleting // We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid. // and re-adding it, so the existing key remains valid.
json_object *existing_value = NULL; json_object *existing_value = NULL;
struct lh_entry *existing_entry; struct lh_entry *existing_entry;
const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key); const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash);
existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object,
(const void *)key, hash);
if (!existing_entry) if (!existing_entry)
{ {
char *keydup = strdup(key); char *keydup = strdup(key);
@@ -482,14 +488,16 @@ int json_object_object_length(const struct json_object *jso)
return lh_table_length(jso->o.c_object); return lh_table_length(jso->o.c_object);
} }


struct json_object* json_object_object_get(const struct json_object* jso, const char *key)
struct json_object* json_object_object_get(const struct json_object* jso,
const char *key)
{ {
struct json_object *result = NULL; struct json_object *result = NULL;
json_object_object_get_ex(jso, key, &result); json_object_object_get_ex(jso, key, &result);
return result; return result;
} }


json_bool json_object_object_get_ex(const struct json_object* jso, const char *key, struct json_object **value)
json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
struct json_object **value)
{ {
if (value != NULL) if (value != NULL)
*value = NULL; *value = NULL;
@@ -500,7 +508,8 @@ json_bool json_object_object_get_ex(const struct json_object* jso, const char *k
switch(jso->o_type) switch(jso->o_type)
{ {
case json_type_object: case json_type_object:
return lh_table_lookup_ex(jso->o.c_object, (const void *)key, (void**)value);
return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
(void**) value);
default: default:
if (value != NULL) if (value != NULL)
*value = NULL; *value = NULL;
@@ -519,12 +528,11 @@ void json_object_object_del(struct json_object* jso, const char *key)
static int json_object_boolean_to_json_string(struct json_object* jso, static int json_object_boolean_to_json_string(struct json_object* jso,
struct printbuf *pb, struct printbuf *pb,
int level, int level,
int flags)
int flags)
{ {
if (jso->o.c_boolean) if (jso->o.c_boolean)
return sprintbuf(pb, "true"); return sprintbuf(pb, "true");
else
return sprintbuf(pb, "false");
return sprintbuf(pb, "false");
} }


struct json_object* json_object_new_boolean(json_bool b) struct json_object* json_object_new_boolean(json_bool b)
@@ -603,10 +611,9 @@ int32_t json_object_get_int(const struct json_object *jso)
/* Make sure we return the correct values for out of range numbers. */ /* Make sure we return the correct values for out of range numbers. */
if (cint64 <= INT32_MIN) if (cint64 <= INT32_MIN)
return INT32_MIN; return INT32_MIN;
else if (cint64 >= INT32_MAX)
if (cint64 >= INT32_MAX)
return INT32_MAX; return INT32_MAX;
else
return (int32_t)cint64;
return (int32_t) cint64;
case json_type_double: case json_type_double:
return (int32_t)jso->o.c_double; return (int32_t)jso->o.c_double;
case json_type_boolean: case json_type_boolean:
@@ -808,7 +815,7 @@ double json_object_get_double(const struct json_object *jso)
static int json_object_string_to_json_string(struct json_object* jso, static int json_object_string_to_json_string(struct json_object* jso,
struct printbuf *pb, struct printbuf *pb,
int level, int level,
int flags)
int flags)
{ {
sprintbuf(pb, "\""); sprintbuf(pb, "\"");
json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags); json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
@@ -938,8 +945,7 @@ static int json_object_array_to_json_string(struct json_object* jso,


if (flags & JSON_C_TO_STRING_SPACED) if (flags & JSON_C_TO_STRING_SPACED)
return sprintbuf(pb, " ]"); return sprintbuf(pb, " ]");
else
return sprintbuf(pb, "]");
return sprintbuf(pb, "]");
} }


static void json_object_array_entry_free(void *data) static void json_object_array_entry_free(void *data)
@@ -982,7 +988,8 @@ struct array_list* json_object_get_array(const struct json_object *jso)
} }
} }


void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
void json_object_array_sort(struct json_object *jso,
int(*sort_fn)(const void *, const void *))
{ {
array_list_sort(jso->o.c_array, sort_fn); array_list_sort(jso->o.c_array, sort_fn);
} }


+ 39
- 31
json_tokener.c View File

@@ -91,9 +91,11 @@ static const char* json_tokener_errors[] = {


const char *json_tokener_error_desc(enum json_tokener_error jerr) const char *json_tokener_error_desc(enum json_tokener_error jerr)
{ {
int jerr_int = (int)jerr;
if (jerr_int < 0 || jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()";
int jerr_int = (int) jerr;
if (jerr_int < 0 ||
jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
return "Unknown error, "
"invalid json_tokener_error value passed to json_tokener_error_desc()";
return json_tokener_errors[jerr]; return json_tokener_errors[jerr];
} }


@@ -114,7 +116,8 @@ struct json_tokener* json_tokener_new_ex(int depth)


tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener)); tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener));
if (!tok) return NULL; if (!tok) return NULL;
tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec));
tok->stack = (struct json_tokener_srec *) calloc(depth,
sizeof(struct json_tokener_srec));
if (!tok->stack) { if (!tok->stack) {
free(tok); free(tok);
return NULL; return NULL;
@@ -168,7 +171,8 @@ struct json_object* json_tokener_parse(const char *str)
return obj; return obj;
} }


struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error)
struct json_object* json_tokener_parse_verbose(const char *str,
enum json_tokener_error *error)
{ {
struct json_tokener* tok; struct json_tokener* tok;
struct json_object* obj; struct json_object* obj;
@@ -211,14 +215,17 @@ struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokene
* Returns 1 on success, sets tok->err and returns 0 if no more chars. * Returns 1 on success, sets tok->err and returns 0 if no more chars.
* Implicit inputs: str, len vars * Implicit inputs: str, len vars
*/ */
#define PEEK_CHAR(dest, tok) \
(((tok)->char_offset == len) ? \
(((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_finish) ? \
(((tok)->err = json_tokener_success), 0) \
: \
(((tok)->err = json_tokener_continue), 0) \
) : \
(((dest) = *str), 1) \
#define PEEK_CHAR(dest, tok) \
(((tok)->char_offset == len) ? \
(((tok)->depth == 0 && \
state == json_tokener_state_eatws && \
saved_state == json_tokener_state_finish \
) ? \
(((tok)->err = json_tokener_success), 0) \
: \
(((tok)->err = json_tokener_continue), 0) \
) : \
(((dest) = *str), 1) \
) )


/* ADVANCE_CHAR() macro: /* ADVANCE_CHAR() macro:
@@ -353,10 +360,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
printbuf_reset(tok->pb); printbuf_reset(tok->pb);
tok->st_pos = 0; tok->st_pos = 0;
goto redo_char; goto redo_char;
#if defined(__GNUC__)
case '0' ... '9':
#else
case '0':
case '0':
case '1': case '1':
case '2': case '2':
case '3': case '3':
@@ -366,7 +370,6 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case '7': case '7':
case '8': case '8':
case '9': case '9':
#endif
case '-': case '-':
state = json_tokener_state_number; state = json_tokener_state_number;
printbuf_reset(tok->pb); printbuf_reset(tok->pb);
@@ -405,7 +408,8 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
{ {
if (tok->st_pos == json_inf_str_len) if (tok->st_pos == json_inf_str_len)
{ {
current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
current = json_object_new_double(is_negative
? -INFINITY : INFINITY);
if(current == NULL) if(current == NULL)
goto out; goto out;
saved_state = json_tokener_state_finish; saved_state = json_tokener_state_finish;
@@ -617,13 +621,15 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
* characters. * characters.
*/ */
if( !ADVANCE_CHAR(str, tok) || !ADVANCE_CHAR(str, tok) ) { if( !ADVANCE_CHAR(str, tok) || !ADVANCE_CHAR(str, tok) ) {
printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
}
printbuf_memappend_fast(tok->pb,
(char*) utf8_replacement_char, 3);
}
/* Advance to the first char of the next sequence and /* Advance to the first char of the next sequence and
* continue processing with the next sequence. * continue processing with the next sequence.
*/ */
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
printbuf_memappend_fast(tok->pb,
(char*) utf8_replacement_char, 3);
goto out; goto out;
} }
tok->ucs_char = 0; tok->ucs_char = 0;
@@ -634,7 +640,8 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
* it. Put a replacement char in for the hi surrogate * it. Put a replacement char in for the hi surrogate
* and pretend we finished. * and pretend we finished.
*/ */
printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3);
printbuf_memappend_fast(tok->pb,
(char*) utf8_replacement_char, 3);
} }
} else if (IS_LOW_SURROGATE(tok->ucs_char)) { } else if (IS_LOW_SURROGATE(tok->ucs_char)) {
/* Got a low surrogate not preceded by a high */ /* Got a low surrogate not preceded by a high */
@@ -771,7 +778,8 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
int64_t num64; int64_t num64;
double numd; double numd;
if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) { if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) {
if (num64 && tok->pb->buf[0]=='0' && (tok->flags & JSON_TOKENER_STRICT)) {
if (num64 && tok->pb->buf[0]=='0' &&
(tok->flags & JSON_TOKENER_STRICT)) {
/* in strict mode, number must not start with 0 */ /* in strict mode, number must not start with 0 */
tok->err = json_tokener_error_parse_number; tok->err = json_tokener_error_parse_number;
goto out; goto out;
@@ -798,12 +806,12 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case json_tokener_state_array_after_sep: case json_tokener_state_array_after_sep:
case json_tokener_state_array: case json_tokener_state_array:
if(c == ']') { if(c == ']') {
if (state == json_tokener_state_array_after_sep &&
(tok->flags & JSON_TOKENER_STRICT))
{
tok->err = json_tokener_error_parse_unexpected;
goto out;
}
if (state == json_tokener_state_array_after_sep &&
(tok->flags & JSON_TOKENER_STRICT))
{
tok->err = json_tokener_error_parse_unexpected;
goto out;
}
saved_state = json_tokener_state_finish; saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws; state = json_tokener_state_eatws;
} else { } else {
@@ -971,5 +979,5 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,


void json_tokener_set_flags(struct json_tokener *tok, int flags) void json_tokener_set_flags(struct json_tokener *tok, int flags)
{ {
tok->flags = flags;
if(tok) tok->flags = flags;
} }

Loading…
Cancel
Save