OSDN Git Service

* src/util.c (str_safe_copy): newly added.
[lha/lha.git] / src / huf.c
index df093a3..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 */ )
 {
@@ -338,7 +348,7 @@ read_pt_len(nn, nbit, i_special)
                     mask >>= 1;
                     c++;
                 }
-                fillbuf(c - 3);         /* fillbuf(c - 7 + 3); ??? */
+                fillbuf(c - 3);
             }
 
             pt_len[i++] = c;
@@ -471,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();