Deleted Added
full compact
uudecode.c (103195) uudecode.c (103197)
1/*-
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/uudecode/uudecode.c 103195 2002-09-10 18:34:07Z fanf $");
47__FBSDID("$FreeBSD: head/usr.bin/uudecode/uudecode.c 103197 2002-09-10 18:52:03Z fanf $");
48
49/*
50 * uudecode [file ...]
51 *
52 * create the specified file, decoding as you go.
53 * used with uuencode.
54 */
55#include <sys/param.h>
56#include <sys/socket.h>
57#include <sys/stat.h>
58
59#include <netinet/in.h>
60
61#include <err.h>
62#include <pwd.h>
63#include <resolv.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68
69const char *filename;
70char *outfile;
71int cflag, iflag, oflag, pflag, sflag;
72
73static void usage(void);
74int decode(void);
75int decode2(int);
76void base64_decode(const char *);
77
78int
79main(int argc, char *argv[])
80{
81 int rval, ch;
82
83 while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
84 switch(ch) {
85 case 'c':
86 if (oflag)
87 usage();
88 cflag = 1; /* multiple uudecode'd files */
89 break;
90 case 'i':
91 iflag = 1; /* ask before override files */
92 break;
93 case 'o':
94 if (cflag || pflag || sflag)
95 usage();
96 oflag = 1; /* output to the specified file */
97 sflag = 1; /* do not strip pathnames for output */
98 outfile = optarg; /* set the output filename */
99 break;
100 case 'p':
101 if (oflag)
102 usage();
103 pflag = 1; /* print output to stdout */
104 break;
105 case 's':
106 if (oflag)
107 usage();
108 sflag = 1; /* do not strip pathnames for output */
109 break;
110 default:
111 usage();
112 }
113 }
114 argc -= optind;
115 argv += optind;
116
117 if (*argv) {
118 rval = 0;
119 do {
120 if (!freopen(filename = *argv, "r", stdin)) {
121 warn("%s", *argv);
122 rval = 1;
123 continue;
124 }
125 rval |= decode();
126 } while (*++argv);
127 } else {
128 filename = "stdin";
129 rval = decode();
130 }
131 exit(rval);
132}
133
134int
135decode(void)
136{
137 int flag;
138
139 /* decode only one file per input stream */
140 if (!cflag)
48
49/*
50 * uudecode [file ...]
51 *
52 * create the specified file, decoding as you go.
53 * used with uuencode.
54 */
55#include <sys/param.h>
56#include <sys/socket.h>
57#include <sys/stat.h>
58
59#include <netinet/in.h>
60
61#include <err.h>
62#include <pwd.h>
63#include <resolv.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68
69const char *filename;
70char *outfile;
71int cflag, iflag, oflag, pflag, sflag;
72
73static void usage(void);
74int decode(void);
75int decode2(int);
76void base64_decode(const char *);
77
78int
79main(int argc, char *argv[])
80{
81 int rval, ch;
82
83 while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
84 switch(ch) {
85 case 'c':
86 if (oflag)
87 usage();
88 cflag = 1; /* multiple uudecode'd files */
89 break;
90 case 'i':
91 iflag = 1; /* ask before override files */
92 break;
93 case 'o':
94 if (cflag || pflag || sflag)
95 usage();
96 oflag = 1; /* output to the specified file */
97 sflag = 1; /* do not strip pathnames for output */
98 outfile = optarg; /* set the output filename */
99 break;
100 case 'p':
101 if (oflag)
102 usage();
103 pflag = 1; /* print output to stdout */
104 break;
105 case 's':
106 if (oflag)
107 usage();
108 sflag = 1; /* do not strip pathnames for output */
109 break;
110 default:
111 usage();
112 }
113 }
114 argc -= optind;
115 argv += optind;
116
117 if (*argv) {
118 rval = 0;
119 do {
120 if (!freopen(filename = *argv, "r", stdin)) {
121 warn("%s", *argv);
122 rval = 1;
123 continue;
124 }
125 rval |= decode();
126 } while (*++argv);
127 } else {
128 filename = "stdin";
129 rval = decode();
130 }
131 exit(rval);
132}
133
134int
135decode(void)
136{
137 int flag;
138
139 /* decode only one file per input stream */
140 if (!cflag)
141 return(decode2(0));
141 return (decode2(0));
142
143 /* multiple uudecode'd files */
144 for (flag = 0; ; flag++)
145 if (decode2(flag))
142
143 /* multiple uudecode'd files */
144 for (flag = 0; ; flag++)
145 if (decode2(flag))
146 return(1);
146 return (1);
147 else if (feof(stdin))
148 break;
149
147 else if (feof(stdin))
148 break;
149
150 return(0);
150 return (0);
151}
152
153int
154decode2(int flag)
155{
156 struct passwd *pw;
157 register int n;
158 register char ch, *p;
159 int base64, n1;
160 char buf[MAXPATHLEN+1];
161 char buffn[MAXPATHLEN+1]; /* file name buffer */
162 char *mode, *s;
163 void *mode_handle;
164 struct stat st;
165
166 base64 = 0;
167 /* search for header line */
168 do {
169 if (!fgets(buf, sizeof(buf), stdin)) {
170 if (flag) /* no error */
151}
152
153int
154decode2(int flag)
155{
156 struct passwd *pw;
157 register int n;
158 register char ch, *p;
159 int base64, n1;
160 char buf[MAXPATHLEN+1];
161 char buffn[MAXPATHLEN+1]; /* file name buffer */
162 char *mode, *s;
163 void *mode_handle;
164 struct stat st;
165
166 base64 = 0;
167 /* search for header line */
168 do {
169 if (!fgets(buf, sizeof(buf), stdin)) {
170 if (flag) /* no error */
171 return(0);
171 return (0);
172
173 warnx("%s: no \"begin\" line", filename);
172
173 warnx("%s: no \"begin\" line", filename);
174 return(1);
174 return (1);
175 }
176 } while (strncmp(buf, "begin", 5) != 0);
177
178 if (strncmp(buf, "begin-base64", 12) == 0)
179 base64 = 1;
180
181 /* Parse the header: begin{,-base64} mode outfile. */
182 s = strtok(buf, " ");
183 if (s == NULL)
184 errx(1, "no mode or filename in input file");
185 s = strtok(NULL, " ");
186 if (s == NULL)
187 errx(1, "no mode in input file");
188 else {
189 mode = strdup(s);
190 if (mode == NULL)
191 err(1, "strdup()");
192 }
193 if (!oflag) {
194 outfile = strtok(NULL, "\r\n");
195 if (outfile == NULL)
196 errx(1, "no filename in input file");
197 }
198
199 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf))
200 errx(1, "%s: filename too long", outfile);
201 if (!sflag && !pflag) {
202 strlcpy(buffn, buf, sizeof(buffn));
203 if (strrchr(buffn, '/') != NULL)
204 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
205 if (buf[0] == '\0') {
206 warnx("%s: illegal filename", buffn);
175 }
176 } while (strncmp(buf, "begin", 5) != 0);
177
178 if (strncmp(buf, "begin-base64", 12) == 0)
179 base64 = 1;
180
181 /* Parse the header: begin{,-base64} mode outfile. */
182 s = strtok(buf, " ");
183 if (s == NULL)
184 errx(1, "no mode or filename in input file");
185 s = strtok(NULL, " ");
186 if (s == NULL)
187 errx(1, "no mode in input file");
188 else {
189 mode = strdup(s);
190 if (mode == NULL)
191 err(1, "strdup()");
192 }
193 if (!oflag) {
194 outfile = strtok(NULL, "\r\n");
195 if (outfile == NULL)
196 errx(1, "no filename in input file");
197 }
198
199 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf))
200 errx(1, "%s: filename too long", outfile);
201 if (!sflag && !pflag) {
202 strlcpy(buffn, buf, sizeof(buffn));
203 if (strrchr(buffn, '/') != NULL)
204 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
205 if (buf[0] == '\0') {
206 warnx("%s: illegal filename", buffn);
207 return(1);
207 return (1);
208 }
209
210 /* handle ~user/file format */
211 if (buf[0] == '~') {
212 if (!(p = index(buf, '/'))) {
213 warnx("%s: illegal ~user", filename);
208 }
209
210 /* handle ~user/file format */
211 if (buf[0] == '~') {
212 if (!(p = index(buf, '/'))) {
213 warnx("%s: illegal ~user", filename);
214 return(1);
214 return (1);
215 }
216 *p++ = '\0';
217 if (!(pw = getpwnam(buf + 1))) {
218 warnx("%s: no user %s", filename, buf);
215 }
216 *p++ = '\0';
217 if (!(pw = getpwnam(buf + 1))) {
218 warnx("%s: no user %s", filename, buf);
219 return(1);
219 return (1);
220 }
221 n = strlen(pw->pw_dir);
222 n1 = strlen(p);
223 if (n + n1 + 2 > MAXPATHLEN) {
224 warnx("%s: path too long", filename);
220 }
221 n = strlen(pw->pw_dir);
222 n1 = strlen(p);
223 if (n + n1 + 2 > MAXPATHLEN) {
224 warnx("%s: path too long", filename);
225 return(1);
225 return (1);
226 }
227 bcopy(p, buf + n + 1, n1 + 1);
228 bcopy(pw->pw_dir, buf, n);
229 buf[n] = '/';
230 }
231 }
232
233 /* create output file, set mode */
234 if (pflag)
235 ; /* print to stdout */
236
237 else {
238 mode_handle = setmode(mode);
239 if (mode_handle == NULL)
240 err(1, "setmode()");
241 if (iflag && !access(buf, F_OK)) {
242 warnx("not overwritten: %s", buf);
226 }
227 bcopy(p, buf + n + 1, n1 + 1);
228 bcopy(pw->pw_dir, buf, n);
229 buf[n] = '/';
230 }
231 }
232
233 /* create output file, set mode */
234 if (pflag)
235 ; /* print to stdout */
236
237 else {
238 mode_handle = setmode(mode);
239 if (mode_handle == NULL)
240 err(1, "setmode()");
241 if (iflag && !access(buf, F_OK)) {
242 warnx("not overwritten: %s", buf);
243 return(0);
243 return (0);
244 } else if (freopen(buf, "w", stdout) == NULL ||
245 stat(buf, &st) < 0 || (S_ISREG(st.st_mode) &&
246 fchmod(fileno(stdout), getmode(mode_handle, 0) & 0666))) {
247 warn("%s: %s", buf, filename);
244 } else if (freopen(buf, "w", stdout) == NULL ||
245 stat(buf, &st) < 0 || (S_ISREG(st.st_mode) &&
246 fchmod(fileno(stdout), getmode(mode_handle, 0) & 0666))) {
247 warn("%s: %s", buf, filename);
248 return(1);
248 return (1);
249 }
250 free(mode_handle);
251 free(mode);
252 }
253 strcpy(buffn, buf); /* store file name from header line */
254
255 /* for each input line */
256next:
257 for (;;) {
258 if (!fgets(p = buf, sizeof(buf), stdin)) {
259 warnx("%s: short file", filename);
249 }
250 free(mode_handle);
251 free(mode);
252 }
253 strcpy(buffn, buf); /* store file name from header line */
254
255 /* for each input line */
256next:
257 for (;;) {
258 if (!fgets(p = buf, sizeof(buf), stdin)) {
259 warnx("%s: short file", filename);
260 return(1);
260 return (1);
261 }
262 if (base64) {
263 if (strncmp(buf, "====", 4) == 0)
264 return (0);
265 base64_decode(buf);
266 goto next;
267 }
268#define DEC(c) (((c) - ' ') & 077) /* single character decode */
269#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
270/* #define IS_DEC(c) (1) */
271
272#define OUT_OF_RANGE \
273{ \
274 warnx( \
275"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
276 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
261 }
262 if (base64) {
263 if (strncmp(buf, "====", 4) == 0)
264 return (0);
265 base64_decode(buf);
266 goto next;
267 }
268#define DEC(c) (((c) - ' ') & 077) /* single character decode */
269#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
270/* #define IS_DEC(c) (1) */
271
272#define OUT_OF_RANGE \
273{ \
274 warnx( \
275"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
276 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
277 return(1); \
277 return (1); \
278}
279
280 /*
281 * `n' is used to avoid writing out all the characters
282 * at the end of the file.
283 */
284 if ((n = DEC(*p)) <= 0)
285 break;
286 for (++p; n > 0; p += 4, n -= 3)
287 if (n >= 3) {
288 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
289 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
290 OUT_OF_RANGE
291
292 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
293 putchar(ch);
294 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
295 putchar(ch);
296 ch = DEC(p[2]) << 6 | DEC(p[3]);
297 putchar(ch);
298 }
299 else {
300 if (n >= 1) {
301 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
302 OUT_OF_RANGE
303 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
304 putchar(ch);
305 }
306 if (n >= 2) {
307 if (!(IS_DEC(*(p + 1)) &&
308 IS_DEC(*(p + 2))))
309 OUT_OF_RANGE
310
311 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
312 putchar(ch);
313 }
314 if (n >= 3) {
315 if (!(IS_DEC(*(p + 2)) &&
316 IS_DEC(*(p + 3))))
317 OUT_OF_RANGE
318 ch = DEC(p[2]) << 6 | DEC(p[3]);
319 putchar(ch);
320 }
321 }
322 }
323 if (fgets(buf, sizeof(buf), stdin) == NULL ||
324 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
325 strcmp(buf, "end\r\n"))) {
326 warnx("%s: no \"end\" line", filename);
278}
279
280 /*
281 * `n' is used to avoid writing out all the characters
282 * at the end of the file.
283 */
284 if ((n = DEC(*p)) <= 0)
285 break;
286 for (++p; n > 0; p += 4, n -= 3)
287 if (n >= 3) {
288 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
289 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
290 OUT_OF_RANGE
291
292 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
293 putchar(ch);
294 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
295 putchar(ch);
296 ch = DEC(p[2]) << 6 | DEC(p[3]);
297 putchar(ch);
298 }
299 else {
300 if (n >= 1) {
301 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
302 OUT_OF_RANGE
303 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
304 putchar(ch);
305 }
306 if (n >= 2) {
307 if (!(IS_DEC(*(p + 1)) &&
308 IS_DEC(*(p + 2))))
309 OUT_OF_RANGE
310
311 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
312 putchar(ch);
313 }
314 if (n >= 3) {
315 if (!(IS_DEC(*(p + 2)) &&
316 IS_DEC(*(p + 3))))
317 OUT_OF_RANGE
318 ch = DEC(p[2]) << 6 | DEC(p[3]);
319 putchar(ch);
320 }
321 }
322 }
323 if (fgets(buf, sizeof(buf), stdin) == NULL ||
324 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
325 strcmp(buf, "end\r\n"))) {
326 warnx("%s: no \"end\" line", filename);
327 return(1);
327 return (1);
328 }
328 }
329 return(0);
329 return (0);
330}
331
332void
333base64_decode(const char *stream)
334{
335 unsigned char out[MAXPATHLEN * 4];
336 int rv;
337
338 if (index(stream, '\r') != NULL)
339 *index(stream, '\r') = '\0';
340 if (index(stream, '\n') != NULL)
341 *index(stream, '\n') = '\0';
342 rv = b64_pton(stream, out, (sizeof(out) / sizeof(out[0])));
343 if (rv == -1)
344 errx(1, "b64_pton: error decoding base64 input stream");
345 fwrite(out, 1, rv, stdout);
346}
347
348static void
349usage(void)
350{
351 (void)fprintf(stderr,
352"usage: uudecode [-cips] [file ...]\n"
353" uudecode [-i] -o output_file [file]\n"
354" b64decode [-cips] [file ...]\n"
355" b64decode [-i] -o output_file [file]\n");
356 exit(1);
357}
330}
331
332void
333base64_decode(const char *stream)
334{
335 unsigned char out[MAXPATHLEN * 4];
336 int rv;
337
338 if (index(stream, '\r') != NULL)
339 *index(stream, '\r') = '\0';
340 if (index(stream, '\n') != NULL)
341 *index(stream, '\n') = '\0';
342 rv = b64_pton(stream, out, (sizeof(out) / sizeof(out[0])));
343 if (rv == -1)
344 errx(1, "b64_pton: error decoding base64 input stream");
345 fwrite(out, 1, rv, stdout);
346}
347
348static void
349usage(void)
350{
351 (void)fprintf(stderr,
352"usage: uudecode [-cips] [file ...]\n"
353" uudecode [-i] -o output_file [file]\n"
354" b64decode [-cips] [file ...]\n"
355" b64decode [-i] -o output_file [file]\n");
356 exit(1);
357}