decodeGoogleEarthEnterprisePacket.js 123 KB

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