deflate.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. 'use strict';
  2. var utils = require('../utils/common');
  3. var trees = require('./trees');
  4. var adler32 = require('./adler32');
  5. var crc32 = require('./crc32');
  6. var msg = require('./messages');
  7. /* Public constants ==========================================================*/
  8. /* ===========================================================================*/
  9. /* Allowed flush values; see deflate() and inflate() below for details */
  10. var Z_NO_FLUSH = 0;
  11. var Z_PARTIAL_FLUSH = 1;
  12. //var Z_SYNC_FLUSH = 2;
  13. var Z_FULL_FLUSH = 3;
  14. var Z_FINISH = 4;
  15. var Z_BLOCK = 5;
  16. //var Z_TREES = 6;
  17. /* Return codes for the compression/decompression functions. Negative values
  18. * are errors, positive values are used for special but normal events.
  19. */
  20. var Z_OK = 0;
  21. var Z_STREAM_END = 1;
  22. //var Z_NEED_DICT = 2;
  23. //var Z_ERRNO = -1;
  24. var Z_STREAM_ERROR = -2;
  25. var Z_DATA_ERROR = -3;
  26. //var Z_MEM_ERROR = -4;
  27. var Z_BUF_ERROR = -5;
  28. //var Z_VERSION_ERROR = -6;
  29. /* compression levels */
  30. //var Z_NO_COMPRESSION = 0;
  31. //var Z_BEST_SPEED = 1;
  32. //var Z_BEST_COMPRESSION = 9;
  33. var Z_DEFAULT_COMPRESSION = -1;
  34. var Z_FILTERED = 1;
  35. var Z_HUFFMAN_ONLY = 2;
  36. var Z_RLE = 3;
  37. var Z_FIXED = 4;
  38. var Z_DEFAULT_STRATEGY = 0;
  39. /* Possible values of the data_type field (though see inflate()) */
  40. //var Z_BINARY = 0;
  41. //var Z_TEXT = 1;
  42. //var Z_ASCII = 1; // = Z_TEXT
  43. var Z_UNKNOWN = 2;
  44. /* The deflate compression method */
  45. var Z_DEFLATED = 8;
  46. /*============================================================================*/
  47. var MAX_MEM_LEVEL = 9;
  48. /* Maximum value for memLevel in deflateInit2 */
  49. var MAX_WBITS = 15;
  50. /* 32K LZ77 window */
  51. var DEF_MEM_LEVEL = 8;
  52. var LENGTH_CODES = 29;
  53. /* number of length codes, not counting the special END_BLOCK code */
  54. var LITERALS = 256;
  55. /* number of literal bytes 0..255 */
  56. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  57. /* number of Literal or Length codes, including the END_BLOCK code */
  58. var D_CODES = 30;
  59. /* number of distance codes */
  60. var BL_CODES = 19;
  61. /* number of codes used to transfer the bit lengths */
  62. var HEAP_SIZE = 2 * L_CODES + 1;
  63. /* maximum heap size */
  64. var MAX_BITS = 15;
  65. /* All codes must not exceed MAX_BITS bits */
  66. var MIN_MATCH = 3;
  67. var MAX_MATCH = 258;
  68. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  69. var PRESET_DICT = 0x20;
  70. var INIT_STATE = 42;
  71. var EXTRA_STATE = 69;
  72. var NAME_STATE = 73;
  73. var COMMENT_STATE = 91;
  74. var HCRC_STATE = 103;
  75. var BUSY_STATE = 113;
  76. var FINISH_STATE = 666;
  77. var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  78. var BS_BLOCK_DONE = 2; /* block flush performed */
  79. var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  80. var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  81. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  82. function err(strm, errorCode) {
  83. strm.msg = msg[errorCode];
  84. return errorCode;
  85. }
  86. function rank(f) {
  87. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  88. }
  89. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  90. /* =========================================================================
  91. * Flush as much pending output as possible. All deflate() output goes
  92. * through this function so some applications may wish to modify it
  93. * to avoid allocating a large strm->output buffer and copying into it.
  94. * (See also read_buf()).
  95. */
  96. function flush_pending(strm) {
  97. var s = strm.state;
  98. //_tr_flush_bits(s);
  99. var len = s.pending;
  100. if (len > strm.avail_out) {
  101. len = strm.avail_out;
  102. }
  103. if (len === 0) { return; }
  104. utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
  105. strm.next_out += len;
  106. s.pending_out += len;
  107. strm.total_out += len;
  108. strm.avail_out -= len;
  109. s.pending -= len;
  110. if (s.pending === 0) {
  111. s.pending_out = 0;
  112. }
  113. }
  114. function flush_block_only(s, last) {
  115. trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  116. s.block_start = s.strstart;
  117. flush_pending(s.strm);
  118. }
  119. function put_byte(s, b) {
  120. s.pending_buf[s.pending++] = b;
  121. }
  122. /* =========================================================================
  123. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  124. * IN assertion: the stream state is correct and there is enough room in
  125. * pending_buf.
  126. */
  127. function putShortMSB(s, b) {
  128. // put_byte(s, (Byte)(b >> 8));
  129. // put_byte(s, (Byte)(b & 0xff));
  130. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  131. s.pending_buf[s.pending++] = b & 0xff;
  132. }
  133. /* ===========================================================================
  134. * Read a new buffer from the current input stream, update the adler32
  135. * and total number of bytes read. All deflate() input goes through
  136. * this function so some applications may wish to modify it to avoid
  137. * allocating a large strm->input buffer and copying from it.
  138. * (See also flush_pending()).
  139. */
  140. function read_buf(strm, buf, start, size) {
  141. var len = strm.avail_in;
  142. if (len > size) { len = size; }
  143. if (len === 0) { return 0; }
  144. strm.avail_in -= len;
  145. // zmemcpy(buf, strm->next_in, len);
  146. utils.arraySet(buf, strm.input, strm.next_in, len, start);
  147. if (strm.state.wrap === 1) {
  148. strm.adler = adler32(strm.adler, buf, len, start);
  149. }
  150. else if (strm.state.wrap === 2) {
  151. strm.adler = crc32(strm.adler, buf, len, start);
  152. }
  153. strm.next_in += len;
  154. strm.total_in += len;
  155. return len;
  156. }
  157. /* ===========================================================================
  158. * Set match_start to the longest match starting at the given string and
  159. * return its length. Matches shorter or equal to prev_length are discarded,
  160. * in which case the result is equal to prev_length and match_start is
  161. * garbage.
  162. * IN assertions: cur_match is the head of the hash chain for the current
  163. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  164. * OUT assertion: the match length is not greater than s->lookahead.
  165. */
  166. function longest_match(s, cur_match) {
  167. var chain_length = s.max_chain_length; /* max hash chain length */
  168. var scan = s.strstart; /* current string */
  169. var match; /* matched string */
  170. var len; /* length of current match */
  171. var best_len = s.prev_length; /* best match length so far */
  172. var nice_match = s.nice_match; /* stop if match long enough */
  173. var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  174. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  175. var _win = s.window; // shortcut
  176. var wmask = s.w_mask;
  177. var prev = s.prev;
  178. /* Stop when cur_match becomes <= limit. To simplify the code,
  179. * we prevent matches with the string of window index 0.
  180. */
  181. var strend = s.strstart + MAX_MATCH;
  182. var scan_end1 = _win[scan + best_len - 1];
  183. var scan_end = _win[scan + best_len];
  184. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  185. * It is easy to get rid of this optimization if necessary.
  186. */
  187. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  188. /* Do not waste too much time if we already have a good match: */
  189. if (s.prev_length >= s.good_match) {
  190. chain_length >>= 2;
  191. }
  192. /* Do not look for matches beyond the end of the input. This is necessary
  193. * to make deflate deterministic.
  194. */
  195. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  196. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  197. do {
  198. // Assert(cur_match < s->strstart, "no future");
  199. match = cur_match;
  200. /* Skip to next match if the match length cannot increase
  201. * or if the match length is less than 2. Note that the checks below
  202. * for insufficient lookahead only occur occasionally for performance
  203. * reasons. Therefore uninitialized memory will be accessed, and
  204. * conditional jumps will be made that depend on those values.
  205. * However the length of the match is limited to the lookahead, so
  206. * the output of deflate is not affected by the uninitialized values.
  207. */
  208. if (_win[match + best_len] !== scan_end ||
  209. _win[match + best_len - 1] !== scan_end1 ||
  210. _win[match] !== _win[scan] ||
  211. _win[++match] !== _win[scan + 1]) {
  212. continue;
  213. }
  214. /* The check at best_len-1 can be removed because it will be made
  215. * again later. (This heuristic is not always a win.)
  216. * It is not necessary to compare scan[2] and match[2] since they
  217. * are always equal when the other bytes match, given that
  218. * the hash keys are equal and that HASH_BITS >= 8.
  219. */
  220. scan += 2;
  221. match++;
  222. // Assert(*scan == *match, "match[2]?");
  223. /* We check for insufficient lookahead only every 8th comparison;
  224. * the 256th check will be made at strstart+258.
  225. */
  226. do {
  227. /*jshint noempty:false*/
  228. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  229. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  230. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  231. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  232. scan < strend);
  233. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  234. len = MAX_MATCH - (strend - scan);
  235. scan = strend - MAX_MATCH;
  236. if (len > best_len) {
  237. s.match_start = cur_match;
  238. best_len = len;
  239. if (len >= nice_match) {
  240. break;
  241. }
  242. scan_end1 = _win[scan + best_len - 1];
  243. scan_end = _win[scan + best_len];
  244. }
  245. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  246. if (best_len <= s.lookahead) {
  247. return best_len;
  248. }
  249. return s.lookahead;
  250. }
  251. /* ===========================================================================
  252. * Fill the window when the lookahead becomes insufficient.
  253. * Updates strstart and lookahead.
  254. *
  255. * IN assertion: lookahead < MIN_LOOKAHEAD
  256. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  257. * At least one byte has been read, or avail_in == 0; reads are
  258. * performed for at least two bytes (required for the zip translate_eol
  259. * option -- not supported here).
  260. */
  261. function fill_window(s) {
  262. var _w_size = s.w_size;
  263. var p, n, m, more, str;
  264. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  265. do {
  266. more = s.window_size - s.lookahead - s.strstart;
  267. // JS ints have 32 bit, block below not needed
  268. /* Deal with !@#$% 64K limit: */
  269. //if (sizeof(int) <= 2) {
  270. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  271. // more = wsize;
  272. //
  273. // } else if (more == (unsigned)(-1)) {
  274. // /* Very unlikely, but possible on 16 bit machine if
  275. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  276. // */
  277. // more--;
  278. // }
  279. //}
  280. /* If the window is almost full and there is insufficient lookahead,
  281. * move the upper half to the lower one to make room in the upper half.
  282. */
  283. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  284. utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
  285. s.match_start -= _w_size;
  286. s.strstart -= _w_size;
  287. /* we now have strstart >= MAX_DIST */
  288. s.block_start -= _w_size;
  289. /* Slide the hash table (could be avoided with 32 bit values
  290. at the expense of memory usage). We slide even when level == 0
  291. to keep the hash table consistent if we switch back to level > 0
  292. later. (Using level 0 permanently is not an optimal usage of
  293. zlib, so we don't care about this pathological case.)
  294. */
  295. n = s.hash_size;
  296. p = n;
  297. do {
  298. m = s.head[--p];
  299. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  300. } while (--n);
  301. n = _w_size;
  302. p = n;
  303. do {
  304. m = s.prev[--p];
  305. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  306. /* If n is not on any hash chain, prev[n] is garbage but
  307. * its value will never be used.
  308. */
  309. } while (--n);
  310. more += _w_size;
  311. }
  312. if (s.strm.avail_in === 0) {
  313. break;
  314. }
  315. /* If there was no sliding:
  316. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  317. * more == window_size - lookahead - strstart
  318. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  319. * => more >= window_size - 2*WSIZE + 2
  320. * In the BIG_MEM or MMAP case (not yet supported),
  321. * window_size == input_size + MIN_LOOKAHEAD &&
  322. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  323. * Otherwise, window_size == 2*WSIZE so more >= 2.
  324. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  325. */
  326. //Assert(more >= 2, "more < 2");
  327. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  328. s.lookahead += n;
  329. /* Initialize the hash value now that we have some input: */
  330. if (s.lookahead + s.insert >= MIN_MATCH) {
  331. str = s.strstart - s.insert;
  332. s.ins_h = s.window[str];
  333. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  334. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
  335. //#if MIN_MATCH != 3
  336. // Call update_hash() MIN_MATCH-3 more times
  337. //#endif
  338. while (s.insert) {
  339. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  340. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  341. s.prev[str & s.w_mask] = s.head[s.ins_h];
  342. s.head[s.ins_h] = str;
  343. str++;
  344. s.insert--;
  345. if (s.lookahead + s.insert < MIN_MATCH) {
  346. break;
  347. }
  348. }
  349. }
  350. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  351. * but this is not important since only literal bytes will be emitted.
  352. */
  353. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  354. /* If the WIN_INIT bytes after the end of the current data have never been
  355. * written, then zero those bytes in order to avoid memory check reports of
  356. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  357. * the longest match routines. Update the high water mark for the next
  358. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  359. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  360. */
  361. // if (s.high_water < s.window_size) {
  362. // var curr = s.strstart + s.lookahead;
  363. // var init = 0;
  364. //
  365. // if (s.high_water < curr) {
  366. // /* Previous high water mark below current data -- zero WIN_INIT
  367. // * bytes or up to end of window, whichever is less.
  368. // */
  369. // init = s.window_size - curr;
  370. // if (init > WIN_INIT)
  371. // init = WIN_INIT;
  372. // zmemzero(s->window + curr, (unsigned)init);
  373. // s->high_water = curr + init;
  374. // }
  375. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  376. // /* High water mark at or above current data, but below current data
  377. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  378. // * to end of window, whichever is less.
  379. // */
  380. // init = (ulg)curr + WIN_INIT - s->high_water;
  381. // if (init > s->window_size - s->high_water)
  382. // init = s->window_size - s->high_water;
  383. // zmemzero(s->window + s->high_water, (unsigned)init);
  384. // s->high_water += init;
  385. // }
  386. // }
  387. //
  388. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  389. // "not enough room for search");
  390. }
  391. /* ===========================================================================
  392. * Copy without compression as much as possible from the input stream, return
  393. * the current block state.
  394. * This function does not insert new strings in the dictionary since
  395. * uncompressible data is probably not useful. This function is used
  396. * only for the level=0 compression option.
  397. * NOTE: this function should be optimized to avoid extra copying from
  398. * window to pending_buf.
  399. */
  400. function deflate_stored(s, flush) {
  401. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  402. * to pending_buf_size, and each stored block has a 5 byte header:
  403. */
  404. var max_block_size = 0xffff;
  405. if (max_block_size > s.pending_buf_size - 5) {
  406. max_block_size = s.pending_buf_size - 5;
  407. }
  408. /* Copy as much as possible from input to output: */
  409. for (;;) {
  410. /* Fill the window as much as possible: */
  411. if (s.lookahead <= 1) {
  412. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  413. // s->block_start >= (long)s->w_size, "slide too late");
  414. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  415. // s.block_start >= s.w_size)) {
  416. // throw new Error("slide too late");
  417. // }
  418. fill_window(s);
  419. if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
  420. return BS_NEED_MORE;
  421. }
  422. if (s.lookahead === 0) {
  423. break;
  424. }
  425. /* flush the current block */
  426. }
  427. //Assert(s->block_start >= 0L, "block gone");
  428. // if (s.block_start < 0) throw new Error("block gone");
  429. s.strstart += s.lookahead;
  430. s.lookahead = 0;
  431. /* Emit a stored block if pending_buf will be full: */
  432. var max_start = s.block_start + max_block_size;
  433. if (s.strstart === 0 || s.strstart >= max_start) {
  434. /* strstart == 0 is possible when wraparound on 16-bit machine */
  435. s.lookahead = s.strstart - max_start;
  436. s.strstart = max_start;
  437. /*** FLUSH_BLOCK(s, 0); ***/
  438. flush_block_only(s, false);
  439. if (s.strm.avail_out === 0) {
  440. return BS_NEED_MORE;
  441. }
  442. /***/
  443. }
  444. /* Flush if we may have to slide, otherwise block_start may become
  445. * negative and the data will be gone:
  446. */
  447. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  448. /*** FLUSH_BLOCK(s, 0); ***/
  449. flush_block_only(s, false);
  450. if (s.strm.avail_out === 0) {
  451. return BS_NEED_MORE;
  452. }
  453. /***/
  454. }
  455. }
  456. s.insert = 0;
  457. if (flush === Z_FINISH) {
  458. /*** FLUSH_BLOCK(s, 1); ***/
  459. flush_block_only(s, true);
  460. if (s.strm.avail_out === 0) {
  461. return BS_FINISH_STARTED;
  462. }
  463. /***/
  464. return BS_FINISH_DONE;
  465. }
  466. if (s.strstart > s.block_start) {
  467. /*** FLUSH_BLOCK(s, 0); ***/
  468. flush_block_only(s, false);
  469. if (s.strm.avail_out === 0) {
  470. return BS_NEED_MORE;
  471. }
  472. /***/
  473. }
  474. return BS_NEED_MORE;
  475. }
  476. /* ===========================================================================
  477. * Compress as much as possible from the input stream, return the current
  478. * block state.
  479. * This function does not perform lazy evaluation of matches and inserts
  480. * new strings in the dictionary only for unmatched strings or for short
  481. * matches. It is used only for the fast compression options.
  482. */
  483. function deflate_fast(s, flush) {
  484. var hash_head; /* head of the hash chain */
  485. var bflush; /* set if current block must be flushed */
  486. for (;;) {
  487. /* Make sure that we always have enough lookahead, except
  488. * at the end of the input file. We need MAX_MATCH bytes
  489. * for the next match, plus MIN_MATCH bytes to insert the
  490. * string following the next match.
  491. */
  492. if (s.lookahead < MIN_LOOKAHEAD) {
  493. fill_window(s);
  494. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  495. return BS_NEED_MORE;
  496. }
  497. if (s.lookahead === 0) {
  498. break; /* flush the current block */
  499. }
  500. }
  501. /* Insert the string window[strstart .. strstart+2] in the
  502. * dictionary, and set hash_head to the head of the hash chain:
  503. */
  504. hash_head = 0/*NIL*/;
  505. if (s.lookahead >= MIN_MATCH) {
  506. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  507. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  508. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  509. s.head[s.ins_h] = s.strstart;
  510. /***/
  511. }
  512. /* Find the longest match, discarding those <= prev_length.
  513. * At this point we have always match_length < MIN_MATCH
  514. */
  515. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  516. /* To simplify the code, we prevent matches with the string
  517. * of window index 0 (in particular we have to avoid a match
  518. * of the string with itself at the start of the input file).
  519. */
  520. s.match_length = longest_match(s, hash_head);
  521. /* longest_match() sets match_start */
  522. }
  523. if (s.match_length >= MIN_MATCH) {
  524. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  525. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  526. s.match_length - MIN_MATCH, bflush); ***/
  527. bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  528. s.lookahead -= s.match_length;
  529. /* Insert new strings in the hash table only if the match length
  530. * is not too large. This saves time but degrades compression.
  531. */
  532. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  533. s.match_length--; /* string at strstart already in table */
  534. do {
  535. s.strstart++;
  536. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  537. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  538. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  539. s.head[s.ins_h] = s.strstart;
  540. /***/
  541. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  542. * always MIN_MATCH bytes ahead.
  543. */
  544. } while (--s.match_length !== 0);
  545. s.strstart++;
  546. } else
  547. {
  548. s.strstart += s.match_length;
  549. s.match_length = 0;
  550. s.ins_h = s.window[s.strstart];
  551. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  552. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
  553. //#if MIN_MATCH != 3
  554. // Call UPDATE_HASH() MIN_MATCH-3 more times
  555. //#endif
  556. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  557. * matter since it will be recomputed at next deflate call.
  558. */
  559. }
  560. } else {
  561. /* No match, output a literal byte */
  562. //Tracevv((stderr,"%c", s.window[s.strstart]));
  563. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  564. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  565. s.lookahead--;
  566. s.strstart++;
  567. }
  568. if (bflush) {
  569. /*** FLUSH_BLOCK(s, 0); ***/
  570. flush_block_only(s, false);
  571. if (s.strm.avail_out === 0) {
  572. return BS_NEED_MORE;
  573. }
  574. /***/
  575. }
  576. }
  577. s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
  578. if (flush === Z_FINISH) {
  579. /*** FLUSH_BLOCK(s, 1); ***/
  580. flush_block_only(s, true);
  581. if (s.strm.avail_out === 0) {
  582. return BS_FINISH_STARTED;
  583. }
  584. /***/
  585. return BS_FINISH_DONE;
  586. }
  587. if (s.last_lit) {
  588. /*** FLUSH_BLOCK(s, 0); ***/
  589. flush_block_only(s, false);
  590. if (s.strm.avail_out === 0) {
  591. return BS_NEED_MORE;
  592. }
  593. /***/
  594. }
  595. return BS_BLOCK_DONE;
  596. }
  597. /* ===========================================================================
  598. * Same as above, but achieves better compression. We use a lazy
  599. * evaluation for matches: a match is finally adopted only if there is
  600. * no better match at the next window position.
  601. */
  602. function deflate_slow(s, flush) {
  603. var hash_head; /* head of hash chain */
  604. var bflush; /* set if current block must be flushed */
  605. var max_insert;
  606. /* Process the input block. */
  607. for (;;) {
  608. /* Make sure that we always have enough lookahead, except
  609. * at the end of the input file. We need MAX_MATCH bytes
  610. * for the next match, plus MIN_MATCH bytes to insert the
  611. * string following the next match.
  612. */
  613. if (s.lookahead < MIN_LOOKAHEAD) {
  614. fill_window(s);
  615. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  616. return BS_NEED_MORE;
  617. }
  618. if (s.lookahead === 0) { break; } /* flush the current block */
  619. }
  620. /* Insert the string window[strstart .. strstart+2] in the
  621. * dictionary, and set hash_head to the head of the hash chain:
  622. */
  623. hash_head = 0/*NIL*/;
  624. if (s.lookahead >= MIN_MATCH) {
  625. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  626. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  627. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  628. s.head[s.ins_h] = s.strstart;
  629. /***/
  630. }
  631. /* Find the longest match, discarding those <= prev_length.
  632. */
  633. s.prev_length = s.match_length;
  634. s.prev_match = s.match_start;
  635. s.match_length = MIN_MATCH - 1;
  636. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  637. s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  638. /* To simplify the code, we prevent matches with the string
  639. * of window index 0 (in particular we have to avoid a match
  640. * of the string with itself at the start of the input file).
  641. */
  642. s.match_length = longest_match(s, hash_head);
  643. /* longest_match() sets match_start */
  644. if (s.match_length <= 5 &&
  645. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  646. /* If prev_match is also MIN_MATCH, match_start is garbage
  647. * but we will ignore the current match anyway.
  648. */
  649. s.match_length = MIN_MATCH - 1;
  650. }
  651. }
  652. /* If there was a match at the previous step and the current
  653. * match is not better, output the previous match:
  654. */
  655. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  656. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  657. /* Do not insert strings in hash table beyond this. */
  658. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  659. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  660. s.prev_length - MIN_MATCH, bflush);***/
  661. bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
  662. /* Insert in hash table all strings up to the end of the match.
  663. * strstart-1 and strstart are already inserted. If there is not
  664. * enough lookahead, the last two strings are not inserted in
  665. * the hash table.
  666. */
  667. s.lookahead -= s.prev_length - 1;
  668. s.prev_length -= 2;
  669. do {
  670. if (++s.strstart <= max_insert) {
  671. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  672. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  673. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  674. s.head[s.ins_h] = s.strstart;
  675. /***/
  676. }
  677. } while (--s.prev_length !== 0);
  678. s.match_available = 0;
  679. s.match_length = MIN_MATCH - 1;
  680. s.strstart++;
  681. if (bflush) {
  682. /*** FLUSH_BLOCK(s, 0); ***/
  683. flush_block_only(s, false);
  684. if (s.strm.avail_out === 0) {
  685. return BS_NEED_MORE;
  686. }
  687. /***/
  688. }
  689. } else if (s.match_available) {
  690. /* If there was no match at the previous position, output a
  691. * single literal. If there was a match but the current match
  692. * is longer, truncate the previous match to a single literal.
  693. */
  694. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  695. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  696. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  697. if (bflush) {
  698. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  699. flush_block_only(s, false);
  700. /***/
  701. }
  702. s.strstart++;
  703. s.lookahead--;
  704. if (s.strm.avail_out === 0) {
  705. return BS_NEED_MORE;
  706. }
  707. } else {
  708. /* There is no previous match to compare with, wait for
  709. * the next step to decide.
  710. */
  711. s.match_available = 1;
  712. s.strstart++;
  713. s.lookahead--;
  714. }
  715. }
  716. //Assert (flush != Z_NO_FLUSH, "no flush?");
  717. if (s.match_available) {
  718. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  719. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  720. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  721. s.match_available = 0;
  722. }
  723. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  724. if (flush === Z_FINISH) {
  725. /*** FLUSH_BLOCK(s, 1); ***/
  726. flush_block_only(s, true);
  727. if (s.strm.avail_out === 0) {
  728. return BS_FINISH_STARTED;
  729. }
  730. /***/
  731. return BS_FINISH_DONE;
  732. }
  733. if (s.last_lit) {
  734. /*** FLUSH_BLOCK(s, 0); ***/
  735. flush_block_only(s, false);
  736. if (s.strm.avail_out === 0) {
  737. return BS_NEED_MORE;
  738. }
  739. /***/
  740. }
  741. return BS_BLOCK_DONE;
  742. }
  743. /* ===========================================================================
  744. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  745. * one. Do not maintain a hash table. (It will be regenerated if this run of
  746. * deflate switches away from Z_RLE.)
  747. */
  748. function deflate_rle(s, flush) {
  749. var bflush; /* set if current block must be flushed */
  750. var prev; /* byte at distance one to match */
  751. var scan, strend; /* scan goes up to strend for length of run */
  752. var _win = s.window;
  753. for (;;) {
  754. /* Make sure that we always have enough lookahead, except
  755. * at the end of the input file. We need MAX_MATCH bytes
  756. * for the longest run, plus one for the unrolled loop.
  757. */
  758. if (s.lookahead <= MAX_MATCH) {
  759. fill_window(s);
  760. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
  761. return BS_NEED_MORE;
  762. }
  763. if (s.lookahead === 0) { break; } /* flush the current block */
  764. }
  765. /* See how many times the previous byte repeats */
  766. s.match_length = 0;
  767. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  768. scan = s.strstart - 1;
  769. prev = _win[scan];
  770. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  771. strend = s.strstart + MAX_MATCH;
  772. do {
  773. /*jshint noempty:false*/
  774. } while (prev === _win[++scan] && prev === _win[++scan] &&
  775. prev === _win[++scan] && prev === _win[++scan] &&
  776. prev === _win[++scan] && prev === _win[++scan] &&
  777. prev === _win[++scan] && prev === _win[++scan] &&
  778. scan < strend);
  779. s.match_length = MAX_MATCH - (strend - scan);
  780. if (s.match_length > s.lookahead) {
  781. s.match_length = s.lookahead;
  782. }
  783. }
  784. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  785. }
  786. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  787. if (s.match_length >= MIN_MATCH) {
  788. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  789. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  790. bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
  791. s.lookahead -= s.match_length;
  792. s.strstart += s.match_length;
  793. s.match_length = 0;
  794. } else {
  795. /* No match, output a literal byte */
  796. //Tracevv((stderr,"%c", s->window[s->strstart]));
  797. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  798. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  799. s.lookahead--;
  800. s.strstart++;
  801. }
  802. if (bflush) {
  803. /*** FLUSH_BLOCK(s, 0); ***/
  804. flush_block_only(s, false);
  805. if (s.strm.avail_out === 0) {
  806. return BS_NEED_MORE;
  807. }
  808. /***/
  809. }
  810. }
  811. s.insert = 0;
  812. if (flush === Z_FINISH) {
  813. /*** FLUSH_BLOCK(s, 1); ***/
  814. flush_block_only(s, true);
  815. if (s.strm.avail_out === 0) {
  816. return BS_FINISH_STARTED;
  817. }
  818. /***/
  819. return BS_FINISH_DONE;
  820. }
  821. if (s.last_lit) {
  822. /*** FLUSH_BLOCK(s, 0); ***/
  823. flush_block_only(s, false);
  824. if (s.strm.avail_out === 0) {
  825. return BS_NEED_MORE;
  826. }
  827. /***/
  828. }
  829. return BS_BLOCK_DONE;
  830. }
  831. /* ===========================================================================
  832. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  833. * (It will be regenerated if this run of deflate switches away from Huffman.)
  834. */
  835. function deflate_huff(s, flush) {
  836. var bflush; /* set if current block must be flushed */
  837. for (;;) {
  838. /* Make sure that we have a literal to write. */
  839. if (s.lookahead === 0) {
  840. fill_window(s);
  841. if (s.lookahead === 0) {
  842. if (flush === Z_NO_FLUSH) {
  843. return BS_NEED_MORE;
  844. }
  845. break; /* flush the current block */
  846. }
  847. }
  848. /* Output a literal byte */
  849. s.match_length = 0;
  850. //Tracevv((stderr,"%c", s->window[s->strstart]));
  851. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  852. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  853. s.lookahead--;
  854. s.strstart++;
  855. if (bflush) {
  856. /*** FLUSH_BLOCK(s, 0); ***/
  857. flush_block_only(s, false);
  858. if (s.strm.avail_out === 0) {
  859. return BS_NEED_MORE;
  860. }
  861. /***/
  862. }
  863. }
  864. s.insert = 0;
  865. if (flush === Z_FINISH) {
  866. /*** FLUSH_BLOCK(s, 1); ***/
  867. flush_block_only(s, true);
  868. if (s.strm.avail_out === 0) {
  869. return BS_FINISH_STARTED;
  870. }
  871. /***/
  872. return BS_FINISH_DONE;
  873. }
  874. if (s.last_lit) {
  875. /*** FLUSH_BLOCK(s, 0); ***/
  876. flush_block_only(s, false);
  877. if (s.strm.avail_out === 0) {
  878. return BS_NEED_MORE;
  879. }
  880. /***/
  881. }
  882. return BS_BLOCK_DONE;
  883. }
  884. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  885. * the desired pack level (0..9). The values given below have been tuned to
  886. * exclude worst case performance for pathological files. Better values may be
  887. * found for specific files.
  888. */
  889. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  890. this.good_length = good_length;
  891. this.max_lazy = max_lazy;
  892. this.nice_length = nice_length;
  893. this.max_chain = max_chain;
  894. this.func = func;
  895. }
  896. var configuration_table;
  897. configuration_table = [
  898. /* good lazy nice chain */
  899. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  900. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  901. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  902. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  903. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  904. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  905. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  906. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  907. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  908. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  909. ];
  910. /* ===========================================================================
  911. * Initialize the "longest match" routines for a new zlib stream
  912. */
  913. function lm_init(s) {
  914. s.window_size = 2 * s.w_size;
  915. /*** CLEAR_HASH(s); ***/
  916. zero(s.head); // Fill with NIL (= 0);
  917. /* Set the default configuration parameters:
  918. */
  919. s.max_lazy_match = configuration_table[s.level].max_lazy;
  920. s.good_match = configuration_table[s.level].good_length;
  921. s.nice_match = configuration_table[s.level].nice_length;
  922. s.max_chain_length = configuration_table[s.level].max_chain;
  923. s.strstart = 0;
  924. s.block_start = 0;
  925. s.lookahead = 0;
  926. s.insert = 0;
  927. s.match_length = s.prev_length = MIN_MATCH - 1;
  928. s.match_available = 0;
  929. s.ins_h = 0;
  930. }
  931. function DeflateState() {
  932. this.strm = null; /* pointer back to this zlib stream */
  933. this.status = 0; /* as the name implies */
  934. this.pending_buf = null; /* output still pending */
  935. this.pending_buf_size = 0; /* size of pending_buf */
  936. this.pending_out = 0; /* next pending byte to output to the stream */
  937. this.pending = 0; /* nb of bytes in the pending buffer */
  938. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  939. this.gzhead = null; /* gzip header information to write */
  940. this.gzindex = 0; /* where in extra, name, or comment */
  941. this.method = Z_DEFLATED; /* can only be DEFLATED */
  942. this.last_flush = -1; /* value of flush param for previous deflate call */
  943. this.w_size = 0; /* LZ77 window size (32K by default) */
  944. this.w_bits = 0; /* log2(w_size) (8..16) */
  945. this.w_mask = 0; /* w_size - 1 */
  946. this.window = null;
  947. /* Sliding window. Input bytes are read into the second half of the window,
  948. * and move to the first half later to keep a dictionary of at least wSize
  949. * bytes. With this organization, matches are limited to a distance of
  950. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  951. * performed with a length multiple of the block size.
  952. */
  953. this.window_size = 0;
  954. /* Actual size of window: 2*wSize, except when the user input buffer
  955. * is directly used as sliding window.
  956. */
  957. this.prev = null;
  958. /* Link to older string with same hash index. To limit the size of this
  959. * array to 64K, this link is maintained only for the last 32K strings.
  960. * An index in this array is thus a window index modulo 32K.
  961. */
  962. this.head = null; /* Heads of the hash chains or NIL. */
  963. this.ins_h = 0; /* hash index of string to be inserted */
  964. this.hash_size = 0; /* number of elements in hash table */
  965. this.hash_bits = 0; /* log2(hash_size) */
  966. this.hash_mask = 0; /* hash_size-1 */
  967. this.hash_shift = 0;
  968. /* Number of bits by which ins_h must be shifted at each input
  969. * step. It must be such that after MIN_MATCH steps, the oldest
  970. * byte no longer takes part in the hash key, that is:
  971. * hash_shift * MIN_MATCH >= hash_bits
  972. */
  973. this.block_start = 0;
  974. /* Window position at the beginning of the current output block. Gets
  975. * negative when the window is moved backwards.
  976. */
  977. this.match_length = 0; /* length of best match */
  978. this.prev_match = 0; /* previous match */
  979. this.match_available = 0; /* set if previous match exists */
  980. this.strstart = 0; /* start of string to insert */
  981. this.match_start = 0; /* start of matching string */
  982. this.lookahead = 0; /* number of valid bytes ahead in window */
  983. this.prev_length = 0;
  984. /* Length of the best match at previous step. Matches not greater than this
  985. * are discarded. This is used in the lazy match evaluation.
  986. */
  987. this.max_chain_length = 0;
  988. /* To speed up deflation, hash chains are never searched beyond this
  989. * length. A higher limit improves compression ratio but degrades the
  990. * speed.
  991. */
  992. this.max_lazy_match = 0;
  993. /* Attempt to find a better match only when the current match is strictly
  994. * smaller than this value. This mechanism is used only for compression
  995. * levels >= 4.
  996. */
  997. // That's alias to max_lazy_match, don't use directly
  998. //this.max_insert_length = 0;
  999. /* Insert new strings in the hash table only if the match length is not
  1000. * greater than this length. This saves time but degrades compression.
  1001. * max_insert_length is used only for compression levels <= 3.
  1002. */
  1003. this.level = 0; /* compression level (1..9) */
  1004. this.strategy = 0; /* favor or force Huffman coding*/
  1005. this.good_match = 0;
  1006. /* Use a faster search when the previous match is longer than this */
  1007. this.nice_match = 0; /* Stop searching when current match exceeds this */
  1008. /* used by trees.c: */
  1009. /* Didn't use ct_data typedef below to suppress compiler warning */
  1010. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  1011. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  1012. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  1013. // Use flat array of DOUBLE size, with interleaved fata,
  1014. // because JS does not support effective
  1015. this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
  1016. this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
  1017. this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
  1018. zero(this.dyn_ltree);
  1019. zero(this.dyn_dtree);
  1020. zero(this.bl_tree);
  1021. this.l_desc = null; /* desc. for literal tree */
  1022. this.d_desc = null; /* desc. for distance tree */
  1023. this.bl_desc = null; /* desc. for bit length tree */
  1024. //ush bl_count[MAX_BITS+1];
  1025. this.bl_count = new utils.Buf16(MAX_BITS + 1);
  1026. /* number of codes at each bit length for an optimal tree */
  1027. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  1028. this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */
  1029. zero(this.heap);
  1030. this.heap_len = 0; /* number of elements in the heap */
  1031. this.heap_max = 0; /* element of largest frequency */
  1032. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  1033. * The same heap array is used to build all trees.
  1034. */
  1035. this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
  1036. zero(this.depth);
  1037. /* Depth of each subtree used as tie breaker for trees of equal frequency
  1038. */
  1039. this.l_buf = 0; /* buffer index for literals or lengths */
  1040. this.lit_bufsize = 0;
  1041. /* Size of match buffer for literals/lengths. There are 4 reasons for
  1042. * limiting lit_bufsize to 64K:
  1043. * - frequencies can be kept in 16 bit counters
  1044. * - if compression is not successful for the first block, all input
  1045. * data is still in the window so we can still emit a stored block even
  1046. * when input comes from standard input. (This can also be done for
  1047. * all blocks if lit_bufsize is not greater than 32K.)
  1048. * - if compression is not successful for a file smaller than 64K, we can
  1049. * even emit a stored file instead of a stored block (saving 5 bytes).
  1050. * This is applicable only for zip (not gzip or zlib).
  1051. * - creating new Huffman trees less frequently may not provide fast
  1052. * adaptation to changes in the input data statistics. (Take for
  1053. * example a binary file with poorly compressible code followed by
  1054. * a highly compressible string table.) Smaller buffer sizes give
  1055. * fast adaptation but have of course the overhead of transmitting
  1056. * trees more frequently.
  1057. * - I can't count above 4
  1058. */
  1059. this.last_lit = 0; /* running index in l_buf */
  1060. this.d_buf = 0;
  1061. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  1062. * the same number of elements. To use different lengths, an extra flag
  1063. * array would be necessary.
  1064. */
  1065. this.opt_len = 0; /* bit length of current block with optimal trees */
  1066. this.static_len = 0; /* bit length of current block with static trees */
  1067. this.matches = 0; /* number of string matches in current block */
  1068. this.insert = 0; /* bytes at end of window left to insert */
  1069. this.bi_buf = 0;
  1070. /* Output buffer. bits are inserted starting at the bottom (least
  1071. * significant bits).
  1072. */
  1073. this.bi_valid = 0;
  1074. /* Number of valid bits in bi_buf. All bits above the last valid bit
  1075. * are always zero.
  1076. */
  1077. // Used for window memory init. We safely ignore it for JS. That makes
  1078. // sense only for pointers and memory check tools.
  1079. //this.high_water = 0;
  1080. /* High water mark offset in window for initialized bytes -- bytes above
  1081. * this are set to zero in order to avoid memory check warnings when
  1082. * longest match routines access bytes past the input. This is then
  1083. * updated to the new high water mark.
  1084. */
  1085. }
  1086. function deflateResetKeep(strm) {
  1087. var s;
  1088. if (!strm || !strm.state) {
  1089. return err(strm, Z_STREAM_ERROR);
  1090. }
  1091. strm.total_in = strm.total_out = 0;
  1092. strm.data_type = Z_UNKNOWN;
  1093. s = strm.state;
  1094. s.pending = 0;
  1095. s.pending_out = 0;
  1096. if (s.wrap < 0) {
  1097. s.wrap = -s.wrap;
  1098. /* was made negative by deflate(..., Z_FINISH); */
  1099. }
  1100. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  1101. strm.adler = (s.wrap === 2) ?
  1102. 0 // crc32(0, Z_NULL, 0)
  1103. :
  1104. 1; // adler32(0, Z_NULL, 0)
  1105. s.last_flush = Z_NO_FLUSH;
  1106. trees._tr_init(s);
  1107. return Z_OK;
  1108. }
  1109. function deflateReset(strm) {
  1110. var ret = deflateResetKeep(strm);
  1111. if (ret === Z_OK) {
  1112. lm_init(strm.state);
  1113. }
  1114. return ret;
  1115. }
  1116. function deflateSetHeader(strm, head) {
  1117. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  1118. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
  1119. strm.state.gzhead = head;
  1120. return Z_OK;
  1121. }
  1122. function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  1123. if (!strm) { // === Z_NULL
  1124. return Z_STREAM_ERROR;
  1125. }
  1126. var wrap = 1;
  1127. if (level === Z_DEFAULT_COMPRESSION) {
  1128. level = 6;
  1129. }
  1130. if (windowBits < 0) { /* suppress zlib wrapper */
  1131. wrap = 0;
  1132. windowBits = -windowBits;
  1133. }
  1134. else if (windowBits > 15) {
  1135. wrap = 2; /* write gzip wrapper instead */
  1136. windowBits -= 16;
  1137. }
  1138. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
  1139. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  1140. strategy < 0 || strategy > Z_FIXED) {
  1141. return err(strm, Z_STREAM_ERROR);
  1142. }
  1143. if (windowBits === 8) {
  1144. windowBits = 9;
  1145. }
  1146. /* until 256-byte window bug fixed */
  1147. var s = new DeflateState();
  1148. strm.state = s;
  1149. s.strm = strm;
  1150. s.wrap = wrap;
  1151. s.gzhead = null;
  1152. s.w_bits = windowBits;
  1153. s.w_size = 1 << s.w_bits;
  1154. s.w_mask = s.w_size - 1;
  1155. s.hash_bits = memLevel + 7;
  1156. s.hash_size = 1 << s.hash_bits;
  1157. s.hash_mask = s.hash_size - 1;
  1158. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  1159. s.window = new utils.Buf8(s.w_size * 2);
  1160. s.head = new utils.Buf16(s.hash_size);
  1161. s.prev = new utils.Buf16(s.w_size);
  1162. // Don't need mem init magic for JS.
  1163. //s.high_water = 0; /* nothing written to s->window yet */
  1164. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  1165. s.pending_buf_size = s.lit_bufsize * 4;
  1166. //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  1167. //s->pending_buf = (uchf *) overlay;
  1168. s.pending_buf = new utils.Buf8(s.pending_buf_size);
  1169. // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
  1170. //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  1171. s.d_buf = 1 * s.lit_bufsize;
  1172. //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  1173. s.l_buf = (1 + 2) * s.lit_bufsize;
  1174. s.level = level;
  1175. s.strategy = strategy;
  1176. s.method = method;
  1177. return deflateReset(strm);
  1178. }
  1179. function deflateInit(strm, level) {
  1180. return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  1181. }
  1182. function deflate(strm, flush) {
  1183. var old_flush, s;
  1184. var beg, val; // for gzip header write only
  1185. if (!strm || !strm.state ||
  1186. flush > Z_BLOCK || flush < 0) {
  1187. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  1188. }
  1189. s = strm.state;
  1190. if (!strm.output ||
  1191. (!strm.input && strm.avail_in !== 0) ||
  1192. (s.status === FINISH_STATE && flush !== Z_FINISH)) {
  1193. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
  1194. }
  1195. s.strm = strm; /* just in case */
  1196. old_flush = s.last_flush;
  1197. s.last_flush = flush;
  1198. /* Write the header */
  1199. if (s.status === INIT_STATE) {
  1200. if (s.wrap === 2) { // GZIP header
  1201. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  1202. put_byte(s, 31);
  1203. put_byte(s, 139);
  1204. put_byte(s, 8);
  1205. if (!s.gzhead) { // s->gzhead == Z_NULL
  1206. put_byte(s, 0);
  1207. put_byte(s, 0);
  1208. put_byte(s, 0);
  1209. put_byte(s, 0);
  1210. put_byte(s, 0);
  1211. put_byte(s, s.level === 9 ? 2 :
  1212. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  1213. 4 : 0));
  1214. put_byte(s, OS_CODE);
  1215. s.status = BUSY_STATE;
  1216. }
  1217. else {
  1218. put_byte(s, (s.gzhead.text ? 1 : 0) +
  1219. (s.gzhead.hcrc ? 2 : 0) +
  1220. (!s.gzhead.extra ? 0 : 4) +
  1221. (!s.gzhead.name ? 0 : 8) +
  1222. (!s.gzhead.comment ? 0 : 16)
  1223. );
  1224. put_byte(s, s.gzhead.time & 0xff);
  1225. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  1226. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  1227. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  1228. put_byte(s, s.level === 9 ? 2 :
  1229. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  1230. 4 : 0));
  1231. put_byte(s, s.gzhead.os & 0xff);
  1232. if (s.gzhead.extra && s.gzhead.extra.length) {
  1233. put_byte(s, s.gzhead.extra.length & 0xff);
  1234. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  1235. }
  1236. if (s.gzhead.hcrc) {
  1237. strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
  1238. }
  1239. s.gzindex = 0;
  1240. s.status = EXTRA_STATE;
  1241. }
  1242. }
  1243. else // DEFLATE header
  1244. {
  1245. var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
  1246. var level_flags = -1;
  1247. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  1248. level_flags = 0;
  1249. } else if (s.level < 6) {
  1250. level_flags = 1;
  1251. } else if (s.level === 6) {
  1252. level_flags = 2;
  1253. } else {
  1254. level_flags = 3;
  1255. }
  1256. header |= (level_flags << 6);
  1257. if (s.strstart !== 0) { header |= PRESET_DICT; }
  1258. header += 31 - (header % 31);
  1259. s.status = BUSY_STATE;
  1260. putShortMSB(s, header);
  1261. /* Save the adler32 of the preset dictionary: */
  1262. if (s.strstart !== 0) {
  1263. putShortMSB(s, strm.adler >>> 16);
  1264. putShortMSB(s, strm.adler & 0xffff);
  1265. }
  1266. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  1267. }
  1268. }
  1269. //#ifdef GZIP
  1270. if (s.status === EXTRA_STATE) {
  1271. if (s.gzhead.extra/* != Z_NULL*/) {
  1272. beg = s.pending; /* start of bytes to update crc */
  1273. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  1274. if (s.pending === s.pending_buf_size) {
  1275. if (s.gzhead.hcrc && s.pending > beg) {
  1276. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  1277. }
  1278. flush_pending(strm);
  1279. beg = s.pending;
  1280. if (s.pending === s.pending_buf_size) {
  1281. break;
  1282. }
  1283. }
  1284. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  1285. s.gzindex++;
  1286. }
  1287. if (s.gzhead.hcrc && s.pending > beg) {
  1288. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  1289. }
  1290. if (s.gzindex === s.gzhead.extra.length) {
  1291. s.gzindex = 0;
  1292. s.status = NAME_STATE;
  1293. }
  1294. }
  1295. else {
  1296. s.status = NAME_STATE;
  1297. }
  1298. }
  1299. if (s.status === NAME_STATE) {
  1300. if (s.gzhead.name/* != Z_NULL*/) {
  1301. beg = s.pending; /* start of bytes to update crc */
  1302. //int val;
  1303. do {
  1304. if (s.pending === s.pending_buf_size) {
  1305. if (s.gzhead.hcrc && s.pending > beg) {
  1306. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  1307. }
  1308. flush_pending(strm);
  1309. beg = s.pending;
  1310. if (s.pending === s.pending_buf_size) {
  1311. val = 1;
  1312. break;
  1313. }
  1314. }
  1315. // JS specific: little magic to add zero terminator to end of string
  1316. if (s.gzindex < s.gzhead.name.length) {
  1317. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  1318. } else {
  1319. val = 0;
  1320. }
  1321. put_byte(s, val);
  1322. } while (val !== 0);
  1323. if (s.gzhead.hcrc && s.pending > beg) {
  1324. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  1325. }
  1326. if (val === 0) {
  1327. s.gzindex = 0;
  1328. s.status = COMMENT_STATE;
  1329. }
  1330. }
  1331. else {
  1332. s.status = COMMENT_STATE;
  1333. }
  1334. }
  1335. if (s.status === COMMENT_STATE) {
  1336. if (s.gzhead.comment/* != Z_NULL*/) {
  1337. beg = s.pending; /* start of bytes to update crc */
  1338. //int val;
  1339. do {
  1340. if (s.pending === s.pending_buf_size) {
  1341. if (s.gzhead.hcrc && s.pending > beg) {
  1342. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  1343. }
  1344. flush_pending(strm);
  1345. beg = s.pending;
  1346. if (s.pending === s.pending_buf_size) {
  1347. val = 1;
  1348. break;
  1349. }
  1350. }
  1351. // JS specific: little magic to add zero terminator to end of string
  1352. if (s.gzindex < s.gzhead.comment.length) {
  1353. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  1354. } else {
  1355. val = 0;
  1356. }
  1357. put_byte(s, val);
  1358. } while (val !== 0);
  1359. if (s.gzhead.hcrc && s.pending > beg) {
  1360. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  1361. }
  1362. if (val === 0) {
  1363. s.status = HCRC_STATE;
  1364. }
  1365. }
  1366. else {
  1367. s.status = HCRC_STATE;
  1368. }
  1369. }
  1370. if (s.status === HCRC_STATE) {
  1371. if (s.gzhead.hcrc) {
  1372. if (s.pending + 2 > s.pending_buf_size) {
  1373. flush_pending(strm);
  1374. }
  1375. if (s.pending + 2 <= s.pending_buf_size) {
  1376. put_byte(s, strm.adler & 0xff);
  1377. put_byte(s, (strm.adler >> 8) & 0xff);
  1378. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  1379. s.status = BUSY_STATE;
  1380. }
  1381. }
  1382. else {
  1383. s.status = BUSY_STATE;
  1384. }
  1385. }
  1386. //#endif
  1387. /* Flush as much pending output as possible */
  1388. if (s.pending !== 0) {
  1389. flush_pending(strm);
  1390. if (strm.avail_out === 0) {
  1391. /* Since avail_out is 0, deflate will be called again with
  1392. * more output space, but possibly with both pending and
  1393. * avail_in equal to zero. There won't be anything to do,
  1394. * but this is not an error situation so make sure we
  1395. * return OK instead of BUF_ERROR at next call of deflate:
  1396. */
  1397. s.last_flush = -1;
  1398. return Z_OK;
  1399. }
  1400. /* Make sure there is something to do and avoid duplicate consecutive
  1401. * flushes. For repeated and useless calls with Z_FINISH, we keep
  1402. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  1403. */
  1404. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  1405. flush !== Z_FINISH) {
  1406. return err(strm, Z_BUF_ERROR);
  1407. }
  1408. /* User must not provide more input after the first FINISH: */
  1409. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  1410. return err(strm, Z_BUF_ERROR);
  1411. }
  1412. /* Start a new block or continue the current one.
  1413. */
  1414. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  1415. (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
  1416. var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  1417. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  1418. configuration_table[s.level].func(s, flush));
  1419. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  1420. s.status = FINISH_STATE;
  1421. }
  1422. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  1423. if (strm.avail_out === 0) {
  1424. s.last_flush = -1;
  1425. /* avoid BUF_ERROR next call, see above */
  1426. }
  1427. return Z_OK;
  1428. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  1429. * of deflate should use the same flush parameter to make sure
  1430. * that the flush is complete. So we don't have to output an
  1431. * empty block here, this will be done at next call. This also
  1432. * ensures that for a very small output buffer, we emit at most
  1433. * one empty block.
  1434. */
  1435. }
  1436. if (bstate === BS_BLOCK_DONE) {
  1437. if (flush === Z_PARTIAL_FLUSH) {
  1438. trees._tr_align(s);
  1439. }
  1440. else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  1441. trees._tr_stored_block(s, 0, 0, false);
  1442. /* For a full flush, this empty block will be recognized
  1443. * as a special marker by inflate_sync().
  1444. */
  1445. if (flush === Z_FULL_FLUSH) {
  1446. /*** CLEAR_HASH(s); ***/ /* forget history */
  1447. zero(s.head); // Fill with NIL (= 0);
  1448. if (s.lookahead === 0) {
  1449. s.strstart = 0;
  1450. s.block_start = 0;
  1451. s.insert = 0;
  1452. }
  1453. }
  1454. }
  1455. flush_pending(strm);
  1456. if (strm.avail_out === 0) {
  1457. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  1458. return Z_OK;
  1459. }
  1460. }
  1461. }
  1462. //Assert(strm->avail_out > 0, "bug2");
  1463. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  1464. if (flush !== Z_FINISH) { return Z_OK; }
  1465. if (s.wrap <= 0) { return Z_STREAM_END; }
  1466. /* Write the trailer */
  1467. if (s.wrap === 2) {
  1468. put_byte(s, strm.adler & 0xff);
  1469. put_byte(s, (strm.adler >> 8) & 0xff);
  1470. put_byte(s, (strm.adler >> 16) & 0xff);
  1471. put_byte(s, (strm.adler >> 24) & 0xff);
  1472. put_byte(s, strm.total_in & 0xff);
  1473. put_byte(s, (strm.total_in >> 8) & 0xff);
  1474. put_byte(s, (strm.total_in >> 16) & 0xff);
  1475. put_byte(s, (strm.total_in >> 24) & 0xff);
  1476. }
  1477. else
  1478. {
  1479. putShortMSB(s, strm.adler >>> 16);
  1480. putShortMSB(s, strm.adler & 0xffff);
  1481. }
  1482. flush_pending(strm);
  1483. /* If avail_out is zero, the application will call deflate again
  1484. * to flush the rest.
  1485. */
  1486. if (s.wrap > 0) { s.wrap = -s.wrap; }
  1487. /* write the trailer only once! */
  1488. return s.pending !== 0 ? Z_OK : Z_STREAM_END;
  1489. }
  1490. function deflateEnd(strm) {
  1491. var status;
  1492. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  1493. return Z_STREAM_ERROR;
  1494. }
  1495. status = strm.state.status;
  1496. if (status !== INIT_STATE &&
  1497. status !== EXTRA_STATE &&
  1498. status !== NAME_STATE &&
  1499. status !== COMMENT_STATE &&
  1500. status !== HCRC_STATE &&
  1501. status !== BUSY_STATE &&
  1502. status !== FINISH_STATE
  1503. ) {
  1504. return err(strm, Z_STREAM_ERROR);
  1505. }
  1506. strm.state = null;
  1507. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
  1508. }
  1509. /* =========================================================================
  1510. * Initializes the compression dictionary from the given byte
  1511. * sequence without producing any compressed output.
  1512. */
  1513. function deflateSetDictionary(strm, dictionary) {
  1514. var dictLength = dictionary.length;
  1515. var s;
  1516. var str, n;
  1517. var wrap;
  1518. var avail;
  1519. var next;
  1520. var input;
  1521. var tmpDict;
  1522. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  1523. return Z_STREAM_ERROR;
  1524. }
  1525. s = strm.state;
  1526. wrap = s.wrap;
  1527. if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
  1528. return Z_STREAM_ERROR;
  1529. }
  1530. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  1531. if (wrap === 1) {
  1532. /* adler32(strm->adler, dictionary, dictLength); */
  1533. strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
  1534. }
  1535. s.wrap = 0; /* avoid computing Adler-32 in read_buf */
  1536. /* if dictionary would fill window, just replace the history */
  1537. if (dictLength >= s.w_size) {
  1538. if (wrap === 0) { /* already empty otherwise */
  1539. /*** CLEAR_HASH(s); ***/
  1540. zero(s.head); // Fill with NIL (= 0);
  1541. s.strstart = 0;
  1542. s.block_start = 0;
  1543. s.insert = 0;
  1544. }
  1545. /* use the tail */
  1546. // dictionary = dictionary.slice(dictLength - s.w_size);
  1547. tmpDict = new utils.Buf8(s.w_size);
  1548. utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
  1549. dictionary = tmpDict;
  1550. dictLength = s.w_size;
  1551. }
  1552. /* insert dictionary into window and hash */
  1553. avail = strm.avail_in;
  1554. next = strm.next_in;
  1555. input = strm.input;
  1556. strm.avail_in = dictLength;
  1557. strm.next_in = 0;
  1558. strm.input = dictionary;
  1559. fill_window(s);
  1560. while (s.lookahead >= MIN_MATCH) {
  1561. str = s.strstart;
  1562. n = s.lookahead - (MIN_MATCH - 1);
  1563. do {
  1564. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  1565. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  1566. s.prev[str & s.w_mask] = s.head[s.ins_h];
  1567. s.head[s.ins_h] = str;
  1568. str++;
  1569. } while (--n);
  1570. s.strstart = str;
  1571. s.lookahead = MIN_MATCH - 1;
  1572. fill_window(s);
  1573. }
  1574. s.strstart += s.lookahead;
  1575. s.block_start = s.strstart;
  1576. s.insert = s.lookahead;
  1577. s.lookahead = 0;
  1578. s.match_length = s.prev_length = MIN_MATCH - 1;
  1579. s.match_available = 0;
  1580. strm.next_in = next;
  1581. strm.input = input;
  1582. strm.avail_in = avail;
  1583. s.wrap = wrap;
  1584. return Z_OK;
  1585. }
  1586. exports.deflateInit = deflateInit;
  1587. exports.deflateInit2 = deflateInit2;
  1588. exports.deflateReset = deflateReset;
  1589. exports.deflateResetKeep = deflateResetKeep;
  1590. exports.deflateSetHeader = deflateSetHeader;
  1591. exports.deflate = deflate;
  1592. exports.deflateEnd = deflateEnd;
  1593. exports.deflateSetDictionary = deflateSetDictionary;
  1594. exports.deflateInfo = 'pako deflate (from Nodeca project)';
  1595. /* Not implemented
  1596. exports.deflateBound = deflateBound;
  1597. exports.deflateCopy = deflateCopy;
  1598. exports.deflateParams = deflateParams;
  1599. exports.deflatePending = deflatePending;
  1600. exports.deflatePrime = deflatePrime;
  1601. exports.deflateTune = deflateTune;
  1602. */