deflate.js 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2016 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. /*
  24. Copyright (c) 2013 Gildas Lormeau. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions are met:
  27. 1. Redistributions of source code must retain the above copyright notice,
  28. this list of conditions and the following disclaimer.
  29. 2. Redistributions in binary form must reproduce the above copyright
  30. notice, this list of conditions and the following disclaimer in
  31. the documentation and/or other materials provided with the distribution.
  32. 3. The names of the authors may not be used to endorse or promote products
  33. derived from this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  37. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  38. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  40. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  41. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  42. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  43. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. /*
  46. * This program is based on JZlib 1.0.2 ymnk, JCraft,Inc.
  47. * JZlib is based on zlib-1.1.3, so all credit should go authors
  48. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  49. * and contributors of zlib.
  50. */
  51. (function(obj) {
  52. // Global
  53. var MAX_BITS = 15;
  54. var D_CODES = 30;
  55. var BL_CODES = 19;
  56. var LENGTH_CODES = 29;
  57. var LITERALS = 256;
  58. var L_CODES = (LITERALS + 1 + LENGTH_CODES);
  59. var HEAP_SIZE = (2 * L_CODES + 1);
  60. var END_BLOCK = 256;
  61. // Bit length codes must not exceed MAX_BL_BITS bits
  62. var MAX_BL_BITS = 7;
  63. // repeat previous bit length 3-6 times (2 bits of repeat count)
  64. var REP_3_6 = 16;
  65. // repeat a zero length 3-10 times (3 bits of repeat count)
  66. var REPZ_3_10 = 17;
  67. // repeat a zero length 11-138 times (7 bits of repeat count)
  68. var REPZ_11_138 = 18;
  69. // The lengths of the bit length codes are sent in order of decreasing
  70. // probability, to avoid transmitting the lengths for unused bit
  71. // length codes.
  72. var Buf_size = 8 * 2;
  73. // JZlib version : "1.0.2"
  74. var Z_DEFAULT_COMPRESSION = -1;
  75. // compression strategy
  76. var Z_FILTERED = 1;
  77. var Z_HUFFMAN_ONLY = 2;
  78. var Z_DEFAULT_STRATEGY = 0;
  79. var Z_NO_FLUSH = 0;
  80. var Z_PARTIAL_FLUSH = 1;
  81. var Z_FULL_FLUSH = 3;
  82. var Z_FINISH = 4;
  83. var Z_OK = 0;
  84. var Z_STREAM_END = 1;
  85. var Z_NEED_DICT = 2;
  86. var Z_STREAM_ERROR = -2;
  87. var Z_DATA_ERROR = -3;
  88. var Z_BUF_ERROR = -5;
  89. // Tree
  90. // see definition of array dist_code below
  91. var _dist_code = [ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  92. 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  93. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  94. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  95. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  96. 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  97. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, 18, 18, 19, 19,
  98. 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  99. 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  100. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  101. 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  102. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29,
  103. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  104. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 ];
  105. function Tree() {
  106. var that = this;
  107. // dyn_tree; // the dynamic tree
  108. // max_code; // largest code with non zero frequency
  109. // stat_desc; // the corresponding static tree
  110. // Compute the optimal bit lengths for a tree and update the total bit
  111. // length
  112. // for the current block.
  113. // IN assertion: the fields freq and dad are set, heap[heap_max] and
  114. // above are the tree nodes sorted by increasing frequency.
  115. // OUT assertions: the field len is set to the optimal bit length, the
  116. // array bl_count contains the frequencies for each bit length.
  117. // The length opt_len is updated; static_len is also updated if stree is
  118. // not null.
  119. function gen_bitlen(s) {
  120. var tree = that.dyn_tree;
  121. var stree = that.stat_desc.static_tree;
  122. var extra = that.stat_desc.extra_bits;
  123. var base = that.stat_desc.extra_base;
  124. var max_length = that.stat_desc.max_length;
  125. var h; // heap index
  126. var n, m; // iterate over the tree elements
  127. var bits; // bit length
  128. var xbits; // extra bits
  129. var f; // frequency
  130. var overflow = 0; // number of elements with bit length too large
  131. for (bits = 0; bits <= MAX_BITS; bits++)
  132. s.bl_count[bits] = 0;
  133. // In a first pass, compute the optimal bit lengths (which may
  134. // overflow in the case of the bit length tree).
  135. tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
  136. for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
  137. n = s.heap[h];
  138. bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
  139. if (bits > max_length) {
  140. bits = max_length;
  141. overflow++;
  142. }
  143. tree[n * 2 + 1] = bits;
  144. // We overwrite tree[n*2+1] which is no longer needed
  145. if (n > that.max_code)
  146. continue; // not a leaf node
  147. s.bl_count[bits]++;
  148. xbits = 0;
  149. if (n >= base)
  150. xbits = extra[n - base];
  151. f = tree[n * 2];
  152. s.opt_len += f * (bits + xbits);
  153. if (stree)
  154. s.static_len += f * (stree[n * 2 + 1] + xbits);
  155. }
  156. if (overflow === 0)
  157. return;
  158. // This happens for example on obj2 and pic of the Calgary corpus
  159. // Find the first bit length which could increase:
  160. do {
  161. bits = max_length - 1;
  162. while (s.bl_count[bits] === 0)
  163. bits--;
  164. s.bl_count[bits]--; // move one leaf down the tree
  165. s.bl_count[bits + 1] += 2; // move one overflow item as its brother
  166. s.bl_count[max_length]--;
  167. // The brother of the overflow item also moves one step up,
  168. // but this does not affect bl_count[max_length]
  169. overflow -= 2;
  170. } while (overflow > 0);
  171. for (bits = max_length; bits !== 0; bits--) {
  172. n = s.bl_count[bits];
  173. while (n !== 0) {
  174. m = s.heap[--h];
  175. if (m > that.max_code)
  176. continue;
  177. if (tree[m * 2 + 1] != bits) {
  178. s.opt_len += (bits - tree[m * 2 + 1]) * tree[m * 2];
  179. tree[m * 2 + 1] = bits;
  180. }
  181. n--;
  182. }
  183. }
  184. }
  185. // Reverse the first len bits of a code, using straightforward code (a
  186. // faster
  187. // method would use a table)
  188. // IN assertion: 1 <= len <= 15
  189. function bi_reverse(code, // the value to invert
  190. len // its bit length
  191. ) {
  192. var res = 0;
  193. do {
  194. res |= code & 1;
  195. code >>>= 1;
  196. res <<= 1;
  197. } while (--len > 0);
  198. return res >>> 1;
  199. }
  200. // Generate the codes for a given tree and bit counts (which need not be
  201. // optimal).
  202. // IN assertion: the array bl_count contains the bit length statistics for
  203. // the given tree and the field len is set for all tree elements.
  204. // OUT assertion: the field code is set for all tree elements of non
  205. // zero code length.
  206. function gen_codes(tree, // the tree to decorate
  207. max_code, // largest code with non zero frequency
  208. bl_count // number of codes at each bit length
  209. ) {
  210. var next_code = []; // next code value for each
  211. // bit length
  212. var code = 0; // running code value
  213. var bits; // bit index
  214. var n; // code index
  215. var len;
  216. // The distribution counts are first used to generate the code values
  217. // without bit reversal.
  218. for (bits = 1; bits <= MAX_BITS; bits++) {
  219. next_code[bits] = code = ((code + bl_count[bits - 1]) << 1);
  220. }
  221. // Check that the bit counts in bl_count are consistent. The last code
  222. // must be all ones.
  223. // Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  224. // "inconsistent bit counts");
  225. // Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  226. for (n = 0; n <= max_code; n++) {
  227. len = tree[n * 2 + 1];
  228. if (len === 0)
  229. continue;
  230. // Now reverse the bits
  231. tree[n * 2] = bi_reverse(next_code[len]++, len);
  232. }
  233. }
  234. // Construct one Huffman tree and assigns the code bit strings and lengths.
  235. // Update the total bit length for the current block.
  236. // IN assertion: the field freq is set for all tree elements.
  237. // OUT assertions: the fields len and code are set to the optimal bit length
  238. // and corresponding code. The length opt_len is updated; static_len is
  239. // also updated if stree is not null. The field max_code is set.
  240. that.build_tree = function(s) {
  241. var tree = that.dyn_tree;
  242. var stree = that.stat_desc.static_tree;
  243. var elems = that.stat_desc.elems;
  244. var n, m; // iterate over heap elements
  245. var max_code = -1; // largest code with non zero frequency
  246. var node; // new node being created
  247. // Construct the initial heap, with least frequent element in
  248. // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  249. // heap[0] is not used.
  250. s.heap_len = 0;
  251. s.heap_max = HEAP_SIZE;
  252. for (n = 0; n < elems; n++) {
  253. if (tree[n * 2] !== 0) {
  254. s.heap[++s.heap_len] = max_code = n;
  255. s.depth[n] = 0;
  256. } else {
  257. tree[n * 2 + 1] = 0;
  258. }
  259. }
  260. // The pkzip format requires that at least one distance code exists,
  261. // and that at least one bit should be sent even if there is only one
  262. // possible code. So to avoid special checks later on we force at least
  263. // two codes of non zero frequency.
  264. while (s.heap_len < 2) {
  265. node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
  266. tree[node * 2] = 1;
  267. s.depth[node] = 0;
  268. s.opt_len--;
  269. if (stree)
  270. s.static_len -= stree[node * 2 + 1];
  271. // node is 0 or 1 so it does not have extra bits
  272. }
  273. that.max_code = max_code;
  274. // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  275. // establish sub-heaps of increasing lengths:
  276. for (n = Math.floor(s.heap_len / 2); n >= 1; n--)
  277. s.pqdownheap(tree, n);
  278. // Construct the Huffman tree by repeatedly combining the least two
  279. // frequent nodes.
  280. node = elems; // next internal node of the tree
  281. do {
  282. // n = node of least frequency
  283. n = s.heap[1];
  284. s.heap[1] = s.heap[s.heap_len--];
  285. s.pqdownheap(tree, 1);
  286. m = s.heap[1]; // m = node of next least frequency
  287. s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
  288. s.heap[--s.heap_max] = m;
  289. // Create a new node father of n and m
  290. tree[node * 2] = (tree[n * 2] + tree[m * 2]);
  291. s.depth[node] = Math.max(s.depth[n], s.depth[m]) + 1;
  292. tree[n * 2 + 1] = tree[m * 2 + 1] = node;
  293. // and insert the new node in the heap
  294. s.heap[1] = node++;
  295. s.pqdownheap(tree, 1);
  296. } while (s.heap_len >= 2);
  297. s.heap[--s.heap_max] = s.heap[1];
  298. // At this point, the fields freq and dad are set. We can now
  299. // generate the bit lengths.
  300. gen_bitlen(s);
  301. // The field len is now set, we can generate the bit codes
  302. gen_codes(tree, that.max_code, s.bl_count);
  303. };
  304. }
  305. Tree._length_code = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
  306. 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  307. 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  308. 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  309. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  310. 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  311. 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 ];
  312. Tree.base_length = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0 ];
  313. Tree.base_dist = [ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384,
  314. 24576 ];
  315. // Mapping from a distance to a distance code. dist is the distance - 1 and
  316. // must not have side effects. _dist_code[256] and _dist_code[257] are never
  317. // used.
  318. Tree.d_code = function(dist) {
  319. return ((dist) < 256 ? _dist_code[dist] : _dist_code[256 + ((dist) >>> 7)]);
  320. };
  321. // extra bits for each length code
  322. Tree.extra_lbits = [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 ];
  323. // extra bits for each distance code
  324. Tree.extra_dbits = [ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 ];
  325. // extra bits for each bit length code
  326. Tree.extra_blbits = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 ];
  327. Tree.bl_order = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  328. // StaticTree
  329. function StaticTree(static_tree, extra_bits, extra_base, elems, max_length) {
  330. var that = this;
  331. that.static_tree = static_tree;
  332. that.extra_bits = extra_bits;
  333. that.extra_base = extra_base;
  334. that.elems = elems;
  335. that.max_length = max_length;
  336. }
  337. StaticTree.static_ltree = [ 12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8, 28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8, 2, 8,
  338. 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8, 18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8, 10, 8, 138, 8, 74, 8, 202, 8, 42,
  339. 8, 170, 8, 106, 8, 234, 8, 26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8, 6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8,
  340. 22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8, 14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8, 30, 8, 158, 8, 94, 8,
  341. 222, 8, 62, 8, 190, 8, 126, 8, 254, 8, 1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8, 17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113,
  342. 8, 241, 8, 9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8, 25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8, 5, 8, 133, 8,
  343. 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8, 21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8, 13, 8, 141, 8, 77, 8, 205, 8, 45, 8,
  344. 173, 8, 109, 8, 237, 8, 29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8, 19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9,
  345. 51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9, 11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9, 43, 9, 299, 9, 171, 9,
  346. 427, 9, 107, 9, 363, 9, 235, 9, 491, 9, 27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9, 59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379,
  347. 9, 251, 9, 507, 9, 7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9, 39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9, 23,
  348. 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9, 55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9, 15, 9, 271, 9, 143, 9,
  349. 399, 9, 79, 9, 335, 9, 207, 9, 463, 9, 47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9, 31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9,
  350. 223, 9, 479, 9, 63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9, 0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7, 8, 7, 72, 7,
  351. 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7, 4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7, 3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8,
  352. 99, 8, 227, 8 ];
  353. StaticTree.static_dtree = [ 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5, 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5, 1, 5, 17, 5, 9, 5,
  354. 25, 5, 5, 5, 21, 5, 13, 5, 29, 5, 3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 ];
  355. StaticTree.static_l_desc = new StaticTree(StaticTree.static_ltree, Tree.extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
  356. StaticTree.static_d_desc = new StaticTree(StaticTree.static_dtree, Tree.extra_dbits, 0, D_CODES, MAX_BITS);
  357. StaticTree.static_bl_desc = new StaticTree(null, Tree.extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  358. // Deflate
  359. var MAX_MEM_LEVEL = 9;
  360. var DEF_MEM_LEVEL = 8;
  361. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  362. var that = this;
  363. that.good_length = good_length;
  364. that.max_lazy = max_lazy;
  365. that.nice_length = nice_length;
  366. that.max_chain = max_chain;
  367. that.func = func;
  368. }
  369. var STORED = 0;
  370. var FAST = 1;
  371. var SLOW = 2;
  372. var config_table = [ new Config(0, 0, 0, 0, STORED), new Config(4, 4, 8, 4, FAST), new Config(4, 5, 16, 8, FAST), new Config(4, 6, 32, 32, FAST),
  373. new Config(4, 4, 16, 16, SLOW), new Config(8, 16, 32, 32, SLOW), new Config(8, 16, 128, 128, SLOW), new Config(8, 32, 128, 256, SLOW),
  374. new Config(32, 128, 258, 1024, SLOW), new Config(32, 258, 258, 4096, SLOW) ];
  375. var z_errmsg = [ "need dictionary", // Z_NEED_DICT
  376. // 2
  377. "stream end", // Z_STREAM_END 1
  378. "", // Z_OK 0
  379. "", // Z_ERRNO (-1)
  380. "stream error", // Z_STREAM_ERROR (-2)
  381. "data error", // Z_DATA_ERROR (-3)
  382. "", // Z_MEM_ERROR (-4)
  383. "buffer error", // Z_BUF_ERROR (-5)
  384. "",// Z_VERSION_ERROR (-6)
  385. "" ];
  386. // block not completed, need more input or more output
  387. var NeedMore = 0;
  388. // block flush performed
  389. var BlockDone = 1;
  390. // finish started, need only more output at next deflate
  391. var FinishStarted = 2;
  392. // finish done, accept no more input or output
  393. var FinishDone = 3;
  394. // preset dictionary flag in zlib header
  395. var PRESET_DICT = 0x20;
  396. var INIT_STATE = 42;
  397. var BUSY_STATE = 113;
  398. var FINISH_STATE = 666;
  399. // The deflate compression method
  400. var Z_DEFLATED = 8;
  401. var STORED_BLOCK = 0;
  402. var STATIC_TREES = 1;
  403. var DYN_TREES = 2;
  404. var MIN_MATCH = 3;
  405. var MAX_MATCH = 258;
  406. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  407. function smaller(tree, n, m, depth) {
  408. var tn2 = tree[n * 2];
  409. var tm2 = tree[m * 2];
  410. return (tn2 < tm2 || (tn2 == tm2 && depth[n] <= depth[m]));
  411. }
  412. function Deflate() {
  413. var that = this;
  414. var strm; // pointer back to this zlib stream
  415. var status; // as the name implies
  416. // pending_buf; // output still pending
  417. var pending_buf_size; // size of pending_buf
  418. // pending_out; // next pending byte to output to the stream
  419. // pending; // nb of bytes in the pending buffer
  420. var method; // STORED (for zip only) or DEFLATED
  421. var last_flush; // value of flush param for previous deflate call
  422. var w_size; // LZ77 window size (32K by default)
  423. var w_bits; // log2(w_size) (8..16)
  424. var w_mask; // w_size - 1
  425. var window;
  426. // Sliding window. Input bytes are read into the second half of the window,
  427. // and move to the first half later to keep a dictionary of at least wSize
  428. // bytes. With this organization, matches are limited to a distance of
  429. // wSize-MAX_MATCH bytes, but this ensures that IO is always
  430. // performed with a length multiple of the block size. Also, it limits
  431. // the window size to 64K, which is quite useful on MSDOS.
  432. // To do: use the user input buffer as sliding window.
  433. var window_size;
  434. // Actual size of window: 2*wSize, except when the user input buffer
  435. // is directly used as sliding window.
  436. var prev;
  437. // Link to older string with same hash index. To limit the size of this
  438. // array to 64K, this link is maintained only for the last 32K strings.
  439. // An index in this array is thus a window index modulo 32K.
  440. var head; // Heads of the hash chains or NIL.
  441. var ins_h; // hash index of string to be inserted
  442. var hash_size; // number of elements in hash table
  443. var hash_bits; // log2(hash_size)
  444. var hash_mask; // hash_size-1
  445. // Number of bits by which ins_h must be shifted at each input
  446. // step. It must be such that after MIN_MATCH steps, the oldest
  447. // byte no longer takes part in the hash key, that is:
  448. // hash_shift * MIN_MATCH >= hash_bits
  449. var hash_shift;
  450. // Window position at the beginning of the current output block. Gets
  451. // negative when the window is moved backwards.
  452. var block_start;
  453. var match_length; // length of best match
  454. var prev_match; // previous match
  455. var match_available; // set if previous match exists
  456. var strstart; // start of string to insert
  457. var match_start; // start of matching string
  458. var lookahead; // number of valid bytes ahead in window
  459. // Length of the best match at previous step. Matches not greater than this
  460. // are discarded. This is used in the lazy match evaluation.
  461. var prev_length;
  462. // To speed up deflation, hash chains are never searched beyond this
  463. // length. A higher limit improves compression ratio but degrades the speed.
  464. var max_chain_length;
  465. // Attempt to find a better match only when the current match is strictly
  466. // smaller than this value. This mechanism is used only for compression
  467. // levels >= 4.
  468. var max_lazy_match;
  469. // Insert new strings in the hash table only if the match length is not
  470. // greater than this length. This saves time but degrades compression.
  471. // max_insert_length is used only for compression levels <= 3.
  472. var level; // compression level (1..9)
  473. var strategy; // favor or force Huffman coding
  474. // Use a faster search when the previous match is longer than this
  475. var good_match;
  476. // Stop searching when current match exceeds this
  477. var nice_match;
  478. var dyn_ltree; // literal and length tree
  479. var dyn_dtree; // distance tree
  480. var bl_tree; // Huffman tree for bit lengths
  481. var l_desc = new Tree(); // desc for literal tree
  482. var d_desc = new Tree(); // desc for distance tree
  483. var bl_desc = new Tree(); // desc for bit length tree
  484. // that.heap_len; // number of elements in the heap
  485. // that.heap_max; // element of largest frequency
  486. // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  487. // The same heap array is used to build all trees.
  488. // Depth of each subtree used as tie breaker for trees of equal frequency
  489. that.depth = [];
  490. var l_buf; // index for literals or lengths */
  491. // Size of match buffer for literals/lengths. There are 4 reasons for
  492. // limiting lit_bufsize to 64K:
  493. // - frequencies can be kept in 16 bit counters
  494. // - if compression is not successful for the first block, all input
  495. // data is still in the window so we can still emit a stored block even
  496. // when input comes from standard input. (This can also be done for
  497. // all blocks if lit_bufsize is not greater than 32K.)
  498. // - if compression is not successful for a file smaller than 64K, we can
  499. // even emit a stored file instead of a stored block (saving 5 bytes).
  500. // This is applicable only for zip (not gzip or zlib).
  501. // - creating new Huffman trees less frequently may not provide fast
  502. // adaptation to changes in the input data statistics. (Take for
  503. // example a binary file with poorly compressible code followed by
  504. // a highly compressible string table.) Smaller buffer sizes give
  505. // fast adaptation but have of course the overhead of transmitting
  506. // trees more frequently.
  507. // - I can't count above 4
  508. var lit_bufsize;
  509. var last_lit; // running index in l_buf
  510. // Buffer for distances. To simplify the code, d_buf and l_buf have
  511. // the same number of elements. To use different lengths, an extra flag
  512. // array would be necessary.
  513. var d_buf; // index of pendig_buf
  514. // that.opt_len; // bit length of current block with optimal trees
  515. // that.static_len; // bit length of current block with static trees
  516. var matches; // number of string matches in current block
  517. var last_eob_len; // bit length of EOB code for last block
  518. // Output buffer. bits are inserted starting at the bottom (least
  519. // significant bits).
  520. var bi_buf;
  521. // Number of valid bits in bi_buf. All bits above the last valid bit
  522. // are always zero.
  523. var bi_valid;
  524. // number of codes at each bit length for an optimal tree
  525. that.bl_count = [];
  526. // heap used to build the Huffman trees
  527. that.heap = [];
  528. dyn_ltree = [];
  529. dyn_dtree = [];
  530. bl_tree = [];
  531. function lm_init() {
  532. var i;
  533. window_size = 2 * w_size;
  534. head[hash_size - 1] = 0;
  535. for (i = 0; i < hash_size - 1; i++) {
  536. head[i] = 0;
  537. }
  538. // Set the default configuration parameters:
  539. max_lazy_match = config_table[level].max_lazy;
  540. good_match = config_table[level].good_length;
  541. nice_match = config_table[level].nice_length;
  542. max_chain_length = config_table[level].max_chain;
  543. strstart = 0;
  544. block_start = 0;
  545. lookahead = 0;
  546. match_length = prev_length = MIN_MATCH - 1;
  547. match_available = 0;
  548. ins_h = 0;
  549. }
  550. function init_block() {
  551. var i;
  552. // Initialize the trees.
  553. for (i = 0; i < L_CODES; i++)
  554. dyn_ltree[i * 2] = 0;
  555. for (i = 0; i < D_CODES; i++)
  556. dyn_dtree[i * 2] = 0;
  557. for (i = 0; i < BL_CODES; i++)
  558. bl_tree[i * 2] = 0;
  559. dyn_ltree[END_BLOCK * 2] = 1;
  560. that.opt_len = that.static_len = 0;
  561. last_lit = matches = 0;
  562. }
  563. // Initialize the tree data structures for a new zlib stream.
  564. function tr_init() {
  565. l_desc.dyn_tree = dyn_ltree;
  566. l_desc.stat_desc = StaticTree.static_l_desc;
  567. d_desc.dyn_tree = dyn_dtree;
  568. d_desc.stat_desc = StaticTree.static_d_desc;
  569. bl_desc.dyn_tree = bl_tree;
  570. bl_desc.stat_desc = StaticTree.static_bl_desc;
  571. bi_buf = 0;
  572. bi_valid = 0;
  573. last_eob_len = 8; // enough lookahead for inflate
  574. // Initialize the first block of the first file:
  575. init_block();
  576. }
  577. // Restore the heap property by moving down the tree starting at node k,
  578. // exchanging a node with the smallest of its two sons if necessary,
  579. // stopping
  580. // when the heap property is re-established (each father smaller than its
  581. // two sons).
  582. that.pqdownheap = function(tree, // the tree to restore
  583. k // node to move down
  584. ) {
  585. var heap = that.heap;
  586. var v = heap[k];
  587. var j = k << 1; // left son of k
  588. while (j <= that.heap_len) {
  589. // Set j to the smallest of the two sons:
  590. if (j < that.heap_len && smaller(tree, heap[j + 1], heap[j], that.depth)) {
  591. j++;
  592. }
  593. // Exit if v is smaller than both sons
  594. if (smaller(tree, v, heap[j], that.depth))
  595. break;
  596. // Exchange v with the smallest son
  597. heap[k] = heap[j];
  598. k = j;
  599. // And continue down the tree, setting j to the left son of k
  600. j <<= 1;
  601. }
  602. heap[k] = v;
  603. };
  604. // Scan a literal or distance tree to determine the frequencies of the codes
  605. // in the bit length tree.
  606. function scan_tree(tree,// the tree to be scanned
  607. max_code // and its largest code of non zero frequency
  608. ) {
  609. var n; // iterates over all tree elements
  610. var prevlen = -1; // last emitted length
  611. var curlen; // length of current code
  612. var nextlen = tree[0 * 2 + 1]; // length of next code
  613. var count = 0; // repeat count of the current code
  614. var max_count = 7; // max repeat count
  615. var min_count = 4; // min repeat count
  616. if (nextlen === 0) {
  617. max_count = 138;
  618. min_count = 3;
  619. }
  620. tree[(max_code + 1) * 2 + 1] = 0xffff; // guard
  621. for (n = 0; n <= max_code; n++) {
  622. curlen = nextlen;
  623. nextlen = tree[(n + 1) * 2 + 1];
  624. if (++count < max_count && curlen == nextlen) {
  625. continue;
  626. } else if (count < min_count) {
  627. bl_tree[curlen * 2] += count;
  628. } else if (curlen !== 0) {
  629. if (curlen != prevlen)
  630. bl_tree[curlen * 2]++;
  631. bl_tree[REP_3_6 * 2]++;
  632. } else if (count <= 10) {
  633. bl_tree[REPZ_3_10 * 2]++;
  634. } else {
  635. bl_tree[REPZ_11_138 * 2]++;
  636. }
  637. count = 0;
  638. prevlen = curlen;
  639. if (nextlen === 0) {
  640. max_count = 138;
  641. min_count = 3;
  642. } else if (curlen == nextlen) {
  643. max_count = 6;
  644. min_count = 3;
  645. } else {
  646. max_count = 7;
  647. min_count = 4;
  648. }
  649. }
  650. }
  651. // Construct the Huffman tree for the bit lengths and return the index in
  652. // bl_order of the last bit length code to send.
  653. function build_bl_tree() {
  654. var max_blindex; // index of last bit length code of non zero freq
  655. // Determine the bit length frequencies for literal and distance trees
  656. scan_tree(dyn_ltree, l_desc.max_code);
  657. scan_tree(dyn_dtree, d_desc.max_code);
  658. // Build the bit length tree:
  659. bl_desc.build_tree(that);
  660. // opt_len now includes the length of the tree representations, except
  661. // the lengths of the bit lengths codes and the 5+5+4 bits for the
  662. // counts.
  663. // Determine the number of bit length codes to send. The pkzip format
  664. // requires that at least 4 bit length codes be sent. (appnote.txt says
  665. // 3 but the actual value used is 4.)
  666. for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
  667. if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] !== 0)
  668. break;
  669. }
  670. // Update opt_len to include the bit length tree and counts
  671. that.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  672. return max_blindex;
  673. }
  674. // Output a byte on the stream.
  675. // IN assertion: there is enough room in pending_buf.
  676. function put_byte(p) {
  677. that.pending_buf[that.pending++] = p;
  678. }
  679. function put_short(w) {
  680. put_byte(w & 0xff);
  681. put_byte((w >>> 8) & 0xff);
  682. }
  683. function putShortMSB(b) {
  684. put_byte((b >> 8) & 0xff);
  685. put_byte((b & 0xff) & 0xff);
  686. }
  687. function send_bits(value, length) {
  688. var val, len = length;
  689. if (bi_valid > Buf_size - len) {
  690. val = value;
  691. // bi_buf |= (val << bi_valid);
  692. bi_buf |= ((val << bi_valid) & 0xffff);
  693. put_short(bi_buf);
  694. bi_buf = val >>> (Buf_size - bi_valid);
  695. bi_valid += len - Buf_size;
  696. } else {
  697. // bi_buf |= (value) << bi_valid;
  698. bi_buf |= (((value) << bi_valid) & 0xffff);
  699. bi_valid += len;
  700. }
  701. }
  702. function send_code(c, tree) {
  703. var c2 = c * 2;
  704. send_bits(tree[c2] & 0xffff, tree[c2 + 1] & 0xffff);
  705. }
  706. // Send a literal or distance tree in compressed form, using the codes in
  707. // bl_tree.
  708. function send_tree(tree,// the tree to be sent
  709. max_code // and its largest code of non zero frequency
  710. ) {
  711. var n; // iterates over all tree elements
  712. var prevlen = -1; // last emitted length
  713. var curlen; // length of current code
  714. var nextlen = tree[0 * 2 + 1]; // length of next code
  715. var count = 0; // repeat count of the current code
  716. var max_count = 7; // max repeat count
  717. var min_count = 4; // min repeat count
  718. if (nextlen === 0) {
  719. max_count = 138;
  720. min_count = 3;
  721. }
  722. for (n = 0; n <= max_code; n++) {
  723. curlen = nextlen;
  724. nextlen = tree[(n + 1) * 2 + 1];
  725. if (++count < max_count && curlen == nextlen) {
  726. continue;
  727. } else if (count < min_count) {
  728. do {
  729. send_code(curlen, bl_tree);
  730. } while (--count !== 0);
  731. } else if (curlen !== 0) {
  732. if (curlen != prevlen) {
  733. send_code(curlen, bl_tree);
  734. count--;
  735. }
  736. send_code(REP_3_6, bl_tree);
  737. send_bits(count - 3, 2);
  738. } else if (count <= 10) {
  739. send_code(REPZ_3_10, bl_tree);
  740. send_bits(count - 3, 3);
  741. } else {
  742. send_code(REPZ_11_138, bl_tree);
  743. send_bits(count - 11, 7);
  744. }
  745. count = 0;
  746. prevlen = curlen;
  747. if (nextlen === 0) {
  748. max_count = 138;
  749. min_count = 3;
  750. } else if (curlen == nextlen) {
  751. max_count = 6;
  752. min_count = 3;
  753. } else {
  754. max_count = 7;
  755. min_count = 4;
  756. }
  757. }
  758. }
  759. // Send the header for a block using dynamic Huffman trees: the counts, the
  760. // lengths of the bit length codes, the literal tree and the distance tree.
  761. // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  762. function send_all_trees(lcodes, dcodes, blcodes) {
  763. var rank; // index in bl_order
  764. send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt
  765. send_bits(dcodes - 1, 5);
  766. send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt
  767. for (rank = 0; rank < blcodes; rank++) {
  768. send_bits(bl_tree[Tree.bl_order[rank] * 2 + 1], 3);
  769. }
  770. send_tree(dyn_ltree, lcodes - 1); // literal tree
  771. send_tree(dyn_dtree, dcodes - 1); // distance tree
  772. }
  773. // Flush the bit buffer, keeping at most 7 bits in it.
  774. function bi_flush() {
  775. if (bi_valid == 16) {
  776. put_short(bi_buf);
  777. bi_buf = 0;
  778. bi_valid = 0;
  779. } else if (bi_valid >= 8) {
  780. put_byte(bi_buf & 0xff);
  781. bi_buf >>>= 8;
  782. bi_valid -= 8;
  783. }
  784. }
  785. // Send one empty static block to give enough lookahead for inflate.
  786. // This takes 10 bits, of which 7 may remain in the bit buffer.
  787. // The current inflate code requires 9 bits of lookahead. If the
  788. // last two codes for the previous block (real code plus EOB) were coded
  789. // on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  790. // the last real code. In this case we send two empty static blocks instead
  791. // of one. (There are no problems if the previous block is stored or fixed.)
  792. // To simplify the code, we assume the worst case of last real code encoded
  793. // on one bit only.
  794. function _tr_align() {
  795. send_bits(STATIC_TREES << 1, 3);
  796. send_code(END_BLOCK, StaticTree.static_ltree);
  797. bi_flush();
  798. // Of the 10 bits for the empty block, we have already sent
  799. // (10 - bi_valid) bits. The lookahead for the last real code (before
  800. // the EOB of the previous block) was thus at least one plus the length
  801. // of the EOB plus what we have just sent of the empty static block.
  802. if (1 + last_eob_len + 10 - bi_valid < 9) {
  803. send_bits(STATIC_TREES << 1, 3);
  804. send_code(END_BLOCK, StaticTree.static_ltree);
  805. bi_flush();
  806. }
  807. last_eob_len = 7;
  808. }
  809. // Save the match info and tally the frequency counts. Return true if
  810. // the current block must be flushed.
  811. function _tr_tally(dist, // distance of matched string
  812. lc // match length-MIN_MATCH or unmatched char (if dist==0)
  813. ) {
  814. var out_length, in_length, dcode;
  815. that.pending_buf[d_buf + last_lit * 2] = (dist >>> 8) & 0xff;
  816. that.pending_buf[d_buf + last_lit * 2 + 1] = dist & 0xff;
  817. that.pending_buf[l_buf + last_lit] = lc & 0xff;
  818. last_lit++;
  819. if (dist === 0) {
  820. // lc is the unmatched char
  821. dyn_ltree[lc * 2]++;
  822. } else {
  823. matches++;
  824. // Here, lc is the match length - MIN_MATCH
  825. dist--; // dist = match distance - 1
  826. dyn_ltree[(Tree._length_code[lc] + LITERALS + 1) * 2]++;
  827. dyn_dtree[Tree.d_code(dist) * 2]++;
  828. }
  829. if ((last_lit & 0x1fff) === 0 && level > 2) {
  830. // Compute an upper bound for the compressed length
  831. out_length = last_lit * 8;
  832. in_length = strstart - block_start;
  833. for (dcode = 0; dcode < D_CODES; dcode++) {
  834. out_length += dyn_dtree[dcode * 2] * (5 + Tree.extra_dbits[dcode]);
  835. }
  836. out_length >>>= 3;
  837. if ((matches < Math.floor(last_lit / 2)) && out_length < Math.floor(in_length / 2))
  838. return true;
  839. }
  840. return (last_lit == lit_bufsize - 1);
  841. // We avoid equality with lit_bufsize because of wraparound at 64K
  842. // on 16 bit machines and because stored blocks are restricted to
  843. // 64K-1 bytes.
  844. }
  845. // Send the block data compressed using the given Huffman trees
  846. function compress_block(ltree, dtree) {
  847. var dist; // distance of matched string
  848. var lc; // match length or unmatched char (if dist === 0)
  849. var lx = 0; // running index in l_buf
  850. var code; // the code to send
  851. var extra; // number of extra bits to send
  852. if (last_lit !== 0) {
  853. do {
  854. dist = ((that.pending_buf[d_buf + lx * 2] << 8) & 0xff00) | (that.pending_buf[d_buf + lx * 2 + 1] & 0xff);
  855. lc = (that.pending_buf[l_buf + lx]) & 0xff;
  856. lx++;
  857. if (dist === 0) {
  858. send_code(lc, ltree); // send a literal byte
  859. } else {
  860. // Here, lc is the match length - MIN_MATCH
  861. code = Tree._length_code[lc];
  862. send_code(code + LITERALS + 1, ltree); // send the length
  863. // code
  864. extra = Tree.extra_lbits[code];
  865. if (extra !== 0) {
  866. lc -= Tree.base_length[code];
  867. send_bits(lc, extra); // send the extra length bits
  868. }
  869. dist--; // dist is now the match distance - 1
  870. code = Tree.d_code(dist);
  871. send_code(code, dtree); // send the distance code
  872. extra = Tree.extra_dbits[code];
  873. if (extra !== 0) {
  874. dist -= Tree.base_dist[code];
  875. send_bits(dist, extra); // send the extra distance bits
  876. }
  877. } // literal or match pair ?
  878. // Check that the overlay between pending_buf and d_buf+l_buf is
  879. // ok:
  880. } while (lx < last_lit);
  881. }
  882. send_code(END_BLOCK, ltree);
  883. last_eob_len = ltree[END_BLOCK * 2 + 1];
  884. }
  885. // Flush the bit buffer and align the output on a byte boundary
  886. function bi_windup() {
  887. if (bi_valid > 8) {
  888. put_short(bi_buf);
  889. } else if (bi_valid > 0) {
  890. put_byte(bi_buf & 0xff);
  891. }
  892. bi_buf = 0;
  893. bi_valid = 0;
  894. }
  895. // Copy a stored block, storing first the length and its
  896. // one's complement if requested.
  897. function copy_block(buf, // the input data
  898. len, // its length
  899. header // true if block header must be written
  900. ) {
  901. bi_windup(); // align on byte boundary
  902. last_eob_len = 8; // enough lookahead for inflate
  903. if (header) {
  904. put_short(len);
  905. put_short(~len);
  906. }
  907. that.pending_buf.set(window.subarray(buf, buf + len), that.pending);
  908. that.pending += len;
  909. }
  910. // Send a stored block
  911. function _tr_stored_block(buf, // input block
  912. stored_len, // length of input block
  913. eof // true if this is the last block for a file
  914. ) {
  915. send_bits((STORED_BLOCK << 1) + (eof ? 1 : 0), 3); // send block type
  916. copy_block(buf, stored_len, true); // with header
  917. }
  918. // Determine the best encoding for the current block: dynamic trees, static
  919. // trees or store, and output the encoded block to the zip file.
  920. function _tr_flush_block(buf, // input block, or NULL if too old
  921. stored_len, // length of input block
  922. eof // true if this is the last block for a file
  923. ) {
  924. var opt_lenb, static_lenb;// opt_len and static_len in bytes
  925. var max_blindex = 0; // index of last bit length code of non zero freq
  926. // Build the Huffman trees unless a stored block is forced
  927. if (level > 0) {
  928. // Construct the literal and distance trees
  929. l_desc.build_tree(that);
  930. d_desc.build_tree(that);
  931. // At this point, opt_len and static_len are the total bit lengths
  932. // of
  933. // the compressed block data, excluding the tree representations.
  934. // Build the bit length tree for the above two trees, and get the
  935. // index
  936. // in bl_order of the last bit length code to send.
  937. max_blindex = build_bl_tree();
  938. // Determine the best encoding. Compute first the block length in
  939. // bytes
  940. opt_lenb = (that.opt_len + 3 + 7) >>> 3;
  941. static_lenb = (that.static_len + 3 + 7) >>> 3;
  942. if (static_lenb <= opt_lenb)
  943. opt_lenb = static_lenb;
  944. } else {
  945. opt_lenb = static_lenb = stored_len + 5; // force a stored block
  946. }
  947. if ((stored_len + 4 <= opt_lenb) && buf != -1) {
  948. // 4: two words for the lengths
  949. // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  950. // Otherwise we can't have processed more than WSIZE input bytes
  951. // since
  952. // the last block flush, because compression would have been
  953. // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  954. // transform a block into a stored block.
  955. _tr_stored_block(buf, stored_len, eof);
  956. } else if (static_lenb == opt_lenb) {
  957. send_bits((STATIC_TREES << 1) + (eof ? 1 : 0), 3);
  958. compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
  959. } else {
  960. send_bits((DYN_TREES << 1) + (eof ? 1 : 0), 3);
  961. send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1);
  962. compress_block(dyn_ltree, dyn_dtree);
  963. }
  964. // The above check is made mod 2^32, for files larger than 512 MB
  965. // and uLong implemented on 32 bits.
  966. init_block();
  967. if (eof) {
  968. bi_windup();
  969. }
  970. }
  971. function flush_block_only(eof) {
  972. _tr_flush_block(block_start >= 0 ? block_start : -1, strstart - block_start, eof);
  973. block_start = strstart;
  974. strm.flush_pending();
  975. }
  976. // Fill the window when the lookahead becomes insufficient.
  977. // Updates strstart and lookahead.
  978. //
  979. // IN assertion: lookahead < MIN_LOOKAHEAD
  980. // OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  981. // At least one byte has been read, or avail_in === 0; reads are
  982. // performed for at least two bytes (required for the zip translate_eol
  983. // option -- not supported here).
  984. function fill_window() {
  985. var n, m;
  986. var p;
  987. var more; // Amount of free space at the end of the window.
  988. do {
  989. more = (window_size - lookahead - strstart);
  990. // Deal with !@#$% 64K limit:
  991. if (more === 0 && strstart === 0 && lookahead === 0) {
  992. more = w_size;
  993. } else if (more == -1) {
  994. // Very unlikely, but possible on 16 bit machine if strstart ==
  995. // 0
  996. // and lookahead == 1 (input done one byte at time)
  997. more--;
  998. // If the window is almost full and there is insufficient
  999. // lookahead,
  1000. // move the upper half to the lower one to make room in the
  1001. // upper half.
  1002. } else if (strstart >= w_size + w_size - MIN_LOOKAHEAD) {
  1003. window.set(window.subarray(w_size, w_size + w_size), 0);
  1004. match_start -= w_size;
  1005. strstart -= w_size; // we now have strstart >= MAX_DIST
  1006. block_start -= w_size;
  1007. // Slide the hash table (could be avoided with 32 bit values
  1008. // at the expense of memory usage). We slide even when level ==
  1009. // 0
  1010. // to keep the hash table consistent if we switch back to level
  1011. // > 0
  1012. // later. (Using level 0 permanently is not an optimal usage of
  1013. // zlib, so we don't care about this pathological case.)
  1014. n = hash_size;
  1015. p = n;
  1016. do {
  1017. m = (head[--p] & 0xffff);
  1018. head[p] = (m >= w_size ? m - w_size : 0);
  1019. } while (--n !== 0);
  1020. n = w_size;
  1021. p = n;
  1022. do {
  1023. m = (prev[--p] & 0xffff);
  1024. prev[p] = (m >= w_size ? m - w_size : 0);
  1025. // If n is not on any hash chain, prev[n] is garbage but
  1026. // its value will never be used.
  1027. } while (--n !== 0);
  1028. more += w_size;
  1029. }
  1030. if (strm.avail_in === 0)
  1031. return;
  1032. // If there was no sliding:
  1033. // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1034. // more == window_size - lookahead - strstart
  1035. // => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1036. // => more >= window_size - 2*WSIZE + 2
  1037. // In the BIG_MEM or MMAP case (not yet supported),
  1038. // window_size == input_size + MIN_LOOKAHEAD &&
  1039. // strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1040. // Otherwise, window_size == 2*WSIZE so more >= 2.
  1041. // If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1042. n = strm.read_buf(window, strstart + lookahead, more);
  1043. lookahead += n;
  1044. // Initialize the hash value now that we have some input:
  1045. if (lookahead >= MIN_MATCH) {
  1046. ins_h = window[strstart] & 0xff;
  1047. ins_h = (((ins_h) << hash_shift) ^ (window[strstart + 1] & 0xff)) & hash_mask;
  1048. }
  1049. // If the whole input has less than MIN_MATCH bytes, ins_h is
  1050. // garbage,
  1051. // but this is not important since only literal bytes will be
  1052. // emitted.
  1053. } while (lookahead < MIN_LOOKAHEAD && strm.avail_in !== 0);
  1054. }
  1055. // Copy without compression as much as possible from the input stream,
  1056. // return
  1057. // the current block state.
  1058. // This function does not insert new strings in the dictionary since
  1059. // uncompressible data is probably not useful. This function is used
  1060. // only for the level=0 compression option.
  1061. // NOTE: this function should be optimized to avoid extra copying from
  1062. // window to pending_buf.
  1063. function deflate_stored(flush) {
  1064. // Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1065. // to pending_buf_size, and each stored block has a 5 byte header:
  1066. var max_block_size = 0xffff;
  1067. var max_start;
  1068. if (max_block_size > pending_buf_size - 5) {
  1069. max_block_size = pending_buf_size - 5;
  1070. }
  1071. // Copy as much as possible from input to output:
  1072. while (true) {
  1073. // Fill the window as much as possible:
  1074. if (lookahead <= 1) {
  1075. fill_window();
  1076. if (lookahead === 0 && flush == Z_NO_FLUSH)
  1077. return NeedMore;
  1078. if (lookahead === 0)
  1079. break; // flush the current block
  1080. }
  1081. strstart += lookahead;
  1082. lookahead = 0;
  1083. // Emit a stored block if pending_buf will be full:
  1084. max_start = block_start + max_block_size;
  1085. if (strstart === 0 || strstart >= max_start) {
  1086. // strstart === 0 is possible when wraparound on 16-bit machine
  1087. lookahead = (strstart - max_start);
  1088. strstart = max_start;
  1089. flush_block_only(false);
  1090. if (strm.avail_out === 0)
  1091. return NeedMore;
  1092. }
  1093. // Flush if we may have to slide, otherwise block_start may become
  1094. // negative and the data will be gone:
  1095. if (strstart - block_start >= w_size - MIN_LOOKAHEAD) {
  1096. flush_block_only(false);
  1097. if (strm.avail_out === 0)
  1098. return NeedMore;
  1099. }
  1100. }
  1101. flush_block_only(flush == Z_FINISH);
  1102. if (strm.avail_out === 0)
  1103. return (flush == Z_FINISH) ? FinishStarted : NeedMore;
  1104. return flush == Z_FINISH ? FinishDone : BlockDone;
  1105. }
  1106. function longest_match(cur_match) {
  1107. var chain_length = max_chain_length; // max hash chain length
  1108. var scan = strstart; // current string
  1109. var match; // matched string
  1110. var len; // length of current match
  1111. var best_len = prev_length; // best match length so far
  1112. var limit = strstart > (w_size - MIN_LOOKAHEAD) ? strstart - (w_size - MIN_LOOKAHEAD) : 0;
  1113. var _nice_match = nice_match;
  1114. // Stop when cur_match becomes <= limit. To simplify the code,
  1115. // we prevent matches with the string of window index 0.
  1116. var wmask = w_mask;
  1117. var strend = strstart + MAX_MATCH;
  1118. var scan_end1 = window[scan + best_len - 1];
  1119. var scan_end = window[scan + best_len];
  1120. // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of
  1121. // 16.
  1122. // It is easy to get rid of this optimization if necessary.
  1123. // Do not waste too much time if we already have a good match:
  1124. if (prev_length >= good_match) {
  1125. chain_length >>= 2;
  1126. }
  1127. // Do not look for matches beyond the end of the input. This is
  1128. // necessary
  1129. // to make deflate deterministic.
  1130. if (_nice_match > lookahead)
  1131. _nice_match = lookahead;
  1132. do {
  1133. match = cur_match;
  1134. // Skip to next match if the match length cannot increase
  1135. // or if the match length is less than 2:
  1136. if (window[match + best_len] != scan_end || window[match + best_len - 1] != scan_end1 || window[match] != window[scan]
  1137. || window[++match] != window[scan + 1])
  1138. continue;
  1139. // The check at best_len-1 can be removed because it will be made
  1140. // again later. (This heuristic is not always a win.)
  1141. // It is not necessary to compare scan[2] and match[2] since they
  1142. // are always equal when the other bytes match, given that
  1143. // the hash keys are equal and that HASH_BITS >= 8.
  1144. scan += 2;
  1145. match++;
  1146. // We check for insufficient lookahead only every 8th comparison;
  1147. // the 256th check will be made at strstart+258.
  1148. do {
  1149. } while (window[++scan] == window[++match] && window[++scan] == window[++match] && window[++scan] == window[++match]
  1150. && window[++scan] == window[++match] && window[++scan] == window[++match] && window[++scan] == window[++match]
  1151. && window[++scan] == window[++match] && window[++scan] == window[++match] && scan < strend);
  1152. len = MAX_MATCH - (strend - scan);
  1153. scan = strend - MAX_MATCH;
  1154. if (len > best_len) {
  1155. match_start = cur_match;
  1156. best_len = len;
  1157. if (len >= _nice_match)
  1158. break;
  1159. scan_end1 = window[scan + best_len - 1];
  1160. scan_end = window[scan + best_len];
  1161. }
  1162. } while ((cur_match = (prev[cur_match & wmask] & 0xffff)) > limit && --chain_length !== 0);
  1163. if (best_len <= lookahead)
  1164. return best_len;
  1165. return lookahead;
  1166. }
  1167. // Compress as much as possible from the input stream, return the current
  1168. // block state.
  1169. // This function does not perform lazy evaluation of matches and inserts
  1170. // new strings in the dictionary only for unmatched strings or for short
  1171. // matches. It is used only for the fast compression options.
  1172. function deflate_fast(flush) {
  1173. // short hash_head = 0; // head of the hash chain
  1174. var hash_head = 0; // head of the hash chain
  1175. var bflush; // set if current block must be flushed
  1176. while (true) {
  1177. // Make sure that we always have enough lookahead, except
  1178. // at the end of the input file. We need MAX_MATCH bytes
  1179. // for the next match, plus MIN_MATCH bytes to insert the
  1180. // string following the next match.
  1181. if (lookahead < MIN_LOOKAHEAD) {
  1182. fill_window();
  1183. if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1184. return NeedMore;
  1185. }
  1186. if (lookahead === 0)
  1187. break; // flush the current block
  1188. }
  1189. // Insert the string window[strstart .. strstart+2] in the
  1190. // dictionary, and set hash_head to the head of the hash chain:
  1191. if (lookahead >= MIN_MATCH) {
  1192. ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1193. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1194. hash_head = (head[ins_h] & 0xffff);
  1195. prev[strstart & w_mask] = head[ins_h];
  1196. head[ins_h] = strstart;
  1197. }
  1198. // Find the longest match, discarding those <= prev_length.
  1199. // At this point we have always match_length < MIN_MATCH
  1200. if (hash_head !== 0 && ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD) {
  1201. // To simplify the code, we prevent matches with the string
  1202. // of window index 0 (in particular we have to avoid a match
  1203. // of the string with itself at the start of the input file).
  1204. if (strategy != Z_HUFFMAN_ONLY) {
  1205. match_length = longest_match(hash_head);
  1206. }
  1207. // longest_match() sets match_start
  1208. }
  1209. if (match_length >= MIN_MATCH) {
  1210. // check_match(strstart, match_start, match_length);
  1211. bflush = _tr_tally(strstart - match_start, match_length - MIN_MATCH);
  1212. lookahead -= match_length;
  1213. // Insert new strings in the hash table only if the match length
  1214. // is not too large. This saves time but degrades compression.
  1215. if (match_length <= max_lazy_match && lookahead >= MIN_MATCH) {
  1216. match_length--; // string at strstart already in hash table
  1217. do {
  1218. strstart++;
  1219. ins_h = ((ins_h << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1220. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1221. hash_head = (head[ins_h] & 0xffff);
  1222. prev[strstart & w_mask] = head[ins_h];
  1223. head[ins_h] = strstart;
  1224. // strstart never exceeds WSIZE-MAX_MATCH, so there are
  1225. // always MIN_MATCH bytes ahead.
  1226. } while (--match_length !== 0);
  1227. strstart++;
  1228. } else {
  1229. strstart += match_length;
  1230. match_length = 0;
  1231. ins_h = window[strstart] & 0xff;
  1232. ins_h = (((ins_h) << hash_shift) ^ (window[strstart + 1] & 0xff)) & hash_mask;
  1233. // If lookahead < MIN_MATCH, ins_h is garbage, but it does
  1234. // not
  1235. // matter since it will be recomputed at next deflate call.
  1236. }
  1237. } else {
  1238. // No match, output a literal byte
  1239. bflush = _tr_tally(0, window[strstart] & 0xff);
  1240. lookahead--;
  1241. strstart++;
  1242. }
  1243. if (bflush) {
  1244. flush_block_only(false);
  1245. if (strm.avail_out === 0)
  1246. return NeedMore;
  1247. }
  1248. }
  1249. flush_block_only(flush == Z_FINISH);
  1250. if (strm.avail_out === 0) {
  1251. if (flush == Z_FINISH)
  1252. return FinishStarted;
  1253. else
  1254. return NeedMore;
  1255. }
  1256. return flush == Z_FINISH ? FinishDone : BlockDone;
  1257. }
  1258. // Same as above, but achieves better compression. We use a lazy
  1259. // evaluation for matches: a match is finally adopted only if there is
  1260. // no better match at the next window position.
  1261. function deflate_slow(flush) {
  1262. // short hash_head = 0; // head of hash chain
  1263. var hash_head = 0; // head of hash chain
  1264. var bflush; // set if current block must be flushed
  1265. var max_insert;
  1266. // Process the input block.
  1267. while (true) {
  1268. // Make sure that we always have enough lookahead, except
  1269. // at the end of the input file. We need MAX_MATCH bytes
  1270. // for the next match, plus MIN_MATCH bytes to insert the
  1271. // string following the next match.
  1272. if (lookahead < MIN_LOOKAHEAD) {
  1273. fill_window();
  1274. if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1275. return NeedMore;
  1276. }
  1277. if (lookahead === 0)
  1278. break; // flush the current block
  1279. }
  1280. // Insert the string window[strstart .. strstart+2] in the
  1281. // dictionary, and set hash_head to the head of the hash chain:
  1282. if (lookahead >= MIN_MATCH) {
  1283. ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1284. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1285. hash_head = (head[ins_h] & 0xffff);
  1286. prev[strstart & w_mask] = head[ins_h];
  1287. head[ins_h] = strstart;
  1288. }
  1289. // Find the longest match, discarding those <= prev_length.
  1290. prev_length = match_length;
  1291. prev_match = match_start;
  1292. match_length = MIN_MATCH - 1;
  1293. if (hash_head !== 0 && prev_length < max_lazy_match && ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD) {
  1294. // To simplify the code, we prevent matches with the string
  1295. // of window index 0 (in particular we have to avoid a match
  1296. // of the string with itself at the start of the input file).
  1297. if (strategy != Z_HUFFMAN_ONLY) {
  1298. match_length = longest_match(hash_head);
  1299. }
  1300. // longest_match() sets match_start
  1301. if (match_length <= 5 && (strategy == Z_FILTERED || (match_length == MIN_MATCH && strstart - match_start > 4096))) {
  1302. // If prev_match is also MIN_MATCH, match_start is garbage
  1303. // but we will ignore the current match anyway.
  1304. match_length = MIN_MATCH - 1;
  1305. }
  1306. }
  1307. // If there was a match at the previous step and the current
  1308. // match is not better, output the previous match:
  1309. if (prev_length >= MIN_MATCH && match_length <= prev_length) {
  1310. max_insert = strstart + lookahead - MIN_MATCH;
  1311. // Do not insert strings in hash table beyond this.
  1312. // check_match(strstart-1, prev_match, prev_length);
  1313. bflush = _tr_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH);
  1314. // Insert in hash table all strings up to the end of the match.
  1315. // strstart-1 and strstart are already inserted. If there is not
  1316. // enough lookahead, the last two strings are not inserted in
  1317. // the hash table.
  1318. lookahead -= prev_length - 1;
  1319. prev_length -= 2;
  1320. do {
  1321. if (++strstart <= max_insert) {
  1322. ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1323. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1324. hash_head = (head[ins_h] & 0xffff);
  1325. prev[strstart & w_mask] = head[ins_h];
  1326. head[ins_h] = strstart;
  1327. }
  1328. } while (--prev_length !== 0);
  1329. match_available = 0;
  1330. match_length = MIN_MATCH - 1;
  1331. strstart++;
  1332. if (bflush) {
  1333. flush_block_only(false);
  1334. if (strm.avail_out === 0)
  1335. return NeedMore;
  1336. }
  1337. } else if (match_available !== 0) {
  1338. // If there was no match at the previous position, output a
  1339. // single literal. If there was a match but the current match
  1340. // is longer, truncate the previous match to a single literal.
  1341. bflush = _tr_tally(0, window[strstart - 1] & 0xff);
  1342. if (bflush) {
  1343. flush_block_only(false);
  1344. }
  1345. strstart++;
  1346. lookahead--;
  1347. if (strm.avail_out === 0)
  1348. return NeedMore;
  1349. } else {
  1350. // There is no previous match to compare with, wait for
  1351. // the next step to decide.
  1352. match_available = 1;
  1353. strstart++;
  1354. lookahead--;
  1355. }
  1356. }
  1357. if (match_available !== 0) {
  1358. bflush = _tr_tally(0, window[strstart - 1] & 0xff);
  1359. match_available = 0;
  1360. }
  1361. flush_block_only(flush == Z_FINISH);
  1362. if (strm.avail_out === 0) {
  1363. if (flush == Z_FINISH)
  1364. return FinishStarted;
  1365. else
  1366. return NeedMore;
  1367. }
  1368. return flush == Z_FINISH ? FinishDone : BlockDone;
  1369. }
  1370. function deflateReset(strm) {
  1371. strm.total_in = strm.total_out = 0;
  1372. strm.msg = null; //
  1373. that.pending = 0;
  1374. that.pending_out = 0;
  1375. status = BUSY_STATE;
  1376. last_flush = Z_NO_FLUSH;
  1377. tr_init();
  1378. lm_init();
  1379. return Z_OK;
  1380. }
  1381. that.deflateInit = function(strm, _level, bits, _method, memLevel, _strategy) {
  1382. if (!_method)
  1383. _method = Z_DEFLATED;
  1384. if (!memLevel)
  1385. memLevel = DEF_MEM_LEVEL;
  1386. if (!_strategy)
  1387. _strategy = Z_DEFAULT_STRATEGY;
  1388. // byte[] my_version=ZLIB_VERSION;
  1389. //
  1390. // if (!version || version[0] != my_version[0]
  1391. // || stream_size != sizeof(z_stream)) {
  1392. // return Z_VERSION_ERROR;
  1393. // }
  1394. strm.msg = null;
  1395. if (_level == Z_DEFAULT_COMPRESSION)
  1396. _level = 6;
  1397. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || _method != Z_DEFLATED || bits < 9 || bits > 15 || _level < 0 || _level > 9 || _strategy < 0
  1398. || _strategy > Z_HUFFMAN_ONLY) {
  1399. return Z_STREAM_ERROR;
  1400. }
  1401. strm.dstate = that;
  1402. w_bits = bits;
  1403. w_size = 1 << w_bits;
  1404. w_mask = w_size - 1;
  1405. hash_bits = memLevel + 7;
  1406. hash_size = 1 << hash_bits;
  1407. hash_mask = hash_size - 1;
  1408. hash_shift = Math.floor((hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  1409. window = new Uint8Array(w_size * 2);
  1410. prev = [];
  1411. head = [];
  1412. lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
  1413. // We overlay pending_buf and d_buf+l_buf. This works since the average
  1414. // output size for (length,distance) codes is <= 24 bits.
  1415. that.pending_buf = new Uint8Array(lit_bufsize * 4);
  1416. pending_buf_size = lit_bufsize * 4;
  1417. d_buf = Math.floor(lit_bufsize / 2);
  1418. l_buf = (1 + 2) * lit_bufsize;
  1419. level = _level;
  1420. strategy = _strategy;
  1421. method = _method & 0xff;
  1422. return deflateReset(strm);
  1423. };
  1424. that.deflateEnd = function() {
  1425. if (status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE) {
  1426. return Z_STREAM_ERROR;
  1427. }
  1428. // Deallocate in reverse order of allocations:
  1429. that.pending_buf = null;
  1430. head = null;
  1431. prev = null;
  1432. window = null;
  1433. // free
  1434. that.dstate = null;
  1435. return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  1436. };
  1437. that.deflateParams = function(strm, _level, _strategy) {
  1438. var err = Z_OK;
  1439. if (_level == Z_DEFAULT_COMPRESSION) {
  1440. _level = 6;
  1441. }
  1442. if (_level < 0 || _level > 9 || _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
  1443. return Z_STREAM_ERROR;
  1444. }
  1445. if (config_table[level].func != config_table[_level].func && strm.total_in !== 0) {
  1446. // Flush the last buffer:
  1447. err = strm.deflate(Z_PARTIAL_FLUSH);
  1448. }
  1449. if (level != _level) {
  1450. level = _level;
  1451. max_lazy_match = config_table[level].max_lazy;
  1452. good_match = config_table[level].good_length;
  1453. nice_match = config_table[level].nice_length;
  1454. max_chain_length = config_table[level].max_chain;
  1455. }
  1456. strategy = _strategy;
  1457. return err;
  1458. };
  1459. that.deflateSetDictionary = function(strm, dictionary, dictLength) {
  1460. var length = dictLength;
  1461. var n, index = 0;
  1462. if (!dictionary || status != INIT_STATE)
  1463. return Z_STREAM_ERROR;
  1464. if (length < MIN_MATCH)
  1465. return Z_OK;
  1466. if (length > w_size - MIN_LOOKAHEAD) {
  1467. length = w_size - MIN_LOOKAHEAD;
  1468. index = dictLength - length; // use the tail of the dictionary
  1469. }
  1470. window.set(dictionary.subarray(index, index + length), 0);
  1471. strstart = length;
  1472. block_start = length;
  1473. // Insert all strings in the hash table (except for the last two bytes).
  1474. // s->lookahead stays null, so s->ins_h will be recomputed at the next
  1475. // call of fill_window.
  1476. ins_h = window[0] & 0xff;
  1477. ins_h = (((ins_h) << hash_shift) ^ (window[1] & 0xff)) & hash_mask;
  1478. for (n = 0; n <= length - MIN_MATCH; n++) {
  1479. ins_h = (((ins_h) << hash_shift) ^ (window[(n) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1480. prev[n & w_mask] = head[ins_h];
  1481. head[ins_h] = n;
  1482. }
  1483. return Z_OK;
  1484. };
  1485. that.deflate = function(_strm, flush) {
  1486. var i, header, level_flags, old_flush, bstate;
  1487. if (flush > Z_FINISH || flush < 0) {
  1488. return Z_STREAM_ERROR;
  1489. }
  1490. if (!_strm.next_out || (!_strm.next_in && _strm.avail_in !== 0) || (status == FINISH_STATE && flush != Z_FINISH)) {
  1491. _strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)];
  1492. return Z_STREAM_ERROR;
  1493. }
  1494. if (_strm.avail_out === 0) {
  1495. _strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
  1496. return Z_BUF_ERROR;
  1497. }
  1498. strm = _strm; // just in case
  1499. old_flush = last_flush;
  1500. last_flush = flush;
  1501. // Write the zlib header
  1502. if (status == INIT_STATE) {
  1503. header = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8;
  1504. level_flags = ((level - 1) & 0xff) >> 1;
  1505. if (level_flags > 3)
  1506. level_flags = 3;
  1507. header |= (level_flags << 6);
  1508. if (strstart !== 0)
  1509. header |= PRESET_DICT;
  1510. header += 31 - (header % 31);
  1511. status = BUSY_STATE;
  1512. putShortMSB(header);
  1513. }
  1514. // Flush as much pending output as possible
  1515. if (that.pending !== 0) {
  1516. strm.flush_pending();
  1517. if (strm.avail_out === 0) {
  1518. // console.log(" avail_out==0");
  1519. // Since avail_out is 0, deflate will be called again with
  1520. // more output space, but possibly with both pending and
  1521. // avail_in equal to zero. There won't be anything to do,
  1522. // but this is not an error situation so make sure we
  1523. // return OK instead of BUF_ERROR at next call of deflate:
  1524. last_flush = -1;
  1525. return Z_OK;
  1526. }
  1527. // Make sure there is something to do and avoid duplicate
  1528. // consecutive
  1529. // flushes. For repeated and useless calls with Z_FINISH, we keep
  1530. // returning Z_STREAM_END instead of Z_BUFF_ERROR.
  1531. } else if (strm.avail_in === 0 && flush <= old_flush && flush != Z_FINISH) {
  1532. strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
  1533. return Z_BUF_ERROR;
  1534. }
  1535. // User must not provide more input after the first FINISH:
  1536. if (status == FINISH_STATE && strm.avail_in !== 0) {
  1537. _strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
  1538. return Z_BUF_ERROR;
  1539. }
  1540. // Start a new block or continue the current one.
  1541. if (strm.avail_in !== 0 || lookahead !== 0 || (flush != Z_NO_FLUSH && status != FINISH_STATE)) {
  1542. bstate = -1;
  1543. switch (config_table[level].func) {
  1544. case STORED:
  1545. bstate = deflate_stored(flush);
  1546. break;
  1547. case FAST:
  1548. bstate = deflate_fast(flush);
  1549. break;
  1550. case SLOW:
  1551. bstate = deflate_slow(flush);
  1552. break;
  1553. default:
  1554. }
  1555. if (bstate == FinishStarted || bstate == FinishDone) {
  1556. status = FINISH_STATE;
  1557. }
  1558. if (bstate == NeedMore || bstate == FinishStarted) {
  1559. if (strm.avail_out === 0) {
  1560. last_flush = -1; // avoid BUF_ERROR next call, see above
  1561. }
  1562. return Z_OK;
  1563. // If flush != Z_NO_FLUSH && avail_out === 0, the next call
  1564. // of deflate should use the same flush parameter to make sure
  1565. // that the flush is complete. So we don't have to output an
  1566. // empty block here, this will be done at next call. This also
  1567. // ensures that for a very small output buffer, we emit at most
  1568. // one empty block.
  1569. }
  1570. if (bstate == BlockDone) {
  1571. if (flush == Z_PARTIAL_FLUSH) {
  1572. _tr_align();
  1573. } else { // FULL_FLUSH or SYNC_FLUSH
  1574. _tr_stored_block(0, 0, false);
  1575. // For a full flush, this empty block will be recognized
  1576. // as a special marker by inflate_sync().
  1577. if (flush == Z_FULL_FLUSH) {
  1578. // state.head[s.hash_size-1]=0;
  1579. for (i = 0; i < hash_size/*-1*/; i++)
  1580. // forget history
  1581. head[i] = 0;
  1582. }
  1583. }
  1584. strm.flush_pending();
  1585. if (strm.avail_out === 0) {
  1586. last_flush = -1; // avoid BUF_ERROR at next call, see above
  1587. return Z_OK;
  1588. }
  1589. }
  1590. }
  1591. if (flush != Z_FINISH)
  1592. return Z_OK;
  1593. return Z_STREAM_END;
  1594. };
  1595. }
  1596. // ZStream
  1597. function ZStream() {
  1598. var that = this;
  1599. that.next_in_index = 0;
  1600. that.next_out_index = 0;
  1601. // that.next_in; // next input byte
  1602. that.avail_in = 0; // number of bytes available at next_in
  1603. that.total_in = 0; // total nb of input bytes read so far
  1604. // that.next_out; // next output byte should be put there
  1605. that.avail_out = 0; // remaining free space at next_out
  1606. that.total_out = 0; // total nb of bytes output so far
  1607. // that.msg;
  1608. // that.dstate;
  1609. }
  1610. ZStream.prototype = {
  1611. deflateInit : function(level, bits) {
  1612. var that = this;
  1613. that.dstate = new Deflate();
  1614. if (!bits)
  1615. bits = MAX_BITS;
  1616. return that.dstate.deflateInit(that, level, bits);
  1617. },
  1618. deflate : function(flush) {
  1619. var that = this;
  1620. if (!that.dstate) {
  1621. return Z_STREAM_ERROR;
  1622. }
  1623. return that.dstate.deflate(that, flush);
  1624. },
  1625. deflateEnd : function() {
  1626. var that = this;
  1627. if (!that.dstate)
  1628. return Z_STREAM_ERROR;
  1629. var ret = that.dstate.deflateEnd();
  1630. that.dstate = null;
  1631. return ret;
  1632. },
  1633. deflateParams : function(level, strategy) {
  1634. var that = this;
  1635. if (!that.dstate)
  1636. return Z_STREAM_ERROR;
  1637. return that.dstate.deflateParams(that, level, strategy);
  1638. },
  1639. deflateSetDictionary : function(dictionary, dictLength) {
  1640. var that = this;
  1641. if (!that.dstate)
  1642. return Z_STREAM_ERROR;
  1643. return that.dstate.deflateSetDictionary(that, dictionary, dictLength);
  1644. },
  1645. // Read a new buffer from the current input stream, update the
  1646. // total number of bytes read. All deflate() input goes through
  1647. // this function so some applications may wish to modify it to avoid
  1648. // allocating a large strm->next_in buffer and copying from it.
  1649. // (See also flush_pending()).
  1650. read_buf : function(buf, start, size) {
  1651. var that = this;
  1652. var len = that.avail_in;
  1653. if (len > size)
  1654. len = size;
  1655. if (len === 0)
  1656. return 0;
  1657. that.avail_in -= len;
  1658. buf.set(that.next_in.subarray(that.next_in_index, that.next_in_index + len), start);
  1659. that.next_in_index += len;
  1660. that.total_in += len;
  1661. return len;
  1662. },
  1663. // Flush as much pending output as possible. All deflate() output goes
  1664. // through this function so some applications may wish to modify it
  1665. // to avoid allocating a large strm->next_out buffer and copying into it.
  1666. // (See also read_buf()).
  1667. flush_pending : function() {
  1668. var that = this;
  1669. var len = that.dstate.pending;
  1670. if (len > that.avail_out)
  1671. len = that.avail_out;
  1672. if (len === 0)
  1673. return;
  1674. // if (that.dstate.pending_buf.length <= that.dstate.pending_out || that.next_out.length <= that.next_out_index
  1675. // || that.dstate.pending_buf.length < (that.dstate.pending_out + len) || that.next_out.length < (that.next_out_index +
  1676. // len)) {
  1677. // console.log(that.dstate.pending_buf.length + ", " + that.dstate.pending_out + ", " + that.next_out.length + ", " +
  1678. // that.next_out_index + ", " + len);
  1679. // console.log("avail_out=" + that.avail_out);
  1680. // }
  1681. that.next_out.set(that.dstate.pending_buf.subarray(that.dstate.pending_out, that.dstate.pending_out + len), that.next_out_index);
  1682. that.next_out_index += len;
  1683. that.dstate.pending_out += len;
  1684. that.total_out += len;
  1685. that.avail_out -= len;
  1686. that.dstate.pending -= len;
  1687. if (that.dstate.pending === 0) {
  1688. that.dstate.pending_out = 0;
  1689. }
  1690. }
  1691. };
  1692. // Deflater
  1693. function Deflater(level) {
  1694. var that = this;
  1695. var z = new ZStream();
  1696. var bufsize = 512;
  1697. var flush = Z_NO_FLUSH;
  1698. var buf = new Uint8Array(bufsize);
  1699. if (typeof level == "undefined")
  1700. level = Z_DEFAULT_COMPRESSION;
  1701. z.deflateInit(level);
  1702. z.next_out = buf;
  1703. that.append = function(data, onprogress) {
  1704. var err, buffers = [], lastIndex = 0, bufferIndex = 0, bufferSize = 0, array;
  1705. if (!data.length)
  1706. return;
  1707. z.next_in_index = 0;
  1708. z.next_in = data;
  1709. z.avail_in = data.length;
  1710. do {
  1711. z.next_out_index = 0;
  1712. z.avail_out = bufsize;
  1713. err = z.deflate(flush);
  1714. if (err != Z_OK)
  1715. throw "deflating: " + z.msg;
  1716. if (z.next_out_index)
  1717. if (z.next_out_index == bufsize)
  1718. buffers.push(new Uint8Array(buf));
  1719. else
  1720. buffers.push(new Uint8Array(buf.subarray(0, z.next_out_index)));
  1721. bufferSize += z.next_out_index;
  1722. if (onprogress && z.next_in_index > 0 && z.next_in_index != lastIndex) {
  1723. onprogress(z.next_in_index);
  1724. lastIndex = z.next_in_index;
  1725. }
  1726. } while (z.avail_in > 0 || z.avail_out === 0);
  1727. array = new Uint8Array(bufferSize);
  1728. buffers.forEach(function(chunk) {
  1729. array.set(chunk, bufferIndex);
  1730. bufferIndex += chunk.length;
  1731. });
  1732. return array;
  1733. };
  1734. that.flush = function() {
  1735. var err, buffers = [], bufferIndex = 0, bufferSize = 0, array;
  1736. do {
  1737. z.next_out_index = 0;
  1738. z.avail_out = bufsize;
  1739. err = z.deflate(Z_FINISH);
  1740. if (err != Z_STREAM_END && err != Z_OK)
  1741. throw "deflating: " + z.msg;
  1742. if (bufsize - z.avail_out > 0)
  1743. buffers.push(new Uint8Array(buf.subarray(0, z.next_out_index)));
  1744. bufferSize += z.next_out_index;
  1745. } while (z.avail_in > 0 || z.avail_out === 0);
  1746. z.deflateEnd();
  1747. array = new Uint8Array(bufferSize);
  1748. buffers.forEach(function(chunk) {
  1749. array.set(chunk, bufferIndex);
  1750. bufferIndex += chunk.length;
  1751. });
  1752. return array;
  1753. };
  1754. }
  1755. var deflater;
  1756. if (obj.zip)
  1757. obj.zip.Deflater = Deflater;
  1758. else {
  1759. deflater = new Deflater();
  1760. obj.addEventListener("message", function(event) {
  1761. var message = event.data;
  1762. if (message.init) {
  1763. deflater = new Deflater(message.level);
  1764. obj.postMessage({
  1765. oninit : true
  1766. });
  1767. }
  1768. if (message.append)
  1769. obj.postMessage({
  1770. onappend : true,
  1771. data : deflater.append(message.data, function(current) {
  1772. obj.postMessage({
  1773. progress : true,
  1774. current : current
  1775. });
  1776. })
  1777. });
  1778. if (message.flush)
  1779. obj.postMessage({
  1780. onflush : true,
  1781. data : deflater.flush()
  1782. });
  1783. }, false);
  1784. }
  1785. })(this);