comparison toys/posix/uuencode.c @ 841:cb0c3a773437

Kyungwan Han submitted ifconfig.
author Rob Landley <rob@landley.net>
date Tue, 02 Apr 2013 01:58:18 -0500
parents
children 357b31ae2f04
comparison
equal deleted inserted replaced
-1:000000000000 0:1f21246c92f3
1 /* uuencode.c - uuencode / base64 encode
2 *
3 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
4 *
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html
6
7 USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN))
8
9 config UUENCODE
10 bool "uuencode"
11 default y
12 help
13 usage: uuencode [-m] [file] encode-filename
14
15 Uuencode stdin (or file) to stdout, with encode-filename in the output.
16
17 -m base64-encode
18 */
19
20 #define FOR_uuencode
21 #include "toys.h"
22
23 void uuencode_main(void)
24 {
25 char *p, *name = toys.optargs[toys.optc-1], buf[(76/4)*3];
26
27 int i, m = toys.optflags & FLAG_m, fd = 0;
28
29 if (toys.optc > 1) fd = xopen(toys.optargs[0], O_RDONLY);
30
31 // base64 table
32
33 p = toybuf;
34 for (i = 'A'; i != ':'; i++) {
35 if (i == 'Z'+1) i = 'a';
36 if (i == 'z'+1) i = '0';
37 *(p++) = i;
38 }
39 *(p++) = '+';
40 *(p++) = '/';
41
42 xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
43 for (;;) {
44 char *in;
45
46 if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;
47
48 if (!m) xputc(i+32);
49 in = buf;
50
51 for (in = buf; in-buf < i; ) {
52 int j, x, bytes = i - (in-buf);
53
54 if (bytes > 3) bytes = 3;
55
56 for (j = x = 0; j<4; j++) {
57 int out;
58
59 if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j));
60 out = (x>>((3-j)*6)) & 0x3f;
61 xputc(m ? j > bytes ? '=' : toybuf[out] : out + 0x20);
62 }
63 }
64 xputc('\n');
65 }
66 xputs(m ? "====" : "end");
67 }