home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / ARCHIVERS / lha208src.lzh / LHA / SRC / shuf.c < prev    next >
Text File  |  1994-02-14  |  4KB  |  178 lines

  1. /***********************************************************
  2.     shuf.c -- extract static Huffman coding
  3. ***********************************************************/
  4. #include <stdio.h>
  5. #include "slidehuf.h"
  6. #ifdef __STDC__
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. #define N1 286  /* alphabet size */
  11. #define N2 (2 * N1 - 1)  /* # of nodes in Huffman tree */
  12. #define EXTRABITS 8
  13. /* >= log2(F-THRESHOLD+258-N1) */
  14. #define BUFBITS  16  /* >= log2(MAXBUF) */
  15. #define LENFIELD  4  /* bit size of length field for tree output */
  16. #define NP (8 * 1024 / 64)
  17. #define NP2 (NP * 2 - 1)
  18.  
  19. static unsigned int np;
  20.  
  21. void decode_start_st0()
  22. {
  23.     n_max = 286;
  24.     maxmatch = MAXMATCH;
  25.     init_getbits();
  26.     np = 1 << (MAX_DICBIT - 6);
  27. }
  28.  
  29. void encode_p_st0(j)
  30. unsigned short j;
  31. {
  32.     unsigned short i;
  33.  
  34.     i = j >> 6;
  35.     putcode(pt_len[i], pt_code[i]);
  36.     putbits(6, j & 0x3f);
  37. }
  38.  
  39. int fixed[2][16] = {
  40.     {3, 0x01, 0x04, 0x0c, 0x18, 0x30, 0},               /* old compatible */
  41.     {2, 0x01, 0x01, 0x03, 0x06, 0x0D, 0x1F, 0x4E, 0}    /* 8K buf */
  42. };
  43.  
  44. static void ready_made(method)
  45. int method;
  46. {
  47.     int i, j;
  48.     unsigned int code, weight;
  49.     int *tbl;
  50.  
  51.     tbl = fixed[method];
  52.     j = *tbl++;
  53.     weight = 1 << (16 - j);
  54.     code = 0; 
  55.     for (i = 0; i < np; i++) {
  56.         while (*tbl == i) {
  57.             j++;
  58.             tbl++;
  59.             weight >>= 1;
  60.         }
  61.         pt_len[i] = j;
  62.         pt_code[i] = code;
  63.         code += weight;
  64.     }
  65. }
  66.  
  67. void encode_start_fix()
  68. {
  69.     n_max = 314;
  70.     maxmatch = 60;
  71.     np = 1 << (12 - 6);
  72.     init_putbits();
  73.     start_c_dyn();
  74.     ready_made(0);
  75. }
  76.  
  77. static void read_tree_c()  /* read tree from file */
  78. {
  79.     int i, c;
  80.  
  81.     i = 0;
  82.     while (i < N1) {
  83.         if (getbits(1)) c_len[i] = getbits(LENFIELD) + 1;
  84.         else            c_len[i] = 0;
  85.         if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1) {
  86.             c = getbits(CBIT);
  87.             for (i = 0; i < N1; i++) c_len[i] = 0;
  88.             for (i = 0; i < 4096; i++) c_table[i] = c;
  89.             return;
  90.         }
  91.     }
  92.     make_table(N1, c_len, 12, c_table);
  93. }
  94.  
  95. static void read_tree_p()  /* read tree from file */
  96. {
  97.     int i, c;
  98.  
  99.     i = 0;
  100.     while (i < NP) {
  101.         pt_len[i] = getbits(LENFIELD);
  102.         if (++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1) {
  103.             c = getbits(MAX_DICBIT - 6);
  104.             for (i = 0; i < NP; i++) c_len[i] = 0;
  105.             for (i = 0; i < 256; i++) c_table[i] = c;
  106.             return;
  107.         }
  108.     }
  109. }
  110.  
  111. void decode_start_fix()
  112. {
  113.     n_max = 314;
  114.     maxmatch = 60;
  115.     init_getbits();
  116.     np = 1 << (12 - 6);
  117.     start_c_dyn();
  118.     ready_made(0);
  119.     make_table(np, pt_len, 8, pt_table);
  120. }
  121.  
  122. unsigned short decode_c_st0()
  123. {
  124.     int i, j;
  125.     static unsigned short blocksize = 0;
  126.  
  127.     if (blocksize == 0) {  /* read block head */
  128.         blocksize = getbits(BUFBITS);  /* read block blocksize */
  129.         read_tree_c();
  130.         if (getbits(1)) {
  131.             read_tree_p();
  132.         } 
  133.         else {
  134.             ready_made(1);
  135.         }
  136.         make_table(NP, pt_len, 8, pt_table);
  137.     }
  138.     blocksize--;
  139.     j = c_table[bitbuf >> 4];
  140.     if (j < N1) fillbuf(c_len[j]);
  141.     else {
  142.         fillbuf(12); 
  143.         i = bitbuf;
  144.         do {
  145.             if ((short)i < 0) j = right[j];
  146.             else              j = left [j];
  147.             i <<= 1;
  148.         } 
  149.         while (j >= N1);
  150.         fillbuf(c_len[j] - 12);
  151.     }
  152.     if (j == N1 - 1)
  153.         j += getbits(EXTRABITS);
  154.     return j;
  155. }
  156.  
  157. unsigned short decode_p_st0()
  158. {
  159.     int i, j;
  160.  
  161.     j = pt_table[bitbuf >> 8];
  162.     if (j < np) {
  163.         fillbuf(pt_len[j]);
  164.     } 
  165.     else {
  166.         fillbuf(8); 
  167.         i = bitbuf;
  168.         do {
  169.             if ((short)i < 0) j = right[j];
  170.             else              j = left [j];
  171.             i <<= 1;
  172.         } 
  173.         while (j >= np);
  174.         fillbuf(pt_len[j] - 8);
  175.     }
  176.     return (j << 6) + getbits(6);
  177. }
  178.