OSDN Git Service

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