Remove bb_ prefixes from xfuncs.c (and a few other places), consolidate
[oweals/busybox.git] / coreutils / uuencode.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Copyright (C) 2000 by Glenn McGrath
4  *
5  *  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.
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include "busybox.h"
12
13 /* Conversion table.  for base 64 */
14 static const char tbl_base64[65] = {
15         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
16         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
17         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
18         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
19         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
20         'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
21         'w', 'x', 'y', 'z', '0', '1', '2', '3',
22         '4', '5', '6', '7', '8', '9', '+', '/',
23         '=' /* termination character */
24 };
25
26 static const char tbl_std[65] = {
27         '`', '!', '"', '#', '$', '%', '&', '\'',
28         '(', ')', '*', '+', ',', '-', '.', '/',
29         '0', '1', '2', '3', '4', '5', '6', '7',
30         '8', '9', ':', ';', '<', '=', '>', '?',
31         '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
32         'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
33         'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
34         'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
35         '`' /* termination character */
36 };
37
38 /*
39  * Encode the string S of length LENGTH to base64 format and place it
40  * to STORE.  STORE will be 0-terminated, and must point to a writable
41  * buffer of at least 1+BASE64_LENGTH(length) bytes.
42  * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
43  */
44 static void uuencode (const unsigned char *s, char *store, const int length, const char *tbl)
45 {
46         int i;
47         char *p = store;
48
49         /* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
50         for (i = 0; i < length; i += 3) {
51                 *p++ = tbl[s[0] >> 2];
52                 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
53                 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
54                 *p++ = tbl[s[2] & 0x3f];
55                 s += 3;
56         }
57         /* Pad the result if necessary...  */
58         if (i == length + 1) {
59                 *(p - 1) = tbl[64];
60         }
61         else if (i == length + 2) {
62                 *(p - 1) = *(p - 2) = tbl[64];
63         }
64         /* ...and zero-terminate it.  */
65         *p = '\0';
66 }
67
68 #define SRC_BUF_SIZE    45  // This *MUST* be a multiple of 3
69 #define DST_BUF_SIZE    4 * ((SRC_BUF_SIZE + 2) / 3)
70 int uuencode_main(int argc, char **argv)
71 {
72         const size_t src_buf_size = SRC_BUF_SIZE;
73         const size_t dst_buf_size = DST_BUF_SIZE;
74         size_t write_size = dst_buf_size;
75         struct stat stat_buf;
76         FILE *src_stream = stdin;
77         const char *tbl;
78         size_t size;
79         mode_t mode;
80         RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1);
81         RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1);
82
83         tbl = tbl_std;
84         if (bb_getopt_ulflags(argc, argv, "m") & 1) {
85                 tbl = tbl_base64;
86         }
87
88         switch (argc - optind) {
89                 case 2:
90                         src_stream = xfopen(argv[optind], "r");
91                         xstat(argv[optind], &stat_buf);
92                         mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
93                         if (src_stream == stdout) {
94                                 puts("NULL");
95                         }
96                         break;
97                 case 1:
98                         mode = 0666 & ~umask(0666);
99                         break;
100                 default:
101                         bb_show_usage();
102         }
103
104         bb_printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]);
105
106         while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
107                 if (size != src_buf_size) {
108                         /* write_size is always 60 until the last line */
109                         write_size=(4 * ((size + 2) / 3));
110                         /* pad with 0s so we can just encode extra bits */
111                         memset(&src_buf[size], 0, src_buf_size - size);
112                 }
113                 /* Encode the buffer we just read in */
114                 uuencode((unsigned char*)src_buf, dst_buf, size, tbl);
115
116                 putchar('\n');
117                 if (tbl == tbl_std) {
118                         putchar(tbl[size]);
119                 }
120                 if (fwrite(dst_buf, 1, write_size, stdout) != write_size) {
121                         bb_perror_msg_and_die(bb_msg_write_error);
122                 }
123         }
124         bb_printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n");
125
126         xferror(src_stream, "source");  /* TODO - Fix this! */
127
128         bb_fflush_stdout_and_exit(EXIT_SUCCESS);
129 }