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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)uudecode.c 8.2 (Berkeley) 4/2/94
31 * $FreeBSD: src/usr.bin/uudecode/uudecode.c,v 1.13.2.6 2003/04/07 20:10:33 fanf Exp $
32 * $DragonFly: src/usr.bin/uudecode/uudecode.c,v 1.3 2004/12/22 11:25:51 liamfoy Exp $
38 * create the specified file, decoding as you go.
41 #include <sys/param.h>
42 #include <sys/socket.h>
45 #include <netinet/in.h>
58 static const char *infile
, *outfile
;
59 static FILE *infp
, *outfp
;
60 static int base64
, cflag
, iflag
, oflag
, pflag
, rflag
, sflag
;
62 static void usage(void);
63 static int decode(void);
64 static int decode2(void);
65 static int uu_decode(void);
66 static int base64_decode(void);
69 main(int argc
, char *argv
[])
73 if (strcmp(basename(argv
[0]), "b64decode") == 0)
76 while ((ch
= getopt(argc
, argv
, "cimo:prs")) != -1) {
81 cflag
= 1; /* multiple uudecode'd files */
84 iflag
= 1; /* ask before override files */
90 if (cflag
|| pflag
|| rflag
|| sflag
)
92 oflag
= 1; /* output to the specified file */
93 sflag
= 1; /* do not strip pathnames for output */
94 outfile
= optarg
; /* set the output filename */
99 pflag
= 1; /* print output to stdout */
104 rflag
= 1; /* decode raw data */
109 sflag
= 1; /* do not strip pathnames for output */
121 infp
= fopen(infile
= *argv
, "r");
144 /* relaxed alternative to decode2() */
145 outfile
= "/dev/stdout";
148 return (base64_decode());
150 return (uu_decode());
154 warnx("%s: missing or bad \"begin\" line", infile
);
157 for (r
= v
; cflag
; r
|= v
) {
175 char buf
[MAXPATHLEN
+1];
178 /* search for header line */
180 if (fgets(buf
, sizeof(buf
), infp
) == NULL
)
183 if (strncmp(p
, "begin-base64 ", 13) == 0) {
186 } else if (strncmp(p
, "begin ", 6) == 0)
190 /* p points to mode */
195 /* q points to filename */
197 while (n
> 0 && (q
[n
-1] == '\n' || q
[n
-1] == '\r'))
199 /* found valid header? */
205 if ((handle
= setmode(p
)) == NULL
) {
207 warnx("invalid file mode: %s", infile
);
209 warn("setmode malloc failed: %s", infile
);
214 mode
= getmode(handle
, 0) & 0666;
218 /* don't strip, so try ~user/file expansion */
225 pw
= getpwnam(q
+ 1);
229 n
= strlen(pw
->pw_dir
);
233 if (sizeof(buf
) < n
+ m
) {
234 warnx("%s: bad output filename",
238 p
= memmove(buf
+ n
, p
, m
);
240 q
= memcpy(p
- n
, pw
->pw_dir
, n
);
243 /* strip down to leaf name */
251 /* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */
252 if (pflag
|| strcmp(outfile
, "/dev/stdout") == 0)
255 flags
= O_WRONLY
|O_CREAT
|O_EXCL
;
256 if (lstat(outfile
, &st
) == 0) {
258 warnc(EEXIST
, "%s: %s", infile
, outfile
);
261 switch (st
.st_mode
& S_IFMT
) {
264 /* avoid symlink attacks */
265 if (unlink(outfile
) == 0 || errno
== ENOENT
)
267 warn("%s: unlink %s", infile
, outfile
);
270 warnc(EISDIR
, "%s: %s", infile
, outfile
);
274 /* trust command-line names */
278 warnc(EEXIST
, "%s: %s", infile
, outfile
);
281 } else if (errno
!= ENOENT
) {
282 warn("%s: %s", infile
, outfile
);
285 if ((fd
= open(outfile
, flags
, mode
)) < 0 ||
286 (outfp
= fdopen(fd
, "w")) == NULL
) {
287 warn("%s: %s", infile
, outfile
);
293 return (base64_decode());
295 return (uu_decode());
299 getline(char *buf
, size_t size
)
301 if (fgets(buf
, size
, infp
) != NULL
)
305 warnx("%s: %s: short file", infile
, outfile
);
310 checkend(const char *ptr
, const char *end
, const char *msg
)
315 if (strncmp(ptr
, end
, n
) != 0 ||
316 strspn(ptr
+ n
, " \t\r\n") != strlen(ptr
+ n
)) {
317 warnx("%s: %s: %s", infile
, outfile
, msg
);
320 if (fclose(outfp
) != 0) {
321 warn("%s: %s", infile
, outfile
);
332 char buf
[MAXPATHLEN
+1];
334 /* for each input line */
336 switch (getline(buf
, sizeof(buf
))) {
341 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
342 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
344 #define OUT_OF_RANGE do { \
345 warnx("%s: %s: character out of range: [%d-%d]", \
346 infile, outfile, 1 + ' ', 077 + ' ' + 1); \
351 * `i' is used to avoid writing out all the characters
352 * at the end of the file.
355 if ((i
= DEC(*p
)) <= 0)
357 for (++p
; i
> 0; p
+= 4, i
-= 3)
359 if (!(IS_DEC(*p
) && IS_DEC(*(p
+ 1)) &&
360 IS_DEC(*(p
+ 2)) && IS_DEC(*(p
+ 3))))
363 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
365 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
367 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
372 if (!(IS_DEC(*p
) && IS_DEC(*(p
+ 1))))
374 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
378 if (!(IS_DEC(*(p
+ 1)) &&
382 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
386 if (!(IS_DEC(*(p
+ 2)) &&
389 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
394 switch (getline(buf
, sizeof(buf
))) {
397 default: return (checkend(buf
, "end", "no \"end\" line"));
405 char inbuf
[MAXPATHLEN
+1];
406 unsigned char outbuf
[MAXPATHLEN
* 4];
409 switch (getline(inbuf
, sizeof(inbuf
))) {
413 n
= b64_pton(inbuf
, outbuf
, sizeof(outbuf
));
416 fwrite(outbuf
, 1, n
, outfp
);
418 return (checkend(inbuf
, "====",
419 "error decoding base64 input stream"));
425 (void)fprintf(stderr
,
426 "usage: uudecode [-cimprs] [file ...]\n"
427 " uudecode [-i] -o output_file [file]\n"
428 " b64decode [-cimprs] [file ...]\n"
429 " b64decode [-i] -o output_file [file]\n");