coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-01-11 02:26:50
Commit195ab4cb1b7d7b23da0f3dd5dd1f8ab6dee22250
Parent1fcd34999646572e7020e8eeaca3453cc6e353c9

base64: Add base64 utility

Base64 decoding is not implemented at this stage.

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A src/base64.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3 files changed, 155 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b103212..72bee82 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,7 @@ ADD_LIBRARY(lib STATIC ${LIBSRC})
 
 LINK_LIBRARIES(lib)
 
+ADD_EXECUTABLE(base64   ${PROJECT_SOURCE_DIR}/src/base64.c)
 ADD_EXECUTABLE(basename ${PROJECT_SOURCE_DIR}/src/basename.c)
 ADD_EXECUTABLE(cat      ${PROJECT_SOURCE_DIR}/src/cat.c)
 ADD_EXECUTABLE(chmod    ${PROJECT_SOURCE_DIR}/src/chmod.c)
diff --git a/README.md b/README.md
index 7b76911..3d60fd1 100644
--- a/README.md
+++ b/README.md
@@ -9,6 +9,7 @@ UNIX-like systems.
 
 | Utility          | Description                              | Standard |
 | ---------------- | ---------------------------------------- | -------- |
+| base64*          | Base64 encode or decode data             |          |
 | basename         | Return non-directory portion of a path   | POSIX    |
 | cat              | Concatenate and print files              | POSIX    |
 | chmod            | Change the file modes                    | POSIX    |
diff --git a/src/base64.c b/src/base64.c
new file mode 100644
index 0000000..6ec968c
--- /dev/null
+++ b/src/base64.c
@@ -0,0 +1,153 @@
+// base64.c, version 0.1.0
+// OMKOV coreutils base64
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+/*
+	TODO Implement Base64 decoding.
+*/
+
+#include "lib/error.h"
+#include "lib/optget.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#define VERSION "0.1.0"
+
+static struct lop lops[] = {
+	{ "help",    ARG_NUL, 256 },
+	{ "version", ARG_NUL, 257 },
+	{ NULL, 0, 0 }
+};
+
+static const uint8_t B64E[];
+
+static bool dflag;
+
+static inline int base64(const char *file);
+static void encodeBase64(FILE *fi);
+static void decodeBase64(FILE *fi);
+
+static void hlp(void);
+static void ver(void);
+
+int main(int ac, char *av[]) { A0 = av[0];
+	struct opt opt = OPTGET_INIT; opt.str = "d"; opt.lops = lops;
+	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
+	case 'd': { dflag = true; break; }
+	case 256: { hlp(); return 0; }
+	case 257: { ver(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (opt.ind == ac) { base64(NULL); }
+	else if (opt.ind != ac - 1) { error(1, "extra operand"); }
+	else if (base64(av[opt.ind])) { error(1, "%s: %s", av[opt.ind], serr()); }
+	
+	return 0;
+}
+
+/*
+	Base64 encode or decode a file.
+	If the file path given is null, then use stdin.
+*/
+static inline int base64(const char *file) {
+	FILE *fi;
+	
+	if (file == NULL) { fi = stdin; }
+	else if (!(fi = fopen(file, "r"))) { return 1; }
+	
+	if (dflag) { decodeBase64(fi); }
+	else { encodeBase64(fi); }
+	
+	if (fi != stdin) { fclose(fi); } return 0;
+}
+
+/* Encode base64 using fixed size buffers. */
+static void encodeBase64(FILE *fi) {
+	uint8_t ibuf[BUFSIZ * 6]; int l = 0;
+	uint8_t obuf[BUFSIZ * 8 + (BUFSIZ * 8 / 76)];
+	
+	for (size_t c; (c = fread(ibuf, 1, sizeof (ibuf), fi)) != 0;) {
+		uint8_t *i = ibuf; uint8_t *j = obuf;
+		
+		while (c >= 3) {
+			*j++ = B64E[i[0] >> 2];
+			*j++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
+			*j++ = B64E[((i[1] & 0x0F) << 2) | (i[2] >> 6)];
+			*j++ = B64E[i[2] & 0x3F]; i += 3;
+			 
+			c -= 3; l += 4; if (l == 76) { *j++ = '\n'; l = 0; }
+		}
+		
+		if (c) {
+			*j++ = B64E[i[0] >> 2];
+			if (c == 1) { *j++ = B64E[(i[0] & 0x03) << 4]; *j++ = '='; }
+			else {
+				*j++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
+				*j++ = B64E[(i[1] & 0x0F) << 2];
+			} *j++ = '='; l += 4;
+		}
+		
+		fwrite(obuf, 1, j - obuf, stdout);
+	}
+	
+	if (l) { fputc('\n', stdout); } return;
+}
+
+/* Decode base64 using fixed size buffers. */
+static void decodeBase64(FILE *fi) {
+	return;
+}
+
+/* Print help information */
+static void hlp(void) {
+	puts("base64 - base64 encode or decode data\n");
+	puts("usage: base64 [-d] [file]\n");
+	puts("options:");
+	puts("  -d         Decode data (unimplemented)");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+/* Print version information */
+static void ver(void) {
+	puts("OMKOV coreutils base64, version " VERSION);
+	puts("Copyright (C) 2021, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}
+
+static const uint8_t B64E[] =
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";