From: Michael Tremer Date: Thu, 26 Jun 2025 13:11:47 +0000 (+0000) Subject: json: Add a function to add some binary as base64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=583619c3213ceae44348085a61b8b2d5b5278581;p=pakfire.git json: Add a function to add some binary as base64 Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/json.c b/src/pakfire/json.c index c0cda39f..de07927e 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -228,6 +229,32 @@ ERROR: return r; } +int pakfire_json_add_bytes(struct json_object* json, + const char* name, const char* data, size_t length) { + char* bytes = NULL; + int r; + + // Add null if we don't have any data + if (!length) + return pakfire_json_add_null(json, name); + + // Encode the buffer as base64 + r = pakfire_b64encode(&bytes, (const unsigned char*)data, length); + if (r < 0) + goto ERROR; + + // Add the string + r = pakfire_json_add_string(json, name, bytes); + if (r < 0) + goto ERROR; + +ERROR: + if (bytes) + free(bytes); + + return r; +} + int pakfire_json_add_int64(struct json_object* json, const char* name, int64_t value) { // Convert integer to JSON object struct json_object* object = json_object_new_int64(value); diff --git a/src/pakfire/json.h b/src/pakfire/json.h index 3f70ba6f..1d4403db 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -48,6 +48,7 @@ int pakfire_json_add_string(struct json_object* json, const char* name, const ch int pakfire_json_add_stringn(struct json_object* json, const char* name, const char* value, size_t length); int pakfire_json_add_stringf(struct json_object* json, const char* name, const char* format, ...) __attribute__((format(printf, 3, 4))); +int pakfire_json_add_bytes(struct json_object* json, const char* name, const char* data, size_t length); int pakfire_json_add_int64(struct json_object* json, const char* name, int64_t value); int pakfire_json_add_uint64(struct json_object* json, const char* name, uint64_t value); int pakfire_json_add_double(struct json_object* json, const char* name, double value);