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