libutil

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

AuthorJamozed <[email protected]>
Date2021-02-04 13:10:07
Commit0becaf1f146b7414edc56190f1352093c8a264c0
Parentb03cf63e712f2512013d2a4e4564b6a170aaf5fa

base64: Add base64

Diffstat

M CMakeLists.txt | 4 +++-
M README.md | 1 +
A src/base64.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/base64.h | 44 ++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 153 insertions, 1 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index eaec185..3d83187 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
-PROJECT(lib VERSION 0.0.0 LANGUAGES C)
+PROJECT(lib LANGUAGES C)
 
 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
@@ -11,12 +11,14 @@ ADD_LIBRARY(lib STATIC ${SOURCES})
 
 LINK_LIBRARIES(lib)
 
+ADD_EXECUTABLE(test_base64 ${PROJECT_SOURCE_DIR}/src/test/test_base64.c)
 ADD_EXECUTABLE(test_crypt  ${PROJECT_SOURCE_DIR}/src/test/test_crypt.c)
 ADD_EXECUTABLE(test_endian ${PROJECT_SOURCE_DIR}/src/test/test_endian.c)
 ADD_EXECUTABLE(test_error  ${PROJECT_SOURCE_DIR}/src/test/test_error.c)
 ADD_EXECUTABLE(test_optget ${PROJECT_SOURCE_DIR}/src/test/test_optget.c)
 
 ENABLE_TESTING()
+ADD_TEST(NAME test_base64 COMMAND test_base64)
 ADD_TEST(NAME test_crypt  COMMAND test_crypt)
 ADD_TEST(NAME test_endian COMMAND test_endian)
 ADD_TEST(NAME test_error  COMMAND test_error)
diff --git a/README.md b/README.md
index 8e4b96f..9db9f52 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,7 @@ OMKOV lib is a lightweight generic library to be included locally in projects.
 
 | Component        | Description                                         |
 | ---------------- | --------------------------------------------------- |
+| base64           | Encode or decode Base64                             |
 | crypt            | Cryptography functions                              |
 | endian           | Endianness related functions                        |
 | error            | Error reporting functions                           |
diff --git a/src/base64.c b/src/base64.c
new file mode 100644
index 0000000..19b6d0f
--- /dev/null
+++ b/src/base64.c
@@ -0,0 +1,105 @@
+// base64.c, version 1.0.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 */
+void 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;
+}
+
+/* Decode Base64 */
+void b64decode(uint8_t *dst, uint8_t *src, size_t len) {
+	if (!len) { *src = 0; return; }
+	
+	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;
+}
+
+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/base64.h b/src/base64.h
new file mode 100644
index 0000000..2717dce
--- /dev/null
+++ b/src/base64.h
@@ -0,0 +1,44 @@
+// base64.h, version 1.0.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>
+
+#define B64ELEN(x) ((x + 2) / 3 * 4)
+
+extern void b64encode(uint8_t *dst, uint8_t *src, size_t len);
+extern void b64decode(uint8_t *dst, uint8_t *src, size_t len);
+
+#endif // OMKOV_LIB_BASE64_H_5YQ80JRV