diff options
author | David Robillard <d@drobilla.net> | 2019-03-17 23:49:10 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-12-19 20:57:52 -0500 |
commit | 7aaf7d5187ded5c726ff23ec58b2f8a3d5d158b2 (patch) | |
tree | e90da3ee89cac469041fff0fdb4d393724946489 /src | |
parent | f03a9cffc2f52d637692210e2a3e96f545972976 (diff) | |
download | serd-7aaf7d5187ded5c726ff23ec58b2f8a3d5d158b2.tar.gz serd-7aaf7d5187ded5c726ff23ec58b2f8a3d5d158b2.tar.bz2 serd-7aaf7d5187ded5c726ff23ec58b2f8a3d5d158b2.zip |
Clean up and expose base64 implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/base64.c | 20 | ||||
-rw-r--r-- | src/base64.h | 45 | ||||
-rw-r--r-- | src/node.c | 3 |
3 files changed, 13 insertions, 55 deletions
diff --git a/src/base64.c b/src/base64.c index a1849838..2477ff4b 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" @@ -60,11 +58,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, @@ -105,13 +109,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; @@ -123,5 +126,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; } diff --git a/src/base64.h b/src/base64.h deleted file mode 100644 index cb89491c..00000000 --- a/src/base64.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - Copyright 2011-2018 David Robillard <http://drobilla.net> - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#ifndef SERD_BASE64_H -#define SERD_BASE64_H - -#include <stdbool.h> -#include <stddef.h> - -/** - 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. -*/ -size_t -serd_base64_get_length(size_t size, bool wrap_lines); - -/** - Encode `size` bytes of `buf` into `str`, which must be large enough. - - @param str Output string buffer. - @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. -*/ -bool -serd_base64_encode(char* str, const void* buf, size_t size, bool wrap_lines); - -#endif // SERD_BASE64_H @@ -16,7 +16,6 @@ #include "node.h" -#include "base64.h" #include "serd_internal.h" #include "static_nodes.h" #include "string_utils.h" @@ -681,7 +680,7 @@ serd_new_blob(const void* buf, } const SerdNode* type = datatype ? datatype : &serd_xsd_base64Binary.node; - const size_t len = serd_base64_get_length(size, wrap_lines); + const size_t len = serd_base64_encoded_length(size, wrap_lines); const size_t type_len = serd_node_total_size(type); const size_t total_len = len + 1 + type_len; |