]> git.ipfire.org Git - pakfire.git/commitdiff
base64: Allow specifying the length of the input buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Jun 2025 12:32:33 +0000 (12:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Jun 2025 12:32:57 +0000 (12:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/base64.c
src/pakfire/base64.h
src/pakfire/key.c
tests/libpakfire/util.c

index 7c162a8110262aafb3cf8f5db3921707e6b1f4e8..9c018cd69cf9b534a5b1ce00e6c77fa58b76357e 100644 (file)
@@ -67,7 +67,7 @@ ERROR:
        return r;
 }
 
-int pakfire_b64decode(unsigned char** output, size_t* length, const char* input) {
+int pakfire_b64decode(unsigned char** output, size_t* length, const char* input, ssize_t l) {
        unsigned char* buffer = NULL;
        size_t block;
        int r;
@@ -76,8 +76,9 @@ int pakfire_b64decode(unsigned char** output, size_t* length, const char* input)
        if (!input || !output || !length)
                return -EINVAL;
 
-       // Determine the length of the input
-       size_t l = strlen(input);
+       // Determine the length of the input if none given
+       if (l < 0)
+               l = strlen(input);
 
        // Remove any trailing whitespace
        while (l >= 1 && isspace(input[l - 1]))
index b114c79d19c8de7c28a39261503af5c02bd61691..8e9306ac6314c08d54050db3f936ab713395ad2a 100644 (file)
 #define PAKFIRE_BASE64_H
 
 #include <stddef.h>
+#include <sys/types.h>
 
 int pakfire_b64encode(char** output, const unsigned char* input, const size_t length);
 
-int pakfire_b64decode(unsigned char** output, size_t* length, const char* input);
+int pakfire_b64decode(unsigned char** output, size_t* length, const char* input, ssize_t l);
 
 #endif /* PAKFIRE_BASE64_H */
index db945dec6471b11d9d4ace9eeec01938100c9a7a..eaee51454555a5bc1a5e55ebf9cc5bda4e9aedcd 100644 (file)
@@ -430,7 +430,7 @@ int pakfire_key_import(struct pakfire_key** key,
                        // The second line should hold the key
                        case 2:
                                // Decode the key
-                               r = pakfire_b64decode(&buffer, &buffer_length, line);
+                               r = pakfire_b64decode(&buffer, &buffer_length, line, -1);
                                if (r < 0) {
                                        ERROR(ctx, "Could not decode the key: %s\n", strerror(-r));
                                        goto ERROR;
@@ -912,7 +912,7 @@ static int pakfire_key_read_signature(struct pakfire_key* key,
                        continue;
 
                // Decode the signature
-               r = pakfire_b64decode(&buffer, &buffer_length, line);
+               r = pakfire_b64decode(&buffer, &buffer_length, line, -1);
                if (r < 0) {
                        ERROR(key->ctx, "Could not decode the signature: %s\n", strerror(-r));
                        goto ERROR;
index ff710304dd3820efaed19ea94d5b0d56b345b054..4053f164ed551f098c2a6020a11451c7617d0d88 100644 (file)
@@ -67,7 +67,7 @@ static int test_base64(const struct test* t) {
        printf("%s\n", base64);
 
        // Decode the data
-       ASSERT_SUCCESS(pakfire_b64decode(&output, &length, base64));
+       ASSERT_SUCCESS(pakfire_b64decode(&output, &length, base64, -1));
 
        // Print the decoded data
        printf("%.*s\n", (int)length, (char*)output);