home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / aufstols.zoo / aufstools.1 / binhex / 8to6.c next >
C/C++ Source or Header  |  1991-02-26  |  1KB  |  65 lines

  1. /*
  2.  * convert 8 bit data stream into 6 bit data stream
  3.  *
  4.  * David Gentzel, Lexeme Corporation
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #define MAXLINELEN    62
  10.  
  11. static
  12. char tr[]="!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
  13.  
  14. /*
  15.  * Output a character to the current output file converting from 8 bit to 6 bit
  16.  * representation.  When called with EOF, flush all pending output.
  17.  */
  18. void putchar_6(c)
  19. int c;
  20. {
  21.     static unsigned char buf[3];
  22.     static unsigned int num_bytes = 0;
  23.     static count = 1;    /* # of characters on current line */
  24.                 /* start at 1 to include colon */
  25.  
  26.     if (c == EOF)    /* flush buffer on EOF */
  27.     {
  28.     while (num_bytes != 0)
  29.         putchar_6(0);
  30.     count = 1; /* for next file */
  31.     return;
  32.     }
  33.  
  34.     buf[num_bytes++] = c;
  35.     if (num_bytes == 3)
  36.     {
  37.  
  38.     num_bytes = 0;
  39.     putchar(tr[buf[0] >> 2]);
  40.     if (count++ > MAXLINELEN)
  41.     {
  42.         count = 0;
  43.         putchar('\n');
  44.     }
  45.     putchar(tr[((buf[0] & 0x03) << 4) | (buf[1] >> 4)]);
  46.     if (count++ > MAXLINELEN)
  47.     {
  48.         count = 0;
  49.         putchar('\n');
  50.     }
  51.     putchar(tr[((buf[1] & 0x0F) << 2) | (buf[2] >> 6)]);
  52.     if (count++ > MAXLINELEN)
  53.     {
  54.         count = 0;
  55.         putchar('\n');
  56.     }
  57.     putchar(tr[buf[2] & 0x3F]);
  58.     if (count++ > MAXLINELEN)
  59.     {
  60.         count = 0;
  61.         putchar('\n');
  62.     }
  63.     }
  64. }
  65.