OSDN Git Service

* src/dhuf.c (decode_start_dyn): call init_code_cache().
[lha/lha.git] / src / shuf.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                                                                                         */
3 /*                              shuf.c -- extract static Huffman coding                                         */
4 /*                                                                                                                                                      */
5 /*              Modified                        Nobutaka Watazaki                                                       */
6 /*                                                                                                                                                      */
7 /*      Ver. 1.14       Source All chagned                              1995.01.14      N.Watazaki              */
8 /* ------------------------------------------------------------------------ */
9 #include "lha.h"
10
11 /* ------------------------------------------------------------------------ */
12 #undef NP
13 #undef NP2
14
15 #define NP                      (8 * 1024 / 64)
16 #define NP2                     (NP * 2 - 1)
17 /* ------------------------------------------------------------------------ */
18 static unsigned int np;
19 int             fixed[2][16] = {
20         {3, 0x01, 0x04, 0x0c, 0x18, 0x30, 0},   /* old compatible */
21         {2, 0x01, 0x01, 0x03, 0x06, 0x0D, 0x1F, 0x4E, 0}        /* 8K buf */
22 };
23 /* ------------------------------------------------------------------------ */
24 void
25 decode_start_st0( /*void*/ )
26 {
27         n_max = 286;
28         maxmatch = MAXMATCH;
29         init_getbits();
30     init_code_cache();
31 #ifdef SUPPORT_LH7
32         np = 1 << (MAX_DICBIT - 7);
33 #endif
34 #ifndef SUPPORT_LH7
35         np = 1 << (MAX_DICBIT - 6);
36 #endif
37
38 }
39
40 /* ------------------------------------------------------------------------ */
41 void
42 encode_p_st0(j)
43         unsigned short  j;
44 {
45         unsigned short  i;
46
47         i = j >> 6;
48         putcode(pt_len[i], pt_code[i]);
49         putbits(6, j & 0x3f);
50 }
51
52 /* ------------------------------------------------------------------------ */
53 static void
54 ready_made(method)
55         int             method;
56 {
57         int             i, j;
58         unsigned int    code, weight;
59         int            *tbl;
60
61         tbl = fixed[method];
62         j = *tbl++;
63         weight = 1 << (16 - j);
64         code = 0;
65         for (i = 0; i < np; i++) {
66                 while (*tbl == i) {
67                         j++;
68                         tbl++;
69                         weight >>= 1;
70                 }
71                 pt_len[i] = j;
72                 pt_code[i] = code;
73                 code += weight;
74         }
75 }
76
77 /* ------------------------------------------------------------------------ */
78 void
79 encode_start_fix( /*void*/ )
80 {
81         n_max = 314;
82         maxmatch = 60;
83         np = 1 << (12 - 6);
84         init_putbits();
85     init_code_cache();
86         start_c_dyn();
87         ready_made(0);
88 }
89
90 /* ------------------------------------------------------------------------ */
91 static void
92 read_tree_c( /*void*/ )
93 {                               /* read tree from file */
94         int             i, c;
95
96         i = 0;
97         while (i < N1) {
98                 if (getbits(1))
99                         c_len[i] = getbits(LENFIELD) + 1;
100                 else
101                         c_len[i] = 0;
102                 if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1) {
103                         c = getbits(CBIT);
104                         for (i = 0; i < N1; i++)
105                                 c_len[i] = 0;
106                         for (i = 0; i < 4096; i++)
107                                 c_table[i] = c;
108                         return;
109                 }
110         }
111         make_table(N1, c_len, 12, c_table);
112 }
113
114 /* ------------------------------------------------------------------------ */
115 static void
116 read_tree_p(/*void*/)
117 {                               /* read tree from file */
118         int             i, c;
119
120         i = 0;
121         while (i < NP) {
122                 pt_len[i] = getbits(LENFIELD);
123                 if (++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1) {
124                         c = getbits(LZHUFF3_DICBIT - 6);
125                         for (i = 0; i < NP; i++)
126                                 pt_len[i] = 0;
127                         for (i = 0; i < 256; i++)
128                                 pt_table[i] = c;
129                         return;
130                 }
131         }
132 }
133
134 /* ------------------------------------------------------------------------ */
135 void
136 decode_start_fix(/*void*/)
137 {
138         n_max = 314;
139         maxmatch = 60;
140         init_getbits();
141     init_code_cache();
142         np = 1 << (LZHUFF1_DICBIT - 6);
143         start_c_dyn();
144         ready_made(0);
145         make_table(np, pt_len, 8, pt_table);
146 }
147
148 /* ------------------------------------------------------------------------ */
149 unsigned short
150 decode_c_st0(/*void*/)
151 {
152         int             i, j;
153         static unsigned short blocksize = 0;
154
155         if (blocksize == 0) {   /* read block head */
156                 blocksize = getbits(BUFBITS);   /* read block blocksize */
157                 read_tree_c();
158                 if (getbits(1)) {
159                         read_tree_p();
160                 }
161                 else {
162                         ready_made(1);
163                 }
164                 make_table(NP, pt_len, 8, pt_table);
165         }
166         blocksize--;
167         j = c_table[bitbuf >> 4];
168         if (j < N1)
169                 fillbuf(c_len[j]);
170         else {
171                 fillbuf(12);
172                 i = bitbuf;
173                 do {
174                         if ((short) i < 0)
175                                 j = right[j];
176                         else
177                                 j = left[j];
178                         i <<= 1;
179                 } while (j >= N1);
180                 fillbuf(c_len[j] - 12);
181         }
182         if (j == N1 - 1)
183                 j += getbits(EXTRABITS);
184         return j;
185 }
186
187 /* ------------------------------------------------------------------------ */
188 unsigned short
189 decode_p_st0(/*void*/)
190 {
191         int             i, j;
192
193         j = pt_table[bitbuf >> 8];
194         if (j < np) {
195                 fillbuf(pt_len[j]);
196         }
197         else {
198                 fillbuf(8);
199                 i = bitbuf;
200                 do {
201                         if ((short) i < 0)
202                                 j = right[j];
203                         else
204                                 j = left[j];
205                         i <<= 1;
206                 } while (j >= np);
207                 fillbuf(pt_len[j] - 8);
208         }
209         return (j << 6) + getbits(6);
210 }
211
212 /* Local Variables: */
213 /* mode:c */
214 /* tab-width:4 */
215 /* End: */
216 /* vi: set tabstop=4: */