1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define FOR_uuencode
21#include "toys.h"
22
23void uuencode_main(void)
24{
25 char *name = toys.optargs[toys.optc-1], buf[(76/4)*3];
26
27 int i, m = FLAG(m), fd = 0;
28
29 if (toys.optc > 1) fd = xopenro(toys.optargs[0]);
30
31 base64_init(toybuf);
32
33 xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
34 for (;;) {
35 char *in;
36
37 if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;
38
39 if (!m) xputc(i+32);
40 in = buf;
41
42 for (in = buf; in-buf < i; ) {
43 int j, x, bytes = i - (in-buf);
44
45 if (bytes > 3) bytes = 3;
46
47 for (j = x = 0; j<4; j++) {
48 int out;
49
50 if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j));
51 out = (x>>((3-j)*6)) & 0x3f;
52 xputc(m ? (j > bytes ? '=' : toybuf[out]) : (out ? out + 0x20 : 0x60));
53 }
54 }
55 xputc('\n');
56 }
57 xputs(m ? "====" : "end");
58}
59