2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
52 * create the specified file, decoding as you go.
55 #include <sys/param.h>
56 #include <sys/socket.h>
59 #include <netinet/in.h>
71 int cflag, iflag, oflag, pflag, sflag;
73 static void usage(void);
76 void base64_decode(const char *);
79 main(int argc, char *argv[])
83 while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
88 cflag = 1; /* multiple uudecode'd files */
91 iflag = 1; /* ask before override files */
94 if (cflag || pflag || sflag)
96 oflag = 1; /* output to the specified file */
97 sflag = 1; /* do not strip pathnames for output */
98 outfile = optarg; /* set the output filename */
103 pflag = 1; /* print output to stdout */
108 sflag = 1; /* do not strip pathnames for output */
121 if (!freopen(filename = *argv, "r", stdin)) {
140 /* decode only one file per input stream */
144 /* multiple uudecode'd files */
145 for (flag = 0; ; flag++)
148 else if (feof(stdin))
159 register char ch, *p;
160 int base64, ignore, n1;
161 char buf[MAXPATHLEN+1];
162 char buffn[MAXPATHLEN+1]; /* file name buffer */
167 /* search for header line */
169 if (!fgets(buf, sizeof(buf), stdin)) {
170 if (flag) /* no error */
173 warnx("%s: no \"begin\" line", filename);
176 } while (strncmp(buf, "begin", 5) != 0);
178 if (strncmp(buf, "begin-base64", 12) == 0)
181 /* Parse the header: begin{,-base64} mode outfile. */
182 s = strtok(buf, " ");
184 errx(1, "no mode or filename in input file");
185 s = strtok(NULL, " ");
187 errx(1, "no mode in input file");
194 outfile = strtok(NULL, "\r\n");
196 errx(1, "no filename in input file");
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);
210 /* handle ~user/file format */
212 if (!(p = index(buf, '/'))) {
213 warnx("%s: illegal ~user", filename);
217 if (!(pw = getpwnam(buf + 1))) {
218 warnx("%s: no user %s", filename, buf);
221 n = strlen(pw->pw_dir);
223 if (n + n1 + 2 > MAXPATHLEN) {
224 warnx("%s: path too long", filename);
227 bcopy(p, buf + n + 1, n1 + 1);
228 bcopy(pw->pw_dir, buf, n);
233 /* create output file, set mode */
235 ; /* print to stdout */
238 mode_handle = setmode(mode);
239 if (mode_handle == NULL)
241 if (iflag && !access(buf, F_OK)) {
242 (void)fprintf(stderr, "not overwritten: %s\n", buf);
244 } else if (!freopen(buf, "w", stdout) ||
245 fchmod(fileno(stdout), getmode(mode_handle, 0) & 0666)) {
246 warn("%s: %s", buf, filename);
252 strcpy(buffn, buf); /* store file name from header line */
254 /* for each input line */
257 if (!fgets(p = buf, sizeof(buf), stdin)) {
258 warnx("%s: short file", filename);
262 if (strncmp(buf, "====", 4) == 0)
267 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
268 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
269 /* #define IS_DEC(c) (1) */
271 #define OUT_OF_RANGE \
274 "\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
275 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
284 * `n' is used to avoid writing out all the characters
285 * at the end of the file.
287 if ((n = DEC(*p)) <= 0)
289 for (++p; n > 0; p += 4, n -= 3)
291 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
292 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
295 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
297 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
299 ch = DEC(p[2]) << 6 | DEC(p[3]);
305 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
307 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
311 if (!(IS_DEC(*(p + 1)) &&
315 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
319 if (!(IS_DEC(*(p + 2)) &&
322 ch = DEC(p[2]) << 6 | DEC(p[3]);
327 if (fgets(buf, sizeof(buf), stdin) == NULL ||
328 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
329 strcmp(buf, "end\r\n"))) {
330 warnx("%s: no \"end\" line", filename);
337 base64_decode(const char *stream)
339 unsigned char out[MAXPATHLEN * 4];
342 if (index(stream, '\r') != NULL)
343 *index(stream, '\r') = '\0';
344 if (index(stream, '\n') != NULL)
345 *index(stream, '\n') = '\0';
346 rv = b64_pton(stream, out, (sizeof(out) / sizeof(out[0])));
348 errx(1, "b64_pton: error decoding base64 input stream");
349 fwrite(out, 1, rv, stdout);
355 (void)fprintf(stderr,
356 "usage: uudecode [-cips] [file ...]\n"
357 " uudecode [-i] -o output_file [file]\n"
358 " b64decode [-cips] [file ...]\n"
359 " b64decode [-i] -o output_file [file]\n");