deflate.js 58 KB

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