s/qmail 4.3.20
Next generation secure email transport
Loading...
Searching...
No Matches
base64.c
Go to the documentation of this file.
1#include "base64.h"
2#include "str.h"
3#include "stralloc.h"
4
5static char *b64alpha =
6 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
7#define B64PAD '='
8
9/* returns 0 ok, 1 illegal, -1 problem */
10
11int b64decode(const unsigned char *in,int l,stralloc *out)
12/* stralloc *out => not null terminated */
13{
14 int p = 0;
15 int n;
16 unsigned int x;
17 int i, j;
18 char *s;
19 unsigned char b[3];
20
21 if (l == 0) {
22 if (!stralloc_copys(out,"")) return -1;
23 return 0;
24 }
25
26 while (in[l-1] == B64PAD) {
27 p ++;
28 l--;
29 }
30
31 n = (l + p) / 4;
32 i = (n * 3) - p;
33 if (!stralloc_ready(out,i)) return -1;
34 out->len = i;
35 s = out->s;
36
37 for (i = 0; i < n - 1; i++) {
38 x = 0;
39 for (j = 0; j < 4; j++) {
40 if (in[j] >= 'A' && in[j] <= 'Z')
41 x = (x << 6) + (unsigned int)(in[j] - 'A' + 0);
42 else if (in[j] >= 'a' && in[j] <= 'z')
43 x = (x << 6) + (unsigned int)(in[j] - 'a' + 26);
44 else if (in[j] >= '0' && in[j] <= '9')
45 x = (x << 6) + (unsigned int)(in[j] - '0' + 52);
46 else if (in[j] == '+')
47 x = (x << 6) + 62;
48 else if (in[j] == '/')
49 x = (x << 6) + 63;
50 else if (in[j] == '=')
51 x = (x << 6);
52 else return 1;
53 }
54
55 s[2] = (unsigned char)(x & 255); x >>= 8;
56 s[1] = (unsigned char)(x & 255); x >>= 8;
57 s[0] = (unsigned char)(x & 255); x >>= 8;
58 s += 3; in += 4;
59 }
60
61 x = 0;
62 for (j = 0; j < 4; j++) {
63 if (in[j] >= 'A' && in[j] <= 'Z')
64 x = (x << 6) + (unsigned int)(in[j] - 'A' + 0);
65 else if (in[j] >= 'a' && in[j] <= 'z')
66 x = (x << 6) + (unsigned int)(in[j] - 'a' + 26);
67 else if (in[j] >= '0' && in[j] <= '9')
68 x = (x << 6) + (unsigned int)(in[j] - '0' + 52);
69 else if (in[j] == '+')
70 x = (x << 6) + 62;
71 else if (in[j] == '/')
72 x = (x << 6) + 63;
73 else if (in[j] == '=')
74 x = (x << 6);
75 else return 1;
76 }
77
78 b[2] = (unsigned char)(x & 255); x >>= 8;
79 b[1] = (unsigned char)(x & 255); x >>= 8;
80 b[0] = (unsigned char)(x & 255); x >>= 8;
81
82 for (i = 0; i < 3 - p; i++)
83 s[i] = b[i];
84
85 return 0;
86}
87
88int b64encode(stralloc *in,stralloc *out)
89{
90 unsigned char a, b, c;
91 int i;
92 char *s;
93
94 if (in->len == 0)
95 {
96 if (!stralloc_copys(out,"")) return -1;
97 return 0;
98 }
99
100 i = in->len / 3 * 4 + 4;
101 if (!stralloc_ready(out,i)) return -1;
102 s = out->s;
103
104 for (i = 0; i < in->len; i += 3) {
105 a = in->s[i];
106 b = i + 1 < in->len ? in->s[i + 1] : 0;
107 c = i + 2 < in->len ? in->s[i + 2] : 0;
108
109 *s++ = b64alpha[a >> 2];
110 *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
111
112 if (i + 1 >= in->len) *s++ = B64PAD;
113 else *s++ = b64alpha[((b & 0x0f) << 2) | (c >> 6)];
114
115 if (i + 2 >= in->len) *s++ = B64PAD;
116 else *s++ = b64alpha[c & 0x3f];
117 }
118 out->len = s - out->s;
119 return 0;
120}
buffer b
Definition: auto-gid.c:10
int b64encode(stralloc *in, stralloc *out)
Definition: base64.c:88
#define B64PAD
Definition: base64.c:7
int b64decode(const unsigned char *in, int l, stralloc *out)
Definition: base64.c:11
int stralloc_copys(stralloc *, char const *)
stralloc out
Definition: dnscname.c:12
void c(char *, char *, char *, int, int, int)
Definition: install.c:67
void p(char *, char *, int, int, int)
Definition: install.c:49
buffer in
Definition: qmail-pw2u.c:240
int j
Definition: qmail-send.c:926