Browse Source

Move base64 back to pls.c

Causal Agent 5 months ago
parent
commit
ef916ddaff
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
6 changed files with 57 additions and 82 deletions
  1. 1
    2
      Makefile
  2. 0
    1
      README
  3. 0
    2
      catgirl.7
  4. 3
    3
      chat.h
  5. 53
    0
      pls.c
  6. 0
    74
      string.c

+ 1
- 2
Makefile View File

@@ -23,7 +23,6 @@ OBJS += input.o
23 23
 OBJS += irc.o
24 24
 OBJS += log.o
25 25
 OBJS += pls.o
26
-OBJS += string.o
27 26
 OBJS += tab.o
28 27
 OBJS += tag.o
29 28
 OBJS += term.o
@@ -31,7 +30,7 @@ OBJS += ui.o
31 30
 OBJS += url.o
32 31
 
33 32
 TESTS += format.t
34
-TESTS += string.t
33
+TESTS += pls.t
35 34
 TESTS += term.t
36 35
 
37 36
 all: tags $(BINS) test

+ 0
- 1
README View File

@@ -31,7 +31,6 @@ FILES
31 31
      edit.c     line editing
32 32
      tab.c      tab-complete
33 33
      url.c      URL detection
34
-     string.c   base64 and rot13
35 34
      pls.c      functions which should not have to be written
36 35
      sandman.m  utility for Darwin to signal sleep
37 36
 

+ 0
- 2
catgirl.7 View File

@@ -66,8 +66,6 @@ line editing
66 66
 tab-complete
67 67
 .It Pa url.c
68 68
 URL detection
69
-.It Pa string.c
70
-base64 and rot13
71 69
 .It Pa pls.c
72 70
 functions which should not have to be written
73 71
 .It Pa sandman.m

+ 3
- 3
chat.h View File

@@ -190,9 +190,6 @@ void logFmt(
190 190
 ) __attribute__((format(printf, 3, 4)));
191 191
 void logReplay(struct Tag tag);
192 192
 
193
-size_t base64Size(size_t len);
194
-void base64(char *dst, const byte *src, size_t len);
195
-
196 193
 wchar_t *wcsnchr(const wchar_t *wcs, size_t len, wchar_t chr);
197 194
 wchar_t *wcsnrchr(const wchar_t *wcs, size_t len, wchar_t chr);
198 195
 wchar_t *ambstowcs(const char *src);
@@ -201,6 +198,9 @@ char *awcsntombs(const wchar_t *src, size_t nwc);
201 198
 int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);
202 199
 int aswprintf(wchar_t **ret, const wchar_t *format, ...);
203 200
 
201
+size_t base64Size(size_t len);
202
+void base64(char *dst, const byte *src, size_t len);
203
+
204 204
 // HACK: clang won't check wchar_t *format strings.
205 205
 #ifdef NDEBUG
206 206
 #define uiFmt(tag, heat, format, ...) uiFmt(tag, heat, L##format, __VA_ARGS__)

+ 53
- 0
pls.c View File

@@ -131,3 +131,56 @@ 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
+
139
+size_t base64Size(size_t len) {
140
+	return 1 + (len + 2) / 3 * 4;
141
+}
142
+
143
+void base64(char *dst, const byte *src, size_t len) {
144
+	size_t i = 0;
145
+	while (len > 2) {
146
+		dst[i++] = Base64[0x3F & (src[0] >> 2)];
147
+		dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
148
+		dst[i++] = Base64[0x3F & (src[1] << 2 | src[2] >> 6)];
149
+		dst[i++] = Base64[0x3F & src[2]];
150
+		src += 3;
151
+		len -= 3;
152
+	}
153
+	if (len) {
154
+		dst[i++] = Base64[0x3F & (src[0] >> 2)];
155
+		if (len > 1) {
156
+			dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
157
+			dst[i++] = Base64[0x3F & (src[1] << 2)];
158
+		} else {
159
+			dst[i++] = Base64[0x3F & (src[0] << 4)];
160
+			dst[i++] = '=';
161
+		}
162
+		dst[i++] = '=';
163
+	}
164
+	dst[i] = '\0';
165
+}
166
+
167
+#ifdef TEST
168
+#include <assert.h>
169
+#include <string.h>
170
+
171
+int main() {
172
+	assert(5 == base64Size(1));
173
+	assert(5 == base64Size(2));
174
+	assert(5 == base64Size(3));
175
+	assert(9 == base64Size(4));
176
+
177
+	char b64[base64Size(3)];
178
+	assert((base64(b64, (byte *)"cat", 3), !strcmp("Y2F0", b64)));
179
+	assert((base64(b64, (byte *)"ca", 2), !strcmp("Y2E=", b64)));
180
+	assert((base64(b64, (byte *)"c", 1), !strcmp("Yw==", b64)));
181
+
182
+	assert((base64(b64, (byte *)"\xFF\x00\xFF", 3), !strcmp("/wD/", b64)));
183
+	assert((base64(b64, (byte *)"\x00\xFF\x00", 3), !strcmp("AP8A", b64)));
184
+}
185
+
186
+#endif

+ 0
- 74
string.c View File

@@ -1,74 +0,0 @@
1
-/* Copyright (C) 2018  C. McEnroe <june@causal.agency>
2
- *
3
- * This program is free software: you can redistribute it and/or modify
4
- * it under the terms of the GNU Affero General Public License as published by
5
- * the Free Software Foundation, either version 3 of the License, or
6
- * (at your option) any later version.
7
- *
8
- * This program is distributed in the hope that it will be useful,
9
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
- * GNU Affero General Public License for more details.
12
- *
13
- * You should have received a copy of the GNU Affero General Public License
14
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
- */
16
-
17
-#include <err.h>
18
-#include <stdlib.h>
19
-#include <string.h>
20
-#include <sysexits.h>
21
-
22
-#include "chat.h"
23
-
24
-static const char Base64[64] = {
25
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
26
-};
27
-
28
-size_t base64Size(size_t len) {
29
-	return 1 + (len + 2) / 3 * 4;
30
-}
31
-
32
-void base64(char *dst, const byte *src, size_t len) {
33
-	size_t i = 0;
34
-	while (len > 2) {
35
-		dst[i++] = Base64[0x3F & (src[0] >> 2)];
36
-		dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
37
-		dst[i++] = Base64[0x3F & (src[1] << 2 | src[2] >> 6)];
38
-		dst[i++] = Base64[0x3F & src[2]];
39
-		src += 3;
40
-		len -= 3;
41
-	}
42
-	if (len) {
43
-		dst[i++] = Base64[0x3F & (src[0] >> 2)];
44
-		if (len > 1) {
45
-			dst[i++] = Base64[0x3F & (src[0] << 4 | src[1] >> 4)];
46
-			dst[i++] = Base64[0x3F & (src[1] << 2)];
47
-		} else {
48
-			dst[i++] = Base64[0x3F & (src[0] << 4)];
49
-			dst[i++] = '=';
50
-		}
51
-		dst[i++] = '=';
52
-	}
53
-	dst[i] = '\0';
54
-}
55
-
56
-#ifdef TEST
57
-#include <assert.h>
58
-
59
-int main() {
60
-	assert(5 == base64Size(1));
61
-	assert(5 == base64Size(2));
62
-	assert(5 == base64Size(3));
63
-	assert(9 == base64Size(4));
64
-
65
-	char b64[base64Size(3)];
66
-	assert((base64(b64, (byte *)"cat", 3), !strcmp("Y2F0", b64)));
67
-	assert((base64(b64, (byte *)"ca", 2), !strcmp("Y2E=", b64)));
68
-	assert((base64(b64, (byte *)"c", 1), !strcmp("Yw==", b64)));
69
-
70
-	assert((base64(b64, (byte *)"\xFF\x00\xFF", 3), !strcmp("/wD/", b64)));
71
-	assert((base64(b64, (byte *)"\x00\xFF\x00", 3), !strcmp("AP8A", b64)));
72
-}
73
-
74
-#endif

Loading…
Cancel
Save