diff options
author | June McEnroe <june@causal.agency> | 2018-12-16 16:57:43 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2018-12-16 16:57:43 -0500 |
commit | ef916ddaff2c651c41e4737cdb50abb06365f8b3 (patch) | |
tree | 9b52bcfbd48276c65091d0d40c2c7aaa148f3b75 | |
parent | Move base64 to string.c (diff) | |
download | catgirl-ef916ddaff2c651c41e4737cdb50abb06365f8b3.tar.gz catgirl-ef916ddaff2c651c41e4737cdb50abb06365f8b3.zip |
Move base64 back to pls.c
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | catgirl.7 | 2 | ||||
-rw-r--r-- | chat.h | 6 | ||||
-rw-r--r-- | pls.c | 53 | ||||
-rw-r--r-- | string.c | 74 |
6 files changed, 57 insertions, 82 deletions
diff --git a/Makefile b/Makefile index 9f2814b..cd96001 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,6 @@ OBJS += input.o OBJS += irc.o OBJS += log.o OBJS += pls.o -OBJS += string.o OBJS += tab.o OBJS += tag.o OBJS += term.o @@ -31,7 +30,7 @@ OBJS += ui.o OBJS += url.o TESTS += format.t -TESTS += string.t +TESTS += pls.t TESTS += term.t all: tags $(BINS) test diff --git a/README b/README index 4b9f913..6a14ed2 100644 --- a/README +++ b/README @@ -31,7 +31,6 @@ FILES edit.c line editing tab.c tab-complete url.c URL detection - string.c base64 and rot13 pls.c functions which should not have to be written sandman.m utility for Darwin to signal sleep diff --git a/catgirl.7 b/catgirl.7 index 32fda39..4151b80 100644 --- a/catgirl.7 +++ b/catgirl.7 @@ -66,8 +66,6 @@ line editing tab-complete .It Pa url.c URL detection -.It Pa string.c -base64 and rot13 .It Pa pls.c functions which should not have to be written .It Pa sandman.m diff --git a/chat.h b/chat.h index 9bce7a0..441056c 100644 --- a/chat.h +++ b/chat.h @@ -190,9 +190,6 @@ void logFmt( ) __attribute__((format(printf, 3, 4))); void logReplay(struct Tag tag); -size_t base64Size(size_t len); -void base64(char *dst, const byte *src, size_t len); - wchar_t *wcsnchr(const wchar_t *wcs, size_t len, wchar_t chr); wchar_t *wcsnrchr(const wchar_t *wcs, size_t len, wchar_t chr); wchar_t *ambstowcs(const char *src); @@ -201,6 +198,9 @@ char *awcsntombs(const wchar_t *src, size_t nwc); int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap); int aswprintf(wchar_t **ret, const wchar_t *format, ...); +size_t base64Size(size_t len); +void base64(char *dst, const byte *src, size_t len); + // HACK: clang won't check wchar_t *format strings. #ifdef NDEBUG #define uiFmt(tag, heat, format, ...) uiFmt(tag, heat, L##format, __VA_ARGS__) diff --git a/pls.c b/pls.c index d91fc97..d553c5c 100644 --- a/pls.c +++ b/pls.c @@ -131,3 +131,56 @@ int aswprintf(wchar_t **ret, const wchar_t *format, ...) { va_end(ap); return n; } + +static const char Base64[64] = { + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" +}; + +size_t base64Size(size_t len) { + return 1 + (len + 2) / 3 * 4; +} + +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'; +} + +#ifdef TEST +#include <assert.h> +#include <string.h> + +int main() { + assert(5 == base64Size(1)); + assert(5 == base64Size(2)); + assert(5 == base64Size(3)); + assert(9 == base64Size(4)); + + char b64[base64Size(3)]; + assert((base64(b64, (byte *)"cat", 3), !strcmp("Y2F0", b64))); + assert((base64(b64, (byte *)"ca", 2), !strcmp("Y2E=", b64))); + assert((base64(b64, (byte *)"c", 1), !strcmp("Yw==", b64))); + + assert((base64(b64, (byte *)"\xFF\x00\xFF", 3), !strcmp("/wD/", b64))); + assert((base64(b64, (byte *)"\x00\xFF\x00", 3), !strcmp("AP8A", b64))); +} + +#endif diff --git a/string.c b/string.c deleted file mode 100644 index 4a6d9d8..0000000 --- a/string.c +++ /dev/null @@ -1,74 +0,0 @@ -/* Copyright (C) 2018 C. McEnroe <june@causal.agency> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <err.h> -#include <stdlib.h> -#include <string.h> -#include <sysexits.h> - -#include "chat.h" - -static const char Base64[64] = { - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" -}; - -size_t base64Size(size_t len) { - return 1 + (len + 2) / 3 * 4; -} - -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'; -} - -#ifdef TEST -#include <assert.h> - -int main() { - assert(5 == base64Size(1)); - assert(5 == base64Size(2)); - assert(5 == base64Size(3)); - assert(9 == base64Size(4)); - - char b64[base64Size(3)]; - assert((base64(b64, (byte *)"cat", 3), !strcmp("Y2F0", b64))); - assert((base64(b64, (byte *)"ca", 2), !strcmp("Y2E=", b64))); - assert((base64(b64, (byte *)"c", 1), !strcmp("Yw==", b64))); - - assert((base64(b64, (byte *)"\xFF\x00\xFF", 3), !strcmp("/wD/", b64))); - assert((base64(b64, (byte *)"\x00\xFF\x00", 3), !strcmp("AP8A", b64))); -} - -#endif |