1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1984, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
/*
* uuencode [-m] [input] decode_pathname
*
* Encode a file so it can be mailed to a remote system.
*/
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
/*
* (Size of TABLE_SIZE octal is large enough to convert a basic 6-bit
* data chunk.)
*/
#define TABLE_SIZE 0x40
static unsigned char base64_table_initializer[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char encode_table[TABLE_SIZE];
/* ENC is the basic 1 character encoding function to make a char printing */
#define ENC(c) encode_table[(c) & 077]
static void encode(FILE *, FILE *, int);
static char *prog;
int
main(int argc, char **argv)
{
FILE *in;
struct stat sbuf;
mode_t mode = 0;
int c, i;
int errflag = 0;
int base64flag = 0;
char oline[PATH_MAX + 20];
prog = argv[0];
(void) signal(SIGPIPE, SIG_DFL);
/* Set locale environment variables local definitions */
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "m")) != EOF)
switch (c) {
case 'm':
base64flag++;
break;
default:
case '?':
errflag++;
}
argc -= optind;
argv = &argv[optind];
/* optional 1st argument */
if (argc > 1) {
if ((in = fopen(*argv, "r")) == NULL) {
perror(*argv);
exit(1);
}
argv++; argc--;
} else {
in = stdin;
mode = 0777;
}
if ((argc != 1) || errflag) {
(void) fprintf(stderr,
gettext("Usage: %s [-m] [infile] remotefile\n"), prog);
exit(2);
}
/* figure out the input file mode */
errno = 0;
if (fstat(fileno(in), &sbuf) < 0 || !S_ISREG(sbuf.st_mode)) {
mode = 0666 & ~umask(0666);
} else {
mode = sbuf.st_mode & 0777;
}
/*
* encoding varies depending on whether we are
* using base64 encoding or the historical algorithm
*/
if (base64flag) {
(void) memcpy(encode_table, base64_table_initializer,
sizeof (encode_table));
} else {
for (i = 0; i < TABLE_SIZE; i++)
encode_table[i] = (unsigned char)i + 0x20;
}
/*
* here's the meat of the whole thing
*/
if (base64flag)
(void) snprintf(oline, sizeof (oline), "begin-base64 %lo %s\n",
(long)mode, *argv);
else
(void) snprintf(oline, sizeof (oline), "begin %lo %s\n",
(long)mode, *argv);
if (printf("%s", oline) < 0) {
perror(prog);
exit(6);
}
encode(in, stdout, base64flag);
if (base64flag)
(void) snprintf(oline, sizeof (oline), "====\n");
else
(void) snprintf(oline, sizeof (oline), "end\n");
if (printf("%s", oline) < 0) {
perror(prog);
exit(6);
}
if (ferror(stdout) != 0 || fclose(stdout) != 0) {
perror(prog);
exit(6);
}
return (0);
}
/*
* copy from in to out, encoding as you go along.
*/
static void
encode(FILE *in, FILE *out, int base64)
{
unsigned char in_buf[80];
unsigned char out_buf[112];
unsigned char *iptr, *optr;
int i;
size_t n, opos;
if (! base64) {
/* historical algorithm */
for (;;) {
iptr = in_buf;
optr = out_buf;
/* 1 (up to) 45 character line */
n = fread(iptr, 1, 45, in);
*(optr++) = ENC(n);
for (i = 0; i < n; i += 3) {
*(optr++) = ENC(*iptr >> 2);
*(optr++) = ENC((*iptr << 4) & 060 |
(*(iptr + 1) >> 4) & 017);
*(optr++) = ENC((*(iptr + 1) << 2) & 074 |
(*(iptr + 2) >> 6) & 03);
*(optr++) = ENC(*(iptr + 2) & 077);
iptr += 3;
}
*(optr++) = '\n';
/*LINTED*/
(void) fwrite(out_buf, 1, (size_t)(optr - out_buf),
out);
if (ferror(out)) {
perror(prog);
exit(6);
}
if (n == 0)
break;
}
} else {
/* base64 algorithm */
optr = out_buf;
/*
* read must be a multiple of 3 bytes for
* this algorithm to work, and also must
* be small enough that read_size * (4/3)
* will always be 76 bytes or less, since
* base64 lines can be no longer than that
*/
while ((n = fread(in_buf, 1, 51, in)) > 0) {
opos = 0;
iptr = in_buf;
for (i = 0; i < n / 3; i++) {
*(optr++) = ENC(*iptr >> 2);
*(optr++) = ENC((*iptr << 4) & 060 |
(*(iptr + 1) >> 4) & 017);
*(optr++) = ENC((*(iptr + 1) << 2)
& 074 | (*(iptr + 2) >> 6) & 03);
*(optr++) = ENC(*(iptr + 2) & 077);
iptr += 3;
opos += 3;
/* need output padding ? */
if (n - opos < 3)
break;
(void) fwrite(out_buf, 1,
/*LINTED*/
(size_t)(optr - out_buf), out);
if (ferror(out)) {
perror(prog);
exit(6);
}
optr = out_buf;
}
/*
* Take care of any output padding that is
* necessary.
*/
assert(n - opos < 3);
switch (n - opos) {
case 0:
/* no-op - 24 bits of data encoded */
*(optr++) = '\n';
break;
case 1:
/* 8 bits encoded - pad with 2 '=' */
*(optr++) = ENC((*iptr & 0xFC) >> 2);
*(optr++) = ENC((*iptr & 0x03) << 4);
*(optr++) = '=';
*(optr++) = '=';
*(optr++) = '\n';
break;
case 2:
/* 16 bits encoded - pad with 1 '=' */
*(optr++) = ENC((*iptr & 0xFC) >> 2);
*(optr++) = ENC(((*iptr & 0x03) << 4) |
((*(iptr + 1) & 0xF0) >> 4));
*(optr++) = ENC((*(iptr + 1) & 0x0F)
<< 2);
*(optr++) = '=';
*(optr++) = '\n';
break;
default:
/* impossible */
break;
}
(void) fwrite(out_buf, 1,
/*LINTED*/
(size_t)(optr - out_buf), out);
if (ferror(out)) {
perror(prog);
exit(6);
}
optr = out_buf;
}
}
}
|