home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / lha100bt.zip / lha-1.00 / src / shuf.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  3KB  |  172 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(void)
  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(void)
  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(void)  /* 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(void)  /* 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(void)
  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(void)
  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.         } else {
  133.             ready_made(1);
  134.         }
  135.         make_table(NP, pt_len, 8, pt_table);
  136.     }
  137.     blocksize--;
  138.     j = c_table[bitbuf >> 4];
  139.     if (j < N1) fillbuf(c_len[j]);
  140.     else {
  141.         fillbuf(12); i = bitbuf;
  142.         do {
  143.             if ((short)i < 0) j = right[j];
  144.             else              j = left [j];
  145.             i <<= 1;
  146.         } while (j >= N1);
  147.         fillbuf(c_len[j] - 12);
  148.     }
  149.     if (j == N1 - 1)
  150.         j += getbits(EXTRABITS);
  151.     return j;
  152. }
  153.  
  154. unsigned short decode_p_st0(void)
  155. {
  156.     int i, j;
  157.  
  158.     j = pt_table[bitbuf >> 8];
  159.     if (j < np) {
  160.         fillbuf(pt_len[j]);
  161.     } else {
  162.         fillbuf(8); i = bitbuf;
  163.         do {
  164.             if ((short)i < 0) j = right[j];
  165.             else              j = left [j];
  166.             i <<= 1;
  167.         } while (j >= np);
  168.         fillbuf(pt_len[j] - 8);
  169.     }
  170.     return (j << 6) + getbits(6);
  171. }
  172.