Mercurial > hg > toybox
annotate toys/pending/uudecode.c @ 839:a315fb343e2b
More uudecode cleanup.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 28 Mar 2013 23:33:27 -0500 |
parents | a60c572a8218 |
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" |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 default n |
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 |
838 | 27 static int uudecode_b64_4bytes(char *out, char *in) |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 { |
838 | 29 int i, len = 3; |
30 unsigned x = 0; | |
31 | |
32 for (i=0; i<4; i++) { | |
33 int c = in[i]; | |
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 |
839 | 35 if (c == '=') { |
36 len--; | |
37 c = 'A'; | |
38 } else if (len != 3) len = 0; | |
39 if (!len) goto bad; | |
838 | 40 |
839 | 41 if (c >= 'A' && c <= 'Z') c -= 'A'; |
42 else if (c >= 'a' && c <= 'z') c += 26 - 'a'; | |
43 else if (c >= '0' && c <= '9') c += 52 - '0'; | |
44 else if (c == '+') c = 62; | |
45 else if (c == '/') c =63; | |
46 else goto bad; | |
47 | |
48 x |= c << (6*(3-i)); | |
838 | 49 if (i && i <= len) *(out++) = (x>>(8*(3-i))) & 0xff; |
50 } | |
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
|
51 |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 return len; |
839 | 53 |
54 bad: | |
55 error_exit("bad input"); | |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 } |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 |
838 | 58 static void uudecode_b64_line(int ofd, char *in, int ilen) |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 { |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 int olen; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 char out[4]; |
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
|
62 |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 while (ilen >= 4) { |
838 | 64 olen = uudecode_b64_4bytes(out, in); |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 xwrite(ofd,out,olen); |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 in += 4; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 ilen -= 4; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 }; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 } |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 |
838 | 71 static void uudecode_uu_4bytes(char *out, char *in, int len) |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 { |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 unsigned int i,x=0; |
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
|
74 |
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
|
75 for (i = 0; i < 4; i++) x |= ((in[i] - 32) & 0x03f) << (6*(3-i)); |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 if (len > 3) len = 3; |
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
|
77 for (i = 0; i < len; i++) *out++ = x >> (8*(2-i)); |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 } |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 |
838 | 80 static void uudecode_uu_line(int ofd, char *in) |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 { |
839 | 82 int olen = (in[0] - 32) & 0x3f; |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 char buf[4]; |
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
|
84 |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 in++; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 while (olen > 0) { |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 uudecode_uu_4bytes(buf,in,olen); |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 xwrite(ofd,buf,olen < 3 ? olen : 3); |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 olen -= 3; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 in += 4; |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 } |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 } |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 void uudecode_main(void) |
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 { |
839 | 96 int ifd = 0, ofd, idx = 0, m; |
97 char *line = 0, | |
98 *class[] = {"begin%*[ ]%15s%*[ ]%n", "begin-base64%*[ ]%15s%*[ ]%n"}; | |
99 | |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
100 |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
101 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
|
102 |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
103 for (;;) { |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
104 char mode[16]; |
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
|
105 |
839 | 106 free(line); |
107 if (!(line = get_line(ifd))) error_exit("bad EOF"); | |
108 for (m=0; m < 2; m++) { | |
109 sscanf(line, class[m], mode, &idx); | |
110 if (idx) break; | |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
111 } |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
112 |
839 | 113 if (!idx) continue; |
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
|
114 |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
115 ofd = xcreate(TT.o ? TT.o : line+idx, O_WRONLY|O_CREAT|O_TRUNC, |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
116 string_to_mode(mode, 0777^toys.old_umask)); |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
117 free(line); |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
118 break; |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
119 } |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
120 |
839 | 121 while ((line = get_line(ifd)) != NULL) { |
122 if (strcmp(line, m ? "====" : "end")) { | |
123 if (m) uudecode_b64_line(ofd, line, strlen(line)); | |
124 else uudecode_uu_line(ofd, line); | |
125 } else m = 2; | |
126 free(line); | |
127 if (m == 2) break; | |
128 } | |
129 | |
835
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
130 if (CFG_TOYBOX_FREE) { |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
131 if (ifd) close(ifd); |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
132 close(ofd); |
89c65a45245a
Incremental cleanup of uudecode.
Rob Landley <rob@landley.net>
parents:
834
diff
changeset
|
133 } |
823
0429050a224b
uuencode and uudecode by Erich Plondke.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 } |