OSDN Git Service

* src/util.c (str_safe_copy): newly added.
[lha/lha.git] / src / huf.c
index 0a37043..2698500 100644 (file)
--- a/src/huf.c
+++ b/src/huf.c
@@ -9,8 +9,6 @@
 /* ------------------------------------------------------------------------ */
 #include "lha.h"
 
-#include <assert.h>
-
 #if HAVE_SYS_PARAM_H
 #include <sys/param.h>
 #endif
@@ -22,17 +20,28 @@ extern char *malloc ();
 #endif
 
 /* ------------------------------------------------------------------------ */
-unsigned short  left[2 * NC - 1], right[2 * NC - 1];
-unsigned char   c_len[NC], pt_len[NPT];
-unsigned short  c_freq[2 * NC - 1], c_table[4096], c_code[NC], p_freq[2 * NP - 1],
-                pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
-
-static unsigned char *buf;
-static unsigned int bufsiz;
-static unsigned short blocksize;
-static unsigned short output_pos, output_mask;
-static          int   pbit;
-static          int   np;
+unsigned short left[2 * NC - 1], right[2 * NC - 1];
+
+unsigned short c_code[NC];      /* encode */
+unsigned short pt_code[NPT];    /* encode */
+
+unsigned short c_table[4096];   /* decode */
+unsigned short pt_table[256];   /* decode */
+
+unsigned short c_freq[2 * NC - 1]; /* encode */
+unsigned short p_freq[2 * NP - 1]; /* encode */
+unsigned short t_freq[2 * NT - 1]; /* encode */
+
+unsigned char  c_len[NC];
+unsigned char  pt_len[NPT];
+
+static unsigned char *buf;      /* encode */
+static unsigned int bufsiz;     /* encode */
+static unsigned short blocksize; /* decode */
+static unsigned short output_pos, output_mask; /* encode */
+
+static int pbit;
+static int np;
 /* ------------------------------------------------------------------------ */
 /*                              Encording                                   */
 /* ------------------------------------------------------------------------ */
@@ -88,6 +97,7 @@ write_pt_len(n, nbit, i_special)
         if (k <= 6)
             putbits(3, k);
         else
+            /* k=7 -> 1110  k=8 -> 11110  k=9 -> 111110 ... */
             putbits(k - 3, USHRT_MAX << 1);
         if (i == i_special) {
             while (i < 6 && pt_len[i] == 0)
@@ -222,7 +232,7 @@ send_block( /* void */ )
 }
 
 /* ------------------------------------------------------------------------ */
-/* lh4, 5, 6 */
+/* lh4, 5, 6, 7 */
 void
 output_st1(c, p)
     unsigned short  c;
@@ -271,7 +281,7 @@ alloc_buf( /* void */ )
 }
 
 /* ------------------------------------------------------------------------ */
-/* lh4, 5, 6 */
+/* lh4, 5, 6, 7 */
 void
 encode_start_st1( /* void */ )
 {
@@ -283,9 +293,9 @@ encode_start_st1( /* void */ )
     case LZHUFF6_DICBIT: pbit = 5; np = LZHUFF6_DICBIT + 1; break;
     case LZHUFF7_DICBIT: pbit = 5; np = LZHUFF7_DICBIT + 1; break;
     default:
-        assert(0);
+        fatal_error("Cannot use %d bytes dictionary", 1 << dicbit);
     }
-        
+
     for (i = 0; i < NC; i++)
         c_freq[i] = 0;
     for (i = 0; i < np; i++)
@@ -297,7 +307,7 @@ encode_start_st1( /* void */ )
 }
 
 /* ------------------------------------------------------------------------ */
-/* lh4, 5, 6 */
+/* lh4, 5, 6, 7 */
 void
 encode_end_st1( /* void */ )
 {
@@ -329,15 +339,18 @@ read_pt_len(nn, nbit, i_special)
     else {
         i = 0;
         while (i < n) {
-            c = bitbuf >> (16 - 3);
-            if (c == 7) {
+            c = peekbits(3);
+            if (c != 7)
+                fillbuf(3);
+            else {
                 unsigned short  mask = 1 << (16 - 4);
                 while (mask & bitbuf) {
                     mask >>= 1;
                     c++;
                 }
+                fillbuf(c - 3);
             }
-            fillbuf((c < 7) ? 3 : c - 3);
+
             pt_len[i++] = c;
             if (i == i_special) {
                 c = getbits(2);
@@ -367,7 +380,7 @@ read_c_len( /* void */ )
     } else {
         i = 0;
         while (i < n) {
-            c = pt_table[bitbuf >> (16 - 8)];
+            c = pt_table[peekbits(8)];
             if (c >= NT) {
                 unsigned short  mask = 1 << (16 - 9);
                 do {
@@ -412,7 +425,7 @@ decode_c_st1( /*void*/ )
         read_pt_len(np, pbit, -1);
     }
     blocksize--;
-    j = c_table[bitbuf >> 4];
+    j = c_table[peekbits(12)];
     if (j < NC)
         fillbuf(c_len[j]);
     else {
@@ -437,7 +450,7 @@ decode_p_st1( /* void */ )
 {
     unsigned short  j, mask;
 
-    j = pt_table[bitbuf >> (16 - 8)];
+    j = pt_table[peekbits(8)];
     if (j < np)
         fillbuf(pt_len[j]);
     else {
@@ -468,7 +481,7 @@ decode_start_st1( /* void */ )
     case LZHUFF6_DICBIT: pbit = 5; np = LZHUFF6_DICBIT + 1; break;
     case LZHUFF7_DICBIT: pbit = 5; np = LZHUFF7_DICBIT + 1; break;
     default:
-        assert(0);
+        fatal_error("Cannot use %d bytes dictionary", 1 << dicbit);
     }
 
     init_getbits();