home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / UUDECODE.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  2KB  |  64 lines

  1. /* This is a junky piece of C designed to take a uuencoded file
  2.    as the first parameter and decode it into the file given as
  3.    the second parameter. there is no error checking or other
  4.    warning if it encounters a problem. The purpose of this
  5.    program is to act as a simple bootstrap to enable the initial
  6.    transfer of a file to allow more advanced binary transfers.
  7.   
  8.    In particular, note that the first line of the file to be
  9.    decoded should be the 'begin' line produced by a uuencode.
  10.   
  11.    This code (for what it is worth) is Copyright 1990, by
  12.    David R. Evans, NQ0I.
  13.   
  14. */
  15.   
  16. #define byte unsigned char
  17.   
  18. #include <stdio.h>
  19. #include <string.h>
  20.   
  21. main(argc, argv)
  22. int argc;
  23. char** argv;
  24. { char line[100];
  25.     FILE *infile, *outfile;
  26.   
  27. /* open the files */
  28.     infile = fopen(argv[1], "r");
  29.     outfile = fopen(argv[2], "w");
  30.   
  31. /* skip the first (begin) line */
  32.     fgets(line, 100, infile);
  33.   
  34. /* decode the remainder of the file */
  35.     while (fgets(line, 100, infile), strncmp(line, "end", 3))
  36.     { int n_to_write, n_to_read, n_reads, n, m;
  37.         byte b[4], out[3];
  38.   
  39.         n_to_write = (int)line[0] - 32;
  40.         n_to_read = ((n_to_write + 2) / 3) * 4;
  41.         if(strlen(line) - 2 < n_to_read)    /* EOF apparently */
  42.             n_to_read = 0;
  43.         n_reads = n_to_read / 4;
  44.         for (n = 0; n < n_reads; n++)
  45.         { for (m = 0; m < 4; m++)
  46.                 b[m] = line[1 + 4 * n + m] - 32;
  47.   
  48. /* now do the actual decode */
  49.             out[0] = (b[0] << 2) | ((b[1] & 0x30) >> 4);
  50.             out[1] = ((b[1] & 0x0f) << 4) | ((b[2] & 0x3c) >> 2);
  51.             out[2] = ((b[2] & 0x03) << 6) | (b[3]);
  52.             if(n_to_write > 3)
  53.                 for (m = 0; m < 3; m++)
  54.                     fputc(out[m], outfile);
  55.             else
  56.                 for (m = 0; m < n_to_write; m++)
  57.                     fputc(out[m], outfile);
  58.             n_to_write -= m;
  59.         }
  60.     }
  61.     fflush(outfile);
  62. }
  63.   
  64.