diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-04 23:32:35 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-04 23:32:35 +0000 |
commit | 746204b1b8ee1948ca91637eeb34043e5e4f0aad (patch) | |
tree | 030dde44d64e3420a1980674c1c8815807c45ca4 | |
parent | 8c1aaf32978da33a462997b269536f1de47c79c6 (diff) | |
download | busybox-w32-746204b1b8ee1948ca91637eeb34043e5e4f0aad.tar.gz busybox-w32-746204b1b8ee1948ca91637eeb34043e5e4f0aad.tar.bz2 busybox-w32-746204b1b8ee1948ca91637eeb34043e5e4f0aad.zip |
uudecode: fix to base64 decode by Jorgen Cederlof <jcz@google.com>
improved help texts
# make bloatcheck
function old new delta
.rodata 127000 127032 +32
packed_usage 22156 22151 -5
uudecode_main 360 348 -12
uuencode_main 490 468 -22
read_base64 283 254 -29
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/4 up/down: 32/-68) Total: -36 bytes
-rw-r--r-- | coreutils/uudecode.c | 68 | ||||
-rw-r--r-- | coreutils/uuencode.c | 5 | ||||
-rw-r--r-- | include/usage.h | 11 |
3 files changed, 56 insertions, 28 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 779710edd..287386d9d 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c | |||
@@ -70,7 +70,7 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) | |||
70 | { | 70 | { |
71 | static const char base64_table[] = | 71 | static const char base64_table[] = |
72 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"; | 72 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"; |
73 | int term_count = 0; | 73 | int term_count = 1; |
74 | 74 | ||
75 | while (1) { | 75 | while (1) { |
76 | char translated[4]; | 76 | char translated[4]; |
@@ -86,19 +86,19 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) | |||
86 | if (ch == EOF) { | 86 | if (ch == EOF) { |
87 | bb_error_msg_and_die("short file"); | 87 | bb_error_msg_and_die("short file"); |
88 | } | 88 | } |
89 | } while ((table_ptr = strchr(base64_table, ch)) == NULL); | 89 | table_ptr = strchr(base64_table, ch); |
90 | } while (table_ptr == NULL); | ||
90 | 91 | ||
91 | /* Convert encoded charcter to decimal */ | 92 | /* Convert encoded charcter to decimal */ |
92 | ch = table_ptr - base64_table; | 93 | ch = table_ptr - base64_table; |
93 | 94 | ||
94 | if (*table_ptr == '=') { | 95 | if (*table_ptr == '=') { |
95 | if (term_count == 0) { | 96 | if (term_count == 0) { |
96 | translated[count] = 0; | 97 | translated[count] = '\0'; |
97 | break; | 98 | break; |
98 | } | 99 | } |
99 | term_count++; | 100 | term_count++; |
100 | } | 101 | } else if (*table_ptr == '\n') { |
101 | else if (*table_ptr == '\n') { | ||
102 | /* Check for terminating line */ | 102 | /* Check for terminating line */ |
103 | if (term_count == 5) { | 103 | if (term_count == 5) { |
104 | return; | 104 | return; |
@@ -113,7 +113,9 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) | |||
113 | } | 113 | } |
114 | 114 | ||
115 | /* Merge 6 bit chars to 8 bit */ | 115 | /* Merge 6 bit chars to 8 bit */ |
116 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); | 116 | if (count > 1) { |
117 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); | ||
118 | } | ||
117 | if (count > 2) { | 119 | if (count > 2) { |
118 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); | 120 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); |
119 | } | 121 | } |
@@ -126,19 +128,16 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) | |||
126 | int uudecode_main(int argc, char **argv); | 128 | int uudecode_main(int argc, char **argv); |
127 | int uudecode_main(int argc, char **argv) | 129 | int uudecode_main(int argc, char **argv) |
128 | { | 130 | { |
129 | FILE *src_stream; | 131 | FILE *src_stream = stdin; |
130 | char *outname = NULL; | 132 | char *outname = NULL; |
131 | char *line; | 133 | char *line; |
132 | 134 | ||
135 | opt_complementary = "?1"; /* 1 argument max */ | ||
133 | getopt32(argc, argv, "o:", &outname); | 136 | getopt32(argc, argv, "o:", &outname); |
137 | argv += optind; | ||
134 | 138 | ||
135 | if (optind == argc) { | 139 | if (argv[0]) |
136 | src_stream = stdin; | 140 | src_stream = xfopen(argv[0], "r"); |
137 | } else if (optind + 1 == argc) { | ||
138 | src_stream = xfopen(argv[optind], "r"); | ||
139 | } else { | ||
140 | bb_show_usage(); | ||
141 | } | ||
142 | 141 | ||
143 | /* Search for the start of the encoding */ | 142 | /* Search for the start of the encoding */ |
144 | while ((line = xmalloc_getline(src_stream)) != NULL) { | 143 | while ((line = xmalloc_getline(src_stream)) != NULL) { |
@@ -158,6 +157,7 @@ int uudecode_main(int argc, char **argv) | |||
158 | continue; | 157 | continue; |
159 | } | 158 | } |
160 | 159 | ||
160 | /* begin line found. decode and exit */ | ||
161 | mode = strtoul(line_ptr, NULL, 8); | 161 | mode = strtoul(line_ptr, NULL, 8); |
162 | if (outname == NULL) { | 162 | if (outname == NULL) { |
163 | outname = strchr(line_ptr, ' '); | 163 | outname = strchr(line_ptr, ' '); |
@@ -166,16 +166,48 @@ int uudecode_main(int argc, char **argv) | |||
166 | } | 166 | } |
167 | outname++; | 167 | outname++; |
168 | } | 168 | } |
169 | if (LONE_DASH(outname)) { | 169 | dst_stream = stdout; |
170 | dst_stream = stdout; | 170 | if (NOT_LONE_DASH(outname)) { |
171 | } else { | ||
172 | dst_stream = xfopen(outname, "w"); | 171 | dst_stream = xfopen(outname, "w"); |
173 | chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)); | 172 | chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
174 | } | 173 | } |
175 | free(line); | 174 | free(line); |
176 | decode_fn_ptr(src_stream, dst_stream); | 175 | decode_fn_ptr(src_stream, dst_stream); |
177 | fclose_if_not_stdin(src_stream); | 176 | /* fclose_if_not_stdin(src_stream); - redundant */ |
178 | return EXIT_SUCCESS; | 177 | return EXIT_SUCCESS; |
179 | } | 178 | } |
180 | bb_error_msg_and_die("no 'begin' line"); | 179 | bb_error_msg_and_die("no 'begin' line"); |
181 | } | 180 | } |
181 | |||
182 | /* Test script. | ||
183 | Put this into an empty dir with busybox binary, an run. | ||
184 | |||
185 | #!/bin/sh | ||
186 | test -x busybox || { echo "No ./busybox?"; exit; } | ||
187 | ln -sf busybox uudecode | ||
188 | ln -sf busybox uuencode | ||
189 | >A_null | ||
190 | echo -n A >A | ||
191 | echo -n AB >AB | ||
192 | echo -n ABC >ABC | ||
193 | echo -n ABCD >ABCD | ||
194 | echo -n ABCDE >ABCDE | ||
195 | echo -n ABCDEF >ABCDEF | ||
196 | cat busybox >A_bbox | ||
197 | for f in A*; do | ||
198 | echo uuencode $f | ||
199 | ./uuencode $f <$f >u_$f | ||
200 | ./uuencode -m $f <$f >m_$f | ||
201 | done | ||
202 | mkdir unpk_u unpk_m 2>/dev/null | ||
203 | for f in u_*; do | ||
204 | ./uudecode <$f -o unpk_u/${f:2} | ||
205 | diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1 | ||
206 | echo uudecode $f: $? | ||
207 | done | ||
208 | for f in m_*; do | ||
209 | ./uudecode <$f -o unpk_m/${f:2} | ||
210 | diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1 | ||
211 | echo uudecode $f: $? | ||
212 | done | ||
213 | */ | ||
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c index 9490474b9..b54e317a2 100644 --- a/coreutils/uuencode.c +++ b/coreutils/uuencode.c | |||
@@ -28,7 +28,7 @@ int uuencode_main(int argc, char **argv) | |||
28 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); | 28 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); |
29 | 29 | ||
30 | tbl = bb_uuenc_tbl_std; | 30 | tbl = bb_uuenc_tbl_std; |
31 | if (getopt32(argc, argv, "m") & 1) { | 31 | if (getopt32(argc, argv, "m")) { |
32 | tbl = bb_uuenc_tbl_base64; | 32 | tbl = bb_uuenc_tbl_base64; |
33 | } | 33 | } |
34 | 34 | ||
@@ -37,9 +37,6 @@ int uuencode_main(int argc, char **argv) | |||
37 | src_stream = xfopen(argv[optind], "r"); | 37 | src_stream = xfopen(argv[optind], "r"); |
38 | xstat(argv[optind], &stat_buf); | 38 | xstat(argv[optind], &stat_buf); |
39 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); | 39 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
40 | if (src_stream == stdout) { | ||
41 | puts("NULL"); | ||
42 | } | ||
43 | break; | 40 | break; |
44 | case 1: | 41 | case 1: |
45 | mode = 0666 & ~umask(0666); | 42 | mode = 0666 & ~umask(0666); |
diff --git a/include/usage.h b/include/usage.h index 085bbdd7b..13d79b639 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -3664,20 +3664,19 @@ USE_FEATURE_RUN_PARTS_FANCY("\n -l Prints names of all matching files even when | |||
3664 | "[pauses for 1 second]\n" | 3664 | "[pauses for 1 second]\n" |
3665 | 3665 | ||
3666 | #define uudecode_trivial_usage \ | 3666 | #define uudecode_trivial_usage \ |
3667 | "[FILE]..." | 3667 | "[-o outfile] [infile]" |
3668 | #define uudecode_full_usage \ | 3668 | #define uudecode_full_usage \ |
3669 | "Uudecode a file" \ | 3669 | "Uudecode a file\n" \ |
3670 | "\n\nOptions:\n" \ | 3670 | "NB: finds outfile name in uuencoded source unless -o is given" |
3671 | " -o FILE Direct output to FILE" | ||
3672 | #define uudecode_example_usage \ | 3671 | #define uudecode_example_usage \ |
3673 | "$ uudecode -o busybox busybox.uu\n" \ | 3672 | "$ uudecode -o busybox busybox.uu\n" \ |
3674 | "$ ls -l busybox\n" \ | 3673 | "$ ls -l busybox\n" \ |
3675 | "-rwxr-xr-x 1 ams ams 245264 Jun 7 21:35 busybox\n" | 3674 | "-rwxr-xr-x 1 ams ams 245264 Jun 7 21:35 busybox\n" |
3676 | 3675 | ||
3677 | #define uuencode_trivial_usage \ | 3676 | #define uuencode_trivial_usage \ |
3678 | "[OPTION] [INFILE] REMOTEFILE" | 3677 | "[-m] [infile] stored_filename" |
3679 | #define uuencode_full_usage \ | 3678 | #define uuencode_full_usage \ |
3680 | "Uuencode a file" \ | 3679 | "Uuencode a file to stdout" \ |
3681 | "\n\nOptions:\n" \ | 3680 | "\n\nOptions:\n" \ |
3682 | " -m Use base64 encoding per RFC1521" | 3681 | " -m Use base64 encoding per RFC1521" |
3683 | #define uuencode_example_usage \ | 3682 | #define uuencode_example_usage \ |