summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2020-02-23 10:01:33 +0000
committerVincent Sanders <vince@kyllikki.org>2020-02-23 10:01:33 +0000
commit09f95e2c5f705a6a78a70ab8be1e9a9b7283aa87 (patch)
tree668a2c9b64882fb1d2121e639456c7cef14e522b /src
parentb7199a77aca01f85a3359737cd4e6c73c29bd82a (diff)
downloadlibnsutils-09f95e2c5f705a6a78a70ab8be1e9a9b7283aa87.tar.gz
libnsutils-09f95e2c5f705a6a78a70ab8be1e9a9b7283aa87.tar.bz2
add non allocating base64 encoding API
Diffstat (limited to 'src')
-rw-r--r--src/base64.c114
1 files changed, 100 insertions, 14 deletions
diff --git a/src/base64.c b/src/base64.c
index 79909ea..87752da 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -110,34 +110,37 @@ static const uint8_t b64url_encoding_table[] = {
static unsigned int mod_table[] = {0, 2, 1};
+
+
/**
* Base 64 encode data using a given encoding table.
*
- * allocate a buffer and encode source data into it using the Base64 encoding.
+ * encode source data into buffer using the Base64 encoding.
*
* \param input The source data to encode.
* \param input_length The length of the source data.
- * \param output The buffer to recive data into, the caller must free.
- * \param output_length The length of data placed in \a output
- * \return NSERROR_OK on success and \a output updated else error code.
+ * \param output The buffer to recive data into,
+ * \param output The output buffer
+ * \param output_length The size of the \a output buffer on entry updated with
+ * length written on exit.
+ * \return NSUERROR_OK on success and \a output_length updated else error code.
*/
static nsuerror
-table_encode_alloc(const uint8_t *encoding_table,
- const uint8_t *input,
- size_t input_length,
- uint8_t **output,
- size_t *output_length)
+table_encode(const uint8_t *encoding_table,
+ const uint8_t *input,
+ size_t input_length,
+ uint8_t *encoded,
+ size_t *output_length)
{
- uint8_t *encoded;
size_t encoded_len;
size_t i; /* input index */
size_t j; /* output index */
encoded_len = 4 * ((input_length + 2) / 3);
- encoded = malloc(encoded_len);
- if (encoded == NULL) {
- return NSUERROR_NOMEM;
+ if (encoded_len > *output_length) {
+ /* output buffer is too small */
+ return NSUERROR_NOSPACE;
}
for (i = 0, j = 0; i < input_length;) {
@@ -158,12 +161,56 @@ table_encode_alloc(const uint8_t *encoding_table,
encoded[encoded_len - 1 - i] = '=';
}
- *output = encoded;
*output_length = encoded_len;
return NSUERROR_OK;
}
+
+
+/**
+ * Base 64 encode data using a given encoding table.
+ *
+ * allocate a buffer and encode source data into it using the Base64 encoding.
+ *
+ * \param input The source data to encode.
+ * \param input_length The length of the source data.
+ * \param output The buffer to recive data into, the caller must free.
+ * \param output_length The length of data placed in \a output
+ * \return NSERROR_OK on success and \a output updated else error code.
+ */
+static nsuerror
+table_encode_alloc(const uint8_t *encoding_table,
+ const uint8_t *input,
+ size_t input_length,
+ uint8_t **output,
+ size_t *output_length)
+{
+ uint8_t *encoded;
+ size_t encoded_len;
+ nsuerror res;
+
+ encoded_len = 4 * ((input_length + 2) / 3);
+
+ encoded = malloc(encoded_len);
+ if (encoded == NULL) {
+ return NSUERROR_NOMEM;
+ }
+
+ res = table_encode(encoding_table,
+ input,
+ input_length,
+ encoded,
+ &encoded_len);
+ if (res != NSUERROR_OK) {
+ free(encoded);
+ } else {
+ *output = encoded;
+ *output_length = encoded_len;
+ }
+ return res;
+}
+
/*
* standard base64 encoding
*
@@ -182,6 +229,7 @@ nsu_base64_encode_alloc(const uint8_t *input,
output_length);
}
+
/*
* url base64 encoding
*
@@ -201,6 +249,44 @@ nsu_base64_encode_alloc_url(const uint8_t *input,
}
+/*
+ * standard base64 encoding
+ *
+ * exported interface documented in nsutils/base64.h
+ */
+nsuerror
+nsu_base64_encode(const uint8_t *input,
+ size_t input_length,
+ uint8_t *output,
+ size_t *output_length)
+{
+ return table_encode(b64_encoding_table,
+ input,
+ input_length,
+ output,
+ output_length);
+}
+
+
+/*
+ * url base64 encoding
+ *
+ * exported interface documented in nsutils/base64.h
+ */
+nsuerror
+nsu_base64_encode_url(const uint8_t *input,
+ size_t input_length,
+ uint8_t *output,
+ size_t *output_length)
+{
+ return table_encode(b64url_encoding_table,
+ input,
+ input_length,
+ output,
+ output_length);
+}
+
+
/**
* Base 64 decode data with a given decoding table.
*