From 30f3e6fc2c1e24c429d5d0b7100dc449ade6703f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 17 Jul 2021 17:31:53 -0400 Subject: Clean up base64 node construction and access API --- src/base64.c | 38 -------------------------------------- src/base64.h | 49 ------------------------------------------------- src/node.c | 27 +++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 87 deletions(-) delete mode 100644 src/base64.c delete mode 100644 src/base64.h (limited to 'src') diff --git a/src/base64.c b/src/base64.c deleted file mode 100644 index 9dac9979..00000000 --- a/src/base64.c +++ /dev/null @@ -1,38 +0,0 @@ -/* - Copyright 2011-2020 David Robillard - - 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. -*/ - -#include "exess/exess.h" -#include "serd/serd.h" - -#include - -void* -serd_base64_decode(const char* const str, const size_t len, size_t* const size) -{ - const size_t max_size = exess_base64_decoded_size(len); - - void* const buf = malloc(max_size); - const ExessVariableResult r = exess_read_base64(max_size, buf, str); - if (r.status) { - *size = 0; - free(buf); - return NULL; - } - - *size = r.write_count; - - return buf; -} diff --git a/src/base64.h b/src/base64.h deleted file mode 100644 index 6fbe6c5c..00000000 --- a/src/base64.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright 2011-2020 David Robillard - - 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 "serd/serd.h" - -#include -#include -#include - -/** - 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_CONST_FUNC -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(uint8_t* str, const void* buf, size_t size, bool wrap_lines); - -#endif // SERD_BASE64_H diff --git a/src/node.c b/src/node.c index 5016200d..eafcec38 100644 --- a/src/node.c +++ b/src/node.c @@ -182,6 +182,13 @@ serd_node_zero_pad(SerdNode* node) } } +static SerdWriteResult +result(const SerdStatus status, const size_t count) +{ + const SerdWriteResult result = {status, count}; + return result; +} + SerdNode* serd_new_simple_node(const SerdNodeType type, const SerdStringView str) { @@ -386,6 +393,26 @@ serd_get_integer(const SerdNode* const node) return value; } +size_t +serd_get_base64_size(const SerdNode* const node) +{ + return exess_base64_decoded_size(serd_node_length(node)); +} + +SerdWriteResult +serd_get_base64(const SerdNode* const node, + const size_t buf_size, + void* const buf) +{ + const size_t max_size = serd_get_base64_size(node); + const ExessVariableResult r = + exess_read_base64(buf_size, buf, serd_node_string(node)); + + return r.status == EXESS_NO_SPACE ? result(SERD_ERR_OVERFLOW, max_size) + : r.status ? result(SERD_ERR_BAD_SYNTAX, 0u) + : result(SERD_SUCCESS, r.write_count); +} + SerdNode* serd_node_copy(const SerdNode* node) { -- cgit v1.2.1