decodeGoogleEarthEnterprisePacket.js 126 KB

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