home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / flzh_rn.c < prev    next >
C/C++ Source or Header  |  1989-05-23  |  20KB  |  885 lines

  1. From ra!tut!draken!kth!mcvax!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!purdue!iuvax!bsu-cs!ibmbin Tue May 23 17:14:03 EET DST 1989
  2. Article 292 of comp.binaries.ibm.pc:
  3. Path: chyde!ra!tut!draken!kth!mcvax!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!purdue!iuvax!bsu-cs!ibmbin
  4. >From: nelson@sun.soe.clarkson.edu (Russ Nelson)
  5. Newsgroups: comp.binaries.ibm.pc
  6. Subject: v02i098: flzh_rn.c, faster lzhuf
  7. Summary: flzh_rn.c, faster lzhuf
  8. Message-ID: <7380@bsu-cs.bsu.edu>
  9. Date: 23 May 89 08:00:19 GMT
  10. Sender: ibmbin@bsu-cs.bsu.edu
  11. Followup-To: comp.binaries.ibm.pc.d
  12. Lines: 868
  13. Approved: dhesi@bsu-cs.bsu.edu
  14.  
  15. Checksum: 2850493429  (Verify with "brik -cv")
  16. Posting-number: Volume 02, Issue 098
  17. Submitted-by: Russ Nelson <nelson@sun.soe.clarkson.edu>
  18. Archive-name: flzh/flzh_rn.c
  19.  
  20. /*
  21. Here is another text posting.  It speaks for itself.  Just in case
  22. others come up with further revisions, I have named this one
  23. "flzh_rn.c" after "Faster LZHuf by Russ Nelson".   Note that I have
  24. added the copyright statement as requested by Kenji Rikitake in an
  25. earlier Usenet article.  There seems to be a compiler dependency here,
  26. because it won't execute correctly if compiled with Turbo C 1.0, but
  27. Russ Nelson tells me it does work with later versions of Turbo C,
  28. 1.5 or 2, but I don't remember which.  -- R.D
  29. */
  30.  
  31. #ifdef USE_ASM
  32. #pragma inline
  33. #endif
  34.  
  35. /*
  36. LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
  37. All rights reserved. Permission granted for non-commercial use.
  38. */
  39.  
  40. /*
  41.  * LZHUF.C English version 1.0
  42.  * Based on Japanese version 29-NOV-1988
  43.  * LZSS coded by Haruhiko OKUMURA
  44.  * Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
  45.  * Edited and translated to English by Kenji RIKITAKE
  46.  * Assembly language added by Russell Nelson (nelson@clutx.clarkson.edu)
  47.  *   Makes it 1.56 times faster in compression,
  48.  *   and 1.53 times faster in decompression.
  49.  * Warning!  If you change anything, verify that the register use doesn't
  50.  * change.
  51.  * Some C optimization added by Russell Nelson.
  52.  */
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <ctype.h>
  58.  
  59. /* These values are Turbo-C dependent;
  60.    EXIT_SUCCESS, EXIT_FAILURE
  61.    renamed by Kenji */
  62.  
  63. #define EXIT_OK 0
  64. #define EXIT_FAILED -1
  65.  
  66. FILE  *infile, *outfile;
  67. unsigned long int  textsize = 0, codesize = 0, printcount = 0;
  68.  
  69. void Error(char *message)
  70. {
  71.     printf("\n%s\n", message);
  72.     exit(EXIT_FAILED);
  73. }
  74.  
  75. /* LZSS Parameters */
  76.  
  77. #define N        4096    /* Size of string buffer */
  78. #define F        60    /* Size of look-ahead buffer */
  79. #define THRESHOLD    2
  80. #define NIL        N    /* End of tree's node  */
  81.  
  82. unsigned char
  83.         text_buf[N + F - 1];
  84. int        match_position, match_length,
  85.         lson[N + 1], rson[N + 257], dad[N + 1];
  86.  
  87. void InitTree(void)  /* Initializing tree */
  88. {
  89.     int  i;
  90.  
  91.     for (i = N + 1; i <= N + 256; i++)
  92.         rson[i] = NIL;            /* root */
  93.     for (i = 0; i < N; i++)
  94.         dad[i] = NIL;            /* node */
  95. }
  96.  
  97. void InsertNode(int r)  /* Inserting node to the tree */
  98. {
  99.     int  i, p, cmp;
  100.     unsigned char  *key, keychar;
  101.     unsigned c;
  102.  
  103.     cmp = 1;
  104.     key = &text_buf[r];
  105.     keychar = key[1];
  106.     p = N + 1 + key[0];
  107.     rson[r] = lson[r] = NIL;
  108.     match_length = 0;
  109.  
  110. #ifdef USE_ASM
  111.  
  112. /* for speed's sake, we use a bunch of hacks.  If you change this code, be
  113.  * sure to tcc -S it before you attempt to run it.  If you can't figure
  114.  * out what something's doing, look at the standard C version of it in the
  115.  * #else clause.
  116.  */
  117.  
  118. #define SF 0x1000            /* 8086 sign flag */
  119.  
  120. /* We're going to hold p in _SI.  Turbo C would ordinarily put it in a
  121.  * register for us, but it refuses to do so if it sees any mention of
  122.  * the register, either in a 'asm' statement or the _SI pseudovariable.
  123.  * When we actually use _SI, we push it first.
  124.  *
  125.  * Similarly for r in _DI.  Note that the algorithm doesn't change r.
  126.  */
  127.  
  128.     _SI = p;
  129. #define p _SI
  130.     _DI = r;
  131. #define r _DI
  132.  
  133.     _ES = _DS;            /* we're going to use cmpsb */
  134.     asm cld
  135.  
  136. /* many times the initial characters don't match, so we spend a fair amount
  137.  * of time in the following unstructured code.
  138.  */
  139.  
  140.     for ( ; ; ) {
  141.         if ((cmp & SF) == 0) {
  142. right:
  143.             asm mov bx,si
  144.             asm mov ax,rson[bx+si]
  145.             if (_AX != NIL) {
  146.                 asm mov si,ax;
  147.                 asm mov al,text_buf[si+1];
  148.                 asm cmp keychar,al;
  149.                 asm jg right;
  150.                 asm jl left;
  151.             } else {
  152.                 rson[p] = r;
  153.                 dad[r] = p;
  154.                 return;
  155.             }
  156.         } else {
  157. left:
  158.             asm mov bx,si
  159.             asm mov ax,lson[bx+si]
  160.             if (_AX != NIL) {
  161.                 asm mov si,ax;
  162.                 asm mov al,text_buf[si+1];
  163.                 asm cmp keychar,al;
  164.                 asm jg right;
  165.                 asm jl left;
  166.             } else {
  167.                 lson[p] = r;
  168.                 dad[r] = p;
  169.                 return;
  170.             }
  171.         }
  172. equal:
  173.         asm push si
  174.         asm push di
  175.         _DI = (unsigned) &text_buf[p+1];
  176.         _SI = (unsigned) &key[1];
  177.         _CX = F - 1;
  178. /* The semantics of cmpsb are not well understood.  Every comparison decrements
  179.  * _CX and bumps _SI and _DI.  If the values compared are equal and _CX <> 0
  180.  * then the cmpsb repeats.  Otherwise the flags are set to the result of the
  181.  * comparison.  The consequence of this is that the only way to determine
  182.  * whether the entire string was equal is to check the flags.  If the two
  183.  * strings are identical up to the last character, _CX will be zero
  184.  * whether or not the last characters match.
  185.  *
  186.  * The Microsoft Macro Assembler 5.0 Reference Booklet gets it wrong, even
  187.  * though Intel documents it very precisely and accurately.  Boo!  Hiss!
  188.  *
  189.  * If _CX is zero before the cmpsb, the flags are unchanged.  This affects
  190.  * the interpretation of zero length strings.  Are they equal or different?
  191.  * If you wish them to be equal, you can "or cx,cx".  If you wish them to
  192.  * be different you can "or sp,sp".  In a subroutine, sp is guaranteed to
  193.  * be nonzero.
  194.  */
  195.         asm    repe cmpsb    /* 7% of runtime is spent here! */
  196. /* remember the sign flag to see if it was larger or smaller */
  197.         asm lahf
  198.         cmp = _AX;
  199. /* if it matched, we want _CX to be zero */
  200.         asm je matched;
  201.         _CX++;
  202. matched:
  203.         i = F - _CX;
  204.         asm pop di;
  205.         asm pop si;
  206.         if (i > THRESHOLD) {
  207.             if (i > match_length) {
  208.                 match_position = ((r - p) & (N - 1)) - 1;
  209.                 if ((match_length = i) >= F)
  210.                     break;
  211.             }
  212.             if (i == match_length) {
  213.                 if (((r - p) & (N - 1)) - 1 < match_position) {
  214.                     match_position = _AX;
  215.                 }
  216.             }
  217.         }
  218.     }
  219. #else
  220.     for ( ; ; ) {
  221.         if (cmp >= 0) {
  222.             if (rson[p] != NIL)
  223.                 p = rson[p];
  224.             else {
  225.                 rson[p] = r;
  226.                 dad[r] = p;
  227.                 return;
  228.             }
  229.         } else {
  230.             if (lson[p] != NIL)
  231.                 p = lson[p];
  232.             else {
  233.                 lson[p] = r;
  234.                 dad[r] = p;
  235.                 return;
  236.             }
  237.         }
  238.         for (i = 1; i < F; i++)
  239.             if ((cmp = key[i] - text_buf[p + i]) != 0)
  240.                 break;
  241.         if (i > THRESHOLD) {
  242.             if (i > match_length) {
  243.                 match_position = ((r - p) & (N - 1)) - 1;
  244.                 if ((match_length = i) >= F)
  245.                     break;
  246.             }
  247.             if (i == match_length) {
  248.                 if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
  249.                     match_position = c;
  250.                 }
  251.             }
  252.         }
  253.     }
  254. #endif
  255.     dad[r] = dad[p];
  256.     lson[r] = lson[p];
  257.     rson[r] = rson[p];
  258.     dad[lson[p]] = r;
  259.     dad[rson[p]] = r;
  260.     if (rson[dad[p]] == p)
  261.         rson[dad[p]] = r;
  262.     else
  263.         lson[dad[p]] = r;
  264.     dad[p] = NIL;  /* remove p */
  265. #undef p
  266. #undef r
  267. }
  268.  
  269. void DeleteNode(int p)  /* Deleting node from the tree */
  270. {
  271.     int  q;
  272.  
  273.     if (dad[p] == NIL)
  274.         return;            /* unregistered */
  275.     if (rson[p] == NIL)
  276.         q = lson[p];
  277.     else
  278.     if (lson[p] == NIL)
  279.         q = rson[p];
  280.     else {
  281.         q = lson[p];
  282.         if (rson[q] != NIL) {
  283.             do {
  284.                 q = rson[q];
  285.             } while (rson[q] != NIL);
  286.             rson[dad[q]] = lson[q];
  287.             dad[lson[q]] = dad[q];
  288.             lson[q] = lson[p];
  289.             dad[lson[p]] = q;
  290.         }
  291.         rson[q] = rson[p];
  292.         dad[rson[p]] = q;
  293.     }
  294.     dad[q] = dad[p];
  295.     if (rson[dad[p]] == p)
  296.         rson[dad[p]] = q;
  297.     else
  298.         lson[dad[p]] = q;
  299.     dad[p] = NIL;
  300. }
  301.  
  302. /* Huffman coding parameters */
  303.  
  304. #define N_CHAR      (256 - THRESHOLD + F)
  305.                 /* character code (= 0..N_CHAR-1) */
  306. #define T         (N_CHAR * 2 - 1)    /* Size of table */
  307. #define R         (T - 1)            /* root position */
  308. #define MAX_FREQ    0x8000
  309.                     /* update when cumulative frequency */
  310.                     /* reaches to this value */
  311.  
  312. typedef unsigned char uchar;
  313.  
  314. /*
  315.  * Tables for encoding/decoding upper 6 bits of
  316.  * sliding dictionary pointer
  317.  */
  318. /* encoder table */
  319. uchar p_len[64] = {
  320.     0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
  321.     0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
  322.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  323.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  324.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  325.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  326.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  327.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
  328. };
  329.  
  330. uchar p_code[64] = {
  331.     0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
  332.     0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
  333.     0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
  334.     0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
  335.     0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
  336.     0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
  337.     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  338.     0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
  339. };
  340.  
  341. /* decoder table */
  342. uchar d_code[256] = {
  343.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  344.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  345.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  346.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  347.     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  348.     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  349.     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  350.     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  351.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  352.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  353.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  354.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  355.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  356.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  357.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  358.     0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  359.     0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
  360.     0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
  361.     0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
  362.     0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
  363.     0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
  364.     0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
  365.     0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
  366.     0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
  367.     0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
  368.     0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
  369.     0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
  370.     0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
  371.     0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
  372.     0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
  373.     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  374.     0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  375. };
  376.  
  377. uchar d_len[256] = {
  378.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  379.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  380.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  381.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  382.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  383.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  384.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  385.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  386.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  387.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  388.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  389.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  390.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  391.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  392.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  393.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  394.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  395.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  396.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  397.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  398.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  399.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  400.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  401.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  402.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  403.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  404.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  405.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  406.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  407.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  408.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  409.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  410. };
  411.  
  412. unsigned freq[T + 1];    /* cumulative freq table */
  413.  
  414. /*
  415.  * pointing parent nodes.
  416.  * area [T..(T + N_CHAR - 1)] are pointers for leaves
  417.  */
  418. int prnt[T + N_CHAR];
  419.  
  420. /* pointing children nodes (son[], son[] + 1)*/
  421. int son[T];
  422.  
  423. unsigned getbuf = 0;
  424. uchar getlen = 0;
  425.  
  426. int GetBit(void)    /* get one bit */
  427. {
  428.     int i;
  429.  
  430.     while (getlen <= 8) {
  431.         if ((i = getc(infile)) < 0) i = 0;
  432.         getbuf |= i << (8 - getlen);
  433.         getlen += 8;
  434.     }
  435.     i = getbuf;
  436.     getbuf <<= 1;
  437.     getlen--;
  438.     return (i < 0);
  439. }
  440.  
  441. int GetByte(void)    /* get a byte */
  442. {
  443.     unsigned i;
  444.  
  445.     while (getlen <= 8) {
  446.         if ((i = getc(infile)) < 0) i = 0;
  447.         getbuf |= i << (8 - getlen);
  448.         getlen += 8;
  449.     }
  450. #ifdef USE_ASM
  451.     _AX = *(((unsigned char *)&getbuf)+1);
  452.     _BX = getbuf;
  453.     _BH = _BL;
  454.     _BL = 0;
  455.     asm mov getbuf,bx;
  456.      getlen -= 8;
  457.     return _AX;
  458. #else
  459.     i = getbuf;
  460.     getbuf <<= 8;
  461.     getlen -= 8;
  462.     return i >> 8;
  463. #endif
  464. }
  465.  
  466. unsigned putbuf = 0;
  467. uchar putlen = 0;
  468.  
  469. void Putcode(int l, unsigned c)        /* output c bits */
  470. {
  471.     putbuf |= c >> putlen;
  472.     if ((putlen += l) >= 8) {
  473.         putc(putbuf >> 8, outfile);
  474.         if ((putlen -= 8) >= 8) {
  475.             putc(putbuf, outfile);
  476.             codesize += 2;
  477.             putlen -= 8;
  478.             putbuf = c << (l - putlen);
  479.         } else {
  480.             putbuf <<= 8;
  481.             codesize++;
  482.         }
  483.     }
  484. }
  485.  
  486.  
  487. /* initialize freq tree */
  488.  
  489. void StartHuff()
  490. {
  491.     int i, j;
  492.  
  493.     for (i = 0; i < N_CHAR; i++) {
  494.         freq[i] = 1;
  495.         son[i] = i + T;
  496.         prnt[i + T] = i;
  497.     }
  498.     i = 0; j = N_CHAR;
  499.     while (j <= R) {
  500.         freq[j] = freq[i] + freq[i + 1];
  501.         son[j] = i;
  502.         prnt[i] = prnt[i + 1] = j;
  503.         i += 2; j++;
  504.     }
  505.     freq[T] = 0xffff;
  506.     prnt[R] = 0;
  507. }
  508.  
  509.  
  510. /* reconstruct freq tree */
  511.  
  512. void reconst()
  513. {
  514.     int i, j, k;
  515.     unsigned f, l;
  516.  
  517.     /* halven cumulative freq for leaf nodes */
  518.     j = 0;
  519.     for (i = 0; i < T; i++) {
  520.         if (son[i] >= T) {
  521.             freq[j] = (freq[i] + 1) / 2;
  522.             son[j] = son[i];
  523.             j++;
  524.         }
  525.     }
  526.     /* make a tree : first, connect children nodes */
  527.     for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
  528.         k = i + 1;
  529.         f = freq[j] = freq[i] + freq[k];
  530.         for (k = j - 1; f < freq[k]; k--);
  531.         k++;
  532.         l = (j - k) * 2;
  533.         (void)memmove(&freq[k + 1], &freq[k], l);
  534.         freq[k] = f;
  535.         (void)memmove(&son[k + 1], &son[k], l);
  536.         son[k] = i;
  537.     }
  538.     /* connect parent nodes */
  539.     for (i = 0; i < T; i++) {
  540.         if ((k = son[i]) >= T) {
  541.             prnt[k] = i;
  542.         } else {
  543.             prnt[k] = prnt[k + 1] = i;
  544.         }
  545.     }
  546. }
  547.  
  548.  
  549. /* update freq tree */
  550.  
  551. void update(int c)
  552. {
  553.     register int k, l;
  554.     int i, j;
  555.  
  556.     if (freq[R] == MAX_FREQ) {
  557.         reconst();
  558.     }
  559. #ifdef USE_ASM
  560. #define k _DX                /* _DX is safe to use. */
  561.     _SI = prnt[c + T];
  562. #define    c _SI
  563.     do {
  564.     more_k:
  565.         k = ++freq[c];
  566.         asm    cmp    dx,word ptr DGROUP:_freq+2[bx];
  567.         asm    ja    start;
  568.         asm    mov    si,word ptr DGROUP:_prnt[bx];
  569.         asm    or    si,si;
  570.         asm    jne    more_k;
  571.         break;
  572.     start:
  573.         _BX = (unsigned)&freq[c+1];
  574.     again:
  575.         asm cmp dx,[bx]
  576.         asm jbe done
  577.         _BX += 4;
  578.         asm cmp dx,[bx-2]
  579.         asm ja again
  580.         _BX -= 2;
  581.     done:
  582.         _BX -= (unsigned) &freq;
  583.         l = _BX >> 1;
  584. #else
  585.     c = prnt[c + T];
  586.     do {
  587.         /* keep the outer loop together so stupid compilers
  588.          * can optimize.
  589.          */
  590.         do {
  591.             k = ++freq[c];
  592.             /* swap nodes to keep the tree freq-ordered */
  593.             if (k > freq[c + 1]) goto start;
  594.         } while ((c = prnt[c]) != 0);
  595.         break;
  596.     start:
  597.         l = c + 1;
  598.         /* this is the inner loop -- unroll it a few times */
  599.         while (k > freq[++l] &&
  600.                k > freq[++l] &&
  601.                k > freq[++l]);
  602. #endif
  603.         l--;
  604.         freq[c] = freq[l];
  605.         freq[l] = k;
  606.  
  607.         i = son[c];
  608.         prnt[i] = l;
  609.         if (i < T) prnt[i + 1] = l;
  610.  
  611.         j = son[l];
  612.         son[l] = i;
  613.  
  614.         prnt[j] = c;
  615.         if (j < T) prnt[j + 1] = c;
  616.         son[c] = j;
  617.  
  618.         c = l;
  619.     } while ((c = prnt[c]) != 0);    /* do it until reaching the root */
  620. #undef k
  621. #undef c
  622. }
  623.  
  624. unsigned code, len;
  625.  
  626. void EncodeChar(unsigned c)
  627. {
  628.     unsigned i;
  629.     int j, k;
  630.  
  631.     i = 0;
  632.     j = 0;
  633.     k = prnt[c + T];
  634.  
  635.     /* search connections from leaf node to the root */
  636.     do {
  637.         i >>= 1;
  638.  
  639.         /*
  640.         if node's address is odd, output 1
  641.         else output 0
  642.         */
  643.         if (k & 1) i += 0x8000;
  644.  
  645.         j++;
  646.     } while ((k = prnt[k]) != R);
  647.     Putcode(j, i);
  648.     code = i;
  649.     len = j;
  650.     update(c);
  651. }
  652.  
  653. void EncodePosition(unsigned c)
  654. {
  655.     unsigned i;
  656.  
  657.     /* output upper 6 bits with encoding */
  658.     i = c >> 6;
  659.     Putcode(p_len[i], (unsigned)p_code[i] << 8);
  660.  
  661.     /* output lower 6 bits directly */
  662.     Putcode(6, (c & 0x3f) << 10);
  663. }
  664.  
  665. void EncodeEnd()
  666. {
  667.     if (putlen) {
  668.         putc(putbuf >> 8, outfile);
  669.         codesize++;
  670.     }
  671. }
  672.  
  673. int DecodeChar()
  674. {
  675.     unsigned c;
  676.     c = son[R];
  677.  
  678.     /*
  679.      * start searching tree from the root to leaves.
  680.      * choose node #(son[]) if input bit == 0
  681.      * else choose #(son[]+1) (input bit == 1)
  682.      */
  683.     while (c < T) {
  684.         if(getlen){
  685.             getlen--;
  686. #ifdef USE_ASM
  687.             getbuf<<=1;
  688.             asm jnc zerobit;
  689.             c++;
  690.         zerobit:;
  691. #else
  692.             if (getbuf < 0)
  693.                 c++;
  694.             getbuf<<=1;
  695. #endif
  696.         } else
  697.             c += GetBit();
  698.         c = son[c];
  699.     }
  700.     c -= T;
  701.     update(c);
  702.     return c;
  703. }
  704.  
  705. int DecodePosition()
  706. {
  707.     unsigned i, j, c;
  708.  
  709.     /* decode upper 6 bits from given table */
  710.     i = GetByte();
  711.     c = (unsigned)d_code[i] << 6;
  712.     j = d_len[i];
  713.  
  714.     /* input lower 6 bits directly */
  715.     j -= 2;
  716.     while (j--) {
  717.         i <<= 1;
  718.         if(getlen){
  719.             getlen--;
  720. #ifdef USE_ASM
  721.             getbuf<<=1;
  722.             asm jnc zerobit;
  723.             i++;
  724.         zerobit:;
  725. #else
  726.             if (getbuf < 0)
  727.                 i++;
  728.             getbuf<<=1;
  729. #endif
  730.         } else
  731.             i += GetBit();
  732.     }
  733.     return c | i & 0x3f;
  734. }
  735.  
  736. /* Compression */
  737.  
  738. void Encode(void)  /* Encoding/Compressing */
  739. {
  740.     int  i, c, len, r, s, last_match_length;
  741.  
  742.     fseek(infile, 0L, 2);
  743.     textsize = ftell(infile);
  744.     if (fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
  745.         Error("Unable to write");    /* write size of original text */
  746.     if (textsize == 0)
  747.         return;
  748.     rewind(infile);
  749.     textsize = 0;            /* rewind and rescan */
  750.     StartHuff();
  751.     InitTree();
  752.     s = 0;
  753.     r = N - F;
  754.     for (i = s; i < r; i++)
  755.         text_buf[i] = ' ';
  756.     for (len = 0; len < F && (c = getc(infile)) != EOF; len++)
  757.         text_buf[r + len] = c;
  758.     textsize = len;
  759.     for (i = 1; i <= F; i++)
  760.         InsertNode(r - i);
  761.     InsertNode(r);
  762.     do {
  763.         if (match_length > len)
  764.             match_length = len;
  765.         if (match_length <= THRESHOLD) {
  766.             match_length = 1;
  767.             EncodeChar(text_buf[r]);
  768.         } else {
  769.             EncodeChar(255 - THRESHOLD + match_length);
  770.             EncodePosition(match_position);
  771.         }
  772.         last_match_length = match_length;
  773.         for (i = 0; i < last_match_length &&
  774.                 (c = getc(infile)) != EOF; i++) {
  775.             DeleteNode(s);
  776.             text_buf[s] = c;
  777.             if (s < F - 1)
  778.                 text_buf[s + N] = c;
  779.             s = (s + 1) & (N - 1);
  780.             r = (r + 1) & (N - 1);
  781.             InsertNode(r);
  782.         }
  783.         if ((textsize += i) > printcount) {
  784.             printf("%12ld\r", textsize);
  785.             printcount += 1024;
  786.         }
  787.         while (i++ < last_match_length) {
  788.             DeleteNode(s);
  789.             s = (s + 1) & (N - 1);
  790.             r = (r + 1) & (N - 1);
  791.             if (--len) InsertNode(r);
  792.         }
  793.     } while (len > 0);
  794.     EncodeEnd();
  795.     printf("input: %ld bytes\n", textsize);
  796.     printf("output: %ld bytes\n", codesize);
  797.     printf("output/input: %.3f\n", (double)codesize / textsize);
  798. }
  799.  
  800. void Decode(void)  /* Decoding/Uncompressing */
  801. {
  802.     int  i, j, k, r, c;
  803.     unsigned long int  count;
  804.  
  805.     if (fread(&textsize, sizeof textsize, 1, infile) < 1)
  806.         Error("Unable to read");  /* read size of original text */
  807.     if (textsize == 0)
  808.         return;
  809.     StartHuff();
  810.     for (i = 0; i < N - F; i++)
  811.         text_buf[i] = ' ';
  812.     r = N - F;
  813.     for (count = 0; count < textsize; ) {
  814.         c = DecodeChar();
  815.         if (c < 256) {
  816.             putc(c, outfile);
  817.             text_buf[r++] = c;
  818.             r &= (N - 1);
  819.             count++;
  820.         } else {
  821.             i = (r - DecodePosition() - 1) & (N - 1);
  822.             j = c - 255 + THRESHOLD;
  823.             if (r + j < N
  824.              && i + j < N
  825.              && (i + j <= r || i >= r)
  826. #ifdef __TURBOC__
  827.              && outfile->level < -j){
  828.                 memcpy(outfile->curp,
  829.                         memmove(&text_buf[r],&text_buf[i], j),
  830.                     j);
  831.                 outfile->curp += j;
  832.                 outfile->level += j;
  833. #else
  834.              ){
  835.                 fwrite(memcpy(&text_buf[r],&text_buf[i], j),
  836.                     1, j, outfile);
  837. #endif
  838.                 r += j;
  839.                 count += j;
  840.             } else
  841.  
  842.             for (k = i, j += i; k < j; k++) {
  843.                 c = text_buf[k & (N - 1)];
  844.                 putc(c, outfile);
  845.                 text_buf[r++] = c;
  846.                 r &= (N - 1);
  847.                 count++;
  848.             }
  849.         }
  850.         if (count > printcount) {
  851.             printf("%12ld\r", count);
  852.             printcount += 4096;
  853.         }
  854.     }
  855.     printf("%12ld\n", count);
  856. }
  857.  
  858. int main(int argc, char *argv[])
  859. {
  860.     char  *s;
  861.  
  862.     if (argc != 4) {
  863.         printf("Usage:lzhuf e(compression)|d(uncompression)"
  864.             " infile outfile\n");
  865.         return EXIT_FAILED;
  866.     }
  867.     if ((s = argv[1], s[1] || strpbrk(s, "DEde") == NULL)
  868.      || (s = argv[2], (infile  = fopen(s, "rb")) == NULL)
  869.      || (s = argv[3], (outfile = fopen(s, "wb")) == NULL)) {
  870.         printf("Trouble with arg %s\n", s);
  871.         return EXIT_FAILED;
  872.     }
  873.     setvbuf(outfile, NULL, _IOFBF, 1<<12);
  874.     setvbuf(infile, NULL, _IOFBF, 1<<12);
  875.     if (toupper(*argv[1]) == 'E')
  876.         Encode();
  877.     else
  878.         Decode();
  879.     fclose(infile);
  880.     fclose(outfile);
  881.     return EXIT_OK;
  882. }
  883.  
  884.  
  885.