aboutsummaryrefslogtreecommitdiff
path: root/lib/base64.c
diff options
context:
space:
mode:
authorGravatar Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> 2007-11-09 21:17:03 +0000
committerGravatar Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> 2007-11-09 21:17:03 +0000
commit7a05ad0166ac11d4206d934825728b56a58d8edd (patch)
tree771b5f856eaa9e38ed4ef525ecefcf1b72e85949 /lib/base64.c
parent29471dda5ae9e750809e7a25b93d6bf6a2913bc2 (diff)
downloadmonitoring-plugins-7a05ad0166ac11d4206d934825728b56a58d8edd.tar.gz
Moved base64 function to /lib.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1817 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/base64.c')
-rw-r--r--lib/base64.c50
1 files changed, 50 insertions, 0 deletions
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;
+}
+