home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / BINHEX / dofile.c < prev    next >
Text File  |  1996-02-02  |  3KB  |  197 lines

  1. #ifdef OSK
  2. #include <stdio.h>
  3. #endif
  4.  
  5. #include "../fileio/machdr.h"
  6. #include "../fileio/rdfile.h"
  7.  
  8. extern int dorep;
  9. extern unsigned long binhex_crcinit;
  10. extern unsigned long binhex_updcrc();
  11.  
  12. #define RUNCHAR 0x90
  13.  
  14. static int pos_ptr;
  15. static char codes[] =
  16.     "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
  17. static int state;
  18. static int savebits;
  19. static int rep_char;
  20. static int rep_count;
  21.  
  22. void doheader();
  23. void dofork();
  24. void outbyte();
  25. void finish();
  26. void outbyte1();
  27. void out6bit();
  28. void outchar();
  29.  
  30. void dofile()
  31. {
  32.     (void)printf("(This file must be converted with BinHex 4.0)\n");
  33.     (void)printf("\n");
  34.     pos_ptr = 1;
  35.     state = 0;
  36.     rep_char = -1;
  37.     rep_count = 0;
  38.     outchar(':');
  39.     doheader();
  40.     dofork(data_fork, data_size);
  41.     dofork(rsrc_fork, rsrc_size);
  42.     finish();
  43.     (void)putchar(':');
  44.     (void)putchar('\n');
  45. }
  46.  
  47. void doheader()
  48. {
  49. unsigned long crc;
  50. int i, n;
  51.  
  52.     crc = binhex_crcinit;
  53.     n = file_info[I_NAMEOFF];
  54.     crc = binhex_updcrc(crc, file_info + I_NAMEOFF, n + 1);
  55.     for(i = 0; i <= n; i++) {
  56.     outbyte(file_info[I_NAMEOFF + i]);
  57.     }
  58.     n = 0;
  59.     crc = binhex_updcrc(crc, (char *)&n, 1);
  60.     outbyte(0);
  61.     crc = binhex_updcrc(crc, file_info + I_TYPEOFF, 4);
  62.     for(i = 0; i < 4; i++) {
  63.     outbyte(file_info[I_TYPEOFF + i]);
  64.     }
  65.     crc = binhex_updcrc(crc, file_info + I_AUTHOFF, 4);
  66.     for(i = 0; i < 4; i++) {
  67.     outbyte(file_info[I_AUTHOFF + i]);
  68.     }
  69.     crc = binhex_updcrc(crc, file_info + I_FLAGOFF, 2);
  70.     for(i = 0; i < 2; i++) {
  71.     outbyte(file_info[I_FLAGOFF + i]);
  72.     }
  73.     crc = binhex_updcrc(crc, file_info + I_DLENOFF, 4);
  74.     for(i = 0; i < 4; i++) {
  75.     outbyte(file_info[I_DLENOFF + i]);
  76.     }
  77.     crc = binhex_updcrc(crc, file_info + I_RLENOFF, 4);
  78.     for(i = 0; i < 4; i++) {
  79.     outbyte(file_info[I_RLENOFF + i]);
  80.     }
  81.     outbyte((int)(crc >> 8));
  82.     outbyte((int)(crc & 0xff));
  83. }
  84.  
  85. void dofork(fork, size)
  86. char *fork;
  87. int size;
  88. {
  89. unsigned long crc;
  90. int i;
  91.  
  92.     crc = binhex_updcrc(binhex_crcinit, fork, size);
  93.     for(i = 0; i < size; i++) {
  94.     outbyte(fork[i]);
  95.     }
  96.     outbyte((int)(crc >> 8));
  97.     outbyte((int)(crc & 0xff));
  98. }
  99.  
  100. void outbyte(b)
  101. int b;
  102. {
  103.     b &= 0xff;
  104.     if(dorep && (b == rep_char)) {
  105.     if(++rep_count == 254) {
  106.         outbyte1(RUNCHAR);
  107.         outbyte1(255);
  108.         rep_char = -1;
  109.         rep_count = 0;
  110.     }
  111.     } else {
  112.     if(rep_count > 0) {
  113.         if(rep_count > 3) {
  114.         outbyte1(RUNCHAR);
  115.         outbyte1(rep_count + 1);
  116.         } else {
  117.         while(rep_count-- > 0) {
  118.             outbyte1(rep_char);
  119.         }
  120.         }
  121.     }
  122.     outbyte1(b);
  123.     if(b == RUNCHAR) {
  124.         outbyte1(0);
  125.         rep_char = -1;
  126.     } else {
  127.         rep_char = b;
  128.     }
  129.     rep_count = 0;
  130.     }
  131. }
  132.  
  133. void finish()
  134. {
  135.     if(rep_count > 0) {
  136.     if(rep_count > 3) {
  137.         outbyte1(RUNCHAR);
  138.         outbyte1(rep_count + 1);
  139.     } else {
  140.         while(rep_count-- > 0) {
  141.         outbyte1(rep_char);
  142.         }
  143.     }
  144.     }
  145.     switch(state) {
  146.     case 1:
  147.     out6bit(savebits << 4);
  148.     break;
  149.     case 2:
  150.     out6bit(savebits << 2);
  151.     break;
  152.     default:
  153.     break;
  154.     }
  155. }
  156.  
  157. void outbyte1(b)
  158. int b;
  159. {
  160.     switch(state) {
  161.     case 0:
  162.     out6bit(b >> 2);
  163.     savebits = b & 0x3;
  164.     state = 1;
  165.     break;
  166.     case 1:
  167.     b |= (savebits << 8);
  168.     out6bit(b >> 4);
  169.     savebits = b & 0xf;
  170.     state = 2;
  171.     break;
  172.     case 2:
  173.     b |= (savebits << 8);
  174.     out6bit(b >> 6);
  175.     out6bit(b & 0x3f);
  176.     state = 0;
  177.     break;
  178.     }
  179. }
  180.  
  181. void out6bit(c)
  182. char c;
  183. {
  184.     outchar(codes[c & 0x3f]);
  185. }
  186.  
  187. void outchar(c)
  188. char c;
  189. {
  190.     (void)putchar(c);
  191.     if(++pos_ptr > 64) {
  192.     (void)putchar('\n');
  193.     pos_ptr = 1;
  194.     }
  195. }
  196.  
  197.