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";
44 static const char rcsid[] =
51 * create the specified file, decoding as you go.
54 #include <sys/param.h>
55 #include <sys/socket.h>
58 #include <netinet/in.h>
70 int cflag, iflag, oflag, pflag, sflag;
72 static void usage(void);
75 void base64_decode(const char *);
84 while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
89 cflag = 1; /* multiple uudecode'd files */
92 iflag = 1; /* ask before override files */
95 if (cflag || pflag || sflag)
97 oflag = 1; /* output to the specified file */
98 sflag = 1; /* do not strip pathnames for output */
99 outfile = optarg; /* set the output filename */
104 pflag = 1; /* print output to stdout */
109 sflag = 1; /* do not strip pathnames for output */
122 if (!freopen(filename = *argv, "r", stdin)) {
141 /* decode only one file per input stream */
145 /* multiple uudecode'd files */
146 for (flag = 0; ; flag++)
149 else if (feof(stdin))
161 register char ch, *p;
162 int base64, ignore, n1;
163 char buf[MAXPATHLEN+1];
164 char buffn[MAXPATHLEN+1]; /* file name buffer */
169 /* search for header line */
171 if (!fgets(buf, sizeof(buf), stdin)) {
172 if (flag) /* no error */
175 warnx("%s: no \"begin\" line", filename);
178 } while (strncmp(buf, "begin", 5) != 0);
180 if (strncmp(buf, "begin-base64", 12) == 0)
183 /* Parse the header: begin{,-base64} mode outfile. */
184 s = strtok(buf, " ");
186 errx(1, "no mode or filename in input file");
187 s = strtok(NULL, " ");
189 errx(1, "no mode in input file");
196 outfile = strtok(NULL, "\r\n");
198 errx(1, "no filename in input file");
201 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf))
202 errx(1, "%s: filename too long", outfile);
203 if (!sflag && !pflag) {
204 strlcpy(buffn, buf, sizeof(buffn));
205 if (strrchr(buffn, '/') != NULL)
206 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
207 if (buf[0] == '\0') {
208 warnx("%s: illegal filename", buffn);
212 /* handle ~user/file format */
214 if (!(p = index(buf, '/'))) {
215 warnx("%s: illegal ~user", filename);
219 if (!(pw = getpwnam(buf + 1))) {
220 warnx("%s: no user %s", filename, buf);
223 n = strlen(pw->pw_dir);
225 if (n + n1 + 2 > MAXPATHLEN) {
226 warnx("%s: path too long", filename);
229 bcopy(p, buf + n + 1, n1 + 1);
230 bcopy(pw->pw_dir, buf, n);
235 /* create output file, set mode */
237 ; /* print to stdout */
240 mode_handle = setmode(mode);
241 if (mode_handle == NULL)
243 if (iflag && !access(buf, F_OK)) {
244 (void)fprintf(stderr, "not overwritten: %s\n", buf);
246 } else if (!freopen(buf, "w", stdout) ||
247 fchmod(fileno(stdout), getmode(mode_handle, 0) & 0666)) {
248 warn("%s: %s", buf, filename);
254 strcpy(buffn, buf); /* store file name from header line */
256 /* for each input line */
259 if (!fgets(p = buf, sizeof(buf), stdin)) {
260 warnx("%s: short file", filename);
264 if (strncmp(buf, "====", 4) == 0)
269 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
270 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
271 /* #define IS_DEC(c) (1) */
273 #define OUT_OF_RANGE \
276 "\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
277 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
286 * `n' is used to avoid writing out all the characters
287 * at the end of the file.
289 if ((n = DEC(*p)) <= 0)
291 for (++p; n > 0; p += 4, n -= 3)
293 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
294 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
297 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
299 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
301 ch = DEC(p[2]) << 6 | DEC(p[3]);
307 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
309 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
313 if (!(IS_DEC(*(p + 1)) &&
317 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
321 if (!(IS_DEC(*(p + 2)) &&
324 ch = DEC(p[2]) << 6 | DEC(p[3]);
329 if (fgets(buf, sizeof(buf), stdin) == NULL ||
330 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
331 strcmp(buf, "end\r\n"))) {
332 warnx("%s: no \"end\" line", filename);
339 base64_decode(stream)
342 unsigned char out[MAXPATHLEN * 4];
345 if (index(stream, '\r') != NULL)
346 *index(stream, '\r') = '\0';
347 if (index(stream, '\n') != NULL)
348 *index(stream, '\n') = '\0';
349 rv = b64_pton(stream, out, (sizeof(out) / sizeof(out[0])));
351 errx(1, "b64_pton: error decoding base64 input stream");
352 fwrite(out, 1, rv, stdout);
358 (void)fprintf(stderr, "usage: uudecode [-cips] [file ...]\n");
359 (void)fprintf(stderr, "usage: uudecode [-i] -o output_file [file]\n");