mess822x 1.26
mess822x
Loading...
Searching...
No Matches
mess822_base64.c
Go to the documentation of this file.
1#include "mess822.h"
2#include "str.h"
3#include "stralloc.h"
4#include "logmsg.h"
5#include <stdio.h>
6
7#define WHO "mess822_base64"
8#define B64PAD '='
9
10static const char *b64alpha =
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
13static const char *b64alphaurl =
14 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
15
16/* returns number of bytes written; -1 problem
17 stralloc *out => null terminated
18 RFC 4648 base64url decoding is considered and handled
19*/
20
21int mess822_b64decode(stralloc *out,const char *in,int len)
22{
23 int p = 0;
24 int n;
25 unsigned int x;
26 int i, j;
27 unsigned char *s;
28 unsigned char b[3];
29
30 if (len == 0) {
31 if (!stralloc_copys(out,"")) return -1;
32 return 0;
33 }
34
35 while (in[len -1] == B64PAD) {
36 p++;
37 len--;
38 }
39
40 n = (len + p) / 4;
41 i = (n * 3) - p;
42 if (!stralloc_ready(out,i)) return -1;
43 out->len = i;
44 s = (unsigned char*) out->s;
45
46 for (i = 0; i < n - 1; i++) {
47 x = 0;
48 for (j = 0; j < 4; j++) {
49 if (in[j] >= 'A' && in[j] <= 'Z')
50 x = (x << 6) + (unsigned int) (in[j] - 'A' + 0);
51 else if (in[j] >= 'a' && in[j] <= 'z')
52 x = (x << 6) + (unsigned int) (in[j] - 'a' + 26);
53 else if (in[j] >= '0' && in[j] <= '9')
54 x = (x << 6) + (unsigned int) (in[j] - '0' + 52);
55 else if (in[j] == '+')
56 x = (x << 6) + 62;
57 else if (in[j] == '/')
58 x = (x << 6) + 63;
59 else if (in[j] == '-') // base64url
60 x = (x << 6) + 62;
61 else if (in[j] == '_') // base64url
62 x = (x << 6) + 63;
63 else if (in[j] == '=')
64 x = (x << 6);
65 else return 1;
66 }
67
68 s[2] = (unsigned char) (x & 255); x >>= 8;
69 s[1] = (unsigned char) (x & 255); x >>= 8;
70 s[0] = (unsigned char) (x & 255); x >>= 8;
71 s += 3; in += 4;
72 }
73
74 x = 0;
75 for (j = 0; j < 4; j++) {
76 if (in[j] >= 'A' && in[j] <= 'Z')
77 x = (x << 6) + (unsigned int) (in[j] - 'A' + 0);
78 else if (in[j] >= 'a' && in[j] <= 'z')
79 x = (x << 6) + (unsigned int) (in[j] - 'a' + 26);
80 else if (in[j] >= '0' && in[j] <= '9')
81 x = (x << 6) + (unsigned int) (in[j] - '0' + 52);
82 else if (in[j] == '+')
83 x = (x << 6) + 62;
84 else if (in[j] == '/')
85 x = (x << 6) + 63;
86 else if (in[j] == '-') // base64url
87 x = (x << 6) + 62;
88 else if (in[j] == '_') // base64url
89 x = (x << 6) + 63;
90 else if (in[j] == '=')
91 x = (x << 6);
92 else return 1;
93 }
94
95 b[2] = (unsigned char) (x & 255); x >>= 8;
96 b[1] = (unsigned char) (x & 255); x >>= 8;
97 b[0] = (unsigned char) (x & 255); x >>= 8;
98
99 for (i = 0; i < 3 - p; i++)
100 s[i] = b[i];
101
102 return out->len;
103}
104
105int mess822_b64encode(stralloc *out,stralloc *in)
106{
107 unsigned char a, b, c;
108 int i, r = 0;
109 unsigned char *s;
110
111 if (in->len == 0)
112 {
113 if (!stralloc_copys(out,"")) return -1;
114 return 0;
115 }
116
117 i = in->len / 3 * 4 + 4;
118 if (!stralloc_ready(out,i)) return -1;
119 s = (unsigned char *) out->s;
120
121 for (i = 0; i < in->len; i += 3) {
122 a = in->s[i];
123 b = i + 1 < in->len ? in->s[i + 1] : 0;
124 c = i + 2 < in->len ? in->s[i + 2] : 0;
125
126 *s++ = b64alpha[a >> 2];
127 *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
128
129 if (i + 1 >= in->len) *s++ = B64PAD;
130 else *s++ = b64alpha[((b & 0x0f) << 2) | (c >> 6)];
131
132 if (i + 2 >= in->len) *s++ = B64PAD;
133 else *s++ = b64alpha[c & 0x3f];
134 r += 4;
135 if (r % 76 == 0) *s++ = '\n';
136 }
137 out->len = s - (unsigned char *) out->s;
138
139 return out->len;
140}
141
142int mess822_b64urlencode(stralloc *out,stralloc *in)
143{
144 unsigned char a, b, c;
145 int i, r = 0;
146 unsigned char *s;
147
148 if (in->len == 0)
149 {
150 if (!stralloc_copys(out,"")) return -1;
151 return 0;
152 }
153
154 i = in->len / 3 * 4 + 4;
155 if (!stralloc_ready(out,i)) return -1;
156 s = (unsigned char *) out->s;
157
158 for (i = 0; i < in->len; i += 3) {
159 a = in->s[i];
160 b = i + 1 < in->len ? in->s[i + 1] : 0;
161 c = i + 2 < in->len ? in->s[i + 2] : 0;
162
163 *s++ = b64alphaurl[a >> 2];
164 *s++ = b64alphaurl[((a & 3 ) << 4) | (b >> 4)];
165
166 if (i + 1 >= in->len) *s++ = B64PAD;
167 else *s++ = b64alphaurl[((b & 0x0f) << 2) | (c >> 6)];
168
169 if (i + 2 >= in->len) *s++ = B64PAD;
170 else *s++ = b64alphaurl[c & 0x3f];
171 r += 4;
172 if (r % 76 == 0) *s++ = '\n';
173 }
174 out->len = s - (unsigned char *) out->s;
175
176 return out->len;
177}
void p(char *home, char *fifo, int uid, int gid, int mode)
Definition instcheck.c:46
#define B64PAD
int mess822_b64urlencode(stralloc *out, stralloc *in)
int mess822_b64decode(stralloc *out, const char *in, int len)
int mess822_b64encode(stralloc *out, stralloc *in)
void c(char *, char *, char *, int, int, int)
Definition install.c:46
stralloc out
Definition b64decode.c:11
mess822_action a[]
Definition 822date.c:25