aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-11-27 15:25:31 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-11-27 15:25:31 +0100
commitc8b3d9a145d2db95869f0ae8081d85c0378f0aef (patch)
tree1a340a4528bf2179a48a757ff85701bdd6033b61
parentfc63549352c371617e68fcf7ecbe4978fa207c6a (diff)
downloadbusybox-w32-c8b3d9a145d2db95869f0ae8081d85c0378f0aef.tar.gz
busybox-w32-c8b3d9a145d2db95869f0ae8081d85c0378f0aef.tar.bz2
busybox-w32-c8b3d9a145d2db95869f0ae8081d85c0378f0aef.zip
base32/64: implement -w COL
function old new delta baseNUM_main 568 655 +87 packed_usage 33478 33533 +55 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 142/0) Total: 142 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/uudecode.c56
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