blob: 3207d72969756c581452da0900eb4bf8c87a1523 [file] [log] [blame]
Glenn L McGrath089deca2001-07-28 21:06:13 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2000 by Glenn McGrath
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00004 *
Glenn L McGrath089deca2001-07-28 21:06:13 +00005 * based on the function base64_encode from http.c in wget v1.6
6 * Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00007 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00009 */
Rob Landleyd921b2e2006-08-03 15:41:12 +000010
Eric Andersencbe31da2001-02-20 06:14:08 +000011#include "busybox.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000012
Eric Andersen4e573f42000-11-14 23:29:24 +000013
Eric Andersenb077c9f2002-12-06 21:39:48 +000014#define SRC_BUF_SIZE 45 // This *MUST* be a multiple of 3
15#define DST_BUF_SIZE 4 * ((SRC_BUF_SIZE + 2) / 3)
Denis Vlasenko06af2162007-02-03 17:28:39 +000016int uuencode_main(int argc, char **argv);
Glenn L McGrath089deca2001-07-28 21:06:13 +000017int uuencode_main(int argc, char **argv)
18{
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000019 const size_t src_buf_size = SRC_BUF_SIZE;
20 const size_t dst_buf_size = DST_BUF_SIZE;
21 size_t write_size = dst_buf_size;
Glenn L McGrath089deca2001-07-28 21:06:13 +000022 struct stat stat_buf;
23 FILE *src_stream = stdin;
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 const char *tbl;
Glenn L McGrath089deca2001-07-28 21:06:13 +000025 size_t size;
26 mode_t mode;
Eric Andersenb077c9f2002-12-06 21:39:48 +000027 RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1);
28 RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1);
Glenn L McGrath089deca2001-07-28 21:06:13 +000029
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000030 tbl = bb_uuenc_tbl_std;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000031 if (getopt32(argc, argv, "m") & 1) {
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000032 tbl = bb_uuenc_tbl_base64;
Glenn L McGrath089deca2001-07-28 21:06:13 +000033 }
34
35 switch (argc - optind) {
36 case 2:
Rob Landleyd921b2e2006-08-03 15:41:12 +000037 src_stream = xfopen(argv[optind], "r");
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000038 xstat(argv[optind], &stat_buf);
Glenn L McGrath089deca2001-07-28 21:06:13 +000039 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
40 if (src_stream == stdout) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 puts("NULL");
Glenn L McGrath089deca2001-07-28 21:06:13 +000042 }
43 break;
44 case 1:
Glenn L McGrathb6071b62001-07-29 06:04:26 +000045 mode = 0666 & ~umask(0666);
Glenn L McGrath089deca2001-07-28 21:06:13 +000046 break;
47 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_show_usage();
Glenn L McGrath089deca2001-07-28 21:06:13 +000049 }
50
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000051 printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, argv[argc - 1]);
Glenn L McGrath089deca2001-07-28 21:06:13 +000052
53 while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
Tim Riker95bf6da2002-05-01 05:57:16 +000054 if (size != src_buf_size) {
55 /* write_size is always 60 until the last line */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000056 write_size = (4 * ((size + 2) / 3));
Tim Riker95bf6da2002-05-01 05:57:16 +000057 /* pad with 0s so we can just encode extra bits */
58 memset(&src_buf[size], 0, src_buf_size - size);
59 }
Glenn L McGrath089deca2001-07-28 21:06:13 +000060 /* Encode the buffer we just read in */
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000061 bb_uuencode((unsigned char*)src_buf, dst_buf, size, tbl);
Glenn L McGrath089deca2001-07-28 21:06:13 +000062
Tim Riker95bf6da2002-05-01 05:57:16 +000063 putchar('\n');
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000064 if (tbl == bb_uuenc_tbl_std) {
Tim Riker95bf6da2002-05-01 05:57:16 +000065 putchar(tbl[size]);
66 }
67 if (fwrite(dst_buf, 1, write_size, stdout) != write_size) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 bb_perror_msg_and_die(bb_msg_write_error);
Tim Riker95bf6da2002-05-01 05:57:16 +000069 }
Glenn L McGrath089deca2001-07-28 21:06:13 +000070 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000071 printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
Glenn L McGrath089deca2001-07-28 21:06:13 +000072
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000073 die_if_ferror(src_stream, "source"); /* TODO - Fix this! */
Manuel Novoa III cad53642003-03-19 09:13:01 +000074
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000075 fflush_stdout_and_exit(EXIT_SUCCESS);
Glenn L McGrath089deca2001-07-28 21:06:13 +000076}