From dd7f57a7d955a323c5691ec64dd96e9b0a5a2553 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 17 Mar 2019 23:49:10 +0100 Subject: Clean up and expose base64 implementation --- src/base64.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/base64.c') diff --git a/src/base64.c b/src/base64.c index b82f0389..e557fdf8 100644 --- a/src/base64.c +++ b/src/base64.c @@ -14,8 +14,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "base64.h" - #include "serd_internal.h" #include "string_utils.h" @@ -59,11 +57,17 @@ encode_chunk(uint8_t out[4], const uint8_t in[3], size_t n_in) } size_t -serd_base64_get_length(const size_t size, const bool wrap_lines) +serd_base64_encoded_length(const size_t size, const bool wrap_lines) { return (size + 2) / 3 * 4 + (wrap_lines * ((size - 1) / 57)); } +size_t +serd_base64_decoded_size(const size_t len) +{ + return (len * 3) / 4 + 2; +} + bool serd_base64_encode(char* const str, const void* const buf, @@ -104,13 +108,12 @@ decode_chunk(const uint8_t in[4], uint8_t out[3]) return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '=')); } -void* -serd_base64_decode(const char* str, size_t len, size_t* size) +SerdStatus +serd_base64_decode(void* buf, size_t* size, const char* str, size_t len) { const uint8_t* ustr = (const uint8_t*)str; - void* buf = malloc((len * 3) / 4 + 2); - *size = 0; + *size = 0; for (size_t i = 0, j = 0; i < len; j += 3) { uint8_t in[] = "===="; size_t n_in = 0; @@ -122,5 +125,6 @@ serd_base64_decode(const char* str, size_t len, size_t* size) *size += decode_chunk(in, (uint8_t*)buf + j); } } - return buf; + + return SERD_SUCCESS; } -- cgit v1.2.1