diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-01 13:24:05 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-01 13:24:05 +0200 |
commit | 6f80fe71f0c3d586dddc0749f744f1112ef8c473 (patch) | |
tree | f3ae81dd59a73af059d51ee36f1c53fec6c75d7f | |
parent | 0004e994934374b5695e004bbcb7b1fd67a170f2 (diff) | |
download | busybox-w32-6f80fe71f0c3d586dddc0749f744f1112ef8c473.tar.gz busybox-w32-6f80fe71f0c3d586dddc0749f744f1112ef8c473.tar.bz2 busybox-w32-6f80fe71f0c3d586dddc0749f744f1112ef8c473.zip |
uudecode,base64: code shrink
function old new delta
read_base64 373 358 -15
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r-- | coreutils/uudecode.c | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 207fb0b8d..0da9b0988 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c | |||
@@ -77,18 +77,21 @@ static void read_stduu(FILE *src_stream, FILE *dst_stream) | |||
77 | 77 | ||
78 | static void read_base64(FILE *src_stream, FILE *dst_stream) | 78 | static void read_base64(FILE *src_stream, FILE *dst_stream) |
79 | { | 79 | { |
80 | int term_count = 1; | 80 | int term_count = 0; |
81 | 81 | ||
82 | while (1) { | 82 | while (1) { |
83 | unsigned char translated[4]; | 83 | unsigned char translated[4]; |
84 | int count = 0; | 84 | int count = 0; |
85 | 85 | ||
86 | /* Process one group of 4 chars */ | ||
86 | while (count < 4) { | 87 | while (count < 4) { |
87 | char *table_ptr; | 88 | char *table_ptr; |
88 | int ch; | 89 | int ch; |
89 | 90 | ||
90 | /* Get next _valid_ character. | 91 | /* Get next _valid_ character. |
91 | * global vector bb_uuenc_tbl_base64[] contains this string: | 92 | * bb_uuenc_tbl_base64[] contains this string: |
93 | * 0 1 2 3 4 5 6 | ||
94 | * 012345678901234567890123456789012345678901234567890123456789012345 | ||
92 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n" | 95 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n" |
93 | */ | 96 | */ |
94 | do { | 97 | do { |
@@ -103,42 +106,38 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) | |||
103 | bb_error_msg_and_die("short file"); | 106 | bb_error_msg_and_die("short file"); |
104 | } | 107 | } |
105 | table_ptr = strchr(bb_uuenc_tbl_base64, ch); | 108 | table_ptr = strchr(bb_uuenc_tbl_base64, ch); |
106 | } while (table_ptr == NULL); | 109 | } while (!table_ptr); |
107 | 110 | ||
108 | /* Convert encoded character to decimal */ | 111 | /* Convert encoded character to decimal */ |
109 | ch = table_ptr - bb_uuenc_tbl_base64; | 112 | ch = table_ptr - bb_uuenc_tbl_base64; |
110 | 113 | ||
111 | if (*table_ptr == '=') { | 114 | if (ch == 65 /* '\n' */) { |
112 | if (term_count == 0) { | 115 | /* Terminating "====" line? */ |
113 | translated[count] = '\0'; | 116 | if (term_count == 4) |
114 | break; | 117 | return; /* yes */ |
115 | } | ||
116 | term_count++; | ||
117 | } else if (*table_ptr == '\n') { | ||
118 | /* Check for terminating line */ | ||
119 | if (term_count == 5) { | ||
120 | return; | ||
121 | } | ||
122 | term_count = 1; | ||
123 | continue; | ||
124 | } else { | ||
125 | translated[count] = ch; | ||
126 | count++; | ||
127 | term_count = 0; | 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; | ||
128 | } | 126 | } |
127 | count++; | ||
129 | } | 128 | } |
130 | 129 | ||
131 | /* Merge 6 bit chars to 8 bit */ | 130 | /* Merge 6 bit chars to 8 bit. |
132 | if (count > 1) { | 131 | * count can be < 4 when we decode the tail: |
132 | * "eQ==" -> "y", not "y NUL NUL" | ||
133 | */ | ||
134 | if (count > 1) | ||
133 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); | 135 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); |
134 | } | 136 | if (count > 2) |
135 | if (count > 2) { | ||
136 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); | 137 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); |
137 | } | 138 | if (count > 3) |
138 | if (count > 3) { | ||
139 | fputc(translated[2] << 6 | translated[3], dst_stream); | 139 | fputc(translated[2] << 6 | translated[3], dst_stream); |
140 | } | 140 | } /* while (1) */ |
141 | } | ||
142 | } | 141 | } |
143 | 142 | ||
144 | #if ENABLE_UUDECODE | 143 | #if ENABLE_UUDECODE |