diff options
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/uudecode.c | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index e4fb0d48b..751976a87 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c | |||
| @@ -181,19 +181,19 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
| 181 | //config: Base64 encode and decode | 181 | //config: Base64 encode and decode |
| 182 | 182 | ||
| 183 | //usage:#define base32_trivial_usage | 183 | //usage:#define base32_trivial_usage |
| 184 | //usage: "[-d] [FILE]" | 184 | //usage: "[-d] [-w COL] [FILE]" |
| 185 | //usage:#define base32_full_usage "\n\n" | 185 | //usage:#define base32_full_usage "\n\n" |
| 186 | //usage: "Base32 encode or decode FILE to standard output" | 186 | //usage: "Base32 encode or decode FILE to standard output" |
| 187 | //usage: "\n -d Decode data" | 187 | //usage: "\n -d Decode data" |
| 188 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" | 188 | //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
| 189 | ////usage: "\n -i When decoding, ignore non-alphabet characters" | 189 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
| 190 | 190 | ||
| 191 | //usage:#define base64_trivial_usage | 191 | //usage:#define base64_trivial_usage |
| 192 | //usage: "[-d] [FILE]" | 192 | //usage: "[-d] [-w COL] [FILE]" |
| 193 | //usage:#define base64_full_usage "\n\n" | 193 | //usage:#define base64_full_usage "\n\n" |
| 194 | //usage: "Base64 encode or decode FILE to standard output" | 194 | //usage: "Base64 encode or decode FILE to standard output" |
| 195 | //usage: "\n -d Decode data" | 195 | //usage: "\n -d Decode data" |
| 196 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" | 196 | //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
| 197 | ////usage: "\n -i When decoding, ignore non-alphabet characters" | 197 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
| 198 | 198 | ||
| 199 | // APPLET_ODDNAME:name main location suid_type help | 199 | // APPLET_ODDNAME:name main location suid_type help |
| @@ -270,41 +270,37 @@ int baseNUM_main(int argc UNUSED_PARAM, char **argv) | |||
| 270 | { | 270 | { |
| 271 | FILE *src_stream; | 271 | FILE *src_stream; |
| 272 | unsigned opts; | 272 | unsigned opts; |
| 273 | unsigned col = 76; | ||
| 273 | 274 | ||
| 274 | opts = getopt32(argv, "^" "d" "\0" "?1"/* 1 arg max*/); | 275 | opts = getopt32(argv, "^" "dw:+" "\0" "?1"/* 1 arg max*/, &col); |
| 275 | argv += optind; | 276 | argv += optind; |
| 276 | 277 | ||
| 277 | if (!argv[0]) | 278 | if (!argv[0]) |
| 278 | *--argv = (char*)"-"; | 279 | *--argv = (char*)"-"; |
| 279 | src_stream = xfopen_stdin(argv[0]); | 280 | src_stream = xfopen_stdin(argv[0]); |
| 280 | if (opts) { | 281 | if (opts & 1) { |
| 281 | int flags = (unsigned char)EOF; | 282 | int flags = (unsigned char)EOF; |
| 282 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) | 283 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) |
| 283 | flags = ((unsigned char)EOF) | BASE64_32; | 284 | flags = ((unsigned char)EOF) | BASE64_32; |
| 284 | read_base64(src_stream, stdout, flags); | 285 | read_base64(src_stream, stdout, flags); |
| 285 | } else { | 286 | } else { |
| 286 | enum { | 287 | enum { |
| 287 | SRC_BUF_SIZE = 76 / 4 * 3, /* this *MUST* be a multiple of 3 */ | 288 | SRC_BUF_SIZE = 3 * 5 * 32, /* this *MUST* be a multiple of 3 and 5 */ |
| 288 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), | 289 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 289 | }; | 290 | }; |
| 290 | char src_buf[SRC_BUF_SIZE]; | 291 | char src_buf[SRC_BUF_SIZE]; |
| 291 | char dst_buf[DST_BUF_SIZE + 1]; | 292 | char dst_buf[DST_BUF_SIZE + 1]; |
| 292 | int src_fd = fileno(src_stream); | 293 | int src_fd = fileno(src_stream); |
| 294 | int rem = 0; | ||
| 295 | |||
| 293 | while (1) { | 296 | while (1) { |
| 294 | size_t size; | 297 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); |
| 295 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) | ||
| 296 | size = 72 / 8 * 5; | ||
| 297 | //FIXME: wrong, default width of base32 is not 72, but 76 chars | ||
| 298 | //(not a multiple of 8 - requires adding wrapping logic) | ||
| 299 | //when this is fixed, can implement -w COL too | ||
| 300 | else | ||
| 301 | size = SRC_BUF_SIZE; | ||
| 302 | |||
| 303 | size = full_read(src_fd, src_buf, size); | ||
| 304 | if (!size) | ||
| 305 | break; | ||
| 306 | if ((ssize_t)size < 0) | 298 | if ((ssize_t)size < 0) |
| 307 | bb_simple_perror_msg_and_die(bb_msg_read_error); | 299 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
| 300 | if (size == 0) { | ||
| 301 | if (rem != 0) bb_putchar('\n'); | ||
| 302 | break; | ||
| 303 | } | ||
| 308 | 304 | ||
| 309 | /* Encode the buffer we just read in */ | 305 | /* Encode the buffer we just read in */ |
| 310 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) { | 306 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) { |
| @@ -315,9 +311,25 @@ int baseNUM_main(int argc UNUSED_PARAM, char **argv) | |||
| 315 | size = 4 * ((size + 2) / 3); | 311 | size = 4 * ((size + 2) / 3); |
| 316 | } | 312 | } |
| 317 | 313 | ||
| 318 | xwrite(STDOUT_FILENO, dst_buf, size); | 314 | if (col == 0) { |
| 319 | bb_putchar('\n'); | 315 | fputs(dst_buf, stdout); |
| 320 | fflush(stdout); | 316 | } else { |
| 317 | char *result = dst_buf; | ||
| 318 | if (rem == 0) | ||
| 319 | rem = col; | ||
| 320 | while (1) { | ||
| 321 | int out = size < rem ? size : rem; | ||
| 322 | rem -= out; | ||
| 323 | printf(rem != 0 ? "%.*s" : "%.*s\n", out, result); | ||
| 324 | if (rem != 0) | ||
| 325 | break; | ||
| 326 | size -= out; | ||
| 327 | if (size == 0) | ||
| 328 | break; | ||
| 329 | result += out; | ||
| 330 | rem = col; | ||
| 331 | } | ||
| 332 | } | ||
| 321 | } | 333 | } |
| 322 | } | 334 | } |
| 323 | 335 | ||
