From f5b4bbef5f186aa705d961c62817eab388f1c13e Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 26 Mar 2013 01:49:18 -0500 Subject: Finish uuencode cleanup, default y, move it from pending to posix. --- toys/posix/uuencode.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 toys/posix/uuencode.c (limited to 'toys/posix/uuencode.c') diff --git a/toys/posix/uuencode.c b/toys/posix/uuencode.c new file mode 100644 index 00000000..78d199a9 --- /dev/null +++ b/toys/posix/uuencode.c @@ -0,0 +1,67 @@ +/* uuencode.c - uuencode / base64 encode + * + * Copyright 2013 Erich Plondke + * + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html + +USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN)) + +config UUENCODE + bool "uuencode" + default y + help + usage: uuencode [-m] [file] encode-filename + + Uuencode stdin (or file) to stdout, with encode-filename in the output. + + -m base64-encode +*/ + +#define FOR_uuencode +#include "toys.h" + +void uuencode_main(void) +{ + char *p, *name = toys.optargs[toys.optc-1], buf[(76/4)*3]; + + int i, m = toys.optflags & FLAG_m, fd = 0; + + if (toys.optc > 1) fd = xopen(toys.optargs[0], O_RDONLY); + + // base64 table + + p = toybuf; + for (i = 'A'; i != ':'; i++) { + if (i == 'Z'+1) i = 'a'; + if (i == 'z'+1) i = '0'; + *(p++) = i; + } + *(p++) = '+'; + *(p++) = '/'; + + xprintf("begin%s 744 %s\n", m ? "-base64" : "", name); + for (;;) { + char *in; + + if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break; + + if (!m) xputc(i+32); + in = buf; + + for (in = buf; in-buf < i; ) { + int j, x, bytes = i - (in-buf); + + if (bytes > 3) bytes = 3; + + for (j = x = 0; j<4; j++) { + int out; + + if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j)); + out = (x>>((3-j)*6)) & 0x3f; + xputc(m ? j > bytes ? '=' : toybuf[out] : out + 0x20); + } + } + xputc('\n'); + } + xputs(m ? "====" : "end"); +} -- cgit v1.2.3