home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / btoa.arc / ATOB.C next >
C/C++ Source or Header  |  1989-01-24  |  3KB  |  158 lines

  1. /* atob: version 4.0
  2.  * stream filter to change printable ascii from "btoa" back into 8 bit bytes
  3.  * if bad chars, or Csums do not match: exit(1) [and NO output]
  4.  *
  5.  *  Paul Rutter        Joe Orost
  6.  *  philabs!per        petsd!joe
  7.  *
  8.  *  This version PC-specific - MSC/TC compatible
  9.  *  rj berry bellevue wa 1/21/89 
  10.  *  CS 73407,3152  uucp ...uw-beaver!tikal!ole!ray
  11.  */
  12. /* TAB 4 */
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <io.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #ifdef TURBO
  21. #include <dir.h>
  22. #endif
  23.  
  24. char vers[]="[RJB 1/21/89]";
  25.  
  26. char tmp_name[40];
  27. unsigned long Ceor = 0;
  28. unsigned long Csum = 0;
  29. unsigned long Crot = 0;
  30. unsigned long word = 0;
  31. int bcount=0;
  32.  
  33. union    {
  34.     unsigned long word;
  35.     unsigned char uchar[4];
  36.     } ublock;
  37.  
  38.  
  39. fatal(char * msg) {
  40.     fprintf(stderr, "atob: ERROR: %s\n", msg);
  41.     exit(1);
  42. }
  43.  
  44. #define DE(c) ((c) - '!')
  45.  
  46. decode(unsigned char c) 
  47. {
  48.     if (c == 'z') {
  49.         if (bcount != 0)
  50.             fatal("bcount synchronization error");
  51.         else {
  52.             byteout(0);
  53.             byteout(0);
  54.             byteout(0);
  55.             byteout(0);
  56.         }
  57.     } 
  58.     else if ((c >= '!') && (c < ('!' + 85))) {
  59.         if (bcount == 0) {
  60.         ublock.word = DE(c);
  61.         ++bcount;
  62.         } 
  63.         else if (bcount < 4) {
  64.                 ublock.word *= 85;
  65.                 ublock.word += DE(c);
  66.                 ++bcount;
  67.                 } 
  68.               else {
  69.                 ublock.word = ublock.word*85 + DE(c);
  70.                 byteout(ublock.uchar[3]);
  71.                 byteout(ublock.uchar[2]);
  72.                 byteout(ublock.uchar[1]);
  73.                 byteout(ublock.uchar[0]);
  74.                 ublock.word = 0;
  75.                 bcount = 0;
  76.               }
  77.     } 
  78.     else
  79.         fatal("illegal character in input stream");
  80. }
  81.  
  82. FILE *tmp_file;
  83.  
  84. byteout(int c)
  85. {
  86.     Ceor ^= c;
  87.     Csum += c;
  88.     Csum += 1;
  89.     if ((Crot & 0x80000000)) {
  90.         Crot <<= 1;
  91.         Crot += 1;
  92.     }
  93.     else
  94.         Crot <<= 1;
  95.     Crot += c;
  96.     if((putc(c, tmp_file))==EOF)
  97.         fatal(strcat("write error on file ", tmp_name));
  98. }
  99.  
  100. main(int argc, char *argv[])
  101. {
  102.     int c;
  103.     char buf[100];
  104.     char *tmppath;
  105.     unsigned long n1, n2, oeor, osum, orot;
  106.  
  107.     if (argc != 1) {
  108.         fprintf(stderr,"bad args to %s\n", argv[0]);
  109.         exit(2);
  110.     }
  111.  
  112.     /* place tmp file in TMP (presumably a ramdisk?) if possible */
  113.  
  114.     if((tmppath=getenv("TMP"))==NULL)
  115.         if((tmppath=getenv("TMPDIR"))==NULL)
  116.             tmppath="";
  117.     strcpy(tmp_name,strcat(tmppath, mktemp("XXXXXX")));
  118.     
  119.     tmp_file = fopen(tmp_name, "wb+");
  120.     if (tmp_file == NULL)
  121.         fatal(strcat("couldn't open tmp file ", tmp_name));
  122.  
  123.     /*search for header line*/
  124.     for (;;) {
  125.     if (fgets(buf, sizeof buf, stdin) == NULL)
  126.         fatal("empty input stream");
  127.     if ((strcmp(buf, "xbtoa Begin\n"))==0)
  128.         break;
  129.     }
  130.  
  131.     while ((c = getchar()) != EOF) {
  132.         if (c == '\n')
  133.             continue;
  134.         else if (c == 'x')
  135.             break;
  136.         else
  137.             decode(c);
  138.     }
  139.     if (scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",
  140.         &n1, &n2, &oeor, &osum, &orot) != 5)
  141.             fatal("scanf failure on sumcheck line");
  142.     if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot))
  143.         fatal("sumcheck values don't match");
  144.     else {
  145.  
  146.         /*copy OK tmp file to stdout*/;
  147.         setmode(fileno(stdout), O_BINARY);
  148.         rewind(tmp_file);
  149.         while ((c=getc(tmp_file))!=EOF)
  150.             putchar(c);
  151.  
  152.         fclose(tmp_file);
  153.          unlink(tmp_name);
  154.     }
  155.     exit(0);
  156. }
  157.  
  158.