summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-23 16:23:43 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-23 16:31:23 (EDT)
commite96957a78147c73e97a611e1c199fee02941b64f (patch)
tree26fbfe97f244b9064a63969aa4d299d2bb3e6ec6 /src
parent8a0e4c830fea4ffa3eaaed370be3b2256cfa925a (diff)
downloaddodge-balls-e96957a78147c73e97a611e1c199fee02941b64f.zip
dodge-balls-e96957a78147c73e97a611e1c199fee02941b64f.tar.gz
dodge-balls-e96957a78147c73e97a611e1c199fee02941b64f.tar.bz2
db_base64_decode(): New function, from boukengine
Diffstat (limited to 'src')
-rw-r--r--src/base64.c81
-rw-r--r--src/base64.h27
-rw-r--r--src/local.mk2
3 files changed, 110 insertions, 0 deletions
diff --git a/src/base64.c b/src/base64.c
new file mode 100644
index 0000000..dacd03e
--- /dev/null
+++ b/src/base64.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2013 P. J. McDermott
+ *
+ * This file is part of Dodge Balls
+ *
+ * Dodge Balls is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Dodge Balls 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Dodge Balls. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stddef.h>
+#include "base64.h"
+
+static void
+_db_base64_decode_block(const char *src, char *dest)
+{
+ int i;
+ /* GCC's SSP won't wrap functions with arrays smaller than 8 bytes. */
+ char buf[8];
+
+ /* Convert base 64 characters into values. */
+ /* Supports ASCII and EBCDIC systems. */
+ for (i = 0; i < 4; ++i) {
+ if (src[i] >= 'A' && src[i] <= 'I') {
+ buf[i] = src[i] - 'A' + 0;
+ } else if (src[i] >= 'J' && src[i] <= 'R') {
+ buf[i] = src[i] - 'J' + 9;
+ } else if (src[i] >= 'S' && src[i] <= 'Z') {
+ buf[i] = src[i] - 'S' + 18;
+ } else if (src[i] >= 'a' && src[i] <= 'i') {
+ buf[i] = src[i] - 'a' + 26;
+ } else if (src[i] >= 'j' && src[i] <= 'r') {
+ buf[i] = src[i] - 'j' + 35;
+ } else if (src[i] >= 's' && src[i] <= 'z') {
+ buf[i] = src[i] - 's' + 44;
+ } else if (src[i] >= '0' && src[i] <= '9') {
+ buf[i] = src[i] - '0' + 52;
+ } else if (src[i] == '+') {
+ buf[i] = 62;
+ } else if (src[i] == '/') {
+ buf[i] = 63;
+ } else {
+ buf[i] = '\0';
+ }
+ }
+
+ /* Map four decoded value bytes into three output bytes. */
+ dest[0] = (buf[0] << 2) | (buf[1] >> 4);
+ dest[1] = (buf[1] << 4) | (buf[2] >> 2);
+ dest[2] = (buf[2] << 6) | (buf[3] >> 0);
+}
+
+void
+db_base64_decode(const char *src, char *dest, size_t dest_len)
+{
+ size_t i;
+
+ /* Decode the input one entire block at a time. */
+ for (i = 0; i < (dest_len - 1) / 3; ++i) {
+ if (src[i * 4] == '\0' ||
+ src[i * 4 + 1] == '\0' ||
+ src[i * 4 + 2] == '\0' ||
+ src[i * 4 + 3] == '\0') {
+ dest[i * 3] = '\0';
+ break;
+ }
+ _db_base64_decode_block(src + i * 4, dest + i * 3);
+ }
+
+ /* Ensure NUL termination. */
+ dest[dest_len - 1] = '\0';
+}
diff --git a/src/base64.h b/src/base64.h
new file mode 100644
index 0000000..361a563
--- /dev/null
+++ b/src/base64.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2013 P. J. McDermott
+ *
+ * This file is part of Dodge Balls
+ *
+ * Dodge Balls is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Dodge Balls 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Dodge Balls. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DB_BASE64_H_
+#define DB_BASE64_H_
+
+#include <stddef.h>
+
+void db_base64_decode(const char *src, char *dest, size_t dest_len);
+
+#endif /* DB_BASE64_H_ */
diff --git a/src/local.mk b/src/local.mk
index c796ae7..4b6f55d 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -1,4 +1,6 @@
dodge_balls_SOURCES += \
+ %reldir%/base64.c \
+ %reldir%/base64.h \
%reldir%/collision.c \
%reldir%/collision.h \
%reldir%/defs.h \