inflate.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. 'use strict';
  2. import {Buf8,Buf16,Buf32,arraySet} from './utils';
  3. import adler32 from './adler32';
  4. import crc32 from './crc32';
  5. import inflate_fast from './inffast';
  6. import inflate_table from './inftrees';
  7. var CODES = 0;
  8. var LENS = 1;
  9. var DISTS = 2;
  10. /* Public constants ==========================================================*/
  11. /* ===========================================================================*/
  12. /* Allowed flush values; see deflate() and inflate() below for details */
  13. //var Z_NO_FLUSH = 0;
  14. //var Z_PARTIAL_FLUSH = 1;
  15. //var Z_SYNC_FLUSH = 2;
  16. //var Z_FULL_FLUSH = 3;
  17. var Z_FINISH = 4;
  18. var Z_BLOCK = 5;
  19. var Z_TREES = 6;
  20. /* Return codes for the compression/decompression functions. Negative values
  21. * are errors, positive values are used for special but normal events.
  22. */
  23. var Z_OK = 0;
  24. var Z_STREAM_END = 1;
  25. var Z_NEED_DICT = 2;
  26. //var Z_ERRNO = -1;
  27. var Z_STREAM_ERROR = -2;
  28. var Z_DATA_ERROR = -3;
  29. var Z_MEM_ERROR = -4;
  30. var Z_BUF_ERROR = -5;
  31. //var Z_VERSION_ERROR = -6;
  32. /* The deflate compression method */
  33. var Z_DEFLATED = 8;
  34. /* STATES ====================================================================*/
  35. /* ===========================================================================*/
  36. var HEAD = 1; /* i: waiting for magic header */
  37. var FLAGS = 2; /* i: waiting for method and flags (gzip) */
  38. var TIME = 3; /* i: waiting for modification time (gzip) */
  39. var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  40. var EXLEN = 5; /* i: waiting for extra length (gzip) */
  41. var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  42. var NAME = 7; /* i: waiting for end of file name (gzip) */
  43. var COMMENT = 8; /* i: waiting for end of comment (gzip) */
  44. var HCRC = 9; /* i: waiting for header crc (gzip) */
  45. var DICTID = 10; /* i: waiting for dictionary check value */
  46. var DICT = 11; /* waiting for inflateSetDictionary() call */
  47. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  48. var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  49. var STORED = 14; /* i: waiting for stored size (length and complement) */
  50. var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  51. var COPY = 16; /* i/o: waiting for input or output to copy stored block */
  52. var TABLE = 17; /* i: waiting for dynamic block table lengths */
  53. var LENLENS = 18; /* i: waiting for code length code lengths */
  54. var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  55. var LEN_ = 20; /* i: same as LEN below, but only first time in */
  56. var LEN = 21; /* i: waiting for length/lit/eob code */
  57. var LENEXT = 22; /* i: waiting for length extra bits */
  58. var DIST = 23; /* i: waiting for distance code */
  59. var DISTEXT = 24; /* i: waiting for distance extra bits */
  60. var MATCH = 25; /* o: waiting for output space to copy string */
  61. var LIT = 26; /* o: waiting for output space to write literal */
  62. var CHECK = 27; /* i: waiting for 32-bit check value */
  63. var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  64. var DONE = 29; /* finished check, done -- remain here until reset */
  65. var BAD = 30; /* got a data error -- remain here until reset */
  66. var MEM = 31; /* got an inflate() memory error -- remain here until reset */
  67. var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  68. /* ===========================================================================*/
  69. var ENOUGH_LENS = 852;
  70. var ENOUGH_DISTS = 592;
  71. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  72. var MAX_WBITS = 15;
  73. /* 32K LZ77 window */
  74. var DEF_WBITS = MAX_WBITS;
  75. function zswap32(q) {
  76. return (((q >>> 24) & 0xff) +
  77. ((q >>> 8) & 0xff00) +
  78. ((q & 0xff00) << 8) +
  79. ((q & 0xff) << 24));
  80. }
  81. function InflateState() {
  82. this.mode = 0; /* current inflate mode */
  83. this.last = false; /* true if processing last block */
  84. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  85. this.havedict = false; /* true if dictionary provided */
  86. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  87. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  88. this.check = 0; /* protected copy of check value */
  89. this.total = 0; /* protected copy of output count */
  90. // TODO: may be {}
  91. this.head = null; /* where to save gzip header information */
  92. /* sliding window */
  93. this.wbits = 0; /* log base 2 of requested window size */
  94. this.wsize = 0; /* window size or zero if not using window */
  95. this.whave = 0; /* valid bytes in the window */
  96. this.wnext = 0; /* window write index */
  97. this.window = null; /* allocated sliding window, if needed */
  98. /* bit accumulator */
  99. this.hold = 0; /* input bit accumulator */
  100. this.bits = 0; /* number of bits in "in" */
  101. /* for string and stored block copying */
  102. this.length = 0; /* literal or length of data to copy */
  103. this.offset = 0; /* distance back to copy string from */
  104. /* for table and code decoding */
  105. this.extra = 0; /* extra bits needed */
  106. /* fixed and dynamic code tables */
  107. this.lencode = null; /* starting table for length/literal codes */
  108. this.distcode = null; /* starting table for distance codes */
  109. this.lenbits = 0; /* index bits for lencode */
  110. this.distbits = 0; /* index bits for distcode */
  111. /* dynamic table building */
  112. this.ncode = 0; /* number of code length code lengths */
  113. this.nlen = 0; /* number of length code lengths */
  114. this.ndist = 0; /* number of distance code lengths */
  115. this.have = 0; /* number of code lengths in lens[] */
  116. this.next = null; /* next available space in codes[] */
  117. this.lens = new Buf16(320); /* temporary storage for code lengths */
  118. this.work = new Buf16(288); /* work area for code table building */
  119. /*
  120. because we don't have pointers in js, we use lencode and distcode directly
  121. as buffers so we don't need codes
  122. */
  123. //this.codes = new Buf32(ENOUGH); /* space for code tables */
  124. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  125. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  126. this.sane = 0; /* if false, allow invalid distance too far */
  127. this.back = 0; /* bits back of last unprocessed length/lit */
  128. this.was = 0; /* initial length of match */
  129. }
  130. export function inflateResetKeep(strm) {
  131. var state;
  132. if (!strm || !strm.state) {
  133. return Z_STREAM_ERROR;
  134. }
  135. state = strm.state;
  136. strm.total_in = strm.total_out = state.total = 0;
  137. strm.msg = ''; /*Z_NULL*/
  138. if (state.wrap) { /* to support ill-conceived Java test suite */
  139. strm.adler = state.wrap & 1;
  140. }
  141. state.mode = HEAD;
  142. state.last = 0;
  143. state.havedict = 0;
  144. state.dmax = 32768;
  145. state.head = null /*Z_NULL*/ ;
  146. state.hold = 0;
  147. state.bits = 0;
  148. //state.lencode = state.distcode = state.next = state.codes;
  149. state.lencode = state.lendyn = new Buf32(ENOUGH_LENS);
  150. state.distcode = state.distdyn = new Buf32(ENOUGH_DISTS);
  151. state.sane = 1;
  152. state.back = -1;
  153. //Tracev((stderr, "inflate: reset\n"));
  154. return Z_OK;
  155. }
  156. export function inflateReset(strm) {
  157. var state;
  158. if (!strm || !strm.state) {
  159. return Z_STREAM_ERROR;
  160. }
  161. state = strm.state;
  162. state.wsize = 0;
  163. state.whave = 0;
  164. state.wnext = 0;
  165. return inflateResetKeep(strm);
  166. }
  167. export function inflateReset2(strm, windowBits) {
  168. var wrap;
  169. var state;
  170. /* get the state */
  171. if (!strm || !strm.state) {
  172. return Z_STREAM_ERROR;
  173. }
  174. state = strm.state;
  175. /* extract wrap request from windowBits parameter */
  176. if (windowBits < 0) {
  177. wrap = 0;
  178. windowBits = -windowBits;
  179. } else {
  180. wrap = (windowBits >> 4) + 1;
  181. if (windowBits < 48) {
  182. windowBits &= 15;
  183. }
  184. }
  185. /* set number of window bits, free window if different */
  186. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  187. return Z_STREAM_ERROR;
  188. }
  189. if (state.window !== null && state.wbits !== windowBits) {
  190. state.window = null;
  191. }
  192. /* update state and reset the rest of it */
  193. state.wrap = wrap;
  194. state.wbits = windowBits;
  195. return inflateReset(strm);
  196. }
  197. export function inflateInit2(strm, windowBits) {
  198. var ret;
  199. var state;
  200. if (!strm) {
  201. return Z_STREAM_ERROR;
  202. }
  203. //strm.msg = Z_NULL; /* in case we return an error */
  204. state = new InflateState();
  205. //if (state === Z_NULL) return Z_MEM_ERROR;
  206. //Tracev((stderr, "inflate: allocated\n"));
  207. strm.state = state;
  208. state.window = null /*Z_NULL*/ ;
  209. ret = inflateReset2(strm, windowBits);
  210. if (ret !== Z_OK) {
  211. strm.state = null /*Z_NULL*/ ;
  212. }
  213. return ret;
  214. }
  215. export function inflateInit(strm) {
  216. return inflateInit2(strm, DEF_WBITS);
  217. }
  218. /*
  219. Return state with length and distance decoding tables and index sizes set to
  220. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  221. If BUILDFIXED is defined, then instead this routine builds the tables the
  222. first time it's called, and returns those tables the first time and
  223. thereafter. This reduces the size of the code by about 2K bytes, in
  224. exchange for a little execution time. However, BUILDFIXED should not be
  225. used for threaded applications, since the rewriting of the tables and virgin
  226. may not be thread-safe.
  227. */
  228. var virgin = true;
  229. var lenfix, distfix; // We have no pointers in JS, so keep tables separate
  230. function fixedtables(state) {
  231. /* build fixed huffman tables if first call (may not be thread safe) */
  232. if (virgin) {
  233. var sym;
  234. lenfix = new Buf32(512);
  235. distfix = new Buf32(32);
  236. /* literal/length table */
  237. sym = 0;
  238. while (sym < 144) {
  239. state.lens[sym++] = 8;
  240. }
  241. while (sym < 256) {
  242. state.lens[sym++] = 9;
  243. }
  244. while (sym < 280) {
  245. state.lens[sym++] = 7;
  246. }
  247. while (sym < 288) {
  248. state.lens[sym++] = 8;
  249. }
  250. inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, {
  251. bits: 9
  252. });
  253. /* distance table */
  254. sym = 0;
  255. while (sym < 32) {
  256. state.lens[sym++] = 5;
  257. }
  258. inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, {
  259. bits: 5
  260. });
  261. /* do this just once */
  262. virgin = false;
  263. }
  264. state.lencode = lenfix;
  265. state.lenbits = 9;
  266. state.distcode = distfix;
  267. state.distbits = 5;
  268. }
  269. /*
  270. Update the window with the last wsize (normally 32K) bytes written before
  271. returning. If window does not exist yet, create it. This is only called
  272. when a window is already in use, or when output has been written during this
  273. inflate call, but the end of the deflate stream has not been reached yet.
  274. It is also called to create a window for dictionary data when a dictionary
  275. is loaded.
  276. Providing output buffers larger than 32K to inflate() should provide a speed
  277. advantage, since only the last 32K of output is copied to the sliding window
  278. upon return from inflate(), and since all distances after the first 32K of
  279. output will fall in the output data, making match copies simpler and faster.
  280. The advantage may be dependent on the size of the processor's data caches.
  281. */
  282. function updatewindow(strm, src, end, copy) {
  283. var dist;
  284. var state = strm.state;
  285. /* if it hasn't been done already, allocate space for the window */
  286. if (state.window === null) {
  287. state.wsize = 1 << state.wbits;
  288. state.wnext = 0;
  289. state.whave = 0;
  290. state.window = new Buf8(state.wsize);
  291. }
  292. /* copy state->wsize or less output bytes into the circular window */
  293. if (copy >= state.wsize) {
  294. arraySet(state.window, src, end - state.wsize, state.wsize, 0);
  295. state.wnext = 0;
  296. state.whave = state.wsize;
  297. } else {
  298. dist = state.wsize - state.wnext;
  299. if (dist > copy) {
  300. dist = copy;
  301. }
  302. //zmemcpy(state->window + state->wnext, end - copy, dist);
  303. arraySet(state.window, src, end - copy, dist, state.wnext);
  304. copy -= dist;
  305. if (copy) {
  306. //zmemcpy(state->window, end - copy, copy);
  307. arraySet(state.window, src, end - copy, copy, 0);
  308. state.wnext = copy;
  309. state.whave = state.wsize;
  310. } else {
  311. state.wnext += dist;
  312. if (state.wnext === state.wsize) {
  313. state.wnext = 0;
  314. }
  315. if (state.whave < state.wsize) {
  316. state.whave += dist;
  317. }
  318. }
  319. }
  320. return 0;
  321. }
  322. export function inflate(strm, flush) {
  323. var state;
  324. var input, output; // input/output buffers
  325. var next; /* next input INDEX */
  326. var put; /* next output INDEX */
  327. var have, left; /* available input and output */
  328. var hold; /* bit buffer */
  329. var bits; /* bits in bit buffer */
  330. var _in, _out; /* save starting available input and output */
  331. var copy; /* number of stored or match bytes to copy */
  332. var from; /* where to copy match bytes from */
  333. var from_source;
  334. var here = 0; /* current decoding table entry */
  335. var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  336. //var last; /* parent table entry */
  337. var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  338. var len; /* length to copy for repeats, bits to drop */
  339. var ret; /* return code */
  340. var hbuf = new Buf8(4); /* buffer for gzip header crc calculation */
  341. var opts;
  342. var n; // temporary var for NEED_BITS
  343. var order = /* permutation of code lengths */ [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
  344. if (!strm || !strm.state || !strm.output ||
  345. (!strm.input && strm.avail_in !== 0)) {
  346. return Z_STREAM_ERROR;
  347. }
  348. state = strm.state;
  349. if (state.mode === TYPE) {
  350. state.mode = TYPEDO;
  351. } /* skip check */
  352. //--- LOAD() ---
  353. put = strm.next_out;
  354. output = strm.output;
  355. left = strm.avail_out;
  356. next = strm.next_in;
  357. input = strm.input;
  358. have = strm.avail_in;
  359. hold = state.hold;
  360. bits = state.bits;
  361. //---
  362. _in = have;
  363. _out = left;
  364. ret = Z_OK;
  365. inf_leave: // goto emulation
  366. for (;;) {
  367. switch (state.mode) {
  368. case HEAD:
  369. if (state.wrap === 0) {
  370. state.mode = TYPEDO;
  371. break;
  372. }
  373. //=== NEEDBITS(16);
  374. while (bits < 16) {
  375. if (have === 0) {
  376. break inf_leave;
  377. }
  378. have--;
  379. hold += input[next++] << bits;
  380. bits += 8;
  381. }
  382. //===//
  383. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  384. state.check = 0 /*crc32(0L, Z_NULL, 0)*/ ;
  385. //=== CRC2(state.check, hold);
  386. hbuf[0] = hold & 0xff;
  387. hbuf[1] = (hold >>> 8) & 0xff;
  388. state.check = crc32(state.check, hbuf, 2, 0);
  389. //===//
  390. //=== INITBITS();
  391. hold = 0;
  392. bits = 0;
  393. //===//
  394. state.mode = FLAGS;
  395. break;
  396. }
  397. state.flags = 0; /* expect zlib header */
  398. if (state.head) {
  399. state.head.done = false;
  400. }
  401. if (!(state.wrap & 1) || /* check if zlib header allowed */
  402. (((hold & 0xff) /*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  403. strm.msg = 'incorrect header check';
  404. state.mode = BAD;
  405. break;
  406. }
  407. if ((hold & 0x0f) /*BITS(4)*/ !== Z_DEFLATED) {
  408. strm.msg = 'unknown compression method';
  409. state.mode = BAD;
  410. break;
  411. }
  412. //--- DROPBITS(4) ---//
  413. hold >>>= 4;
  414. bits -= 4;
  415. //---//
  416. len = (hold & 0x0f) /*BITS(4)*/ + 8;
  417. if (state.wbits === 0) {
  418. state.wbits = len;
  419. } else if (len > state.wbits) {
  420. strm.msg = 'invalid window size';
  421. state.mode = BAD;
  422. break;
  423. }
  424. state.dmax = 1 << len;
  425. //Tracev((stderr, "inflate: zlib header ok\n"));
  426. strm.adler = state.check = 1 /*adler32(0L, Z_NULL, 0)*/ ;
  427. state.mode = hold & 0x200 ? DICTID : TYPE;
  428. //=== INITBITS();
  429. hold = 0;
  430. bits = 0;
  431. //===//
  432. break;
  433. case FLAGS:
  434. //=== NEEDBITS(16); */
  435. while (bits < 16) {
  436. if (have === 0) {
  437. break inf_leave;
  438. }
  439. have--;
  440. hold += input[next++] << bits;
  441. bits += 8;
  442. }
  443. //===//
  444. state.flags = hold;
  445. if ((state.flags & 0xff) !== Z_DEFLATED) {
  446. strm.msg = 'unknown compression method';
  447. state.mode = BAD;
  448. break;
  449. }
  450. if (state.flags & 0xe000) {
  451. strm.msg = 'unknown header flags set';
  452. state.mode = BAD;
  453. break;
  454. }
  455. if (state.head) {
  456. state.head.text = ((hold >> 8) & 1);
  457. }
  458. if (state.flags & 0x0200) {
  459. //=== CRC2(state.check, hold);
  460. hbuf[0] = hold & 0xff;
  461. hbuf[1] = (hold >>> 8) & 0xff;
  462. state.check = crc32(state.check, hbuf, 2, 0);
  463. //===//
  464. }
  465. //=== INITBITS();
  466. hold = 0;
  467. bits = 0;
  468. //===//
  469. state.mode = TIME;
  470. /* falls through */
  471. case TIME:
  472. //=== NEEDBITS(32); */
  473. while (bits < 32) {
  474. if (have === 0) {
  475. break inf_leave;
  476. }
  477. have--;
  478. hold += input[next++] << bits;
  479. bits += 8;
  480. }
  481. //===//
  482. if (state.head) {
  483. state.head.time = hold;
  484. }
  485. if (state.flags & 0x0200) {
  486. //=== CRC4(state.check, hold)
  487. hbuf[0] = hold & 0xff;
  488. hbuf[1] = (hold >>> 8) & 0xff;
  489. hbuf[2] = (hold >>> 16) & 0xff;
  490. hbuf[3] = (hold >>> 24) & 0xff;
  491. state.check = crc32(state.check, hbuf, 4, 0);
  492. //===
  493. }
  494. //=== INITBITS();
  495. hold = 0;
  496. bits = 0;
  497. //===//
  498. state.mode = OS;
  499. /* falls through */
  500. case OS:
  501. //=== NEEDBITS(16); */
  502. while (bits < 16) {
  503. if (have === 0) {
  504. break inf_leave;
  505. }
  506. have--;
  507. hold += input[next++] << bits;
  508. bits += 8;
  509. }
  510. //===//
  511. if (state.head) {
  512. state.head.xflags = (hold & 0xff);
  513. state.head.os = (hold >> 8);
  514. }
  515. if (state.flags & 0x0200) {
  516. //=== CRC2(state.check, hold);
  517. hbuf[0] = hold & 0xff;
  518. hbuf[1] = (hold >>> 8) & 0xff;
  519. state.check = crc32(state.check, hbuf, 2, 0);
  520. //===//
  521. }
  522. //=== INITBITS();
  523. hold = 0;
  524. bits = 0;
  525. //===//
  526. state.mode = EXLEN;
  527. /* falls through */
  528. case EXLEN:
  529. if (state.flags & 0x0400) {
  530. //=== NEEDBITS(16); */
  531. while (bits < 16) {
  532. if (have === 0) {
  533. break inf_leave;
  534. }
  535. have--;
  536. hold += input[next++] << bits;
  537. bits += 8;
  538. }
  539. //===//
  540. state.length = hold;
  541. if (state.head) {
  542. state.head.extra_len = hold;
  543. }
  544. if (state.flags & 0x0200) {
  545. //=== CRC2(state.check, hold);
  546. hbuf[0] = hold & 0xff;
  547. hbuf[1] = (hold >>> 8) & 0xff;
  548. state.check = crc32(state.check, hbuf, 2, 0);
  549. //===//
  550. }
  551. //=== INITBITS();
  552. hold = 0;
  553. bits = 0;
  554. //===//
  555. } else if (state.head) {
  556. state.head.extra = null /*Z_NULL*/ ;
  557. }
  558. state.mode = EXTRA;
  559. /* falls through */
  560. case EXTRA:
  561. if (state.flags & 0x0400) {
  562. copy = state.length;
  563. if (copy > have) {
  564. copy = have;
  565. }
  566. if (copy) {
  567. if (state.head) {
  568. len = state.head.extra_len - state.length;
  569. if (!state.head.extra) {
  570. // Use untyped array for more conveniend processing later
  571. state.head.extra = new Array(state.head.extra_len);
  572. }
  573. arraySet(
  574. state.head.extra,
  575. input,
  576. next,
  577. // extra field is limited to 65536 bytes
  578. // - no need for additional size check
  579. copy,
  580. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  581. len
  582. );
  583. //zmemcpy(state.head.extra + len, next,
  584. // len + copy > state.head.extra_max ?
  585. // state.head.extra_max - len : copy);
  586. }
  587. if (state.flags & 0x0200) {
  588. state.check = crc32(state.check, input, copy, next);
  589. }
  590. have -= copy;
  591. next += copy;
  592. state.length -= copy;
  593. }
  594. if (state.length) {
  595. break inf_leave;
  596. }
  597. }
  598. state.length = 0;
  599. state.mode = NAME;
  600. /* falls through */
  601. case NAME:
  602. if (state.flags & 0x0800) {
  603. if (have === 0) {
  604. break inf_leave;
  605. }
  606. copy = 0;
  607. do {
  608. // TODO: 2 or 1 bytes?
  609. len = input[next + copy++];
  610. /* use constant limit because in js we should not preallocate memory */
  611. if (state.head && len &&
  612. (state.length < 65536 /*state.head.name_max*/ )) {
  613. state.head.name += String.fromCharCode(len);
  614. }
  615. } while (len && copy < have);
  616. if (state.flags & 0x0200) {
  617. state.check = crc32(state.check, input, copy, next);
  618. }
  619. have -= copy;
  620. next += copy;
  621. if (len) {
  622. break inf_leave;
  623. }
  624. } else if (state.head) {
  625. state.head.name = null;
  626. }
  627. state.length = 0;
  628. state.mode = COMMENT;
  629. /* falls through */
  630. case COMMENT:
  631. if (state.flags & 0x1000) {
  632. if (have === 0) {
  633. break inf_leave;
  634. }
  635. copy = 0;
  636. do {
  637. len = input[next + copy++];
  638. /* use constant limit because in js we should not preallocate memory */
  639. if (state.head && len &&
  640. (state.length < 65536 /*state.head.comm_max*/ )) {
  641. state.head.comment += String.fromCharCode(len);
  642. }
  643. } while (len && copy < have);
  644. if (state.flags & 0x0200) {
  645. state.check = crc32(state.check, input, copy, next);
  646. }
  647. have -= copy;
  648. next += copy;
  649. if (len) {
  650. break inf_leave;
  651. }
  652. } else if (state.head) {
  653. state.head.comment = null;
  654. }
  655. state.mode = HCRC;
  656. /* falls through */
  657. case HCRC:
  658. if (state.flags & 0x0200) {
  659. //=== NEEDBITS(16); */
  660. while (bits < 16) {
  661. if (have === 0) {
  662. break inf_leave;
  663. }
  664. have--;
  665. hold += input[next++] << bits;
  666. bits += 8;
  667. }
  668. //===//
  669. if (hold !== (state.check & 0xffff)) {
  670. strm.msg = 'header crc mismatch';
  671. state.mode = BAD;
  672. break;
  673. }
  674. //=== INITBITS();
  675. hold = 0;
  676. bits = 0;
  677. //===//
  678. }
  679. if (state.head) {
  680. state.head.hcrc = ((state.flags >> 9) & 1);
  681. state.head.done = true;
  682. }
  683. strm.adler = state.check = 0;
  684. state.mode = TYPE;
  685. break;
  686. case DICTID:
  687. //=== NEEDBITS(32); */
  688. while (bits < 32) {
  689. if (have === 0) {
  690. break inf_leave;
  691. }
  692. have--;
  693. hold += input[next++] << bits;
  694. bits += 8;
  695. }
  696. //===//
  697. strm.adler = state.check = zswap32(hold);
  698. //=== INITBITS();
  699. hold = 0;
  700. bits = 0;
  701. //===//
  702. state.mode = DICT;
  703. /* falls through */
  704. case DICT:
  705. if (state.havedict === 0) {
  706. //--- RESTORE() ---
  707. strm.next_out = put;
  708. strm.avail_out = left;
  709. strm.next_in = next;
  710. strm.avail_in = have;
  711. state.hold = hold;
  712. state.bits = bits;
  713. //---
  714. return Z_NEED_DICT;
  715. }
  716. strm.adler = state.check = 1 /*adler32(0L, Z_NULL, 0)*/ ;
  717. state.mode = TYPE;
  718. /* falls through */
  719. case TYPE:
  720. if (flush === Z_BLOCK || flush === Z_TREES) {
  721. break inf_leave;
  722. }
  723. /* falls through */
  724. case TYPEDO:
  725. if (state.last) {
  726. //--- BYTEBITS() ---//
  727. hold >>>= bits & 7;
  728. bits -= bits & 7;
  729. //---//
  730. state.mode = CHECK;
  731. break;
  732. }
  733. //=== NEEDBITS(3); */
  734. while (bits < 3) {
  735. if (have === 0) {
  736. break inf_leave;
  737. }
  738. have--;
  739. hold += input[next++] << bits;
  740. bits += 8;
  741. }
  742. //===//
  743. state.last = (hold & 0x01) /*BITS(1)*/ ;
  744. //--- DROPBITS(1) ---//
  745. hold >>>= 1;
  746. bits -= 1;
  747. //---//
  748. switch ((hold & 0x03) /*BITS(2)*/ ) {
  749. case 0:
  750. /* stored block */
  751. //Tracev((stderr, "inflate: stored block%s\n",
  752. // state.last ? " (last)" : ""));
  753. state.mode = STORED;
  754. break;
  755. case 1:
  756. /* fixed block */
  757. fixedtables(state);
  758. //Tracev((stderr, "inflate: fixed codes block%s\n",
  759. // state.last ? " (last)" : ""));
  760. state.mode = LEN_; /* decode codes */
  761. if (flush === Z_TREES) {
  762. //--- DROPBITS(2) ---//
  763. hold >>>= 2;
  764. bits -= 2;
  765. //---//
  766. break inf_leave;
  767. }
  768. break;
  769. case 2:
  770. /* dynamic block */
  771. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  772. // state.last ? " (last)" : ""));
  773. state.mode = TABLE;
  774. break;
  775. case 3:
  776. strm.msg = 'invalid block type';
  777. state.mode = BAD;
  778. }
  779. //--- DROPBITS(2) ---//
  780. hold >>>= 2;
  781. bits -= 2;
  782. //---//
  783. break;
  784. case STORED:
  785. //--- BYTEBITS() ---// /* go to byte boundary */
  786. hold >>>= bits & 7;
  787. bits -= bits & 7;
  788. //---//
  789. //=== NEEDBITS(32); */
  790. while (bits < 32) {
  791. if (have === 0) {
  792. break inf_leave;
  793. }
  794. have--;
  795. hold += input[next++] << bits;
  796. bits += 8;
  797. }
  798. //===//
  799. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  800. strm.msg = 'invalid stored block lengths';
  801. state.mode = BAD;
  802. break;
  803. }
  804. state.length = hold & 0xffff;
  805. //Tracev((stderr, "inflate: stored length %u\n",
  806. // state.length));
  807. //=== INITBITS();
  808. hold = 0;
  809. bits = 0;
  810. //===//
  811. state.mode = COPY_;
  812. if (flush === Z_TREES) {
  813. break inf_leave;
  814. }
  815. /* falls through */
  816. case COPY_:
  817. state.mode = COPY;
  818. /* falls through */
  819. case COPY:
  820. copy = state.length;
  821. if (copy) {
  822. if (copy > have) {
  823. copy = have;
  824. }
  825. if (copy > left) {
  826. copy = left;
  827. }
  828. if (copy === 0) {
  829. break inf_leave;
  830. }
  831. //--- zmemcpy(put, next, copy); ---
  832. arraySet(output, input, next, copy, put);
  833. //---//
  834. have -= copy;
  835. next += copy;
  836. left -= copy;
  837. put += copy;
  838. state.length -= copy;
  839. break;
  840. }
  841. //Tracev((stderr, "inflate: stored end\n"));
  842. state.mode = TYPE;
  843. break;
  844. case TABLE:
  845. //=== NEEDBITS(14); */
  846. while (bits < 14) {
  847. if (have === 0) {
  848. break inf_leave;
  849. }
  850. have--;
  851. hold += input[next++] << bits;
  852. bits += 8;
  853. }
  854. //===//
  855. state.nlen = (hold & 0x1f) /*BITS(5)*/ + 257;
  856. //--- DROPBITS(5) ---//
  857. hold >>>= 5;
  858. bits -= 5;
  859. //---//
  860. state.ndist = (hold & 0x1f) /*BITS(5)*/ + 1;
  861. //--- DROPBITS(5) ---//
  862. hold >>>= 5;
  863. bits -= 5;
  864. //---//
  865. state.ncode = (hold & 0x0f) /*BITS(4)*/ + 4;
  866. //--- DROPBITS(4) ---//
  867. hold >>>= 4;
  868. bits -= 4;
  869. //---//
  870. //#ifndef PKZIP_BUG_WORKAROUND
  871. if (state.nlen > 286 || state.ndist > 30) {
  872. strm.msg = 'too many length or distance symbols';
  873. state.mode = BAD;
  874. break;
  875. }
  876. //#endif
  877. //Tracev((stderr, "inflate: table sizes ok\n"));
  878. state.have = 0;
  879. state.mode = LENLENS;
  880. /* falls through */
  881. case LENLENS:
  882. while (state.have < state.ncode) {
  883. //=== NEEDBITS(3);
  884. while (bits < 3) {
  885. if (have === 0) {
  886. break inf_leave;
  887. }
  888. have--;
  889. hold += input[next++] << bits;
  890. bits += 8;
  891. }
  892. //===//
  893. state.lens[order[state.have++]] = (hold & 0x07); //BITS(3);
  894. //--- DROPBITS(3) ---//
  895. hold >>>= 3;
  896. bits -= 3;
  897. //---//
  898. }
  899. while (state.have < 19) {
  900. state.lens[order[state.have++]] = 0;
  901. }
  902. // We have separate tables & no pointers. 2 commented lines below not needed.
  903. //state.next = state.codes;
  904. //state.lencode = state.next;
  905. // Switch to use dynamic table
  906. state.lencode = state.lendyn;
  907. state.lenbits = 7;
  908. opts = {
  909. bits: state.lenbits
  910. };
  911. ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  912. state.lenbits = opts.bits;
  913. if (ret) {
  914. strm.msg = 'invalid code lengths set';
  915. state.mode = BAD;
  916. break;
  917. }
  918. //Tracev((stderr, "inflate: code lengths ok\n"));
  919. state.have = 0;
  920. state.mode = CODELENS;
  921. /* falls through */
  922. case CODELENS:
  923. while (state.have < state.nlen + state.ndist) {
  924. for (;;) {
  925. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  926. here_bits = here >>> 24;
  927. here_op = (here >>> 16) & 0xff;
  928. here_val = here & 0xffff;
  929. if ((here_bits) <= bits) {
  930. break;
  931. }
  932. //--- PULLBYTE() ---//
  933. if (have === 0) {
  934. break inf_leave;
  935. }
  936. have--;
  937. hold += input[next++] << bits;
  938. bits += 8;
  939. //---//
  940. }
  941. if (here_val < 16) {
  942. //--- DROPBITS(here.bits) ---//
  943. hold >>>= here_bits;
  944. bits -= here_bits;
  945. //---//
  946. state.lens[state.have++] = here_val;
  947. } else {
  948. if (here_val === 16) {
  949. //=== NEEDBITS(here.bits + 2);
  950. n = here_bits + 2;
  951. while (bits < n) {
  952. if (have === 0) {
  953. break inf_leave;
  954. }
  955. have--;
  956. hold += input[next++] << bits;
  957. bits += 8;
  958. }
  959. //===//
  960. //--- DROPBITS(here.bits) ---//
  961. hold >>>= here_bits;
  962. bits -= here_bits;
  963. //---//
  964. if (state.have === 0) {
  965. strm.msg = 'invalid bit length repeat';
  966. state.mode = BAD;
  967. break;
  968. }
  969. len = state.lens[state.have - 1];
  970. copy = 3 + (hold & 0x03); //BITS(2);
  971. //--- DROPBITS(2) ---//
  972. hold >>>= 2;
  973. bits -= 2;
  974. //---//
  975. } else if (here_val === 17) {
  976. //=== NEEDBITS(here.bits + 3);
  977. n = here_bits + 3;
  978. while (bits < n) {
  979. if (have === 0) {
  980. break inf_leave;
  981. }
  982. have--;
  983. hold += input[next++] << bits;
  984. bits += 8;
  985. }
  986. //===//
  987. //--- DROPBITS(here.bits) ---//
  988. hold >>>= here_bits;
  989. bits -= here_bits;
  990. //---//
  991. len = 0;
  992. copy = 3 + (hold & 0x07); //BITS(3);
  993. //--- DROPBITS(3) ---//
  994. hold >>>= 3;
  995. bits -= 3;
  996. //---//
  997. } else {
  998. //=== NEEDBITS(here.bits + 7);
  999. n = here_bits + 7;
  1000. while (bits < n) {
  1001. if (have === 0) {
  1002. break inf_leave;
  1003. }
  1004. have--;
  1005. hold += input[next++] << bits;
  1006. bits += 8;
  1007. }
  1008. //===//
  1009. //--- DROPBITS(here.bits) ---//
  1010. hold >>>= here_bits;
  1011. bits -= here_bits;
  1012. //---//
  1013. len = 0;
  1014. copy = 11 + (hold & 0x7f); //BITS(7);
  1015. //--- DROPBITS(7) ---//
  1016. hold >>>= 7;
  1017. bits -= 7;
  1018. //---//
  1019. }
  1020. if (state.have + copy > state.nlen + state.ndist) {
  1021. strm.msg = 'invalid bit length repeat';
  1022. state.mode = BAD;
  1023. break;
  1024. }
  1025. while (copy--) {
  1026. state.lens[state.have++] = len;
  1027. }
  1028. }
  1029. }
  1030. /* handle error breaks in while */
  1031. if (state.mode === BAD) {
  1032. break;
  1033. }
  1034. /* check for end-of-block code (better have one) */
  1035. if (state.lens[256] === 0) {
  1036. strm.msg = 'invalid code -- missing end-of-block';
  1037. state.mode = BAD;
  1038. break;
  1039. }
  1040. /* build code tables -- note: do not change the lenbits or distbits
  1041. values here (9 and 6) without reading the comments in inftrees.h
  1042. concerning the ENOUGH constants, which depend on those values */
  1043. state.lenbits = 9;
  1044. opts = {
  1045. bits: state.lenbits
  1046. };
  1047. ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  1048. // We have separate tables & no pointers. 2 commented lines below not needed.
  1049. // state.next_index = opts.table_index;
  1050. state.lenbits = opts.bits;
  1051. // state.lencode = state.next;
  1052. if (ret) {
  1053. strm.msg = 'invalid literal/lengths set';
  1054. state.mode = BAD;
  1055. break;
  1056. }
  1057. state.distbits = 6;
  1058. //state.distcode.copy(state.codes);
  1059. // Switch to use dynamic table
  1060. state.distcode = state.distdyn;
  1061. opts = {
  1062. bits: state.distbits
  1063. };
  1064. ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  1065. // We have separate tables & no pointers. 2 commented lines below not needed.
  1066. // state.next_index = opts.table_index;
  1067. state.distbits = opts.bits;
  1068. // state.distcode = state.next;
  1069. if (ret) {
  1070. strm.msg = 'invalid distances set';
  1071. state.mode = BAD;
  1072. break;
  1073. }
  1074. //Tracev((stderr, 'inflate: codes ok\n'));
  1075. state.mode = LEN_;
  1076. if (flush === Z_TREES) {
  1077. break inf_leave;
  1078. }
  1079. /* falls through */
  1080. case LEN_:
  1081. state.mode = LEN;
  1082. /* falls through */
  1083. case LEN:
  1084. if (have >= 6 && left >= 258) {
  1085. //--- RESTORE() ---
  1086. strm.next_out = put;
  1087. strm.avail_out = left;
  1088. strm.next_in = next;
  1089. strm.avail_in = have;
  1090. state.hold = hold;
  1091. state.bits = bits;
  1092. //---
  1093. inflate_fast(strm, _out);
  1094. //--- LOAD() ---
  1095. put = strm.next_out;
  1096. output = strm.output;
  1097. left = strm.avail_out;
  1098. next = strm.next_in;
  1099. input = strm.input;
  1100. have = strm.avail_in;
  1101. hold = state.hold;
  1102. bits = state.bits;
  1103. //---
  1104. if (state.mode === TYPE) {
  1105. state.back = -1;
  1106. }
  1107. break;
  1108. }
  1109. state.back = 0;
  1110. for (;;) {
  1111. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  1112. here_bits = here >>> 24;
  1113. here_op = (here >>> 16) & 0xff;
  1114. here_val = here & 0xffff;
  1115. if (here_bits <= bits) {
  1116. break;
  1117. }
  1118. //--- PULLBYTE() ---//
  1119. if (have === 0) {
  1120. break inf_leave;
  1121. }
  1122. have--;
  1123. hold += input[next++] << bits;
  1124. bits += 8;
  1125. //---//
  1126. }
  1127. if (here_op && (here_op & 0xf0) === 0) {
  1128. last_bits = here_bits;
  1129. last_op = here_op;
  1130. last_val = here_val;
  1131. for (;;) {
  1132. here = state.lencode[last_val +
  1133. ((hold & ((1 << (last_bits + last_op)) - 1)) /*BITS(last.bits + last.op)*/ >> last_bits)];
  1134. here_bits = here >>> 24;
  1135. here_op = (here >>> 16) & 0xff;
  1136. here_val = here & 0xffff;
  1137. if ((last_bits + here_bits) <= bits) {
  1138. break;
  1139. }
  1140. //--- PULLBYTE() ---//
  1141. if (have === 0) {
  1142. break inf_leave;
  1143. }
  1144. have--;
  1145. hold += input[next++] << bits;
  1146. bits += 8;
  1147. //---//
  1148. }
  1149. //--- DROPBITS(last.bits) ---//
  1150. hold >>>= last_bits;
  1151. bits -= last_bits;
  1152. //---//
  1153. state.back += last_bits;
  1154. }
  1155. //--- DROPBITS(here.bits) ---//
  1156. hold >>>= here_bits;
  1157. bits -= here_bits;
  1158. //---//
  1159. state.back += here_bits;
  1160. state.length = here_val;
  1161. if (here_op === 0) {
  1162. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  1163. // "inflate: literal '%c'\n" :
  1164. // "inflate: literal 0x%02x\n", here.val));
  1165. state.mode = LIT;
  1166. break;
  1167. }
  1168. if (here_op & 32) {
  1169. //Tracevv((stderr, "inflate: end of block\n"));
  1170. state.back = -1;
  1171. state.mode = TYPE;
  1172. break;
  1173. }
  1174. if (here_op & 64) {
  1175. strm.msg = 'invalid literal/length code';
  1176. state.mode = BAD;
  1177. break;
  1178. }
  1179. state.extra = here_op & 15;
  1180. state.mode = LENEXT;
  1181. /* falls through */
  1182. case LENEXT:
  1183. if (state.extra) {
  1184. //=== NEEDBITS(state.extra);
  1185. n = state.extra;
  1186. while (bits < n) {
  1187. if (have === 0) {
  1188. break inf_leave;
  1189. }
  1190. have--;
  1191. hold += input[next++] << bits;
  1192. bits += 8;
  1193. }
  1194. //===//
  1195. state.length += hold & ((1 << state.extra) - 1) /*BITS(state.extra)*/ ;
  1196. //--- DROPBITS(state.extra) ---//
  1197. hold >>>= state.extra;
  1198. bits -= state.extra;
  1199. //---//
  1200. state.back += state.extra;
  1201. }
  1202. //Tracevv((stderr, "inflate: length %u\n", state.length));
  1203. state.was = state.length;
  1204. state.mode = DIST;
  1205. /* falls through */
  1206. case DIST:
  1207. for (;;) {
  1208. here = state.distcode[hold & ((1 << state.distbits) - 1)]; /*BITS(state.distbits)*/
  1209. here_bits = here >>> 24;
  1210. here_op = (here >>> 16) & 0xff;
  1211. here_val = here & 0xffff;
  1212. if ((here_bits) <= bits) {
  1213. break;
  1214. }
  1215. //--- PULLBYTE() ---//
  1216. if (have === 0) {
  1217. break inf_leave;
  1218. }
  1219. have--;
  1220. hold += input[next++] << bits;
  1221. bits += 8;
  1222. //---//
  1223. }
  1224. if ((here_op & 0xf0) === 0) {
  1225. last_bits = here_bits;
  1226. last_op = here_op;
  1227. last_val = here_val;
  1228. for (;;) {
  1229. here = state.distcode[last_val +
  1230. ((hold & ((1 << (last_bits + last_op)) - 1)) /*BITS(last.bits + last.op)*/ >> last_bits)];
  1231. here_bits = here >>> 24;
  1232. here_op = (here >>> 16) & 0xff;
  1233. here_val = here & 0xffff;
  1234. if ((last_bits + here_bits) <= bits) {
  1235. break;
  1236. }
  1237. //--- PULLBYTE() ---//
  1238. if (have === 0) {
  1239. break inf_leave;
  1240. }
  1241. have--;
  1242. hold += input[next++] << bits;
  1243. bits += 8;
  1244. //---//
  1245. }
  1246. //--- DROPBITS(last.bits) ---//
  1247. hold >>>= last_bits;
  1248. bits -= last_bits;
  1249. //---//
  1250. state.back += last_bits;
  1251. }
  1252. //--- DROPBITS(here.bits) ---//
  1253. hold >>>= here_bits;
  1254. bits -= here_bits;
  1255. //---//
  1256. state.back += here_bits;
  1257. if (here_op & 64) {
  1258. strm.msg = 'invalid distance code';
  1259. state.mode = BAD;
  1260. break;
  1261. }
  1262. state.offset = here_val;
  1263. state.extra = (here_op) & 15;
  1264. state.mode = DISTEXT;
  1265. /* falls through */
  1266. case DISTEXT:
  1267. if (state.extra) {
  1268. //=== NEEDBITS(state.extra);
  1269. n = state.extra;
  1270. while (bits < n) {
  1271. if (have === 0) {
  1272. break inf_leave;
  1273. }
  1274. have--;
  1275. hold += input[next++] << bits;
  1276. bits += 8;
  1277. }
  1278. //===//
  1279. state.offset += hold & ((1 << state.extra) - 1) /*BITS(state.extra)*/ ;
  1280. //--- DROPBITS(state.extra) ---//
  1281. hold >>>= state.extra;
  1282. bits -= state.extra;
  1283. //---//
  1284. state.back += state.extra;
  1285. }
  1286. //#ifdef INFLATE_STRICT
  1287. if (state.offset > state.dmax) {
  1288. strm.msg = 'invalid distance too far back';
  1289. state.mode = BAD;
  1290. break;
  1291. }
  1292. //#endif
  1293. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  1294. state.mode = MATCH;
  1295. /* falls through */
  1296. case MATCH:
  1297. if (left === 0) {
  1298. break inf_leave;
  1299. }
  1300. copy = _out - left;
  1301. if (state.offset > copy) { /* copy from window */
  1302. copy = state.offset - copy;
  1303. if (copy > state.whave) {
  1304. if (state.sane) {
  1305. strm.msg = 'invalid distance too far back';
  1306. state.mode = BAD;
  1307. break;
  1308. }
  1309. // (!) This block is disabled in zlib defailts,
  1310. // don't enable it for binary compatibility
  1311. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1312. // Trace((stderr, "inflate.c too far\n"));
  1313. // copy -= state.whave;
  1314. // if (copy > state.length) { copy = state.length; }
  1315. // if (copy > left) { copy = left; }
  1316. // left -= copy;
  1317. // state.length -= copy;
  1318. // do {
  1319. // output[put++] = 0;
  1320. // } while (--copy);
  1321. // if (state.length === 0) { state.mode = LEN; }
  1322. // break;
  1323. //#endif
  1324. }
  1325. if (copy > state.wnext) {
  1326. copy -= state.wnext;
  1327. from = state.wsize - copy;
  1328. } else {
  1329. from = state.wnext - copy;
  1330. }
  1331. if (copy > state.length) {
  1332. copy = state.length;
  1333. }
  1334. from_source = state.window;
  1335. } else { /* copy from output */
  1336. from_source = output;
  1337. from = put - state.offset;
  1338. copy = state.length;
  1339. }
  1340. if (copy > left) {
  1341. copy = left;
  1342. }
  1343. left -= copy;
  1344. state.length -= copy;
  1345. do {
  1346. output[put++] = from_source[from++];
  1347. } while (--copy);
  1348. if (state.length === 0) {
  1349. state.mode = LEN;
  1350. }
  1351. break;
  1352. case LIT:
  1353. if (left === 0) {
  1354. break inf_leave;
  1355. }
  1356. output[put++] = state.length;
  1357. left--;
  1358. state.mode = LEN;
  1359. break;
  1360. case CHECK:
  1361. if (state.wrap) {
  1362. //=== NEEDBITS(32);
  1363. while (bits < 32) {
  1364. if (have === 0) {
  1365. break inf_leave;
  1366. }
  1367. have--;
  1368. // Use '|' insdead of '+' to make sure that result is signed
  1369. hold |= input[next++] << bits;
  1370. bits += 8;
  1371. }
  1372. //===//
  1373. _out -= left;
  1374. strm.total_out += _out;
  1375. state.total += _out;
  1376. if (_out) {
  1377. strm.adler = state.check =
  1378. /*UPDATE(state.check, put - _out, _out);*/
  1379. (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
  1380. }
  1381. _out = left;
  1382. // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  1383. if ((state.flags ? hold : zswap32(hold)) !== state.check) {
  1384. strm.msg = 'incorrect data check';
  1385. state.mode = BAD;
  1386. break;
  1387. }
  1388. //=== INITBITS();
  1389. hold = 0;
  1390. bits = 0;
  1391. //===//
  1392. //Tracev((stderr, "inflate: check matches trailer\n"));
  1393. }
  1394. state.mode = LENGTH;
  1395. /* falls through */
  1396. case LENGTH:
  1397. if (state.wrap && state.flags) {
  1398. //=== NEEDBITS(32);
  1399. while (bits < 32) {
  1400. if (have === 0) {
  1401. break inf_leave;
  1402. }
  1403. have--;
  1404. hold += input[next++] << bits;
  1405. bits += 8;
  1406. }
  1407. //===//
  1408. if (hold !== (state.total & 0xffffffff)) {
  1409. strm.msg = 'incorrect length check';
  1410. state.mode = BAD;
  1411. break;
  1412. }
  1413. //=== INITBITS();
  1414. hold = 0;
  1415. bits = 0;
  1416. //===//
  1417. //Tracev((stderr, "inflate: length matches trailer\n"));
  1418. }
  1419. state.mode = DONE;
  1420. /* falls through */
  1421. case DONE:
  1422. ret = Z_STREAM_END;
  1423. break inf_leave;
  1424. case BAD:
  1425. ret = Z_DATA_ERROR;
  1426. break inf_leave;
  1427. case MEM:
  1428. return Z_MEM_ERROR;
  1429. case SYNC:
  1430. /* falls through */
  1431. default:
  1432. return Z_STREAM_ERROR;
  1433. }
  1434. }
  1435. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  1436. /*
  1437. Return from inflate(), updating the total counts and the check value.
  1438. If there was no progress during the inflate() call, return a buffer
  1439. error. Call updatewindow() to create and/or update the window state.
  1440. Note: a memory error from inflate() is non-recoverable.
  1441. */
  1442. //--- RESTORE() ---
  1443. strm.next_out = put;
  1444. strm.avail_out = left;
  1445. strm.next_in = next;
  1446. strm.avail_in = have;
  1447. state.hold = hold;
  1448. state.bits = bits;
  1449. //---
  1450. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  1451. (state.mode < CHECK || flush !== Z_FINISH))) {
  1452. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) {
  1453. state.mode = MEM;
  1454. return Z_MEM_ERROR;
  1455. }
  1456. }
  1457. _in -= strm.avail_in;
  1458. _out -= strm.avail_out;
  1459. strm.total_in += _in;
  1460. strm.total_out += _out;
  1461. state.total += _out;
  1462. if (state.wrap && _out) {
  1463. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  1464. (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
  1465. }
  1466. strm.data_type = state.bits + (state.last ? 64 : 0) +
  1467. (state.mode === TYPE ? 128 : 0) +
  1468. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  1469. if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
  1470. ret = Z_BUF_ERROR;
  1471. }
  1472. return ret;
  1473. }
  1474. export function inflateEnd(strm) {
  1475. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/ ) {
  1476. return Z_STREAM_ERROR;
  1477. }
  1478. var state = strm.state;
  1479. if (state.window) {
  1480. state.window = null;
  1481. }
  1482. strm.state = null;
  1483. return Z_OK;
  1484. }
  1485. export function inflateGetHeader(strm, head) {
  1486. var state;
  1487. /* check state */
  1488. if (!strm || !strm.state) {
  1489. return Z_STREAM_ERROR;
  1490. }
  1491. state = strm.state;
  1492. if ((state.wrap & 2) === 0) {
  1493. return Z_STREAM_ERROR;
  1494. }
  1495. /* save header structure */
  1496. state.head = head;
  1497. head.done = false;
  1498. return Z_OK;
  1499. }
  1500. export function inflateSetDictionary(strm, dictionary) {
  1501. var dictLength = dictionary.length;
  1502. var state;
  1503. var dictid;
  1504. var ret;
  1505. /* check state */
  1506. if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */ ) {
  1507. return Z_STREAM_ERROR;
  1508. }
  1509. state = strm.state;
  1510. if (state.wrap !== 0 && state.mode !== DICT) {
  1511. return Z_STREAM_ERROR;
  1512. }
  1513. /* check for correct dictionary identifier */
  1514. if (state.mode === DICT) {
  1515. dictid = 1; /* adler32(0, null, 0)*/
  1516. /* dictid = adler32(dictid, dictionary, dictLength); */
  1517. dictid = adler32(dictid, dictionary, dictLength, 0);
  1518. if (dictid !== state.check) {
  1519. return Z_DATA_ERROR;
  1520. }
  1521. }
  1522. /* copy dictionary to window using updatewindow(), which will amend the
  1523. existing dictionary if appropriate */
  1524. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  1525. if (ret) {
  1526. state.mode = MEM;
  1527. return Z_MEM_ERROR;
  1528. }
  1529. state.havedict = 1;
  1530. // Tracev((stderr, "inflate: dictionary set\n"));
  1531. return Z_OK;
  1532. }
  1533. export var inflateInfo = 'pako inflate (from Nodeca project)';
  1534. /* Not implemented
  1535. exports.inflateCopy = inflateCopy;
  1536. exports.inflateGetDictionary = inflateGetDictionary;
  1537. exports.inflateMark = inflateMark;
  1538. exports.inflatePrime = inflatePrime;
  1539. exports.inflateSync = inflateSync;
  1540. exports.inflateSyncPoint = inflateSyncPoint;
  1541. exports.inflateUndermine = inflateUndermine;
  1542. */