Browse Source

Implement base64 encoding

Will be used for SASL authentication.
June 1 week ago
parent
commit
a8c30b898c
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
3 changed files with 64 additions and 0 deletions
  1. 1
    0
      Makefile
  2. 3
    0
      chat.h
  3. 60
    0
      pls.c

+ 1
- 0
Makefile View File

@@ -30,6 +30,7 @@ OBJS += ui.o
30 30
 OBJS += url.o
31 31
 
32 32
 TESTS += format.t
33
+TESTS += pls.t
33 34
 TESTS += term.t
34 35
 
35 36
 all: tags $(BINS) test

+ 3
- 0
chat.h View File

@@ -30,6 +30,8 @@
30 30
 #define err(...) do { uiHide(); err(__VA_ARGS__); } while (0)
31 31
 #define errx(...) do { uiHide(); errx(__VA_ARGS__); } while (0)
32 32
 
33
+typedef unsigned char byte;
34
+
33 35
 struct {
34 36
 	char *host;
35 37
 	char *port;
@@ -193,6 +195,7 @@ char *awcstombs(const wchar_t *src);
193 195
 char *awcsntombs(const wchar_t *src, size_t nwc);
194 196
 int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);
195 197
 int aswprintf(wchar_t **ret, const wchar_t *format, ...);
198
+char *base64(const byte *src, size_t len);
196 199
 
197 200
 // HACK: clang won't check wchar_t *format strings.
198 201
 #ifdef NDEBUG

+ 60
- 0
pls.c View File

@@ -131,3 +131,63 @@ int aswprintf(wchar_t **ret, const wchar_t *format, ...) {
131 131
 	va_end(ap);
132 132
 	return n;
133 133
 }
134
+
135
+static const char Base64[64] =
136
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
137
+
138
+char *base64(const byte *src, size_t len) {
139
+	char *dst = malloc(1 + 4 * (len + 2) / 3);
140
+	if (!dst) return NULL;
141
+	size_t i = 0;
142
+	while (len > 2) {
143
+		dst[i++] = Base64[0x3F & (src[0] >> 2)];
144
+		dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
145
+		dst[i++] = Base64[0x3F & (src[1] << 2 | src[2] >> 6)];
146
+		dst[i++] = Base64[0x3F & src[2]];
147
+		src += 3;
148
+		len -= 3;
149
+	}
150
+	if (len) {
151
+		dst[i++] = Base64[0x3F & (src[0] >> 2)];
152
+		if (len > 1) {
153
+			dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
154
+			dst[i++] = Base64[0x3F & (src[1] << 2)];
155
+		} else {
156
+			dst[i++] = Base64[0x3F & (src[0] << 4)];
157
+			dst[i++] = '=';
158
+		}
159
+		dst[i++] = '=';
160
+	}
161
+	dst[i] = '\0';
162
+	return dst;
163
+}
164
+
165
+#ifdef TEST
166
+#include <assert.h>
167
+#include <string.h>
168
+
169
+int main() {
170
+	char *cat = base64((byte *)"cat", 3);
171
+	char *ca = base64((byte *)"ca", 2);
172
+	char *c = base64((byte *)"c", 1);
173
+	assert(cat);
174
+	assert(ca);
175
+	assert(c);
176
+	assert(!strcmp("Y2F0", cat));
177
+	assert(!strcmp("Y2E=", ca));
178
+	assert(!strcmp("Yw==", c));
179
+	free(cat);
180
+	free(ca);
181
+	free(c);
182
+
183
+	char *fzf = base64((byte []) { 0xFF, 0x00, 0xFF }, 3);
184
+	char *zfz = base64((byte []) { 0x00, 0xFF, 0x00 }, 3);
185
+	assert(fzf);
186
+	assert(zfz);
187
+	assert(!strcmp("/wD/", fzf));
188
+	assert(!strcmp("AP8A", zfz));
189
+	free(fzf);
190
+	free(zfz);
191
+}
192
+
193
+#endif

Loading…
Cancel
Save