-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib-base64.c
471 lines (423 loc) · 9.16 KB
/
lib-base64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Copyright Neil Brown ©2016-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Filter a view on a document to make base64 look like the
* decoded bytes. A UTF-8 filter would be needed if the base64
* is actually utf-8.
*
* Each mark needs not just a location in the b64, but also
* which byte of a QUAD (4 b64 characters) it is at.
* We store this in the mark as attribute "b64-pos", which makes
* stacking b64 impossible - but who would want to?
* This can have a value "0", "1", "2". A mark is never on the
* 4th char of a QUAD.
* doc:set-ref initialises this as does a mark:arrived notification
* which references another mark. doc:char and doc:byte will use
* the pos to know how to interpret, and will update it after any
* movement as will doc:content.
*/
#include <unistd.h>
#include <stdlib.h>
#include <wctype.h>
#define DOC_NEXT base64_next
#define DOC_PREV base64_prev
#define PANE_DATA_VOID
#include "core.h"
static struct map *b64_map safe;
DEF_LOOKUP_CMD(b64_handle, b64_map);
static int is_b64(wint_t c)
{
return (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '+' || c == '/' || c == '=';
}
static wint_t from_b64(char c)
{
/* This assumes that 'c' is_b64() */
if (c <= '+')
return 62;
else if (c == '/')
return 63;
else if (c <= '9')
return (c - '0') + 52;
else if (c == '=')
return 64;
else if (c <= 'Z')
return (c - 'A') + 0;
else
return (c - 'a') + 26;
}
static wint_t get_b64(struct pane *p safe, struct mark *m safe)
{
wint_t c;
wint_t ret;
do {
c = doc_next(p, m);
} while (c != WEOF && !is_b64(c));
if (c == WEOF)
return WEOF;
ret = from_b64(c);
while ((c = doc_following(p, m)) != WEOF && !is_b64(c))
doc_next(p, m);
return ret;
}
static wint_t get_b64_rev(struct pane *p safe, struct mark *m safe)
{
wint_t c;
do {
c = doc_prev(p, m);
} while (c != WEOF && !is_b64(c));
if (c == WEOF)
return WEOF;
return from_b64(c);
}
static void set_pos(struct mark *m safe, int pos)
{
char ps[2] = "0";
while (pos < 0)
pos += 4;
while (pos >= 4)
pos -= 4;
ps[0] += pos;
attr_set_str(&m->attrs, "b64-pos", ps);
mark_watch(m);
}
static int get_pos(struct mark *m safe)
{
char *ps = attr_find(m->attrs, "b64-pos");
if (ps && strlen(ps) == 1 &&
ps[0] >= '0' && ps[1] <= '2')
return ps[0] - '0';
return -1;
}
static inline wint_t base64_next(struct pane *home safe, struct mark *mark safe,
struct doc_ref *r safe, bool bytes)
{
int pos = 0;
int move = r == &mark->ref;
struct mark *m;
struct pane *p = home->parent;
wint_t c1, c2, b;
pos = get_pos(mark);
if (pos < 0)
/* bug? */
pos = 0;
m = mark_dup(mark);
retry:
c1 = get_b64(p, m);
c2 = get_b64(p, m);
/* If we found '=', there is no more to find */
if (c1 == 64 || c2 == 64) {
while (pos < 2 && c2 != WEOF) {
c2 = get_b64(p, m);
pos += 1;
}
pos = 0;
if (c2 != WEOF)
/* hopefully it was 64 aka '=' */
goto retry;
}
if (c1 == WEOF || c2 == WEOF) {
mark_free(m);
return CHAR_RET(WEOF);
}
switch(pos) {
case 0:
b = (c1 << 2) | (c2 >> 4);
break;
case 1:
b = ((c1 << 4) & 0xF0) | (c2 >> 2);
break;
case 2:
b = ((c1 << 6) & 0xC0) | c2;
break;
default:
b = 0;
}
if (move) {
if (pos < 2)
/* Step back to look at the last char read */
doc_prev(p, m);
else
pos += 1;
mark_to_mark(mark, m);
set_pos(mark, pos+1);
}
mark_free(m);
return b;
}
static inline wint_t base64_prev(struct pane *home safe, struct mark *mark safe,
struct doc_ref *r safe, bool bytes)
{
int pos = 0;
int move = r == &mark->ref;
struct mark *m;
struct pane *p = home->parent;
wint_t c1, c2, b;
pos = get_pos(mark);
if (pos < 0)
/* bug? */
pos = 0;
m = mark_dup(mark);
if (pos)
if (get_b64(p, m) == WEOF)
pos = 0;
c2 = get_b64_rev(p, m);
c1 = get_b64_rev(p, m);
if (pos <= 0)
pos = 2;
else
pos -= 1;
while (c2 == 64) {
c2 = c1;
c1 = get_b64_rev(p, m);
if (pos <= 0)
pos = 2;
else
pos -= 1;
}
if (c1 == WEOF || c2 == WEOF) {
mark_free(m);
return CHAR_RET(WEOF);
}
switch(pos) {
case 0:
b = (c1 << 2) | (c2 >> 4);
break;
case 1:
b = ((c1 << 4) & 0xF0) | (c2 >> 2);
break;
case 2:
b = ((c1 << 6) & 0xC0) | c2;
break;
default:
b = 0;
}
if (move) {
mark_to_mark(mark, m);
set_pos(mark, pos);
}
mark_free(m);
return b;
}
DEF_CMD(base64_char)
{
return do_char_byte(ci);
}
DEF_CMD(base64_setref)
{
if (ci->mark)
/* Start and end are always pos 0 */
set_pos(ci->mark, 0);
return Efallthrough;
}
DEF_CMD(base64_arrived)
{
struct mark *m = ci->mark;
struct mark *ref = ci->mark2;
int pos;
if (!m)
return 1;
if (get_pos(m) >= 0)
/* Interesting mark, keep tracking it */
mark_watch(m);
if (!ref)
return 1;
pos = get_pos(ref);
if (pos >= 0)
set_pos(m, pos);
return 1;
}
struct b64c {
struct command c;
struct command *cb;
struct pane *p safe;
struct pane *home safe;
struct mark *m safe; /* trails 1 b64 char behind main mark */
int pos;
int size;
char c1;
bool nobulk;
};
static int b64_bulk(struct b64c *c safe, wchar_t first, const char *s safe, int len)
{
/* Parse out 4char->3byte section of 's' and then
* pass them to c->cb.
* Return the number of chars processed.
*/
int ret = 0;
char *out = malloc((len+1)*3/4);
int b[4];
int in_pos = 0, out_pos = 0, buf_pos = 0;
int i;
if (!out)
return ret;
if (is_b64(first))
b[buf_pos++] = from_b64(first);
while (len > 0) {
wint_t wc = (unsigned char)s[in_pos++];
len -= 1;
if (!is_b64(wc))
continue;
b[buf_pos++] = from_b64(wc);
if (b[buf_pos-1] == 64)
break;
if (buf_pos < 4)
continue;
out[out_pos++] = ((b[0] << 2) & 0xFC) | (b[1] >> 4);
out[out_pos++] = ((b[1] << 4) & 0xF0) | (b[2] >> 2);
out[out_pos++] = ((b[2] << 6) & 0xC0) | (b[3] >> 0);
ret = in_pos+1;
buf_pos = 0;
}
/* Now send 'out' to callback */
i = 0;
while (i < out_pos) {
int rv = comm_call(c->cb, "cb", c->p, out[i], c->m,
out+i+1, out_pos-i-1, NULL, NULL,
c->size, 0);
c->size = 0;
if (rv <= 0 || rv > (out_pos - i) + 1) {
if (rv <= 0)
ret = rv;
c->nobulk = True;
break;
}
i += rv;
if (i < out_pos)
/* Only some was consumed, so need to
* advance c->m by the amount of chars that
* were consumed - in 'home'.
*/
call("doc:char", c->home, rv, c->m);
}
free(out);
return ret;
}
DEF_CMD(base64_content_cb)
{
struct b64c *c = container_of(ci->comm, struct b64c, c);
wint_t wc = ci->num;
char c2;
wint_t b;
int ret;
if (!ci->mark)
return Enoarg;
if (ci->x)
c->size = ci->x * 3 / 4;
if (!is_b64(wc))
return 1;
/* Mark as advances down in the doc so we didn't see it.
* Need to explicitly set pos
*/
set_pos(ci->mark, (c->pos+1)%4);
if (!c->nobulk && wc != '=' && (c->pos % 4) == 0 &&
ci->str && ci->num2 >= 4) {
mark_to_mark(c->m, ci->mark);
ret = b64_bulk(c, wc, ci->str, ci->num2);
if (ret > 0)
return ret;
}
c2 = from_b64(wc);
if (c2 == 64) {
/* We've found a padding '=', that's all folks. */
c->c1 = 64;
return Efalse;
}
if (c->pos <= 0 || c->pos > 3) {
c->c1 = c2;
c->pos = 1;
mark_to_mark(c->m, ci->mark);
return 1;
}
if (c->c1 == 64) {
/* This is first b64 */
c->c1 = c2;
c->pos = (c->pos + 1) % 4;
mark_to_mark(c->m, ci->mark);
return 1;
}
/* Have 2 b64 chars, can report one char */
switch(c->pos) {
case 1:
b = (c->c1 << 2) | (c2 >> 4);
break;
case 2:
b = ((c->c1 << 4) & 0xF0) | (c2 >> 2);
break;
case 3:
b = ((c->c1 << 6) & 0xC0) | c2;
break;
default:
b = 0;
}
c->pos += 1;
c->c1 = c2;
if (c->pos == 4)
mark_to_mark(c->m, ci->mark);
ret = comm_call(c->cb, ci->key, c->p, b, c->m, NULL,
0, NULL, NULL, c->size, 0);
if (c->pos != 4)
mark_to_mark(c->m, ci->mark);
c->size = 0;
if (ret == Efalse)
c->c1 = 64;
return ret;
}
DEF_CMD(base64_content)
{
struct b64c c;
int ret;
if (!ci->comm2 || !ci->mark)
return Enoarg;
/* No need to check ->key as providing bytes as chars
* is close enough.
*/
c.c = base64_content_cb;
c.nobulk = False;
c.cb = ci->comm2;
c.p = ci->focus;
c.home = ci->home;
c.pos = get_pos(ci->mark);
c.size = 0;
c.m = mark_dup(ci->mark);
c.c1 = 64;
ret = home_call_comm(ci->home->parent, ci->key, ci->home, &c.c,
0, ci->mark, NULL, 0, ci->mark2);
if (c.c1 != 64 && (c.pos % 4) > 0 && ci->mark2) {
/* We must have reached mark2, but need one more
* b64 char. Skip space if needed to find it.
*/
wint_t c2;
while ((c2 = doc_next(ci->home->parent, c.m)) != WEOF &&
iswspace(c2))
;
if (c2 != WEOF)
comm_call(&c.c, "cb", ci->home->parent,
c2, ci->mark2);
}
mark_free(c.m);
return ret;
}
DEF_CMD(b64_attach)
{
struct pane *p;
p = pane_register(ci->focus, 0, &b64_handle.c);
if (!p)
return Efail;
call("doc:request:mark:arrived", p);
return comm_call(ci->comm2, "callback:attach", p);
}
void edlib_init(struct pane *ed safe)
{
b64_map = key_alloc();
key_add(b64_map, "doc:char", &base64_char);
key_add(b64_map, "doc:byte", &base64_char);
key_add(b64_map, "doc:content", &base64_content);
key_add(b64_map, "doc:content-bytes", &base64_content);
key_add(b64_map, "doc:set-ref", &base64_setref);
key_add(b64_map, "mark:arrived", &base64_arrived);
call_comm("global-set-command", ed, &b64_attach, 0, NULL, "attach-base64");
}