annotate toys/pending/uuencode.c @ 830:ce155547bca2

First round of uuencode cleanup: generate table, tweak help text, remove unnecessary output buffers, simplify base64 functions..
author Rob Landley <rob@landley.net>
date Sun, 24 Mar 2013 22:35:32 -0500
parents 1fdaba9a7124
children
rev   line source
rob@823 1 /* uuencode.c - uuencode / base64 encode
rob@823 2 *
rob@823 3 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
rob@823 4 *
rob@823 5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html
rob@823 6
rob@823 7 USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN))
rob@823 8
rob@823 9 config UUENCODE
rob@823 10 bool "uuencode"
rob@823 11 default n
rob@823 12 help
rob@823 13 usage: uuencode [-m] [file] encode-filename
rob@823 14
rob@830 15 Uuencode stdin (or file) to stdout, with encode-filename in the output.
rob@830 16
rob@830 17 -m base64-encode
rob@823 18 */
rob@823 19
rob@823 20 #define FOR_uuencode
rob@823 21 #include "toys.h"
rob@823 22
rob@830 23 // Convert 3 bytes of input to 4 bytes of output
rob@830 24 static void uuencode_b64_3bytes(const char *in, int bytes)
rob@830 25 {
rob@830 26 unsigned int i, x;
rob@823 27
rob@830 28 for (i = x = 0; i<4; i++) {
rob@830 29 if (i < bytes) x |= (in[i] & 0x0ff) << (8*(2-i));
rob@830 30 xputc(i > bytes ? '=' : toybuf[(x>>((3-i)*6)) & 0x3f]);
rob@830 31 }
rob@823 32 }
rob@823 33
rob@830 34 static void uuencode_b64_line(const char *in, int len)
rob@823 35 {
rob@823 36 while (len > 0) {
rob@830 37 uuencode_b64_3bytes(in, len < 3 ? len : 3);
rob@823 38 in += 3;
rob@823 39 len -= 3;
rob@823 40 };
rob@823 41 xprintf("\n");
rob@823 42 }
rob@823 43
rob@823 44 static void uuencode_b64(int fd, const char *name)
rob@823 45 {
rob@823 46 int len;
rob@830 47 char buf[(76/4)*3];
rob@830 48
rob@823 49 xprintf("begin-base64 744 %s\n",name);
rob@823 50 do {
rob@830 51 len = xread(fd, buf, sizeof(buf));
rob@830 52
rob@830 53 uuencode_b64_line(buf, len);
rob@823 54 } while (len > 0);
rob@823 55 xprintf("====\n");
rob@823 56 }
rob@823 57
rob@823 58
rob@830 59 static void uuencode_uu_3bytes(const char *in)
rob@823 60 {
rob@830 61 unsigned int i, x = 0;
rob@830 62
rob@830 63 for (i = 0; i <= 2; i++) x |= (in[i] & 0x0ff) << (8*(2-i));
rob@830 64 for (i = 0; i <= 3; i++) xputc(32 + ((x >> (6*(3-i))) & 0x3f));
rob@823 65 }
rob@823 66
rob@830 67 static void uuencode_uu_line(char *in, int len)
rob@823 68 {
rob@830 69 if (len > 0) {
rob@830 70 int i;
rob@830 71
rob@830 72 xputc(len+32);
rob@830 73 for (i = 0; i < len; i += 3) uuencode_uu_3bytes(in+i);
rob@823 74 }
rob@823 75 xprintf("\n");
rob@823 76 }
rob@823 77
rob@823 78 static void uuencode_uu(int fd, const char *name)
rob@823 79 {
rob@823 80 int len;
rob@830 81 char buf[45];
rob@830 82
rob@823 83 xprintf("begin 744 %s\n",name);
rob@823 84 do {
rob@830 85 len = xread(fd, buf, 45);
rob@830 86 uuencode_uu_line(buf, len);
rob@823 87 } while (len > 0);
rob@823 88 xprintf("end\n");
rob@823 89 }
rob@823 90
rob@823 91 void uuencode_main(void)
rob@823 92 {
rob@830 93 char *p, *name = toys.optargs[0];
rob@830 94 int i, fd = 0;
rob@830 95
rob@830 96 // base64 table
rob@830 97
rob@830 98 p = toybuf;
rob@830 99 for (i = 'A'; i != ':'; i++) {
rob@830 100 if (i == 'Z'+1) i = 'a';
rob@830 101 if (i == 'z'+1) i = '0';
rob@830 102 *(p++) = i;
rob@830 103 }
rob@830 104 *(p++) = '+';
rob@830 105 *(p++) = '/';
rob@823 106
rob@823 107 if (toys.optc == 2) {
rob@830 108 fd = xopen(toys.optargs[0], O_RDONLY); // dies if error
rob@830 109 name = toys.optargs[1];
rob@823 110 }
rob@830 111 if (toys.optflags & FLAG_m) uuencode_b64(fd, name);
rob@830 112 else uuencode_uu(fd, name);
rob@823 113 }