1 /* uuencode.c - uuencode / base64 encode
3 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html
7 USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN))
13 usage: uuencode [-m] [file] encode-filename
15 Uuencode stdin (or file) to stdout, with encode-filename in the output.
23 void uuencode_main(void)
25 char *name = toys.optargs[toys.optc-1], buf[(76/4)*3];
27 int i, m = toys.optflags & FLAG_m, fd = 0;
29 if (toys.optc > 1) fd = xopen(toys.optargs[0], O_RDONLY);
33 xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
37 if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;
42 for (in = buf; in-buf < i; ) {
43 int j, x, bytes = i - (in-buf);
45 if (bytes > 3) bytes = 3;
47 for (j = x = 0; j<4; j++) {
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));
57 xputs(m ? "====" : "end");