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