summary refs log tree commit diff
path: root/handle.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-11 17:40:08 -0500
committerJune McEnroe <june@causal.agency>2020-02-11 17:40:08 -0500
commit83a8952cf5fa7e1e8147d322d80bdecbe0a1a217 (patch)
treee1edd9f4351507382c6cc4a2d2b3c4ea65e9da8f /handle.c
parentMove XDG_SUBDIR out of chat.h (diff)
downloadcatgirl-83a8952cf5fa7e1e8147d322d80bdecbe0a1a217.tar.gz
catgirl-83a8952cf5fa7e1e8147d322d80bdecbe0a1a217.zip
Move base64 out of chat.h
Diffstat (limited to '')
-rw-r--r--handle.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/handle.c b/handle.c
index f76a181..77f0806 100644
--- a/handle.c
+++ b/handle.c
@@ -118,6 +118,35 @@ static void handleCap(struct Message *msg) {
 	}
 }
 
+#define BASE64_SIZE(len) (1 + ((len) + 2) / 3 * 4)
+
+static void base64(char *dst, const byte *src, size_t len) {
+	static const char Base64[64] = {
+		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+	};
+	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';
+}
+
 static void handleAuthenticate(struct Message *msg) {
 	(void)msg;
 	if (!self.plain) {