2
* $Id: rawinflate.js,v 0.3 2013/04/09 14:25:38 dankogai Exp dankogai $
4
* GNU General Public License, version 2 (GPL-2.0)
5
* http://opensource.org/licenses/GPL-2.0
7
* http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
12
/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
14
* LastModified: Dec 25 1999
18
* data = zip_inflate(src);
21
/* constant parameters */
22
var zip_WSIZE = 32768; // Sliding Window size
23
var zip_STORED_BLOCK = 0;
24
var zip_STATIC_TREES = 1;
25
var zip_DYN_TREES = 2;
28
var zip_lbits = 9; // bits in base literal/length lookup table
29
var zip_dbits = 6; // bits in base distance lookup table
30
var zip_INBUFSIZ = 32768; // Input buffer size
31
var zip_INBUF_EXTRA = 64; // Extra buffer
33
/* variables (inflate) */
35
var zip_wp; // current position in slide
36
var zip_fixed_tl = null; // inflate static
37
var zip_fixed_td; // inflate static
38
var zip_fixed_bl, fixed_bd; // inflate static
39
var zip_bit_buf; // bit buffer
40
var zip_bit_len; // bits in bit buffer
45
var zip_tl, zip_td; // literal/length and distance decoder tables
46
var zip_bl, zip_bd; // number of bits decoded by tl and td
52
/* constant tables (inflate) */
53
var zip_MASK_BITS = new Array(
55
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
56
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff);
57
// Tables for deflate from PKZIP's appnote.txt.
58
var zip_cplens = new Array( // Copy lengths for literal codes 257..285
59
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
60
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0);
61
/* note: see note #13 above about the 258 in this list. */
62
var zip_cplext = new Array( // Extra bits for literal codes 257..285
63
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
64
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99); // 99==invalid
65
var zip_cpdist = new Array( // Copy offsets for distance codes 0..29
66
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
67
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
68
8193, 12289, 16385, 24577);
69
var zip_cpdext = new Array( // Extra bits for distance codes
70
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
71
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
73
var zip_border = new Array( // Order of the bit length code lengths
74
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
75
/* objects (inflate) */
77
var zip_HuftList = function() {
82
var zip_HuftNode = function() {
83
this.e = 0; // number of extra bits or operation
84
this.b = 0; // number of bits in this code or subcode
87
this.n = 0; // literal, length base, or distance base
88
this.t = null; // (zip_HuftNode) pointer to next level of table
91
var zip_HuftBuild = function(b, // code lengths in bits (all assumed <= BMAX)
92
n, // number of codes (assumed <= N_MAX)
93
s, // number of simple-valued codes (0..s-1)
94
d, // list of base values for non-simple codes
95
e, // list of extra bits for non-simple codes
96
mm // maximum lookup bits
98
this.BMAX = 16; // maximum bit length of any code
99
this.N_MAX = 288; // maximum number of codes in any set
100
this.status = 0; // 0: success, 1: incomplete table, 2: bad input
101
this.root = null; // (zip_HuftList) starting table
102
this.m = 0; // maximum lookup bits, returns actual
104
/* Given a list of code lengths and a maximum table size, make a set of
105
tables to decode that set of codes. Return zero on success, one if
106
the given code set is incomplete (the tables are still built in this
107
case), two if the input is invalid (all zero length codes or an
108
oversubscribed set of lengths), and three if not enough memory.
109
The code with value 256 is special, and the tables are constructed
110
so that no bits beyond that code are fetched when that code is
113
var a; // counter for codes of length k
114
var c = new Array(this.BMAX+1); // bit length count table
115
var el; // length of EOB code (value 256)
116
var f; // i repeats in table every f entries
117
var g; // maximum code length
118
var h; // table level
119
var i; // counter, current code
121
var k; // number of bits in current code
122
var lx = new Array(this.BMAX+1); // stack of bits per table
123
var p; // pointer into c[], b[], or v[]
124
var pidx; // index of p
125
var q; // (zip_HuftNode) points to current table
126
var r = new zip_HuftNode(); // table entry for structure assignment
127
var u = new Array(this.BMAX); // zip_HuftNode[BMAX][] table stack
128
var v = new Array(this.N_MAX); // values in order of bit length
130
var x = new Array(this.BMAX+1);// bit offsets, then code stack
131
var xp; // pointer into x or c
132
var y; // number of dummy codes added
133
var z; // number of entries in current table
135
var tail; // (zip_HuftList)
137
tail = this.root = null;
138
for(i = 0; i < c.length; i++)
140
for(i = 0; i < lx.length; i++)
142
for(i = 0; i < u.length; i++)
144
for(i = 0; i < v.length; i++)
146
for(i = 0; i < x.length; i++)
149
// Generate counts for each bit length
150
el = n > 256 ? b[256] : this.BMAX; // set length of EOB code, if any
154
c[p[pidx]]++; // assume all entries <= BMAX
157
if(c[0] == n) { // null input--all zero length codes
164
// Find minimum and maximum length, bound *m by those
165
for(j = 1; j <= this.BMAX; j++)
168
k = j; // minimum code length
171
for(i = this.BMAX; i != 0; i--)
174
g = i; // maximum code length
178
// Adjust last length count to fill out codes, if needed
179
for(y = 1 << j; j < i; j++, y <<= 1)
180
if((y -= c[j]) < 0) {
181
this.status = 2; // bad input: more codes than bits
185
if((y -= c[i]) < 0) {
192
// Generate starting offsets into the value table for each length
197
while(--i > 0) // note that i == g from above
198
x[xp++] = (j += p[pidx++]);
200
// Make a table of values in order of bit lengths
204
if((j = p[pidx++]) != 0)
207
n = x[g]; // set n to length of v
209
// Generate the Huffman codes and for each, make the table entries
210
x[0] = i = 0; // first Huffman code is zero
211
p = v; pidx = 0; // grab values in bit order
212
h = -1; // no tables yet--level -1
213
w = lx[0] = 0; // no bits decoded yet
217
// go through the bit lengths (k already is bits in shortest code)
221
// here i is the Huffman code of length k bits for value p[pidx]
222
// make tables up to required level
223
while(k > w + lx[1 + h]) {
224
w += lx[1 + h]; // add bits already decoded
227
// compute minimum size table less than or equal to *m bits
228
z = (z = g - w) > mm ? mm : z; // upper limit
229
if((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
230
// too few codes for k-w bit table
231
f -= a + 1; // deduct codes from patterns left
233
while(++j < z) { // try smaller tables up to z bits
234
if((f <<= 1) <= c[++xp])
235
break; // enough codes to use up j bits
236
f -= c[xp]; // else deduct codes from patterns
239
if(w + j > el && w < el)
240
j = el - w; // make EOB code end at table
241
z = 1 << j; // table entries for j-bit table
242
lx[1 + h] = j; // set table size in stack
244
// allocate and link in new table
246
for(o = 0; o < z; o++) {
247
q[o] = new zip_HuftNode();
251
tail = this.root = new zip_HuftList();
253
tail = tail.next = new zip_HuftList();
256
u[h] = q; // table starts after link
258
/* connect to last table, if there is one */
260
x[h] = i; // save pattern for backing up
261
r.b = lx[h]; // bits to dump before this table
262
r.e = 16 + j; // bits in this table
263
r.t = q; // pointer to this table
264
j = (i & ((1 << w) - 1)) >> (w - lx[h]);
272
// set up table entry in r
275
r.e = 99; // out of values--invalid code
276
else if(p[pidx] < s) {
277
r.e = (p[pidx] < 256 ? 16 : 15); // 256 is end-of-block code
278
r.n = p[pidx++]; // simple code is just the value
280
r.e = e[p[pidx] - s]; // non-simple--look up in lists
281
r.n = d[p[pidx++] - s];
284
// fill code-like entries with r //
286
for(j = i >> w; j < z; j += f) {
293
// backwards increment the k-bit code i
294
for(j = 1 << (k - 1); (i & j) != 0; j >>= 1)
298
// backup over finished tables
299
while((i & ((1 << w) - 1)) != x[h]) {
300
w -= lx[h]; // don't need to update q
306
/* return actual size of base table */
309
/* Return true (1) if we were given an incomplete table */
310
this.status = ((y != 0 && g != 1) ? 1 : 0);
311
} /* end of constructor */
315
/* routines (inflate) */
317
var zip_GET_BYTE = function() {
318
if(zip_inflate_data.length == zip_inflate_pos)
320
//return zip_inflate_data.charCodeAt(zip_inflate_pos++) & 0xff;
321
return zip_inflate_data[zip_inflate_pos++];
324
var zip_NEEDBITS = function(n) {
325
while(zip_bit_len < n) {
326
zip_bit_buf |= zip_GET_BYTE() << zip_bit_len;
331
var zip_GETBITS = function(n) {
332
return zip_bit_buf & zip_MASK_BITS[n];
335
var zip_DUMPBITS = function(n) {
340
var zip_inflate_codes = function(buff, off, size) {
341
/* inflate (decompress) the codes in a deflated (compressed) block.
342
Return an error code or zero if it all goes ok. */
343
var e; // table entry flag/number of extra bits
344
var t; // (zip_HuftNode) pointer to table entry
350
// inflate the coded data
352
for(;;) { // do until end of block
353
zip_NEEDBITS(zip_bl);
354
t = zip_tl.list[zip_GETBITS(zip_bl)];
362
t = t.t[zip_GETBITS(e)];
367
if(e == 16) { // then it's a literal
368
zip_wp &= zip_WSIZE - 1;
369
buff[off + n++] = zip_slide[zip_wp++] = t.n;
375
// exit if end of block
379
// it's an EOB or a length
381
// get length of block to copy
383
zip_copy_leng = t.n + zip_GETBITS(e);
386
// decode distance of block to copy
387
zip_NEEDBITS(zip_bd);
388
t = zip_td.list[zip_GETBITS(zip_bd)];
397
t = t.t[zip_GETBITS(e)];
402
zip_copy_dist = zip_wp - t.n - zip_GETBITS(e);
406
while(zip_copy_leng > 0 && n < size) {
408
zip_copy_dist &= zip_WSIZE - 1;
409
zip_wp &= zip_WSIZE - 1;
410
buff[off + n++] = zip_slide[zip_wp++]
411
= zip_slide[zip_copy_dist++];
418
zip_method = -1; // done
422
var zip_inflate_stored = function(buff, off, size) {
423
/* "decompress" an inflated type 0 (stored) block. */
426
// go to byte boundary
430
// get the length and its complement
435
if(n != ((~zip_bit_buf) & 0xffff))
436
return -1; // error in compressed data
439
// read and output the compressed data
443
while(zip_copy_leng > 0 && n < size) {
445
zip_wp &= zip_WSIZE - 1;
447
buff[off + n++] = zip_slide[zip_wp++] =
452
if(zip_copy_leng == 0)
453
zip_method = -1; // done
457
var zip_inflate_fixed = function(buff, off, size) {
458
/* decompress an inflated type 1 (fixed Huffman codes) block. We should
459
either replace this with a custom decoder, or at least precompute the
462
// if first time, set up tables for fixed blocks
463
if(zip_fixed_tl == null) {
464
var i; // temporary variable
465
var l = new Array(288); // length list for huft_build
466
var h; // zip_HuftBuild
469
for(i = 0; i < 144; i++)
475
for(; i < 288; i++) // make a complete, but wrong code set
479
h = new zip_HuftBuild(l, 288, 257, zip_cplens, zip_cplext,
482
alert("HufBuild error: "+h.status);
485
zip_fixed_tl = h.root;
489
for(i = 0; i < 30; i++) // make an incomplete code set
493
h = new zip_HuftBuild(l, 30, 0, zip_cpdist, zip_cpdext, zip_fixed_bd);
496
alert("HufBuild error: "+h.status);
499
zip_fixed_td = h.root;
503
zip_tl = zip_fixed_tl;
504
zip_td = zip_fixed_td;
505
zip_bl = zip_fixed_bl;
506
zip_bd = zip_fixed_bd;
507
return zip_inflate_codes(buff, off, size);
510
var zip_inflate_dynamic = function(buff, off, size) {
511
// decompress an inflated type 2 (dynamic Huffman codes) block.
512
var i; // temporary variables
514
var l; // last length
515
var n; // number of lengths to get
516
var t; // (zip_HuftNode) literal/length code table
517
var nb; // number of bit length codes
518
var nl; // number of literal/length codes
519
var nd; // number of distance codes
520
var ll = new Array(286+30); // literal/length and distance code lengths
521
var h; // (zip_HuftBuild)
523
for(i = 0; i < ll.length; i++)
526
// read in table lengths
528
nl = 257 + zip_GETBITS(5); // number of literal/length codes
531
nd = 1 + zip_GETBITS(5); // number of distance codes
534
nb = 4 + zip_GETBITS(4); // number of bit length codes
536
if(nl > 286 || nd > 30)
537
return -1; // bad lengths
539
// read in bit-length-code lengths
540
for(j = 0; j < nb; j++)
543
ll[zip_border[j]] = zip_GETBITS(3);
547
ll[zip_border[j]] = 0;
549
// build decoding table for trees--single level, 7 bit lookup
551
h = new zip_HuftBuild(ll, 19, 19, null, null, zip_bl);
553
return -1; // incomplete code set
558
// read in literal and distance code lengths
562
zip_NEEDBITS(zip_bl);
563
t = zip_tl.list[zip_GETBITS(zip_bl)];
567
if(j < 16) // length of code in bits (0..15)
568
ll[i++] = l = j; // save last length in l
569
else if(j == 16) { // repeat last length 3 to 6 times
571
j = 3 + zip_GETBITS(2);
577
} else if(j == 17) { // 3 to 10 zero length codes
579
j = 3 + zip_GETBITS(3);
586
} else { // j == 18: 11 to 138 zero length codes
588
j = 11 + zip_GETBITS(7);
598
// build the decoding tables for literal/length and distance codes
600
h = new zip_HuftBuild(ll, nl, 257, zip_cplens, zip_cplext, zip_bl);
601
if(zip_bl == 0) // no literals or lengths
605
;// **incomplete literal tree**
606
return -1; // incomplete code set
611
for(i = 0; i < nd; i++)
614
h = new zip_HuftBuild(ll, nd, 0, zip_cpdist, zip_cpdext, zip_bd);
618
if(zip_bd == 0 && nl > 257) { // lengths but no distances
619
// **incomplete distance tree**
624
;// **incomplete distance tree**
629
// decompress until an end-of-block code
630
return zip_inflate_codes(buff, off, size);
633
var zip_inflate_start = function() {
636
if(zip_slide == null)
637
zip_slide = new Array(2 * zip_WSIZE);
643
zip_copy_leng = zip_copy_dist = 0;
647
var zip_inflate_internal = function(buff, off, size) {
648
// decompress an inflated entry
653
if(zip_eof && zip_method == -1)
656
if(zip_copy_leng > 0) {
657
if(zip_method != zip_STORED_BLOCK) {
658
// STATIC_TREES or DYN_TREES
659
while(zip_copy_leng > 0 && n < size) {
661
zip_copy_dist &= zip_WSIZE - 1;
662
zip_wp &= zip_WSIZE - 1;
663
buff[off + n++] = zip_slide[zip_wp++] =
664
zip_slide[zip_copy_dist++];
667
while(zip_copy_leng > 0 && n < size) {
669
zip_wp &= zip_WSIZE - 1;
671
buff[off + n++] = zip_slide[zip_wp++] = zip_GETBITS(8);
674
if(zip_copy_leng == 0)
675
zip_method = -1; // done
681
if(zip_method == -1) {
685
// read in last block bit
687
if(zip_GETBITS(1) != 0)
691
// read in block type
693
zip_method = zip_GETBITS(2);
700
case 0: // zip_STORED_BLOCK
701
i = zip_inflate_stored(buff, off + n, size - n);
704
case 1: // zip_STATIC_TREES
706
i = zip_inflate_codes(buff, off + n, size - n);
708
i = zip_inflate_fixed(buff, off + n, size - n);
711
case 2: // zip_DYN_TREES
713
i = zip_inflate_codes(buff, off + n, size - n);
715
i = zip_inflate_dynamic(buff, off + n, size - n);
733
var zip_inflate = function(str) {
737
zip_inflate_data = str;
740
var buff = new Array(1024);
742
while((i = zip_inflate_internal(buff, 0, buff.length)) > 0) {
743
var cbuf = new Array(i);
744
for(j = 0; j < i; j++){
745
cbuf[j] = String.fromCharCode(buff[j]);
747
aout[aout.length] = cbuf.join("");
749
zip_inflate_data = null; // G.C.
750
return aout.join("");
753
if (! ctx.RawDeflate) ctx.RawDeflate = {};
754
ctx.RawDeflate.inflate = zip_inflate;