blob: e8f8d541c8e52f523e7ef7145a5faf24639c51fa [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)
Glenn L McGrath089deca2001-07-28 21:06:13 +000016int uuencode_main(int argc, char **argv)
17{
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000018 const size_t src_buf_size = SRC_BUF_SIZE;
19 const size_t dst_buf_size = DST_BUF_SIZE;
20 size_t write_size = dst_buf_size;
Glenn L McGrath089deca2001-07-28 21:06:13 +000021 struct stat stat_buf;
22 FILE *src_stream = stdin;
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 const char *tbl;
Glenn L McGrath089deca2001-07-28 21:06:13 +000024 size_t size;
25 mode_t mode;
Eric Andersenb077c9f2002-12-06 21:39:48 +000026 RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1);
27 RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1);
Glenn L McGrath089deca2001-07-28 21:06:13 +000028
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000029 tbl = bb_uuenc_tbl_std;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000030 if (getopt32(argc, argv, "m") & 1) {
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000031 tbl = bb_uuenc_tbl_base64;
Glenn L McGrath089deca2001-07-28 21:06:13 +000032 }
33
34 switch (argc - optind) {
35 case 2:
Rob Landleyd921b2e2006-08-03 15:41:12 +000036 src_stream = xfopen(argv[optind], "r");
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000037 xstat(argv[optind], &stat_buf);
Glenn L McGrath089deca2001-07-28 21:06:13 +000038 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
39 if (src_stream == stdout) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 puts("NULL");
Glenn L McGrath089deca2001-07-28 21:06:13 +000041 }
42 break;
43 case 1:
Glenn L McGrathb6071b62001-07-29 06:04:26 +000044 mode = 0666 & ~umask(0666);
Glenn L McGrath089deca2001-07-28 21:06:13 +000045 break;
46 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_show_usage();
Glenn L McGrath089deca2001-07-28 21:06:13 +000048 }
49
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000050 printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, argv[argc - 1]);
Glenn L McGrath089deca2001-07-28 21:06:13 +000051
52 while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
Tim Riker95bf6da2002-05-01 05:57:16 +000053 if (size != src_buf_size) {
54 /* write_size is always 60 until the last line */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000055 write_size = (4 * ((size + 2) / 3));
Tim Riker95bf6da2002-05-01 05:57:16 +000056 /* pad with 0s so we can just encode extra bits */
57 memset(&src_buf[size], 0, src_buf_size - size);
58 }
Glenn L McGrath089deca2001-07-28 21:06:13 +000059 /* Encode the buffer we just read in */
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000060 bb_uuencode((unsigned char*)src_buf, dst_buf, size, tbl);
Glenn L McGrath089deca2001-07-28 21:06:13 +000061
Tim Riker95bf6da2002-05-01 05:57:16 +000062 putchar('\n');
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000063 if (tbl == bb_uuenc_tbl_std) {
Tim Riker95bf6da2002-05-01 05:57:16 +000064 putchar(tbl[size]);
65 }
66 if (fwrite(dst_buf, 1, write_size, stdout) != write_size) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 bb_perror_msg_and_die(bb_msg_write_error);
Tim Riker95bf6da2002-05-01 05:57:16 +000068 }
Glenn L McGrath089deca2001-07-28 21:06:13 +000069 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000070 printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
Glenn L McGrath089deca2001-07-28 21:06:13 +000071
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000072 die_if_ferror(src_stream, "source"); /* TODO - Fix this! */
Manuel Novoa III cad53642003-03-19 09:13:01 +000073
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000074 fflush_stdout_and_exit(EXIT_SUCCESS);
Glenn L McGrath089deca2001-07-28 21:06:13 +000075}