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