1 /* uudecode.c - uudecode / base64 decode
3 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uudecode.html
7 USE_UUDECODE(NEWTOY(uudecode, ">1o:", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
13 usage: uudecode [-o OUTFILE] [INFILE]
15 Decode file from stdin (or INFILE).
17 -o write to OUTFILE instead of filename in header
27 void uudecode_main(void)
29 int ifd = 0, ofd, idx = 0, m = m;
30 char *line = 0, mode[16],
31 *class[] = {"begin%*[ ]%15s%*[ ]%n", "begin-base64%*[ ]%15s%*[ ]%n"};
33 if (toys.optc) ifd = xopenro(*toys.optargs);
37 if (!(line = get_line(ifd))) error_exit("bad EOF");
38 for (m=0; m < 2; m++) {
39 sscanf(line, class[m], mode, &idx);
44 ofd = xcreate(TT.o ? TT.o : line+idx, O_WRONLY|O_CREAT|O_TRUNC,
45 string_to_mode(mode, 0777^toys.old_umask));
52 if (m == 2 || !(line = get_line(ifd))) break;
53 if (!strcmp(line, m ? "====" : "end")) {
60 if (!m) olen = (*(in++) - 32) & 0x3f;
63 int i = 0, x = 0, len = 4;
68 if (olen < 3) len = olen + 1;
72 if (!(c = *(in++))) goto line_done;
78 } else if (len != 4) break;
80 if (c >= 'A' && c <= 'Z') c -= 'A';
81 else if (c >= 'a' && c <= 'z') c += 26 - 'a';
82 else if (c >= '0' && c <= '9') c += 52 - '0';
83 else if (c == '+') c = 62;
84 else if (c == '/') c = 63;
86 } else c = (c - 32) & 0x3f;
91 *(out++) = (x>>(8*(3-i))) & 0xff;
97 if (i && i!=len) error_exit("bad %s", line);
100 xwrite(ofd, line, out-line);
103 if (CFG_TOYBOX_FREE) {