SEA3DDeflate.js 21 KB

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