diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libgme/inflate/inflate.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libgme/inflate/inflate.c')
-rw-r--r-- | lib/rbcodec/codecs/libgme/inflate/inflate.c | 1156 |
1 files changed, 1156 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/inflate/inflate.c b/lib/rbcodec/codecs/libgme/inflate/inflate.c new file mode 100644 index 0000000000..130abe89be --- /dev/null +++ b/lib/rbcodec/codecs/libgme/inflate/inflate.c | |||
@@ -0,0 +1,1156 @@ | |||
1 | /* | ||
2 | * gunzip implementation for wikiviewer (c) Frederik M.J.V., 2006. | ||
3 | * some bug fixes by Adam Gashlin gunzip implementation for busybox | ||
4 | * | ||
5 | * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly. | ||
6 | * | ||
7 | * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de> | ||
8 | * based on gzip sources | ||
9 | * | ||
10 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as | ||
11 | *well as stdin/stdout, and to generally behave itself wrt command line | ||
12 | *handling. | ||
13 | * | ||
14 | * General cleanup to better adhere to the style guide and make use of standard | ||
15 | *busybox functions by Glenn McGrath <bug1@iinet.net.au> | ||
16 | * | ||
17 | * read_gz interface + associated hacking by Laurence Anderson | ||
18 | * | ||
19 | * Fixed huft_build() so decoding end-of-block code does not grab more bits than | ||
20 | *necessary (this is required by unzip applet), added inflate_cleanup() to free | ||
21 | *leaked bytebuffer memory (used in unzip.c), and some minor style guide | ||
22 | *cleanups by Ed Clark | ||
23 | * | ||
24 | * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface | ||
25 | *Copyright (C) 1992-1993 Jean-loup Gailly The unzip code was written and put in | ||
26 | *the public domain by Mark Adler. Portions of the lzw code are derived from the | ||
27 | *public domain 'compress' | ||
28 | * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies, | ||
29 | *Ken Turkowski, Dave Mack and Peter Jannesen. | ||
30 | * | ||
31 | * | ||
32 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
33 | */ | ||
34 | |||
35 | #include <inttypes.h> | ||
36 | #ifndef NULL | ||
37 | #define NULL 0 | ||
38 | #endif | ||
39 | #define ENABLE_DESKTOP 0 | ||
40 | #define USE_DESKTOP(...) | ||
41 | #include "mallocer.h" | ||
42 | #include "bbfuncs.h" | ||
43 | #include "inflate.h" | ||
44 | |||
45 | #define TRIM_FILE_ON_ERROR 1 | ||
46 | |||
47 | typedef struct huft_s { | ||
48 | unsigned char e; /* number of extra bits or operation */ | ||
49 | unsigned char b; /* number of bits in this code or subcode */ | ||
50 | union { | ||
51 | unsigned short n; /* literal, length base, or distance base */ | ||
52 | struct huft_s *t; /* pointer to next level of table */ | ||
53 | } v; | ||
54 | } huft_t; | ||
55 | |||
56 | /*static void *mainmembuf;*/ | ||
57 | static void *huftbuffer1; | ||
58 | static void *huftbuffer2; | ||
59 | |||
60 | #define HUFT_MMP1 8 | ||
61 | #define HUFT_MMP2 9 | ||
62 | |||
63 | static struct mbreader_t *gunzip_src_md; | ||
64 | static unsigned int gunzip_bytes_out; /* number of output bytes */ | ||
65 | static unsigned int gunzip_outbuf_count; /* bytes in output buffer */ | ||
66 | |||
67 | /* gunzip_window size--must be a power of two, and at least 32K for zip's | ||
68 | deflate method */ | ||
69 | enum { | ||
70 | gunzip_wsize = 0x8000 | ||
71 | }; | ||
72 | |||
73 | static unsigned char *gunzip_window; | ||
74 | static uint32_t ifl_total; | ||
75 | |||
76 | static uint32_t gunzip_crc; | ||
77 | |||
78 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ | ||
79 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ | ||
80 | #define N_MAX 288 /* maximum number of codes in any set */ | ||
81 | |||
82 | /* bitbuffer */ | ||
83 | static unsigned int gunzip_bb; /* bit buffer */ | ||
84 | static unsigned char gunzip_bk; /* bits in bit buffer */ | ||
85 | |||
86 | /* These control the size of the bytebuffer */ | ||
87 | static unsigned int bytebuffer_max = 0x8000; | ||
88 | static unsigned char *bytebuffer = NULL; | ||
89 | static unsigned int bytebuffer_offset = 0; | ||
90 | static unsigned int bytebuffer_size = 0; | ||
91 | |||
92 | static const unsigned short mask_bits[] = { | ||
93 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, | ||
94 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff | ||
95 | }; | ||
96 | |||
97 | /* Copy lengths for literal codes 257..285 */ | ||
98 | static const unsigned short cplens[] = { | ||
99 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, | ||
100 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 | ||
101 | }; | ||
102 | |||
103 | /* note: see note #13 above about the 258 in this list. */ | ||
104 | /* Extra bits for literal codes 257..285 */ | ||
105 | static const unsigned char cplext[] = { | ||
106 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, | ||
107 | 5, 5, 5, 0, 99, 99 | ||
108 | }; /* 99==invalid */ | ||
109 | |||
110 | /* Copy offsets for distance codes 0..29 */ | ||
111 | static const unsigned short cpdist[] = { | ||
112 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, | ||
113 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 | ||
114 | }; | ||
115 | |||
116 | /* Extra bits for distance codes */ | ||
117 | static const unsigned char cpdext[] = { | ||
118 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, | ||
119 | 11, 11, 12, 12, 13, 13 | ||
120 | }; | ||
121 | |||
122 | /* Tables for deflate from PKZIP's appnote.txt. */ | ||
123 | /* Order of the bit length code lengths */ | ||
124 | static const unsigned char border[] = { | ||
125 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | ||
126 | }; | ||
127 | |||
128 | static const uint32_t crc_table[256]= { | ||
129 | 0,1996959894,-301047508,-1727442502,124634137,1886057615, | ||
130 | -379345611,-1637575261,249268274,2044508324,-522852066, | ||
131 | -1747789432,162941995,2125561021,-407360249,-1866523247, | ||
132 | 498536548,1789927666,-205950648,-2067906082,450548861, | ||
133 | 1843258603,-187386543,-2083289657,325883990,1684777152, | ||
134 | -43845254,-1973040660,335633487,1661365465,-99664541, | ||
135 | -1928851979,997073096,1281953886,-715111964,-1570279054, | ||
136 | 1006888145,1258607687,-770865667,-1526024853,901097722, | ||
137 | 1119000684,-608450090,-1396901568,853044451,1172266101, | ||
138 | -589951537,-1412350631,651767980,1373503546,-925412992, | ||
139 | -1076862698,565507253,1454621731,-809855591,-1195530993, | ||
140 | 671266974,1594198024,-972236366,-1324619484,795835527, | ||
141 | 1483230225,-1050600021,-1234817731,1994146192,31158534, | ||
142 | -1731059524,-271249366,1907459465,112637215,-1614814043, | ||
143 | -390540237,2013776290,251722036,-1777751922,-519137256, | ||
144 | 2137656763,141376813,-1855689577,-429695999,1802195444, | ||
145 | 476864866,-2056965928,-228458418,1812370925,453092731, | ||
146 | -2113342271,-183516073,1706088902,314042704,-1950435094, | ||
147 | -54949764,1658658271,366619977,-1932296973,-69972891, | ||
148 | 1303535960,984961486,-1547960204,-725929758,1256170817, | ||
149 | 1037604311,-1529756563,-740887301,1131014506,879679996, | ||
150 | -1385723834,-631195440,1141124467,855842277,-1442165665, | ||
151 | -586318647,1342533948,654459306,-1106571248,-921952122, | ||
152 | 1466479909,544179635,-1184443383,-832445281,1591671054, | ||
153 | 702138776,-1328506846,-942167884,1504918807,783551873, | ||
154 | -1212326853,-1061524307,-306674912,-1698712650,62317068, | ||
155 | 1957810842,-355121351,-1647151185,81470997,1943803523, | ||
156 | -480048366,-1805370492,225274430,2053790376,-468791541, | ||
157 | -1828061283,167816743,2097651377,-267414716,-2029476910, | ||
158 | 503444072,1762050814,-144550051,-2140837941,426522225, | ||
159 | 1852507879,-19653770,-1982649376,282753626,1742555852, | ||
160 | -105259153,-1900089351,397917763,1622183637,-690576408, | ||
161 | -1580100738,953729732,1340076626,-776247311,-1497606297, | ||
162 | 1068828381,1219638859,-670225446,-1358292148,906185462, | ||
163 | 1090812512,-547295293,-1469587627,829329135,1181335161, | ||
164 | -882789492,-1134132454,628085408,1382605366,-871598187, | ||
165 | -1156888829,570562233,1426400815,-977650754,-1296233688, | ||
166 | 733239954,1555261956,-1026031705,-1244606671,752459403, | ||
167 | 1541320221,-1687895376,-328994266,1969922972,40735498, | ||
168 | -1677130071,-351390145,1913087877,83908371,-1782625662, | ||
169 | -491226604,2075208622,213261112,-1831694693,-438977011, | ||
170 | 2094854071,198958881,-2032938284,-237706686,1759359992, | ||
171 | 534414190,-2118248755,-155638181,1873836001,414664567, | ||
172 | -2012718362,-15766928,1711684554,285281116,-1889165569, | ||
173 | -127750551,1634467795,376229701,-1609899400,-686959890, | ||
174 | 1308918612,956543938,-1486412191,-799009033,1231636301, | ||
175 | 1047427035,-1362007478,-640263460,1088359270,936918000, | ||
176 | -1447252397,-558129467,1202900863,817233897,-1111625188, | ||
177 | -893730166,1404277552,615818150,-1160759803,-841546093, | ||
178 | 1423857449,601450431,-1285129682,-1000256840,1567103746, | ||
179 | 711928724,-1274298825,-1022587231,1510334235,755167117 | ||
180 | }; | ||
181 | |||
182 | static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, | ||
183 | const unsigned int required) | ||
184 | { | ||
185 | while (*current < required) | ||
186 | { | ||
187 | if (bytebuffer_offset >= bytebuffer_size) | ||
188 | { | ||
189 | /* Leave the first 4 bytes empty so we can always unwind the | ||
190 | bitbuffer to the front of the bytebuffer, leave 4 bytes free at | ||
191 | end of tail so we can easily top up buffer in | ||
192 | check_trailer_gzip() */ | ||
193 | if (1 > (bytebuffer_size = safe_read(gunzip_src_md, &bytebuffer[4], | ||
194 | bytebuffer_max - 8))) | ||
195 | error_die("unexpected end of file"); | ||
196 | |||
197 | bytebuffer_size += 4; | ||
198 | bytebuffer_offset = 4; | ||
199 | } | ||
200 | |||
201 | bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current; | ||
202 | bytebuffer_offset++; | ||
203 | *current += 8; | ||
204 | } | ||
205 | return(bitbuffer); | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * Free the malloc'ed tables built by huft_build(), which makes a linked list of | ||
210 | *the tables it made, with the links in a dummy first entry of each table. | ||
211 | * t: table to free | ||
212 | */ | ||
213 | static int huft_free(huft_t * t,unsigned char bufnum) | ||
214 | { | ||
215 | wpw_reset_mempool(bufnum); | ||
216 | if(t==0) | ||
217 | { | ||
218 | } | ||
219 | |||
220 | return 0; | ||
221 | } | ||
222 | |||
223 | /* Given a list of code lengths and a maximum table size, make a set of tables | ||
224 | to decode that set of codes. Return zero on success, one if the given code | ||
225 | set is incomplete (the tables are still built in this case), two if the input | ||
226 | is invalid (all zero length codes or an oversubscribed set of lengths), and | ||
227 | three if not enough memory. | ||
228 | * | ||
229 | * b: code lengths in bits (all assumed <= BMAX) n: number of codes | ||
230 | *(assumed <= N_MAX) s: number of simple-valued codes (0..s-1) d: list of | ||
231 | *base values for non-simple codes e: list of extra bits for non-simple codes | ||
232 | *t: result: starting table m: maximum lookup bits, returns actual bufnum: | ||
233 | *the number of the memory pool to fetch memory from | ||
234 | */ | ||
235 | static | ||
236 | int huft_build(unsigned int *b, const unsigned int n, | ||
237 | const unsigned int s, const unsigned short *d, | ||
238 | const unsigned char *e, huft_t ** t, unsigned int *m, | ||
239 | unsigned char bufnum) | ||
240 | { | ||
241 | unsigned a=0; /* counter for codes of length k */ | ||
242 | unsigned c[BMAX + 1]; /* bit length count table */ | ||
243 | unsigned eob_len=0; /* length of end-of-block code (value 256) */ | ||
244 | unsigned f=0; /* i repeats in table every f entries */ | ||
245 | int g=0; /* maximum code length */ | ||
246 | int htl=0; /* table level */ | ||
247 | unsigned i=0; /* counter, current code */ | ||
248 | unsigned j=0; /* counter */ | ||
249 | int k=0; /* number of bits in current code */ | ||
250 | unsigned *p; /* pointer into c[], b[], or v[] */ | ||
251 | huft_t *q; /* points to current table */ | ||
252 | huft_t r; /* table entry for structure assignment */ | ||
253 | huft_t *u[BMAX]; /* table stack */ | ||
254 | unsigned v[N_MAX]; /* values in order of bit length */ | ||
255 | int ws[BMAX+1]; /* bits decoded stack */ | ||
256 | int w=0; /* bits decoded */ | ||
257 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ | ||
258 | unsigned *xp; /* pointer into x */ | ||
259 | int y=0; /* number of dummy codes added */ | ||
260 | unsigned z=0; /* number of entries in current table */ | ||
261 | |||
262 | /* Length of EOB code, if any */ | ||
263 | eob_len = n > 256 ? b[256] : BMAX; | ||
264 | |||
265 | /* Generate counts for each bit length */ | ||
266 | memset((void *)c, 0, sizeof(c)); | ||
267 | p = b; | ||
268 | i = n; | ||
269 | do { | ||
270 | c[*p]++; /* assume all entries <= BMAX */ | ||
271 | p++; /* Can't combine with above line (Solaris bug) */ | ||
272 | } while (--i); | ||
273 | if (c[0] == n) /* null input--all zero length codes */ | ||
274 | { | ||
275 | *t = (huft_t *) NULL; | ||
276 | *m = 0; | ||
277 | return 2; | ||
278 | } | ||
279 | |||
280 | /* Find minimum and maximum length, bound *m by those */ | ||
281 | for (j = 1; (c[j] == 0) && (j <= BMAX); j++) ; | ||
282 | |||
283 | k = j; /* minimum code length */ | ||
284 | for (i = BMAX; (c[i] == 0) && i; i--) ; | ||
285 | |||
286 | g = i; /* maximum code length */ | ||
287 | *m = (*m < j) ? j : ((*m > i) ? i : *m); | ||
288 | |||
289 | /* Adjust last length count to fill out codes, if needed */ | ||
290 | for (y = 1 << j; j < i; j++, y <<= 1) | ||
291 | { | ||
292 | if ((y -= c[j]) < 0) | ||
293 | return 2; /* bad input: more codes than bits */ | ||
294 | } | ||
295 | |||
296 | if ((y -= c[i]) < 0) | ||
297 | return 2; | ||
298 | |||
299 | c[i] += y; | ||
300 | |||
301 | /* Generate starting offsets into the value table for each length */ | ||
302 | x[1] = j = 0; | ||
303 | p = c + 1; | ||
304 | xp = x + 2; | ||
305 | while (--i) /* note that i == g from above */ | ||
306 | { | ||
307 | *xp++ = (j += *p++); | ||
308 | } | ||
309 | |||
310 | /* Make a table of values in order of bit lengths */ | ||
311 | p = b; | ||
312 | i = 0; | ||
313 | do { | ||
314 | if ((j = *p++) != 0) | ||
315 | v[x[j]++] = i; | ||
316 | } while (++i < n); | ||
317 | |||
318 | /* Generate the Huffman codes and for each, make the table entries */ | ||
319 | x[0] = i = 0; /* first Huffman code is zero */ | ||
320 | p = v; /* grab values in bit order */ | ||
321 | htl = -1; /* no tables yet--level -1 */ | ||
322 | w = ws[0] = 0; /* bits decoded */ | ||
323 | u[0] = (huft_t *) NULL; /* just to keep compilers happy */ | ||
324 | q = (huft_t *) NULL; /* ditto */ | ||
325 | z = 0; /* ditto */ | ||
326 | |||
327 | /* go through the bit lengths (k already is bits in shortest code) */ | ||
328 | for (; k <= g; k++) | ||
329 | { | ||
330 | a = c[k]; | ||
331 | while (a--) | ||
332 | { | ||
333 | /* here i is the Huffman code of length k bits for value *p */ | ||
334 | /* make tables up to required level */ | ||
335 | while (k > ws[htl + 1]) | ||
336 | { | ||
337 | w = ws[++htl]; | ||
338 | |||
339 | /* compute minimum size table less than or equal to *m bits */ | ||
340 | z = (z = g - w) > *m ? *m : z; /* upper limit on table size */ | ||
341 | if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ | ||
342 | { /* too few codes for k-w bit table */ | ||
343 | f -= a + 1; /* deduct codes from patterns left */ | ||
344 | xp = c + k; | ||
345 | while (++j < z) /* try smaller tables up to z bits */ | ||
346 | { | ||
347 | if ((f <<= 1) <= *++xp) | ||
348 | break; /* enough codes to use up j bits */ | ||
349 | |||
350 | f -= *xp; /* else deduct codes from patterns */ | ||
351 | } | ||
352 | } | ||
353 | |||
354 | j = ((unsigned)(w + j) > eob_len && (unsigned)w < eob_len) | ||
355 | ? eob_len - w : j; /* make EOB code end at table */ | ||
356 | z = 1 << j; /* table entries for j-bit table */ | ||
357 | ws[htl+1] = w + j; /* set bits decoded in stack */ | ||
358 | |||
359 | /* allocate and link in new table */ | ||
360 | q = (huft_t *) wpw_malloc(bufnum,(z + 1) * sizeof(huft_t)); | ||
361 | if(q==0) | ||
362 | return 3; | ||
363 | |||
364 | *t = q + 1; /* link to list for huft_free() */ | ||
365 | t = &(q->v.t); | ||
366 | u[htl] = ++q; /* table starts after link */ | ||
367 | |||
368 | /* connect to last table, if there is one */ | ||
369 | if (htl) | ||
370 | { | ||
371 | x[htl] = i; /* save pattern for backing up */ | ||
372 | |||
373 | /* bits to dump before this table */ | ||
374 | r.b = (unsigned char) (w - ws[htl - 1]); | ||
375 | r.e = (unsigned char) (16 + j); /* bits in this table */ | ||
376 | r.v.t = q; /* pointer to this table */ | ||
377 | j = (i & ((1 << w) - 1)) >> ws[htl - 1]; | ||
378 | u[htl - 1][j] = r; /* connect to last table */ | ||
379 | } | ||
380 | } | ||
381 | |||
382 | /* set up table entry in r */ | ||
383 | r.b = (unsigned char) (k - w); | ||
384 | if (p >= v + n) | ||
385 | r.e = 99; /* out of values--invalid code */ | ||
386 | else if (*p < s) | ||
387 | { | ||
388 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB | ||
389 | code */ | ||
390 | r.v.n = (unsigned short) (*p++); /* simple code is just the | ||
391 | value */ | ||
392 | } | ||
393 | else | ||
394 | { | ||
395 | r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists | ||
396 | */ | ||
397 | r.v.n = d[*p++ - s]; | ||
398 | } | ||
399 | |||
400 | /* fill code-like entries with r */ | ||
401 | f = 1 << (k - w); | ||
402 | for (j = i >> w; j < z; j += f) | ||
403 | { | ||
404 | q[j] = r; | ||
405 | } | ||
406 | |||
407 | /* backwards increment the k-bit code i */ | ||
408 | for (j = 1 << (k - 1); i &j; j >>= 1) | ||
409 | { | ||
410 | i ^= j; | ||
411 | } | ||
412 | i ^= j; | ||
413 | |||
414 | /* backup over finished tables */ | ||
415 | while ((i & ((1 << w) - 1)) != x[htl]) | ||
416 | { | ||
417 | w = ws[--htl]; | ||
418 | } | ||
419 | } | ||
420 | } | ||
421 | |||
422 | /* return actual size of base table */ | ||
423 | *m = ws[1]; | ||
424 | |||
425 | /* Return true (1) if we were given an incomplete table */ | ||
426 | return y != 0 && g != 1; | ||
427 | } | ||
428 | |||
429 | /* | ||
430 | * inflate (decompress) the codes in a deflated (compressed) block. Return an | ||
431 | *error code or zero if it all goes ok. | ||
432 | * | ||
433 | * tl, td: literal/length and distance decoder tables bl, bd: number of bits | ||
434 | *decoded by tl[] and td[] | ||
435 | */ | ||
436 | static int inflate_codes_resumeCopy = 0; | ||
437 | static int inflate_codes(huft_t * my_tl, huft_t * my_td, | ||
438 | const unsigned int my_bl, const unsigned int my_bd, | ||
439 | int setup) | ||
440 | { | ||
441 | static unsigned int e; /* table entry flag/number of extra bits */ | ||
442 | static unsigned int n, d; /* length and index for copy */ | ||
443 | static unsigned int w; /* current gunzip_window position */ | ||
444 | static huft_t *t; /* pointer to table entry */ | ||
445 | static unsigned int ml, md; /* masks for bl and bd bits */ | ||
446 | static unsigned int b; /* bit buffer */ | ||
447 | static unsigned int k; /* number of bits in bit buffer */ | ||
448 | static huft_t *tl, *td; | ||
449 | static unsigned int bl, bd; | ||
450 | |||
451 | if (setup) /* 1st time we are called, copy in variables */ | ||
452 | { | ||
453 | tl = my_tl; | ||
454 | td = my_td; | ||
455 | bl = my_bl; | ||
456 | bd = my_bd; | ||
457 | /* make local copies of globals */ | ||
458 | b = gunzip_bb; /* initialize bit buffer */ | ||
459 | k = gunzip_bk; | ||
460 | w = gunzip_outbuf_count; /* initialize gunzip_window position | ||
461 | */ | ||
462 | |||
463 | /* inflate the coded data */ | ||
464 | ml = mask_bits[bl]; /* precompute masks for speed */ | ||
465 | md = mask_bits[bd]; | ||
466 | return 0; /* Don't actually do anything the first time */ | ||
467 | } | ||
468 | |||
469 | if (inflate_codes_resumeCopy) goto do_copy; | ||
470 | |||
471 | while (1) /* do until end of block */ | ||
472 | { | ||
473 | b = fill_bitbuffer(b, &k, bl); | ||
474 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | ||
475 | do { | ||
476 | if (e == 99) | ||
477 | error_die("inflate_codes error 1"); | ||
478 | |||
479 | b >>= t->b; | ||
480 | k -= t->b; | ||
481 | e -= 16; | ||
482 | b = fill_bitbuffer(b, &k, e); | ||
483 | } while ((e = | ||
484 | (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); | ||
485 | |||
486 | b >>= t->b; | ||
487 | k -= t->b; | ||
488 | if (e == 16) /* then it's a literal */ | ||
489 | { | ||
490 | gunzip_window[w++] = (unsigned char) t->v.n; | ||
491 | if (w == gunzip_wsize) | ||
492 | { | ||
493 | gunzip_outbuf_count = (w); | ||
494 | w = 0; | ||
495 | return 1; /* We have a block to read */ | ||
496 | } | ||
497 | } | ||
498 | else /* it's an EOB or a length */ | ||
499 | { /* exit if end of block */ | ||
500 | if (e == 15) | ||
501 | break; | ||
502 | |||
503 | /* get length of block to copy */ | ||
504 | b = fill_bitbuffer(b, &k, e); | ||
505 | n = t->v.n + ((unsigned) b & mask_bits[e]); | ||
506 | b >>= e; | ||
507 | k -= e; | ||
508 | |||
509 | /* decode distance of block to copy */ | ||
510 | b = fill_bitbuffer(b, &k, bd); | ||
511 | if ((e = (t = td + ((unsigned) b & md))->e) > 16) | ||
512 | do { | ||
513 | if (e == 99) | ||
514 | error_die("inflate_codes error 2"); | ||
515 | |||
516 | b >>= t->b; | ||
517 | k -= t->b; | ||
518 | e -= 16; | ||
519 | b = fill_bitbuffer(b, &k, e); | ||
520 | } while ((e = | ||
521 | (t = | ||
522 | t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); | ||
523 | |||
524 | b >>= t->b; | ||
525 | k -= t->b; | ||
526 | b = fill_bitbuffer(b, &k, e); | ||
527 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); | ||
528 | b >>= e; | ||
529 | k -= e; | ||
530 | |||
531 | /* do the copy */ | ||
532 | do_copy: do { | ||
533 | n -= (e = | ||
534 | (e = | ||
535 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); | ||
536 | /* copy to new buffer to prevent possible overwrite */ | ||
537 | if (w - d >= e) /* (this test assumes unsigned comparison) | ||
538 | */ | ||
539 | { | ||
540 | memcpy(gunzip_window + w, gunzip_window + d, e); | ||
541 | w += e; | ||
542 | d += e; | ||
543 | } | ||
544 | else | ||
545 | { | ||
546 | /* do it slow to avoid memcpy() overlap */ | ||
547 | /* !NOMEMCPY */ | ||
548 | do { | ||
549 | gunzip_window[w++] = gunzip_window[d++]; | ||
550 | } while (--e); | ||
551 | } | ||
552 | |||
553 | if (w == gunzip_wsize) | ||
554 | { | ||
555 | gunzip_outbuf_count = (w); | ||
556 | if (n) inflate_codes_resumeCopy = 1; | ||
557 | else inflate_codes_resumeCopy = 0; | ||
558 | |||
559 | w = 0; | ||
560 | return 1; | ||
561 | } | ||
562 | } while (n); | ||
563 | inflate_codes_resumeCopy = 0; | ||
564 | } | ||
565 | } | ||
566 | |||
567 | /* restore the globals from the locals */ | ||
568 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ | ||
569 | gunzip_bb = b; /* restore global bit buffer */ | ||
570 | gunzip_bk = k; | ||
571 | |||
572 | /* normally just after call to inflate_codes, but save code by putting it | ||
573 | here */ | ||
574 | /* free the decoding tables, return */ | ||
575 | huft_free(tl,HUFT_MMP1); | ||
576 | huft_free(td,HUFT_MMP2); | ||
577 | |||
578 | /* done */ | ||
579 | return 0; | ||
580 | } | ||
581 | |||
582 | static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) | ||
583 | { | ||
584 | static unsigned int n, b_stored, k_stored, w; | ||
585 | if (setup) | ||
586 | { | ||
587 | n = my_n; | ||
588 | b_stored = my_b_stored; | ||
589 | k_stored = my_k_stored; | ||
590 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
591 | return 0; /* Don't do anything first time */ | ||
592 | } | ||
593 | |||
594 | /* read and output the compressed data */ | ||
595 | while (n--) | ||
596 | { | ||
597 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); | ||
598 | gunzip_window[w++] = (unsigned char) b_stored; | ||
599 | if (w == gunzip_wsize) | ||
600 | { | ||
601 | gunzip_outbuf_count = (w); | ||
602 | w = 0; | ||
603 | b_stored >>= 8; | ||
604 | k_stored -= 8; | ||
605 | return 1; /* We have a block */ | ||
606 | } | ||
607 | |||
608 | b_stored >>= 8; | ||
609 | k_stored -= 8; | ||
610 | } | ||
611 | |||
612 | /* restore the globals from the locals */ | ||
613 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ | ||
614 | gunzip_bb = b_stored; /* restore global bit buffer */ | ||
615 | gunzip_bk = k_stored; | ||
616 | return 0; /* Finished */ | ||
617 | } | ||
618 | |||
619 | /* | ||
620 | * decompress an inflated block e: last block flag | ||
621 | * | ||
622 | * GLOBAL VARIABLES: bb, kk, | ||
623 | */ | ||
624 | /* Return values: -1 = inflate_stored, -2 = inflate_codes */ | ||
625 | static int inflate_block(int *e) | ||
626 | { | ||
627 | unsigned t; /* block type */ | ||
628 | unsigned int b; /* bit buffer */ | ||
629 | unsigned int k; /* number of bits in bit buffer */ | ||
630 | |||
631 | /* make local bit buffer */ | ||
632 | |||
633 | b = gunzip_bb; | ||
634 | k = gunzip_bk; | ||
635 | |||
636 | /* read in last block bit */ | ||
637 | b = fill_bitbuffer(b, &k, 1); | ||
638 | *e = (int) b & 1; | ||
639 | b >>= 1; | ||
640 | k -= 1; | ||
641 | |||
642 | /* read in block type */ | ||
643 | b = fill_bitbuffer(b, &k, 2); | ||
644 | t = (unsigned) b & 3; | ||
645 | b >>= 2; | ||
646 | k -= 2; | ||
647 | |||
648 | /* restore the global bit buffer */ | ||
649 | gunzip_bb = b; | ||
650 | gunzip_bk = k; | ||
651 | |||
652 | /* inflate that block type */ | ||
653 | switch (t) | ||
654 | { | ||
655 | case 0: /* Inflate stored */ | ||
656 | { | ||
657 | unsigned int n=0; /* number of bytes in block */ | ||
658 | |||
659 | /* make local copies of globals */ | ||
660 | unsigned int b_stored=gunzip_bb; /* bit buffer */ | ||
661 | unsigned int k_stored=gunzip_bk; /* number of bits in bit buffer */ | ||
662 | |||
663 | /* go to byte boundary */ | ||
664 | n = k_stored & 7; | ||
665 | b_stored >>= n; | ||
666 | k_stored -= n; | ||
667 | |||
668 | /* get the length and its complement */ | ||
669 | b_stored = fill_bitbuffer(b_stored, &k_stored, 16); | ||
670 | n = ((unsigned) b_stored & 0xffff); | ||
671 | b_stored >>= 16; | ||
672 | k_stored -= 16; | ||
673 | |||
674 | b_stored = fill_bitbuffer(b_stored, &k_stored, 16); | ||
675 | if (n != (unsigned) ((~b_stored) & 0xffff)) | ||
676 | return 1; /* error in compressed data */ | ||
677 | |||
678 | b_stored >>= 16; | ||
679 | k_stored -= 16; | ||
680 | |||
681 | inflate_stored(n, b_stored, k_stored, 1); /* Setup inflate_stored */ | ||
682 | return -1; | ||
683 | } | ||
684 | case 1: /* Inflate fixed decompress an inflated type 1 (fixed | ||
685 | Huffman codes) block. We should either replace this | ||
686 | with a custom decoder, or at least precompute the | ||
687 | Huffman tables. | ||
688 | */ | ||
689 | { | ||
690 | int i; /* temporary variable */ | ||
691 | huft_t *tl; /* literal/length code table */ | ||
692 | huft_t *td; /* distance code table */ | ||
693 | unsigned int bl; /* lookup bits for tl */ | ||
694 | unsigned int bd; /* lookup bits for td */ | ||
695 | unsigned int l[288]; /* length list for huft_build */ | ||
696 | |||
697 | /* set up literal table */ | ||
698 | for (i = 0; i < 144; i++) | ||
699 | { | ||
700 | l[i] = 8; | ||
701 | } | ||
702 | for (; i < 256; i++) | ||
703 | { | ||
704 | l[i] = 9; | ||
705 | } | ||
706 | for (; i < 280; i++) | ||
707 | { | ||
708 | l[i] = 7; | ||
709 | } | ||
710 | for (; i < 288; i++) /* make a complete, but wrong code set */ | ||
711 | { | ||
712 | l[i] = 8; | ||
713 | } | ||
714 | bl = 7; | ||
715 | if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl,HUFT_MMP1)) != 0) | ||
716 | return i; | ||
717 | |||
718 | /* set up distance table */ | ||
719 | for (i = 0; i < 30; i++) /* make an incomplete code set */ | ||
720 | { | ||
721 | l[i] = 5; | ||
722 | } | ||
723 | bd = 5; | ||
724 | if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd,HUFT_MMP2)) > 1) | ||
725 | { | ||
726 | huft_free(tl,HUFT_MMP1); | ||
727 | return i; | ||
728 | } | ||
729 | |||
730 | /* decompress until an end-of-block code */ | ||
731 | inflate_codes(tl, td, bl, bd, 1); /* Setup inflate_codes */ | ||
732 | |||
733 | /* huft_free code moved into inflate_codes */ | ||
734 | |||
735 | return -2; | ||
736 | } | ||
737 | case 2: /* Inflate dynamic */ | ||
738 | { | ||
739 | const int dbits = 6; /* bits in base distance lookup table */ | ||
740 | const int lbits = 9; /* bits in base literal/length lookup table */ | ||
741 | |||
742 | huft_t *tl; /* literal/length code table */ | ||
743 | huft_t *td; /* distance code table */ | ||
744 | unsigned int i; /* temporary variables */ | ||
745 | unsigned int j; | ||
746 | unsigned int l; /* last length */ | ||
747 | unsigned int m; /* mask for bit lengths table */ | ||
748 | unsigned int n; /* number of lengths to get */ | ||
749 | unsigned int bl; /* lookup bits for tl */ | ||
750 | unsigned int bd; /* lookup bits for td */ | ||
751 | unsigned int nb; /* number of bit length codes */ | ||
752 | unsigned int nl; /* number of literal/length codes */ | ||
753 | unsigned int nd; /* number of distance codes */ | ||
754 | |||
755 | unsigned int ll[286 + 30]; /* literal/length and distance code | ||
756 | lengths */ | ||
757 | unsigned int b_dynamic; /* bit buffer */ | ||
758 | unsigned int k_dynamic; /* number of bits in bit buffer */ | ||
759 | |||
760 | /* make local bit buffer */ | ||
761 | b_dynamic = gunzip_bb; | ||
762 | k_dynamic = gunzip_bk; | ||
763 | |||
764 | /* read in table lengths */ | ||
765 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5); | ||
766 | nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of | ||
767 | literal/length codes | ||
768 | */ | ||
769 | |||
770 | b_dynamic >>= 5; | ||
771 | k_dynamic -= 5; | ||
772 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5); | ||
773 | nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance | ||
774 | codes */ | ||
775 | |||
776 | b_dynamic >>= 5; | ||
777 | k_dynamic -= 5; | ||
778 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4); | ||
779 | nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length | ||
780 | codes */ | ||
781 | |||
782 | b_dynamic >>= 4; | ||
783 | k_dynamic -= 4; | ||
784 | if (nl > 286 || nd > 30) | ||
785 | return 1; /* bad lengths */ | ||
786 | |||
787 | /* read in bit-length-code lengths */ | ||
788 | for (j = 0; j < nb; j++) | ||
789 | { | ||
790 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3); | ||
791 | ll[border[j]] = (unsigned int) b_dynamic & 7; | ||
792 | b_dynamic >>= 3; | ||
793 | k_dynamic -= 3; | ||
794 | } | ||
795 | for (; j < 19; j++) | ||
796 | { | ||
797 | ll[border[j]] = 0; | ||
798 | } | ||
799 | |||
800 | /* build decoding table for trees--single level, 7 bit lookup */ | ||
801 | bl = 7; | ||
802 | i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl,HUFT_MMP1); | ||
803 | if (i != 0) | ||
804 | { | ||
805 | if (i == 1) | ||
806 | huft_free(tl,HUFT_MMP1); | ||
807 | |||
808 | return i; /* incomplete code set */ | ||
809 | } | ||
810 | |||
811 | /* read in literal and distance code lengths */ | ||
812 | n = nl + nd; | ||
813 | m = mask_bits[bl]; | ||
814 | i = l = 0; | ||
815 | while ((unsigned int) i < n) | ||
816 | { | ||
817 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl); | ||
818 | j = (td = tl + ((unsigned int) b_dynamic & m))->b; | ||
819 | b_dynamic >>= j; | ||
820 | k_dynamic -= j; | ||
821 | j = td->v.n; | ||
822 | if (j < 16) /* length of code in bits (0..15) */ | ||
823 | ll[i++] = l = j; /* save last length in l */ | ||
824 | else if (j == 16) /* repeat last length 3 to 6 times */ | ||
825 | { | ||
826 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2); | ||
827 | j = 3 + ((unsigned int) b_dynamic & 3); | ||
828 | b_dynamic >>= 2; | ||
829 | k_dynamic -= 2; | ||
830 | if ((unsigned int) i + j > n) | ||
831 | return 1; | ||
832 | |||
833 | while (j--) | ||
834 | { | ||
835 | ll[i++] = l; | ||
836 | } | ||
837 | } | ||
838 | else if (j == 17) /* 3 to 10 zero length codes */ | ||
839 | { | ||
840 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3); | ||
841 | j = 3 + ((unsigned int) b_dynamic & 7); | ||
842 | b_dynamic >>= 3; | ||
843 | k_dynamic -= 3; | ||
844 | if ((unsigned int) i + j > n) | ||
845 | return 1; | ||
846 | |||
847 | while (j--) | ||
848 | { | ||
849 | ll[i++] = 0; | ||
850 | } | ||
851 | l = 0; | ||
852 | } | ||
853 | else /* j == 18: 11 to 138 zero length codes */ | ||
854 | { | ||
855 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7); | ||
856 | j = 11 + ((unsigned int) b_dynamic & 0x7f); | ||
857 | b_dynamic >>= 7; | ||
858 | k_dynamic -= 7; | ||
859 | if ((unsigned int) i + j > n) | ||
860 | return 1; | ||
861 | |||
862 | while (j--) | ||
863 | { | ||
864 | ll[i++] = 0; | ||
865 | } | ||
866 | l = 0; | ||
867 | } | ||
868 | } | ||
869 | |||
870 | /* free decoding table for trees */ | ||
871 | huft_free(tl,HUFT_MMP1); | ||
872 | |||
873 | /* restore the global bit buffer */ | ||
874 | gunzip_bb = b_dynamic; | ||
875 | gunzip_bk = k_dynamic; | ||
876 | |||
877 | /* build the decoding tables for literal/length and distance codes */ | ||
878 | bl = lbits; | ||
879 | |||
880 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl,HUFT_MMP1)) != 0) | ||
881 | { | ||
882 | if (i == 1) | ||
883 | { | ||
884 | error_die("Incomplete literal tree"); | ||
885 | huft_free(tl,HUFT_MMP1); | ||
886 | } | ||
887 | |||
888 | return i; /* incomplete code set */ | ||
889 | } | ||
890 | |||
891 | bd = dbits; | ||
892 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd,HUFT_MMP2)) != 0) | ||
893 | { | ||
894 | if (i == 1) | ||
895 | { | ||
896 | error_die("incomplete distance tree"); | ||
897 | huft_free(td,HUFT_MMP2); | ||
898 | } | ||
899 | |||
900 | huft_free(tl,HUFT_MMP1); | ||
901 | return i; /* incomplete code set */ | ||
902 | } | ||
903 | |||
904 | /* decompress until an end-of-block code */ | ||
905 | inflate_codes(tl, td, bl, bd, 1); /* Setup inflate_codes */ | ||
906 | |||
907 | /* huft_free code moved into inflate_codes */ | ||
908 | |||
909 | return -2; | ||
910 | } | ||
911 | default: | ||
912 | /* bad block type */ | ||
913 | error_die("bad block type"); | ||
914 | } | ||
915 | return 0; | ||
916 | } | ||
917 | |||
918 | static void calculate_gunzip_crc(void) | ||
919 | { | ||
920 | unsigned int n; | ||
921 | for (n = 0; n < gunzip_outbuf_count; n++) | ||
922 | { | ||
923 | gunzip_crc = crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] | ||
924 | ^ (gunzip_crc >> 8); | ||
925 | } | ||
926 | gunzip_bytes_out += gunzip_outbuf_count; | ||
927 | } | ||
928 | |||
929 | static int inflate_get_next_window_method = -1; /* Method == -1 for stored, -2 | ||
930 | for codes */ | ||
931 | static int inflate_get_next_window_e = 0; | ||
932 | static int inflate_get_next_window_needAnotherBlock = 1; | ||
933 | |||
934 | static int inflate_get_next_window(void) | ||
935 | { | ||
936 | gunzip_outbuf_count = 0; | ||
937 | |||
938 | while(1) | ||
939 | { | ||
940 | int ret=0; | ||
941 | if (inflate_get_next_window_needAnotherBlock) | ||
942 | { | ||
943 | if(inflate_get_next_window_e) | ||
944 | { | ||
945 | calculate_gunzip_crc(); | ||
946 | inflate_get_next_window_e = 0; | ||
947 | inflate_get_next_window_needAnotherBlock = 1; | ||
948 | return 0; | ||
949 | } /* Last block */ | ||
950 | |||
951 | inflate_get_next_window_method = inflate_block(&inflate_get_next_window_e); | ||
952 | inflate_get_next_window_needAnotherBlock = 0; | ||
953 | } | ||
954 | |||
955 | switch (inflate_get_next_window_method) | ||
956 | { | ||
957 | case -1: ret = inflate_stored(0,0,0,0); | ||
958 | break; | ||
959 | case -2: ret = inflate_codes(0,0,0,0,0); | ||
960 | break; | ||
961 | default: | ||
962 | error_die("inflate error"); | ||
963 | } | ||
964 | |||
965 | if (ret == 1) | ||
966 | { | ||
967 | calculate_gunzip_crc(); | ||
968 | return 1; /* More data left */ | ||
969 | } | ||
970 | else inflate_get_next_window_needAnotherBlock = 1; /* End of that | ||
971 | block */ | ||
972 | } | ||
973 | /* Doesnt get here */ | ||
974 | } | ||
975 | |||
976 | /* Initialise bytebuffer, be careful not to overfill the buffer */ | ||
977 | static void inflate_init(unsigned int bufsize) | ||
978 | { | ||
979 | /* Set the bytebuffer size, default is same as gunzip_wsize */ | ||
980 | bytebuffer_max = bufsize + 8; | ||
981 | bytebuffer_offset = 4; | ||
982 | bytebuffer_size = 0; | ||
983 | } | ||
984 | |||
985 | static void inflate_cleanup(void) | ||
986 | { | ||
987 | /* free(bytebuffer); */ | ||
988 | } | ||
989 | |||
990 | USE_DESKTOP(long long) static int | ||
991 | inflate_unzip(struct mbreader_t *in,char* outbuffer,uint32_t outbuflen) | ||
992 | { | ||
993 | USE_DESKTOP(long long total = 0; ) | ||
994 | typedef void (*sig_type)(int); | ||
995 | |||
996 | /* Allocate all global buffers (for DYN_ALLOC option) */ | ||
997 | gunzip_outbuf_count = 0; | ||
998 | gunzip_bytes_out = 0; | ||
999 | gunzip_src_md = in; | ||
1000 | |||
1001 | /* initialize gunzip_window, bit buffer */ | ||
1002 | gunzip_bk = 0; | ||
1003 | gunzip_bb = 0; | ||
1004 | |||
1005 | /* Create the crc table */ | ||
1006 | gunzip_crc = ~0; | ||
1007 | |||
1008 | /* Allocate space for buffer */ | ||
1009 | while(1) | ||
1010 | { | ||
1011 | int ret = inflate_get_next_window(); | ||
1012 | if((signed int)outbuflen-(signed int)gunzip_outbuf_count<0) | ||
1013 | { | ||
1014 | error_msg("write_error"); | ||
1015 | #ifdef TRIM_FILE_ON_ERROR | ||
1016 | return USE_DESKTOP(total) + 0; | ||
1017 | #else | ||
1018 | return -1; | ||
1019 | #endif | ||
1020 | } | ||
1021 | |||
1022 | memcpy(outbuffer,gunzip_window,gunzip_outbuf_count); | ||
1023 | outbuffer+=sizeof(char)*gunzip_outbuf_count; | ||
1024 | ifl_total+=sizeof(char)*gunzip_outbuf_count; | ||
1025 | outbuflen-=gunzip_outbuf_count; | ||
1026 | USE_DESKTOP(total += gunzip_outbuf_count; ) | ||
1027 | if (ret == 0) break; | ||
1028 | } | ||
1029 | |||
1030 | /* Store unused bytes in a global buffer so calling applets can access it */ | ||
1031 | if (gunzip_bk >= 8) | ||
1032 | { | ||
1033 | /* Undo too much lookahead. The next read will be byte aligned so we can | ||
1034 | discard unused bits in the last meaningful byte. */ | ||
1035 | bytebuffer_offset--; | ||
1036 | bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff; | ||
1037 | gunzip_bb >>= 8; | ||
1038 | gunzip_bk -= 8; | ||
1039 | } | ||
1040 | |||
1041 | return USE_DESKTOP(total) + 0; | ||
1042 | } | ||
1043 | |||
1044 | USE_DESKTOP(long long) static int | ||
1045 | inflate_gunzip(struct mbreader_t *in,char* outbuffer,uint32_t outbuflen) | ||
1046 | { | ||
1047 | uint32_t stored_crc = 0; | ||
1048 | unsigned int count; | ||
1049 | USE_DESKTOP(long long total = ) inflate_unzip(in, outbuffer,outbuflen); | ||
1050 | |||
1051 | USE_DESKTOP(if (total < 0) return total; | ||
1052 | |||
1053 | ) | ||
1054 | |||
1055 | /* top up the input buffer with the rest of the trailer */ | ||
1056 | count = bytebuffer_size - bytebuffer_offset; | ||
1057 | if (count < 8) | ||
1058 | { | ||
1059 | xread(in, &bytebuffer[bytebuffer_size], 8 - count); | ||
1060 | bytebuffer_size += 8 - count; | ||
1061 | } | ||
1062 | |||
1063 | for (count = 0; count != 4; count++) | ||
1064 | { | ||
1065 | stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8)); | ||
1066 | bytebuffer_offset++; | ||
1067 | } | ||
1068 | |||
1069 | /* Validate decompression - crc */ | ||
1070 | if (stored_crc != (~gunzip_crc)) | ||
1071 | { | ||
1072 | error_msg("crc error"); | ||
1073 | |||
1074 | #ifdef TRIM_FILE_ON_ERROR | ||
1075 | return USE_DESKTOP(total) + 0; | ||
1076 | #else | ||
1077 | return -1; | ||
1078 | #endif | ||
1079 | } | ||
1080 | |||
1081 | /* Validate decompression - size */ | ||
1082 | if ((signed int)gunzip_bytes_out != | ||
1083 | (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) | | ||
1084 | (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) | ||
1085 | { | ||
1086 | error_msg("incorrect length"); | ||
1087 | return -1; | ||
1088 | } | ||
1089 | |||
1090 | return USE_DESKTOP(total) + 0; | ||
1091 | } | ||
1092 | |||
1093 | /*An allocated memory buffer at least 0x13100 (72448) bytes long*/ | ||
1094 | uint32_t decompress(const char *inbuffer,uint32_t inbuflen,char* outbuffer,uint32_t outbuflen,uint32_t offset,char* membuf) | ||
1095 | { | ||
1096 | signed char status=0; | ||
1097 | int exitcode=0; | ||
1098 | struct mbreader_t src_md; | ||
1099 | ifl_total=0; | ||
1100 | /* reset statics */ | ||
1101 | inflate_codes_resumeCopy = 0; | ||
1102 | inflate_get_next_window_method = -1; /* Method == -1 for stored, -2 for | ||
1103 | codes */ | ||
1104 | inflate_get_next_window_e = 0; | ||
1105 | inflate_get_next_window_needAnotherBlock = 1; | ||
1106 | /* init */ | ||
1107 | inflate_init(0x8000-8); | ||
1108 | /*Memory init*/ | ||
1109 | huftbuffer1=membuf; | ||
1110 | huftbuffer2=membuf+0x2A00; | ||
1111 | gunzip_window=membuf+0x2A00+0xA00; | ||
1112 | bytebuffer=membuf+0x2A00+0xA00+0x8000; | ||
1113 | wpw_init_mempool_pdm(HUFT_MMP1,(unsigned char*)huftbuffer1,0x2A00); | ||
1114 | wpw_init_mempool_pdm(HUFT_MMP2,(unsigned char*)huftbuffer2,0xA00); | ||
1115 | |||
1116 | /* Initialize memory buffer reader */ | ||
1117 | src_md.ptr = inbuffer; | ||
1118 | src_md.size = inbuflen; | ||
1119 | src_md.offset = offset; | ||
1120 | |||
1121 | if ((exitcode=xread_char(&src_md)) == 0x1f) | ||
1122 | { | ||
1123 | unsigned char magic2; | ||
1124 | magic2 = xread_char(&src_md); | ||
1125 | if (magic2 == 0x8b) | ||
1126 | { | ||
1127 | check_header_gzip(&src_md); /* FIXME: xfunc? _or_die? */ | ||
1128 | status = inflate_gunzip(&src_md, outbuffer,outbuflen); | ||
1129 | } | ||
1130 | else | ||
1131 | { | ||
1132 | error_msg("invalid magic"); | ||
1133 | exitcode = -1; | ||
1134 | } | ||
1135 | |||
1136 | if (status < 0) | ||
1137 | { | ||
1138 | error_msg("error inflating"); | ||
1139 | exitcode = -1; | ||
1140 | } | ||
1141 | } | ||
1142 | else | ||
1143 | { | ||
1144 | error_msg("invalid magic"); | ||
1145 | exitcode = -1; | ||
1146 | } | ||
1147 | |||
1148 | inflate_cleanup(); | ||
1149 | wpw_destroy_mempool(HUFT_MMP1); | ||
1150 | wpw_destroy_mempool(HUFT_MMP2); | ||
1151 | |||
1152 | if(exitcode==-1) | ||
1153 | return 0; | ||
1154 | |||
1155 | return ifl_total; | ||
1156 | } | ||