aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am4
-rw-r--r--lib/base64.c50
-rw-r--r--lib/base64.h4
3 files changed, 56 insertions, 2 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9dd3a0c2..1b309846 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -5,8 +5,8 @@ SUBDIRS = tests
noinst_LIBRARIES = libnagiosplug.a
-libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c
-EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h
+libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c base64.c
+EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h base64.h
INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
diff --git a/lib/base64.c b/lib/base64.c
new file mode 100644
index 00000000..1f1fcb8c
--- /dev/null
+++ b/lib/base64.c
@@ -0,0 +1,50 @@
+/****************************************************************************
+* Function to encode in Base64
+*
+* Written by Lauri Alanko
+*
+*****************************************************************************/
+
+#include "common.h"
+#include "base64.h"
+
+char *
+base64 (const char *bin, size_t len)
+{
+
+ char *buf = (char *) malloc ((len + 2) / 3 * 4 + 1);
+ size_t i = 0, j = 0;
+
+ char BASE64_END = '=';
+ char base64_table[64];
+ strncpy (base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 64);
+
+ while (j < len - 2) {
+ buf[i++] = base64_table[bin[j] >> 2];
+ buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
+ buf[i++] = base64_table[((bin[j + 1] & 15) << 2) | (bin[j + 2] >> 6)];
+ buf[i++] = base64_table[bin[j + 2] & 63];
+ j += 3;
+ }
+
+ switch (len - j) {
+ case 1:
+ buf[i++] = base64_table[bin[j] >> 2];
+ buf[i++] = base64_table[(bin[j] & 3) << 4];
+ buf[i++] = BASE64_END;
+ buf[i++] = BASE64_END;
+ break;
+ case 2:
+ buf[i++] = base64_table[bin[j] >> 2];
+ buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
+ buf[i++] = base64_table[(bin[j + 1] & 15) << 2];
+ buf[i++] = BASE64_END;
+ break;
+ case 0:
+ break;
+ }
+
+ buf[i] = '\0';
+ return buf;
+}
+
diff --git a/lib/base64.h b/lib/base64.h
new file mode 100644
index 00000000..1e0a0b04
--- /dev/null
+++ b/lib/base64.h
@@ -0,0 +1,4 @@
+/* Header file for base64.c */
+
+char *base64 (const char *bin, size_t len);
+