inflate.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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 Z_OK = 0;
  55. var Z_STREAM_END = 1;
  56. var Z_NEED_DICT = 2;
  57. var Z_STREAM_ERROR = -2;
  58. var Z_DATA_ERROR = -3;
  59. var Z_MEM_ERROR = -4;
  60. var Z_BUF_ERROR = -5;
  61. var inflate_mask = [ 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff,
  62. 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff ];
  63. var MANY = 1440;
  64. // JZlib version : "1.0.2"
  65. var Z_NO_FLUSH = 0;
  66. var Z_FINISH = 4;
  67. // InfTree
  68. var fixed_bl = 9;
  69. var fixed_bd = 5;
  70. var fixed_tl = [ 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0,
  71. 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40,
  72. 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13,
  73. 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60,
  74. 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7,
  75. 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8,
  76. 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80,
  77. 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0,
  78. 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0,
  79. 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97,
  80. 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210,
  81. 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117,
  82. 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154,
  83. 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83,
  84. 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230,
  85. 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139,
  86. 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174,
  87. 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111,
  88. 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9,
  89. 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8,
  90. 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8,
  91. 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8,
  92. 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9,
  93. 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8,
  94. 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9,
  95. 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8,
  96. 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9,
  97. 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0,
  98. 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9,
  99. 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8,
  100. 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9,
  101. 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8,
  102. 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9,
  103. 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8,
  104. 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9,
  105. 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8,
  106. 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255 ];
  107. var fixed_td = [ 80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5,
  108. 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5,
  109. 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577 ];
  110. // Tables for deflate from PKZIP's appnote.txt.
  111. var cplens = [ // Copy lengths for literal codes 257..285
  112. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 ];
  113. // see note #13 above about 258
  114. var cplext = [ // Extra bits for literal codes 257..285
  115. 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, 112, 112 // 112==invalid
  116. ];
  117. var cpdist = [ // Copy offsets for distance codes 0..29
  118. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
  119. var cpdext = [ // Extra bits for distance codes
  120. 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 ];
  121. // If BMAX needs to be larger than 16, then h and x[] should be uLong.
  122. var BMAX = 15; // maximum bit length of any code
  123. function InfTree() {
  124. var that = this;
  125. var hn; // hufts used in space
  126. var v; // work area for huft_build
  127. var c; // bit length count table
  128. var r; // table entry for structure assignment
  129. var u; // table stack
  130. var x; // bit offsets, then code stack
  131. function huft_build(b, // code lengths in bits (all assumed <=
  132. // BMAX)
  133. bindex, n, // number of codes (assumed <= 288)
  134. s, // number of simple-valued codes (0..s-1)
  135. d, // list of base values for non-simple codes
  136. e, // list of extra bits for non-simple codes
  137. t, // result: starting table
  138. m, // maximum lookup bits, returns actual
  139. hp,// space for trees
  140. hn,// hufts used in space
  141. v // working area: values in order of bit length
  142. ) {
  143. // Given a list of code lengths and a maximum table size, make a set of
  144. // tables to decode that set of codes. Return Z_OK on success,
  145. // Z_BUF_ERROR
  146. // if the given code set is incomplete (the tables are still built in
  147. // this
  148. // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set
  149. // of
  150. // lengths), or Z_MEM_ERROR if not enough memory.
  151. var a; // counter for codes of length k
  152. var f; // i repeats in table every f entries
  153. var g; // maximum code length
  154. var h; // table level
  155. var i; // counter, current code
  156. var j; // counter
  157. var k; // number of bits in current code
  158. var l; // bits per table (returned in m)
  159. var mask; // (1 << w) - 1, to avoid cc -O bug on HP
  160. var p; // pointer into c[], b[], or v[]
  161. var q; // points to current table
  162. var w; // bits before this table == (l * h)
  163. var xp; // pointer into x
  164. var y; // number of dummy codes added
  165. var z; // number of entries in current table
  166. // Generate counts for each bit length
  167. p = 0;
  168. i = n;
  169. do {
  170. c[b[bindex + p]]++;
  171. p++;
  172. i--; // assume all entries <= BMAX
  173. } while (i !== 0);
  174. if (c[0] == n) { // null input--all zero length codes
  175. t[0] = -1;
  176. m[0] = 0;
  177. return Z_OK;
  178. }
  179. // Find minimum and maximum length, bound *m by those
  180. l = m[0];
  181. for (j = 1; j <= BMAX; j++)
  182. if (c[j] !== 0)
  183. break;
  184. k = j; // minimum code length
  185. if (l < j) {
  186. l = j;
  187. }
  188. for (i = BMAX; i !== 0; i--) {
  189. if (c[i] !== 0)
  190. break;
  191. }
  192. g = i; // maximum code length
  193. if (l > i) {
  194. l = i;
  195. }
  196. m[0] = l;
  197. // Adjust last length count to fill out codes, if needed
  198. for (y = 1 << j; j < i; j++, y <<= 1) {
  199. if ((y -= c[j]) < 0) {
  200. return Z_DATA_ERROR;
  201. }
  202. }
  203. if ((y -= c[i]) < 0) {
  204. return Z_DATA_ERROR;
  205. }
  206. c[i] += y;
  207. // Generate starting offsets into the value table for each length
  208. x[1] = j = 0;
  209. p = 1;
  210. xp = 2;
  211. while (--i !== 0) { // note that i == g from above
  212. x[xp] = (j += c[p]);
  213. xp++;
  214. p++;
  215. }
  216. // Make a table of values in order of bit lengths
  217. i = 0;
  218. p = 0;
  219. do {
  220. if ((j = b[bindex + p]) !== 0) {
  221. v[x[j]++] = i;
  222. }
  223. p++;
  224. } while (++i < n);
  225. n = x[g]; // set n to length of v
  226. // Generate the Huffman codes and for each, make the table entries
  227. x[0] = i = 0; // first Huffman code is zero
  228. p = 0; // grab values in bit order
  229. h = -1; // no tables yet--level -1
  230. w = -l; // bits decoded == (l * h)
  231. u[0] = 0; // just to keep compilers happy
  232. q = 0; // ditto
  233. z = 0; // ditto
  234. // go through the bit lengths (k already is bits in shortest code)
  235. for (; k <= g; k++) {
  236. a = c[k];
  237. while (a-- !== 0) {
  238. // here i is the Huffman code of length k bits for value *p
  239. // make tables up to required level
  240. while (k > w + l) {
  241. h++;
  242. w += l; // previous table always l bits
  243. // compute minimum size table less than or equal to l bits
  244. z = g - w;
  245. z = (z > l) ? l : z; // table size upper limit
  246. if ((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
  247. // too few codes for
  248. // k-w bit table
  249. f -= a + 1; // deduct codes from patterns left
  250. xp = k;
  251. if (j < z) {
  252. while (++j < z) { // try smaller tables up to z bits
  253. if ((f <<= 1) <= c[++xp])
  254. break; // enough codes to use up j bits
  255. f -= c[xp]; // else deduct codes from patterns
  256. }
  257. }
  258. }
  259. z = 1 << j; // table entries for j-bit table
  260. // allocate new table
  261. if (hn[0] + z > MANY) { // (note: doesn't matter for fixed)
  262. return Z_DATA_ERROR; // overflow of MANY
  263. }
  264. u[h] = q = /* hp+ */hn[0]; // DEBUG
  265. hn[0] += z;
  266. // connect to last table, if there is one
  267. if (h !== 0) {
  268. x[h] = i; // save pattern for backing up
  269. r[0] = /* (byte) */j; // bits in this table
  270. r[1] = /* (byte) */l; // bits to dump before this table
  271. j = i >>> (w - l);
  272. r[2] = /* (int) */(q - u[h - 1] - j); // offset to this table
  273. hp.set(r, (u[h - 1] + j) * 3);
  274. // to
  275. // last
  276. // table
  277. } else {
  278. t[0] = q; // first table is returned result
  279. }
  280. }
  281. // set up table entry in r
  282. r[1] = /* (byte) */(k - w);
  283. if (p >= n) {
  284. r[0] = 128 + 64; // out of values--invalid code
  285. } else if (v[p] < s) {
  286. r[0] = /* (byte) */(v[p] < 256 ? 0 : 32 + 64); // 256 is
  287. // end-of-block
  288. r[2] = v[p++]; // simple code is just the value
  289. } else {
  290. r[0] = /* (byte) */(e[v[p] - s] + 16 + 64); // non-simple--look
  291. // up in lists
  292. r[2] = d[v[p++] - s];
  293. }
  294. // fill code-like entries with r
  295. f = 1 << (k - w);
  296. for (j = i >>> w; j < z; j += f) {
  297. hp.set(r, (q + j) * 3);
  298. }
  299. // backwards increment the k-bit code i
  300. for (j = 1 << (k - 1); (i & j) !== 0; j >>>= 1) {
  301. i ^= j;
  302. }
  303. i ^= j;
  304. // backup over finished tables
  305. mask = (1 << w) - 1; // needed on HP, cc -O bug
  306. while ((i & mask) != x[h]) {
  307. h--; // don't need to update q
  308. w -= l;
  309. mask = (1 << w) - 1;
  310. }
  311. }
  312. }
  313. // Return Z_BUF_ERROR if we were given an incomplete table
  314. return y !== 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  315. }
  316. function initWorkArea(vsize) {
  317. var i;
  318. if (!hn) {
  319. hn = []; // []; //new Array(1);
  320. v = []; // new Array(vsize);
  321. c = new Int32Array(BMAX + 1); // new Array(BMAX + 1);
  322. r = []; // new Array(3);
  323. u = new Int32Array(BMAX); // new Array(BMAX);
  324. x = new Int32Array(BMAX + 1); // new Array(BMAX + 1);
  325. }
  326. if (v.length < vsize) {
  327. v = []; // new Array(vsize);
  328. }
  329. for (i = 0; i < vsize; i++) {
  330. v[i] = 0;
  331. }
  332. for (i = 0; i < BMAX + 1; i++) {
  333. c[i] = 0;
  334. }
  335. for (i = 0; i < 3; i++) {
  336. r[i] = 0;
  337. }
  338. // for(int i=0; i<BMAX; i++){u[i]=0;}
  339. u.set(c.subarray(0, BMAX), 0);
  340. // for(int i=0; i<BMAX+1; i++){x[i]=0;}
  341. x.set(c.subarray(0, BMAX + 1), 0);
  342. }
  343. that.inflate_trees_bits = function(c, // 19 code lengths
  344. bb, // bits tree desired/actual depth
  345. tb, // bits tree result
  346. hp, // space for trees
  347. z // for messages
  348. ) {
  349. var result;
  350. initWorkArea(19);
  351. hn[0] = 0;
  352. result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
  353. if (result == Z_DATA_ERROR) {
  354. z.msg = "oversubscribed dynamic bit lengths tree";
  355. } else if (result == Z_BUF_ERROR || bb[0] === 0) {
  356. z.msg = "incomplete dynamic bit lengths tree";
  357. result = Z_DATA_ERROR;
  358. }
  359. return result;
  360. };
  361. that.inflate_trees_dynamic = function(nl, // number of literal/length codes
  362. nd, // number of distance codes
  363. c, // that many (total) code lengths
  364. bl, // literal desired/actual bit depth
  365. bd, // distance desired/actual bit depth
  366. tl, // literal/length tree result
  367. td, // distance tree result
  368. hp, // space for trees
  369. z // for messages
  370. ) {
  371. var result;
  372. // build literal/length tree
  373. initWorkArea(288);
  374. hn[0] = 0;
  375. result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
  376. if (result != Z_OK || bl[0] === 0) {
  377. if (result == Z_DATA_ERROR) {
  378. z.msg = "oversubscribed literal/length tree";
  379. } else if (result != Z_MEM_ERROR) {
  380. z.msg = "incomplete literal/length tree";
  381. result = Z_DATA_ERROR;
  382. }
  383. return result;
  384. }
  385. // build distance tree
  386. initWorkArea(288);
  387. result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
  388. if (result != Z_OK || (bd[0] === 0 && nl > 257)) {
  389. if (result == Z_DATA_ERROR) {
  390. z.msg = "oversubscribed distance tree";
  391. } else if (result == Z_BUF_ERROR) {
  392. z.msg = "incomplete distance tree";
  393. result = Z_DATA_ERROR;
  394. } else if (result != Z_MEM_ERROR) {
  395. z.msg = "empty distance tree with lengths";
  396. result = Z_DATA_ERROR;
  397. }
  398. return result;
  399. }
  400. return Z_OK;
  401. };
  402. }
  403. InfTree.inflate_trees_fixed = function(bl, // literal desired/actual bit depth
  404. bd, // distance desired/actual bit depth
  405. tl,// literal/length tree result
  406. td// distance tree result
  407. ) {
  408. bl[0] = fixed_bl;
  409. bd[0] = fixed_bd;
  410. tl[0] = fixed_tl;
  411. td[0] = fixed_td;
  412. return Z_OK;
  413. };
  414. // InfCodes
  415. // waiting for "i:"=input,
  416. // "o:"=output,
  417. // "x:"=nothing
  418. var START = 0; // x: set up for LEN
  419. var LEN = 1; // i: get length/literal/eob next
  420. var LENEXT = 2; // i: getting length extra (have base)
  421. var DIST = 3; // i: get distance next
  422. var DISTEXT = 4;// i: getting distance extra
  423. var COPY = 5; // o: copying bytes in window, waiting
  424. // for space
  425. var LIT = 6; // o: got literal, waiting for output
  426. // space
  427. var WASH = 7; // o: got eob, possibly still output
  428. // waiting
  429. var END = 8; // x: got eob and all data flushed
  430. var BADCODE = 9;// x: got error
  431. function InfCodes() {
  432. var that = this;
  433. var mode; // current inflate_codes mode
  434. // mode dependent information
  435. var len = 0;
  436. var tree; // pointer into tree
  437. var tree_index = 0;
  438. var need = 0; // bits needed
  439. var lit = 0;
  440. // if EXT or COPY, where and how much
  441. var get = 0; // bits to get for extra
  442. var dist = 0; // distance back to copy from
  443. var lbits = 0; // ltree bits decoded per branch
  444. var dbits = 0; // dtree bits decoder per branch
  445. var ltree; // literal/length/eob tree
  446. var ltree_index = 0; // literal/length/eob tree
  447. var dtree; // distance tree
  448. var dtree_index = 0; // distance tree
  449. // Called with number of bytes left to write in window at least 258
  450. // (the maximum string length) and number of input bytes available
  451. // at least ten. The ten bytes are six bytes for the longest length/
  452. // distance pair plus four bytes for overloading the bit buffer.
  453. function inflate_fast(bl, bd, tl, tl_index, td, td_index, s, z) {
  454. var t; // temporary pointer
  455. var tp; // temporary pointer
  456. var tp_index; // temporary pointer
  457. var e; // extra bits or operation
  458. var b; // bit buffer
  459. var k; // bits in bit buffer
  460. var p; // input data pointer
  461. var n; // bytes available there
  462. var q; // output window write pointer
  463. var m; // bytes to end of window or read pointer
  464. var ml; // mask for literal/length tree
  465. var md; // mask for distance tree
  466. var c; // bytes to copy
  467. var d; // distance back to copy from
  468. var r; // copy source pointer
  469. var tp_index_t_3; // (tp_index+t)*3
  470. // load input, output, bit values
  471. p = z.next_in_index;
  472. n = z.avail_in;
  473. b = s.bitb;
  474. k = s.bitk;
  475. q = s.write;
  476. m = q < s.read ? s.read - q - 1 : s.end - q;
  477. // initialize masks
  478. ml = inflate_mask[bl];
  479. md = inflate_mask[bd];
  480. // do until not enough input or output space for fast loop
  481. do { // assume called with m >= 258 && n >= 10
  482. // get literal/length code
  483. while (k < (20)) { // max bits for literal/length code
  484. n--;
  485. b |= (z.read_byte(p++) & 0xff) << k;
  486. k += 8;
  487. }
  488. t = b & ml;
  489. tp = tl;
  490. tp_index = tl_index;
  491. tp_index_t_3 = (tp_index + t) * 3;
  492. if ((e = tp[tp_index_t_3]) === 0) {
  493. b >>= (tp[tp_index_t_3 + 1]);
  494. k -= (tp[tp_index_t_3 + 1]);
  495. s.window[q++] = /* (byte) */tp[tp_index_t_3 + 2];
  496. m--;
  497. continue;
  498. }
  499. do {
  500. b >>= (tp[tp_index_t_3 + 1]);
  501. k -= (tp[tp_index_t_3 + 1]);
  502. if ((e & 16) !== 0) {
  503. e &= 15;
  504. c = tp[tp_index_t_3 + 2] + (/* (int) */b & inflate_mask[e]);
  505. b >>= e;
  506. k -= e;
  507. // decode distance base of block to copy
  508. while (k < (15)) { // max bits for distance code
  509. n--;
  510. b |= (z.read_byte(p++) & 0xff) << k;
  511. k += 8;
  512. }
  513. t = b & md;
  514. tp = td;
  515. tp_index = td_index;
  516. tp_index_t_3 = (tp_index + t) * 3;
  517. e = tp[tp_index_t_3];
  518. do {
  519. b >>= (tp[tp_index_t_3 + 1]);
  520. k -= (tp[tp_index_t_3 + 1]);
  521. if ((e & 16) !== 0) {
  522. // get extra bits to add to distance base
  523. e &= 15;
  524. while (k < (e)) { // get extra bits (up to 13)
  525. n--;
  526. b |= (z.read_byte(p++) & 0xff) << k;
  527. k += 8;
  528. }
  529. d = tp[tp_index_t_3 + 2] + (b & inflate_mask[e]);
  530. b >>= (e);
  531. k -= (e);
  532. // do the copy
  533. m -= c;
  534. if (q >= d) { // offset before dest
  535. // just copy
  536. r = q - d;
  537. if (q - r > 0 && 2 > (q - r)) {
  538. s.window[q++] = s.window[r++]; // minimum
  539. // count is
  540. // three,
  541. s.window[q++] = s.window[r++]; // so unroll
  542. // loop a
  543. // little
  544. c -= 2;
  545. } else {
  546. s.window.set(s.window.subarray(r, r + 2), q);
  547. q += 2;
  548. r += 2;
  549. c -= 2;
  550. }
  551. } else { // else offset after destination
  552. r = q - d;
  553. do {
  554. r += s.end; // force pointer in window
  555. } while (r < 0); // covers invalid distances
  556. e = s.end - r;
  557. if (c > e) { // if source crosses,
  558. c -= e; // wrapped copy
  559. if (q - r > 0 && e > (q - r)) {
  560. do {
  561. s.window[q++] = s.window[r++];
  562. } while (--e !== 0);
  563. } else {
  564. s.window.set(s.window.subarray(r, r + e), q);
  565. q += e;
  566. r += e;
  567. e = 0;
  568. }
  569. r = 0; // copy rest from start of window
  570. }
  571. }
  572. // copy all or what's left
  573. if (q - r > 0 && c > (q - r)) {
  574. do {
  575. s.window[q++] = s.window[r++];
  576. } while (--c !== 0);
  577. } else {
  578. s.window.set(s.window.subarray(r, r + c), q);
  579. q += c;
  580. r += c;
  581. c = 0;
  582. }
  583. break;
  584. } else if ((e & 64) === 0) {
  585. t += tp[tp_index_t_3 + 2];
  586. t += (b & inflate_mask[e]);
  587. tp_index_t_3 = (tp_index + t) * 3;
  588. e = tp[tp_index_t_3];
  589. } else {
  590. z.msg = "invalid distance code";
  591. c = z.avail_in - n;
  592. c = (k >> 3) < c ? k >> 3 : c;
  593. n += c;
  594. p -= c;
  595. k -= c << 3;
  596. s.bitb = b;
  597. s.bitk = k;
  598. z.avail_in = n;
  599. z.total_in += p - z.next_in_index;
  600. z.next_in_index = p;
  601. s.write = q;
  602. return Z_DATA_ERROR;
  603. }
  604. } while (true);
  605. break;
  606. }
  607. if ((e & 64) === 0) {
  608. t += tp[tp_index_t_3 + 2];
  609. t += (b & inflate_mask[e]);
  610. tp_index_t_3 = (tp_index + t) * 3;
  611. if ((e = tp[tp_index_t_3]) === 0) {
  612. b >>= (tp[tp_index_t_3 + 1]);
  613. k -= (tp[tp_index_t_3 + 1]);
  614. s.window[q++] = /* (byte) */tp[tp_index_t_3 + 2];
  615. m--;
  616. break;
  617. }
  618. } else if ((e & 32) !== 0) {
  619. c = z.avail_in - n;
  620. c = (k >> 3) < c ? k >> 3 : c;
  621. n += c;
  622. p -= c;
  623. k -= c << 3;
  624. s.bitb = b;
  625. s.bitk = k;
  626. z.avail_in = n;
  627. z.total_in += p - z.next_in_index;
  628. z.next_in_index = p;
  629. s.write = q;
  630. return Z_STREAM_END;
  631. } else {
  632. z.msg = "invalid literal/length code";
  633. c = z.avail_in - n;
  634. c = (k >> 3) < c ? k >> 3 : c;
  635. n += c;
  636. p -= c;
  637. k -= c << 3;
  638. s.bitb = b;
  639. s.bitk = k;
  640. z.avail_in = n;
  641. z.total_in += p - z.next_in_index;
  642. z.next_in_index = p;
  643. s.write = q;
  644. return Z_DATA_ERROR;
  645. }
  646. } while (true);
  647. } while (m >= 258 && n >= 10);
  648. // not enough input or output--restore pointers and return
  649. c = z.avail_in - n;
  650. c = (k >> 3) < c ? k >> 3 : c;
  651. n += c;
  652. p -= c;
  653. k -= c << 3;
  654. s.bitb = b;
  655. s.bitk = k;
  656. z.avail_in = n;
  657. z.total_in += p - z.next_in_index;
  658. z.next_in_index = p;
  659. s.write = q;
  660. return Z_OK;
  661. }
  662. that.init = function(bl, bd, tl, tl_index, td, td_index) {
  663. mode = START;
  664. lbits = /* (byte) */bl;
  665. dbits = /* (byte) */bd;
  666. ltree = tl;
  667. ltree_index = tl_index;
  668. dtree = td;
  669. dtree_index = td_index;
  670. tree = null;
  671. };
  672. that.proc = function(s, z, r) {
  673. var j; // temporary storage
  674. var tindex; // temporary pointer
  675. var e; // extra bits or operation
  676. var b = 0; // bit buffer
  677. var k = 0; // bits in bit buffer
  678. var p = 0; // input data pointer
  679. var n; // bytes available there
  680. var q; // output window write pointer
  681. var m; // bytes to end of window or read pointer
  682. var f; // pointer to copy strings from
  683. // copy input/output information to locals (UPDATE macro restores)
  684. p = z.next_in_index;
  685. n = z.avail_in;
  686. b = s.bitb;
  687. k = s.bitk;
  688. q = s.write;
  689. m = q < s.read ? s.read - q - 1 : s.end - q;
  690. // process input and output based on current state
  691. while (true) {
  692. switch (mode) {
  693. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  694. case START: // x: set up for LEN
  695. if (m >= 258 && n >= 10) {
  696. s.bitb = b;
  697. s.bitk = k;
  698. z.avail_in = n;
  699. z.total_in += p - z.next_in_index;
  700. z.next_in_index = p;
  701. s.write = q;
  702. r = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z);
  703. p = z.next_in_index;
  704. n = z.avail_in;
  705. b = s.bitb;
  706. k = s.bitk;
  707. q = s.write;
  708. m = q < s.read ? s.read - q - 1 : s.end - q;
  709. if (r != Z_OK) {
  710. mode = r == Z_STREAM_END ? WASH : BADCODE;
  711. break;
  712. }
  713. }
  714. need = lbits;
  715. tree = ltree;
  716. tree_index = ltree_index;
  717. mode = LEN;
  718. case LEN: // i: get length/literal/eob next
  719. j = need;
  720. while (k < (j)) {
  721. if (n !== 0)
  722. r = Z_OK;
  723. else {
  724. s.bitb = b;
  725. s.bitk = k;
  726. z.avail_in = n;
  727. z.total_in += p - z.next_in_index;
  728. z.next_in_index = p;
  729. s.write = q;
  730. return s.inflate_flush(z, r);
  731. }
  732. n--;
  733. b |= (z.read_byte(p++) & 0xff) << k;
  734. k += 8;
  735. }
  736. tindex = (tree_index + (b & inflate_mask[j])) * 3;
  737. b >>>= (tree[tindex + 1]);
  738. k -= (tree[tindex + 1]);
  739. e = tree[tindex];
  740. if (e === 0) { // literal
  741. lit = tree[tindex + 2];
  742. mode = LIT;
  743. break;
  744. }
  745. if ((e & 16) !== 0) { // length
  746. get = e & 15;
  747. len = tree[tindex + 2];
  748. mode = LENEXT;
  749. break;
  750. }
  751. if ((e & 64) === 0) { // next table
  752. need = e;
  753. tree_index = tindex / 3 + tree[tindex + 2];
  754. break;
  755. }
  756. if ((e & 32) !== 0) { // end of block
  757. mode = WASH;
  758. break;
  759. }
  760. mode = BADCODE; // invalid code
  761. z.msg = "invalid literal/length code";
  762. r = Z_DATA_ERROR;
  763. s.bitb = b;
  764. s.bitk = k;
  765. z.avail_in = n;
  766. z.total_in += p - z.next_in_index;
  767. z.next_in_index = p;
  768. s.write = q;
  769. return s.inflate_flush(z, r);
  770. case LENEXT: // i: getting length extra (have base)
  771. j = get;
  772. while (k < (j)) {
  773. if (n !== 0)
  774. r = Z_OK;
  775. else {
  776. s.bitb = b;
  777. s.bitk = k;
  778. z.avail_in = n;
  779. z.total_in += p - z.next_in_index;
  780. z.next_in_index = p;
  781. s.write = q;
  782. return s.inflate_flush(z, r);
  783. }
  784. n--;
  785. b |= (z.read_byte(p++) & 0xff) << k;
  786. k += 8;
  787. }
  788. len += (b & inflate_mask[j]);
  789. b >>= j;
  790. k -= j;
  791. need = dbits;
  792. tree = dtree;
  793. tree_index = dtree_index;
  794. mode = DIST;
  795. case DIST: // i: get distance next
  796. j = need;
  797. while (k < (j)) {
  798. if (n !== 0)
  799. r = Z_OK;
  800. else {
  801. s.bitb = b;
  802. s.bitk = k;
  803. z.avail_in = n;
  804. z.total_in += p - z.next_in_index;
  805. z.next_in_index = p;
  806. s.write = q;
  807. return s.inflate_flush(z, r);
  808. }
  809. n--;
  810. b |= (z.read_byte(p++) & 0xff) << k;
  811. k += 8;
  812. }
  813. tindex = (tree_index + (b & inflate_mask[j])) * 3;
  814. b >>= tree[tindex + 1];
  815. k -= tree[tindex + 1];
  816. e = (tree[tindex]);
  817. if ((e & 16) !== 0) { // distance
  818. get = e & 15;
  819. dist = tree[tindex + 2];
  820. mode = DISTEXT;
  821. break;
  822. }
  823. if ((e & 64) === 0) { // next table
  824. need = e;
  825. tree_index = tindex / 3 + tree[tindex + 2];
  826. break;
  827. }
  828. mode = BADCODE; // invalid code
  829. z.msg = "invalid distance code";
  830. r = Z_DATA_ERROR;
  831. s.bitb = b;
  832. s.bitk = k;
  833. z.avail_in = n;
  834. z.total_in += p - z.next_in_index;
  835. z.next_in_index = p;
  836. s.write = q;
  837. return s.inflate_flush(z, r);
  838. case DISTEXT: // i: getting distance extra
  839. j = get;
  840. while (k < (j)) {
  841. if (n !== 0)
  842. r = Z_OK;
  843. else {
  844. s.bitb = b;
  845. s.bitk = k;
  846. z.avail_in = n;
  847. z.total_in += p - z.next_in_index;
  848. z.next_in_index = p;
  849. s.write = q;
  850. return s.inflate_flush(z, r);
  851. }
  852. n--;
  853. b |= (z.read_byte(p++) & 0xff) << k;
  854. k += 8;
  855. }
  856. dist += (b & inflate_mask[j]);
  857. b >>= j;
  858. k -= j;
  859. mode = COPY;
  860. case COPY: // o: copying bytes in window, waiting for space
  861. f = q - dist;
  862. while (f < 0) { // modulo window size-"while" instead
  863. f += s.end; // of "if" handles invalid distances
  864. }
  865. while (len !== 0) {
  866. if (m === 0) {
  867. if (q == s.end && s.read !== 0) {
  868. q = 0;
  869. m = q < s.read ? s.read - q - 1 : s.end - q;
  870. }
  871. if (m === 0) {
  872. s.write = q;
  873. r = s.inflate_flush(z, r);
  874. q = s.write;
  875. m = q < s.read ? s.read - q - 1 : s.end - q;
  876. if (q == s.end && s.read !== 0) {
  877. q = 0;
  878. m = q < s.read ? s.read - q - 1 : s.end - q;
  879. }
  880. if (m === 0) {
  881. s.bitb = b;
  882. s.bitk = k;
  883. z.avail_in = n;
  884. z.total_in += p - z.next_in_index;
  885. z.next_in_index = p;
  886. s.write = q;
  887. return s.inflate_flush(z, r);
  888. }
  889. }
  890. }
  891. s.window[q++] = s.window[f++];
  892. m--;
  893. if (f == s.end)
  894. f = 0;
  895. len--;
  896. }
  897. mode = START;
  898. break;
  899. case LIT: // o: got literal, waiting for output space
  900. if (m === 0) {
  901. if (q == s.end && s.read !== 0) {
  902. q = 0;
  903. m = q < s.read ? s.read - q - 1 : s.end - q;
  904. }
  905. if (m === 0) {
  906. s.write = q;
  907. r = s.inflate_flush(z, r);
  908. q = s.write;
  909. m = q < s.read ? s.read - q - 1 : s.end - q;
  910. if (q == s.end && s.read !== 0) {
  911. q = 0;
  912. m = q < s.read ? s.read - q - 1 : s.end - q;
  913. }
  914. if (m === 0) {
  915. s.bitb = b;
  916. s.bitk = k;
  917. z.avail_in = n;
  918. z.total_in += p - z.next_in_index;
  919. z.next_in_index = p;
  920. s.write = q;
  921. return s.inflate_flush(z, r);
  922. }
  923. }
  924. }
  925. r = Z_OK;
  926. s.window[q++] = /* (byte) */lit;
  927. m--;
  928. mode = START;
  929. break;
  930. case WASH: // o: got eob, possibly more output
  931. if (k > 7) { // return unused byte, if any
  932. k -= 8;
  933. n++;
  934. p--; // can always return one
  935. }
  936. s.write = q;
  937. r = s.inflate_flush(z, r);
  938. q = s.write;
  939. m = q < s.read ? s.read - q - 1 : s.end - q;
  940. if (s.read != s.write) {
  941. s.bitb = b;
  942. s.bitk = k;
  943. z.avail_in = n;
  944. z.total_in += p - z.next_in_index;
  945. z.next_in_index = p;
  946. s.write = q;
  947. return s.inflate_flush(z, r);
  948. }
  949. mode = END;
  950. case END:
  951. r = Z_STREAM_END;
  952. s.bitb = b;
  953. s.bitk = k;
  954. z.avail_in = n;
  955. z.total_in += p - z.next_in_index;
  956. z.next_in_index = p;
  957. s.write = q;
  958. return s.inflate_flush(z, r);
  959. case BADCODE: // x: got error
  960. r = Z_DATA_ERROR;
  961. s.bitb = b;
  962. s.bitk = k;
  963. z.avail_in = n;
  964. z.total_in += p - z.next_in_index;
  965. z.next_in_index = p;
  966. s.write = q;
  967. return s.inflate_flush(z, r);
  968. default:
  969. r = Z_STREAM_ERROR;
  970. s.bitb = b;
  971. s.bitk = k;
  972. z.avail_in = n;
  973. z.total_in += p - z.next_in_index;
  974. z.next_in_index = p;
  975. s.write = q;
  976. return s.inflate_flush(z, r);
  977. }
  978. }
  979. };
  980. that.free = function() {
  981. // ZFREE(z, c);
  982. };
  983. }
  984. // InfBlocks
  985. // Table for deflate from PKZIP's appnote.txt.
  986. var border = [ // Order of the bit length code lengths
  987. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  988. var TYPE = 0; // get type bits (3, including end bit)
  989. var LENS = 1; // get lengths for stored
  990. var STORED = 2;// processing stored block
  991. var TABLE = 3; // get table lengths
  992. var BTREE = 4; // get bit lengths tree for a dynamic
  993. // block
  994. var DTREE = 5; // get length, distance trees for a
  995. // dynamic block
  996. var CODES = 6; // processing fixed or dynamic block
  997. var DRY = 7; // output remaining window bytes
  998. var DONELOCKS = 8; // finished last block, done
  999. var BADBLOCKS = 9; // ot a data error--stuck here
  1000. function InfBlocks(z, w) {
  1001. var that = this;
  1002. var mode = TYPE; // current inflate_block mode
  1003. var left = 0; // if STORED, bytes left to copy
  1004. var table = 0; // table lengths (14 bits)
  1005. var index = 0; // index into blens (or border)
  1006. var blens; // bit lengths of codes
  1007. var bb = [ 0 ]; // bit length tree depth
  1008. var tb = [ 0 ]; // bit length decoding tree
  1009. var codes = new InfCodes(); // if CODES, current state
  1010. var last = 0; // true if this block is the last block
  1011. var hufts = new Int32Array(MANY * 3); // single malloc for tree space
  1012. var check = 0; // check on output
  1013. var inftree = new InfTree();
  1014. that.bitk = 0; // bits in bit buffer
  1015. that.bitb = 0; // bit buffer
  1016. that.window = new Uint8Array(w); // sliding window
  1017. that.end = w; // one byte after sliding window
  1018. that.read = 0; // window read pointer
  1019. that.write = 0; // window write pointer
  1020. that.reset = function(z, c) {
  1021. if (c)
  1022. c[0] = check;
  1023. // if (mode == BTREE || mode == DTREE) {
  1024. // }
  1025. if (mode == CODES) {
  1026. codes.free(z);
  1027. }
  1028. mode = TYPE;
  1029. that.bitk = 0;
  1030. that.bitb = 0;
  1031. that.read = that.write = 0;
  1032. };
  1033. that.reset(z, null);
  1034. // copy as much as possible from the sliding window to the output area
  1035. that.inflate_flush = function(z, r) {
  1036. var n;
  1037. var p;
  1038. var q;
  1039. // local copies of source and destination pointers
  1040. p = z.next_out_index;
  1041. q = that.read;
  1042. // compute number of bytes to copy as far as end of window
  1043. n = /* (int) */((q <= that.write ? that.write : that.end) - q);
  1044. if (n > z.avail_out)
  1045. n = z.avail_out;
  1046. if (n !== 0 && r == Z_BUF_ERROR)
  1047. r = Z_OK;
  1048. // update counters
  1049. z.avail_out -= n;
  1050. z.total_out += n;
  1051. // copy as far as end of window
  1052. z.next_out.set(that.window.subarray(q, q + n), p);
  1053. p += n;
  1054. q += n;
  1055. // see if more to copy at beginning of window
  1056. if (q == that.end) {
  1057. // wrap pointers
  1058. q = 0;
  1059. if (that.write == that.end)
  1060. that.write = 0;
  1061. // compute bytes to copy
  1062. n = that.write - q;
  1063. if (n > z.avail_out)
  1064. n = z.avail_out;
  1065. if (n !== 0 && r == Z_BUF_ERROR)
  1066. r = Z_OK;
  1067. // update counters
  1068. z.avail_out -= n;
  1069. z.total_out += n;
  1070. // copy
  1071. z.next_out.set(that.window.subarray(q, q + n), p);
  1072. p += n;
  1073. q += n;
  1074. }
  1075. // update pointers
  1076. z.next_out_index = p;
  1077. that.read = q;
  1078. // done
  1079. return r;
  1080. };
  1081. that.proc = function(z, r) {
  1082. var t; // temporary storage
  1083. var b; // bit buffer
  1084. var k; // bits in bit buffer
  1085. var p; // input data pointer
  1086. var n; // bytes available there
  1087. var q; // output window write pointer
  1088. var m; // bytes to end of window or read pointer
  1089. var i;
  1090. // copy input/output information to locals (UPDATE macro restores)
  1091. // {
  1092. p = z.next_in_index;
  1093. n = z.avail_in;
  1094. b = that.bitb;
  1095. k = that.bitk;
  1096. // }
  1097. // {
  1098. q = that.write;
  1099. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1100. // }
  1101. // process input based on current state
  1102. // DEBUG dtree
  1103. while (true) {
  1104. switch (mode) {
  1105. case TYPE:
  1106. while (k < (3)) {
  1107. if (n !== 0) {
  1108. r = Z_OK;
  1109. } else {
  1110. that.bitb = b;
  1111. that.bitk = k;
  1112. z.avail_in = n;
  1113. z.total_in += p - z.next_in_index;
  1114. z.next_in_index = p;
  1115. that.write = q;
  1116. return that.inflate_flush(z, r);
  1117. }
  1118. n--;
  1119. b |= (z.read_byte(p++) & 0xff) << k;
  1120. k += 8;
  1121. }
  1122. t = /* (int) */(b & 7);
  1123. last = t & 1;
  1124. switch (t >>> 1) {
  1125. case 0: // stored
  1126. // {
  1127. b >>>= (3);
  1128. k -= (3);
  1129. // }
  1130. t = k & 7; // go to byte boundary
  1131. // {
  1132. b >>>= (t);
  1133. k -= (t);
  1134. // }
  1135. mode = LENS; // get length of stored block
  1136. break;
  1137. case 1: // fixed
  1138. // {
  1139. var bl = []; // new Array(1);
  1140. var bd = []; // new Array(1);
  1141. var tl = [ [] ]; // new Array(1);
  1142. var td = [ [] ]; // new Array(1);
  1143. InfTree.inflate_trees_fixed(bl, bd, tl, td);
  1144. codes.init(bl[0], bd[0], tl[0], 0, td[0], 0);
  1145. // }
  1146. // {
  1147. b >>>= (3);
  1148. k -= (3);
  1149. // }
  1150. mode = CODES;
  1151. break;
  1152. case 2: // dynamic
  1153. // {
  1154. b >>>= (3);
  1155. k -= (3);
  1156. // }
  1157. mode = TABLE;
  1158. break;
  1159. case 3: // illegal
  1160. // {
  1161. b >>>= (3);
  1162. k -= (3);
  1163. // }
  1164. mode = BADBLOCKS;
  1165. z.msg = "invalid block type";
  1166. r = Z_DATA_ERROR;
  1167. that.bitb = b;
  1168. that.bitk = k;
  1169. z.avail_in = n;
  1170. z.total_in += p - z.next_in_index;
  1171. z.next_in_index = p;
  1172. that.write = q;
  1173. return that.inflate_flush(z, r);
  1174. }
  1175. break;
  1176. case LENS:
  1177. while (k < (32)) {
  1178. if (n !== 0) {
  1179. r = Z_OK;
  1180. } else {
  1181. that.bitb = b;
  1182. that.bitk = k;
  1183. z.avail_in = n;
  1184. z.total_in += p - z.next_in_index;
  1185. z.next_in_index = p;
  1186. that.write = q;
  1187. return that.inflate_flush(z, r);
  1188. }
  1189. n--;
  1190. b |= (z.read_byte(p++) & 0xff) << k;
  1191. k += 8;
  1192. }
  1193. if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)) {
  1194. mode = BADBLOCKS;
  1195. z.msg = "invalid stored block lengths";
  1196. r = Z_DATA_ERROR;
  1197. that.bitb = b;
  1198. that.bitk = k;
  1199. z.avail_in = n;
  1200. z.total_in += p - z.next_in_index;
  1201. z.next_in_index = p;
  1202. that.write = q;
  1203. return that.inflate_flush(z, r);
  1204. }
  1205. left = (b & 0xffff);
  1206. b = k = 0; // dump bits
  1207. mode = left !== 0 ? STORED : (last !== 0 ? DRY : TYPE);
  1208. break;
  1209. case STORED:
  1210. if (n === 0) {
  1211. that.bitb = b;
  1212. that.bitk = k;
  1213. z.avail_in = n;
  1214. z.total_in += p - z.next_in_index;
  1215. z.next_in_index = p;
  1216. that.write = q;
  1217. return that.inflate_flush(z, r);
  1218. }
  1219. if (m === 0) {
  1220. if (q == that.end && that.read !== 0) {
  1221. q = 0;
  1222. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1223. }
  1224. if (m === 0) {
  1225. that.write = q;
  1226. r = that.inflate_flush(z, r);
  1227. q = that.write;
  1228. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1229. if (q == that.end && that.read !== 0) {
  1230. q = 0;
  1231. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1232. }
  1233. if (m === 0) {
  1234. that.bitb = b;
  1235. that.bitk = k;
  1236. z.avail_in = n;
  1237. z.total_in += p - z.next_in_index;
  1238. z.next_in_index = p;
  1239. that.write = q;
  1240. return that.inflate_flush(z, r);
  1241. }
  1242. }
  1243. }
  1244. r = Z_OK;
  1245. t = left;
  1246. if (t > n)
  1247. t = n;
  1248. if (t > m)
  1249. t = m;
  1250. that.window.set(z.read_buf(p, t), q);
  1251. p += t;
  1252. n -= t;
  1253. q += t;
  1254. m -= t;
  1255. if ((left -= t) !== 0)
  1256. break;
  1257. mode = last !== 0 ? DRY : TYPE;
  1258. break;
  1259. case TABLE:
  1260. while (k < (14)) {
  1261. if (n !== 0) {
  1262. r = Z_OK;
  1263. } else {
  1264. that.bitb = b;
  1265. that.bitk = k;
  1266. z.avail_in = n;
  1267. z.total_in += p - z.next_in_index;
  1268. z.next_in_index = p;
  1269. that.write = q;
  1270. return that.inflate_flush(z, r);
  1271. }
  1272. n--;
  1273. b |= (z.read_byte(p++) & 0xff) << k;
  1274. k += 8;
  1275. }
  1276. table = t = (b & 0x3fff);
  1277. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
  1278. mode = BADBLOCKS;
  1279. z.msg = "too many length or distance symbols";
  1280. r = Z_DATA_ERROR;
  1281. that.bitb = b;
  1282. that.bitk = k;
  1283. z.avail_in = n;
  1284. z.total_in += p - z.next_in_index;
  1285. z.next_in_index = p;
  1286. that.write = q;
  1287. return that.inflate_flush(z, r);
  1288. }
  1289. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  1290. if (!blens || blens.length < t) {
  1291. blens = []; // new Array(t);
  1292. } else {
  1293. for (i = 0; i < t; i++) {
  1294. blens[i] = 0;
  1295. }
  1296. }
  1297. // {
  1298. b >>>= (14);
  1299. k -= (14);
  1300. // }
  1301. index = 0;
  1302. mode = BTREE;
  1303. case BTREE:
  1304. while (index < 4 + (table >>> 10)) {
  1305. while (k < (3)) {
  1306. if (n !== 0) {
  1307. r = Z_OK;
  1308. } else {
  1309. that.bitb = b;
  1310. that.bitk = k;
  1311. z.avail_in = n;
  1312. z.total_in += p - z.next_in_index;
  1313. z.next_in_index = p;
  1314. that.write = q;
  1315. return that.inflate_flush(z, r);
  1316. }
  1317. n--;
  1318. b |= (z.read_byte(p++) & 0xff) << k;
  1319. k += 8;
  1320. }
  1321. blens[border[index++]] = b & 7;
  1322. // {
  1323. b >>>= (3);
  1324. k -= (3);
  1325. // }
  1326. }
  1327. while (index < 19) {
  1328. blens[border[index++]] = 0;
  1329. }
  1330. bb[0] = 7;
  1331. t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
  1332. if (t != Z_OK) {
  1333. r = t;
  1334. if (r == Z_DATA_ERROR) {
  1335. blens = null;
  1336. mode = BADBLOCKS;
  1337. }
  1338. that.bitb = b;
  1339. that.bitk = k;
  1340. z.avail_in = n;
  1341. z.total_in += p - z.next_in_index;
  1342. z.next_in_index = p;
  1343. that.write = q;
  1344. return that.inflate_flush(z, r);
  1345. }
  1346. index = 0;
  1347. mode = DTREE;
  1348. case DTREE:
  1349. while (true) {
  1350. t = table;
  1351. if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))) {
  1352. break;
  1353. }
  1354. var j, c;
  1355. t = bb[0];
  1356. while (k < (t)) {
  1357. if (n !== 0) {
  1358. r = Z_OK;
  1359. } else {
  1360. that.bitb = b;
  1361. that.bitk = k;
  1362. z.avail_in = n;
  1363. z.total_in += p - z.next_in_index;
  1364. z.next_in_index = p;
  1365. that.write = q;
  1366. return that.inflate_flush(z, r);
  1367. }
  1368. n--;
  1369. b |= (z.read_byte(p++) & 0xff) << k;
  1370. k += 8;
  1371. }
  1372. // if (tb[0] == -1) {
  1373. // System.err.println("null...");
  1374. // }
  1375. t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
  1376. c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];
  1377. if (c < 16) {
  1378. b >>>= (t);
  1379. k -= (t);
  1380. blens[index++] = c;
  1381. } else { // c == 16..18
  1382. i = c == 18 ? 7 : c - 14;
  1383. j = c == 18 ? 11 : 3;
  1384. while (k < (t + i)) {
  1385. if (n !== 0) {
  1386. r = Z_OK;
  1387. } else {
  1388. that.bitb = b;
  1389. that.bitk = k;
  1390. z.avail_in = n;
  1391. z.total_in += p - z.next_in_index;
  1392. z.next_in_index = p;
  1393. that.write = q;
  1394. return that.inflate_flush(z, r);
  1395. }
  1396. n--;
  1397. b |= (z.read_byte(p++) & 0xff) << k;
  1398. k += 8;
  1399. }
  1400. b >>>= (t);
  1401. k -= (t);
  1402. j += (b & inflate_mask[i]);
  1403. b >>>= (i);
  1404. k -= (i);
  1405. i = index;
  1406. t = table;
  1407. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1)) {
  1408. blens = null;
  1409. mode = BADBLOCKS;
  1410. z.msg = "invalid bit length repeat";
  1411. r = Z_DATA_ERROR;
  1412. that.bitb = b;
  1413. that.bitk = k;
  1414. z.avail_in = n;
  1415. z.total_in += p - z.next_in_index;
  1416. z.next_in_index = p;
  1417. that.write = q;
  1418. return that.inflate_flush(z, r);
  1419. }
  1420. c = c == 16 ? blens[i - 1] : 0;
  1421. do {
  1422. blens[i++] = c;
  1423. } while (--j !== 0);
  1424. index = i;
  1425. }
  1426. }
  1427. tb[0] = -1;
  1428. // {
  1429. var bl_ = []; // new Array(1);
  1430. var bd_ = []; // new Array(1);
  1431. var tl_ = []; // new Array(1);
  1432. var td_ = []; // new Array(1);
  1433. bl_[0] = 9; // must be <= 9 for lookahead assumptions
  1434. bd_[0] = 6; // must be <= 9 for lookahead assumptions
  1435. t = table;
  1436. t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl_, bd_, tl_, td_, hufts, z);
  1437. if (t != Z_OK) {
  1438. if (t == Z_DATA_ERROR) {
  1439. blens = null;
  1440. mode = BADBLOCKS;
  1441. }
  1442. r = t;
  1443. that.bitb = b;
  1444. that.bitk = k;
  1445. z.avail_in = n;
  1446. z.total_in += p - z.next_in_index;
  1447. z.next_in_index = p;
  1448. that.write = q;
  1449. return that.inflate_flush(z, r);
  1450. }
  1451. codes.init(bl_[0], bd_[0], hufts, tl_[0], hufts, td_[0]);
  1452. // }
  1453. mode = CODES;
  1454. case CODES:
  1455. that.bitb = b;
  1456. that.bitk = k;
  1457. z.avail_in = n;
  1458. z.total_in += p - z.next_in_index;
  1459. z.next_in_index = p;
  1460. that.write = q;
  1461. if ((r = codes.proc(that, z, r)) != Z_STREAM_END) {
  1462. return that.inflate_flush(z, r);
  1463. }
  1464. r = Z_OK;
  1465. codes.free(z);
  1466. p = z.next_in_index;
  1467. n = z.avail_in;
  1468. b = that.bitb;
  1469. k = that.bitk;
  1470. q = that.write;
  1471. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1472. if (last === 0) {
  1473. mode = TYPE;
  1474. break;
  1475. }
  1476. mode = DRY;
  1477. case DRY:
  1478. that.write = q;
  1479. r = that.inflate_flush(z, r);
  1480. q = that.write;
  1481. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1482. if (that.read != that.write) {
  1483. that.bitb = b;
  1484. that.bitk = k;
  1485. z.avail_in = n;
  1486. z.total_in += p - z.next_in_index;
  1487. z.next_in_index = p;
  1488. that.write = q;
  1489. return that.inflate_flush(z, r);
  1490. }
  1491. mode = DONELOCKS;
  1492. case DONELOCKS:
  1493. r = Z_STREAM_END;
  1494. that.bitb = b;
  1495. that.bitk = k;
  1496. z.avail_in = n;
  1497. z.total_in += p - z.next_in_index;
  1498. z.next_in_index = p;
  1499. that.write = q;
  1500. return that.inflate_flush(z, r);
  1501. case BADBLOCKS:
  1502. r = Z_DATA_ERROR;
  1503. that.bitb = b;
  1504. that.bitk = k;
  1505. z.avail_in = n;
  1506. z.total_in += p - z.next_in_index;
  1507. z.next_in_index = p;
  1508. that.write = q;
  1509. return that.inflate_flush(z, r);
  1510. default:
  1511. r = Z_STREAM_ERROR;
  1512. that.bitb = b;
  1513. that.bitk = k;
  1514. z.avail_in = n;
  1515. z.total_in += p - z.next_in_index;
  1516. z.next_in_index = p;
  1517. that.write = q;
  1518. return that.inflate_flush(z, r);
  1519. }
  1520. }
  1521. };
  1522. that.free = function(z) {
  1523. that.reset(z, null);
  1524. that.window = null;
  1525. hufts = null;
  1526. // ZFREE(z, s);
  1527. };
  1528. that.set_dictionary = function(d, start, n) {
  1529. that.window.set(d.subarray(start, start + n), 0);
  1530. that.read = that.write = n;
  1531. };
  1532. // Returns true if inflate is currently at the end of a block generated
  1533. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  1534. that.sync_point = function() {
  1535. return mode == LENS ? 1 : 0;
  1536. };
  1537. }
  1538. // Inflate
  1539. // preset dictionary flag in zlib header
  1540. var PRESET_DICT = 0x20;
  1541. var Z_DEFLATED = 8;
  1542. var METHOD = 0; // waiting for method byte
  1543. var FLAG = 1; // waiting for flag byte
  1544. var DICT4 = 2; // four dictionary check bytes to go
  1545. var DICT3 = 3; // three dictionary check bytes to go
  1546. var DICT2 = 4; // two dictionary check bytes to go
  1547. var DICT1 = 5; // one dictionary check byte to go
  1548. var DICT0 = 6; // waiting for inflateSetDictionary
  1549. var BLOCKS = 7; // decompressing blocks
  1550. var DONE = 12; // finished check, done
  1551. var BAD = 13; // got an error--stay here
  1552. var mark = [ 0, 0, 0xff, 0xff ];
  1553. function Inflate() {
  1554. var that = this;
  1555. that.mode = 0; // current inflate mode
  1556. // mode dependent information
  1557. that.method = 0; // if FLAGS, method byte
  1558. // if CHECK, check values to compare
  1559. that.was = [ 0 ]; // new Array(1); // computed check value
  1560. that.need = 0; // stream check value
  1561. // if BAD, inflateSync's marker bytes count
  1562. that.marker = 0;
  1563. // mode independent information
  1564. that.wbits = 0; // log2(window size) (8..15, defaults to 15)
  1565. // this.blocks; // current inflate_blocks state
  1566. function inflateReset(z) {
  1567. if (!z || !z.istate)
  1568. return Z_STREAM_ERROR;
  1569. z.total_in = z.total_out = 0;
  1570. z.msg = null;
  1571. z.istate.mode = BLOCKS;
  1572. z.istate.blocks.reset(z, null);
  1573. return Z_OK;
  1574. }
  1575. that.inflateEnd = function(z) {
  1576. if (that.blocks)
  1577. that.blocks.free(z);
  1578. that.blocks = null;
  1579. // ZFREE(z, z->state);
  1580. return Z_OK;
  1581. };
  1582. that.inflateInit = function(z, w) {
  1583. z.msg = null;
  1584. that.blocks = null;
  1585. // set window size
  1586. if (w < 8 || w > 15) {
  1587. that.inflateEnd(z);
  1588. return Z_STREAM_ERROR;
  1589. }
  1590. that.wbits = w;
  1591. z.istate.blocks = new InfBlocks(z, 1 << w);
  1592. // reset state
  1593. inflateReset(z);
  1594. return Z_OK;
  1595. };
  1596. that.inflate = function(z, f) {
  1597. var r;
  1598. var b;
  1599. if (!z || !z.istate || !z.next_in)
  1600. return Z_STREAM_ERROR;
  1601. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  1602. r = Z_BUF_ERROR;
  1603. while (true) {
  1604. // System.out.println("mode: "+z.istate.mode);
  1605. switch (z.istate.mode) {
  1606. case METHOD:
  1607. if (z.avail_in === 0)
  1608. return r;
  1609. r = f;
  1610. z.avail_in--;
  1611. z.total_in++;
  1612. if (((z.istate.method = z.read_byte(z.next_in_index++)) & 0xf) != Z_DEFLATED) {
  1613. z.istate.mode = BAD;
  1614. z.msg = "unknown compression method";
  1615. z.istate.marker = 5; // can't try inflateSync
  1616. break;
  1617. }
  1618. if ((z.istate.method >> 4) + 8 > z.istate.wbits) {
  1619. z.istate.mode = BAD;
  1620. z.msg = "invalid window size";
  1621. z.istate.marker = 5; // can't try inflateSync
  1622. break;
  1623. }
  1624. z.istate.mode = FLAG;
  1625. case FLAG:
  1626. if (z.avail_in === 0)
  1627. return r;
  1628. r = f;
  1629. z.avail_in--;
  1630. z.total_in++;
  1631. b = (z.read_byte(z.next_in_index++)) & 0xff;
  1632. if ((((z.istate.method << 8) + b) % 31) !== 0) {
  1633. z.istate.mode = BAD;
  1634. z.msg = "incorrect header check";
  1635. z.istate.marker = 5; // can't try inflateSync
  1636. break;
  1637. }
  1638. if ((b & PRESET_DICT) === 0) {
  1639. z.istate.mode = BLOCKS;
  1640. break;
  1641. }
  1642. z.istate.mode = DICT4;
  1643. case DICT4:
  1644. if (z.avail_in === 0)
  1645. return r;
  1646. r = f;
  1647. z.avail_in--;
  1648. z.total_in++;
  1649. z.istate.need = ((z.read_byte(z.next_in_index++) & 0xff) << 24) & 0xff000000;
  1650. z.istate.mode = DICT3;
  1651. case DICT3:
  1652. if (z.avail_in === 0)
  1653. return r;
  1654. r = f;
  1655. z.avail_in--;
  1656. z.total_in++;
  1657. z.istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 16) & 0xff0000;
  1658. z.istate.mode = DICT2;
  1659. case DICT2:
  1660. if (z.avail_in === 0)
  1661. return r;
  1662. r = f;
  1663. z.avail_in--;
  1664. z.total_in++;
  1665. z.istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 8) & 0xff00;
  1666. z.istate.mode = DICT1;
  1667. case DICT1:
  1668. if (z.avail_in === 0)
  1669. return r;
  1670. r = f;
  1671. z.avail_in--;
  1672. z.total_in++;
  1673. z.istate.need += (z.read_byte(z.next_in_index++) & 0xff);
  1674. z.istate.mode = DICT0;
  1675. return Z_NEED_DICT;
  1676. case DICT0:
  1677. z.istate.mode = BAD;
  1678. z.msg = "need dictionary";
  1679. z.istate.marker = 0; // can try inflateSync
  1680. return Z_STREAM_ERROR;
  1681. case BLOCKS:
  1682. r = z.istate.blocks.proc(z, r);
  1683. if (r == Z_DATA_ERROR) {
  1684. z.istate.mode = BAD;
  1685. z.istate.marker = 0; // can try inflateSync
  1686. break;
  1687. }
  1688. if (r == Z_OK) {
  1689. r = f;
  1690. }
  1691. if (r != Z_STREAM_END) {
  1692. return r;
  1693. }
  1694. r = f;
  1695. z.istate.blocks.reset(z, z.istate.was);
  1696. z.istate.mode = DONE;
  1697. case DONE:
  1698. return Z_STREAM_END;
  1699. case BAD:
  1700. return Z_DATA_ERROR;
  1701. default:
  1702. return Z_STREAM_ERROR;
  1703. }
  1704. }
  1705. };
  1706. that.inflateSetDictionary = function(z, dictionary, dictLength) {
  1707. var index = 0;
  1708. var length = dictLength;
  1709. if (!z || !z.istate || z.istate.mode != DICT0)
  1710. return Z_STREAM_ERROR;
  1711. if (length >= (1 << z.istate.wbits)) {
  1712. length = (1 << z.istate.wbits) - 1;
  1713. index = dictLength - length;
  1714. }
  1715. z.istate.blocks.set_dictionary(dictionary, index, length);
  1716. z.istate.mode = BLOCKS;
  1717. return Z_OK;
  1718. };
  1719. that.inflateSync = function(z) {
  1720. var n; // number of bytes to look at
  1721. var p; // pointer to bytes
  1722. var m; // number of marker bytes found in a row
  1723. var r, w; // temporaries to save total_in and total_out
  1724. // set up
  1725. if (!z || !z.istate)
  1726. return Z_STREAM_ERROR;
  1727. if (z.istate.mode != BAD) {
  1728. z.istate.mode = BAD;
  1729. z.istate.marker = 0;
  1730. }
  1731. if ((n = z.avail_in) === 0)
  1732. return Z_BUF_ERROR;
  1733. p = z.next_in_index;
  1734. m = z.istate.marker;
  1735. // search
  1736. while (n !== 0 && m < 4) {
  1737. if (z.read_byte(p) == mark[m]) {
  1738. m++;
  1739. } else if (z.read_byte(p) !== 0) {
  1740. m = 0;
  1741. } else {
  1742. m = 4 - m;
  1743. }
  1744. p++;
  1745. n--;
  1746. }
  1747. // restore
  1748. z.total_in += p - z.next_in_index;
  1749. z.next_in_index = p;
  1750. z.avail_in = n;
  1751. z.istate.marker = m;
  1752. // return no joy or set up to restart on a new block
  1753. if (m != 4) {
  1754. return Z_DATA_ERROR;
  1755. }
  1756. r = z.total_in;
  1757. w = z.total_out;
  1758. inflateReset(z);
  1759. z.total_in = r;
  1760. z.total_out = w;
  1761. z.istate.mode = BLOCKS;
  1762. return Z_OK;
  1763. };
  1764. // Returns true if inflate is currently at the end of a block generated
  1765. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1766. // implementation to provide an additional safety check. PPP uses
  1767. // Z_SYNC_FLUSH
  1768. // but removes the length bytes of the resulting empty stored block. When
  1769. // decompressing, PPP checks that at the end of input packet, inflate is
  1770. // waiting for these length bytes.
  1771. that.inflateSyncPoint = function(z) {
  1772. if (!z || !z.istate || !z.istate.blocks)
  1773. return Z_STREAM_ERROR;
  1774. return z.istate.blocks.sync_point();
  1775. };
  1776. }
  1777. // ZStream
  1778. function ZStream() {
  1779. }
  1780. ZStream.prototype = {
  1781. inflateInit : function(bits) {
  1782. var that = this;
  1783. that.istate = new Inflate();
  1784. if (!bits)
  1785. bits = MAX_BITS;
  1786. return that.istate.inflateInit(that, bits);
  1787. },
  1788. inflate : function(f) {
  1789. var that = this;
  1790. if (!that.istate)
  1791. return Z_STREAM_ERROR;
  1792. return that.istate.inflate(that, f);
  1793. },
  1794. inflateEnd : function() {
  1795. var that = this;
  1796. if (!that.istate)
  1797. return Z_STREAM_ERROR;
  1798. var ret = that.istate.inflateEnd(that);
  1799. that.istate = null;
  1800. return ret;
  1801. },
  1802. inflateSync : function() {
  1803. var that = this;
  1804. if (!that.istate)
  1805. return Z_STREAM_ERROR;
  1806. return that.istate.inflateSync(that);
  1807. },
  1808. inflateSetDictionary : function(dictionary, dictLength) {
  1809. var that = this;
  1810. if (!that.istate)
  1811. return Z_STREAM_ERROR;
  1812. return that.istate.inflateSetDictionary(that, dictionary, dictLength);
  1813. },
  1814. read_byte : function(start) {
  1815. var that = this;
  1816. return that.next_in.subarray(start, start + 1)[0];
  1817. },
  1818. read_buf : function(start, size) {
  1819. var that = this;
  1820. return that.next_in.subarray(start, start + size);
  1821. }
  1822. };
  1823. // Inflater
  1824. function Inflater() {
  1825. var that = this;
  1826. var z = new ZStream();
  1827. var bufsize = 512;
  1828. var flush = Z_NO_FLUSH;
  1829. var buf = new Uint8Array(bufsize);
  1830. var nomoreinput = false;
  1831. z.inflateInit();
  1832. z.next_out = buf;
  1833. that.append = function(data, onprogress) {
  1834. var err, buffers = [], lastIndex = 0, bufferIndex = 0, bufferSize = 0, array;
  1835. if (data.length === 0)
  1836. return;
  1837. z.next_in_index = 0;
  1838. z.next_in = data;
  1839. z.avail_in = data.length;
  1840. do {
  1841. z.next_out_index = 0;
  1842. z.avail_out = bufsize;
  1843. if ((z.avail_in === 0) && (!nomoreinput)) { // if buffer is empty and more input is available, refill it
  1844. z.next_in_index = 0;
  1845. nomoreinput = true;
  1846. }
  1847. err = z.inflate(flush);
  1848. if (nomoreinput && (err == Z_BUF_ERROR))
  1849. return -1;
  1850. if (err != Z_OK && err != Z_STREAM_END)
  1851. throw "inflating: " + z.msg;
  1852. if ((nomoreinput || err == Z_STREAM_END) && (z.avail_in == data.length))
  1853. return -1;
  1854. if (z.next_out_index)
  1855. if (z.next_out_index == bufsize)
  1856. buffers.push(new Uint8Array(buf));
  1857. else
  1858. buffers.push(new Uint8Array(buf.subarray(0, z.next_out_index)));
  1859. bufferSize += z.next_out_index;
  1860. if (onprogress && z.next_in_index > 0 && z.next_in_index != lastIndex) {
  1861. onprogress(z.next_in_index);
  1862. lastIndex = z.next_in_index;
  1863. }
  1864. } while (z.avail_in > 0 || z.avail_out === 0);
  1865. array = new Uint8Array(bufferSize);
  1866. buffers.forEach(function(chunk) {
  1867. array.set(chunk, bufferIndex);
  1868. bufferIndex += chunk.length;
  1869. });
  1870. return array;
  1871. };
  1872. that.flush = function() {
  1873. z.inflateEnd();
  1874. };
  1875. }
  1876. var inflater;
  1877. if (obj.zip)
  1878. obj.zip.Inflater = Inflater;
  1879. else {
  1880. inflater = new Inflater();
  1881. obj.addEventListener("message", function(event) {
  1882. var message = event.data;
  1883. if (message.append)
  1884. obj.postMessage({
  1885. onappend : true,
  1886. data : inflater.append(message.data, function(current) {
  1887. obj.postMessage({
  1888. progress : true,
  1889. current : current
  1890. });
  1891. })
  1892. });
  1893. if (message.flush) {
  1894. inflater.flush();
  1895. obj.postMessage({
  1896. onflush : true
  1897. });
  1898. }
  1899. }, false);
  1900. }
  1901. })(this);