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