decodeGoogleEarthEnterprisePacket.js 147 KB

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