summary refs log tree commit diff
path: root/bounce.h
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-11-04 16:56:12 -0500
committerJune McEnroe <june@causal.agency>2019-11-04 16:56:12 -0500
commit0bd6b684a7e130de2312c7e81934b81bd3964859 (patch)
treef9435816b6d847dac79503ae8061804474f4e779 /bounce.h
parentRename bind-unix to bind-path (diff)
downloadpounce-0bd6b684a7e130de2312c7e81934b81bd3964859.tar.gz
pounce-0bd6b684a7e130de2312c7e81934b81bd3964859.zip
Move base64 to bounce.h
Diffstat (limited to '')
-rw-r--r--bounce.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/bounce.h b/bounce.h
index d07c2cb..b8c2298 100644
--- a/bounce.h
+++ b/bounce.h
@@ -111,3 +111,35 @@ int getopt_config(
 	int argc, char *const *argv,
 	const char *optstring, const struct option *longopts, int *longindex
 );
+
+static const char Base64[64] = {
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+};
+
+static inline size_t base64Size(size_t len) {
+	return 1 + (len + 2) / 3 * 4;
+}
+
+static inline void base64(char *dst, const byte *src, size_t len) {
+	size_t i = 0;
+	while (len > 2) {
+		dst[i++] = Base64[0x3F & (src[0] >> 2)];
+		dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
+		dst[i++] = Base64[0x3F & (src[1] << 2 | src[2] >> 6)];
+		dst[i++] = Base64[0x3F & src[2]];
+		src += 3;
+		len -= 3;
+	}
+	if (len) {
+		dst[i++] = Base64[0x3F & (src[0] >> 2)];
+		if (len > 1) {
+			dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
+			dst[i++] = Base64[0x3F & (src[1] << 2)];
+		} else {
+			dst[i++] = Base64[0x3F & (src[0] << 4)];
+			dst[i++] = '=';
+		}
+		dst[i++] = '=';
+	}
+	dst[i] = '\0';
+}