aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-19 09:11:25 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-19 09:11:25 +0000
commitbb8a733758f1ca3137d2aef2665a05aa5b3859eb (patch)
tree702ed937fd770c5339ae75591373b77afe4c6b7c
parentosc: foreground/background reply is now in correct XParseColor format (diff)
base64: initial base64 decode implementation
-rw-r--r--base64.c88
-rw-r--r--base64.h3
-rw-r--r--meson.build1
3 files changed, 92 insertions, 0 deletions
diff --git a/base64.c b/base64.c
new file mode 100644
index 00000000..1ea8e9c7
--- /dev/null
+++ b/base64.c
@@ -0,0 +1,88 @@
+#include "base64.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#define LOG_MODULE "base64"
+#define LOG_ENABLE_DBG 0
+#include "log.h"
+
+static const uint8_t reverse_lookup[256] = {
+ ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3, ['E'] = 4, ['F'] = 5, ['G'] = 6,
+ ['H'] = 7, ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11, ['M'] = 12, ['N'] = 13,
+ ['O'] = 14, ['P'] = 15, ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19, ['U'] = 20,
+ ['V'] = 21, ['W'] = 22, ['X'] = 23, ['Y'] = 24, ['Z'] = 25,
+
+ ['a'] = 26, ['b'] = 27, ['c'] = 28, ['d'] = 29, ['e'] = 30, ['f'] = 31, ['g'] = 32,
+ ['h'] = 33, ['i'] = 34, ['j'] = 35, ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39,
+ ['o'] = 40, ['p'] = 41, ['q'] = 42, ['r'] = 43, ['s'] = 44, ['t'] = 45, ['u'] = 46,
+ ['v'] = 47, ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51,
+
+ ['0'] = 52, ['1'] = 53, ['2'] = 54, ['3'] = 55, ['4'] = 56, ['5'] = 57,
+ ['6'] = 58, ['7'] = 59, ['8'] = 60, ['9'] = 61,
+
+ ['+'] = 62, ['/'] = 63,
+};
+
+#if 0
+static const char lookup[64] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ '+', '/',
+};
+#endif
+
+static inline bool
+is_valid(char c)
+{
+ return ((c >= 'A' && c <= 'Z') ||
+ (c >= 'a' && c <= 'z') ||
+ (c >= '0' && c <= '9') ||
+ c == '+' || c == '/' || c == '=');
+}
+
+char *
+base64_decode(const char *s)
+{
+ const size_t len = strlen(s);
+
+ if (len % 4 != 0)
+ return NULL;
+
+ char *ret = malloc(len / 4 * 3 + 1);
+
+ for (size_t i = 0, o = 0; i < len; i += 4, o += 3) {
+ unsigned char c0 = s[i + 0];
+ unsigned char c1 = s[i + 1];
+ unsigned char c2 = s[i + 2];
+ unsigned char c3 = s[i + 3];
+
+ if (!is_valid(c0) || !is_valid(c1) || !is_valid(c2) || !is_valid(c3)) {
+ free(ret);
+ return NULL;
+ }
+
+ unsigned a = reverse_lookup[c0];
+ unsigned b = reverse_lookup[c1];
+ unsigned c = reverse_lookup[c2];
+ unsigned d = reverse_lookup[c3];
+
+ uint32_t v = a << 18 | b << 12 | c << 6 | d << 0;
+ char x = (v >> 16) & 0xff;
+ char y = (v >> 8) & 0xff;
+ char z = (v >> 0) & 0xff;
+
+ LOG_DBG("%c%c%c", x, y, z);
+ ret[o + 0] = x;
+ ret[o + 1] = y;
+ ret[o + 2] = z;
+ }
+
+ ret[len / 4 * 3] = '\0';
+ return ret;
+}
diff --git a/base64.h b/base64.h
new file mode 100644
index 00000000..e7a3613b
--- /dev/null
+++ b/base64.h
@@ -0,0 +1,3 @@
+#pragma once
+
+char *base64_decode(const char *s);
diff --git a/meson.build b/meson.build
index 4e074458..9b1aa679 100644
--- a/meson.build
+++ b/meson.build
@@ -59,6 +59,7 @@ endforeach
executable(
'foot',
+ 'base64.c', 'base64.h',
'config.c', 'config.h',
'commands.c', 'commands.h',
'csi.c', 'csi.h',