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
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
49 * create the specified file, decoding as you go.
52 #include <sys/param.h>
53 #include <sys/socket.h>
56 #include <netinet/in.h>
70 static const char *infile, *outfile;
71 static FILE *infp, *outfp;
72 static int base64, cflag, iflag, oflag, pflag, rflag, sflag;
74 static void usage(void);
75 static int decode(void);
76 static int decode2(void);
77 static int uu_decode(void);
78 static int base64_decode(void);
81 main(int argc, char *argv[])
85 if (strcmp(basename(argv[0]), "b64decode") == 0)
88 while ((ch = getopt(argc, argv, "cimo:prs")) != -1) {
93 cflag = 1; /* multiple uudecode'd files */
96 iflag = 1; /* ask before override files */
102 if (cflag || pflag || rflag || sflag)
104 oflag = 1; /* output to the specified file */
105 sflag = 1; /* do not strip pathnames for output */
106 outfile = optarg; /* set the output filename */
111 pflag = 1; /* print output to stdout */
116 rflag = 1; /* decode raw data */
121 sflag = 1; /* do not strip pathnames for output */
133 infp = fopen(infile = *argv, "r");
156 /* relaxed alternative to decode2() */
157 outfile = "/dev/stdout";
160 return (base64_decode());
162 return (uu_decode());
166 warnx("%s: missing or bad \"begin\" line", infile);
169 for (r = v; cflag; r |= v) {
186 char buf[MAXPATHLEN + 1];
189 /* search for header line */
191 if (fgets(buf, sizeof(buf), infp) == NULL)
194 if (strncmp(p, "begin-base64 ", 13) == 0) {
197 } else if (strncmp(p, "begin ", 6) == 0)
201 /* p points to mode */
206 /* q points to filename */
208 while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r'))
210 /* found valid header? */
216 if (handle == NULL) {
217 warnx("%s: unable to parse file mode", infile);
220 mode = getmode(handle, 0) & 0666;
224 /* don't strip, so try ~user/file expansion */
231 pw = getpwnam(q + 1);
235 n = strlen(pw->pw_dir);
239 if (sizeof(buf) < n + m) {
240 warnx("%s: bad output filename",
244 p = memmove(buf + n, p, m);
246 q = memcpy(p - n, pw->pw_dir, n);
249 /* strip down to leaf name */
257 /* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */
258 if (pflag || strcmp(outfile, "/dev/stdout") == 0)
261 flags = O_WRONLY | O_CREAT | O_EXCL;
262 if (lstat(outfile, &st) == 0) {
264 warnc(EEXIST, "%s: %s", infile, outfile);
267 switch (st.st_mode & S_IFMT) {
270 /* avoid symlink attacks */
271 if (unlink(outfile) == 0 || errno == ENOENT)
273 warn("%s: unlink %s", infile, outfile);
276 warnc(EISDIR, "%s: %s", infile, outfile);
280 /* trust command-line names */
284 warnc(EEXIST, "%s: %s", infile, outfile);
287 } else if (errno != ENOENT) {
288 warn("%s: %s", infile, outfile);
291 if ((fd = open(outfile, flags, mode)) < 0 ||
292 (outfp = fdopen(fd, "w")) == NULL) {
293 warn("%s: %s", infile, outfile);
299 return (base64_decode());
301 return (uu_decode());
305 get_line(char *buf, size_t size)
308 if (fgets(buf, size, infp) != NULL)
312 warnx("%s: %s: short file", infile, outfile);
317 checkend(const char *ptr, const char *end, const char *msg)
322 if (strncmp(ptr, end, n) != 0 ||
323 strspn(ptr + n, " \t\r\n") != strlen(ptr + n)) {
324 warnx("%s: %s: %s", infile, outfile, msg);
327 if (fclose(outfp) != 0) {
328 warn("%s: %s", infile, outfile);
339 char buf[MAXPATHLEN+1];
341 /* for each input line */
343 switch (get_line(buf, sizeof(buf))) {
350 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
351 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
353 #define OUT_OF_RANGE do { \
354 warnx("%s: %s: character out of range: [%d-%d]", \
355 infile, outfile, 1 + ' ', 077 + ' ' + 1); \
360 * `i' is used to avoid writing out all the characters
361 * at the end of the file.
364 if ((i = DEC(*p)) <= 0)
366 for (++p; i > 0; p += 4, i -= 3)
368 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
369 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
372 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
374 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
376 ch = DEC(p[2]) << 6 | DEC(p[3]);
380 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
382 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
386 if (!(IS_DEC(*(p + 1)) &&
390 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
394 if (!(IS_DEC(*(p + 2)) &&
397 ch = DEC(p[2]) << 6 | DEC(p[3]);
402 switch (get_line(buf, sizeof(buf))) {
408 return (checkend(buf, "end", "no \"end\" line"));
415 int n, count, count4;
416 char inbuf[MAXPATHLEN + 1], *p;
417 unsigned char outbuf[MAXPATHLEN * 4];
418 char leftover[MAXPATHLEN + 1];
422 strcpy(inbuf, leftover);
423 switch (get_line(inbuf + strlen(inbuf),
424 sizeof(inbuf) - strlen(inbuf))) {
436 * Base64 encoded strings have the following
437 * characters in them: A-Z, a-z, 0-9 and +, / and =
439 if (isalnum(*p) || *p == '+' || *p == '/' || *p == '=')
446 strcpy(leftover, inbuf + count4 + 1);
447 inbuf[count4 + 1] = 0;
449 n = b64_pton(inbuf, outbuf, sizeof(outbuf));
453 fwrite(outbuf, 1, n, outfp);
455 return (checkend(inbuf, "====", "error decoding base64 input stream"));
462 (void)fprintf(stderr,
463 "usage: uudecode [-cimprs] [file ...]\n"
464 " uudecode [-i] -o output_file [file]\n"
465 " b64decode [-cimprs] [file ...]\n"
466 " b64decode [-i] -o output_file [file]\n");