otsdaq_utilities  v2_05_02_indev
rawinflate.js
1 /*
2  * $Id$
3  *
4  * original:
5  * http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
6  */
7 
8 
9 (function( factory ) {
10  if ( typeof define === "function" && define.amd ) {
11  define( ['JSRootCore'], factory );
12  } else
13  if (typeof exports === 'object' && typeof module !== 'undefined') {
14  factory(require("./JSRootCore.js"));
15  } else {
16  if (typeof JSROOT == 'undefined')
17  throw new Error('JSROOT is not defined', 'rawinflate.js');
18 
19  if (typeof JSROOT.ZIP !== 'undefined')
20  throw new Error('JSROOT.ZIP already exists', 'rawinflate.js');
21 
22  factory(JSROOT);
23  }
24 } (function(JSROOT) {
25 
26  "use strict";
27 
28 /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
29  * Version: 1.0.0.1
30  * LastModified: Dec 25 1999
31  */
32 
33 /* Interface:
34  * data = zip_inflate(src);
35  */
36 
37 /* constant parameters */
38 var zip_WSIZE = 32768, // Sliding Window size
39 // zip_STORED_BLOCK = 0,
40 // zip_STATIC_TREES = 1,
41 // zip_DYN_TREES = 2,
42 
43 /* for inflate */
44 // zip_lbits = 9, // bits in base literal/length lookup table
45 // zip_dbits = 6, // bits in base distance lookup table
46 // zip_INBUFSIZ = 32768, // Input buffer size
47 // zip_INBUF_EXTRA = 64, // Extra buffer
48 
49 /* variables (inflate) */
50  zip_slide = null,
51  zip_wp, // current position in slide
52  zip_fixed_tl = null, // inflate static
53  zip_fixed_td, // inflate static
54  zip_fixed_bl, zip_fixed_bd, // inflate static
55  zip_bit_buf, // bit buffer
56  zip_bit_len, // bits in bit buffer
57  zip_method,
58  zip_eof,
59  zip_copy_leng,
60  zip_copy_dist,
61  zip_tl, zip_td, // literal/length and distance decoder tables
62  zip_bl, zip_bd, // number of bits decoded by tl and td
63  zip_inflate_data,
64  zip_inflate_datalen,
65  zip_inflate_pos,
66 
67 /* constant tables (inflate) */
68  zip_MASK_BITS = new Array(
69  0x0000,
70  0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
71  0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff),
72 
73 // Tables for deflate from PKZIP's appnote.txt.
74  zip_cplens = new Array( // Copy lengths for literal codes 257..285
75  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
76  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0),
77 
78 /* note: see note #13 above about the 258 in this list. */
79  zip_cplext = new Array( // Extra bits for literal codes 257..285
80  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
81  3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99), // 99==invalid
82 
83  zip_cpdist = new Array( // Copy offsets for distance codes 0..29
84  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
85  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
86  8193, 12289, 16385, 24577),
87 
88  zip_cpdext = new Array( // Extra bits for distance codes
89  0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
90  7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
91  12, 12, 13, 13),
92 
93  zip_border = new Array( // Order of the bit length code lengths
94  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
95 /* objects (inflate) */
96 
97 var zip_HuftList = function() {
98  this.next = null;
99  this.list = null;
100 }
101 
102 var zip_HuftNode = function() {
103  this.e = 0; // number of extra bits or operation
104  this.b = 0; // number of bits in this code or subcode
105 
106  // union
107  this.n = 0; // literal, length base, or distance base
108  this.t = null; // (zip_HuftNode) pointer to next level of table
109 }
110 
111 var zip_HuftBuild = function(b, // code lengths in bits (all assumed <= BMAX)
112  n, // number of codes (assumed <= N_MAX)
113  s, // number of simple-valued codes (0..s-1)
114  d, // list of base values for non-simple codes
115  e, // list of extra bits for non-simple codes
116  mm ) { // maximum lookup bits
117 
118  this.status = 0; // 0: success, 1: incomplete table, 2: bad input
119  this.root = null; // (zip_HuftList) starting table
120  this.m = 0; // maximum lookup bits, returns actual
121 
122 /* Given a list of code lengths and a maximum table size, make a set of
123  tables to decode that set of codes. Return zero on success, one if
124  the given code set is incomplete (the tables are still built in this
125  case), two if the input is invalid (all zero length codes or an
126  oversubscribed set of lengths), and three if not enough memory.
127  The code with value 256 is special, and the tables are constructed
128  so that no bits beyond that code are fetched when that code is
129  decoded. */
130 
131  var BMAX = 16, // maximum bit length of any code
132  N_MAX = 288, // maximum number of codes in any set
133  c = new Array(BMAX+1), // bit length count table
134  lx = new Array(BMAX+1), // stack of bits per table
135  u = new Array(BMAX), // zip_HuftNode[BMAX][] table stack
136  v = new Array(N_MAX), // values in order of bit length
137  x = new Array(BMAX+1),// bit offsets, then code stack
138  r = new zip_HuftNode(), // table entry for structure assignment
139  rr = null, // temporary variable, use in assignment
140  a, // counter for codes of length k
141  el, // length of EOB code (value 256)
142  f, // i repeats in table every f entries
143  g, // maximum code length
144  h, // table level
145  i, // counter, current code
146  j, // counter
147  k, // number of bits in current code
148  p, // pointer into c[], b[], or v[]
149  pidx, // index of p
150  q, // (zip_HuftNode) points to current table
151  w,
152  xp, // pointer into x or c
153  y, // number of dummy codes added
154  z, // number of entries in current table
155  o,
156  tail = this.root = null; // (zip_HuftList)
157 
158  for (i=0; i<=BMAX; ++i) c[i] = lx[i] = x[i] = 0;
159  for (i=0; i<BMAX; ++i) u[i] = null;
160  for (i=0; i<N_MAX; ++i) v[i] = 0;
161 
162  // Generate counts for each bit length
163  el = (n > 256) ? b[256] : BMAX; // set length of EOB code, if any
164  p = b; pidx = 0; i = n;
165  do {
166  c[p[pidx++]]++; // assume all entries <= BMAX
167  } while (--i > 0);
168 
169  if (c[0] == n) { // null input--all zero length codes
170  this.root = null;
171  this.m = 0;
172  this.status = 0;
173  return this;
174  }
175 
176  // Find minimum and maximum length, bound *m by those
177  for (j = 1; j <= BMAX; ++j)
178  if (c[j] != 0)
179  break;
180  k = j; // minimum code length
181  if (mm < j)
182  mm = j;
183  for (i = BMAX; i != 0; --i)
184  if (c[i] != 0)
185  break;
186  g = i; // maximum code length
187  if (mm > i)
188  mm = i;
189 
190  // Adjust last length count to fill out codes, if needed
191  for (y = 1 << j; j < i; ++j, y <<= 1) {
192  if ((y -= c[j]) < 0) {
193  this.status = 2; // bad input: more codes than bits
194  this.m = mm;
195  return this;
196  }
197  }
198  if ((y -= c[i]) < 0) {
199  this.status = 2;
200  this.m = mm;
201  return this;
202  }
203  c[i] += y;
204 
205  // Generate starting offsets into the value table for each length
206  x[1] = j = 0;
207  p = c;
208  pidx = 1;
209  xp = 2;
210  while (--i > 0) // note that i == g from above
211  x[xp++] = (j += p[pidx++]);
212 
213  // Make a table of values in order of bit lengths
214  p = b; pidx = 0;
215  i = 0;
216  do {
217  if ((j = p[pidx++]) != 0)
218  v[x[j]++] = i;
219  } while (++i < n);
220  n = x[g]; // set n to length of v
221 
222  // Generate the Huffman codes and for each, make the table entries
223  x[0] = i = 0; // first Huffman code is zero
224  p = v; pidx = 0; // grab values in bit order
225  h = -1; // no tables yet--level -1
226  w = lx[0] = 0; // no bits decoded yet
227  q = null; // ditto
228  z = 0; // ditto
229 
230  // go through the bit lengths (k already is bits in shortest code)
231  for (; k <= g; ++k) {
232  a = c[k];
233  while (a-- > 0) {
234  // here i is the Huffman code of length k bits for value p[pidx]
235  // make tables up to required level
236  while (k > w + lx[1 + h]) {
237  w += lx[1 + h++]; // add bits already decoded
238 
239  // compute minimum size table less than or equal to *m bits
240  z = (z = g - w) > mm ? mm : z; // upper limit
241  if ((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
242  // too few codes for k-w bit table
243  f -= a + 1; // deduct codes from patterns left
244  xp = k;
245  while (++j < z) { // try smaller tables up to z bits
246  if ((f <<= 1) <= c[++xp])
247  break; // enough codes to use up j bits
248  f -= c[xp]; // else deduct codes from patterns
249  }
250  }
251  if (w + j > el && w < el)
252  j = el - w; // make EOB code end at table
253  z = 1 << j; // table entries for j-bit table
254  lx[1 + h] = j; // set table size in stack
255 
256  // allocate and link in new table
257  q = new Array(z);
258  for (o = 0; o < z; ++o) {
259  q[o] = new zip_HuftNode();
260  }
261 
262  if (tail == null)
263  tail = this.root = new zip_HuftList();
264  else
265  tail = tail.next = new zip_HuftList();
266  tail.next = null;
267  tail.list = q;
268  u[h] = q; // table starts after link
269 
270  /* connect to last table, if there is one */
271  if (h > 0) {
272  x[h] = i; // save pattern for backing up
273  r.b = lx[h]; // bits to dump before this table
274  r.e = 16 + j; // bits in this table
275  r.t = q; // pointer to this table
276  j = (i & ((1 << w) - 1)) >> (w - lx[h]);
277  rr = u[h-1][j];
278  rr.e = r.e;
279  rr.b = r.b;
280  rr.n = r.n;
281  rr.t = r.t;
282  }
283  }
284 
285  // set up table entry in r
286  r.b = k - w;
287  if (pidx >= n)
288  r.e = 99; // out of values--invalid code
289  else if (p[pidx] < s) {
290  r.e = (p[pidx] < 256 ? 16 : 15); // 256 is end-of-block code
291  r.n = p[pidx++]; // simple code is just the value
292  } else {
293  r.e = e[p[pidx] - s]; // non-simple--look up in lists
294  r.n = d[p[pidx++] - s];
295  }
296 
297  // fill code-like entries with r //
298  f = 1 << (k - w);
299  for (j = i >> w; j < z; j += f) {
300  rr = q[j];
301  rr.e = r.e;
302  rr.b = r.b;
303  rr.n = r.n;
304  rr.t = r.t;
305  }
306 
307  // backwards increment the k-bit code i
308  for (j = 1 << (k - 1); (i & j) != 0; j >>= 1)
309  i ^= j;
310  i ^= j;
311 
312  // backup over finished tables
313  while ((i & ((1 << w) - 1)) != x[h]) {
314  w -= lx[h--]; // don't need to update q
315  }
316  }
317  }
318 
319  /* return actual size of base table */
320  this.m = lx[1];
321 
322  /* Return true (1) if we were given an incomplete table */
323  this.status = ((y != 0 && g != 1) ? 1 : 0);
324  /* end of constructor */
325 
326  return this;
327 }
328 
329 /* routines (inflate) */
330 
331 var zip_NEEDBITS = function(n) {
332  while (zip_bit_len < n) {
333  if (zip_inflate_pos < zip_inflate_datalen)
334  zip_bit_buf |= zip_inflate_data[zip_inflate_pos++] << zip_bit_len;
335  zip_bit_len += 8;
336  }
337 }
338 
339 var zip_GETBITS = function(n) {
340  return zip_bit_buf & zip_MASK_BITS[n];
341 }
342 
343 var zip_DUMPBITS = function(n) {
344  zip_bit_buf >>= n;
345  zip_bit_len -= n;
346 }
347 
348 var zip_inflate_codes = function(buff, off, size) {
349  if (size == 0) return 0;
350 
351  /* inflate (decompress) the codes in a deflated (compressed) block.
352  Return an error code or zero if it all goes ok. */
353 
354  var e, // table entry flag/number of extra bits
355  t, // (zip_HuftNode) pointer to table entry
356  n = 0;
357 
358  // inflate the coded data
359  for (;;) { // do until end of block
360  zip_NEEDBITS(zip_bl);
361  t = zip_tl.list[zip_GETBITS(zip_bl)];
362  e = t.e;
363  while (e > 16) {
364  if (e == 99)
365  return -1;
366  zip_DUMPBITS(t.b);
367  e -= 16;
368  zip_NEEDBITS(e);
369  t = t.t[zip_GETBITS(e)];
370  e = t.e;
371  }
372  zip_DUMPBITS(t.b);
373 
374  if (e == 16) { // then it's a literal
375  zip_wp &= zip_WSIZE - 1;
376  buff[off + n++] = zip_slide[zip_wp++] = t.n;
377  if (n == size)
378  return size;
379  continue;
380  }
381 
382  // exit if end of block
383  if (e == 15)
384  break;
385 
386  // it's an EOB or a length
387 
388  // get length of block to copy
389  zip_NEEDBITS(e);
390  zip_copy_leng = t.n + zip_GETBITS(e);
391  zip_DUMPBITS(e);
392 
393  // decode distance of block to copy
394  zip_NEEDBITS(zip_bd);
395  t = zip_td.list[zip_GETBITS(zip_bd)];
396  e = t.e;
397 
398  while (e > 16) {
399  if (e == 99)
400  return -1;
401  zip_DUMPBITS(t.b);
402  e -= 16;
403  zip_NEEDBITS(e);
404  t = t.t[zip_GETBITS(e)];
405  e = t.e;
406  }
407  zip_DUMPBITS(t.b);
408  zip_NEEDBITS(e);
409  zip_copy_dist = zip_wp - t.n - zip_GETBITS(e);
410  zip_DUMPBITS(e);
411 
412  // do the copy
413  while (zip_copy_leng > 0 && n < size) {
414  --zip_copy_leng;
415  zip_copy_dist &= zip_WSIZE - 1;
416  zip_wp &= zip_WSIZE - 1;
417  buff[off + n++] = zip_slide[zip_wp++] = zip_slide[zip_copy_dist++];
418  }
419 
420  if (n == size)
421  return size;
422  }
423 
424  zip_method = -1; // done
425  return n;
426 }
427 
428 var zip_inflate_stored = function(buff, off, size) {
429  /* "decompress" an inflated type 0 (stored) block. */
430 
431  // go to byte boundary
432  var n = zip_bit_len & 7;
433  zip_DUMPBITS(n);
434 
435  // get the length and its complement
436  zip_NEEDBITS(16);
437  n = zip_GETBITS(16);
438  zip_DUMPBITS(16);
439  zip_NEEDBITS(16);
440  if (n != ((~zip_bit_buf) & 0xffff))
441  return -1; // error in compressed data
442  zip_DUMPBITS(16);
443 
444  // read and output the compressed data
445  zip_copy_leng = n;
446 
447  n = 0;
448  while (zip_copy_leng > 0 && n < size) {
449  --zip_copy_leng;
450  zip_wp &= zip_WSIZE - 1;
451  zip_NEEDBITS(8);
452  buff[off + n++] = zip_slide[zip_wp++] = zip_GETBITS(8);
453  zip_DUMPBITS(8);
454  }
455 
456  if (zip_copy_leng == 0)
457  zip_method = -1; // done
458  return n;
459 }
460 
461 var zip_inflate_fixed = function(buff, off, size) {
462  /* decompress an inflated type 1 (fixed Huffman codes) block. We should
463  either replace this with a custom decoder, or at least precompute the
464  Huffman tables. */
465 
466  // if first time, set up tables for fixed blocks
467  if (zip_fixed_tl == null) {
468  var i = 0, // temporary variable
469  l = new Array(288); // length list for huft_build
470 
471  // literal table
472  while (i < 144) l[i++] = 8;
473  while (i < 256) l[i++] = 9;
474  while (i < 280) l[i++] = 7;
475  while (i < 288) l[i++] = 8; // make a complete, but wrong code set
476  zip_fixed_bl = 7;
477 
478  var h = new zip_HuftBuild(l, 288, 257, zip_cplens, zip_cplext, zip_fixed_bl);
479  if (h.status != 0) {
480  throw new Error("HufBuild error: "+h.status,"rawinflate.js");
481  return -1;
482  }
483  zip_fixed_tl = h.root;
484  zip_fixed_bl = h.m;
485 
486  // distance table
487  for (i = 0; i<30; ++i) l[i] = 5;// make an incomplete code set
488  zip_fixed_bd = 5;
489 
490  h = new zip_HuftBuild(l, 30, 0, zip_cpdist, zip_cpdext, zip_fixed_bd);
491  if (h.status > 1) {
492  zip_fixed_tl = null;
493  throw new Error("HufBuild error: "+h.status,"rawinflate.js");
494  return -1;
495  }
496  zip_fixed_td = h.root;
497  zip_fixed_bd = h.m;
498  }
499 
500  zip_tl = zip_fixed_tl;
501  zip_td = zip_fixed_td;
502  zip_bl = zip_fixed_bl;
503  zip_bd = zip_fixed_bd;
504  return zip_inflate_codes(buff, off, size);
505 }
506 
507 var zip_inflate_dynamic = function(buff, off, size) {
508  // decompress an inflated type 2 (dynamic Huffman codes) block.
509  var i,j, // temporary variables
510  l, // last length
511  n, // number of lengths to get
512  t, // (zip_HuftNode) literal/length code table
513  h, // (zip_HuftBuild)
514  ll = new Array(286+30); // literal/length and distance code lengths
515 
516  for (i = 0; i < ll.length; ++i)
517  ll[i] = 0;
518 
519  // read in table lengths
520  zip_NEEDBITS(5);
521  var nl = 257 + zip_GETBITS(5); // number of literal/length codes
522  zip_DUMPBITS(5);
523  zip_NEEDBITS(5);
524  var nd = 1 + zip_GETBITS(5); // number of distance codes
525  zip_DUMPBITS(5);
526  zip_NEEDBITS(4);
527  var nb = 4 + zip_GETBITS(4); // number of bit length codes
528  zip_DUMPBITS(4);
529  if (nl > 286 || nd > 30)
530  return -1; // bad lengths
531 
532  // read in bit-length-code lengths
533  for (j = 0; j < nb; ++j) {
534  zip_NEEDBITS(3);
535  ll[zip_border[j]] = zip_GETBITS(3);
536  zip_DUMPBITS(3);
537  }
538  for (; j < 19; ++j)
539  ll[zip_border[j]] = 0;
540 
541  // build decoding table for trees--single level, 7 bit lookup
542  zip_bl = 7;
543  h = new zip_HuftBuild(ll, 19, 19, null, null, zip_bl);
544  if (h.status != 0)
545  return -1; // incomplete code set
546 
547  zip_tl = h.root;
548  zip_bl = h.m;
549 
550  // read in literal and distance code lengths
551  n = nl + nd;
552  i = l = 0;
553  while (i < n) {
554  zip_NEEDBITS(zip_bl);
555  t = zip_tl.list[zip_GETBITS(zip_bl)];
556  j = t.b;
557  zip_DUMPBITS(j);
558  j = t.n;
559  if (j < 16) // length of code in bits (0..15)
560  ll[i++] = l = j; // save last length in l
561  else if (j == 16) { // repeat last length 3 to 6 times
562  zip_NEEDBITS(2);
563  j = 3 + zip_GETBITS(2);
564  zip_DUMPBITS(2);
565  if (i + j > n)
566  return -1;
567  while (j-- > 0)
568  ll[i++] = l;
569  } else if (j == 17) { // 3 to 10 zero length codes
570  zip_NEEDBITS(3);
571  j = 3 + zip_GETBITS(3);
572  zip_DUMPBITS(3);
573  if (i + j > n)
574  return -1;
575  while (j-- > 0)
576  ll[i++] = 0;
577  l = 0;
578  } else { // j == 18: 11 to 138 zero length codes
579  zip_NEEDBITS(7);
580  j = 11 + zip_GETBITS(7);
581  zip_DUMPBITS(7);
582  if (i + j > n)
583  return -1;
584  while (j-- > 0)
585  ll[i++] = 0;
586  l = 0;
587  }
588  }
589 
590  // build the decoding tables for literal/length and distance codes
591  zip_bl = 9; // zip_lbits;
592  h = new zip_HuftBuild(ll, nl, 257, zip_cplens, zip_cplext, zip_bl);
593  if (zip_bl == 0) // no literals or lengths
594  h.status = 1;
595  if (h.status != 0) {
596  // if (h.status == 1); // **incomplete literal tree**
597  return -1; // incomplete code set
598  }
599  zip_tl = h.root;
600  zip_bl = h.m;
601 
602  for (i = 0; i < nd; ++i)
603  ll[i] = ll[i + nl];
604  zip_bd = 6; // zip_dbits;
605  h = new zip_HuftBuild(ll, nd, 0, zip_cpdist, zip_cpdext, zip_bd);
606  zip_td = h.root;
607  zip_bd = h.m;
608 
609  if (zip_bd == 0 && nl > 257) { // lengths but no distances
610  // **incomplete distance tree**
611  return -1;
612  }
613 
614  //if (h.status == 1); // **incomplete distance tree**
615 
616  if (h.status != 0)
617  return -1;
618 
619  // decompress until an end-of-block code
620  return zip_inflate_codes(buff, off, size);
621 }
622 
623 var zip_inflate_internal = function(buff, off, size) {
624  // decompress an inflated entry
625  var n = 0, i;
626 
627  while (n < size) {
628  if (zip_eof && zip_method == -1)
629  return n;
630 
631  if (zip_copy_leng > 0) {
632  if (zip_method != 0 /*zip_STORED_BLOCK*/) {
633  // STATIC_TREES or DYN_TREES
634  while (zip_copy_leng > 0 && n < size) {
635  --zip_copy_leng;
636  zip_copy_dist &= zip_WSIZE - 1;
637  zip_wp &= zip_WSIZE - 1;
638  buff[off + n++] = zip_slide[zip_wp++] =
639  zip_slide[zip_copy_dist++];
640  }
641  } else {
642  while (zip_copy_leng > 0 && n < size) {
643  --zip_copy_leng;
644  zip_wp &= zip_WSIZE - 1;
645  zip_NEEDBITS(8);
646  buff[off + n++] = zip_slide[zip_wp++] = zip_GETBITS(8);
647  zip_DUMPBITS(8);
648  }
649  if (zip_copy_leng == 0)
650  zip_method = -1; // done
651  }
652  if (n == size)
653  return n;
654  }
655 
656  if (zip_method == -1) {
657  if (zip_eof)
658  break;
659 
660  // read in last block bit
661  zip_NEEDBITS(1);
662  if (zip_GETBITS(1) != 0)
663  zip_eof = true;
664  zip_DUMPBITS(1);
665 
666  // read in block type
667  zip_NEEDBITS(2);
668  zip_method = zip_GETBITS(2);
669  zip_DUMPBITS(2);
670  zip_tl = null;
671  zip_copy_leng = 0;
672  }
673 
674  switch (zip_method) {
675  case 0: // zip_STORED_BLOCK
676  i = zip_inflate_stored(buff, off + n, size - n);
677  break;
678 
679  case 1: // zip_STATIC_TREES
680  if (zip_tl != null)
681  i = zip_inflate_codes(buff, off + n, size - n);
682  else
683  i = zip_inflate_fixed(buff, off + n, size - n);
684  break;
685 
686  case 2: // zip_DYN_TREES
687  if (zip_tl != null)
688  i = zip_inflate_codes(buff, off + n, size - n);
689  else
690  i = zip_inflate_dynamic(buff, off + n, size - n);
691  break;
692 
693  default: // error
694  i = -1;
695  break;
696  }
697 
698  if (i == -1)
699  return zip_eof ? 0 : -1;
700  n += i;
701  }
702  return n;
703 }
704 
705 JSROOT.ZIP = {};
706 
707 JSROOT.ZIP.inflate = function(arr, tgt)
708 {
709  if (!zip_slide)
710  zip_slide = new Array(2 * zip_WSIZE);
711  zip_wp = 0;
712  zip_bit_buf = 0;
713  zip_bit_len = 0;
714  zip_method = -1;
715  zip_eof = false;
716  zip_copy_leng = zip_copy_dist = 0;
717  zip_tl = null;
718 
719  zip_inflate_data = arr;
720  zip_inflate_datalen = arr.byteLength;
721  zip_inflate_pos = 0;
722 
723  var i, cnt = 0;
724  while ((i = zip_inflate_internal(tgt, cnt, Math.min(1024, tgt.byteLength-cnt))) > 0) {
725  cnt += i;
726  }
727  zip_inflate_data = null; // G.C.
728 
729  return cnt;
730 }
731 
740 JSROOT.LZ4 = {};
752 JSROOT.LZ4.uncompress = function (input, output, sIdx, eIdx) {
753  sIdx = sIdx || 0
754  eIdx = eIdx || (input.length - sIdx)
755  // Process each sequence in the incoming data
756  for (var i = sIdx, n = eIdx, j = 0; i < n;) {
757  var token = input[i++]
758 
759  // Literals
760  var literals_length = (token >> 4)
761  if (literals_length > 0) {
762  // length of literals
763  var l = literals_length + 240
764  while (l === 255) {
765  l = input[i++]
766  literals_length += l
767  }
768 
769  // Copy the literals
770  var end = i + literals_length
771  while (i < end) output[j++] = input[i++]
772 
773  // End of buffer?
774  if (i === n) return j
775  }
776 
777  // Match copy
778  // 2 bytes offset (little endian)
779  var offset = input[i++] | (input[i++] << 8)
780 
781  // 0 is an invalid offset value
782  if (offset === 0 || offset > j) return -(i-2)
783 
784  // length of match copy
785  var match_length = (token & 0xf)
786  var l = match_length + 240
787  while (l === 255) {
788  l = input[i++]
789  match_length += l
790  }
791 
792  // Copy the match
793  var pos = j - offset // position of the match copy in the current output
794  var end = j + match_length + 4 // minmatch = 4
795  while (j < end) output[j++] = output[pos++]
796  }
797 
798  return j
799 }
800 
801 
802 
803 return JSROOT;
804 
805 }));