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
decode_start_st0()25 decode_start_st0( /*void*/ )
26 {
27 n_max = 286;
28 maxmatch = MAXMATCH;
29 init_getbits();
30 #ifdef SUPPORT_LH7
31 np = 1 << (MAX_DICBIT - 7);
32 #endif
33 #ifndef SUPPORT_LH7
34 np = 1 << (MAX_DICBIT - 6);
35 #endif
36
37 }
38
39 /* ------------------------------------------------------------------------ */
40 void
encode_p_st0(unsigned short j)41 encode_p_st0(unsigned short j)
42 {
43 unsigned short i;
44
45 i = j >> 6;
46 putcode(pt_len[i], pt_code[i]);
47 putbits(6, j & 0x3f);
48 }
49
50 /* ------------------------------------------------------------------------ */
51 static void
ready_made(method)52 ready_made(method)
53 int method;
54 {
55 int i, j;
56 unsigned int code, weight;
57 int *tbl;
58
59 tbl = fixed[method];
60 j = *tbl++;
61 weight = 1 << (16 - j);
62 code = 0;
63 for (i = 0; i < np; i++) {
64 while (*tbl == i) {
65 j++;
66 tbl++;
67 weight >>= 1;
68 }
69 pt_len[i] = j;
70 pt_code[i] = code;
71 code += weight;
72 }
73 }
74
75 /* ------------------------------------------------------------------------ */
76 void
encode_start_fix()77 encode_start_fix( /*void*/ )
78 {
79 n_max = 314;
80 maxmatch = 60;
81 np = 1 << (12 - 6);
82 init_putbits();
83 start_c_dyn();
84 ready_made(0);
85 }
86
87 /* ------------------------------------------------------------------------ */
88 static void
read_tree_c()89 read_tree_c( /*void*/ )
90 { /* read tree from file */
91 int i, c;
92
93 i = 0;
94 while (i < N1) {
95 if (getbits(1))
96 c_len[i] = getbits(LENFIELD) + 1;
97 else
98 c_len[i] = 0;
99 if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1) {
100 c = getbits(CBIT);
101 for (i = 0; i < N1; i++)
102 c_len[i] = 0;
103 for (i = 0; i < 4096; i++)
104 c_table[i] = c;
105 return;
106 }
107 }
108 make_table(N1, c_len, 12, c_table);
109 }
110
111 /* ------------------------------------------------------------------------ */
112 static void
read_tree_p()113 read_tree_p(/*void*/)
114 { /* read tree from file */
115 int i, c;
116
117 i = 0;
118 while (i < NP) {
119 pt_len[i] = getbits(LENFIELD);
120 if (++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1) {
121 #ifdef SUPPORT_LH7
122 c = getbits(MAX_DICBIT - 7);
123 #else
124 c = getbits(MAX_DICBIT - 6);
125 #endif
126 for (i = 0; i < NP; i++)
127 c_len[i] = 0;
128 for (i = 0; i < 256; i++)
129 c_table[i] = c;
130 return;
131 }
132 }
133 }
134
135 /* ------------------------------------------------------------------------ */
136 void
decode_start_fix()137 decode_start_fix(/*void*/)
138 {
139 n_max = 314;
140 maxmatch = 60;
141 init_getbits();
142 np = 1 << (12 - 6);
143 start_c_dyn();
144 ready_made(0);
145 make_table(np, pt_len, 8, pt_table);
146 }
147
148 /* ------------------------------------------------------------------------ */
149 unsigned short
decode_c_st0()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
decode_p_st0()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