From: Michael Tremer Date: Sat, 21 Jun 2025 12:32:33 +0000 (+0000) Subject: base64: Allow specifying the length of the input buffer X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb98e87f9290c8b3c273751b625a15076c79b0be;p=pakfire.git base64: Allow specifying the length of the input buffer Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/base64.c b/src/pakfire/base64.c index 7c162a81..9c018cd6 100644 --- a/src/pakfire/base64.c +++ b/src/pakfire/base64.c @@ -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])) diff --git a/src/pakfire/base64.h b/src/pakfire/base64.h index b114c79d..8e9306ac 100644 --- a/src/pakfire/base64.h +++ b/src/pakfire/base64.h @@ -22,9 +22,10 @@ #define PAKFIRE_BASE64_H #include +#include 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 */ diff --git a/src/pakfire/key.c b/src/pakfire/key.c index db945dec..eaee5145 100644 --- a/src/pakfire/key.c +++ b/src/pakfire/key.c @@ -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; diff --git a/tests/libpakfire/util.c b/tests/libpakfire/util.c index ff710304..4053f164 100644 --- a/tests/libpakfire/util.c +++ b/tests/libpakfire/util.c @@ -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);