aboutsummaryrefslogtreecommitdiff
path: root/base64.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2024-07-25 16:32:57 +0000
committerDaniel Eklöf <daniel@ekloef.se>2024-07-25 16:32:57 +0000
commita213e14ca35e73a1f353ae97f2e398d145e19f69 (patch)
treefb1624e20a7380b18621259d3c9d5913083176ac /base64.c
parentinput: allow mouse selections to start inside the margins (diff)
parentosc+notify: strcmp() -> streq() (diff)
Merge branch 'kitty-notifications'
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;
}