summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenis Vlasenko2007-06-04 23:32:35 +0000
committerDenis Vlasenko2007-06-04 23:32:35 +0000
commit746204b1b8ee1948ca91637eeb34043e5e4f0aad (patch)
tree030dde44d64e3420a1980674c1c8815807c45ca4 /coreutils
parent8c1aaf32978da33a462997b269536f1de47c79c6 (diff)
downloadbusybox-746204b1b8ee1948ca91637eeb34043e5e4f0aad.zip
busybox-746204b1b8ee1948ca91637eeb34043e5e4f0aad.tar.gz
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
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/uudecode.c68
-rw-r--r--coreutils/uuencode.c5
2 files changed, 51 insertions, 22 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c
index 779710e..287386d 100644
--- a/coreutils/uudecode.c
+++ b/coreutils/uudecode.c
@@ -70,7 +70,7 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
{
static const char base64_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
- int term_count = 0;
+ int term_count = 1;
while (1) {
char translated[4];
@@ -86,19 +86,19 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
if (ch == EOF) {
bb_error_msg_and_die("short file");
}
- } while ((table_ptr = strchr(base64_table, ch)) == NULL);
+ table_ptr = strchr(base64_table, ch);
+ } while (table_ptr == NULL);
/* Convert encoded charcter to decimal */
ch = table_ptr - base64_table;
if (*table_ptr == '=') {
if (term_count == 0) {
- translated[count] = 0;
+ translated[count] = '\0';
break;
}
term_count++;
- }
- else if (*table_ptr == '\n') {
+ } else if (*table_ptr == '\n') {
/* Check for terminating line */
if (term_count == 5) {
return;
@@ -113,7 +113,9 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
}
/* Merge 6 bit chars to 8 bit */
- fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
+ if (count > 1) {
+ fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
+ }
if (count > 2) {
fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
}
@@ -126,19 +128,16 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
int uudecode_main(int argc, char **argv);
int uudecode_main(int argc, char **argv)
{
- FILE *src_stream;
+ FILE *src_stream = stdin;
char *outname = NULL;
char *line;
+ opt_complementary = "?1"; /* 1 argument max */
getopt32(argc, argv, "o:", &outname);
+ argv += optind;
- if (optind == argc) {
- src_stream = stdin;
- } else if (optind + 1 == argc) {
- src_stream = xfopen(argv[optind], "r");
- } else {
- bb_show_usage();
- }
+ if (argv[0])
+ src_stream = xfopen(argv[0], "r");
/* Search for the start of the encoding */
while ((line = xmalloc_getline(src_stream)) != NULL) {
@@ -158,6 +157,7 @@ int uudecode_main(int argc, char **argv)
continue;
}
+ /* begin line found. decode and exit */
mode = strtoul(line_ptr, NULL, 8);
if (outname == NULL) {
outname = strchr(line_ptr, ' ');
@@ -166,16 +166,48 @@ int uudecode_main(int argc, char **argv)
}
outname++;
}
- if (LONE_DASH(outname)) {
- dst_stream = stdout;
- } else {
+ dst_stream = stdout;
+ if (NOT_LONE_DASH(outname)) {
dst_stream = xfopen(outname, "w");
chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
}
free(line);
decode_fn_ptr(src_stream, dst_stream);
- fclose_if_not_stdin(src_stream);
+ /* fclose_if_not_stdin(src_stream); - redundant */
return EXIT_SUCCESS;
}
bb_error_msg_and_die("no 'begin' line");
}
+
+/* Test script.
+Put this into an empty dir with busybox binary, an run.
+
+#!/bin/sh
+test -x busybox || { echo "No ./busybox?"; exit; }
+ln -sf busybox uudecode
+ln -sf busybox uuencode
+>A_null
+echo -n A >A
+echo -n AB >AB
+echo -n ABC >ABC
+echo -n ABCD >ABCD
+echo -n ABCDE >ABCDE
+echo -n ABCDEF >ABCDEF
+cat busybox >A_bbox
+for f in A*; do
+ echo uuencode $f
+ ./uuencode $f <$f >u_$f
+ ./uuencode -m $f <$f >m_$f
+done
+mkdir unpk_u unpk_m 2>/dev/null
+for f in u_*; do
+ ./uudecode <$f -o unpk_u/${f:2}
+ diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
+ echo uudecode $f: $?
+done
+for f in m_*; do
+ ./uudecode <$f -o unpk_m/${f:2}
+ diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
+ echo uudecode $f: $?
+done
+*/
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c
index 9490474..b54e317 100644
--- a/coreutils/uuencode.c
+++ b/coreutils/uuencode.c
@@ -28,7 +28,7 @@ int uuencode_main(int argc, char **argv)
RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1);
tbl = bb_uuenc_tbl_std;
- if (getopt32(argc, argv, "m") & 1) {
+ if (getopt32(argc, argv, "m")) {
tbl = bb_uuenc_tbl_base64;
}
@@ -37,9 +37,6 @@ int uuencode_main(int argc, char **argv)
src_stream = xfopen(argv[optind], "r");
xstat(argv[optind], &stat_buf);
mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
- if (src_stream == stdout) {
- puts("NULL");
- }
break;
case 1:
mode = 0666 & ~umask(0666);