decodeGoogleEarthEnterprisePacket.js 126 KB

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