home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / kit / part01 / btoa / atob.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.2 KB  |  168 lines

  1. /*
  2.  
  3.    ##     #####   ####   #####            ####
  4.   #  #      #    #    #  #    #          #    #
  5.  #    #     #    #    #  #####           #
  6.  ######     #    #    #  #    #   ###    #
  7.  #    #     #    #    #  #    #   ###    #    #
  8.  #    #     #     ####   #####    ###     ####
  9.  
  10.     stream filter to change printable ascii from "btoa" back into
  11.     8 bit bytes if bad chars, or Csums do not match: exit(1)
  12.     [and NO output]
  13.  
  14.     Paul Rutter Joe Orost
  15.  
  16.     Raphael Manfredi (ram) modified this file, in order to produce
  17.     meaningfull error messages (otherwise, fatal() was called, even
  18.     when a temporary file could not be created...); added error()
  19. */
  20.  
  21. /*
  22.  * $Id: atob.c,v 2.0 91/02/19 15:49:21 ram Exp $
  23.  *
  24.  * $Log:    atob.c,v $
  25.  * Revision 2.0  91/02/19  15:49:21  ram
  26.  * Baseline for first official release.
  27.  * 
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #define reg register
  33.  
  34. #define streq(s0, s1)  strcmp(s0, s1) == 0
  35.  
  36. #define times85(x)    ((((((x<<2)+x)<<2)+x)<<2)+x)
  37.  
  38. long int Ceor = 0;
  39. long int Csum = 0;
  40. long int Crot = 0;
  41. long int word = 0;
  42. long int bcount = 0;
  43.  
  44. fatal() {
  45.   fprintf(stderr, "bad format or Csum to atob\n");
  46.   exit(1);
  47. }
  48.  
  49. error(s)
  50. char *s;    /* the error message */
  51. {
  52.   fprintf(stderr, "%s\n", s);
  53.   exit(1);
  54. }
  55.  
  56. #define DE(c) ((c) - '!')
  57.  
  58. decode(c) reg c;
  59. {
  60.   if (c == 'z') {
  61.     if (bcount != 0) {
  62.       fatal();
  63.     } 
  64.     else {
  65.       byteout(0);
  66.       byteout(0);
  67.       byteout(0);
  68.       byteout(0);
  69.     }
  70.   } 
  71.   else if ((c >= '!') && (c < ('!' + 85))) {
  72.     if (bcount == 0) {
  73.       word = DE(c);
  74.       ++bcount;
  75.     } 
  76.     else if (bcount < 4) {
  77.       word = times85(word);
  78.       word += DE(c);
  79.       ++bcount;
  80.     } 
  81.     else {
  82.       word = times85(word) + DE(c);
  83.       byteout((int)((word >> 24) & 255));
  84.       byteout((int)((word >> 16) & 255));
  85.       byteout((int)((word >> 8) & 255));
  86.       byteout((int)(word & 255));
  87.       word = 0;
  88.       bcount = 0;
  89.     }
  90.   } 
  91.   else {
  92.     fatal();
  93.   }
  94. }
  95.  
  96. FILE *tmp_file;
  97.  
  98. byteout(c) reg c;
  99. {
  100.   Ceor ^= c;
  101.   Csum += c;
  102.   Csum += 1;
  103.   if ((Crot & 0x80000000)) {
  104.     Crot <<= 1;
  105.     Crot += 1;
  106.   } 
  107.   else {
  108.     Crot <<= 1;
  109.   }
  110.   Crot += c;
  111.   putc(c, tmp_file);
  112. }
  113.  
  114. main(argc, argv) char **argv;
  115. {
  116.   reg c;
  117.   reg long int i;
  118.   char tmp_name[100];
  119.   char buf[100];
  120.   long int n1, n2, oeor, osum, orot;
  121.  
  122.   if (argc != 1) {
  123.     fprintf(stderr,"bad args to %s\n", argv[0]);
  124.     exit(2);
  125.   }
  126.   sprintf(tmp_name, "/usr/tmp/atob.%x", getpid());
  127.   tmp_file = fopen(tmp_name, "w+");
  128.   if (tmp_file == NULL) {
  129.     error("can't create temporary file");
  130.   }
  131.   unlink(tmp_name); /* Make file disappear */
  132.   /*search for header line*/
  133.   for (;;) {
  134.     if (fgets(buf, sizeof buf, stdin) == NULL) {
  135.       error("could not read header line");
  136.     }
  137.     if (streq(buf, "xbtoa Begin\n")) {
  138.       break;
  139.     }
  140.   }
  141.  
  142.   while ((c = getchar()) != EOF) {
  143.     if (c == '\n') {
  144.       continue;
  145.     } 
  146.     else if (c == 'x') {
  147.       break;
  148.     } 
  149.     else {
  150.       decode(c);
  151.     }
  152.   }
  153.   if (scanf("btoa End N %ld %lx E %lx S %lx R %lx\n", &n1, &n2, &oeor, &osum, &orot) != 5) {
  154.     error("could not read check sum");
  155.   }
  156.   if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) {
  157.     fatal();
  158.   } 
  159.   else {
  160.     /*copy OK tmp file to stdout*/;
  161.     fseek(tmp_file, 0L, 0);
  162.     for (i = n1; --i >= 0;) {
  163.       putchar(getc(tmp_file));
  164.     }
  165.   }
  166.   exit(0);
  167. }
  168.