coreutils

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

AuthorJamozed <[email protected]>
Date2021-02-05 11:50:02
Commit2b96ababa2b0cf4e8c2bd4465841007e9848f271
Parent972e4429ba84618d1fe6e8922ebc22d69548a5de

lib: Add base64 files

Diffstat

A src/lib/base64.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/lib/base64.h | 42 ++++++++++++++++++++++++++++++++++++++++++

2 files changed, 147 insertions, 0 deletions

diff --git a/src/lib/base64.c b/src/lib/base64.c
new file mode 100644
index 0000000..8446847
--- /dev/null
+++ b/src/lib/base64.c
@@ -0,0 +1,105 @@
+// base64.c, version 1.1.0
+// Base64 header file for OMKOV lib
+// 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.
+*/
+
+#include "base64.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+static const uint8_t B64E[];
+static const uint8_t B64D[];
+
+/* Encode Base64 */
+size_t b64encode(uint8_t *dst, uint8_t *src, size_t len) {
+	register uint8_t *i = src, *o = dst;
+	
+	for (; len >= 3; len -= 3) {
+		*o++ = B64E[i[0] >> 2];
+		*o++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
+		*o++ = B64E[((i[1] & 0x0F) << 2) | (i[2] >> 6)];
+		*o++ = B64E[i[2] & 0x3F]; i += 3;
+	}
+	
+	if (len) {
+		*o++ = B64E[i[0] >> 2];
+		
+		if (len == 1) {
+			*o++ = B64E[(i[0] & 0x03) << 4]; *o++ = '=';
+		}
+		else {
+			*o++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
+			*o++ = B64E[(i[1] & 0x0F) << 2];
+		} *o++ = '=';
+	}
+	
+	return o - dst;
+}
+
+/* Decode Base64 */
+size_t b64decode(uint8_t *dst, uint8_t *src, size_t len) {
+	if (!len) { return 0; }
+	
+	register uint8_t *o = dst;
+	
+	bool pad1 = len % 4 || src[len - 1] == '=';
+	bool pad2 = pad1 && (len % 4 > 2 || src[len - 2] != '=');
+	
+	size_t end = (len - pad1) / 4 << 2;
+	
+	for (size_t j = 0; j != end; j += 4) {
+		*o++ = B64D[src[j + 0]] << 2 | B64D[src[j + 1]] >> 4;
+		*o++ = B64D[src[j + 1]] << 4 | B64D[src[j + 2]] >> 2;
+		*o++ = B64D[src[j + 2]] << 6 | B64D[src[j + 3]];
+	}
+	
+	if (pad1) {
+		int n = B64D[src[end]] << 18 | B64D[src[end + 1]] << 12; *o++ = n >> 16;
+		if (pad2) { n |= B64D[src[end + 2]] << 6; *o++ = n >> 8 & 0xFF; }
+    }
+	
+	return o - dst;
+}
+
+static const uint8_t B64E[] =
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+static const uint8_t B64D[] = {
+	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 62, 63, 62, 62, 63,
+	52, 53, 54, 55, 56, 57, 58, 59, 60, 61,  0,  0,  0,  0,  0,  0,
+	 0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
+	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,  0,  0,  0, 63,
+	 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
+};
diff --git a/src/lib/base64.h b/src/lib/base64.h
new file mode 100644
index 0000000..c56d726
--- /dev/null
+++ b/src/lib/base64.h
@@ -0,0 +1,42 @@
+// base64.h, version 1.1.0
+// Base64 header file for OMKOV lib
+// 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.
+*/
+
+#ifndef OMKOV_LIB_BASE64_H_5YQ80JRV
+#define OMKOV_LIB_BASE64_H_5YQ80JRV
+
+#include <stdint.h>
+#include <stdlib.h>
+
+extern size_t b64encode(uint8_t *dst, uint8_t *src, size_t len);
+extern size_t b64decode(uint8_t *dst, uint8_t *src, size_t len);
+
+#endif // OMKOV_LIB_BASE64_H_5YQ80JRV