From 7aaf7d5187ded5c726ff23ec58b2f8a3d5d158b2 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 --- serd/serd.h | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'serd') diff --git a/serd/serd.h b/serd/serd.h index 1dc4db97..dfd53480 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -360,19 +360,60 @@ SERD_API double serd_strtod(const char* str, size_t* end); +/** + @} + @name Base64 + @{ +*/ + +/** + Return the number of bytes required to encode `size` bytes in base64. + + @param size The number of input (binary) bytes to encode. + @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. + @return The length of the base64 encoding, excluding null terminator. +*/ +SERD_API +size_t +serd_base64_encoded_length(size_t size, bool wrap_lines); + +/** + Return the maximum number of bytes required to decode `size` bytes of base64. + + @param len The number of input (text) bytes to decode. + @return The required buffer size to decode `size` bytes of base64. +*/ +SERD_API +size_t +serd_base64_decoded_size(size_t len); + +/** + Encode `size` bytes of `buf` into `str`, which must be large enough. + + @param str Output buffer of at least serd_base64_encoded_length(size) bytes. + @param buf Input binary data. + @param size Number of bytes to encode from `buf`. + @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. + @return True iff `str` contains newlines. +*/ +SERD_API +bool +serd_base64_encode(char* str, const void* buf, size_t size, bool wrap_lines); + /** Decode a base64 string. + This function can be used to deserialise a blob node created with serd_new_blob(). + @param buf Output buffer of at least serd_base64_decoded_size(size) bytes. + @param size Set to the size of the decoded data in bytes. @param str Base64 string to decode. @param len The length of `str`. - @param size Set to the size of the returned blob in bytes. - @return A newly allocated blob which must be freed with serd_free(). */ SERD_API -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); /** @} -- cgit v1.2.1