diff options
Diffstat (limited to 'coreutils/uudecode.c')
-rw-r--r-- | coreutils/uudecode.c | 73 |
1 files changed, 4 insertions, 69 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 0da9b0988..0c4311f24 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c | |||
@@ -13,7 +13,7 @@ | |||
13 | #include "libbb.h" | 13 | #include "libbb.h" |
14 | 14 | ||
15 | #if ENABLE_UUDECODE | 15 | #if ENABLE_UUDECODE |
16 | static void read_stduu(FILE *src_stream, FILE *dst_stream) | 16 | static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM) |
17 | { | 17 | { |
18 | char *line; | 18 | char *line; |
19 | 19 | ||
@@ -75,71 +75,6 @@ static void read_stduu(FILE *src_stream, FILE *dst_stream) | |||
75 | } | 75 | } |
76 | #endif | 76 | #endif |
77 | 77 | ||
78 | static void read_base64(FILE *src_stream, FILE *dst_stream) | ||
79 | { | ||
80 | int term_count = 0; | ||
81 | |||
82 | while (1) { | ||
83 | unsigned char translated[4]; | ||
84 | int count = 0; | ||
85 | |||
86 | /* Process one group of 4 chars */ | ||
87 | while (count < 4) { | ||
88 | char *table_ptr; | ||
89 | int ch; | ||
90 | |||
91 | /* Get next _valid_ character. | ||
92 | * bb_uuenc_tbl_base64[] contains this string: | ||
93 | * 0 1 2 3 4 5 6 | ||
94 | * 012345678901234567890123456789012345678901234567890123456789012345 | ||
95 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n" | ||
96 | */ | ||
97 | do { | ||
98 | ch = fgetc(src_stream); | ||
99 | if (ch == EOF) { | ||
100 | if (ENABLE_BASE64 | ||
101 | && (!ENABLE_UUDECODE || applet_name[0] == 'b') | ||
102 | && count == 0 | ||
103 | ) { | ||
104 | return; | ||
105 | } | ||
106 | bb_error_msg_and_die("short file"); | ||
107 | } | ||
108 | table_ptr = strchr(bb_uuenc_tbl_base64, ch); | ||
109 | } while (!table_ptr); | ||
110 | |||
111 | /* Convert encoded character to decimal */ | ||
112 | ch = table_ptr - bb_uuenc_tbl_base64; | ||
113 | |||
114 | if (ch == 65 /* '\n' */) { | ||
115 | /* Terminating "====" line? */ | ||
116 | if (term_count == 4) | ||
117 | return; /* yes */ | ||
118 | term_count = 0; | ||
119 | continue; | ||
120 | } | ||
121 | /* ch is 64 is char was '=', otherwise 0..63 */ | ||
122 | translated[count] = ch & 63; /* 64 -> 0 */ | ||
123 | if (ch == 64) { | ||
124 | term_count++; | ||
125 | break; | ||
126 | } | ||
127 | count++; | ||
128 | } | ||
129 | |||
130 | /* Merge 6 bit chars to 8 bit. | ||
131 | * count can be < 4 when we decode the tail: | ||
132 | * "eQ==" -> "y", not "y NUL NUL" | ||
133 | */ | ||
134 | if (count > 1) | ||
135 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); | ||
136 | if (count > 2) | ||
137 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); | ||
138 | if (count > 3) | ||
139 | fputc(translated[2] << 6 | translated[3], dst_stream); | ||
140 | } /* while (1) */ | ||
141 | } | ||
142 | |||
143 | #if ENABLE_UUDECODE | 78 | #if ENABLE_UUDECODE |
144 | int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 79 | int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
145 | int uudecode_main(int argc UNUSED_PARAM, char **argv) | 80 | int uudecode_main(int argc UNUSED_PARAM, char **argv) |
@@ -158,7 +93,7 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
158 | 93 | ||
159 | /* Search for the start of the encoding */ | 94 | /* Search for the start of the encoding */ |
160 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { | 95 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { |
161 | void (*decode_fn_ptr)(FILE *src, FILE *dst); | 96 | void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags); |
162 | char *line_ptr; | 97 | char *line_ptr; |
163 | FILE *dst_stream; | 98 | FILE *dst_stream; |
164 | int mode; | 99 | int mode; |
@@ -189,7 +124,7 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
189 | fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO)); | 124 | fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
190 | } | 125 | } |
191 | free(line); | 126 | free(line); |
192 | decode_fn_ptr(src_stream, dst_stream); | 127 | decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR); |
193 | /* fclose_if_not_stdin(src_stream); - redundant */ | 128 | /* fclose_if_not_stdin(src_stream); - redundant */ |
194 | return EXIT_SUCCESS; | 129 | return EXIT_SUCCESS; |
195 | } | 130 | } |
@@ -231,7 +166,7 @@ int base64_main(int argc UNUSED_PARAM, char **argv) | |||
231 | *--argv = (char*)"-"; | 166 | *--argv = (char*)"-"; |
232 | src_stream = xfopen_stdin(argv[0]); | 167 | src_stream = xfopen_stdin(argv[0]); |
233 | if (opts) { | 168 | if (opts) { |
234 | read_base64(src_stream, stdout); | 169 | read_base64(src_stream, stdout, /*flags:*/ (char)EOF); |
235 | } else { | 170 | } else { |
236 | enum { | 171 | enum { |
237 | SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */ | 172 | SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */ |