uuencode.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* uuencode.c - uuencode / base64 encode
  2. *
  3. * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
  4. *
  5. * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html
  6. USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN))
  7. config UUENCODE
  8. bool "uuencode"
  9. default y
  10. help
  11. usage: uuencode [-m] [INFILE] ENCODE_FILENAME
  12. Uuencode stdin (or INFILE) to stdout, with ENCODE_FILENAME in the output.
  13. -m Base64
  14. */
  15. #define FOR_uuencode
  16. #include "toys.h"
  17. void uuencode_main(void)
  18. {
  19. char *name = toys.optargs[toys.optc-1], buf[(76/4)*3];
  20. int i, m = FLAG(m), fd = 0;
  21. if (toys.optc > 1) fd = xopenro(toys.optargs[0]);
  22. base64_init(toybuf);
  23. xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
  24. for (;;) {
  25. char *in;
  26. if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;
  27. if (!m) xputc(i+32);
  28. in = buf;
  29. for (in = buf; in-buf < i; ) {
  30. int j, x, bytes = i - (in-buf);
  31. if (bytes > 3) bytes = 3;
  32. for (j = x = 0; j<4; j++) {
  33. int out;
  34. if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j));
  35. out = (x>>((3-j)*6)) & 0x3f;
  36. xputc(m ? (j > bytes ? '=' : toybuf[out]) : (out ? out + 0x20 : 0x60));
  37. }
  38. }
  39. xputc('\n');
  40. }
  41. xputs(m ? "====" : "end");
  42. }