1 | /* |
---|
2 | * extract.c -- global extracting function for all known file compressions |
---|
3 | * in a MPQ archive. |
---|
4 | * |
---|
5 | * Copyright (C) 2003 Maik Broemme <mbroemme@plusserver.de> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation; either version 2 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #ifdef HAVE_CONFIG_H |
---|
23 | #include "config.h" |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdio.h> |
---|
29 | |
---|
30 | #define HAVE_LIBZ |
---|
31 | #ifdef HAVE_LIBZ |
---|
32 | #include <zlib.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | #include "mpq.h" |
---|
36 | #include "explode.h" |
---|
37 | #include "huffman.h" |
---|
38 | |
---|
39 | #include "wave.h" |
---|
40 | |
---|
41 | /* |
---|
42 | * Support functions for PKWARE data compression library. |
---|
43 | * |
---|
44 | * Function loads data from the input buffer. Used by mpq_pkzip |
---|
45 | * "implode" and "explode" function as user-defined callback. |
---|
46 | * Returns number of bytes loaded. |
---|
47 | * |
---|
48 | * char * buf - Pointer to a buffer where to store loaded data |
---|
49 | * unsigned int * size - Max. number of bytes to read |
---|
50 | * void * param - Custom pointer, parameter of implode/explode |
---|
51 | */ |
---|
52 | static unsigned int libmpq_pkzip_read_input_data(char *buf, unsigned int *size, void *param) { |
---|
53 | pkzip_data *info = (pkzip_data *)param; |
---|
54 | unsigned int max_avail = (info->in_bytes - info->in_pos); |
---|
55 | unsigned int to_read = *size; |
---|
56 | |
---|
57 | /* Check the case when not enough data available */ |
---|
58 | if (to_read > max_avail) { |
---|
59 | to_read = max_avail; |
---|
60 | } |
---|
61 | |
---|
62 | /* Load data and increment offsets */ |
---|
63 | memcpy(buf, info->in_buf + info->in_pos, to_read); |
---|
64 | info->in_pos += to_read; |
---|
65 | |
---|
66 | return to_read; |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | * Support functions for PKWARE data compression library. |
---|
71 | * |
---|
72 | * Function for store output data. Used by mpq_pkzip "implode" and |
---|
73 | * "explode" as user-defined callback. |
---|
74 | * |
---|
75 | * char * buf - Pointer to data to be written |
---|
76 | * unsigned int * size - Number of bytes to write |
---|
77 | * void * param - Custom pointer, parameter of implode/explode |
---|
78 | */ |
---|
79 | static void libmpq_pkzip_write_output_data(char *buf, unsigned int *size, void *param) { |
---|
80 | pkzip_data *info = (pkzip_data *)param; |
---|
81 | unsigned int max_write = (info->max_out - info->out_pos); |
---|
82 | unsigned int to_write = *size; |
---|
83 | |
---|
84 | /* Check the case when not enough space in the output buffer */ |
---|
85 | if (to_write > max_write) { |
---|
86 | to_write = max_write; |
---|
87 | } |
---|
88 | |
---|
89 | /* Write output data and increments offsets */ |
---|
90 | memcpy(info->out_buf + info->out_pos, buf, to_write); |
---|
91 | info->out_pos += to_write; |
---|
92 | } |
---|
93 | |
---|
94 | int libmpq_pkzip_decompress(char *out_buf, int *out_length, char *in_buf, int in_length) { |
---|
95 | pkzip_data info; /* Data information */ |
---|
96 | char *work_buf = (char *)malloc(LIBMPQ_PKZIP_EXP_BUFFER_SIZE); /* mpq_pkzip work buffer */ |
---|
97 | |
---|
98 | /* Fill data information structure */ |
---|
99 | info.in_buf = in_buf; |
---|
100 | info.in_pos = 0; |
---|
101 | info.in_bytes = in_length; |
---|
102 | info.out_buf = out_buf; |
---|
103 | info.out_pos = 0; |
---|
104 | info.max_out = *out_length; |
---|
105 | |
---|
106 | /* Do the decompression */ |
---|
107 | libmpq_pkzip_explode(libmpq_pkzip_read_input_data, libmpq_pkzip_write_output_data, work_buf, &info); |
---|
108 | *out_length = info.out_pos; |
---|
109 | free(work_buf); |
---|
110 | return 0; |
---|
111 | } |
---|
112 | |
---|
113 | int libmpq_wave_decompress_mono(char *out_buf, int *out_length, char *in_buf, int in_length) { |
---|
114 | *out_length = libmpq_wave_decompress((unsigned char *)out_buf, *out_length, (unsigned char *)in_buf, in_length, 1); |
---|
115 | return 1; |
---|
116 | } |
---|
117 | |
---|
118 | int libmpq_wave_decompress_stereo(char *out_buf, int *out_length, char *in_buf, int in_length) { |
---|
119 | *out_length = libmpq_wave_decompress((unsigned char *)out_buf, *out_length, (unsigned char *)in_buf, in_length, 2); |
---|
120 | return 1; |
---|
121 | } |
---|
122 | |
---|
123 | int libmpq_zlib_decompress(char *out_buf, int *out_length, char *in_buf, int in_length) { |
---|
124 | #ifdef HAVE_LIBZ |
---|
125 | z_stream z; /* Stream information for zlib */ |
---|
126 | int result; |
---|
127 | |
---|
128 | /* Fill the stream structure for zlib */ |
---|
129 | z.next_in = (Bytef *)in_buf; |
---|
130 | z.avail_in = (uInt)in_length; |
---|
131 | z.total_in = in_length; |
---|
132 | z.next_out = (Bytef *)out_buf; |
---|
133 | z.avail_out = *out_length; |
---|
134 | z.total_out = 0; |
---|
135 | z.zalloc = NULL; |
---|
136 | z.zfree = NULL; |
---|
137 | |
---|
138 | /* Initialize the decompression structure. Storm.dll uses zlib version 1.1.3 */ |
---|
139 | if ((result = inflateInit(&z)) == 0) { |
---|
140 | |
---|
141 | /* Call zlib to decompress the data */ |
---|
142 | result = inflate(&z, Z_FINISH); |
---|
143 | *out_length = z.total_out; |
---|
144 | inflateEnd(&z); |
---|
145 | } |
---|
146 | return result; |
---|
147 | #else |
---|
148 | memset(out_buf, '0', *out_length); |
---|
149 | return 0; |
---|
150 | #endif |
---|
151 | } |
---|
152 | |
---|
153 | /* |
---|
154 | * Huffmann decompression routine. The in_length parameter is not used, but needs |
---|
155 | * to be specified due to compatibility reasons. |
---|
156 | * |
---|
157 | * 1500F5F0 |
---|
158 | */ |
---|
159 | int libmpq_huff_decompress(char *out_buf, int *out_length, char *in_buf, int in_length) { |
---|
160 | struct huffman_tree *ht = (huffman_tree *)malloc(sizeof(struct huffman_tree)); |
---|
161 | struct huffman_input_stream *is = (huffman_input_stream *)malloc(sizeof(struct huffman_input_stream)); |
---|
162 | struct huffman_tree_item *hi = (huffman_tree_item *)malloc(sizeof(struct huffman_tree_item)); |
---|
163 | memset(ht, 0, sizeof(struct huffman_tree)); |
---|
164 | memset(is, 0, sizeof(struct huffman_input_stream)); |
---|
165 | memset(hi, 0, sizeof(struct huffman_tree_item)); |
---|
166 | |
---|
167 | /* Initialize input stream */ |
---|
168 | is->bit_buf = *(unsigned long *)in_buf; |
---|
169 | in_buf += sizeof(unsigned long); |
---|
170 | is->in_buf = (unsigned char *)in_buf; |
---|
171 | is->bits = 32; |
---|
172 | |
---|
173 | /* Initialize the Huffmann tree for decompression */ |
---|
174 | libmpq_huff_init_tree(ht, hi, LIBMPQ_HUFF_DECOMPRESS); |
---|
175 | |
---|
176 | *out_length = libmpq_huff_do_decompress(ht, is, (unsigned char *)out_buf, *out_length); |
---|
177 | |
---|
178 | free(hi); |
---|
179 | free(is); |
---|
180 | free(ht); |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | int libmpq_multi_decompress(char *out_buf, int *pout_length, char *in_buf, int in_length) { |
---|
185 | char *temp_buf = NULL; /* Temporary storage for decompressed data */ |
---|
186 | char *work_buf = NULL; /* Where to store decompressed data */ |
---|
187 | int out_length = *pout_length; /* For storage number of output bytes */ |
---|
188 | unsigned fDecompressions1; /* Decompressions applied to the block */ |
---|
189 | unsigned fDecompressions2; /* Just another copy of decompressions applied to the block */ |
---|
190 | int count = 0; /* Counter for every use */ |
---|
191 | int entries = (sizeof(dcmp_table) / sizeof(decompress_table)); |
---|
192 | int i; |
---|
193 | |
---|
194 | /* If the input length is the same as output, do nothing. */ |
---|
195 | if (in_length == out_length) { |
---|
196 | if (in_buf == out_buf) { |
---|
197 | return 1; |
---|
198 | } |
---|
199 | memcpy(out_buf, in_buf, in_length); |
---|
200 | return 1; |
---|
201 | } |
---|
202 | |
---|
203 | /* Get applied compression types and decrement data length */ |
---|
204 | fDecompressions1 = fDecompressions2 = (unsigned char)*in_buf++; |
---|
205 | in_length--; |
---|
206 | |
---|
207 | /* Search decompression table type and get all types of compression */ |
---|
208 | for (i = 0; i < entries; i++) { |
---|
209 | /* We have to apply this decompression? */ |
---|
210 | if (fDecompressions1 & dcmp_table[i].mask) { |
---|
211 | count++; |
---|
212 | } |
---|
213 | |
---|
214 | /* Clear this flag from temporary variable. */ |
---|
215 | fDecompressions2 &= ~dcmp_table[i].mask; |
---|
216 | } |
---|
217 | |
---|
218 | /* |
---|
219 | * Check if there is some method unhandled |
---|
220 | * (E.g. compressed by future versions) |
---|
221 | */ |
---|
222 | if (fDecompressions2 != 0) { |
---|
223 | printf("Unknown Compression\n"); |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | /* If there is more than only one compression, we have to allocate extra buffer */ |
---|
228 | if (count >= 2) { |
---|
229 | temp_buf = (char *)malloc(out_length); |
---|
230 | } |
---|
231 | |
---|
232 | /* Apply all decompressions */ |
---|
233 | for (i = 0, count = 0; i < entries; i++) { |
---|
234 | |
---|
235 | /* If not used this kind of compression, skip the loop */ |
---|
236 | if (fDecompressions1 & dcmp_table[i].mask) { |
---|
237 | |
---|
238 | /* If odd case, use target buffer for output, otherwise use allocated tempbuf */ |
---|
239 | work_buf = (count++ & 1) ? temp_buf : out_buf; |
---|
240 | out_length = *pout_length; |
---|
241 | |
---|
242 | /* Decompress buffer using corresponding function */ |
---|
243 | dcmp_table[i].decompress(work_buf, &out_length, in_buf, in_length); |
---|
244 | |
---|
245 | /* Move output length to src length for next compression */ |
---|
246 | in_length = out_length; |
---|
247 | in_buf = work_buf; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | /* If output buffer is not the same like target buffer, we have to copy data */ |
---|
252 | if (work_buf != out_buf) { |
---|
253 | memcpy(out_buf, in_buf, out_length); |
---|
254 | } |
---|
255 | *pout_length = out_length; |
---|
256 | |
---|
257 | /* Delete temporary buffer, if necessary */ |
---|
258 | if (temp_buf != NULL) { |
---|
259 | free(temp_buf); |
---|
260 | } |
---|
261 | return 1; |
---|
262 | } |
---|