libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-02-19 05:05:40
Commitf0b8e1b5c4493237d4fb525a67193017aae89610
Parentc20e9c703382248d3654161354392277af7c2653

base32: Add base32

Diffstat

A src/base32.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/base32.h | 42 ++++++++++++++++++++++++++++++++++++++++++

2 files changed, 147 insertions, 0 deletions

diff --git a/src/base32.c b/src/base32.c
new file mode 100644
index 0000000..5e0a619
--- /dev/null
+++ b/src/base32.c
@@ -0,0 +1,105 @@
+// base32.c, version 0.1.0
+// Base32 source 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 "base32.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const uint8_t B32E[];
+static const uint8_t B32D[];
+
+/* Encode Base32 */
+size_t b32encode(uint8_t *dst, uint8_t *src, size_t len) {
+	register uint8_t *i = src, *o = dst;
+	
+	for (; len >= 5; len -= 5) {
+		*o++ = B32E[i[0] >> 3];
+		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+		*o++ = B32E[(i[1] >> 1) & 0x1F];
+		*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
+		*o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)];
+		*o++ = B32E[(i[3] >> 2) & 0x1F];
+		*o++ = B32E[((i[3] & 0x03) << 3) | (i[4] >> 5)];
+		*o++ = B32E[i[4] & 0x1F]; i += 5;
+	}
+	
+	if (len) {
+		*o++ = B32E[i[0] >> 3];
+		
+		switch (len) {
+		case 1: {
+			*o++ = B32E[(i[0] & 0x07) << 2];
+			memset(o, '=', 6); o += 6;
+			break;
+		}
+		case 2: {
+			*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+			*o++ = B32E[(i[1] >> 1) & 0x1F];
+			*o++ = B32E[(i[1] & 0x01) << 4];
+			memset(o, '=', 4); o += 4;
+			break;
+		}
+		case 3: {
+			*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+			*o++ = B32E[(i[1] >> 1) & 0x1F];
+			*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
+			*o++ = B32E[(i[2] & 0x0F) << 1];
+			memset(o, '=', 3); o += 3;
+			break;
+		}
+		case 4: {
+			*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+			*o++ = B32E[(i[1] >> 1) & 0x1F];
+			*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
+			*o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)];
+			*o++ = B32E[(i[3] >> 2) & 0x1F];
+			*o++ = B32E[(i[3] & 0x03) << 3];
+			*o++ = '=';
+			break;
+		}
+		}
+	}
+	
+	return o - dst;
+}
+
+/* Decode Base32 */
+size_t b32decode(uint8_t *dst, uint8_t *src, size_t len) {
+	return 0;
+}
+
+static const uint8_t B32E[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+
+static const uint8_t B32D[] = {};
diff --git a/src/base32.h b/src/base32.h
new file mode 100644
index 0000000..10e8fe6
--- /dev/null
+++ b/src/base32.h
@@ -0,0 +1,42 @@
+// base32.h, version 0.1.0
+// Base32 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_BASE32_H_2PSMZTB0
+#define OMKOV_LIB_BASE32_H_2PSMZTB0
+
+#include <stdint.h>
+#include <stdlib.h>
+
+extern size_t b32encode(uint8_t *dst, uint8_t *src, size_t len);
+extern size_t b32decode(uint8_t *dst, uint8_t *src, size_t len);
+
+#endif // OMKOV_LIB_BASE32_H_2PSMZTB0