Home | History | Annotate | Download | only in posix
      1 /* uuencode.c - uuencode / base64 encode
      2  *
      3  * Copyright 2013 Erich Plondke <toybox (at) 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 *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_init(toybuf);
     32 
     33   xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
     34   for (;;) {
     35     char *in;
     36 
     37     if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;
     38 
     39     if (!m) xputc(i+32);
     40     in = buf;
     41 
     42     for (in = buf; in-buf < i; ) {
     43       int j, x, bytes = i - (in-buf);
     44 
     45       if (bytes > 3) bytes = 3;
     46 
     47       for (j = x = 0; j<4; j++) {
     48         int out;
     49 
     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));
     53       }
     54     }
     55     xputc('\n');
     56   }
     57   xputs(m ? "====" : "end");
     58 }
     59