OSDN Git Service

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