1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 /*
19  * Modified 12 April 1990 by Mark Adler for use on MSDOS systems with
20  * Microsoft C and Turbo C.
21  *
22  * Modifed 13 February 1991 by Greg Roelofs for use on VMS systems.  As
23  * with the MS-DOS version, the setting of the file mode has been disabled.
24  * Compile and link normally (but note that the shared-image link option
25  * produces a binary only 6 blocks long, as opposed to the 137-block one
26  * produced by an ordinary link).  To set up the VMS symbol to run the
27  * program ("run uudecode filename" won't work), do:
28  *		uudecode :== "$disk:[directory]uudecode.exe"
29  * and don't forget the leading "$" or it still won't work.  The binaries
30  * produced by this program are in VMS "stream-LF" format; this makes no
31  * difference to VMS when running decoded executables, nor to VMS unzip,
32  * but other programs such as zoo or arc may or may not require the file
33  * to be "BILFed" (or "unBILFed" or whatever).  Also, unlike the other
34  * flavors, VMS files don't get overwritten (a higher version is created).
35  *
36  * Modified 13 April 1991 by Gary Mussar to be forgiving of systems that
37  * appear to be stripping trailing blanks.
38  *
39  * Modified 28 February 2002 for use on WIN32 systems with Microsoft C.
40  *
41  * Modified 08 July 2006 to cast strlen() result to int to suppress a
42  * warning on platforms where size_t > sizeof(int).
43  *
44  * $NHDT-Date: 1432512787 2015/05/25 00:13:07 $  $NHDT-Branch: master $:$NHDT-Revision: 1.7 $
45  */
46 
47 #ifndef lint
48 static char sccsid[] = "@(#)uudecode.c	5.5 (Berkeley) 7/6/88";
49 #endif /* not lint */
50 
51 #ifdef __MSDOS__ /* For Turbo C */
52 #define MSDOS 1
53 #endif
54 
55 #ifdef _WIN32
56 #undef MSDOS
57 #undef __MSDOS__
58 #ifndef WIN32
59 #define WIN32
60 #endif
61 #endif
62 
63 /*
64  * uudecode [input]
65  *
66  * create the specified file, decoding as you go.
67  * used with uuencode.
68  */
69 #include <stdio.h>
70 
71 #ifdef VMS
72 #include <types.h>
73 #include <stat.h>
74 #else
75 #if !defined(MSDOS) && !defined(WIN32)
76 #include <pwd.h>
77 #endif
78 #include <sys/types.h> /* MSDOS, WIN32, or UNIX */
79 #include <sys/stat.h>
80 #include <string.h>
81 #include <stdlib.h>
82 #endif
83 
84 #include "warnings.h"
85 
86 static void decode(FILE *, FILE *);
87 static void outdec(char *, FILE *, int);
88 
89 /* single-character decode */
90 #define DEC(c) (((c) - ' ') & 077)
91 
92 DISABLE_WARNING_UNREACHABLE_CODE
93 
94 int
main(int argc,char ** argv)95 main(int argc, char **argv)
96 {
97     FILE *in, *out;
98     int mode;
99     char dest[128];
100     char buf[80];
101 
102     /* optional input arg */
103     if (argc > 1) {
104         if ((in = fopen(argv[1], "r")) == NULL) {
105             perror(argv[1]);
106             exit(1);
107         }
108         argv++;
109         argc--;
110     } else
111         in = stdin;
112 
113     if (argc != 1) {
114         printf("Usage: uudecode [infile]\n");
115         exit(2);
116     }
117 
118     /* search for header line */
119     for (;;) {
120         if (fgets(buf, sizeof buf, in) == NULL) {
121             fprintf(stderr, "No begin line\n");
122             exit(3);
123         }
124         if (strncmp(buf, "begin ", 6) == 0)
125             break;
126     }
127     (void) sscanf(buf, "begin %o %s", &mode, dest);
128 
129 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
130     /* handle ~user/file format */
131     if (dest[0] == '~') {
132         char *sl;
133         struct passwd *getpwnam();
134         struct passwd *user;
135         char dnbuf[100], *index(), *strcat(), *strcpy();
136 
137         sl = index(dest, '/');
138         if (sl == NULL) {
139             fprintf(stderr, "Illegal ~user\n");
140             exit(3);
141         }
142         *sl++ = 0;
143         user = getpwnam(dest + 1);
144         if (user == NULL) {
145             fprintf(stderr, "No such user as %s\n", dest);
146             exit(4);
147         }
148         strcpy(dnbuf, user->pw_dir);
149         strcat(dnbuf, "/");
150         strcat(dnbuf, sl);
151         strcpy(dest, dnbuf);
152     }
153 #endif /* !defined(MSDOS) && !defined(VMS) */
154 
155 /* create output file */
156 #if defined(MSDOS) || defined(WIN32)
157     out = fopen(dest, "wb"); /* Binary file */
158 #else
159     out = fopen(dest, "w");
160 #endif
161     if (out == NULL) {
162         perror(dest);
163         exit(4);
164     }
165 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32) /* i.e., UNIX */
166     chmod(dest, mode);
167 #endif
168 
169     decode(in, out);
170 
171     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
172         fprintf(stderr, "No end line\n");
173         exit(5);
174     }
175     exit(0);
176     /*NOTREACHED*/
177     return 0;
178 }
179 
180 RESTORE_WARNINGS
181 
182 /*
183  * copy from in to out, decoding as you go along.
184  */
185 void
decode(FILE * in,FILE * out)186 decode(FILE *in, FILE *out)
187 {
188     char buf[80];
189     char *bp;
190     int n, i, expected;
191 
192     for (;;) {
193         /* for each input line */
194         if (fgets(buf, sizeof buf, in) == NULL) {
195             printf("Short file\n");
196             exit(10);
197         }
198         n = DEC(buf[0]);
199         if ((n <= 0) || (buf[0] == '\n'))
200             break;
201 
202         /* Calculate expected # of chars and pad if necessary */
203         expected = ((n + 2) / 3) << 2;
204         for (i = (int) strlen(buf) - 1; i <= expected; i++)
205             buf[i] = ' ';
206 
207         bp = &buf[1];
208         while (n > 0) {
209             outdec(bp, out, n);
210             bp += 4;
211             n -= 3;
212         }
213     }
214 }
215 
216 /*
217  * output a group of 3 bytes (4 input characters).
218  * the input chars are pointed to by p, they are to
219  * be output to file f.  n is used to tell us not to
220  * output all of them at the end of the file.
221  */
222 void
outdec(char * p,FILE * f,int n)223 outdec(char *p, FILE *f, int n)
224 {
225     int c1, c2, c3;
226 
227     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
228     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
229     c3 = DEC(p[2]) << 6 | DEC(p[3]);
230     if (n >= 1)
231         putc(c1, f);
232     if (n >= 2)
233         putc(c2, f);
234     if (n >= 3)
235         putc(c3, f);
236 }
237 
238 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
239 /*
240  * Return the ptr in sp at which the character c appears;
241  * NULL if not found
242  */
243 
244 #ifndef NULL
245 #define NULL 0
246 #endif
247 
248 char *
index(sp,c)249 index(sp, c)
250 register char *sp, c;
251 {
252     do {
253         if (*sp == c)
254             return (sp);
255     } while (*sp++);
256     return (NULL);
257 }
258 #endif
259