summaryrefslogtreecommitdiffstats
path: root/base64.c
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2013-02-04 02:32:40 +0100
committermanuel <manuel@mausz.at>2013-02-04 02:32:40 +0100
commit8514473287c9594137c6fbc39f5619672ebc2430 (patch)
treea5b965d8c7b60dee396bf8ebe25dd3eddfaa6753 /base64.c
parent35ddb916045abafaa4ae2c778b9383059fa06726 (diff)
downloadqmail-8514473287c9594137c6fbc39f5619672ebc2430.tar.gz
qmail-8514473287c9594137c6fbc39f5619672ebc2430.tar.bz2
qmail-8514473287c9594137c6fbc39f5619672ebc2430.zip
[PATCH] qregex-starttls-2way-auth-20060423-mm
Diffstat (limited to 'base64.c')
-rw-r--r--base64.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/base64.c b/base64.c
new file mode 100644
index 0000000..ea8b777
--- /dev/null
+++ b/base64.c
@@ -0,0 +1,122 @@
1#include "base64.h"
2#include "stralloc.h"
3#include "substdio.h"
4#include "str.h"
5
6static char *b64alpha =
7 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
8#define B64PAD '='
9
10/* returns 0 ok, 1 illegal, -1 problem */
11
12int b64decode(in,l,out)
13const unsigned char *in;
14int l;
15stralloc *out; /* not null terminated */
16{
17 int p = 0;
18 int n;
19 unsigned int x;
20 int i, j;
21 char *s;
22 unsigned char b[3];
23
24 if (l == 0)
25 {
26 if (!stralloc_copys(out,"")) return -1;
27 return 0;
28 }
29
30 while(in[l-1] == B64PAD) {
31 p ++;
32 l--;
33 }
34
35 n = (l + p) / 4;
36 out->len = (n * 3) - p;
37 if (!stralloc_ready(out,out->len)) return -1;
38 s = out->s;
39
40 for(i = 0; i < n - 1 ; i++) {
41 x = 0;
42 for(j = 0; j < 4; j++) {
43 if(in[j] >= 'A' && in[j] <= 'Z')
44 x = (x << 6) + (unsigned int)(in[j] - 'A' + 0);
45 else if(in[j] >= 'a' && in[j] <= 'z')
46 x = (x << 6) + (unsigned int)(in[j] - 'a' + 26);
47 else if(in[j] >= '0' && in[j] <= '9')
48 x = (x << 6) + (unsigned int)(in[j] - '0' + 52);
49 else if(in[j] == '+')
50 x = (x << 6) + 62;
51 else if(in[j] == '/')
52 x = (x << 6) + 63;
53 else if(in[j] == '=')
54 x = (x << 6);
55 }
56
57 s[2] = (unsigned char)(x & 255); x >>= 8;
58 s[1] = (unsigned char)(x & 255); x >>= 8;
59 s[0] = (unsigned char)(x & 255); x >>= 8;
60 s += 3; in += 4;
61 }
62
63 x = 0;
64 for(j = 0; j < 4; j++) {
65 if(in[j] >= 'A' && in[j] <= 'Z')
66 x = (x << 6) + (unsigned int)(in[j] - 'A' + 0);
67 else if(in[j] >= 'a' && in[j] <= 'z')
68 x = (x << 6) + (unsigned int)(in[j] - 'a' + 26);
69 else if(in[j] >= '0' && in[j] <= '9')
70 x = (x << 6) + (unsigned int)(in[j] - '0' + 52);
71 else if(in[j] == '+')
72 x = (x << 6) + 62;
73 else if(in[j] == '/')
74 x = (x << 6) + 63;
75 else if(in[j] == '=')
76 x = (x << 6);
77 }
78
79 b[2] = (unsigned char)(x & 255); x >>= 8;
80 b[1] = (unsigned char)(x & 255); x >>= 8;
81 b[0] = (unsigned char)(x & 255); x >>= 8;
82
83 for(i = 0; i < 3 - p; i++)
84 s[i] = b[i];
85
86 return 0;
87}
88
89int b64encode(in,out)
90stralloc *in;
91stralloc *out; /* not null terminated */
92{
93 unsigned char a, b, c;
94 int i;
95 char *s;
96
97 if (in->len == 0)
98 {
99 if (!stralloc_copys(out,"")) return -1;
100 return 0;
101 }
102
103 if (!stralloc_ready(out,in->len / 3 * 4 + 4)) return -1;
104 s = out->s;
105
106 for (i = 0;i < in->len;i += 3) {
107 a = in->s[i];
108 b = i + 1 < in->len ? in->s[i + 1] : 0;
109 c = i + 2 < in->len ? in->s[i + 2] : 0;
110
111 *s++ = b64alpha[a >> 2];
112 *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
113
114 if (i + 1 >= in->len) *s++ = B64PAD;
115 else *s++ = b64alpha[((b & 15) << 2) | (c >> 6)];
116
117 if (i + 2 >= in->len) *s++ = B64PAD;
118 else *s++ = b64alpha[c & 63];
119 }
120 out->len = s - out->s;
121 return 0;
122}