aboutsummaryrefslogtreecommitdiff
path: root/base64.c
diff options
context:
space:
mode:
authorCraig Barnes <craigbarnes@protonmail.com>2020-08-08 19:34:30 +0000
committerCraig Barnes <craigbarnes@protonmail.com>2020-08-08 19:37:57 +0000
commit7a77958ba20c2b344be5e66e69e2f212a4bbf6b3 (patch)
treecdb8191177f5f9ca69ea7858f9543f36fcbcf616 /base64.c
parentchangelog: add birger to 'contributers' for the upcoming release (diff)
Convert most dynamic allocations to use functions from xmalloc.h
Diffstat (limited to 'base64.c')
-rw-r--r--base64.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/base64.c b/base64.c
index 214d6943..3acfbe34 100644
--- a/base64.c
+++ b/base64.c
@@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
+#include <errno.h>
#define LOG_MODULE "base64"
#define LOG_ENABLE_DBG 0
@@ -47,10 +48,14 @@ base64_decode(const char *s)
{
const size_t len = strlen(s);
- if (len % 4 != 0)
+ if (len % 4 != 0) {
+ errno = EINVAL;
return NULL;
+ }
char *ret = malloc(len / 4 * 3 + 1);
+ if (unlikely(ret == NULL))
+ return NULL;
for (size_t i = 0, o = 0; i < len; i += 4, o += 3) {
unsigned char c0 = s[i + 0];
@@ -60,6 +65,7 @@ base64_decode(const char *s)
if (!is_valid(c0) || !is_valid(c1) || !is_valid(c2) || !is_valid(c3)) {
free(ret);
+ errno = EINVAL;
return NULL;
}
@@ -87,10 +93,12 @@ char *
base64_encode(const uint8_t *data, size_t size)
{
assert(size % 3 == 0);
- if (size %3 != 0)
+ if (unlikely(size % 3 != 0))
return NULL;
char *ret = malloc(size / 3 * 4 + 1);
+ if (unlikely(ret == NULL))
+ return NULL;
for (size_t i = 0, o = 0; i < size; i += 3, o += 4) {
int x = data[i + 0];