#define PROG_NAME "jsuudecode" #define PROG_DESC "Jorge Stolfi's version of \"uudecode\"" #define PROG_VERS "1.1" /* Copyright © 1983 Regents of the University of California. */ /* See the copyright, authorship, and warranty notice at end of file. */ /* Last edited on 2009-01-08 03:57:35 by stolfi" */ #define PROG_HELP \ PROG_NAME " < INFILE > OUTFILE" #define PROG_INFO \ "NAME\n" \ " " PROG_NAME " - " PROG_DESC "\n" \ "\n" \ "SYNOPSIS\n" \ PROG_HELP "\n" \ "\n" \ "DESCRIPTION\n" \ " A simplified version of uudecode(1). Reads from standard" \ " input a file produced by uuencode(1). Writes" \ " to standard output the decoded plain file.\n" \ "\n" \ "AUTHORS\n" \ " The author of uudecode(1) is unknown.\n" \ " Copyright © 1983 Regents of the University of California.\n" \ "\n" \ " Modified 12 April 1990 by Mark Adler.\n" \ "\n" \ " Modifed 13 February 1991 by Greg Roelofs.\n" \ "\n" \ " Modified 13 April 1991 by Gary Mussar.\n" \ "\n" \ " Radically simplified 19 March 2005 by Jorge Stolfi: for" \ " Linux-only, works strictly as a filter." #include #include #include void decode(FILE *in, FILE *out); /* Copy from in to out, decoding as you go along. */ void outdec(char *p, FILE *f, int n); /* Output a group of 3 bytes (4 input characters). The input chars are pointed to by p, they are to be output to file f. Argument n is used to tell us not to output all of them at the end of the file. */ /* single-character decode */ #define DEC(c) (((c) - ' ') & 077) int main(int argc, char **argv) { FILE *in = stdin; FILE *out = stdout; char buf[80]; /* Check command line args: */ if (argc > 1) { printf("%s version %s, usage:\n%s\n", PROG_NAME, PROG_VERS, PROG_HELP); exit(1); } /* Search and skip header line: */ for (;;) { if (fgets(buf, sizeof buf, in) == NULL) { fprintf(stderr, "No begin line\n"); exit(1); } if (strncmp(buf, "begin ", 6) == 0) break; } decode(in, out); if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) { fprintf(stderr, "No end line\n"); exit(1); } return 0; } void decode(FILE *in, FILE *out) { char buf[80]; char *bp; int n, i, expected; for (;;) { /* for each input line */ if (fgets(buf, sizeof buf, in) == NULL) { printf("Short file\n"); exit(10); } n = DEC(buf[0]); if ((n <= 0) || (buf[0] == '\n')) break; /* Calculate expected # of chars and pad if necessary */ expected = ((n+2)/3)<<2; for (i = strlen(buf)-1; i <= expected; i++) buf[i] = ' '; bp = &buf[1]; while (n > 0) { outdec(bp, out, n); bp += 4; n -= 3; } } } void outdec(char *p, FILE *f, int n) { int c1, c2, c3; c1 = DEC(*p) << 2 | DEC(p[1]) >> 4; c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2; c3 = DEC(p[2]) << 6 | DEC(p[3]); if (n >= 1) putc(c1, f); if (n >= 2) putc(c2, f); if (n >= 3) putc(c3, f); } /* ** Modified 12 April 1990 by Mark Adler for use on MSDOS systems with ** Microsoft C and Turbo C. ** ** Modifed 13 February 1991 by Greg Roelofs for use on VMS systems. As ** with the MS-DOS version, the setting of the file mode has been disabled. ** Compile and link normally (but note that the shared-image link option ** produces a binary only 6 blocks long, as opposed to the 137-block one ** produced by an ordinary link). To set up the VMS symbol to run the ** program ("run uudecode filename" won't work), do: ** uudecode :== "$disk:[directory]uudecode.exe" ** and don't forget the leading "$" or it still won't work. The binaries ** produced by this program are in VMS "stream-LF" format; this makes no ** difference to VMS when running decoded executables, nor to VMS unzip, ** but other programs such as zoo or arc may or may not require the file ** to be "BILFed" (or "unBILFed" or whatever). Also, unlike the other ** flavors, VMS files don't get overwritten (a higher version is created). ** ** Modified 13 April 1991 by Gary Mussar to be forgiving of systems that ** appear to be stripping trailing blanks. ** ** Modified 19 mar 2005 by Jorge Stolfi: radically simplified: for ** Linux-only, works strictly as a filter. */ /* COPYRIGHT, AUTHORSHIP, AND WARRANTY NOTICE: ** ** Copyright (c) 1983 Regents of the University of California. ** All rights reserved. ** ** Redistribution and use in source and binary forms are permitted ** provided that the above copyright notice and this paragraph are ** duplicated in all such forms and that any documentation, ** advertising materials, and other materials related to such ** distribution and use acknowledge that the software was developed ** by the University of California, Berkeley. The name of the ** University may not be used to endorse or promote products derived ** from this software without specific prior written permission. ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ** WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */