deflate.js 60 KB

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