summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2007-11-09 21:17:03 +0000
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2007-11-09 21:17:03 +0000
commit7a05ad0166ac11d4206d934825728b56a58d8edd (patch)
tree771b5f856eaa9e38ed4ef525ecefcf1b72e85949 /lib
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')
-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
5noinst_LIBRARIES = libnagiosplug.a 5noinst_LIBRARIES = libnagiosplug.a
6 6
7 7
8libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c 8libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c base64.c
9EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h 9EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h base64.h
10 10
11INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins 11INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
12 12
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 @@
1/****************************************************************************
2* Function to encode in Base64
3*
4* Written by Lauri Alanko
5*
6*****************************************************************************/
7
8#include "common.h"
9#include "base64.h"
10
11char *
12base64 (const char *bin, size_t len)
13{
14
15 char *buf = (char *) malloc ((len + 2) / 3 * 4 + 1);
16 size_t i = 0, j = 0;
17
18 char BASE64_END = '=';
19 char base64_table[64];
20 strncpy (base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 64);
21
22 while (j < len - 2) {
23 buf[i++] = base64_table[bin[j] >> 2];
24 buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
25 buf[i++] = base64_table[((bin[j + 1] & 15) << 2) | (bin[j + 2] >> 6)];
26 buf[i++] = base64_table[bin[j + 2] & 63];
27 j += 3;
28 }
29
30 switch (len - j) {
31 case 1:
32 buf[i++] = base64_table[bin[j] >> 2];
33 buf[i++] = base64_table[(bin[j] & 3) << 4];
34 buf[i++] = BASE64_END;
35 buf[i++] = BASE64_END;
36 break;
37 case 2:
38 buf[i++] = base64_table[bin[j] >> 2];
39 buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
40 buf[i++] = base64_table[(bin[j + 1] & 15) << 2];
41 buf[i++] = BASE64_END;
42 break;
43 case 0:
44 break;
45 }
46
47 buf[i] = '\0';
48 return buf;
49}
50
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 @@
1/* Header file for base64.c */
2
3char *base64 (const char *bin, size_t len);
4