]> git.ipfire.org Git - pakfire.git/commitdiff
json: Add a function to add some binary as base64
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:11:47 +0000 (13:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:11:47 +0000 (13:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h

index c0cda39fcfdb705bcb6ad4c4c56bf9f5750b1457..de07927edbe4e2f1578523ec96c5a7be00f1f729 100644 (file)
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <sys/mman.h>
 
+#include <pakfire/base64.h>
 #include <pakfire/ctx.h>
 #include <pakfire/json.h>
 #include <pakfire/logging.h>
@@ -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);
index 3f70ba6f27068fecfa9d1c589f3109539ced905d..1d4403db185d88ba97362518c0dc09fc91f041f7 100644 (file)
@@ -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);