deflate.js 59 KB

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