about summary refs log tree commit diff
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
parentRename bind-unix to bind-path (diff)
downloadpounce-0bd6b684a7e130de2312c7e81934b81bd3964859.tar.gz
pounce-0bd6b684a7e130de2312c7e81934b81bd3964859.zip
Move base64 to bounce.h
-rw-r--r--bounce.h32
-rw-r--r--state.c35
2 files changed, 35 insertions, 32 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';
+}
diff --git a/state.c b/state.c
index c980ad7..421c23f 100644
--- a/state.c
+++ b/state.c
@@ -37,37 +37,6 @@ static void require(const struct Message *msg, bool origin, size_t len) {
 	}
 }
 
-static const char Base64[64] = {
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-};
-
-static char *base64(const byte *src, size_t len) {
-	char *dst = malloc(1 + (len + 2) / 3 * 4);
-	if (!dst) err(EX_OSERR, "malloc");
-	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';
-	return dst;
-}
-
 static char *plainBase64;
 
 void stateLogin(
@@ -80,7 +49,9 @@ void stateLogin(
 		for (size_t i = 0; auth[i]; ++i) {
 			plain[1 + i] = (auth[i] == ':' ? 0 : auth[i]);
 		}
-		plainBase64 = base64(plain, sizeof(plain));
+		plainBase64 = malloc(base64Size(sizeof(plain)));
+		if (!plainBase64) err(EX_OSERR, "malloc");
+		base64(plainBase64, plain, sizeof(plain));
 		serverFormat("CAP REQ :sasl\r\n");
 	}
 	if (pass) serverFormat("PASS :%s\r\n", pass);