OSDN Git Service

* configure.ac: updated version.
[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 /* lh3 */
25 void
26 decode_start_st0( /*void*/ )
27 {
28     n_max = 286;
29     maxmatch = MAXMATCH;
30     init_getbits();
31     init_code_cache();
32 #ifdef SUPPORT_LH7
33     np = 1 << (MAX_DICBIT - 7);
34 #endif
35 #ifndef SUPPORT_LH7
36     np = 1 << (MAX_DICBIT - 6);
37 #endif
38
39 }
40
41 /* ------------------------------------------------------------------------ */
42 void
43 encode_p_st0(j)
44     unsigned short  j;
45 {
46     unsigned short  i;
47
48     i = j >> 6;
49     putcode(pt_len[i], pt_code[i]);
50     putbits(6, j & 0x3f);
51 }
52
53 /* ------------------------------------------------------------------------ */
54 static void
55 ready_made(method)
56     int             method;
57 {
58     int             i, j;
59     unsigned int    code, weight;
60     int            *tbl;
61
62     tbl = fixed[method];
63     j = *tbl++;
64     weight = 1 << (16 - j);
65     code = 0;
66     for (i = 0; i < np; i++) {
67         while (*tbl == i) {
68             j++;
69             tbl++;
70             weight >>= 1;
71         }
72         pt_len[i] = j;
73         pt_code[i] = code;
74         code += weight;
75     }
76 }
77
78 /* ------------------------------------------------------------------------ */
79 /* lh1 */
80 void
81 encode_start_fix( /*void*/ )
82 {
83     n_max = 314;
84     maxmatch = 60;
85     np = 1 << (12 - 6);
86     init_putbits();
87     init_code_cache();
88     start_c_dyn();
89     ready_made(0);
90 }
91
92 /* ------------------------------------------------------------------------ */
93 static void
94 read_tree_c( /*void*/ )
95 {               /* read tree from file */
96     int             i, c;
97
98     i = 0;
99     while (i < N1) {
100         if (getbits(1))
101             c_len[i] = getbits(LENFIELD) + 1;
102         else
103             c_len[i] = 0;
104         if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1) {
105             c = getbits(CBIT);
106             for (i = 0; i < N1; i++)
107                 c_len[i] = 0;
108             for (i = 0; i < 4096; i++)
109                 c_table[i] = c;
110             return;
111         }
112     }
113     make_table(N1, c_len, 12, c_table);
114 }
115
116 /* ------------------------------------------------------------------------ */
117 static void
118 read_tree_p(/*void*/)
119 {               /* read tree from file */
120     int             i, c;
121
122     i = 0;
123     while (i < NP) {
124         pt_len[i] = getbits(LENFIELD);
125         if (++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1) {
126             c = getbits(LZHUFF3_DICBIT - 6);
127             for (i = 0; i < NP; i++)
128                 pt_len[i] = 0;
129             for (i = 0; i < 256; i++)
130                 pt_table[i] = c;
131             return;
132         }
133     }
134 }
135
136 /* ------------------------------------------------------------------------ */
137 /* lh1 */
138 void
139 decode_start_fix(/*void*/)
140 {
141     n_max = 314;
142     maxmatch = 60;
143     init_getbits();
144     init_code_cache();
145     np = 1 << (LZHUFF1_DICBIT - 6);
146     start_c_dyn();
147     ready_made(0);
148     make_table(np, pt_len, 8, pt_table);
149 }
150
151 /* ------------------------------------------------------------------------ */
152 /* lh3 */
153 unsigned short
154 decode_c_st0(/*void*/)
155 {
156     int             i, j;
157     static unsigned short blocksize = 0;
158
159     if (blocksize == 0) {   /* read block head */
160         blocksize = getbits(BUFBITS);   /* read block blocksize */
161         read_tree_c();
162         if (getbits(1)) {
163             read_tree_p();
164         }
165         else {
166             ready_made(1);
167         }
168         make_table(NP, pt_len, 8, pt_table);
169     }
170     blocksize--;
171     j = c_table[peekbits(12)];
172     if (j < N1)
173         fillbuf(c_len[j]);
174     else {
175         fillbuf(12);
176         i = bitbuf;
177         do {
178             if ((short) i < 0)
179                 j = right[j];
180             else
181                 j = left[j];
182             i <<= 1;
183         } while (j >= N1);
184         fillbuf(c_len[j] - 12);
185     }
186     if (j == N1 - 1)
187         j += getbits(EXTRABITS);
188     return j;
189 }
190
191 /* ------------------------------------------------------------------------ */
192 /* lh1, 3 */
193 unsigned short
194 decode_p_st0(/*void*/)
195 {
196     int             i, j;
197
198     j = pt_table[peekbits(8)];
199     if (j < np) {
200         fillbuf(pt_len[j]);
201     }
202     else {
203         fillbuf(8);
204         i = bitbuf;
205         do {
206             if ((short) i < 0)
207                 j = right[j];
208             else
209                 j = left[j];
210             i <<= 1;
211         } while (j >= np);
212         fillbuf(pt_len[j] - 8);
213     }
214     return (j << 6) + getbits(6);
215 }