aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/base64.c')
-rw-r--r--src/daemon/base64.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/daemon/base64.c b/src/daemon/base64.c
new file mode 100644
index 00000000..0ac002a7
--- /dev/null
+++ b/src/daemon/base64.c
@@ -0,0 +1,86 @@
1/*
2 * This code implements the BASE64 algorithm.
3 * This code is in the public domain; do with it what you wish.
4 *
5 * @file base64.c
6 * @brief This code implements the BASE64 algorithm
7 * @author Matthieu Speder
8 */
9
10#include <memory.h>
11#include <stdlib.h>
12#include "base64.h"
13
14static const char base64_chars[] =
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16
17static const char base64_digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
20 0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
21 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26,
22 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
23 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
29
30char* BASE64Encode(const char* src) {
31 unsigned int in_len = strlen(src);
32 char* dest = (char*)malloc((in_len + 2 - ((in_len + 2) % 3)) / 3 * 4 + 1);
33 char* result = dest;
34
35 while (*src) {
36 dest[0] = base64_chars[(src[0] & 0xfc) >> 2];
37 dest[1] = base64_chars[((src[0] & 0x03) << 4) + ((src[1] & 0xf0) >> 4)];
38 dest[2] = base64_chars[((src[1] & 0x0f) << 2) + ((src[2] & 0xc0) >> 6)];
39 dest[3] = base64_chars[src[2] & 0x3f];
40 if (!*(++src)) {
41 dest[2] = dest[3] = '=';
42 dest[4] = 0;
43 return result;
44 }
45 if (!*(++src)) {
46 dest[3] = '=';
47 dest[4] = 0;
48 return result;
49 }
50 src++;
51 dest += 4;
52 }
53 *dest = 0;
54 return result;
55
56}
57
58char* BASE64Decode(const char* src) {
59 unsigned int in_len = strlen(src);
60 char* dest;
61 char* result;
62
63 if (in_len % 4) {
64 /* Wrong base64 string length */
65 return NULL;
66 }
67 result = dest = (char*) malloc(in_len / 4 * 3 + 1);
68
69 while (*src) {
70 char a = base64_digits[(unsigned char)*(src++)];
71 char b = base64_digits[(unsigned char)*(src++)];
72 char c = base64_digits[(unsigned char)*(src++)];
73 char d = base64_digits[(unsigned char)*(src++)];
74 *(dest++) = (a << 2) | ((b & 0x30) >> 4);
75 if (c == -1)
76 break;
77 *(dest++) = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
78 if (d == -1)
79 break;
80 *(dest++) = ((c & 0x03) << 6) | d;
81 }
82 *dest = 0;
83 return result;
84}
85
86/* end of base64.c */