base64: add base64_encode_oneshot()

Convenience function for base64_encode() + base64_encode_final().

Internally, we also add base64_encode_to_buffer()

This is an internal function that encodes a multiple-of-three long
input into a user allocated buffer.

base64_encode() and base64_encode_oneshot() uses the new function to
do the bulk of the encoding. This allows base64_encode_oneshot() to do
a single allocation.

This patch also adds unit tests for both base64_encode() and
base64_encode_oneshot() (but _not_ for base64_encode_final()).

Finally, the type of the 'data' parameter is changed to 'void*'.
This commit is contained in:
Daniel Eklöf 2026-05-16 20:26:37 +02:00
parent 4bf60d0fbc
commit 4bc8a39d6c
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 113 additions and 16 deletions

View file

@ -4,5 +4,6 @@
#include <stddef.h>
char *base64_decode(const char *s, size_t *out_len);
char *base64_encode(const uint8_t *data, size_t size);
void base64_encode_final(const uint8_t *data, size_t size, char result[4]);
char *base64_encode(const void *data, size_t size);
void base64_encode_final(const void *data, size_t size, char result[4]);
char *base64_encode_oneshot(const void *data, size_t size);