home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / mpwtools.cpt / misc src / btoa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-25  |  2.3 KB  |  120 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.  
  17. #include <stdio.h>
  18. #include <CursorCtl.h>
  19. #include <String.h>
  20.  
  21. #define reg register
  22.  
  23. #define MAXPERLINE 78
  24.  
  25. long int Ceor = 0;
  26. long int Csum = 0;
  27. long int Crot = 0;
  28.  
  29. long int ccount = 0;
  30. long int bcount = 0;
  31. long int word;
  32.  
  33. #define EN(c)    (int) ((c) + '!')
  34.  
  35. encode(c) 
  36.   reg c;
  37. {
  38.   Ceor ^= c;
  39.   Csum += c;
  40.   Csum += 1;
  41.   if ((Crot & 0x80000000)) {
  42.     Crot <<= 1;
  43.     Crot += 1;
  44.   } else {
  45.     Crot <<= 1;
  46.   }
  47.   Crot += c;
  48.  
  49.   word <<= 8;
  50.   word |= c;
  51.   if (bcount == 3) {
  52.     wordout(word);
  53.     bcount = 0;
  54.   } else {
  55.     bcount += 1;
  56.   }
  57. }
  58.  
  59. wordout(word) 
  60.   reg long int word;
  61. {
  62.   if (word == 0) {
  63.     charout('z');
  64.   } else {
  65.     reg int tmp = 0;
  66.     
  67.     if(word < 0) {    /* Because some don't support unsigned long */
  68.       tmp = 32;
  69.       word = word - (long)(85 * 85 * 85 * 85 * 32);
  70.     }
  71.     if(word < 0) {
  72.       tmp = 64;
  73.       word = word - (long)(85 * 85 * 85 * 85 * 32);
  74.     }
  75.     charout(EN((word / (long)(85 * 85 * 85 * 85)) + tmp));
  76.     word %= (long)(85 * 85 * 85 * 85);
  77.     charout(EN(word / (85 * 85 * 85)));
  78.     word %= (85 * 85 * 85);
  79.     charout(EN(word / (85 * 85)));
  80.     word %= (85 * 85);
  81.     charout(EN(word / 85));
  82.     word %= 85;
  83.     charout(EN(word));
  84.   }
  85. }
  86.  
  87. charout(c) {
  88.   putchar(c);
  89.   ccount += 1;
  90.   if (ccount == MAXPERLINE) {
  91.       SpinCursor(-1);
  92.     putchar('\n');
  93.     ccount = 0;
  94.   }
  95. }
  96.  
  97. main(argc,argv) 
  98.   char **argv;
  99. {
  100.   reg c;
  101.   reg long int n;
  102.  
  103.   if (argc != 1) {
  104.     fprintf(stderr,"bad args to %s\n", argv[0]);
  105.     exit(2);
  106.   }
  107.   printf("xbtoa Begin\n");
  108.   n = 0;
  109.   while ((c = getchar()) != EOF) {
  110.     encode(c);
  111.     n += 1;
  112.   }
  113.   while (bcount != 0) {
  114.     encode(0);
  115.   }
  116.   /* n is written twice as crude cross check*/
  117.   printf("\nxbtoa End N %ld %lx E %lx S %lx R %lx\n", n, n, Ceor, Csum, Crot);
  118.   exit(0);
  119. }
  120.