pako_inflate.js 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. /*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.pako = {}));
  6. })(this, (function (exports) { 'use strict';
  7. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  8. // It isn't worth it to make additional optimizations as in original.
  9. // Small size is preferable.
  10. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  11. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  12. //
  13. // This software is provided 'as-is', without any express or implied
  14. // warranty. In no event will the authors be held liable for any damages
  15. // arising from the use of this software.
  16. //
  17. // Permission is granted to anyone to use this software for any purpose,
  18. // including commercial applications, and to alter it and redistribute it
  19. // freely, subject to the following restrictions:
  20. //
  21. // 1. The origin of this software must not be misrepresented; you must not
  22. // claim that you wrote the original software. If you use this software
  23. // in a product, an acknowledgment in the product documentation would be
  24. // appreciated but is not required.
  25. // 2. Altered source versions must be plainly marked as such, and must not be
  26. // misrepresented as being the original software.
  27. // 3. This notice may not be removed or altered from any source distribution.
  28. const adler32 = (adler, buf, len, pos) => {
  29. let s1 = (adler & 0xffff) |0,
  30. s2 = ((adler >>> 16) & 0xffff) |0,
  31. n = 0;
  32. while (len !== 0) {
  33. // Set limit ~ twice less than 5552, to keep
  34. // s2 in 31-bits, because we force signed ints.
  35. // in other case %= will fail.
  36. n = len > 2000 ? 2000 : len;
  37. len -= n;
  38. do {
  39. s1 = (s1 + buf[pos++]) |0;
  40. s2 = (s2 + s1) |0;
  41. } while (--n);
  42. s1 %= 65521;
  43. s2 %= 65521;
  44. }
  45. return (s1 | (s2 << 16)) |0;
  46. };
  47. var adler32_1 = adler32;
  48. // Note: we can't get significant speed boost here.
  49. // So write code to minimize size - no pregenerated tables
  50. // and array tools dependencies.
  51. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  52. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  53. //
  54. // This software is provided 'as-is', without any express or implied
  55. // warranty. In no event will the authors be held liable for any damages
  56. // arising from the use of this software.
  57. //
  58. // Permission is granted to anyone to use this software for any purpose,
  59. // including commercial applications, and to alter it and redistribute it
  60. // freely, subject to the following restrictions:
  61. //
  62. // 1. The origin of this software must not be misrepresented; you must not
  63. // claim that you wrote the original software. If you use this software
  64. // in a product, an acknowledgment in the product documentation would be
  65. // appreciated but is not required.
  66. // 2. Altered source versions must be plainly marked as such, and must not be
  67. // misrepresented as being the original software.
  68. // 3. This notice may not be removed or altered from any source distribution.
  69. // Use ordinary array, since untyped makes no boost here
  70. const makeTable = () => {
  71. let c, table = [];
  72. for (var n = 0; n < 256; n++) {
  73. c = n;
  74. for (var k = 0; k < 8; k++) {
  75. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  76. }
  77. table[n] = c;
  78. }
  79. return table;
  80. };
  81. // Create table on load. Just 255 signed longs. Not a problem.
  82. const crcTable = new Uint32Array(makeTable());
  83. const crc32 = (crc, buf, len, pos) => {
  84. const t = crcTable;
  85. const end = pos + len;
  86. crc ^= -1;
  87. for (let i = pos; i < end; i++) {
  88. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  89. }
  90. return (crc ^ (-1)); // >>> 0;
  91. };
  92. var crc32_1 = crc32;
  93. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  94. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  95. //
  96. // This software is provided 'as-is', without any express or implied
  97. // warranty. In no event will the authors be held liable for any damages
  98. // arising from the use of this software.
  99. //
  100. // Permission is granted to anyone to use this software for any purpose,
  101. // including commercial applications, and to alter it and redistribute it
  102. // freely, subject to the following restrictions:
  103. //
  104. // 1. The origin of this software must not be misrepresented; you must not
  105. // claim that you wrote the original software. If you use this software
  106. // in a product, an acknowledgment in the product documentation would be
  107. // appreciated but is not required.
  108. // 2. Altered source versions must be plainly marked as such, and must not be
  109. // misrepresented as being the original software.
  110. // 3. This notice may not be removed or altered from any source distribution.
  111. // See state defs from inflate.js
  112. const BAD$1 = 16209; /* got a data error -- remain here until reset */
  113. const TYPE$1 = 16191; /* i: waiting for type bits, including last-flag bit */
  114. /*
  115. Decode literal, length, and distance codes and write out the resulting
  116. literal and match bytes until either not enough input or output is
  117. available, an end-of-block is encountered, or a data error is encountered.
  118. When large enough input and output buffers are supplied to inflate(), for
  119. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  120. inflate execution time is spent in this routine.
  121. Entry assumptions:
  122. state.mode === LEN
  123. strm.avail_in >= 6
  124. strm.avail_out >= 258
  125. start >= strm.avail_out
  126. state.bits < 8
  127. On return, state.mode is one of:
  128. LEN -- ran out of enough output space or enough available input
  129. TYPE -- reached end of block code, inflate() to interpret next block
  130. BAD -- error in block data
  131. Notes:
  132. - The maximum input bits used by a length/distance pair is 15 bits for the
  133. length code, 5 bits for the length extra, 15 bits for the distance code,
  134. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  135. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  136. checking for available input while decoding.
  137. - The maximum bytes that a single length/distance pair can output is 258
  138. bytes, which is the maximum length that can be coded. inflate_fast()
  139. requires strm.avail_out >= 258 for each loop to avoid checking for
  140. output space.
  141. */
  142. var inffast = function inflate_fast(strm, start) {
  143. let _in; /* local strm.input */
  144. let last; /* have enough input while in < last */
  145. let _out; /* local strm.output */
  146. let beg; /* inflate()'s initial strm.output */
  147. let end; /* while out < end, enough space available */
  148. //#ifdef INFLATE_STRICT
  149. let dmax; /* maximum distance from zlib header */
  150. //#endif
  151. let wsize; /* window size or zero if not using window */
  152. let whave; /* valid bytes in the window */
  153. let wnext; /* window write index */
  154. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  155. let s_window; /* allocated sliding window, if wsize != 0 */
  156. let hold; /* local strm.hold */
  157. let bits; /* local strm.bits */
  158. let lcode; /* local strm.lencode */
  159. let dcode; /* local strm.distcode */
  160. let lmask; /* mask for first level of length codes */
  161. let dmask; /* mask for first level of distance codes */
  162. let here; /* retrieved table entry */
  163. let op; /* code bits, operation, extra bits, or */
  164. /* window position, window bytes to copy */
  165. let len; /* match length, unused bytes */
  166. let dist; /* match distance */
  167. let from; /* where to copy match from */
  168. let from_source;
  169. let input, output; // JS specific, because we have no pointers
  170. /* copy state to local variables */
  171. const state = strm.state;
  172. //here = state.here;
  173. _in = strm.next_in;
  174. input = strm.input;
  175. last = _in + (strm.avail_in - 5);
  176. _out = strm.next_out;
  177. output = strm.output;
  178. beg = _out - (start - strm.avail_out);
  179. end = _out + (strm.avail_out - 257);
  180. //#ifdef INFLATE_STRICT
  181. dmax = state.dmax;
  182. //#endif
  183. wsize = state.wsize;
  184. whave = state.whave;
  185. wnext = state.wnext;
  186. s_window = state.window;
  187. hold = state.hold;
  188. bits = state.bits;
  189. lcode = state.lencode;
  190. dcode = state.distcode;
  191. lmask = (1 << state.lenbits) - 1;
  192. dmask = (1 << state.distbits) - 1;
  193. /* decode literals and length/distances until end-of-block or not enough
  194. input data or output space */
  195. top:
  196. do {
  197. if (bits < 15) {
  198. hold += input[_in++] << bits;
  199. bits += 8;
  200. hold += input[_in++] << bits;
  201. bits += 8;
  202. }
  203. here = lcode[hold & lmask];
  204. dolen:
  205. for (;;) { // Goto emulation
  206. op = here >>> 24/*here.bits*/;
  207. hold >>>= op;
  208. bits -= op;
  209. op = (here >>> 16) & 0xff/*here.op*/;
  210. if (op === 0) { /* literal */
  211. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  212. // "inflate: literal '%c'\n" :
  213. // "inflate: literal 0x%02x\n", here.val));
  214. output[_out++] = here & 0xffff/*here.val*/;
  215. }
  216. else if (op & 16) { /* length base */
  217. len = here & 0xffff/*here.val*/;
  218. op &= 15; /* number of extra bits */
  219. if (op) {
  220. if (bits < op) {
  221. hold += input[_in++] << bits;
  222. bits += 8;
  223. }
  224. len += hold & ((1 << op) - 1);
  225. hold >>>= op;
  226. bits -= op;
  227. }
  228. //Tracevv((stderr, "inflate: length %u\n", len));
  229. if (bits < 15) {
  230. hold += input[_in++] << bits;
  231. bits += 8;
  232. hold += input[_in++] << bits;
  233. bits += 8;
  234. }
  235. here = dcode[hold & dmask];
  236. dodist:
  237. for (;;) { // goto emulation
  238. op = here >>> 24/*here.bits*/;
  239. hold >>>= op;
  240. bits -= op;
  241. op = (here >>> 16) & 0xff/*here.op*/;
  242. if (op & 16) { /* distance base */
  243. dist = here & 0xffff/*here.val*/;
  244. op &= 15; /* number of extra bits */
  245. if (bits < op) {
  246. hold += input[_in++] << bits;
  247. bits += 8;
  248. if (bits < op) {
  249. hold += input[_in++] << bits;
  250. bits += 8;
  251. }
  252. }
  253. dist += hold & ((1 << op) - 1);
  254. //#ifdef INFLATE_STRICT
  255. if (dist > dmax) {
  256. strm.msg = 'invalid distance too far back';
  257. state.mode = BAD$1;
  258. break top;
  259. }
  260. //#endif
  261. hold >>>= op;
  262. bits -= op;
  263. //Tracevv((stderr, "inflate: distance %u\n", dist));
  264. op = _out - beg; /* max distance in output */
  265. if (dist > op) { /* see if copy from window */
  266. op = dist - op; /* distance back in window */
  267. if (op > whave) {
  268. if (state.sane) {
  269. strm.msg = 'invalid distance too far back';
  270. state.mode = BAD$1;
  271. break top;
  272. }
  273. // (!) This block is disabled in zlib defaults,
  274. // don't enable it for binary compatibility
  275. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  276. // if (len <= op - whave) {
  277. // do {
  278. // output[_out++] = 0;
  279. // } while (--len);
  280. // continue top;
  281. // }
  282. // len -= op - whave;
  283. // do {
  284. // output[_out++] = 0;
  285. // } while (--op > whave);
  286. // if (op === 0) {
  287. // from = _out - dist;
  288. // do {
  289. // output[_out++] = output[from++];
  290. // } while (--len);
  291. // continue top;
  292. // }
  293. //#endif
  294. }
  295. from = 0; // window index
  296. from_source = s_window;
  297. if (wnext === 0) { /* very common case */
  298. from += wsize - op;
  299. if (op < len) { /* some from window */
  300. len -= op;
  301. do {
  302. output[_out++] = s_window[from++];
  303. } while (--op);
  304. from = _out - dist; /* rest from output */
  305. from_source = output;
  306. }
  307. }
  308. else if (wnext < op) { /* wrap around window */
  309. from += wsize + wnext - op;
  310. op -= wnext;
  311. if (op < len) { /* some from end of window */
  312. len -= op;
  313. do {
  314. output[_out++] = s_window[from++];
  315. } while (--op);
  316. from = 0;
  317. if (wnext < len) { /* some from start of window */
  318. op = wnext;
  319. len -= op;
  320. do {
  321. output[_out++] = s_window[from++];
  322. } while (--op);
  323. from = _out - dist; /* rest from output */
  324. from_source = output;
  325. }
  326. }
  327. }
  328. else { /* contiguous in window */
  329. from += wnext - op;
  330. if (op < len) { /* some from window */
  331. len -= op;
  332. do {
  333. output[_out++] = s_window[from++];
  334. } while (--op);
  335. from = _out - dist; /* rest from output */
  336. from_source = output;
  337. }
  338. }
  339. while (len > 2) {
  340. output[_out++] = from_source[from++];
  341. output[_out++] = from_source[from++];
  342. output[_out++] = from_source[from++];
  343. len -= 3;
  344. }
  345. if (len) {
  346. output[_out++] = from_source[from++];
  347. if (len > 1) {
  348. output[_out++] = from_source[from++];
  349. }
  350. }
  351. }
  352. else {
  353. from = _out - dist; /* copy direct from output */
  354. do { /* minimum length is three */
  355. output[_out++] = output[from++];
  356. output[_out++] = output[from++];
  357. output[_out++] = output[from++];
  358. len -= 3;
  359. } while (len > 2);
  360. if (len) {
  361. output[_out++] = output[from++];
  362. if (len > 1) {
  363. output[_out++] = output[from++];
  364. }
  365. }
  366. }
  367. }
  368. else if ((op & 64) === 0) { /* 2nd level distance code */
  369. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  370. continue dodist;
  371. }
  372. else {
  373. strm.msg = 'invalid distance code';
  374. state.mode = BAD$1;
  375. break top;
  376. }
  377. break; // need to emulate goto via "continue"
  378. }
  379. }
  380. else if ((op & 64) === 0) { /* 2nd level length code */
  381. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  382. continue dolen;
  383. }
  384. else if (op & 32) { /* end-of-block */
  385. //Tracevv((stderr, "inflate: end of block\n"));
  386. state.mode = TYPE$1;
  387. break top;
  388. }
  389. else {
  390. strm.msg = 'invalid literal/length code';
  391. state.mode = BAD$1;
  392. break top;
  393. }
  394. break; // need to emulate goto via "continue"
  395. }
  396. } while (_in < last && _out < end);
  397. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  398. len = bits >> 3;
  399. _in -= len;
  400. bits -= len << 3;
  401. hold &= (1 << bits) - 1;
  402. /* update state and return */
  403. strm.next_in = _in;
  404. strm.next_out = _out;
  405. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  406. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  407. state.hold = hold;
  408. state.bits = bits;
  409. return;
  410. };
  411. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  412. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  413. //
  414. // This software is provided 'as-is', without any express or implied
  415. // warranty. In no event will the authors be held liable for any damages
  416. // arising from the use of this software.
  417. //
  418. // Permission is granted to anyone to use this software for any purpose,
  419. // including commercial applications, and to alter it and redistribute it
  420. // freely, subject to the following restrictions:
  421. //
  422. // 1. The origin of this software must not be misrepresented; you must not
  423. // claim that you wrote the original software. If you use this software
  424. // in a product, an acknowledgment in the product documentation would be
  425. // appreciated but is not required.
  426. // 2. Altered source versions must be plainly marked as such, and must not be
  427. // misrepresented as being the original software.
  428. // 3. This notice may not be removed or altered from any source distribution.
  429. const MAXBITS = 15;
  430. const ENOUGH_LENS$1 = 852;
  431. const ENOUGH_DISTS$1 = 592;
  432. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  433. const CODES$1 = 0;
  434. const LENS$1 = 1;
  435. const DISTS$1 = 2;
  436. const lbase = new Uint16Array([ /* Length codes 257..285 base */
  437. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  438. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  439. ]);
  440. const lext = new Uint8Array([ /* Length codes 257..285 extra */
  441. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  442. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  443. ]);
  444. const dbase = new Uint16Array([ /* Distance codes 0..29 base */
  445. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  446. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  447. 8193, 12289, 16385, 24577, 0, 0
  448. ]);
  449. const dext = new Uint8Array([ /* Distance codes 0..29 extra */
  450. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  451. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  452. 28, 28, 29, 29, 64, 64
  453. ]);
  454. const inflate_table = (type, lens, lens_index, codes, table, table_index, work, opts) =>
  455. {
  456. const bits = opts.bits;
  457. //here = opts.here; /* table entry for duplication */
  458. let len = 0; /* a code's length in bits */
  459. let sym = 0; /* index of code symbols */
  460. let min = 0, max = 0; /* minimum and maximum code lengths */
  461. let root = 0; /* number of index bits for root table */
  462. let curr = 0; /* number of index bits for current table */
  463. let drop = 0; /* code bits to drop for sub-table */
  464. let left = 0; /* number of prefix codes available */
  465. let used = 0; /* code entries in table used */
  466. let huff = 0; /* Huffman code */
  467. let incr; /* for incrementing code, index */
  468. let fill; /* index for replicating entries */
  469. let low; /* low bits for current root entry */
  470. let mask; /* mask for low root bits */
  471. let next; /* next available space in table */
  472. let base = null; /* base value table to use */
  473. // let shoextra; /* extra bits table to use */
  474. let match; /* use base and extra for symbol >= match */
  475. const count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  476. const offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  477. let extra = null;
  478. let here_bits, here_op, here_val;
  479. /*
  480. Process a set of code lengths to create a canonical Huffman code. The
  481. code lengths are lens[0..codes-1]. Each length corresponds to the
  482. symbols 0..codes-1. The Huffman code is generated by first sorting the
  483. symbols by length from short to long, and retaining the symbol order
  484. for codes with equal lengths. Then the code starts with all zero bits
  485. for the first code of the shortest length, and the codes are integer
  486. increments for the same length, and zeros are appended as the length
  487. increases. For the deflate format, these bits are stored backwards
  488. from their more natural integer increment ordering, and so when the
  489. decoding tables are built in the large loop below, the integer codes
  490. are incremented backwards.
  491. This routine assumes, but does not check, that all of the entries in
  492. lens[] are in the range 0..MAXBITS. The caller must assure this.
  493. 1..MAXBITS is interpreted as that code length. zero means that that
  494. symbol does not occur in this code.
  495. The codes are sorted by computing a count of codes for each length,
  496. creating from that a table of starting indices for each length in the
  497. sorted table, and then entering the symbols in order in the sorted
  498. table. The sorted table is work[], with that space being provided by
  499. the caller.
  500. The length counts are used for other purposes as well, i.e. finding
  501. the minimum and maximum length codes, determining if there are any
  502. codes at all, checking for a valid set of lengths, and looking ahead
  503. at length counts to determine sub-table sizes when building the
  504. decoding tables.
  505. */
  506. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  507. for (len = 0; len <= MAXBITS; len++) {
  508. count[len] = 0;
  509. }
  510. for (sym = 0; sym < codes; sym++) {
  511. count[lens[lens_index + sym]]++;
  512. }
  513. /* bound code lengths, force root to be within code lengths */
  514. root = bits;
  515. for (max = MAXBITS; max >= 1; max--) {
  516. if (count[max] !== 0) { break; }
  517. }
  518. if (root > max) {
  519. root = max;
  520. }
  521. if (max === 0) { /* no symbols to code at all */
  522. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  523. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  524. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  525. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  526. //table.op[opts.table_index] = 64;
  527. //table.bits[opts.table_index] = 1;
  528. //table.val[opts.table_index++] = 0;
  529. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  530. opts.bits = 1;
  531. return 0; /* no symbols, but wait for decoding to report error */
  532. }
  533. for (min = 1; min < max; min++) {
  534. if (count[min] !== 0) { break; }
  535. }
  536. if (root < min) {
  537. root = min;
  538. }
  539. /* check for an over-subscribed or incomplete set of lengths */
  540. left = 1;
  541. for (len = 1; len <= MAXBITS; len++) {
  542. left <<= 1;
  543. left -= count[len];
  544. if (left < 0) {
  545. return -1;
  546. } /* over-subscribed */
  547. }
  548. if (left > 0 && (type === CODES$1 || max !== 1)) {
  549. return -1; /* incomplete set */
  550. }
  551. /* generate offsets into symbol table for each length for sorting */
  552. offs[1] = 0;
  553. for (len = 1; len < MAXBITS; len++) {
  554. offs[len + 1] = offs[len] + count[len];
  555. }
  556. /* sort symbols by length, by symbol order within each length */
  557. for (sym = 0; sym < codes; sym++) {
  558. if (lens[lens_index + sym] !== 0) {
  559. work[offs[lens[lens_index + sym]]++] = sym;
  560. }
  561. }
  562. /*
  563. Create and fill in decoding tables. In this loop, the table being
  564. filled is at next and has curr index bits. The code being used is huff
  565. with length len. That code is converted to an index by dropping drop
  566. bits off of the bottom. For codes where len is less than drop + curr,
  567. those top drop + curr - len bits are incremented through all values to
  568. fill the table with replicated entries.
  569. root is the number of index bits for the root table. When len exceeds
  570. root, sub-tables are created pointed to by the root entry with an index
  571. of the low root bits of huff. This is saved in low to check for when a
  572. new sub-table should be started. drop is zero when the root table is
  573. being filled, and drop is root when sub-tables are being filled.
  574. When a new sub-table is needed, it is necessary to look ahead in the
  575. code lengths to determine what size sub-table is needed. The length
  576. counts are used for this, and so count[] is decremented as codes are
  577. entered in the tables.
  578. used keeps track of how many table entries have been allocated from the
  579. provided *table space. It is checked for LENS and DIST tables against
  580. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  581. the initial root table size constants. See the comments in inftrees.h
  582. for more information.
  583. sym increments through all symbols, and the loop terminates when
  584. all codes of length max, i.e. all codes, have been processed. This
  585. routine permits incomplete codes, so another loop after this one fills
  586. in the rest of the decoding tables with invalid code markers.
  587. */
  588. /* set up for code type */
  589. // poor man optimization - use if-else instead of switch,
  590. // to avoid deopts in old v8
  591. if (type === CODES$1) {
  592. base = extra = work; /* dummy value--not used */
  593. match = 20;
  594. } else if (type === LENS$1) {
  595. base = lbase;
  596. extra = lext;
  597. match = 257;
  598. } else { /* DISTS */
  599. base = dbase;
  600. extra = dext;
  601. match = 0;
  602. }
  603. /* initialize opts for loop */
  604. huff = 0; /* starting code */
  605. sym = 0; /* starting code symbol */
  606. len = min; /* starting code length */
  607. next = table_index; /* current table to fill in */
  608. curr = root; /* current table index bits */
  609. drop = 0; /* current bits to drop from code for index */
  610. low = -1; /* trigger new sub-table when len > root */
  611. used = 1 << root; /* use root table entries */
  612. mask = used - 1; /* mask for comparing low */
  613. /* check available table space */
  614. if ((type === LENS$1 && used > ENOUGH_LENS$1) ||
  615. (type === DISTS$1 && used > ENOUGH_DISTS$1)) {
  616. return 1;
  617. }
  618. /* process all codes and make table entries */
  619. for (;;) {
  620. /* create table entry */
  621. here_bits = len - drop;
  622. if (work[sym] + 1 < match) {
  623. here_op = 0;
  624. here_val = work[sym];
  625. }
  626. else if (work[sym] >= match) {
  627. here_op = extra[work[sym] - match];
  628. here_val = base[work[sym] - match];
  629. }
  630. else {
  631. here_op = 32 + 64; /* end of block */
  632. here_val = 0;
  633. }
  634. /* replicate for those indices with low len bits equal to huff */
  635. incr = 1 << (len - drop);
  636. fill = 1 << curr;
  637. min = fill; /* save offset to next table */
  638. do {
  639. fill -= incr;
  640. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  641. } while (fill !== 0);
  642. /* backwards increment the len-bit code huff */
  643. incr = 1 << (len - 1);
  644. while (huff & incr) {
  645. incr >>= 1;
  646. }
  647. if (incr !== 0) {
  648. huff &= incr - 1;
  649. huff += incr;
  650. } else {
  651. huff = 0;
  652. }
  653. /* go to next symbol, update count, len */
  654. sym++;
  655. if (--count[len] === 0) {
  656. if (len === max) { break; }
  657. len = lens[lens_index + work[sym]];
  658. }
  659. /* create new sub-table if needed */
  660. if (len > root && (huff & mask) !== low) {
  661. /* if first time, transition to sub-tables */
  662. if (drop === 0) {
  663. drop = root;
  664. }
  665. /* increment past last table */
  666. next += min; /* here min is 1 << curr */
  667. /* determine length of next table */
  668. curr = len - drop;
  669. left = 1 << curr;
  670. while (curr + drop < max) {
  671. left -= count[curr + drop];
  672. if (left <= 0) { break; }
  673. curr++;
  674. left <<= 1;
  675. }
  676. /* check for enough space */
  677. used += 1 << curr;
  678. if ((type === LENS$1 && used > ENOUGH_LENS$1) ||
  679. (type === DISTS$1 && used > ENOUGH_DISTS$1)) {
  680. return 1;
  681. }
  682. /* point entry in root table to sub-table */
  683. low = huff & mask;
  684. /*table.op[low] = curr;
  685. table.bits[low] = root;
  686. table.val[low] = next - opts.table_index;*/
  687. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  688. }
  689. }
  690. /* fill in remaining table entry if code is incomplete (guaranteed to have
  691. at most one remaining entry, since if the code is incomplete, the
  692. maximum code length that was allowed to get this far is one bit) */
  693. if (huff !== 0) {
  694. //table.op[next + huff] = 64; /* invalid code marker */
  695. //table.bits[next + huff] = len - drop;
  696. //table.val[next + huff] = 0;
  697. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  698. }
  699. /* set return parameters */
  700. //opts.table_index += used;
  701. opts.bits = root;
  702. return 0;
  703. };
  704. var inftrees = inflate_table;
  705. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  706. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  707. //
  708. // This software is provided 'as-is', without any express or implied
  709. // warranty. In no event will the authors be held liable for any damages
  710. // arising from the use of this software.
  711. //
  712. // Permission is granted to anyone to use this software for any purpose,
  713. // including commercial applications, and to alter it and redistribute it
  714. // freely, subject to the following restrictions:
  715. //
  716. // 1. The origin of this software must not be misrepresented; you must not
  717. // claim that you wrote the original software. If you use this software
  718. // in a product, an acknowledgment in the product documentation would be
  719. // appreciated but is not required.
  720. // 2. Altered source versions must be plainly marked as such, and must not be
  721. // misrepresented as being the original software.
  722. // 3. This notice may not be removed or altered from any source distribution.
  723. var constants$1 = {
  724. /* Allowed flush values; see deflate() and inflate() below for details */
  725. Z_NO_FLUSH: 0,
  726. Z_PARTIAL_FLUSH: 1,
  727. Z_SYNC_FLUSH: 2,
  728. Z_FULL_FLUSH: 3,
  729. Z_FINISH: 4,
  730. Z_BLOCK: 5,
  731. Z_TREES: 6,
  732. /* Return codes for the compression/decompression functions. Negative values
  733. * are errors, positive values are used for special but normal events.
  734. */
  735. Z_OK: 0,
  736. Z_STREAM_END: 1,
  737. Z_NEED_DICT: 2,
  738. Z_ERRNO: -1,
  739. Z_STREAM_ERROR: -2,
  740. Z_DATA_ERROR: -3,
  741. Z_MEM_ERROR: -4,
  742. Z_BUF_ERROR: -5,
  743. //Z_VERSION_ERROR: -6,
  744. /* compression levels */
  745. Z_NO_COMPRESSION: 0,
  746. Z_BEST_SPEED: 1,
  747. Z_BEST_COMPRESSION: 9,
  748. Z_DEFAULT_COMPRESSION: -1,
  749. Z_FILTERED: 1,
  750. Z_HUFFMAN_ONLY: 2,
  751. Z_RLE: 3,
  752. Z_FIXED: 4,
  753. Z_DEFAULT_STRATEGY: 0,
  754. /* Possible values of the data_type field (though see inflate()) */
  755. Z_BINARY: 0,
  756. Z_TEXT: 1,
  757. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  758. Z_UNKNOWN: 2,
  759. /* The deflate compression method */
  760. Z_DEFLATED: 8
  761. //Z_NULL: null // Use -1 or null inline, depending on var type
  762. };
  763. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  764. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  765. //
  766. // This software is provided 'as-is', without any express or implied
  767. // warranty. In no event will the authors be held liable for any damages
  768. // arising from the use of this software.
  769. //
  770. // Permission is granted to anyone to use this software for any purpose,
  771. // including commercial applications, and to alter it and redistribute it
  772. // freely, subject to the following restrictions:
  773. //
  774. // 1. The origin of this software must not be misrepresented; you must not
  775. // claim that you wrote the original software. If you use this software
  776. // in a product, an acknowledgment in the product documentation would be
  777. // appreciated but is not required.
  778. // 2. Altered source versions must be plainly marked as such, and must not be
  779. // misrepresented as being the original software.
  780. // 3. This notice may not be removed or altered from any source distribution.
  781. const CODES = 0;
  782. const LENS = 1;
  783. const DISTS = 2;
  784. /* Public constants ==========================================================*/
  785. /* ===========================================================================*/
  786. const {
  787. Z_FINISH: Z_FINISH$1, Z_BLOCK, Z_TREES,
  788. Z_OK: Z_OK$1, Z_STREAM_END: Z_STREAM_END$1, Z_NEED_DICT: Z_NEED_DICT$1, Z_STREAM_ERROR: Z_STREAM_ERROR$1, Z_DATA_ERROR: Z_DATA_ERROR$1, Z_MEM_ERROR: Z_MEM_ERROR$1, Z_BUF_ERROR,
  789. Z_DEFLATED
  790. } = constants$1;
  791. /* STATES ====================================================================*/
  792. /* ===========================================================================*/
  793. const HEAD = 16180; /* i: waiting for magic header */
  794. const FLAGS = 16181; /* i: waiting for method and flags (gzip) */
  795. const TIME = 16182; /* i: waiting for modification time (gzip) */
  796. const OS = 16183; /* i: waiting for extra flags and operating system (gzip) */
  797. const EXLEN = 16184; /* i: waiting for extra length (gzip) */
  798. const EXTRA = 16185; /* i: waiting for extra bytes (gzip) */
  799. const NAME = 16186; /* i: waiting for end of file name (gzip) */
  800. const COMMENT = 16187; /* i: waiting for end of comment (gzip) */
  801. const HCRC = 16188; /* i: waiting for header crc (gzip) */
  802. const DICTID = 16189; /* i: waiting for dictionary check value */
  803. const DICT = 16190; /* waiting for inflateSetDictionary() call */
  804. const TYPE = 16191; /* i: waiting for type bits, including last-flag bit */
  805. const TYPEDO = 16192; /* i: same, but skip check to exit inflate on new block */
  806. const STORED = 16193; /* i: waiting for stored size (length and complement) */
  807. const COPY_ = 16194; /* i/o: same as COPY below, but only first time in */
  808. const COPY = 16195; /* i/o: waiting for input or output to copy stored block */
  809. const TABLE = 16196; /* i: waiting for dynamic block table lengths */
  810. const LENLENS = 16197; /* i: waiting for code length code lengths */
  811. const CODELENS = 16198; /* i: waiting for length/lit and distance code lengths */
  812. const LEN_ = 16199; /* i: same as LEN below, but only first time in */
  813. const LEN = 16200; /* i: waiting for length/lit/eob code */
  814. const LENEXT = 16201; /* i: waiting for length extra bits */
  815. const DIST = 16202; /* i: waiting for distance code */
  816. const DISTEXT = 16203; /* i: waiting for distance extra bits */
  817. const MATCH = 16204; /* o: waiting for output space to copy string */
  818. const LIT = 16205; /* o: waiting for output space to write literal */
  819. const CHECK = 16206; /* i: waiting for 32-bit check value */
  820. const LENGTH = 16207; /* i: waiting for 32-bit length (gzip) */
  821. const DONE = 16208; /* finished check, done -- remain here until reset */
  822. const BAD = 16209; /* got a data error -- remain here until reset */
  823. const MEM = 16210; /* got an inflate() memory error -- remain here until reset */
  824. const SYNC = 16211; /* looking for synchronization bytes to restart inflate() */
  825. /* ===========================================================================*/
  826. const ENOUGH_LENS = 852;
  827. const ENOUGH_DISTS = 592;
  828. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  829. const MAX_WBITS = 15;
  830. /* 32K LZ77 window */
  831. const DEF_WBITS = MAX_WBITS;
  832. const zswap32 = (q) => {
  833. return (((q >>> 24) & 0xff) +
  834. ((q >>> 8) & 0xff00) +
  835. ((q & 0xff00) << 8) +
  836. ((q & 0xff) << 24));
  837. };
  838. function InflateState() {
  839. this.strm = null; /* pointer back to this zlib stream */
  840. this.mode = 0; /* current inflate mode */
  841. this.last = false; /* true if processing last block */
  842. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip,
  843. bit 2 true to validate check value */
  844. this.havedict = false; /* true if dictionary provided */
  845. this.flags = 0; /* gzip header method and flags (0 if zlib), or
  846. -1 if raw or no header yet */
  847. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  848. this.check = 0; /* protected copy of check value */
  849. this.total = 0; /* protected copy of output count */
  850. // TODO: may be {}
  851. this.head = null; /* where to save gzip header information */
  852. /* sliding window */
  853. this.wbits = 0; /* log base 2 of requested window size */
  854. this.wsize = 0; /* window size or zero if not using window */
  855. this.whave = 0; /* valid bytes in the window */
  856. this.wnext = 0; /* window write index */
  857. this.window = null; /* allocated sliding window, if needed */
  858. /* bit accumulator */
  859. this.hold = 0; /* input bit accumulator */
  860. this.bits = 0; /* number of bits in "in" */
  861. /* for string and stored block copying */
  862. this.length = 0; /* literal or length of data to copy */
  863. this.offset = 0; /* distance back to copy string from */
  864. /* for table and code decoding */
  865. this.extra = 0; /* extra bits needed */
  866. /* fixed and dynamic code tables */
  867. this.lencode = null; /* starting table for length/literal codes */
  868. this.distcode = null; /* starting table for distance codes */
  869. this.lenbits = 0; /* index bits for lencode */
  870. this.distbits = 0; /* index bits for distcode */
  871. /* dynamic table building */
  872. this.ncode = 0; /* number of code length code lengths */
  873. this.nlen = 0; /* number of length code lengths */
  874. this.ndist = 0; /* number of distance code lengths */
  875. this.have = 0; /* number of code lengths in lens[] */
  876. this.next = null; /* next available space in codes[] */
  877. this.lens = new Uint16Array(320); /* temporary storage for code lengths */
  878. this.work = new Uint16Array(288); /* work area for code table building */
  879. /*
  880. because we don't have pointers in js, we use lencode and distcode directly
  881. as buffers so we don't need codes
  882. */
  883. //this.codes = new Int32Array(ENOUGH); /* space for code tables */
  884. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  885. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  886. this.sane = 0; /* if false, allow invalid distance too far */
  887. this.back = 0; /* bits back of last unprocessed length/lit */
  888. this.was = 0; /* initial length of match */
  889. }
  890. const inflateStateCheck = (strm) => {
  891. if (!strm) {
  892. return 1;
  893. }
  894. const state = strm.state;
  895. if (!state || state.strm !== strm ||
  896. state.mode < HEAD || state.mode > SYNC) {
  897. return 1;
  898. }
  899. return 0;
  900. };
  901. const inflateResetKeep = (strm) => {
  902. if (inflateStateCheck(strm)) { return Z_STREAM_ERROR$1; }
  903. const state = strm.state;
  904. strm.total_in = strm.total_out = state.total = 0;
  905. strm.msg = ''; /*Z_NULL*/
  906. if (state.wrap) { /* to support ill-conceived Java test suite */
  907. strm.adler = state.wrap & 1;
  908. }
  909. state.mode = HEAD;
  910. state.last = 0;
  911. state.havedict = 0;
  912. state.flags = -1;
  913. state.dmax = 32768;
  914. state.head = null/*Z_NULL*/;
  915. state.hold = 0;
  916. state.bits = 0;
  917. //state.lencode = state.distcode = state.next = state.codes;
  918. state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
  919. state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
  920. state.sane = 1;
  921. state.back = -1;
  922. //Tracev((stderr, "inflate: reset\n"));
  923. return Z_OK$1;
  924. };
  925. const inflateReset = (strm) => {
  926. if (inflateStateCheck(strm)) { return Z_STREAM_ERROR$1; }
  927. const state = strm.state;
  928. state.wsize = 0;
  929. state.whave = 0;
  930. state.wnext = 0;
  931. return inflateResetKeep(strm);
  932. };
  933. const inflateReset2 = (strm, windowBits) => {
  934. let wrap;
  935. /* get the state */
  936. if (inflateStateCheck(strm)) { return Z_STREAM_ERROR$1; }
  937. const state = strm.state;
  938. /* extract wrap request from windowBits parameter */
  939. if (windowBits < 0) {
  940. wrap = 0;
  941. windowBits = -windowBits;
  942. }
  943. else {
  944. wrap = (windowBits >> 4) + 5;
  945. if (windowBits < 48) {
  946. windowBits &= 15;
  947. }
  948. }
  949. /* set number of window bits, free window if different */
  950. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  951. return Z_STREAM_ERROR$1;
  952. }
  953. if (state.window !== null && state.wbits !== windowBits) {
  954. state.window = null;
  955. }
  956. /* update state and reset the rest of it */
  957. state.wrap = wrap;
  958. state.wbits = windowBits;
  959. return inflateReset(strm);
  960. };
  961. const inflateInit2 = (strm, windowBits) => {
  962. if (!strm) { return Z_STREAM_ERROR$1; }
  963. //strm.msg = Z_NULL; /* in case we return an error */
  964. const state = new InflateState();
  965. //if (state === Z_NULL) return Z_MEM_ERROR;
  966. //Tracev((stderr, "inflate: allocated\n"));
  967. strm.state = state;
  968. state.strm = strm;
  969. state.window = null/*Z_NULL*/;
  970. state.mode = HEAD; /* to pass state test in inflateReset2() */
  971. const ret = inflateReset2(strm, windowBits);
  972. if (ret !== Z_OK$1) {
  973. strm.state = null/*Z_NULL*/;
  974. }
  975. return ret;
  976. };
  977. const inflateInit = (strm) => {
  978. return inflateInit2(strm, DEF_WBITS);
  979. };
  980. /*
  981. Return state with length and distance decoding tables and index sizes set to
  982. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  983. If BUILDFIXED is defined, then instead this routine builds the tables the
  984. first time it's called, and returns those tables the first time and
  985. thereafter. This reduces the size of the code by about 2K bytes, in
  986. exchange for a little execution time. However, BUILDFIXED should not be
  987. used for threaded applications, since the rewriting of the tables and virgin
  988. may not be thread-safe.
  989. */
  990. let virgin = true;
  991. let lenfix, distfix; // We have no pointers in JS, so keep tables separate
  992. const fixedtables = (state) => {
  993. /* build fixed huffman tables if first call (may not be thread safe) */
  994. if (virgin) {
  995. lenfix = new Int32Array(512);
  996. distfix = new Int32Array(32);
  997. /* literal/length table */
  998. let sym = 0;
  999. while (sym < 144) { state.lens[sym++] = 8; }
  1000. while (sym < 256) { state.lens[sym++] = 9; }
  1001. while (sym < 280) { state.lens[sym++] = 7; }
  1002. while (sym < 288) { state.lens[sym++] = 8; }
  1003. inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
  1004. /* distance table */
  1005. sym = 0;
  1006. while (sym < 32) { state.lens[sym++] = 5; }
  1007. inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
  1008. /* do this just once */
  1009. virgin = false;
  1010. }
  1011. state.lencode = lenfix;
  1012. state.lenbits = 9;
  1013. state.distcode = distfix;
  1014. state.distbits = 5;
  1015. };
  1016. /*
  1017. Update the window with the last wsize (normally 32K) bytes written before
  1018. returning. If window does not exist yet, create it. This is only called
  1019. when a window is already in use, or when output has been written during this
  1020. inflate call, but the end of the deflate stream has not been reached yet.
  1021. It is also called to create a window for dictionary data when a dictionary
  1022. is loaded.
  1023. Providing output buffers larger than 32K to inflate() should provide a speed
  1024. advantage, since only the last 32K of output is copied to the sliding window
  1025. upon return from inflate(), and since all distances after the first 32K of
  1026. output will fall in the output data, making match copies simpler and faster.
  1027. The advantage may be dependent on the size of the processor's data caches.
  1028. */
  1029. const updatewindow = (strm, src, end, copy) => {
  1030. let dist;
  1031. const state = strm.state;
  1032. /* if it hasn't been done already, allocate space for the window */
  1033. if (state.window === null) {
  1034. state.wsize = 1 << state.wbits;
  1035. state.wnext = 0;
  1036. state.whave = 0;
  1037. state.window = new Uint8Array(state.wsize);
  1038. }
  1039. /* copy state->wsize or less output bytes into the circular window */
  1040. if (copy >= state.wsize) {
  1041. state.window.set(src.subarray(end - state.wsize, end), 0);
  1042. state.wnext = 0;
  1043. state.whave = state.wsize;
  1044. }
  1045. else {
  1046. dist = state.wsize - state.wnext;
  1047. if (dist > copy) {
  1048. dist = copy;
  1049. }
  1050. //zmemcpy(state->window + state->wnext, end - copy, dist);
  1051. state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext);
  1052. copy -= dist;
  1053. if (copy) {
  1054. //zmemcpy(state->window, end - copy, copy);
  1055. state.window.set(src.subarray(end - copy, end), 0);
  1056. state.wnext = copy;
  1057. state.whave = state.wsize;
  1058. }
  1059. else {
  1060. state.wnext += dist;
  1061. if (state.wnext === state.wsize) { state.wnext = 0; }
  1062. if (state.whave < state.wsize) { state.whave += dist; }
  1063. }
  1064. }
  1065. return 0;
  1066. };
  1067. const inflate$1 = (strm, flush) => {
  1068. let state;
  1069. let input, output; // input/output buffers
  1070. let next; /* next input INDEX */
  1071. let put; /* next output INDEX */
  1072. let have, left; /* available input and output */
  1073. let hold; /* bit buffer */
  1074. let bits; /* bits in bit buffer */
  1075. let _in, _out; /* save starting available input and output */
  1076. let copy; /* number of stored or match bytes to copy */
  1077. let from; /* where to copy match bytes from */
  1078. let from_source;
  1079. let here = 0; /* current decoding table entry */
  1080. let here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  1081. //let last; /* parent table entry */
  1082. let last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  1083. let len; /* length to copy for repeats, bits to drop */
  1084. let ret; /* return code */
  1085. const hbuf = new Uint8Array(4); /* buffer for gzip header crc calculation */
  1086. let opts;
  1087. let n; // temporary variable for NEED_BITS
  1088. const order = /* permutation of code lengths */
  1089. new Uint8Array([ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]);
  1090. if (inflateStateCheck(strm) || !strm.output ||
  1091. (!strm.input && strm.avail_in !== 0)) {
  1092. return Z_STREAM_ERROR$1;
  1093. }
  1094. state = strm.state;
  1095. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  1096. //--- LOAD() ---
  1097. put = strm.next_out;
  1098. output = strm.output;
  1099. left = strm.avail_out;
  1100. next = strm.next_in;
  1101. input = strm.input;
  1102. have = strm.avail_in;
  1103. hold = state.hold;
  1104. bits = state.bits;
  1105. //---
  1106. _in = have;
  1107. _out = left;
  1108. ret = Z_OK$1;
  1109. inf_leave: // goto emulation
  1110. for (;;) {
  1111. switch (state.mode) {
  1112. case HEAD:
  1113. if (state.wrap === 0) {
  1114. state.mode = TYPEDO;
  1115. break;
  1116. }
  1117. //=== NEEDBITS(16);
  1118. while (bits < 16) {
  1119. if (have === 0) { break inf_leave; }
  1120. have--;
  1121. hold += input[next++] << bits;
  1122. bits += 8;
  1123. }
  1124. //===//
  1125. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  1126. if (state.wbits === 0) {
  1127. state.wbits = 15;
  1128. }
  1129. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  1130. //=== CRC2(state.check, hold);
  1131. hbuf[0] = hold & 0xff;
  1132. hbuf[1] = (hold >>> 8) & 0xff;
  1133. state.check = crc32_1(state.check, hbuf, 2, 0);
  1134. //===//
  1135. //=== INITBITS();
  1136. hold = 0;
  1137. bits = 0;
  1138. //===//
  1139. state.mode = FLAGS;
  1140. break;
  1141. }
  1142. if (state.head) {
  1143. state.head.done = false;
  1144. }
  1145. if (!(state.wrap & 1) || /* check if zlib header allowed */
  1146. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  1147. strm.msg = 'incorrect header check';
  1148. state.mode = BAD;
  1149. break;
  1150. }
  1151. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  1152. strm.msg = 'unknown compression method';
  1153. state.mode = BAD;
  1154. break;
  1155. }
  1156. //--- DROPBITS(4) ---//
  1157. hold >>>= 4;
  1158. bits -= 4;
  1159. //---//
  1160. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  1161. if (state.wbits === 0) {
  1162. state.wbits = len;
  1163. }
  1164. if (len > 15 || len > state.wbits) {
  1165. strm.msg = 'invalid window size';
  1166. state.mode = BAD;
  1167. break;
  1168. }
  1169. // !!! pako patch. Force use `options.windowBits` if passed.
  1170. // Required to always use max window size by default.
  1171. state.dmax = 1 << state.wbits;
  1172. //state.dmax = 1 << len;
  1173. state.flags = 0; /* indicate zlib header */
  1174. //Tracev((stderr, "inflate: zlib header ok\n"));
  1175. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  1176. state.mode = hold & 0x200 ? DICTID : TYPE;
  1177. //=== INITBITS();
  1178. hold = 0;
  1179. bits = 0;
  1180. //===//
  1181. break;
  1182. case FLAGS:
  1183. //=== NEEDBITS(16); */
  1184. while (bits < 16) {
  1185. if (have === 0) { break inf_leave; }
  1186. have--;
  1187. hold += input[next++] << bits;
  1188. bits += 8;
  1189. }
  1190. //===//
  1191. state.flags = hold;
  1192. if ((state.flags & 0xff) !== Z_DEFLATED) {
  1193. strm.msg = 'unknown compression method';
  1194. state.mode = BAD;
  1195. break;
  1196. }
  1197. if (state.flags & 0xe000) {
  1198. strm.msg = 'unknown header flags set';
  1199. state.mode = BAD;
  1200. break;
  1201. }
  1202. if (state.head) {
  1203. state.head.text = ((hold >> 8) & 1);
  1204. }
  1205. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1206. //=== CRC2(state.check, hold);
  1207. hbuf[0] = hold & 0xff;
  1208. hbuf[1] = (hold >>> 8) & 0xff;
  1209. state.check = crc32_1(state.check, hbuf, 2, 0);
  1210. //===//
  1211. }
  1212. //=== INITBITS();
  1213. hold = 0;
  1214. bits = 0;
  1215. //===//
  1216. state.mode = TIME;
  1217. /* falls through */
  1218. case TIME:
  1219. //=== NEEDBITS(32); */
  1220. while (bits < 32) {
  1221. if (have === 0) { break inf_leave; }
  1222. have--;
  1223. hold += input[next++] << bits;
  1224. bits += 8;
  1225. }
  1226. //===//
  1227. if (state.head) {
  1228. state.head.time = hold;
  1229. }
  1230. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1231. //=== CRC4(state.check, hold)
  1232. hbuf[0] = hold & 0xff;
  1233. hbuf[1] = (hold >>> 8) & 0xff;
  1234. hbuf[2] = (hold >>> 16) & 0xff;
  1235. hbuf[3] = (hold >>> 24) & 0xff;
  1236. state.check = crc32_1(state.check, hbuf, 4, 0);
  1237. //===
  1238. }
  1239. //=== INITBITS();
  1240. hold = 0;
  1241. bits = 0;
  1242. //===//
  1243. state.mode = OS;
  1244. /* falls through */
  1245. case OS:
  1246. //=== NEEDBITS(16); */
  1247. while (bits < 16) {
  1248. if (have === 0) { break inf_leave; }
  1249. have--;
  1250. hold += input[next++] << bits;
  1251. bits += 8;
  1252. }
  1253. //===//
  1254. if (state.head) {
  1255. state.head.xflags = (hold & 0xff);
  1256. state.head.os = (hold >> 8);
  1257. }
  1258. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1259. //=== CRC2(state.check, hold);
  1260. hbuf[0] = hold & 0xff;
  1261. hbuf[1] = (hold >>> 8) & 0xff;
  1262. state.check = crc32_1(state.check, hbuf, 2, 0);
  1263. //===//
  1264. }
  1265. //=== INITBITS();
  1266. hold = 0;
  1267. bits = 0;
  1268. //===//
  1269. state.mode = EXLEN;
  1270. /* falls through */
  1271. case EXLEN:
  1272. if (state.flags & 0x0400) {
  1273. //=== NEEDBITS(16); */
  1274. while (bits < 16) {
  1275. if (have === 0) { break inf_leave; }
  1276. have--;
  1277. hold += input[next++] << bits;
  1278. bits += 8;
  1279. }
  1280. //===//
  1281. state.length = hold;
  1282. if (state.head) {
  1283. state.head.extra_len = hold;
  1284. }
  1285. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1286. //=== CRC2(state.check, hold);
  1287. hbuf[0] = hold & 0xff;
  1288. hbuf[1] = (hold >>> 8) & 0xff;
  1289. state.check = crc32_1(state.check, hbuf, 2, 0);
  1290. //===//
  1291. }
  1292. //=== INITBITS();
  1293. hold = 0;
  1294. bits = 0;
  1295. //===//
  1296. }
  1297. else if (state.head) {
  1298. state.head.extra = null/*Z_NULL*/;
  1299. }
  1300. state.mode = EXTRA;
  1301. /* falls through */
  1302. case EXTRA:
  1303. if (state.flags & 0x0400) {
  1304. copy = state.length;
  1305. if (copy > have) { copy = have; }
  1306. if (copy) {
  1307. if (state.head) {
  1308. len = state.head.extra_len - state.length;
  1309. if (!state.head.extra) {
  1310. // Use untyped array for more convenient processing later
  1311. state.head.extra = new Uint8Array(state.head.extra_len);
  1312. }
  1313. state.head.extra.set(
  1314. input.subarray(
  1315. next,
  1316. // extra field is limited to 65536 bytes
  1317. // - no need for additional size check
  1318. next + copy
  1319. ),
  1320. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  1321. len
  1322. );
  1323. //zmemcpy(state.head.extra + len, next,
  1324. // len + copy > state.head.extra_max ?
  1325. // state.head.extra_max - len : copy);
  1326. }
  1327. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1328. state.check = crc32_1(state.check, input, copy, next);
  1329. }
  1330. have -= copy;
  1331. next += copy;
  1332. state.length -= copy;
  1333. }
  1334. if (state.length) { break inf_leave; }
  1335. }
  1336. state.length = 0;
  1337. state.mode = NAME;
  1338. /* falls through */
  1339. case NAME:
  1340. if (state.flags & 0x0800) {
  1341. if (have === 0) { break inf_leave; }
  1342. copy = 0;
  1343. do {
  1344. // TODO: 2 or 1 bytes?
  1345. len = input[next + copy++];
  1346. /* use constant limit because in js we should not preallocate memory */
  1347. if (state.head && len &&
  1348. (state.length < 65536 /*state.head.name_max*/)) {
  1349. state.head.name += String.fromCharCode(len);
  1350. }
  1351. } while (len && copy < have);
  1352. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1353. state.check = crc32_1(state.check, input, copy, next);
  1354. }
  1355. have -= copy;
  1356. next += copy;
  1357. if (len) { break inf_leave; }
  1358. }
  1359. else if (state.head) {
  1360. state.head.name = null;
  1361. }
  1362. state.length = 0;
  1363. state.mode = COMMENT;
  1364. /* falls through */
  1365. case COMMENT:
  1366. if (state.flags & 0x1000) {
  1367. if (have === 0) { break inf_leave; }
  1368. copy = 0;
  1369. do {
  1370. len = input[next + copy++];
  1371. /* use constant limit because in js we should not preallocate memory */
  1372. if (state.head && len &&
  1373. (state.length < 65536 /*state.head.comm_max*/)) {
  1374. state.head.comment += String.fromCharCode(len);
  1375. }
  1376. } while (len && copy < have);
  1377. if ((state.flags & 0x0200) && (state.wrap & 4)) {
  1378. state.check = crc32_1(state.check, input, copy, next);
  1379. }
  1380. have -= copy;
  1381. next += copy;
  1382. if (len) { break inf_leave; }
  1383. }
  1384. else if (state.head) {
  1385. state.head.comment = null;
  1386. }
  1387. state.mode = HCRC;
  1388. /* falls through */
  1389. case HCRC:
  1390. if (state.flags & 0x0200) {
  1391. //=== NEEDBITS(16); */
  1392. while (bits < 16) {
  1393. if (have === 0) { break inf_leave; }
  1394. have--;
  1395. hold += input[next++] << bits;
  1396. bits += 8;
  1397. }
  1398. //===//
  1399. if ((state.wrap & 4) && hold !== (state.check & 0xffff)) {
  1400. strm.msg = 'header crc mismatch';
  1401. state.mode = BAD;
  1402. break;
  1403. }
  1404. //=== INITBITS();
  1405. hold = 0;
  1406. bits = 0;
  1407. //===//
  1408. }
  1409. if (state.head) {
  1410. state.head.hcrc = ((state.flags >> 9) & 1);
  1411. state.head.done = true;
  1412. }
  1413. strm.adler = state.check = 0;
  1414. state.mode = TYPE;
  1415. break;
  1416. case DICTID:
  1417. //=== NEEDBITS(32); */
  1418. while (bits < 32) {
  1419. if (have === 0) { break inf_leave; }
  1420. have--;
  1421. hold += input[next++] << bits;
  1422. bits += 8;
  1423. }
  1424. //===//
  1425. strm.adler = state.check = zswap32(hold);
  1426. //=== INITBITS();
  1427. hold = 0;
  1428. bits = 0;
  1429. //===//
  1430. state.mode = DICT;
  1431. /* falls through */
  1432. case DICT:
  1433. if (state.havedict === 0) {
  1434. //--- RESTORE() ---
  1435. strm.next_out = put;
  1436. strm.avail_out = left;
  1437. strm.next_in = next;
  1438. strm.avail_in = have;
  1439. state.hold = hold;
  1440. state.bits = bits;
  1441. //---
  1442. return Z_NEED_DICT$1;
  1443. }
  1444. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  1445. state.mode = TYPE;
  1446. /* falls through */
  1447. case TYPE:
  1448. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  1449. /* falls through */
  1450. case TYPEDO:
  1451. if (state.last) {
  1452. //--- BYTEBITS() ---//
  1453. hold >>>= bits & 7;
  1454. bits -= bits & 7;
  1455. //---//
  1456. state.mode = CHECK;
  1457. break;
  1458. }
  1459. //=== NEEDBITS(3); */
  1460. while (bits < 3) {
  1461. if (have === 0) { break inf_leave; }
  1462. have--;
  1463. hold += input[next++] << bits;
  1464. bits += 8;
  1465. }
  1466. //===//
  1467. state.last = (hold & 0x01)/*BITS(1)*/;
  1468. //--- DROPBITS(1) ---//
  1469. hold >>>= 1;
  1470. bits -= 1;
  1471. //---//
  1472. switch ((hold & 0x03)/*BITS(2)*/) {
  1473. case 0: /* stored block */
  1474. //Tracev((stderr, "inflate: stored block%s\n",
  1475. // state.last ? " (last)" : ""));
  1476. state.mode = STORED;
  1477. break;
  1478. case 1: /* fixed block */
  1479. fixedtables(state);
  1480. //Tracev((stderr, "inflate: fixed codes block%s\n",
  1481. // state.last ? " (last)" : ""));
  1482. state.mode = LEN_; /* decode codes */
  1483. if (flush === Z_TREES) {
  1484. //--- DROPBITS(2) ---//
  1485. hold >>>= 2;
  1486. bits -= 2;
  1487. //---//
  1488. break inf_leave;
  1489. }
  1490. break;
  1491. case 2: /* dynamic block */
  1492. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  1493. // state.last ? " (last)" : ""));
  1494. state.mode = TABLE;
  1495. break;
  1496. case 3:
  1497. strm.msg = 'invalid block type';
  1498. state.mode = BAD;
  1499. }
  1500. //--- DROPBITS(2) ---//
  1501. hold >>>= 2;
  1502. bits -= 2;
  1503. //---//
  1504. break;
  1505. case STORED:
  1506. //--- BYTEBITS() ---// /* go to byte boundary */
  1507. hold >>>= bits & 7;
  1508. bits -= bits & 7;
  1509. //---//
  1510. //=== NEEDBITS(32); */
  1511. while (bits < 32) {
  1512. if (have === 0) { break inf_leave; }
  1513. have--;
  1514. hold += input[next++] << bits;
  1515. bits += 8;
  1516. }
  1517. //===//
  1518. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  1519. strm.msg = 'invalid stored block lengths';
  1520. state.mode = BAD;
  1521. break;
  1522. }
  1523. state.length = hold & 0xffff;
  1524. //Tracev((stderr, "inflate: stored length %u\n",
  1525. // state.length));
  1526. //=== INITBITS();
  1527. hold = 0;
  1528. bits = 0;
  1529. //===//
  1530. state.mode = COPY_;
  1531. if (flush === Z_TREES) { break inf_leave; }
  1532. /* falls through */
  1533. case COPY_:
  1534. state.mode = COPY;
  1535. /* falls through */
  1536. case COPY:
  1537. copy = state.length;
  1538. if (copy) {
  1539. if (copy > have) { copy = have; }
  1540. if (copy > left) { copy = left; }
  1541. if (copy === 0) { break inf_leave; }
  1542. //--- zmemcpy(put, next, copy); ---
  1543. output.set(input.subarray(next, next + copy), put);
  1544. //---//
  1545. have -= copy;
  1546. next += copy;
  1547. left -= copy;
  1548. put += copy;
  1549. state.length -= copy;
  1550. break;
  1551. }
  1552. //Tracev((stderr, "inflate: stored end\n"));
  1553. state.mode = TYPE;
  1554. break;
  1555. case TABLE:
  1556. //=== NEEDBITS(14); */
  1557. while (bits < 14) {
  1558. if (have === 0) { break inf_leave; }
  1559. have--;
  1560. hold += input[next++] << bits;
  1561. bits += 8;
  1562. }
  1563. //===//
  1564. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  1565. //--- DROPBITS(5) ---//
  1566. hold >>>= 5;
  1567. bits -= 5;
  1568. //---//
  1569. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  1570. //--- DROPBITS(5) ---//
  1571. hold >>>= 5;
  1572. bits -= 5;
  1573. //---//
  1574. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  1575. //--- DROPBITS(4) ---//
  1576. hold >>>= 4;
  1577. bits -= 4;
  1578. //---//
  1579. //#ifndef PKZIP_BUG_WORKAROUND
  1580. if (state.nlen > 286 || state.ndist > 30) {
  1581. strm.msg = 'too many length or distance symbols';
  1582. state.mode = BAD;
  1583. break;
  1584. }
  1585. //#endif
  1586. //Tracev((stderr, "inflate: table sizes ok\n"));
  1587. state.have = 0;
  1588. state.mode = LENLENS;
  1589. /* falls through */
  1590. case LENLENS:
  1591. while (state.have < state.ncode) {
  1592. //=== NEEDBITS(3);
  1593. while (bits < 3) {
  1594. if (have === 0) { break inf_leave; }
  1595. have--;
  1596. hold += input[next++] << bits;
  1597. bits += 8;
  1598. }
  1599. //===//
  1600. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  1601. //--- DROPBITS(3) ---//
  1602. hold >>>= 3;
  1603. bits -= 3;
  1604. //---//
  1605. }
  1606. while (state.have < 19) {
  1607. state.lens[order[state.have++]] = 0;
  1608. }
  1609. // We have separate tables & no pointers. 2 commented lines below not needed.
  1610. //state.next = state.codes;
  1611. //state.lencode = state.next;
  1612. // Switch to use dynamic table
  1613. state.lencode = state.lendyn;
  1614. state.lenbits = 7;
  1615. opts = { bits: state.lenbits };
  1616. ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  1617. state.lenbits = opts.bits;
  1618. if (ret) {
  1619. strm.msg = 'invalid code lengths set';
  1620. state.mode = BAD;
  1621. break;
  1622. }
  1623. //Tracev((stderr, "inflate: code lengths ok\n"));
  1624. state.have = 0;
  1625. state.mode = CODELENS;
  1626. /* falls through */
  1627. case CODELENS:
  1628. while (state.have < state.nlen + state.ndist) {
  1629. for (;;) {
  1630. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  1631. here_bits = here >>> 24;
  1632. here_op = (here >>> 16) & 0xff;
  1633. here_val = here & 0xffff;
  1634. if ((here_bits) <= bits) { break; }
  1635. //--- PULLBYTE() ---//
  1636. if (have === 0) { break inf_leave; }
  1637. have--;
  1638. hold += input[next++] << bits;
  1639. bits += 8;
  1640. //---//
  1641. }
  1642. if (here_val < 16) {
  1643. //--- DROPBITS(here.bits) ---//
  1644. hold >>>= here_bits;
  1645. bits -= here_bits;
  1646. //---//
  1647. state.lens[state.have++] = here_val;
  1648. }
  1649. else {
  1650. if (here_val === 16) {
  1651. //=== NEEDBITS(here.bits + 2);
  1652. n = here_bits + 2;
  1653. while (bits < n) {
  1654. if (have === 0) { break inf_leave; }
  1655. have--;
  1656. hold += input[next++] << bits;
  1657. bits += 8;
  1658. }
  1659. //===//
  1660. //--- DROPBITS(here.bits) ---//
  1661. hold >>>= here_bits;
  1662. bits -= here_bits;
  1663. //---//
  1664. if (state.have === 0) {
  1665. strm.msg = 'invalid bit length repeat';
  1666. state.mode = BAD;
  1667. break;
  1668. }
  1669. len = state.lens[state.have - 1];
  1670. copy = 3 + (hold & 0x03);//BITS(2);
  1671. //--- DROPBITS(2) ---//
  1672. hold >>>= 2;
  1673. bits -= 2;
  1674. //---//
  1675. }
  1676. else if (here_val === 17) {
  1677. //=== NEEDBITS(here.bits + 3);
  1678. n = here_bits + 3;
  1679. while (bits < n) {
  1680. if (have === 0) { break inf_leave; }
  1681. have--;
  1682. hold += input[next++] << bits;
  1683. bits += 8;
  1684. }
  1685. //===//
  1686. //--- DROPBITS(here.bits) ---//
  1687. hold >>>= here_bits;
  1688. bits -= here_bits;
  1689. //---//
  1690. len = 0;
  1691. copy = 3 + (hold & 0x07);//BITS(3);
  1692. //--- DROPBITS(3) ---//
  1693. hold >>>= 3;
  1694. bits -= 3;
  1695. //---//
  1696. }
  1697. else {
  1698. //=== NEEDBITS(here.bits + 7);
  1699. n = here_bits + 7;
  1700. while (bits < n) {
  1701. if (have === 0) { break inf_leave; }
  1702. have--;
  1703. hold += input[next++] << bits;
  1704. bits += 8;
  1705. }
  1706. //===//
  1707. //--- DROPBITS(here.bits) ---//
  1708. hold >>>= here_bits;
  1709. bits -= here_bits;
  1710. //---//
  1711. len = 0;
  1712. copy = 11 + (hold & 0x7f);//BITS(7);
  1713. //--- DROPBITS(7) ---//
  1714. hold >>>= 7;
  1715. bits -= 7;
  1716. //---//
  1717. }
  1718. if (state.have + copy > state.nlen + state.ndist) {
  1719. strm.msg = 'invalid bit length repeat';
  1720. state.mode = BAD;
  1721. break;
  1722. }
  1723. while (copy--) {
  1724. state.lens[state.have++] = len;
  1725. }
  1726. }
  1727. }
  1728. /* handle error breaks in while */
  1729. if (state.mode === BAD) { break; }
  1730. /* check for end-of-block code (better have one) */
  1731. if (state.lens[256] === 0) {
  1732. strm.msg = 'invalid code -- missing end-of-block';
  1733. state.mode = BAD;
  1734. break;
  1735. }
  1736. /* build code tables -- note: do not change the lenbits or distbits
  1737. values here (9 and 6) without reading the comments in inftrees.h
  1738. concerning the ENOUGH constants, which depend on those values */
  1739. state.lenbits = 9;
  1740. opts = { bits: state.lenbits };
  1741. ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  1742. // We have separate tables & no pointers. 2 commented lines below not needed.
  1743. // state.next_index = opts.table_index;
  1744. state.lenbits = opts.bits;
  1745. // state.lencode = state.next;
  1746. if (ret) {
  1747. strm.msg = 'invalid literal/lengths set';
  1748. state.mode = BAD;
  1749. break;
  1750. }
  1751. state.distbits = 6;
  1752. //state.distcode.copy(state.codes);
  1753. // Switch to use dynamic table
  1754. state.distcode = state.distdyn;
  1755. opts = { bits: state.distbits };
  1756. ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  1757. // We have separate tables & no pointers. 2 commented lines below not needed.
  1758. // state.next_index = opts.table_index;
  1759. state.distbits = opts.bits;
  1760. // state.distcode = state.next;
  1761. if (ret) {
  1762. strm.msg = 'invalid distances set';
  1763. state.mode = BAD;
  1764. break;
  1765. }
  1766. //Tracev((stderr, 'inflate: codes ok\n'));
  1767. state.mode = LEN_;
  1768. if (flush === Z_TREES) { break inf_leave; }
  1769. /* falls through */
  1770. case LEN_:
  1771. state.mode = LEN;
  1772. /* falls through */
  1773. case LEN:
  1774. if (have >= 6 && left >= 258) {
  1775. //--- RESTORE() ---
  1776. strm.next_out = put;
  1777. strm.avail_out = left;
  1778. strm.next_in = next;
  1779. strm.avail_in = have;
  1780. state.hold = hold;
  1781. state.bits = bits;
  1782. //---
  1783. inffast(strm, _out);
  1784. //--- LOAD() ---
  1785. put = strm.next_out;
  1786. output = strm.output;
  1787. left = strm.avail_out;
  1788. next = strm.next_in;
  1789. input = strm.input;
  1790. have = strm.avail_in;
  1791. hold = state.hold;
  1792. bits = state.bits;
  1793. //---
  1794. if (state.mode === TYPE) {
  1795. state.back = -1;
  1796. }
  1797. break;
  1798. }
  1799. state.back = 0;
  1800. for (;;) {
  1801. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  1802. here_bits = here >>> 24;
  1803. here_op = (here >>> 16) & 0xff;
  1804. here_val = here & 0xffff;
  1805. if (here_bits <= bits) { break; }
  1806. //--- PULLBYTE() ---//
  1807. if (have === 0) { break inf_leave; }
  1808. have--;
  1809. hold += input[next++] << bits;
  1810. bits += 8;
  1811. //---//
  1812. }
  1813. if (here_op && (here_op & 0xf0) === 0) {
  1814. last_bits = here_bits;
  1815. last_op = here_op;
  1816. last_val = here_val;
  1817. for (;;) {
  1818. here = state.lencode[last_val +
  1819. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  1820. here_bits = here >>> 24;
  1821. here_op = (here >>> 16) & 0xff;
  1822. here_val = here & 0xffff;
  1823. if ((last_bits + here_bits) <= bits) { break; }
  1824. //--- PULLBYTE() ---//
  1825. if (have === 0) { break inf_leave; }
  1826. have--;
  1827. hold += input[next++] << bits;
  1828. bits += 8;
  1829. //---//
  1830. }
  1831. //--- DROPBITS(last.bits) ---//
  1832. hold >>>= last_bits;
  1833. bits -= last_bits;
  1834. //---//
  1835. state.back += last_bits;
  1836. }
  1837. //--- DROPBITS(here.bits) ---//
  1838. hold >>>= here_bits;
  1839. bits -= here_bits;
  1840. //---//
  1841. state.back += here_bits;
  1842. state.length = here_val;
  1843. if (here_op === 0) {
  1844. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  1845. // "inflate: literal '%c'\n" :
  1846. // "inflate: literal 0x%02x\n", here.val));
  1847. state.mode = LIT;
  1848. break;
  1849. }
  1850. if (here_op & 32) {
  1851. //Tracevv((stderr, "inflate: end of block\n"));
  1852. state.back = -1;
  1853. state.mode = TYPE;
  1854. break;
  1855. }
  1856. if (here_op & 64) {
  1857. strm.msg = 'invalid literal/length code';
  1858. state.mode = BAD;
  1859. break;
  1860. }
  1861. state.extra = here_op & 15;
  1862. state.mode = LENEXT;
  1863. /* falls through */
  1864. case LENEXT:
  1865. if (state.extra) {
  1866. //=== NEEDBITS(state.extra);
  1867. n = state.extra;
  1868. while (bits < n) {
  1869. if (have === 0) { break inf_leave; }
  1870. have--;
  1871. hold += input[next++] << bits;
  1872. bits += 8;
  1873. }
  1874. //===//
  1875. state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  1876. //--- DROPBITS(state.extra) ---//
  1877. hold >>>= state.extra;
  1878. bits -= state.extra;
  1879. //---//
  1880. state.back += state.extra;
  1881. }
  1882. //Tracevv((stderr, "inflate: length %u\n", state.length));
  1883. state.was = state.length;
  1884. state.mode = DIST;
  1885. /* falls through */
  1886. case DIST:
  1887. for (;;) {
  1888. here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/
  1889. here_bits = here >>> 24;
  1890. here_op = (here >>> 16) & 0xff;
  1891. here_val = here & 0xffff;
  1892. if ((here_bits) <= bits) { break; }
  1893. //--- PULLBYTE() ---//
  1894. if (have === 0) { break inf_leave; }
  1895. have--;
  1896. hold += input[next++] << bits;
  1897. bits += 8;
  1898. //---//
  1899. }
  1900. if ((here_op & 0xf0) === 0) {
  1901. last_bits = here_bits;
  1902. last_op = here_op;
  1903. last_val = here_val;
  1904. for (;;) {
  1905. here = state.distcode[last_val +
  1906. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  1907. here_bits = here >>> 24;
  1908. here_op = (here >>> 16) & 0xff;
  1909. here_val = here & 0xffff;
  1910. if ((last_bits + here_bits) <= bits) { break; }
  1911. //--- PULLBYTE() ---//
  1912. if (have === 0) { break inf_leave; }
  1913. have--;
  1914. hold += input[next++] << bits;
  1915. bits += 8;
  1916. //---//
  1917. }
  1918. //--- DROPBITS(last.bits) ---//
  1919. hold >>>= last_bits;
  1920. bits -= last_bits;
  1921. //---//
  1922. state.back += last_bits;
  1923. }
  1924. //--- DROPBITS(here.bits) ---//
  1925. hold >>>= here_bits;
  1926. bits -= here_bits;
  1927. //---//
  1928. state.back += here_bits;
  1929. if (here_op & 64) {
  1930. strm.msg = 'invalid distance code';
  1931. state.mode = BAD;
  1932. break;
  1933. }
  1934. state.offset = here_val;
  1935. state.extra = (here_op) & 15;
  1936. state.mode = DISTEXT;
  1937. /* falls through */
  1938. case DISTEXT:
  1939. if (state.extra) {
  1940. //=== NEEDBITS(state.extra);
  1941. n = state.extra;
  1942. while (bits < n) {
  1943. if (have === 0) { break inf_leave; }
  1944. have--;
  1945. hold += input[next++] << bits;
  1946. bits += 8;
  1947. }
  1948. //===//
  1949. state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  1950. //--- DROPBITS(state.extra) ---//
  1951. hold >>>= state.extra;
  1952. bits -= state.extra;
  1953. //---//
  1954. state.back += state.extra;
  1955. }
  1956. //#ifdef INFLATE_STRICT
  1957. if (state.offset > state.dmax) {
  1958. strm.msg = 'invalid distance too far back';
  1959. state.mode = BAD;
  1960. break;
  1961. }
  1962. //#endif
  1963. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  1964. state.mode = MATCH;
  1965. /* falls through */
  1966. case MATCH:
  1967. if (left === 0) { break inf_leave; }
  1968. copy = _out - left;
  1969. if (state.offset > copy) { /* copy from window */
  1970. copy = state.offset - copy;
  1971. if (copy > state.whave) {
  1972. if (state.sane) {
  1973. strm.msg = 'invalid distance too far back';
  1974. state.mode = BAD;
  1975. break;
  1976. }
  1977. // (!) This block is disabled in zlib defaults,
  1978. // don't enable it for binary compatibility
  1979. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1980. // Trace((stderr, "inflate.c too far\n"));
  1981. // copy -= state.whave;
  1982. // if (copy > state.length) { copy = state.length; }
  1983. // if (copy > left) { copy = left; }
  1984. // left -= copy;
  1985. // state.length -= copy;
  1986. // do {
  1987. // output[put++] = 0;
  1988. // } while (--copy);
  1989. // if (state.length === 0) { state.mode = LEN; }
  1990. // break;
  1991. //#endif
  1992. }
  1993. if (copy > state.wnext) {
  1994. copy -= state.wnext;
  1995. from = state.wsize - copy;
  1996. }
  1997. else {
  1998. from = state.wnext - copy;
  1999. }
  2000. if (copy > state.length) { copy = state.length; }
  2001. from_source = state.window;
  2002. }
  2003. else { /* copy from output */
  2004. from_source = output;
  2005. from = put - state.offset;
  2006. copy = state.length;
  2007. }
  2008. if (copy > left) { copy = left; }
  2009. left -= copy;
  2010. state.length -= copy;
  2011. do {
  2012. output[put++] = from_source[from++];
  2013. } while (--copy);
  2014. if (state.length === 0) { state.mode = LEN; }
  2015. break;
  2016. case LIT:
  2017. if (left === 0) { break inf_leave; }
  2018. output[put++] = state.length;
  2019. left--;
  2020. state.mode = LEN;
  2021. break;
  2022. case CHECK:
  2023. if (state.wrap) {
  2024. //=== NEEDBITS(32);
  2025. while (bits < 32) {
  2026. if (have === 0) { break inf_leave; }
  2027. have--;
  2028. // Use '|' instead of '+' to make sure that result is signed
  2029. hold |= input[next++] << bits;
  2030. bits += 8;
  2031. }
  2032. //===//
  2033. _out -= left;
  2034. strm.total_out += _out;
  2035. state.total += _out;
  2036. if ((state.wrap & 4) && _out) {
  2037. strm.adler = state.check =
  2038. /*UPDATE_CHECK(state.check, put - _out, _out);*/
  2039. (state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out));
  2040. }
  2041. _out = left;
  2042. // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  2043. if ((state.wrap & 4) && (state.flags ? hold : zswap32(hold)) !== state.check) {
  2044. strm.msg = 'incorrect data check';
  2045. state.mode = BAD;
  2046. break;
  2047. }
  2048. //=== INITBITS();
  2049. hold = 0;
  2050. bits = 0;
  2051. //===//
  2052. //Tracev((stderr, "inflate: check matches trailer\n"));
  2053. }
  2054. state.mode = LENGTH;
  2055. /* falls through */
  2056. case LENGTH:
  2057. if (state.wrap && state.flags) {
  2058. //=== NEEDBITS(32);
  2059. while (bits < 32) {
  2060. if (have === 0) { break inf_leave; }
  2061. have--;
  2062. hold += input[next++] << bits;
  2063. bits += 8;
  2064. }
  2065. //===//
  2066. if ((state.wrap & 4) && hold !== (state.total & 0xffffffff)) {
  2067. strm.msg = 'incorrect length check';
  2068. state.mode = BAD;
  2069. break;
  2070. }
  2071. //=== INITBITS();
  2072. hold = 0;
  2073. bits = 0;
  2074. //===//
  2075. //Tracev((stderr, "inflate: length matches trailer\n"));
  2076. }
  2077. state.mode = DONE;
  2078. /* falls through */
  2079. case DONE:
  2080. ret = Z_STREAM_END$1;
  2081. break inf_leave;
  2082. case BAD:
  2083. ret = Z_DATA_ERROR$1;
  2084. break inf_leave;
  2085. case MEM:
  2086. return Z_MEM_ERROR$1;
  2087. case SYNC:
  2088. /* falls through */
  2089. default:
  2090. return Z_STREAM_ERROR$1;
  2091. }
  2092. }
  2093. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  2094. /*
  2095. Return from inflate(), updating the total counts and the check value.
  2096. If there was no progress during the inflate() call, return a buffer
  2097. error. Call updatewindow() to create and/or update the window state.
  2098. Note: a memory error from inflate() is non-recoverable.
  2099. */
  2100. //--- RESTORE() ---
  2101. strm.next_out = put;
  2102. strm.avail_out = left;
  2103. strm.next_in = next;
  2104. strm.avail_in = have;
  2105. state.hold = hold;
  2106. state.bits = bits;
  2107. //---
  2108. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  2109. (state.mode < CHECK || flush !== Z_FINISH$1))) {
  2110. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ;
  2111. }
  2112. _in -= strm.avail_in;
  2113. _out -= strm.avail_out;
  2114. strm.total_in += _in;
  2115. strm.total_out += _out;
  2116. state.total += _out;
  2117. if ((state.wrap & 4) && _out) {
  2118. strm.adler = state.check = /*UPDATE_CHECK(state.check, strm.next_out - _out, _out);*/
  2119. (state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out));
  2120. }
  2121. strm.data_type = state.bits + (state.last ? 64 : 0) +
  2122. (state.mode === TYPE ? 128 : 0) +
  2123. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  2124. if (((_in === 0 && _out === 0) || flush === Z_FINISH$1) && ret === Z_OK$1) {
  2125. ret = Z_BUF_ERROR;
  2126. }
  2127. return ret;
  2128. };
  2129. const inflateEnd = (strm) => {
  2130. if (inflateStateCheck(strm)) {
  2131. return Z_STREAM_ERROR$1;
  2132. }
  2133. let state = strm.state;
  2134. if (state.window) {
  2135. state.window = null;
  2136. }
  2137. strm.state = null;
  2138. return Z_OK$1;
  2139. };
  2140. const inflateGetHeader = (strm, head) => {
  2141. /* check state */
  2142. if (inflateStateCheck(strm)) { return Z_STREAM_ERROR$1; }
  2143. const state = strm.state;
  2144. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR$1; }
  2145. /* save header structure */
  2146. state.head = head;
  2147. head.done = false;
  2148. return Z_OK$1;
  2149. };
  2150. const inflateSetDictionary = (strm, dictionary) => {
  2151. const dictLength = dictionary.length;
  2152. let state;
  2153. let dictid;
  2154. let ret;
  2155. /* check state */
  2156. if (inflateStateCheck(strm)) { return Z_STREAM_ERROR$1; }
  2157. state = strm.state;
  2158. if (state.wrap !== 0 && state.mode !== DICT) {
  2159. return Z_STREAM_ERROR$1;
  2160. }
  2161. /* check for correct dictionary identifier */
  2162. if (state.mode === DICT) {
  2163. dictid = 1; /* adler32(0, null, 0)*/
  2164. /* dictid = adler32(dictid, dictionary, dictLength); */
  2165. dictid = adler32_1(dictid, dictionary, dictLength, 0);
  2166. if (dictid !== state.check) {
  2167. return Z_DATA_ERROR$1;
  2168. }
  2169. }
  2170. /* copy dictionary to window using updatewindow(), which will amend the
  2171. existing dictionary if appropriate */
  2172. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  2173. if (ret) {
  2174. state.mode = MEM;
  2175. return Z_MEM_ERROR$1;
  2176. }
  2177. state.havedict = 1;
  2178. // Tracev((stderr, "inflate: dictionary set\n"));
  2179. return Z_OK$1;
  2180. };
  2181. var inflateReset_1 = inflateReset;
  2182. var inflateReset2_1 = inflateReset2;
  2183. var inflateResetKeep_1 = inflateResetKeep;
  2184. var inflateInit_1 = inflateInit;
  2185. var inflateInit2_1 = inflateInit2;
  2186. var inflate_2$1 = inflate$1;
  2187. var inflateEnd_1 = inflateEnd;
  2188. var inflateGetHeader_1 = inflateGetHeader;
  2189. var inflateSetDictionary_1 = inflateSetDictionary;
  2190. var inflateInfo = 'pako inflate (from Nodeca project)';
  2191. /* Not implemented
  2192. module.exports.inflateCodesUsed = inflateCodesUsed;
  2193. module.exports.inflateCopy = inflateCopy;
  2194. module.exports.inflateGetDictionary = inflateGetDictionary;
  2195. module.exports.inflateMark = inflateMark;
  2196. module.exports.inflatePrime = inflatePrime;
  2197. module.exports.inflateSync = inflateSync;
  2198. module.exports.inflateSyncPoint = inflateSyncPoint;
  2199. module.exports.inflateUndermine = inflateUndermine;
  2200. module.exports.inflateValidate = inflateValidate;
  2201. */
  2202. var inflate_1$1 = {
  2203. inflateReset: inflateReset_1,
  2204. inflateReset2: inflateReset2_1,
  2205. inflateResetKeep: inflateResetKeep_1,
  2206. inflateInit: inflateInit_1,
  2207. inflateInit2: inflateInit2_1,
  2208. inflate: inflate_2$1,
  2209. inflateEnd: inflateEnd_1,
  2210. inflateGetHeader: inflateGetHeader_1,
  2211. inflateSetDictionary: inflateSetDictionary_1,
  2212. inflateInfo: inflateInfo
  2213. };
  2214. const _has = (obj, key) => {
  2215. return Object.prototype.hasOwnProperty.call(obj, key);
  2216. };
  2217. var assign = function (obj /*from1, from2, from3, ...*/) {
  2218. const sources = Array.prototype.slice.call(arguments, 1);
  2219. while (sources.length) {
  2220. const source = sources.shift();
  2221. if (!source) { continue; }
  2222. if (typeof source !== 'object') {
  2223. throw new TypeError(source + 'must be non-object');
  2224. }
  2225. for (const p in source) {
  2226. if (_has(source, p)) {
  2227. obj[p] = source[p];
  2228. }
  2229. }
  2230. }
  2231. return obj;
  2232. };
  2233. // Join array of chunks to single array.
  2234. var flattenChunks = (chunks) => {
  2235. // calculate data length
  2236. let len = 0;
  2237. for (let i = 0, l = chunks.length; i < l; i++) {
  2238. len += chunks[i].length;
  2239. }
  2240. // join chunks
  2241. const result = new Uint8Array(len);
  2242. for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
  2243. let chunk = chunks[i];
  2244. result.set(chunk, pos);
  2245. pos += chunk.length;
  2246. }
  2247. return result;
  2248. };
  2249. var common = {
  2250. assign: assign,
  2251. flattenChunks: flattenChunks
  2252. };
  2253. // String encode/decode helpers
  2254. // Quick check if we can use fast array to bin string conversion
  2255. //
  2256. // - apply(Array) can fail on Android 2.2
  2257. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  2258. //
  2259. let STR_APPLY_UIA_OK = true;
  2260. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  2261. // Table with utf8 lengths (calculated by first byte of sequence)
  2262. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  2263. // because max possible codepoint is 0x10ffff
  2264. const _utf8len = new Uint8Array(256);
  2265. for (let q = 0; q < 256; q++) {
  2266. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  2267. }
  2268. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  2269. // convert string to array (typed, when possible)
  2270. var string2buf = (str) => {
  2271. if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
  2272. return new TextEncoder().encode(str);
  2273. }
  2274. let buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  2275. // count binary size
  2276. for (m_pos = 0; m_pos < str_len; m_pos++) {
  2277. c = str.charCodeAt(m_pos);
  2278. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  2279. c2 = str.charCodeAt(m_pos + 1);
  2280. if ((c2 & 0xfc00) === 0xdc00) {
  2281. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2282. m_pos++;
  2283. }
  2284. }
  2285. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  2286. }
  2287. // allocate buffer
  2288. buf = new Uint8Array(buf_len);
  2289. // convert
  2290. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  2291. c = str.charCodeAt(m_pos);
  2292. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  2293. c2 = str.charCodeAt(m_pos + 1);
  2294. if ((c2 & 0xfc00) === 0xdc00) {
  2295. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2296. m_pos++;
  2297. }
  2298. }
  2299. if (c < 0x80) {
  2300. /* one byte */
  2301. buf[i++] = c;
  2302. } else if (c < 0x800) {
  2303. /* two bytes */
  2304. buf[i++] = 0xC0 | (c >>> 6);
  2305. buf[i++] = 0x80 | (c & 0x3f);
  2306. } else if (c < 0x10000) {
  2307. /* three bytes */
  2308. buf[i++] = 0xE0 | (c >>> 12);
  2309. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  2310. buf[i++] = 0x80 | (c & 0x3f);
  2311. } else {
  2312. /* four bytes */
  2313. buf[i++] = 0xf0 | (c >>> 18);
  2314. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  2315. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  2316. buf[i++] = 0x80 | (c & 0x3f);
  2317. }
  2318. }
  2319. return buf;
  2320. };
  2321. // Helper
  2322. const buf2binstring = (buf, len) => {
  2323. // On Chrome, the arguments in a function call that are allowed is `65534`.
  2324. // If the length of the buffer is smaller than that, we can use this optimization,
  2325. // otherwise we will take a slower path.
  2326. if (len < 65534) {
  2327. if (buf.subarray && STR_APPLY_UIA_OK) {
  2328. return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
  2329. }
  2330. }
  2331. let result = '';
  2332. for (let i = 0; i < len; i++) {
  2333. result += String.fromCharCode(buf[i]);
  2334. }
  2335. return result;
  2336. };
  2337. // convert array to string
  2338. var buf2string = (buf, max) => {
  2339. const len = max || buf.length;
  2340. if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) {
  2341. return new TextDecoder().decode(buf.subarray(0, max));
  2342. }
  2343. let i, out;
  2344. // Reserve max possible length (2 words per char)
  2345. // NB: by unknown reasons, Array is significantly faster for
  2346. // String.fromCharCode.apply than Uint16Array.
  2347. const utf16buf = new Array(len * 2);
  2348. for (out = 0, i = 0; i < len;) {
  2349. let c = buf[i++];
  2350. // quick process ascii
  2351. if (c < 0x80) { utf16buf[out++] = c; continue; }
  2352. let c_len = _utf8len[c];
  2353. // skip 5 & 6 byte codes
  2354. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  2355. // apply mask on first byte
  2356. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  2357. // join the rest
  2358. while (c_len > 1 && i < len) {
  2359. c = (c << 6) | (buf[i++] & 0x3f);
  2360. c_len--;
  2361. }
  2362. // terminated by end of string?
  2363. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  2364. if (c < 0x10000) {
  2365. utf16buf[out++] = c;
  2366. } else {
  2367. c -= 0x10000;
  2368. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  2369. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  2370. }
  2371. }
  2372. return buf2binstring(utf16buf, out);
  2373. };
  2374. // Calculate max possible position in utf8 buffer,
  2375. // that will not break sequence. If that's not possible
  2376. // - (very small limits) return max size as is.
  2377. //
  2378. // buf[] - utf8 bytes array
  2379. // max - length limit (mandatory);
  2380. var utf8border = (buf, max) => {
  2381. max = max || buf.length;
  2382. if (max > buf.length) { max = buf.length; }
  2383. // go back from last position, until start of sequence found
  2384. let pos = max - 1;
  2385. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  2386. // Very small and broken sequence,
  2387. // return max, because we should return something anyway.
  2388. if (pos < 0) { return max; }
  2389. // If we came to start of buffer - that means buffer is too small,
  2390. // return max too.
  2391. if (pos === 0) { return max; }
  2392. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  2393. };
  2394. var strings = {
  2395. string2buf: string2buf,
  2396. buf2string: buf2string,
  2397. utf8border: utf8border
  2398. };
  2399. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2400. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2401. //
  2402. // This software is provided 'as-is', without any express or implied
  2403. // warranty. In no event will the authors be held liable for any damages
  2404. // arising from the use of this software.
  2405. //
  2406. // Permission is granted to anyone to use this software for any purpose,
  2407. // including commercial applications, and to alter it and redistribute it
  2408. // freely, subject to the following restrictions:
  2409. //
  2410. // 1. The origin of this software must not be misrepresented; you must not
  2411. // claim that you wrote the original software. If you use this software
  2412. // in a product, an acknowledgment in the product documentation would be
  2413. // appreciated but is not required.
  2414. // 2. Altered source versions must be plainly marked as such, and must not be
  2415. // misrepresented as being the original software.
  2416. // 3. This notice may not be removed or altered from any source distribution.
  2417. var messages = {
  2418. 2: 'need dictionary', /* Z_NEED_DICT 2 */
  2419. 1: 'stream end', /* Z_STREAM_END 1 */
  2420. 0: '', /* Z_OK 0 */
  2421. '-1': 'file error', /* Z_ERRNO (-1) */
  2422. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  2423. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  2424. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  2425. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  2426. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  2427. };
  2428. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2429. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2430. //
  2431. // This software is provided 'as-is', without any express or implied
  2432. // warranty. In no event will the authors be held liable for any damages
  2433. // arising from the use of this software.
  2434. //
  2435. // Permission is granted to anyone to use this software for any purpose,
  2436. // including commercial applications, and to alter it and redistribute it
  2437. // freely, subject to the following restrictions:
  2438. //
  2439. // 1. The origin of this software must not be misrepresented; you must not
  2440. // claim that you wrote the original software. If you use this software
  2441. // in a product, an acknowledgment in the product documentation would be
  2442. // appreciated but is not required.
  2443. // 2. Altered source versions must be plainly marked as such, and must not be
  2444. // misrepresented as being the original software.
  2445. // 3. This notice may not be removed or altered from any source distribution.
  2446. function ZStream() {
  2447. /* next input byte */
  2448. this.input = null; // JS specific, because we have no pointers
  2449. this.next_in = 0;
  2450. /* number of bytes available at input */
  2451. this.avail_in = 0;
  2452. /* total number of input bytes read so far */
  2453. this.total_in = 0;
  2454. /* next output byte should be put there */
  2455. this.output = null; // JS specific, because we have no pointers
  2456. this.next_out = 0;
  2457. /* remaining free space at output */
  2458. this.avail_out = 0;
  2459. /* total number of bytes output so far */
  2460. this.total_out = 0;
  2461. /* last error message, NULL if no error */
  2462. this.msg = ''/*Z_NULL*/;
  2463. /* not visible by applications */
  2464. this.state = null;
  2465. /* best guess about the data type: binary or text */
  2466. this.data_type = 2/*Z_UNKNOWN*/;
  2467. /* adler32 value of the uncompressed data */
  2468. this.adler = 0;
  2469. }
  2470. var zstream = ZStream;
  2471. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2472. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2473. //
  2474. // This software is provided 'as-is', without any express or implied
  2475. // warranty. In no event will the authors be held liable for any damages
  2476. // arising from the use of this software.
  2477. //
  2478. // Permission is granted to anyone to use this software for any purpose,
  2479. // including commercial applications, and to alter it and redistribute it
  2480. // freely, subject to the following restrictions:
  2481. //
  2482. // 1. The origin of this software must not be misrepresented; you must not
  2483. // claim that you wrote the original software. If you use this software
  2484. // in a product, an acknowledgment in the product documentation would be
  2485. // appreciated but is not required.
  2486. // 2. Altered source versions must be plainly marked as such, and must not be
  2487. // misrepresented as being the original software.
  2488. // 3. This notice may not be removed or altered from any source distribution.
  2489. function GZheader() {
  2490. /* true if compressed data believed to be text */
  2491. this.text = 0;
  2492. /* modification time */
  2493. this.time = 0;
  2494. /* extra flags (not used when writing a gzip file) */
  2495. this.xflags = 0;
  2496. /* operating system */
  2497. this.os = 0;
  2498. /* pointer to extra field or Z_NULL if none */
  2499. this.extra = null;
  2500. /* extra field length (valid if extra != Z_NULL) */
  2501. this.extra_len = 0; // Actually, we don't need it in JS,
  2502. // but leave for few code modifications
  2503. //
  2504. // Setup limits is not necessary because in js we should not preallocate memory
  2505. // for inflate use constant limit in 65536 bytes
  2506. //
  2507. /* space at extra (only when reading header) */
  2508. // this.extra_max = 0;
  2509. /* pointer to zero-terminated file name or Z_NULL */
  2510. this.name = '';
  2511. /* space at name (only when reading header) */
  2512. // this.name_max = 0;
  2513. /* pointer to zero-terminated comment or Z_NULL */
  2514. this.comment = '';
  2515. /* space at comment (only when reading header) */
  2516. // this.comm_max = 0;
  2517. /* true if there was or will be a header crc */
  2518. this.hcrc = 0;
  2519. /* true when done reading gzip header (not used when writing a gzip file) */
  2520. this.done = false;
  2521. }
  2522. var gzheader = GZheader;
  2523. const toString = Object.prototype.toString;
  2524. /* Public constants ==========================================================*/
  2525. /* ===========================================================================*/
  2526. const {
  2527. Z_NO_FLUSH, Z_FINISH,
  2528. Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR
  2529. } = constants$1;
  2530. /* ===========================================================================*/
  2531. /**
  2532. * class Inflate
  2533. *
  2534. * Generic JS-style wrapper for zlib calls. If you don't need
  2535. * streaming behaviour - use more simple functions: [[inflate]]
  2536. * and [[inflateRaw]].
  2537. **/
  2538. /* internal
  2539. * inflate.chunks -> Array
  2540. *
  2541. * Chunks of output data, if [[Inflate#onData]] not overridden.
  2542. **/
  2543. /**
  2544. * Inflate.result -> Uint8Array|String
  2545. *
  2546. * Uncompressed result, generated by default [[Inflate#onData]]
  2547. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  2548. * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
  2549. **/
  2550. /**
  2551. * Inflate.err -> Number
  2552. *
  2553. * Error code after inflate finished. 0 (Z_OK) on success.
  2554. * Should be checked if broken data possible.
  2555. **/
  2556. /**
  2557. * Inflate.msg -> String
  2558. *
  2559. * Error message, if [[Inflate.err]] != 0
  2560. **/
  2561. /**
  2562. * new Inflate(options)
  2563. * - options (Object): zlib inflate options.
  2564. *
  2565. * Creates new inflator instance with specified params. Throws exception
  2566. * on bad params. Supported options:
  2567. *
  2568. * - `windowBits`
  2569. * - `dictionary`
  2570. *
  2571. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2572. * for more information on these.
  2573. *
  2574. * Additional options, for internal needs:
  2575. *
  2576. * - `chunkSize` - size of generated data chunks (16K by default)
  2577. * - `raw` (Boolean) - do raw inflate
  2578. * - `to` (String) - if equal to 'string', then result will be converted
  2579. * from utf8 to utf16 (javascript) string. When string output requested,
  2580. * chunk length can differ from `chunkSize`, depending on content.
  2581. *
  2582. * By default, when no options set, autodetect deflate/gzip data format via
  2583. * wrapper header.
  2584. *
  2585. * ##### Example:
  2586. *
  2587. * ```javascript
  2588. * const pako = require('pako')
  2589. * const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  2590. * const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  2591. *
  2592. * const inflate = new pako.Inflate({ level: 3});
  2593. *
  2594. * inflate.push(chunk1, false);
  2595. * inflate.push(chunk2, true); // true -> last chunk
  2596. *
  2597. * if (inflate.err) { throw new Error(inflate.err); }
  2598. *
  2599. * console.log(inflate.result);
  2600. * ```
  2601. **/
  2602. function Inflate(options) {
  2603. this.options = common.assign({
  2604. chunkSize: 1024 * 64,
  2605. windowBits: 15,
  2606. to: ''
  2607. }, options || {});
  2608. const opt = this.options;
  2609. // Force window size for `raw` data, if not set directly,
  2610. // because we have no header for autodetect.
  2611. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  2612. opt.windowBits = -opt.windowBits;
  2613. if (opt.windowBits === 0) { opt.windowBits = -15; }
  2614. }
  2615. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  2616. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  2617. !(options && options.windowBits)) {
  2618. opt.windowBits += 32;
  2619. }
  2620. // Gzip header has no info about windows size, we can do autodetect only
  2621. // for deflate. So, if window size not set, force it to max when gzip possible
  2622. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  2623. // bit 3 (16) -> gzipped data
  2624. // bit 4 (32) -> autodetect gzip/deflate
  2625. if ((opt.windowBits & 15) === 0) {
  2626. opt.windowBits |= 15;
  2627. }
  2628. }
  2629. this.err = 0; // error code, if happens (0 = Z_OK)
  2630. this.msg = ''; // error message
  2631. this.ended = false; // used to avoid multiple onEnd() calls
  2632. this.chunks = []; // chunks of compressed data
  2633. this.strm = new zstream();
  2634. this.strm.avail_out = 0;
  2635. let status = inflate_1$1.inflateInit2(
  2636. this.strm,
  2637. opt.windowBits
  2638. );
  2639. if (status !== Z_OK) {
  2640. throw new Error(messages[status]);
  2641. }
  2642. this.header = new gzheader();
  2643. inflate_1$1.inflateGetHeader(this.strm, this.header);
  2644. // Setup dictionary
  2645. if (opt.dictionary) {
  2646. // Convert data if needed
  2647. if (typeof opt.dictionary === 'string') {
  2648. opt.dictionary = strings.string2buf(opt.dictionary);
  2649. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  2650. opt.dictionary = new Uint8Array(opt.dictionary);
  2651. }
  2652. if (opt.raw) { //In raw mode we need to set the dictionary early
  2653. status = inflate_1$1.inflateSetDictionary(this.strm, opt.dictionary);
  2654. if (status !== Z_OK) {
  2655. throw new Error(messages[status]);
  2656. }
  2657. }
  2658. }
  2659. }
  2660. /**
  2661. * Inflate#push(data[, flush_mode]) -> Boolean
  2662. * - data (Uint8Array|ArrayBuffer): input data
  2663. * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE
  2664. * flush modes. See constants. Skipped or `false` means Z_NO_FLUSH,
  2665. * `true` means Z_FINISH.
  2666. *
  2667. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  2668. * new output chunks. Returns `true` on success. If end of stream detected,
  2669. * [[Inflate#onEnd]] will be called.
  2670. *
  2671. * `flush_mode` is not needed for normal operation, because end of stream
  2672. * detected automatically. You may try to use it for advanced things, but
  2673. * this functionality was not tested.
  2674. *
  2675. * On fail call [[Inflate#onEnd]] with error code and return false.
  2676. *
  2677. * ##### Example
  2678. *
  2679. * ```javascript
  2680. * push(chunk, false); // push one of data chunks
  2681. * ...
  2682. * push(chunk, true); // push last chunk
  2683. * ```
  2684. **/
  2685. Inflate.prototype.push = function (data, flush_mode) {
  2686. const strm = this.strm;
  2687. const chunkSize = this.options.chunkSize;
  2688. const dictionary = this.options.dictionary;
  2689. let status, _flush_mode, last_avail_out;
  2690. if (this.ended) return false;
  2691. if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
  2692. else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH;
  2693. // Convert data if needed
  2694. if (toString.call(data) === '[object ArrayBuffer]') {
  2695. strm.input = new Uint8Array(data);
  2696. } else {
  2697. strm.input = data;
  2698. }
  2699. strm.next_in = 0;
  2700. strm.avail_in = strm.input.length;
  2701. for (;;) {
  2702. if (strm.avail_out === 0) {
  2703. strm.output = new Uint8Array(chunkSize);
  2704. strm.next_out = 0;
  2705. strm.avail_out = chunkSize;
  2706. }
  2707. status = inflate_1$1.inflate(strm, _flush_mode);
  2708. if (status === Z_NEED_DICT && dictionary) {
  2709. status = inflate_1$1.inflateSetDictionary(strm, dictionary);
  2710. if (status === Z_OK) {
  2711. status = inflate_1$1.inflate(strm, _flush_mode);
  2712. } else if (status === Z_DATA_ERROR) {
  2713. // Replace code with more verbose
  2714. status = Z_NEED_DICT;
  2715. }
  2716. }
  2717. // Skip snyc markers if more data follows and not raw mode
  2718. while (strm.avail_in > 0 &&
  2719. status === Z_STREAM_END &&
  2720. strm.state.wrap > 0 &&
  2721. data[strm.next_in] !== 0)
  2722. {
  2723. inflate_1$1.inflateReset(strm);
  2724. status = inflate_1$1.inflate(strm, _flush_mode);
  2725. }
  2726. switch (status) {
  2727. case Z_STREAM_ERROR:
  2728. case Z_DATA_ERROR:
  2729. case Z_NEED_DICT:
  2730. case Z_MEM_ERROR:
  2731. this.onEnd(status);
  2732. this.ended = true;
  2733. return false;
  2734. }
  2735. // Remember real `avail_out` value, because we may patch out buffer content
  2736. // to align utf8 strings boundaries.
  2737. last_avail_out = strm.avail_out;
  2738. if (strm.next_out) {
  2739. if (strm.avail_out === 0 || status === Z_STREAM_END) {
  2740. if (this.options.to === 'string') {
  2741. let next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  2742. let tail = strm.next_out - next_out_utf8;
  2743. let utf8str = strings.buf2string(strm.output, next_out_utf8);
  2744. // move tail & realign counters
  2745. strm.next_out = tail;
  2746. strm.avail_out = chunkSize - tail;
  2747. if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0);
  2748. this.onData(utf8str);
  2749. } else {
  2750. this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out));
  2751. }
  2752. }
  2753. }
  2754. // Must repeat iteration if out buffer is full
  2755. if (status === Z_OK && last_avail_out === 0) continue;
  2756. // Finalize if end of stream reached.
  2757. if (status === Z_STREAM_END) {
  2758. status = inflate_1$1.inflateEnd(this.strm);
  2759. this.onEnd(status);
  2760. this.ended = true;
  2761. return true;
  2762. }
  2763. if (strm.avail_in === 0) break;
  2764. }
  2765. return true;
  2766. };
  2767. /**
  2768. * Inflate#onData(chunk) -> Void
  2769. * - chunk (Uint8Array|String): output data. When string output requested,
  2770. * each chunk will be string.
  2771. *
  2772. * By default, stores data blocks in `chunks[]` property and glue
  2773. * those in `onEnd`. Override this handler, if you need another behaviour.
  2774. **/
  2775. Inflate.prototype.onData = function (chunk) {
  2776. this.chunks.push(chunk);
  2777. };
  2778. /**
  2779. * Inflate#onEnd(status) -> Void
  2780. * - status (Number): inflate status. 0 (Z_OK) on success,
  2781. * other if not.
  2782. *
  2783. * Called either after you tell inflate that the input stream is
  2784. * complete (Z_FINISH). By default - join collected chunks,
  2785. * free memory and fill `results` / `err` properties.
  2786. **/
  2787. Inflate.prototype.onEnd = function (status) {
  2788. // On success - join
  2789. if (status === Z_OK) {
  2790. if (this.options.to === 'string') {
  2791. this.result = this.chunks.join('');
  2792. } else {
  2793. this.result = common.flattenChunks(this.chunks);
  2794. }
  2795. }
  2796. this.chunks = [];
  2797. this.err = status;
  2798. this.msg = this.strm.msg;
  2799. };
  2800. /**
  2801. * inflate(data[, options]) -> Uint8Array|String
  2802. * - data (Uint8Array|ArrayBuffer): input data to decompress.
  2803. * - options (Object): zlib inflate options.
  2804. *
  2805. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  2806. * format via wrapper header by default. That's why we don't provide
  2807. * separate `ungzip` method.
  2808. *
  2809. * Supported options are:
  2810. *
  2811. * - windowBits
  2812. *
  2813. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2814. * for more information.
  2815. *
  2816. * Sugar (options):
  2817. *
  2818. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  2819. * negative windowBits implicitly.
  2820. * - `to` (String) - if equal to 'string', then result will be converted
  2821. * from utf8 to utf16 (javascript) string. When string output requested,
  2822. * chunk length can differ from `chunkSize`, depending on content.
  2823. *
  2824. *
  2825. * ##### Example:
  2826. *
  2827. * ```javascript
  2828. * const pako = require('pako');
  2829. * const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9]));
  2830. * let output;
  2831. *
  2832. * try {
  2833. * output = pako.inflate(input);
  2834. * } catch (err) {
  2835. * console.log(err);
  2836. * }
  2837. * ```
  2838. **/
  2839. function inflate(input, options) {
  2840. const inflator = new Inflate(options);
  2841. inflator.push(input);
  2842. // That will never happens, if you don't cheat with options :)
  2843. if (inflator.err) throw inflator.msg || messages[inflator.err];
  2844. return inflator.result;
  2845. }
  2846. /**
  2847. * inflateRaw(data[, options]) -> Uint8Array|String
  2848. * - data (Uint8Array|ArrayBuffer): input data to decompress.
  2849. * - options (Object): zlib inflate options.
  2850. *
  2851. * The same as [[inflate]], but creates raw data, without wrapper
  2852. * (header and adler32 crc).
  2853. **/
  2854. function inflateRaw(input, options) {
  2855. options = options || {};
  2856. options.raw = true;
  2857. return inflate(input, options);
  2858. }
  2859. /**
  2860. * ungzip(data[, options]) -> Uint8Array|String
  2861. * - data (Uint8Array|ArrayBuffer): input data to decompress.
  2862. * - options (Object): zlib inflate options.
  2863. *
  2864. * Just shortcut to [[inflate]], because it autodetects format
  2865. * by header.content. Done for convenience.
  2866. **/
  2867. var Inflate_1 = Inflate;
  2868. var inflate_2 = inflate;
  2869. var inflateRaw_1 = inflateRaw;
  2870. var ungzip = inflate;
  2871. var constants = constants$1;
  2872. var inflate_1 = {
  2873. Inflate: Inflate_1,
  2874. inflate: inflate_2,
  2875. inflateRaw: inflateRaw_1,
  2876. ungzip: ungzip,
  2877. constants: constants
  2878. };
  2879. exports.Inflate = Inflate_1;
  2880. exports.constants = constants;
  2881. exports["default"] = inflate_1;
  2882. exports.inflate = inflate_2;
  2883. exports.inflateRaw = inflateRaw_1;
  2884. exports.ungzip = ungzip;
  2885. Object.defineProperty(exports, '__esModule', { value: true });
  2886. }));