home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / shark.lzh / btoa.c < prev    next >
C/C++ Source or Header  |  1990-11-12  |  3KB  |  141 lines

  1. /* btoa: version 4.0
  2.  * stream filter to change 8 bit bytes into printable ascii
  3.  * computes the number of bytes, and three kinds of simple checksums
  4.  * incoming bytes are collected into 32-bit words, then printed in base 85
  5.  *  exp(85,5) > exp(2,32)
  6.  * the ASCII characters used are between '!' and 'u'
  7.  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
  8.  *
  9.  *  Paul Rutter        Joe Orost
  10.  *  philabs!per        petsd!joe
  11.  *
  12.  *  WARNING: this version is not compatible with the original as sent out
  13.  *  on the net.  The original encoded from ' ' to 't'; which cause problems
  14.  *  with some mailers (stripping off trailing blanks).
  15.  */
  16. /* DjG - Don Gloistein
  17.  *  Made some changes to compile with msdos/16 bit compilers. Some compilers
  18.  *  do not make the same assumptions on promoting an integer to long constant.
  19.  *  Msdos computers support a text and binary mode file open. Stdin/stdout are
  20.  *  defaulted to text streams. Included SETMODE for those systems. If compiled
  21.  *  without any defines, it will default to the stock unix version.
  22.  */
  23.  
  24. #include <stdio.h>
  25.  
  26. #ifdef MSC
  27. #include <fcntl.h>
  28. #include <io.h>
  29. #define SETMODE(x) setmode(fileno((x)),O_BINARY)
  30. #endif
  31.  
  32. #ifdef MWC_ATARI
  33. #define SETMODE(x) ((x)->_ff &= ~(_FASCII))
  34. #endif
  35. /* following take care of unix systems */
  36. #ifndef SETMODE
  37. #define SETMODE(x)
  38. #endif
  39.  
  40. #define reg register
  41.  
  42. #define MAXPERLINE 78
  43.  
  44. long int Ceor = 0L;
  45. long int Csum = 0L;
  46. long int Crot = 0L;
  47.  
  48. long int ccount = 0L;
  49. int bcount = 0;
  50. long int word = 0L;
  51.  
  52. #define EN(c)    (int) ((c) + '!')
  53.  
  54. encode(c)
  55.   reg c;
  56. {
  57.   Ceor ^= c;
  58.   Csum += c;
  59.   Csum += 1;
  60.   if ((Crot & 0x80000000)) {
  61.     Crot <<= 1;
  62.     Crot += 1;
  63.   } else {
  64.     Crot <<= 1;
  65.   }
  66.   Crot += c;
  67.  
  68.   word <<= 8;
  69.   word |= c;
  70.   if (bcount == 3) {
  71.     wordout(word);
  72.     bcount = 0;
  73.   } else {
  74.     bcount += 1;
  75.   }
  76. }
  77.  
  78. wordout(word)
  79.   reg long int word;
  80. {
  81.   if (word == 0) {
  82.     charout('z');
  83.   } else {
  84.     reg int tmp = 0;
  85.  
  86.     if(word < 0L) {  /* Because some don't support unsigned long */
  87.       tmp = 32;
  88.       word = word - (85L * 85L * 85L * 85L * 32L);
  89.     }
  90.     if(word < 0L) {
  91.       tmp = 64;
  92.       word = word - (85L * 85L * 85L * 85L * 32L);
  93.     }
  94.     charout(EN((word / (85L * 85L * 85L * 85L)) + tmp));
  95.     word %= (85L * 85L * 85L * 85L);
  96.     charout(EN(word / (85L * 85L * 85L)));
  97.     word %= (85L * 85L * 85L);
  98.     charout(EN(word / (85L * 85L)));
  99.     word %= (85L * 85L);
  100.     charout(EN(word / 85L));
  101.     word %= 85L;
  102.     charout(EN(word));
  103.   }
  104. }
  105.  
  106. charout(c) {
  107.   putchar(c);
  108.   ccount += 1;
  109.   if (ccount == MAXPERLINE) {
  110.     putchar('\n');
  111.     ccount = 0;
  112.   }
  113. }
  114.  
  115. main(argc,argv)
  116.   char **argv;
  117. {
  118.   reg c;
  119.   reg long int n;
  120.  
  121.   if (argc != 1) {
  122.     fprintf(stderr,"bad args to %s\n", argv[0]);
  123.     exit(2);
  124.   }
  125.   SETMODE(stdin);
  126.   SETMODE(stdout);
  127.   printf("xbtoa Begin\n");
  128.   n = 0;
  129.   while ((c = getchar()) != EOF) {
  130.     encode(c);
  131.     n += 1;
  132.   }
  133.   while (bcount != 0) {
  134.     encode(0);
  135.   }
  136.   /* n is written twice as crude cross check*/
  137.   printf("\nxbtoa End N %ld %lx E %lx S %lx R %lx\n", n, n, Ceor, Csum, Crot);
  138.   exit(0);
  139. }
  140.  
  141.