mandoc: update to 1.14.5
[unleashed.git] / bin / uudecode / uudecode.c
blobe2f4c1e4bc9ca6047e6ec18d6e0b8da86af21831
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 * uudecode [file ...]
35 * create the specified file, decoding as you go.
36 * used with uuencode.
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
42 #include <netinet/in.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <libgen.h>
49 #include <pwd.h>
50 #include <resolv.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
56 static const char *infile, *outfile;
57 static FILE *infp, *outfp;
58 static int base64, cflag, iflag, oflag, pflag, rflag, sflag;
60 static void usage(void);
61 static int decode(void);
62 static int decode2(void);
63 static int uu_decode(void);
64 static int base64_decode(void);
66 int
67 main(int argc, char *argv[])
69 int rval, ch;
71 if (strcmp(basename(argv[0]), "b64decode") == 0)
72 base64 = 1;
74 while ((ch = getopt(argc, argv, "cimo:prs")) != -1) {
75 switch (ch) {
76 case 'c':
77 if (oflag || rflag)
78 usage();
79 cflag = 1; /* multiple uudecode'd files */
80 break;
81 case 'i':
82 iflag = 1; /* ask before override files */
83 break;
84 case 'm':
85 base64 = 1;
86 break;
87 case 'o':
88 if (cflag || pflag || rflag || sflag)
89 usage();
90 oflag = 1; /* output to the specified file */
91 sflag = 1; /* do not strip pathnames for output */
92 outfile = optarg; /* set the output filename */
93 break;
94 case 'p':
95 if (oflag)
96 usage();
97 pflag = 1; /* print output to stdout */
98 break;
99 case 'r':
100 if (cflag || oflag)
101 usage();
102 rflag = 1; /* decode raw data */
103 break;
104 case 's':
105 if (oflag)
106 usage();
107 sflag = 1; /* do not strip pathnames for output */
108 break;
109 default:
110 usage();
113 argc -= optind;
114 argv += optind;
116 if (*argv != NULL) {
117 rval = 0;
118 do {
119 infp = fopen(infile = *argv, "r");
120 if (infp == NULL) {
121 warn("%s", *argv);
122 rval = 1;
123 continue;
125 rval |= decode();
126 fclose(infp);
127 } while (*++argv);
128 } else {
129 infile = "stdin";
130 infp = stdin;
131 rval = decode();
133 exit(rval);
136 static int
137 decode(void)
139 int r, v;
141 if (rflag) {
142 /* relaxed alternative to decode2() */
143 outfile = "/dev/stdout";
144 outfp = stdout;
145 if (base64)
146 return (base64_decode());
147 else
148 return (uu_decode());
150 v = decode2();
151 if (v == EOF) {
152 warnx("%s: missing or bad \"begin\" line", infile);
153 return (1);
155 for (r = v; cflag; r |= v) {
156 v = decode2();
157 if (v == EOF)
158 break;
160 return (r);
163 static int
164 decode2(void)
166 int flags, fd, mode;
167 size_t n, m;
168 char *p, *q;
169 void *handle;
170 struct passwd *pw;
171 struct stat st;
172 char buf[MAXPATHLEN + 1];
174 base64 = 0;
175 /* search for header line */
176 for (;;) {
177 if (fgets(buf, sizeof(buf), infp) == NULL)
178 return (EOF);
179 p = buf;
180 if (strncmp(p, "begin-base64 ", 13) == 0) {
181 base64 = 1;
182 p += 13;
183 } else if (strncmp(p, "begin ", 6) == 0)
184 p += 6;
185 else
186 continue;
187 /* p points to mode */
188 q = strchr(p, ' ');
189 if (q == NULL)
190 continue;
191 *q++ = '\0';
192 /* q points to filename */
193 n = strlen(q);
194 while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r'))
195 q[--n] = '\0';
196 /* found valid header? */
197 if (n > 0)
198 break;
201 handle = setmode(p);
202 if (handle == NULL) {
203 warnx("%s: unable to parse file mode", infile);
204 return (1);
206 mode = getmode(handle, 0) & 0666;
207 free(handle);
209 if (sflag) {
210 /* don't strip, so try ~user/file expansion */
211 p = NULL;
212 pw = NULL;
213 if (*q == '~')
214 p = strchr(q, '/');
215 if (p != NULL) {
216 *p = '\0';
217 pw = getpwnam(q + 1);
218 *p = '/';
220 if (pw != NULL) {
221 n = strlen(pw->pw_dir);
222 if (buf + n > p) {
223 /* make room */
224 m = strlen(p);
225 if (sizeof(buf) < n + m) {
226 warnx("%s: bad output filename",
227 infile);
228 return (1);
230 p = memmove(buf + n, p, m);
232 q = memcpy(p - n, pw->pw_dir, n);
234 } else {
235 /* strip down to leaf name */
236 p = strrchr(q, '/');
237 if (p != NULL)
238 q = p + 1;
240 if (!oflag)
241 outfile = q;
243 /* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */
244 if (pflag || strcmp(outfile, "/dev/stdout") == 0)
245 outfp = stdout;
246 else {
247 flags = O_WRONLY | O_CREAT | O_EXCL;
248 if (lstat(outfile, &st) == 0) {
249 if (iflag) {
250 warnc(EEXIST, "%s: %s", infile, outfile);
251 return (0);
253 switch (st.st_mode & S_IFMT) {
254 case S_IFREG:
255 case S_IFLNK:
256 /* avoid symlink attacks */
257 if (unlink(outfile) == 0 || errno == ENOENT)
258 break;
259 warn("%s: unlink %s", infile, outfile);
260 return (1);
261 case S_IFDIR:
262 warnc(EISDIR, "%s: %s", infile, outfile);
263 return (1);
264 default:
265 if (oflag) {
266 /* trust command-line names */
267 flags &= ~O_EXCL;
268 break;
270 warnc(EEXIST, "%s: %s", infile, outfile);
271 return (1);
273 } else if (errno != ENOENT) {
274 warn("%s: %s", infile, outfile);
275 return (1);
277 if ((fd = open(outfile, flags, mode)) < 0 ||
278 (outfp = fdopen(fd, "w")) == NULL) {
279 warn("%s: %s", infile, outfile);
280 return (1);
284 if (base64)
285 return (base64_decode());
286 else
287 return (uu_decode());
290 static int
291 get_line(char *buf, size_t size)
294 if (fgets(buf, size, infp) != NULL)
295 return (2);
296 if (rflag)
297 return (0);
298 warnx("%s: %s: short file", infile, outfile);
299 return (1);
302 static int
303 checkend(const char *ptr, const char *end, const char *msg)
305 size_t n;
307 n = strlen(end);
308 if (strncmp(ptr, end, n) != 0 ||
309 strspn(ptr + n, " \t\r\n") != strlen(ptr + n)) {
310 warnx("%s: %s: %s", infile, outfile, msg);
311 return (1);
313 if (fclose(outfp) != 0) {
314 warn("%s: %s", infile, outfile);
315 return (1);
317 return (0);
320 static int
321 uu_decode(void)
323 int i, ch;
324 char *p;
325 char buf[MAXPATHLEN+1];
327 /* for each input line */
328 for (;;) {
329 switch (get_line(buf, sizeof(buf))) {
330 case 0:
331 return (0);
332 case 1:
333 return (1);
336 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
337 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
339 #define OUT_OF_RANGE do { \
340 warnx("%s: %s: character out of range: [%d-%d]", \
341 infile, outfile, 1 + ' ', 077 + ' ' + 1); \
342 return (1); \
343 } while (0)
346 * `i' is used to avoid writing out all the characters
347 * at the end of the file.
349 p = buf;
350 if ((i = DEC(*p)) <= 0)
351 break;
352 for (++p; i > 0; p += 4, i -= 3)
353 if (i >= 3) {
354 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
355 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
356 OUT_OF_RANGE;
358 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
359 putc(ch, outfp);
360 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
361 putc(ch, outfp);
362 ch = DEC(p[2]) << 6 | DEC(p[3]);
363 putc(ch, outfp);
364 } else {
365 if (i >= 1) {
366 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
367 OUT_OF_RANGE;
368 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
369 putc(ch, outfp);
371 if (i >= 2) {
372 if (!(IS_DEC(*(p + 1)) &&
373 IS_DEC(*(p + 2))))
374 OUT_OF_RANGE;
376 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
377 putc(ch, outfp);
379 if (i >= 3) {
380 if (!(IS_DEC(*(p + 2)) &&
381 IS_DEC(*(p + 3))))
382 OUT_OF_RANGE;
383 ch = DEC(p[2]) << 6 | DEC(p[3]);
384 putc(ch, outfp);
388 switch (get_line(buf, sizeof(buf))) {
389 case 0:
390 return (0);
391 case 1:
392 return (1);
393 default:
394 return (checkend(buf, "end", "no \"end\" line"));
398 static int
399 base64_decode(void)
401 int n, count, count4;
402 char inbuf[MAXPATHLEN + 1], *p;
403 unsigned char outbuf[MAXPATHLEN * 4];
404 char leftover[MAXPATHLEN + 1];
406 leftover[0] = '\0';
407 for (;;) {
408 strcpy(inbuf, leftover);
409 switch (get_line(inbuf + strlen(inbuf),
410 sizeof(inbuf) - strlen(inbuf))) {
411 case 0:
412 return (0);
413 case 1:
414 return (1);
417 count = 0;
418 count4 = -1;
419 p = inbuf;
420 while (*p != '\0') {
422 * Base64 encoded strings have the following
423 * characters in them: A-Z, a-z, 0-9 and +, / and =
425 if (isalnum(*p) || *p == '+' || *p == '/' || *p == '=')
426 count++;
427 if (count % 4 == 0)
428 count4 = p - inbuf;
429 p++;
432 strcpy(leftover, inbuf + count4 + 1);
433 inbuf[count4 + 1] = 0;
435 n = b64_pton(inbuf, outbuf, sizeof(outbuf));
437 if (n < 0)
438 break;
439 fwrite(outbuf, 1, n, outfp);
441 return (checkend(inbuf, "====", "error decoding base64 input stream"));
444 static void
445 usage(void)
448 (void)fprintf(stderr,
449 "usage: uudecode [-cimprs] [file ...]\n"
450 " uudecode [-i] -o output_file [file]\n"
451 " b64decode [-cimprs] [file ...]\n"
452 " b64decode [-i] -o output_file [file]\n");
453 exit(1);