plantuml-decoder.js 108 KB

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