Mercurial > hg > toybox
annotate toys/posix/uudecode.c @ 1277:23817e1675f2 draft
Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 10 May 2014 13:06:31 -0500 |
parents | 8a6b36696ca6 |
children |
rev | line source |
---|---|
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* uudecode.c - uudecode / base64 decode |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org> |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uudecode.html |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
7 USE_UUDECODE(NEWTOY(uudecode, ">1o:", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK)) |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 config UUDECODE |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 bool "uudecode" |
840
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
11 default y |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 help |
834
1c61f2827bf8
Fix the USE() macro around NEWTOY, tweak help text, remove unnecessary wrapper, tweak whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
828
diff
changeset
|
13 usage: uudecode [-o OUTFILE] [INFILE] |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 |
834
1c61f2827bf8
Fix the USE() macro around NEWTOY, tweak help text, remove unnecessary wrapper, tweak whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
828
diff
changeset
|
15 Decode file from stdin (or INFILE). |
1c61f2827bf8
Fix the USE() macro around NEWTOY, tweak help text, remove unnecessary wrapper, tweak whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
828
diff
changeset
|
16 |
1c61f2827bf8
Fix the USE() macro around NEWTOY, tweak help text, remove unnecessary wrapper, tweak whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
828
diff
changeset
|
17 -o write to OUTFILE instead of filename in header |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 */ |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 #define FOR_uudecode |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 #include "toys.h" |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 GLOBALS( |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 char *o; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 ) |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 void uudecode_main(void) |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 { |
840
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
29 int ifd = 0, ofd, idx = 0, m = m; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
30 char *line = 0, mode[16], |
839 | 31 *class[] = {"begin%*[ ]%15s%*[ ]%n", "begin-base64%*[ ]%15s%*[ ]%n"}; |
32 | |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
33 if (toys.optc) ifd = xopen(*toys.optargs, O_RDONLY); |
834
1c61f2827bf8
Fix the USE() macro around NEWTOY, tweak help text, remove unnecessary wrapper, tweak whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
828
diff
changeset
|
34 |
840
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
35 while (!idx) { |
839 | 36 free(line); |
37 if (!(line = get_line(ifd))) error_exit("bad EOF"); | |
840
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
38 for (m=0; m < 2; m++) { |
839 | 39 sscanf(line, class[m], mode, &idx); |
40 if (idx) break; | |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
41 } |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
42 } |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
43 |
840
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
44 ofd = xcreate(TT.o ? TT.o : line+idx, O_WRONLY|O_CREAT|O_TRUNC, |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
45 string_to_mode(mode, 0777^toys.old_umask)); |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
46 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
47 for(;;) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
48 char *in, *out; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
49 int olen; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
50 |
839 | 51 free(line); |
840
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
52 if (m == 2 || !(line = get_line(ifd))) break; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
53 if (!strcmp(line, m ? "====" : "end")) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
54 m = 2; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
55 continue; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
56 } |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
57 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
58 olen = 0; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
59 in = out = line; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
60 if (!m) olen = (*(in++) - 32) & 0x3f; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
61 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
62 for (;;) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
63 int i = 0, x = 0, len = 4; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
64 char c = 0; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
65 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
66 if (!m) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
67 if (olen < 1) break; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
68 if (olen < 3) len = olen + 1; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
69 } |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
70 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
71 while (i < len) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
72 if (!(c = *(in++))) goto line_done; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
73 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
74 if (m) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
75 if (c == '=') { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
76 len--; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
77 continue; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
78 } else if (len != 4) break; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
79 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
80 if (c >= 'A' && c <= 'Z') c -= 'A'; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
81 else if (c >= 'a' && c <= 'z') c += 26 - 'a'; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
82 else if (c >= '0' && c <= '9') c += 52 - '0'; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
83 else if (c == '+') c = 62; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
84 else if (c == '/') c = 63; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
85 else continue; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
86 } else c = (c - 32) & 0x3f; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
87 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
88 x |= c << (6*(3-i)); |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
89 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
90 if (i && i < len) { |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
91 *(out++) = (x>>(8*(3-i))) & 0xff; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
92 olen--; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
93 } |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
94 i++; |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
95 } |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
96 |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
97 if (i && i!=len) error_exit("bad %s", line); |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
98 } |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
99 line_done: |
8a6b36696ca6
Clean uudecode up the rest of the way, move pending->posix and default y.
Rob Landley <rob@landley.net>
parents:
839
diff
changeset
|
100 xwrite(ofd, line, out-line); |
839 | 101 } |
102 | |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
103 if (CFG_TOYBOX_FREE) { |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
104 if (ifd) close(ifd); |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
105 close(ofd); |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
106 } |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 } |