home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / internet / uucode_68020 / uucoders / uudecode.c < prev    next >
C/C++ Source or Header  |  1997-08-31  |  6KB  |  239 lines

  1.  
  2. /*
  3.  * uudecode [input]
  4.  *
  5.  * Decode a file encoded with uuencode.  Will extract multiple encoded
  6.  * modules from a single file.    Can deal with most mangled files, including
  7.  * those that have been mangled by common BITNET problems.
  8.  *
  9.  */
  10.  
  11. /* Modified by Andreas R. Kleinert
  12.  *
  13.  * 31.08.97: - reworked for SAS/C 6.58
  14.  *           - better compiler settings
  15.  *           - ANSI-fied
  16.  *
  17.  * 04.08.94: - first, internal version
  18.  *
  19.  *
  20.  * Original authors:
  21.  *
  22.  * Written by Mark Horton
  23.  * Modified by ajr (Alan J Rosenthatl,flaps@utcsri.UUCP) to use checksums
  24.  * Modified by fnf (Fred Fish,well!fnf) to use Keith Pyle's suggestion for
  25.  * compatibility
  26.  * Modified by bcn (Bryce Nesbitt,ucbvax!cogsci!bryce) to fix a misleading
  27.  * error message on the Amiga port, to fix a bug that prevented decoding
  28.  * certain files, to work even if trailing spaces have been removed from a
  29.  * file, to check the filesize (if present), to add some error checking, to
  30.  * loop for multiple decodes from a single file, and to handle common
  31.  * BITNET mangling.  Kludged around a missing string function in Aztec
  32.  * C. Changed "r" to "rb" and "w" to "wb" for Messy-dos machines
  33.  * (Thanks to Andrew Wylie).
  34.  */
  35.  
  36. #define __USE_SYSBASE
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41.  
  42. #include <ctype.h>
  43.  
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46.  
  47. #include <exec/types.h>
  48.  
  49. #include <proto/exec.h>
  50.  
  51. void decode(FILE *in, FILE *out, UBYTE *dest);
  52.  
  53. UBYTE __aligned version [] = "\0$VER: uudecode 1.0 (31.8.97)";
  54.  
  55. long main(long argc, char **argv)
  56. {
  57.  FILE *in, *out;
  58.  LONG through_loop = 0; /* Dejavu indicator */
  59.  LONG mode;        /* file's mode (from header) */
  60.  LONG filesize;            /* theoretical file size (from header) */
  61.  UBYTE dest[128];
  62.  UBYTE buf[80];
  63.  
  64.     /* A filename can be specified to be uudecoded, or nothing can
  65.     be specified, and the input will come from STDIN */
  66.  
  67.     switch (argc)
  68.     {
  69.     case 1:
  70.     in=stdin;
  71.     break;
  72.  
  73.     case 2:
  74.     if ((in = fopen(argv[1], "r")) == NULL)
  75.         {
  76.         fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
  77.         fprintf(stderr, "USAGE: uudecode [infile]\n");
  78.         exit(20);
  79.         }
  80.     break;
  81.  
  82.     default:
  83.     fprintf(stderr, "USAGE: uudecode [infile]\n");
  84.     exit(0);
  85.     break;
  86.     }
  87.  
  88.     /* Loop through file, searching for headers.  Decode anything with a
  89.        header, complain if there where no headers. */
  90.  
  91. for (;;)
  92. {
  93.     /* search file for header line */
  94.     for (;;)
  95.     {
  96.     if (fgets(buf, sizeof buf, in) == NULL)
  97.         {
  98.         if (!through_loop)
  99.         {
  100.         fprintf(stderr, "ERROR: no `begin' line!\n");
  101.         exit(20);
  102.         }
  103.         else
  104.         {
  105.         exit(0);
  106.         }
  107.         }
  108.     if (strncmp(buf, "begin ", 6) == 0)
  109.         break;
  110.     }
  111.     sscanf(buf, "begin %o %s", &mode, dest);
  112.  
  113.     /* create output file */
  114.     if ((out = fopen(dest, "w")) == NULL)
  115.     {
  116.     fprintf(stderr, "ERROR: can't open output file %s\n", dest);
  117.     exit(20);
  118.     }
  119.  
  120.     decode(in, out, dest);
  121.  
  122.     if (fgets(buf, sizeof buf, in) == NULL || strncmp(buf,"end",3))
  123.     {           /* don't be overly picky about newline ^ */
  124.     fprintf(stderr, "ERROR: no `end' line\n");
  125.     exit(20);
  126.     }
  127.  
  128.     if (!(fgets(buf,sizeof buf,in) == NULL || strncmp(buf,"size ",3)))
  129.     {
  130.     sscanf(buf, "size %ld", &filesize);
  131.     if (ftell(out) != filesize)
  132.         {
  133.         fprintf(stderr, "ERROR: file should have been %ld bytes long but was %ld.\n", filesize, ftell(out));
  134.         exit(20);
  135.         }
  136.     }
  137.     through_loop = 1;
  138. }   /* forever */
  139.  
  140.  exit(0);
  141. }
  142.  
  143. #define SUMSIZE 64
  144. #define DEC(c)  (((c) - ' ') & 077)    /* single character decode */
  145.  
  146. /*
  147.  * Copy from in to out, decoding as you go.
  148.  * If a return or newline is encountered too early in a line, it is
  149.  * assumed that means that some editor has truncated trailing spaces.
  150.  */
  151. void decode(FILE *in, FILE *out, UBYTE *dest)
  152. {
  153.  extern errno;
  154.  
  155.  UBYTE *bp;
  156.  LONG nosum=0;
  157.  LONG j, n, checksum, line;
  158.  UBYTE buf[256];
  159.  
  160.     for (line = 1; ; line++)    /* for each input line */
  161.     {
  162.     if (fgets(buf, sizeof buf, in) == NULL)
  163.         {
  164.         fprintf(stderr, "ERROR: input ended unexpectedly!\n");
  165.         exit(20);
  166.         }
  167.  
  168.     /* Pad end of lines in case some editor truncated trailing
  169.        spaces */
  170.  
  171.     for (n=0;n<79;n++)  /* search for first \r, \n or \000 */
  172.         {
  173.         if (buf[n]=='\176')     /* If BITNET made a twiddle, */
  174.         buf[n]='\136';     /* we make a caret           */
  175.         if (buf[n]=='\r'||buf[n]=='\n'||buf[n]=='\000')
  176.         break;
  177.         }
  178.     for (;n<79;n++)     /* when found, fill rest of line with space */
  179.         {
  180.         buf[n]=' ';
  181.         }
  182.     buf[79]=0;        /* terminate new string */
  183.  
  184.     checksum = 0;
  185.     n = DEC(buf[0]);
  186.     if (n <= 0)
  187.         break;    /* 0 bytes on a line??    Must be the last line */
  188.  
  189.     bp = &buf[1];
  190.  
  191.     /* FOUR input characters go into each THREE output charcters */
  192.  
  193.     while (n >= 4)
  194.         {
  195.         j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4; putc(j, out); checksum += j;
  196.         j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2; putc(j, out); checksum += j;
  197.         j = DEC(bp[2]) << 6 | DEC(bp[3]);      putc(j, out); checksum += j;
  198.         checksum = checksum % SUMSIZE;
  199.         bp += 4;
  200.         n -= 3;
  201.         }
  202.  
  203.         j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4;
  204.         checksum += j;
  205.         if (n >= 1)
  206.             putc(j, out);
  207.         j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2;
  208.         checksum += j;
  209.         if (n >= 2)
  210.             putc(j, out);
  211.         j = DEC(bp[2]) << 6 | DEC(bp[3]);
  212.         checksum += j;
  213.         if (n >= 3)
  214.             putc(j, out);
  215.         checksum = checksum % SUMSIZE;
  216.         bp += 4;
  217.         n -= 3;
  218.  
  219.      /* Error checking under UNIX??? You must be kidding... */
  220.      /* Check if an error occured while writing to that last line */
  221.     if (errno)
  222.         {
  223.         fprintf(stderr, "ERROR: error writing to %s\n",dest);
  224.         exit(20);
  225.         }
  226.  
  227.     /* The line has been decoded; now check that sum */
  228.  
  229.     nosum |= !isspace(*bp);
  230.     if (nosum)                      /* Is there a checksum at all?? */
  231.         {
  232.         if (checksum != DEC(*bp))   /* Does that checksum match? */
  233.         {
  234.         fprintf(stderr, "ERROR: checksum mismatch decoding %s, line %d.\n",dest, line);
  235.         }
  236.         }    /* sum */
  237.     }    /* line */
  238. }   /* function */
  239.