From 2d2869ce6060f974004ef98020b38b67366166d1 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 30 Dec 2023 23:29:43 +0100 Subject: json: add the lib prefix to wrapper functions It makes it easier to audit for non-wrapped json-c usage. --- src/json.c | 107 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 51 deletions(-) (limited to 'src/json.c') diff --git a/src/json.c b/src/json.c index 9a38e4e..06bb57d 100644 --- a/src/json.c +++ b/src/json.c @@ -18,38 +18,43 @@ #include #include -void json_free(struct json_object *obj) +#define libjson_errno(fn) \ + do { \ + log_err("JSON: %s failed\n", fn); \ + } while (0) + +void libjson_free(struct json_object *obj) { json_object_put(obj); } -static const char *json_to_string_internal(struct json_object *obj, int flags) +static const char *libjson_to_string_internal(struct json_object *obj, int flags) { const char *result = json_object_to_json_string_ext(obj, flags); if (!result) { - json_errno("json_object_to_json_string"); + libjson_errno("json_object_to_json_string"); return NULL; } return result; } -const char *json_to_string(struct json_object *obj) +const char *libjson_to_string(struct json_object *obj) { - return json_to_string_internal(obj, JSON_C_TO_STRING_SPACED); + return libjson_to_string_internal(obj, JSON_C_TO_STRING_SPACED); } -const char *json_to_string_pretty(struct json_object *obj) +const char *libjson_to_string_pretty(struct json_object *obj) { - return json_to_string_internal(obj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + return libjson_to_string_internal(obj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); } -struct json_object *json_from_string(const char *src) +struct json_object *libjson_from_string(const char *src) { enum json_tokener_error error; struct json_object *result = json_tokener_parse_verbose(src, &error); if (!result) { - json_errno("json_tokener_parse_verbose"); + libjson_errno("json_tokener_parse_verbose"); log_err("JSON: parsing failed: %s\n", json_tokener_error_desc(error)); return NULL; } @@ -57,12 +62,12 @@ struct json_object *json_from_string(const char *src) return result; } -int json_clone(const struct json_object *obj, const char *key, struct json_object **_value) +int libjson_clone(const struct json_object *obj, const char *key, struct json_object **_value) { int ret = 0; struct json_object *old_value = NULL; - ret = json_get(obj, key, &old_value); + ret = libjson_get(obj, key, &old_value); if (ret < 0) return ret; @@ -75,11 +80,11 @@ int json_clone(const struct json_object *obj, const char *key, struct json_objec return ret; } -int json_send(struct json_object *obj, int fd) +int libjson_send(struct json_object *obj, int fd) { int ret = 0; - const char *str = json_to_string(obj); + const char *str = libjson_to_string(obj); if (!str) return -1; @@ -93,7 +98,7 @@ int json_send(struct json_object *obj, int fd) return ret; } -struct json_object *json_recv(int fd) +struct json_object *libjson_recv(int fd) { struct json_object *result = NULL; int ret = 0; @@ -103,7 +108,7 @@ struct json_object *json_recv(int fd) if (ret < 0) return NULL; - result = json_from_string((const char *)buf_get_data(buf)); + result = libjson_from_string((const char *)buf_get_data(buf)); if (!result) goto destroy_buf; @@ -114,36 +119,36 @@ destroy_buf: return result; } -int json_new_object(struct json_object **_obj) +int libjson_new_object(struct json_object **_obj) { struct json_object *obj = json_object_new_object(); if (!obj) { - json_errno("json_object_new_object"); + libjson_errno("json_object_new_object"); return -1; } *_obj = obj; return 0; } -int json_new_array(struct json_object **_arr) +int libjson_new_array(struct json_object **_arr) { struct json_object *arr = json_object_new_array(); if (!arr) { - json_errno("json_object_new_array"); + libjson_errno("json_object_new_array"); return -1; } *_arr = arr; return 0; } -int json_has(const struct json_object *obj, const char *key) +int libjson_has(const struct json_object *obj, const char *key) { return json_object_object_get_ex(obj, key, NULL); } -int json_get(const struct json_object *obj, const char *key, struct json_object **value) +int libjson_get(const struct json_object *obj, const char *key, struct json_object **value) { - if (!json_has(obj, key)) { + if (!libjson_has(obj, key)) { log_err("JSON: key is missing: %s\n", key); return -1; } @@ -151,11 +156,11 @@ int json_get(const struct json_object *obj, const char *key, struct json_object return json_object_object_get_ex(obj, key, value); } -int json_get_string(const struct json_object *obj, const char *key, const char **_value) +int libjson_get_string(const struct json_object *obj, const char *key, const char **_value) { struct json_object *value = NULL; - int ret = json_get(obj, key, &value); + int ret = libjson_get(obj, key, &value); if (ret < 0) return ret; @@ -168,11 +173,11 @@ int json_get_string(const struct json_object *obj, const char *key, const char * return 0; } -int json_get_int(const struct json_object *obj, const char *key, int64_t *_value) +int libjson_get_int(const struct json_object *obj, const char *key, int64_t *_value) { struct json_object *value = NULL; - int ret = json_get(obj, key, &value); + int ret = libjson_get(obj, key, &value); if (ret < 0) return ret; @@ -192,30 +197,30 @@ int json_get_int(const struct json_object *obj, const char *key, int64_t *_value return 0; } -static int json_set_internal(struct json_object *obj, const char *key, struct json_object *value, - unsigned flags) +static int libjson_set_internal(struct json_object *obj, const char *key, struct json_object *value, + unsigned flags) { int ret = 0; ret = json_object_object_add_ex(obj, key, value, flags); if (ret < 0) { - json_errno("json_object_object_add_ex"); + libjson_errno("json_object_object_add_ex"); return ret; } return 0; } -static int json_set_string_internal(struct json_object *obj, const char *key, const char *_value, - unsigned flags) +static int libjson_set_string_internal(struct json_object *obj, const char *key, const char *_value, + unsigned flags) { struct json_object *value = json_object_new_string(_value); if (!value) { - json_errno("json_object_new_string"); + libjson_errno("json_object_new_string"); return -1; } - int ret = json_set_internal(obj, key, value, flags); + int ret = libjson_set_internal(obj, key, value, flags); if (ret < 0) goto free_value; @@ -227,16 +232,16 @@ free_value: return ret; } -static int json_set_int_internal(struct json_object *obj, const char *key, int64_t _value, - unsigned flags) +static int libjson_set_int_internal(struct json_object *obj, const char *key, int64_t _value, + unsigned flags) { struct json_object *value = json_object_new_int64(_value); if (!value) { - json_errno("json_object_new_int"); + libjson_errno("json_object_new_int"); return -1; } - int ret = json_set_internal(obj, key, value, flags); + int ret = libjson_set_internal(obj, key, value, flags); if (ret < 0) goto free_value; @@ -248,19 +253,19 @@ free_value: return ret; } -int json_set(struct json_object *obj, const char *key, struct json_object *value) +int libjson_set(struct json_object *obj, const char *key, struct json_object *value) { - return json_set_internal(obj, key, value, 0); + return libjson_set_internal(obj, key, value, 0); } -int json_set_string(struct json_object *obj, const char *key, const char *value) +int libjson_set_string(struct json_object *obj, const char *key, const char *value) { - return json_set_string_internal(obj, key, value, 0); + return libjson_set_string_internal(obj, key, value, 0); } -int json_set_int(struct json_object *obj, const char *key, int64_t value) +int libjson_set_int(struct json_object *obj, const char *key, int64_t value) { - return json_set_int_internal(obj, key, value, 0); + return libjson_set_int_internal(obj, key, value, 0); } #ifndef JSON_C_OBJECT_ADD_CONSTANT_KEY @@ -268,26 +273,26 @@ int json_set_int(struct json_object *obj, const char *key, int64_t value) #endif static const unsigned json_const_key_flags = JSON_C_OBJECT_ADD_CONSTANT_KEY; -int json_set_const_key(struct json_object *obj, const char *key, struct json_object *value) +int libjson_set_const_key(struct json_object *obj, const char *key, struct json_object *value) { - return json_set_internal(obj, key, value, json_const_key_flags); + return libjson_set_internal(obj, key, value, json_const_key_flags); } -int json_set_string_const_key(struct json_object *obj, const char *key, const char *value) +int libjson_set_string_const_key(struct json_object *obj, const char *key, const char *value) { - return json_set_string_internal(obj, key, value, json_const_key_flags); + return libjson_set_string_internal(obj, key, value, json_const_key_flags); } -int json_set_int_const_key(struct json_object *obj, const char *key, int64_t value) +int libjson_set_int_const_key(struct json_object *obj, const char *key, int64_t value) { - return json_set_int_internal(obj, key, value, json_const_key_flags); + return libjson_set_int_internal(obj, key, value, json_const_key_flags); } -int json_append(struct json_object *arr, struct json_object *elem) +int libjson_append(struct json_object *arr, struct json_object *elem) { int ret = json_object_array_add(arr, elem); if (ret < 0) { - json_errno("json_object_array_add"); + libjson_errno("json_object_array_add"); return ret; } return ret; -- cgit v1.2.3