]>
git.cameronkatri.com Git - apple_cmds.git/blob - basic_cmds/uudecode/uudecode.c
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
36 static const char copyright
[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: head/usr.bin/uudecode/uudecode.c 214010 2010-10-18 05:44:11Z edwin $");
51 * create the specified file, decoding as you go.
54 #include <sys/param.h>
55 #include <sys/socket.h>
58 #include <netinet/in.h>
72 static const char *infile
, *outfile
;
73 static FILE *infp
, *outfp
;
74 static int base64
, cflag
, iflag
, oflag
, pflag
, rflag
, sflag
;
76 static void usage(void);
77 static int decode(void);
78 static int decode2(void);
79 static int uu_decode(void);
80 static int base64_decode(void);
83 main(int argc
, char *argv
[])
87 if (strcmp(basename(argv
[0]), "b64decode") == 0)
90 while ((ch
= getopt(argc
, argv
, "cimo:prs")) != -1) {
95 cflag
= 1; /* multiple uudecode'd files */
98 iflag
= 1; /* ask before override files */
104 if (cflag
|| pflag
|| rflag
|| sflag
)
106 oflag
= 1; /* output to the specified file */
107 sflag
= 1; /* do not strip pathnames for output */
108 outfile
= optarg
; /* set the output filename */
113 pflag
= 1; /* print output to stdout */
118 rflag
= 1; /* decode raw data */
123 sflag
= 1; /* do not strip pathnames for output */
135 infp
= fopen(infile
= *argv
, "r");
158 /* relaxed alternative to decode2() */
159 outfile
= "/dev/stdout";
162 return (base64_decode());
164 return (uu_decode());
168 warnx("%s: missing or bad \"begin\" line", infile
);
171 for (r
= v
; cflag
; r
|= v
) {
184 char *p
, *q
= NULL
, *orig_outfile
= NULL
;
188 char buf
[MAXPATHLEN
+ 1];
191 /* search for header line */
193 if (fgets(buf
, sizeof(buf
), infp
) == NULL
)
196 if (strncmp(p
, "begin-base64 ", 13) == 0) {
199 } else if (strncmp(p
, "begin ", 6) == 0)
203 /* p points to mode */
208 /* q points to filename */
210 while (n
> 0 && (q
[n
-1] == '\n' || q
[n
-1] == '\r'))
212 /* found valid header? */
218 if (handle
== NULL
) {
219 warnx("%s: unable to parse file mode", infile
);
222 mode
= getmode(handle
, 0);
226 /* don't strip, so try ~user/file expansion */
233 pw
= getpwnam(q
+ 1);
237 n
= strlen(pw
->pw_dir
);
241 if (sizeof(buf
) < n
+ m
) {
242 warnx("%s: bad output filename",
246 p
= memmove(buf
+ n
, p
, m
);
248 q
= memcpy(p
- n
, pw
->pw_dir
, n
);
251 /* strip down to leaf name */
257 outfile
= q
? strdup(q
) : q
;
260 /* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */
261 if (pflag
|| (strcmp(oflag
? outfile
: orig_outfile
, "/dev/stdout") == 0)) {
264 char pwd
[MAXPATHLEN
] = "";
265 char targetpath
[MAXPATHLEN
];
266 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
267 if (lstat(outfile
, &st
) == 0) {
268 if (iflag
&& !S_ISFIFO(st
.st_mode
)) {
269 warnc(EEXIST
, "%s: %s", infile
, outfile
);
274 switch (st
.st_mode
& S_IFMT
) {
276 flags
|= O_NOFOLLOW
| O_TRUNC
;
279 if (NULL
== realpath(".", pwd
)) {
280 warn("Unable to get realpath for .");
283 flags
|= O_CREAT
; // for dangling symlinks
286 warnc(EISDIR
, "%s: %s", infile
, outfile
);
293 /* trust command-line names */
297 warnc(EEXIST
, "%s: %s", infile
, outfile
);
300 } else if (errno
!= ENOENT
) {
301 warn("%s: %s", infile
, outfile
);
304 fd
= open(outfile
, flags
, mode
);
306 warn("%s: %s", infile
, outfile
);
310 if (-1 != fcntl(fd
, F_GETPATH
, targetpath
)) {
311 if (strstr(targetpath
, pwd
) != targetpath
) {
312 warnx("Target path %s is not based at %s", targetpath
, pwd
);
319 warn("Unable to get path for target: %s", outfile
);
323 if ((outfp
= fdopen(fd
, "w")) == NULL
) {
324 warn("%s: %s", infile
, outfile
);
328 if (fchmod(fileno(outfp
), mode
) && (EPERM
!= errno
)) {
329 warn("%s: %s", infile
, outfile
);
335 return (base64_decode());
337 return (uu_decode());
341 uugetline(char *buf
, size_t size
)
344 if (fgets(buf
, size
, infp
) != NULL
)
348 warnx("%s: %s: short file", infile
, outfile
);
353 checkend(const char *ptr
, const char *end
, const char *msg
)
358 if (strncmp(ptr
, end
, n
) != 0 ||
359 strspn(ptr
+ n
, " \t\r\n") != strlen(ptr
+ n
)) {
360 warnx("%s: %s: %s", infile
, outfile
, msg
);
363 if (fclose(outfp
) != 0) {
364 warn("%s: %s", infile
, outfile
);
375 char buf
[MAXPATHLEN
+1];
377 /* for each input line */
379 switch (uugetline(buf
, sizeof(buf
))) {
386 #define DEC(c) (((c) - ' ') & 077) /* single character decode */
387 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
389 #define OUT_OF_RANGE do { \
390 warnx("%s: %s: character out of range: [%d-%d]", \
391 infile, outfile, 1 + ' ', 077 + ' ' + 1); \
396 * `i' is used to avoid writing out all the characters
397 * at the end of the file.
400 if ((i
= DEC(*p
)) <= 0)
402 for (++p
; i
> 0; p
+= 4, i
-= 3)
404 if (!(IS_DEC(*p
) && IS_DEC(*(p
+ 1)) &&
405 IS_DEC(*(p
+ 2)) && IS_DEC(*(p
+ 3))))
408 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
410 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
412 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
416 if (!(IS_DEC(*p
) && IS_DEC(*(p
+ 1))))
418 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
422 if (!(IS_DEC(*(p
+ 1)) &&
426 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
430 if (!(IS_DEC(*(p
+ 2)) &&
433 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
438 switch (uugetline(buf
, sizeof(buf
))) {
444 return (checkend(buf
, "end", "no \"end\" line"));
451 int n
, count
, count4
;
452 char inbuf
[MAXPATHLEN
+ 1], *p
;
453 unsigned char outbuf
[MAXPATHLEN
* 4];
454 char leftover
[MAXPATHLEN
+ 1];
458 strcpy(inbuf
, leftover
);
459 switch (uugetline(inbuf
+ strlen(inbuf
),
460 sizeof(inbuf
) - strlen(inbuf
))) {
472 * Base64 encoded strings have the following
473 * characters in them: A-Z, a-z, 0-9 and +, / and =
475 if (isalnum(*p
) || *p
== '+' || *p
== '/' || *p
== '=')
482 strcpy(leftover
, inbuf
+ count4
+ 1);
483 inbuf
[count4
+ 1] = 0;
485 n
= b64_pton(inbuf
, outbuf
, sizeof(outbuf
));
489 fwrite(outbuf
, 1, n
, outfp
);
491 return (checkend(inbuf
, "====", "error decoding base64 input stream"));
498 (void)fprintf(stderr
,
499 "usage: uudecode [-cimprs] [file ...]\n"
500 " uudecode [-i] -o output_file [file]\n"
501 " b64decode [-cimprs] [file ...]\n"
502 " b64decode [-i] -o output_file [file]\n");