aboutsummaryrefslogtreecommitdiff
path: root/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'base64.c')
-rw-r--r--base64.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/base64.c b/base64.c
index 5d01ab07..db697cb0 100644
--- a/base64.c
+++ b/base64.c
@@ -42,7 +42,7 @@ static const char lookup[64] = {
};
char *
-base64_decode(const char *s)
+base64_decode(const char *s, size_t *size)
{
const size_t len = strlen(s);
if (unlikely(len % 4 != 0)) {
@@ -54,6 +54,9 @@ base64_decode(const char *s)
if (unlikely(ret == NULL))
return NULL;
+ if (unlikely(size != NULL))
+ *size = len / 4 * 3;
+
for (size_t i = 0, o = 0; i < len; i += 4, o += 3) {
unsigned a = reverse_lookup[(unsigned char)s[i + 0]];
unsigned b = reverse_lookup[(unsigned char)s[i + 1]];
@@ -68,6 +71,13 @@ base64_decode(const char *s)
if (unlikely(i + 4 != len || (a | b) & P || (c & P && !(d & P))))
goto invalid;
+ if (unlikely(size != NULL)) {
+ if (c & P)
+ *size = len / 4 * 3 - 2;
+ else
+ *size = len / 4 * 3 - 1;
+ }
+
c &= 63;
d &= 63;
}