plantuml-encoder.js 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077
  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/deflate.js')
  4. module.exports = function (data) {
  5. return pako.deflateRaw(data, { level: 9, to: 'string' })
  6. }
  7. },{"pako/lib/deflate.js":4}],2:[function(require,module,exports){
  8. 'use strict'
  9. // Encode code taken from the PlantUML website:
  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 encode6bit (b) {
  14. if (b < 10) {
  15. return String.fromCharCode(48 + b)
  16. }
  17. b -= 10
  18. if (b < 26) {
  19. return String.fromCharCode(65 + b)
  20. }
  21. b -= 26
  22. if (b < 26) {
  23. return String.fromCharCode(97 + b)
  24. }
  25. b -= 26
  26. if (b === 0) {
  27. return '-'
  28. }
  29. if (b === 1) {
  30. return '_'
  31. }
  32. return '?'
  33. }
  34. function append3bytes (b1, b2, b3) {
  35. var c1 = b1 >> 2
  36. var c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
  37. var c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
  38. var c4 = b3 & 0x3F
  39. var r = ''
  40. r += encode6bit(c1 & 0x3F)
  41. r += encode6bit(c2 & 0x3F)
  42. r += encode6bit(c3 & 0x3F)
  43. r += encode6bit(c4 & 0x3F)
  44. return r
  45. }
  46. module.exports = function (data) {
  47. var r = ''
  48. for (var i = 0; i < data.length; i += 3) {
  49. if (i + 2 === data.length) {
  50. r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), 0)
  51. } else if (i + 1 === data.length) {
  52. r += append3bytes(data.charCodeAt(i), 0, 0)
  53. } else {
  54. r += append3bytes(data.charCodeAt(i),
  55. data.charCodeAt(i + 1),
  56. data.charCodeAt(i + 2))
  57. }
  58. }
  59. return r
  60. }
  61. },{}],3:[function(require,module,exports){
  62. 'use strict'
  63. var deflate = require('./deflate')
  64. var encode64 = require('./encode64')
  65. module.exports.encode = function (puml) {
  66. var deflated = deflate(puml)
  67. return encode64(deflated)
  68. }
  69. },{"./deflate":1,"./encode64":2}],4:[function(require,module,exports){
  70. 'use strict';
  71. var zlib_deflate = require('./zlib/deflate');
  72. var utils = require('./utils/common');
  73. var strings = require('./utils/strings');
  74. var msg = require('./zlib/messages');
  75. var ZStream = require('./zlib/zstream');
  76. var toString = Object.prototype.toString;
  77. /* Public constants ==========================================================*/
  78. /* ===========================================================================*/
  79. var Z_NO_FLUSH = 0;
  80. var Z_FINISH = 4;
  81. var Z_OK = 0;
  82. var Z_STREAM_END = 1;
  83. var Z_SYNC_FLUSH = 2;
  84. var Z_DEFAULT_COMPRESSION = -1;
  85. var Z_DEFAULT_STRATEGY = 0;
  86. var Z_DEFLATED = 8;
  87. /* ===========================================================================*/
  88. /**
  89. * class Deflate
  90. *
  91. * Generic JS-style wrapper for zlib calls. If you don't need
  92. * streaming behaviour - use more simple functions: [[deflate]],
  93. * [[deflateRaw]] and [[gzip]].
  94. **/
  95. /* internal
  96. * Deflate.chunks -> Array
  97. *
  98. * Chunks of output data, if [[Deflate#onData]] not overridden.
  99. **/
  100. /**
  101. * Deflate.result -> Uint8Array|Array
  102. *
  103. * Compressed result, generated by default [[Deflate#onData]]
  104. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  105. * (call [[Deflate#push]] with `Z_FINISH` / `true` param) or if you
  106. * push a chunk with explicit flush (call [[Deflate#push]] with
  107. * `Z_SYNC_FLUSH` param).
  108. **/
  109. /**
  110. * Deflate.err -> Number
  111. *
  112. * Error code after deflate finished. 0 (Z_OK) on success.
  113. * You will not need it in real life, because deflate errors
  114. * are possible only on wrong options or bad `onData` / `onEnd`
  115. * custom handlers.
  116. **/
  117. /**
  118. * Deflate.msg -> String
  119. *
  120. * Error message, if [[Deflate.err]] != 0
  121. **/
  122. /**
  123. * new Deflate(options)
  124. * - options (Object): zlib deflate options.
  125. *
  126. * Creates new deflator instance with specified params. Throws exception
  127. * on bad params. Supported options:
  128. *
  129. * - `level`
  130. * - `windowBits`
  131. * - `memLevel`
  132. * - `strategy`
  133. * - `dictionary`
  134. *
  135. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  136. * for more information on these.
  137. *
  138. * Additional options, for internal needs:
  139. *
  140. * - `chunkSize` - size of generated data chunks (16K by default)
  141. * - `raw` (Boolean) - do raw deflate
  142. * - `gzip` (Boolean) - create gzip wrapper
  143. * - `to` (String) - if equal to 'string', then result will be "binary string"
  144. * (each char code [0..255])
  145. * - `header` (Object) - custom header for gzip
  146. * - `text` (Boolean) - true if compressed data believed to be text
  147. * - `time` (Number) - modification time, unix timestamp
  148. * - `os` (Number) - operation system code
  149. * - `extra` (Array) - array of bytes with extra data (max 65536)
  150. * - `name` (String) - file name (binary string)
  151. * - `comment` (String) - comment (binary string)
  152. * - `hcrc` (Boolean) - true if header crc should be added
  153. *
  154. * ##### Example:
  155. *
  156. * ```javascript
  157. * var pako = require('pako')
  158. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  159. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  160. *
  161. * var deflate = new pako.Deflate({ level: 3});
  162. *
  163. * deflate.push(chunk1, false);
  164. * deflate.push(chunk2, true); // true -> last chunk
  165. *
  166. * if (deflate.err) { throw new Error(deflate.err); }
  167. *
  168. * console.log(deflate.result);
  169. * ```
  170. **/
  171. function Deflate(options) {
  172. if (!(this instanceof Deflate)) return new Deflate(options);
  173. this.options = utils.assign({
  174. level: Z_DEFAULT_COMPRESSION,
  175. method: Z_DEFLATED,
  176. chunkSize: 16384,
  177. windowBits: 15,
  178. memLevel: 8,
  179. strategy: Z_DEFAULT_STRATEGY,
  180. to: ''
  181. }, options || {});
  182. var opt = this.options;
  183. if (opt.raw && (opt.windowBits > 0)) {
  184. opt.windowBits = -opt.windowBits;
  185. }
  186. else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
  187. opt.windowBits += 16;
  188. }
  189. this.err = 0; // error code, if happens (0 = Z_OK)
  190. this.msg = ''; // error message
  191. this.ended = false; // used to avoid multiple onEnd() calls
  192. this.chunks = []; // chunks of compressed data
  193. this.strm = new ZStream();
  194. this.strm.avail_out = 0;
  195. var status = zlib_deflate.deflateInit2(
  196. this.strm,
  197. opt.level,
  198. opt.method,
  199. opt.windowBits,
  200. opt.memLevel,
  201. opt.strategy
  202. );
  203. if (status !== Z_OK) {
  204. throw new Error(msg[status]);
  205. }
  206. if (opt.header) {
  207. zlib_deflate.deflateSetHeader(this.strm, opt.header);
  208. }
  209. if (opt.dictionary) {
  210. var dict;
  211. // Convert data if needed
  212. if (typeof opt.dictionary === 'string') {
  213. // If we need to compress text, change encoding to utf8.
  214. dict = strings.string2buf(opt.dictionary);
  215. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  216. dict = new Uint8Array(opt.dictionary);
  217. } else {
  218. dict = opt.dictionary;
  219. }
  220. status = zlib_deflate.deflateSetDictionary(this.strm, dict);
  221. if (status !== Z_OK) {
  222. throw new Error(msg[status]);
  223. }
  224. this._dict_set = true;
  225. }
  226. }
  227. /**
  228. * Deflate#push(data[, mode]) -> Boolean
  229. * - data (Uint8Array|Array|ArrayBuffer|String): input data. Strings will be
  230. * converted to utf8 byte sequence.
  231. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  232. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
  233. *
  234. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  235. * new compressed chunks. Returns `true` on success. The last data block must have
  236. * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
  237. * [[Deflate#onEnd]]. For interim explicit flushes (without ending the stream) you
  238. * can use mode Z_SYNC_FLUSH, keeping the compression context.
  239. *
  240. * On fail call [[Deflate#onEnd]] with error code and return false.
  241. *
  242. * We strongly recommend to use `Uint8Array` on input for best speed (output
  243. * array format is detected automatically). Also, don't skip last param and always
  244. * use the same type in your code (boolean or number). That will improve JS speed.
  245. *
  246. * For regular `Array`-s make sure all elements are [0..255].
  247. *
  248. * ##### Example
  249. *
  250. * ```javascript
  251. * push(chunk, false); // push one of data chunks
  252. * ...
  253. * push(chunk, true); // push last chunk
  254. * ```
  255. **/
  256. Deflate.prototype.push = function (data, mode) {
  257. var strm = this.strm;
  258. var chunkSize = this.options.chunkSize;
  259. var status, _mode;
  260. if (this.ended) { return false; }
  261. _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
  262. // Convert data if needed
  263. if (typeof data === 'string') {
  264. // If we need to compress text, change encoding to utf8.
  265. strm.input = strings.string2buf(data);
  266. } else if (toString.call(data) === '[object ArrayBuffer]') {
  267. strm.input = new Uint8Array(data);
  268. } else {
  269. strm.input = data;
  270. }
  271. strm.next_in = 0;
  272. strm.avail_in = strm.input.length;
  273. do {
  274. if (strm.avail_out === 0) {
  275. strm.output = new utils.Buf8(chunkSize);
  276. strm.next_out = 0;
  277. strm.avail_out = chunkSize;
  278. }
  279. status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
  280. if (status !== Z_STREAM_END && status !== Z_OK) {
  281. this.onEnd(status);
  282. this.ended = true;
  283. return false;
  284. }
  285. if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) {
  286. if (this.options.to === 'string') {
  287. this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out)));
  288. } else {
  289. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  290. }
  291. }
  292. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
  293. // Finalize on the last chunk.
  294. if (_mode === Z_FINISH) {
  295. status = zlib_deflate.deflateEnd(this.strm);
  296. this.onEnd(status);
  297. this.ended = true;
  298. return status === Z_OK;
  299. }
  300. // callback interim results if Z_SYNC_FLUSH.
  301. if (_mode === Z_SYNC_FLUSH) {
  302. this.onEnd(Z_OK);
  303. strm.avail_out = 0;
  304. return true;
  305. }
  306. return true;
  307. };
  308. /**
  309. * Deflate#onData(chunk) -> Void
  310. * - chunk (Uint8Array|Array|String): output data. Type of array depends
  311. * on js engine support. When string output requested, each chunk
  312. * will be string.
  313. *
  314. * By default, stores data blocks in `chunks[]` property and glue
  315. * those in `onEnd`. Override this handler, if you need another behaviour.
  316. **/
  317. Deflate.prototype.onData = function (chunk) {
  318. this.chunks.push(chunk);
  319. };
  320. /**
  321. * Deflate#onEnd(status) -> Void
  322. * - status (Number): deflate status. 0 (Z_OK) on success,
  323. * other if not.
  324. *
  325. * Called once after you tell deflate that the input stream is
  326. * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
  327. * or if an error happened. By default - join collected chunks,
  328. * free memory and fill `results` / `err` properties.
  329. **/
  330. Deflate.prototype.onEnd = function (status) {
  331. // On success - join
  332. if (status === Z_OK) {
  333. if (this.options.to === 'string') {
  334. this.result = this.chunks.join('');
  335. } else {
  336. this.result = utils.flattenChunks(this.chunks);
  337. }
  338. }
  339. this.chunks = [];
  340. this.err = status;
  341. this.msg = this.strm.msg;
  342. };
  343. /**
  344. * deflate(data[, options]) -> Uint8Array|Array|String
  345. * - data (Uint8Array|Array|String): input data to compress.
  346. * - options (Object): zlib deflate options.
  347. *
  348. * Compress `data` with deflate algorithm and `options`.
  349. *
  350. * Supported options are:
  351. *
  352. * - level
  353. * - windowBits
  354. * - memLevel
  355. * - strategy
  356. * - dictionary
  357. *
  358. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  359. * for more information on these.
  360. *
  361. * Sugar (options):
  362. *
  363. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  364. * negative windowBits implicitly.
  365. * - `to` (String) - if equal to 'string', then result will be "binary string"
  366. * (each char code [0..255])
  367. *
  368. * ##### Example:
  369. *
  370. * ```javascript
  371. * var pako = require('pako')
  372. * , data = Uint8Array([1,2,3,4,5,6,7,8,9]);
  373. *
  374. * console.log(pako.deflate(data));
  375. * ```
  376. **/
  377. function deflate(input, options) {
  378. var deflator = new Deflate(options);
  379. deflator.push(input, true);
  380. // That will never happens, if you don't cheat with options :)
  381. if (deflator.err) { throw deflator.msg || msg[deflator.err]; }
  382. return deflator.result;
  383. }
  384. /**
  385. * deflateRaw(data[, options]) -> Uint8Array|Array|String
  386. * - data (Uint8Array|Array|String): input data to compress.
  387. * - options (Object): zlib deflate options.
  388. *
  389. * The same as [[deflate]], but creates raw data, without wrapper
  390. * (header and adler32 crc).
  391. **/
  392. function deflateRaw(input, options) {
  393. options = options || {};
  394. options.raw = true;
  395. return deflate(input, options);
  396. }
  397. /**
  398. * gzip(data[, options]) -> Uint8Array|Array|String
  399. * - data (Uint8Array|Array|String): input data to compress.
  400. * - options (Object): zlib deflate options.
  401. *
  402. * The same as [[deflate]], but create gzip wrapper instead of
  403. * deflate one.
  404. **/
  405. function gzip(input, options) {
  406. options = options || {};
  407. options.gzip = true;
  408. return deflate(input, options);
  409. }
  410. exports.Deflate = Deflate;
  411. exports.deflate = deflate;
  412. exports.deflateRaw = deflateRaw;
  413. exports.gzip = gzip;
  414. },{"./utils/common":5,"./utils/strings":6,"./zlib/deflate":9,"./zlib/messages":10,"./zlib/zstream":12}],5:[function(require,module,exports){
  415. 'use strict';
  416. var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
  417. (typeof Uint16Array !== 'undefined') &&
  418. (typeof Int32Array !== 'undefined');
  419. function _has(obj, key) {
  420. return Object.prototype.hasOwnProperty.call(obj, key);
  421. }
  422. exports.assign = function (obj /*from1, from2, from3, ...*/) {
  423. var sources = Array.prototype.slice.call(arguments, 1);
  424. while (sources.length) {
  425. var source = sources.shift();
  426. if (!source) { continue; }
  427. if (typeof source !== 'object') {
  428. throw new TypeError(source + 'must be non-object');
  429. }
  430. for (var p in source) {
  431. if (_has(source, p)) {
  432. obj[p] = source[p];
  433. }
  434. }
  435. }
  436. return obj;
  437. };
  438. // reduce buffer size, avoiding mem copy
  439. exports.shrinkBuf = function (buf, size) {
  440. if (buf.length === size) { return buf; }
  441. if (buf.subarray) { return buf.subarray(0, size); }
  442. buf.length = size;
  443. return buf;
  444. };
  445. var fnTyped = {
  446. arraySet: function (dest, src, src_offs, len, dest_offs) {
  447. if (src.subarray && dest.subarray) {
  448. dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
  449. return;
  450. }
  451. // Fallback to ordinary array
  452. for (var i = 0; i < len; i++) {
  453. dest[dest_offs + i] = src[src_offs + i];
  454. }
  455. },
  456. // Join array of chunks to single array.
  457. flattenChunks: function (chunks) {
  458. var i, l, len, pos, chunk, result;
  459. // calculate data length
  460. len = 0;
  461. for (i = 0, l = chunks.length; i < l; i++) {
  462. len += chunks[i].length;
  463. }
  464. // join chunks
  465. result = new Uint8Array(len);
  466. pos = 0;
  467. for (i = 0, l = chunks.length; i < l; i++) {
  468. chunk = chunks[i];
  469. result.set(chunk, pos);
  470. pos += chunk.length;
  471. }
  472. return result;
  473. }
  474. };
  475. var fnUntyped = {
  476. arraySet: function (dest, src, src_offs, len, dest_offs) {
  477. for (var i = 0; i < len; i++) {
  478. dest[dest_offs + i] = src[src_offs + i];
  479. }
  480. },
  481. // Join array of chunks to single array.
  482. flattenChunks: function (chunks) {
  483. return [].concat.apply([], chunks);
  484. }
  485. };
  486. // Enable/Disable typed arrays use, for testing
  487. //
  488. exports.setTyped = function (on) {
  489. if (on) {
  490. exports.Buf8 = Uint8Array;
  491. exports.Buf16 = Uint16Array;
  492. exports.Buf32 = Int32Array;
  493. exports.assign(exports, fnTyped);
  494. } else {
  495. exports.Buf8 = Array;
  496. exports.Buf16 = Array;
  497. exports.Buf32 = Array;
  498. exports.assign(exports, fnUntyped);
  499. }
  500. };
  501. exports.setTyped(TYPED_OK);
  502. },{}],6:[function(require,module,exports){
  503. // String encode/decode helpers
  504. 'use strict';
  505. var utils = require('./common');
  506. // Quick check if we can use fast array to bin string conversion
  507. //
  508. // - apply(Array) can fail on Android 2.2
  509. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  510. //
  511. var STR_APPLY_OK = true;
  512. var STR_APPLY_UIA_OK = true;
  513. try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; }
  514. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  515. // Table with utf8 lengths (calculated by first byte of sequence)
  516. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  517. // because max possible codepoint is 0x10ffff
  518. var _utf8len = new utils.Buf8(256);
  519. for (var q = 0; q < 256; q++) {
  520. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  521. }
  522. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  523. // convert string to array (typed, when possible)
  524. exports.string2buf = function (str) {
  525. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  526. // count binary size
  527. for (m_pos = 0; m_pos < str_len; m_pos++) {
  528. c = str.charCodeAt(m_pos);
  529. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  530. c2 = str.charCodeAt(m_pos + 1);
  531. if ((c2 & 0xfc00) === 0xdc00) {
  532. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  533. m_pos++;
  534. }
  535. }
  536. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  537. }
  538. // allocate buffer
  539. buf = new utils.Buf8(buf_len);
  540. // convert
  541. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  542. c = str.charCodeAt(m_pos);
  543. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  544. c2 = str.charCodeAt(m_pos + 1);
  545. if ((c2 & 0xfc00) === 0xdc00) {
  546. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  547. m_pos++;
  548. }
  549. }
  550. if (c < 0x80) {
  551. /* one byte */
  552. buf[i++] = c;
  553. } else if (c < 0x800) {
  554. /* two bytes */
  555. buf[i++] = 0xC0 | (c >>> 6);
  556. buf[i++] = 0x80 | (c & 0x3f);
  557. } else if (c < 0x10000) {
  558. /* three bytes */
  559. buf[i++] = 0xE0 | (c >>> 12);
  560. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  561. buf[i++] = 0x80 | (c & 0x3f);
  562. } else {
  563. /* four bytes */
  564. buf[i++] = 0xf0 | (c >>> 18);
  565. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  566. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  567. buf[i++] = 0x80 | (c & 0x3f);
  568. }
  569. }
  570. return buf;
  571. };
  572. // Helper (used in 2 places)
  573. function buf2binstring(buf, len) {
  574. // On Chrome, the arguments in a function call that are allowed is `65534`.
  575. // If the length of the buffer is smaller than that, we can use this optimization,
  576. // otherwise we will take a slower path.
  577. if (len < 65534) {
  578. if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
  579. return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
  580. }
  581. }
  582. var result = '';
  583. for (var i = 0; i < len; i++) {
  584. result += String.fromCharCode(buf[i]);
  585. }
  586. return result;
  587. }
  588. // Convert byte array to binary string
  589. exports.buf2binstring = function (buf) {
  590. return buf2binstring(buf, buf.length);
  591. };
  592. // Convert binary string (typed, when possible)
  593. exports.binstring2buf = function (str) {
  594. var buf = new utils.Buf8(str.length);
  595. for (var i = 0, len = buf.length; i < len; i++) {
  596. buf[i] = str.charCodeAt(i);
  597. }
  598. return buf;
  599. };
  600. // convert array to string
  601. exports.buf2string = function (buf, max) {
  602. var i, out, c, c_len;
  603. var len = max || buf.length;
  604. // Reserve max possible length (2 words per char)
  605. // NB: by unknown reasons, Array is significantly faster for
  606. // String.fromCharCode.apply than Uint16Array.
  607. var utf16buf = new Array(len * 2);
  608. for (out = 0, i = 0; i < len;) {
  609. c = buf[i++];
  610. // quick process ascii
  611. if (c < 0x80) { utf16buf[out++] = c; continue; }
  612. c_len = _utf8len[c];
  613. // skip 5 & 6 byte codes
  614. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  615. // apply mask on first byte
  616. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  617. // join the rest
  618. while (c_len > 1 && i < len) {
  619. c = (c << 6) | (buf[i++] & 0x3f);
  620. c_len--;
  621. }
  622. // terminated by end of string?
  623. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  624. if (c < 0x10000) {
  625. utf16buf[out++] = c;
  626. } else {
  627. c -= 0x10000;
  628. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  629. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  630. }
  631. }
  632. return buf2binstring(utf16buf, out);
  633. };
  634. // Calculate max possible position in utf8 buffer,
  635. // that will not break sequence. If that's not possible
  636. // - (very small limits) return max size as is.
  637. //
  638. // buf[] - utf8 bytes array
  639. // max - length limit (mandatory);
  640. exports.utf8border = function (buf, max) {
  641. var pos;
  642. max = max || buf.length;
  643. if (max > buf.length) { max = buf.length; }
  644. // go back from last position, until start of sequence found
  645. pos = max - 1;
  646. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  647. // Very small and broken sequence,
  648. // return max, because we should return something anyway.
  649. if (pos < 0) { return max; }
  650. // If we came to start of buffer - that means buffer is too small,
  651. // return max too.
  652. if (pos === 0) { return max; }
  653. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  654. };
  655. },{"./common":5}],7:[function(require,module,exports){
  656. 'use strict';
  657. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  658. // It isn't worth it to make additional optimizations as in original.
  659. // Small size is preferable.
  660. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  661. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  662. //
  663. // This software is provided 'as-is', without any express or implied
  664. // warranty. In no event will the authors be held liable for any damages
  665. // arising from the use of this software.
  666. //
  667. // Permission is granted to anyone to use this software for any purpose,
  668. // including commercial applications, and to alter it and redistribute it
  669. // freely, subject to the following restrictions:
  670. //
  671. // 1. The origin of this software must not be misrepresented; you must not
  672. // claim that you wrote the original software. If you use this software
  673. // in a product, an acknowledgment in the product documentation would be
  674. // appreciated but is not required.
  675. // 2. Altered source versions must be plainly marked as such, and must not be
  676. // misrepresented as being the original software.
  677. // 3. This notice may not be removed or altered from any source distribution.
  678. function adler32(adler, buf, len, pos) {
  679. var s1 = (adler & 0xffff) |0,
  680. s2 = ((adler >>> 16) & 0xffff) |0,
  681. n = 0;
  682. while (len !== 0) {
  683. // Set limit ~ twice less than 5552, to keep
  684. // s2 in 31-bits, because we force signed ints.
  685. // in other case %= will fail.
  686. n = len > 2000 ? 2000 : len;
  687. len -= n;
  688. do {
  689. s1 = (s1 + buf[pos++]) |0;
  690. s2 = (s2 + s1) |0;
  691. } while (--n);
  692. s1 %= 65521;
  693. s2 %= 65521;
  694. }
  695. return (s1 | (s2 << 16)) |0;
  696. }
  697. module.exports = adler32;
  698. },{}],8:[function(require,module,exports){
  699. 'use strict';
  700. // Note: we can't get significant speed boost here.
  701. // So write code to minimize size - no pregenerated tables
  702. // and array tools dependencies.
  703. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  704. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  705. //
  706. // This software is provided 'as-is', without any express or implied
  707. // warranty. In no event will the authors be held liable for any damages
  708. // arising from the use of this software.
  709. //
  710. // Permission is granted to anyone to use this software for any purpose,
  711. // including commercial applications, and to alter it and redistribute it
  712. // freely, subject to the following restrictions:
  713. //
  714. // 1. The origin of this software must not be misrepresented; you must not
  715. // claim that you wrote the original software. If you use this software
  716. // in a product, an acknowledgment in the product documentation would be
  717. // appreciated but is not required.
  718. // 2. Altered source versions must be plainly marked as such, and must not be
  719. // misrepresented as being the original software.
  720. // 3. This notice may not be removed or altered from any source distribution.
  721. // Use ordinary array, since untyped makes no boost here
  722. function makeTable() {
  723. var c, table = [];
  724. for (var n = 0; n < 256; n++) {
  725. c = n;
  726. for (var k = 0; k < 8; k++) {
  727. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  728. }
  729. table[n] = c;
  730. }
  731. return table;
  732. }
  733. // Create table on load. Just 255 signed longs. Not a problem.
  734. var crcTable = makeTable();
  735. function crc32(crc, buf, len, pos) {
  736. var t = crcTable,
  737. end = pos + len;
  738. crc ^= -1;
  739. for (var i = pos; i < end; i++) {
  740. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  741. }
  742. return (crc ^ (-1)); // >>> 0;
  743. }
  744. module.exports = crc32;
  745. },{}],9:[function(require,module,exports){
  746. 'use strict';
  747. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  748. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  749. //
  750. // This software is provided 'as-is', without any express or implied
  751. // warranty. In no event will the authors be held liable for any damages
  752. // arising from the use of this software.
  753. //
  754. // Permission is granted to anyone to use this software for any purpose,
  755. // including commercial applications, and to alter it and redistribute it
  756. // freely, subject to the following restrictions:
  757. //
  758. // 1. The origin of this software must not be misrepresented; you must not
  759. // claim that you wrote the original software. If you use this software
  760. // in a product, an acknowledgment in the product documentation would be
  761. // appreciated but is not required.
  762. // 2. Altered source versions must be plainly marked as such, and must not be
  763. // misrepresented as being the original software.
  764. // 3. This notice may not be removed or altered from any source distribution.
  765. var utils = require('../utils/common');
  766. var trees = require('./trees');
  767. var adler32 = require('./adler32');
  768. var crc32 = require('./crc32');
  769. var msg = require('./messages');
  770. /* Public constants ==========================================================*/
  771. /* ===========================================================================*/
  772. /* Allowed flush values; see deflate() and inflate() below for details */
  773. var Z_NO_FLUSH = 0;
  774. var Z_PARTIAL_FLUSH = 1;
  775. //var Z_SYNC_FLUSH = 2;
  776. var Z_FULL_FLUSH = 3;
  777. var Z_FINISH = 4;
  778. var Z_BLOCK = 5;
  779. //var Z_TREES = 6;
  780. /* Return codes for the compression/decompression functions. Negative values
  781. * are errors, positive values are used for special but normal events.
  782. */
  783. var Z_OK = 0;
  784. var Z_STREAM_END = 1;
  785. //var Z_NEED_DICT = 2;
  786. //var Z_ERRNO = -1;
  787. var Z_STREAM_ERROR = -2;
  788. var Z_DATA_ERROR = -3;
  789. //var Z_MEM_ERROR = -4;
  790. var Z_BUF_ERROR = -5;
  791. //var Z_VERSION_ERROR = -6;
  792. /* compression levels */
  793. //var Z_NO_COMPRESSION = 0;
  794. //var Z_BEST_SPEED = 1;
  795. //var Z_BEST_COMPRESSION = 9;
  796. var Z_DEFAULT_COMPRESSION = -1;
  797. var Z_FILTERED = 1;
  798. var Z_HUFFMAN_ONLY = 2;
  799. var Z_RLE = 3;
  800. var Z_FIXED = 4;
  801. var Z_DEFAULT_STRATEGY = 0;
  802. /* Possible values of the data_type field (though see inflate()) */
  803. //var Z_BINARY = 0;
  804. //var Z_TEXT = 1;
  805. //var Z_ASCII = 1; // = Z_TEXT
  806. var Z_UNKNOWN = 2;
  807. /* The deflate compression method */
  808. var Z_DEFLATED = 8;
  809. /*============================================================================*/
  810. var MAX_MEM_LEVEL = 9;
  811. /* Maximum value for memLevel in deflateInit2 */
  812. var MAX_WBITS = 15;
  813. /* 32K LZ77 window */
  814. var DEF_MEM_LEVEL = 8;
  815. var LENGTH_CODES = 29;
  816. /* number of length codes, not counting the special END_BLOCK code */
  817. var LITERALS = 256;
  818. /* number of literal bytes 0..255 */
  819. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  820. /* number of Literal or Length codes, including the END_BLOCK code */
  821. var D_CODES = 30;
  822. /* number of distance codes */
  823. var BL_CODES = 19;
  824. /* number of codes used to transfer the bit lengths */
  825. var HEAP_SIZE = 2 * L_CODES + 1;
  826. /* maximum heap size */
  827. var MAX_BITS = 15;
  828. /* All codes must not exceed MAX_BITS bits */
  829. var MIN_MATCH = 3;
  830. var MAX_MATCH = 258;
  831. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  832. var PRESET_DICT = 0x20;
  833. var INIT_STATE = 42;
  834. var EXTRA_STATE = 69;
  835. var NAME_STATE = 73;
  836. var COMMENT_STATE = 91;
  837. var HCRC_STATE = 103;
  838. var BUSY_STATE = 113;
  839. var FINISH_STATE = 666;
  840. var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  841. var BS_BLOCK_DONE = 2; /* block flush performed */
  842. var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  843. var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  844. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  845. function err(strm, errorCode) {
  846. strm.msg = msg[errorCode];
  847. return errorCode;
  848. }
  849. function rank(f) {
  850. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  851. }
  852. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  853. /* =========================================================================
  854. * Flush as much pending output as possible. All deflate() output goes
  855. * through this function so some applications may wish to modify it
  856. * to avoid allocating a large strm->output buffer and copying into it.
  857. * (See also read_buf()).
  858. */
  859. function flush_pending(strm) {
  860. var s = strm.state;
  861. //_tr_flush_bits(s);
  862. var len = s.pending;
  863. if (len > strm.avail_out) {
  864. len = strm.avail_out;
  865. }
  866. if (len === 0) { return; }
  867. utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
  868. strm.next_out += len;
  869. s.pending_out += len;
  870. strm.total_out += len;
  871. strm.avail_out -= len;
  872. s.pending -= len;
  873. if (s.pending === 0) {
  874. s.pending_out = 0;
  875. }
  876. }
  877. function flush_block_only(s, last) {
  878. trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  879. s.block_start = s.strstart;
  880. flush_pending(s.strm);
  881. }
  882. function put_byte(s, b) {
  883. s.pending_buf[s.pending++] = b;
  884. }
  885. /* =========================================================================
  886. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  887. * IN assertion: the stream state is correct and there is enough room in
  888. * pending_buf.
  889. */
  890. function putShortMSB(s, b) {
  891. // put_byte(s, (Byte)(b >> 8));
  892. // put_byte(s, (Byte)(b & 0xff));
  893. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  894. s.pending_buf[s.pending++] = b & 0xff;
  895. }
  896. /* ===========================================================================
  897. * Read a new buffer from the current input stream, update the adler32
  898. * and total number of bytes read. All deflate() input goes through
  899. * this function so some applications may wish to modify it to avoid
  900. * allocating a large strm->input buffer and copying from it.
  901. * (See also flush_pending()).
  902. */
  903. function read_buf(strm, buf, start, size) {
  904. var len = strm.avail_in;
  905. if (len > size) { len = size; }
  906. if (len === 0) { return 0; }
  907. strm.avail_in -= len;
  908. // zmemcpy(buf, strm->next_in, len);
  909. utils.arraySet(buf, strm.input, strm.next_in, len, start);
  910. if (strm.state.wrap === 1) {
  911. strm.adler = adler32(strm.adler, buf, len, start);
  912. }
  913. else if (strm.state.wrap === 2) {
  914. strm.adler = crc32(strm.adler, buf, len, start);
  915. }
  916. strm.next_in += len;
  917. strm.total_in += len;
  918. return len;
  919. }
  920. /* ===========================================================================
  921. * Set match_start to the longest match starting at the given string and
  922. * return its length. Matches shorter or equal to prev_length are discarded,
  923. * in which case the result is equal to prev_length and match_start is
  924. * garbage.
  925. * IN assertions: cur_match is the head of the hash chain for the current
  926. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  927. * OUT assertion: the match length is not greater than s->lookahead.
  928. */
  929. function longest_match(s, cur_match) {
  930. var chain_length = s.max_chain_length; /* max hash chain length */
  931. var scan = s.strstart; /* current string */
  932. var match; /* matched string */
  933. var len; /* length of current match */
  934. var best_len = s.prev_length; /* best match length so far */
  935. var nice_match = s.nice_match; /* stop if match long enough */
  936. var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  937. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  938. var _win = s.window; // shortcut
  939. var wmask = s.w_mask;
  940. var prev = s.prev;
  941. /* Stop when cur_match becomes <= limit. To simplify the code,
  942. * we prevent matches with the string of window index 0.
  943. */
  944. var strend = s.strstart + MAX_MATCH;
  945. var scan_end1 = _win[scan + best_len - 1];
  946. var scan_end = _win[scan + best_len];
  947. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  948. * It is easy to get rid of this optimization if necessary.
  949. */
  950. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  951. /* Do not waste too much time if we already have a good match: */
  952. if (s.prev_length >= s.good_match) {
  953. chain_length >>= 2;
  954. }
  955. /* Do not look for matches beyond the end of the input. This is necessary
  956. * to make deflate deterministic.
  957. */
  958. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  959. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  960. do {
  961. // Assert(cur_match < s->strstart, "no future");
  962. match = cur_match;
  963. /* Skip to next match if the match length cannot increase
  964. * or if the match length is less than 2. Note that the checks below
  965. * for insufficient lookahead only occur occasionally for performance
  966. * reasons. Therefore uninitialized memory will be accessed, and
  967. * conditional jumps will be made that depend on those values.
  968. * However the length of the match is limited to the lookahead, so
  969. * the output of deflate is not affected by the uninitialized values.
  970. */
  971. if (_win[match + best_len] !== scan_end ||
  972. _win[match + best_len - 1] !== scan_end1 ||
  973. _win[match] !== _win[scan] ||
  974. _win[++match] !== _win[scan + 1]) {
  975. continue;
  976. }
  977. /* The check at best_len-1 can be removed because it will be made
  978. * again later. (This heuristic is not always a win.)
  979. * It is not necessary to compare scan[2] and match[2] since they
  980. * are always equal when the other bytes match, given that
  981. * the hash keys are equal and that HASH_BITS >= 8.
  982. */
  983. scan += 2;
  984. match++;
  985. // Assert(*scan == *match, "match[2]?");
  986. /* We check for insufficient lookahead only every 8th comparison;
  987. * the 256th check will be made at strstart+258.
  988. */
  989. do {
  990. /*jshint noempty:false*/
  991. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  992. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  993. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  994. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  995. scan < strend);
  996. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  997. len = MAX_MATCH - (strend - scan);
  998. scan = strend - MAX_MATCH;
  999. if (len > best_len) {
  1000. s.match_start = cur_match;
  1001. best_len = len;
  1002. if (len >= nice_match) {
  1003. break;
  1004. }
  1005. scan_end1 = _win[scan + best_len - 1];
  1006. scan_end = _win[scan + best_len];
  1007. }
  1008. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  1009. if (best_len <= s.lookahead) {
  1010. return best_len;
  1011. }
  1012. return s.lookahead;
  1013. }
  1014. /* ===========================================================================
  1015. * Fill the window when the lookahead becomes insufficient.
  1016. * Updates strstart and lookahead.
  1017. *
  1018. * IN assertion: lookahead < MIN_LOOKAHEAD
  1019. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1020. * At least one byte has been read, or avail_in == 0; reads are
  1021. * performed for at least two bytes (required for the zip translate_eol
  1022. * option -- not supported here).
  1023. */
  1024. function fill_window(s) {
  1025. var _w_size = s.w_size;
  1026. var p, n, m, more, str;
  1027. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  1028. do {
  1029. more = s.window_size - s.lookahead - s.strstart;
  1030. // JS ints have 32 bit, block below not needed
  1031. /* Deal with !@#$% 64K limit: */
  1032. //if (sizeof(int) <= 2) {
  1033. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1034. // more = wsize;
  1035. //
  1036. // } else if (more == (unsigned)(-1)) {
  1037. // /* Very unlikely, but possible on 16 bit machine if
  1038. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  1039. // */
  1040. // more--;
  1041. // }
  1042. //}
  1043. /* If the window is almost full and there is insufficient lookahead,
  1044. * move the upper half to the lower one to make room in the upper half.
  1045. */
  1046. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  1047. utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
  1048. s.match_start -= _w_size;
  1049. s.strstart -= _w_size;
  1050. /* we now have strstart >= MAX_DIST */
  1051. s.block_start -= _w_size;
  1052. /* Slide the hash table (could be avoided with 32 bit values
  1053. at the expense of memory usage). We slide even when level == 0
  1054. to keep the hash table consistent if we switch back to level > 0
  1055. later. (Using level 0 permanently is not an optimal usage of
  1056. zlib, so we don't care about this pathological case.)
  1057. */
  1058. n = s.hash_size;
  1059. p = n;
  1060. do {
  1061. m = s.head[--p];
  1062. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  1063. } while (--n);
  1064. n = _w_size;
  1065. p = n;
  1066. do {
  1067. m = s.prev[--p];
  1068. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  1069. /* If n is not on any hash chain, prev[n] is garbage but
  1070. * its value will never be used.
  1071. */
  1072. } while (--n);
  1073. more += _w_size;
  1074. }
  1075. if (s.strm.avail_in === 0) {
  1076. break;
  1077. }
  1078. /* If there was no sliding:
  1079. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1080. * more == window_size - lookahead - strstart
  1081. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1082. * => more >= window_size - 2*WSIZE + 2
  1083. * In the BIG_MEM or MMAP case (not yet supported),
  1084. * window_size == input_size + MIN_LOOKAHEAD &&
  1085. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1086. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1087. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1088. */
  1089. //Assert(more >= 2, "more < 2");
  1090. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  1091. s.lookahead += n;
  1092. /* Initialize the hash value now that we have some input: */
  1093. if (s.lookahead + s.insert >= MIN_MATCH) {
  1094. str = s.strstart - s.insert;
  1095. s.ins_h = s.window[str];
  1096. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  1097. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
  1098. //#if MIN_MATCH != 3
  1099. // Call update_hash() MIN_MATCH-3 more times
  1100. //#endif
  1101. while (s.insert) {
  1102. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  1103. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  1104. s.prev[str & s.w_mask] = s.head[s.ins_h];
  1105. s.head[s.ins_h] = str;
  1106. str++;
  1107. s.insert--;
  1108. if (s.lookahead + s.insert < MIN_MATCH) {
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1114. * but this is not important since only literal bytes will be emitted.
  1115. */
  1116. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  1117. /* If the WIN_INIT bytes after the end of the current data have never been
  1118. * written, then zero those bytes in order to avoid memory check reports of
  1119. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  1120. * the longest match routines. Update the high water mark for the next
  1121. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  1122. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  1123. */
  1124. // if (s.high_water < s.window_size) {
  1125. // var curr = s.strstart + s.lookahead;
  1126. // var init = 0;
  1127. //
  1128. // if (s.high_water < curr) {
  1129. // /* Previous high water mark below current data -- zero WIN_INIT
  1130. // * bytes or up to end of window, whichever is less.
  1131. // */
  1132. // init = s.window_size - curr;
  1133. // if (init > WIN_INIT)
  1134. // init = WIN_INIT;
  1135. // zmemzero(s->window + curr, (unsigned)init);
  1136. // s->high_water = curr + init;
  1137. // }
  1138. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  1139. // /* High water mark at or above current data, but below current data
  1140. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  1141. // * to end of window, whichever is less.
  1142. // */
  1143. // init = (ulg)curr + WIN_INIT - s->high_water;
  1144. // if (init > s->window_size - s->high_water)
  1145. // init = s->window_size - s->high_water;
  1146. // zmemzero(s->window + s->high_water, (unsigned)init);
  1147. // s->high_water += init;
  1148. // }
  1149. // }
  1150. //
  1151. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1152. // "not enough room for search");
  1153. }
  1154. /* ===========================================================================
  1155. * Copy without compression as much as possible from the input stream, return
  1156. * the current block state.
  1157. * This function does not insert new strings in the dictionary since
  1158. * uncompressible data is probably not useful. This function is used
  1159. * only for the level=0 compression option.
  1160. * NOTE: this function should be optimized to avoid extra copying from
  1161. * window to pending_buf.
  1162. */
  1163. function deflate_stored(s, flush) {
  1164. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1165. * to pending_buf_size, and each stored block has a 5 byte header:
  1166. */
  1167. var max_block_size = 0xffff;
  1168. if (max_block_size > s.pending_buf_size - 5) {
  1169. max_block_size = s.pending_buf_size - 5;
  1170. }
  1171. /* Copy as much as possible from input to output: */
  1172. for (;;) {
  1173. /* Fill the window as much as possible: */
  1174. if (s.lookahead <= 1) {
  1175. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1176. // s->block_start >= (long)s->w_size, "slide too late");
  1177. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  1178. // s.block_start >= s.w_size)) {
  1179. // throw new Error("slide too late");
  1180. // }
  1181. fill_window(s);
  1182. if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
  1183. return BS_NEED_MORE;
  1184. }
  1185. if (s.lookahead === 0) {
  1186. break;
  1187. }
  1188. /* flush the current block */
  1189. }
  1190. //Assert(s->block_start >= 0L, "block gone");
  1191. // if (s.block_start < 0) throw new Error("block gone");
  1192. s.strstart += s.lookahead;
  1193. s.lookahead = 0;
  1194. /* Emit a stored block if pending_buf will be full: */
  1195. var max_start = s.block_start + max_block_size;
  1196. if (s.strstart === 0 || s.strstart >= max_start) {
  1197. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1198. s.lookahead = s.strstart - max_start;
  1199. s.strstart = max_start;
  1200. /*** FLUSH_BLOCK(s, 0); ***/
  1201. flush_block_only(s, false);
  1202. if (s.strm.avail_out === 0) {
  1203. return BS_NEED_MORE;
  1204. }
  1205. /***/
  1206. }
  1207. /* Flush if we may have to slide, otherwise block_start may become
  1208. * negative and the data will be gone:
  1209. */
  1210. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  1211. /*** FLUSH_BLOCK(s, 0); ***/
  1212. flush_block_only(s, false);
  1213. if (s.strm.avail_out === 0) {
  1214. return BS_NEED_MORE;
  1215. }
  1216. /***/
  1217. }
  1218. }
  1219. s.insert = 0;
  1220. if (flush === Z_FINISH) {
  1221. /*** FLUSH_BLOCK(s, 1); ***/
  1222. flush_block_only(s, true);
  1223. if (s.strm.avail_out === 0) {
  1224. return BS_FINISH_STARTED;
  1225. }
  1226. /***/
  1227. return BS_FINISH_DONE;
  1228. }
  1229. if (s.strstart > s.block_start) {
  1230. /*** FLUSH_BLOCK(s, 0); ***/
  1231. flush_block_only(s, false);
  1232. if (s.strm.avail_out === 0) {
  1233. return BS_NEED_MORE;
  1234. }
  1235. /***/
  1236. }
  1237. return BS_NEED_MORE;
  1238. }
  1239. /* ===========================================================================
  1240. * Compress as much as possible from the input stream, return the current
  1241. * block state.
  1242. * This function does not perform lazy evaluation of matches and inserts
  1243. * new strings in the dictionary only for unmatched strings or for short
  1244. * matches. It is used only for the fast compression options.
  1245. */
  1246. function deflate_fast(s, flush) {
  1247. var hash_head; /* head of the hash chain */
  1248. var bflush; /* set if current block must be flushed */
  1249. for (;;) {
  1250. /* Make sure that we always have enough lookahead, except
  1251. * at the end of the input file. We need MAX_MATCH bytes
  1252. * for the next match, plus MIN_MATCH bytes to insert the
  1253. * string following the next match.
  1254. */
  1255. if (s.lookahead < MIN_LOOKAHEAD) {
  1256. fill_window(s);
  1257. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  1258. return BS_NEED_MORE;
  1259. }
  1260. if (s.lookahead === 0) {
  1261. break; /* flush the current block */
  1262. }
  1263. }
  1264. /* Insert the string window[strstart .. strstart+2] in the
  1265. * dictionary, and set hash_head to the head of the hash chain:
  1266. */
  1267. hash_head = 0/*NIL*/;
  1268. if (s.lookahead >= MIN_MATCH) {
  1269. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1270. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1271. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1272. s.head[s.ins_h] = s.strstart;
  1273. /***/
  1274. }
  1275. /* Find the longest match, discarding those <= prev_length.
  1276. * At this point we have always match_length < MIN_MATCH
  1277. */
  1278. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  1279. /* To simplify the code, we prevent matches with the string
  1280. * of window index 0 (in particular we have to avoid a match
  1281. * of the string with itself at the start of the input file).
  1282. */
  1283. s.match_length = longest_match(s, hash_head);
  1284. /* longest_match() sets match_start */
  1285. }
  1286. if (s.match_length >= MIN_MATCH) {
  1287. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  1288. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  1289. s.match_length - MIN_MATCH, bflush); ***/
  1290. bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  1291. s.lookahead -= s.match_length;
  1292. /* Insert new strings in the hash table only if the match length
  1293. * is not too large. This saves time but degrades compression.
  1294. */
  1295. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  1296. s.match_length--; /* string at strstart already in table */
  1297. do {
  1298. s.strstart++;
  1299. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1300. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1301. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1302. s.head[s.ins_h] = s.strstart;
  1303. /***/
  1304. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1305. * always MIN_MATCH bytes ahead.
  1306. */
  1307. } while (--s.match_length !== 0);
  1308. s.strstart++;
  1309. } else
  1310. {
  1311. s.strstart += s.match_length;
  1312. s.match_length = 0;
  1313. s.ins_h = s.window[s.strstart];
  1314. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  1315. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
  1316. //#if MIN_MATCH != 3
  1317. // Call UPDATE_HASH() MIN_MATCH-3 more times
  1318. //#endif
  1319. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1320. * matter since it will be recomputed at next deflate call.
  1321. */
  1322. }
  1323. } else {
  1324. /* No match, output a literal byte */
  1325. //Tracevv((stderr,"%c", s.window[s.strstart]));
  1326. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1327. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1328. s.lookahead--;
  1329. s.strstart++;
  1330. }
  1331. if (bflush) {
  1332. /*** FLUSH_BLOCK(s, 0); ***/
  1333. flush_block_only(s, false);
  1334. if (s.strm.avail_out === 0) {
  1335. return BS_NEED_MORE;
  1336. }
  1337. /***/
  1338. }
  1339. }
  1340. s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
  1341. if (flush === Z_FINISH) {
  1342. /*** FLUSH_BLOCK(s, 1); ***/
  1343. flush_block_only(s, true);
  1344. if (s.strm.avail_out === 0) {
  1345. return BS_FINISH_STARTED;
  1346. }
  1347. /***/
  1348. return BS_FINISH_DONE;
  1349. }
  1350. if (s.last_lit) {
  1351. /*** FLUSH_BLOCK(s, 0); ***/
  1352. flush_block_only(s, false);
  1353. if (s.strm.avail_out === 0) {
  1354. return BS_NEED_MORE;
  1355. }
  1356. /***/
  1357. }
  1358. return BS_BLOCK_DONE;
  1359. }
  1360. /* ===========================================================================
  1361. * Same as above, but achieves better compression. We use a lazy
  1362. * evaluation for matches: a match is finally adopted only if there is
  1363. * no better match at the next window position.
  1364. */
  1365. function deflate_slow(s, flush) {
  1366. var hash_head; /* head of hash chain */
  1367. var bflush; /* set if current block must be flushed */
  1368. var max_insert;
  1369. /* Process the input block. */
  1370. for (;;) {
  1371. /* Make sure that we always have enough lookahead, except
  1372. * at the end of the input file. We need MAX_MATCH bytes
  1373. * for the next match, plus MIN_MATCH bytes to insert the
  1374. * string following the next match.
  1375. */
  1376. if (s.lookahead < MIN_LOOKAHEAD) {
  1377. fill_window(s);
  1378. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  1379. return BS_NEED_MORE;
  1380. }
  1381. if (s.lookahead === 0) { break; } /* flush the current block */
  1382. }
  1383. /* Insert the string window[strstart .. strstart+2] in the
  1384. * dictionary, and set hash_head to the head of the hash chain:
  1385. */
  1386. hash_head = 0/*NIL*/;
  1387. if (s.lookahead >= MIN_MATCH) {
  1388. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1389. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1390. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1391. s.head[s.ins_h] = s.strstart;
  1392. /***/
  1393. }
  1394. /* Find the longest match, discarding those <= prev_length.
  1395. */
  1396. s.prev_length = s.match_length;
  1397. s.prev_match = s.match_start;
  1398. s.match_length = MIN_MATCH - 1;
  1399. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  1400. s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  1401. /* To simplify the code, we prevent matches with the string
  1402. * of window index 0 (in particular we have to avoid a match
  1403. * of the string with itself at the start of the input file).
  1404. */
  1405. s.match_length = longest_match(s, hash_head);
  1406. /* longest_match() sets match_start */
  1407. if (s.match_length <= 5 &&
  1408. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  1409. /* If prev_match is also MIN_MATCH, match_start is garbage
  1410. * but we will ignore the current match anyway.
  1411. */
  1412. s.match_length = MIN_MATCH - 1;
  1413. }
  1414. }
  1415. /* If there was a match at the previous step and the current
  1416. * match is not better, output the previous match:
  1417. */
  1418. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  1419. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  1420. /* Do not insert strings in hash table beyond this. */
  1421. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  1422. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  1423. s.prev_length - MIN_MATCH, bflush);***/
  1424. bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
  1425. /* Insert in hash table all strings up to the end of the match.
  1426. * strstart-1 and strstart are already inserted. If there is not
  1427. * enough lookahead, the last two strings are not inserted in
  1428. * the hash table.
  1429. */
  1430. s.lookahead -= s.prev_length - 1;
  1431. s.prev_length -= 2;
  1432. do {
  1433. if (++s.strstart <= max_insert) {
  1434. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1435. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1436. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1437. s.head[s.ins_h] = s.strstart;
  1438. /***/
  1439. }
  1440. } while (--s.prev_length !== 0);
  1441. s.match_available = 0;
  1442. s.match_length = MIN_MATCH - 1;
  1443. s.strstart++;
  1444. if (bflush) {
  1445. /*** FLUSH_BLOCK(s, 0); ***/
  1446. flush_block_only(s, false);
  1447. if (s.strm.avail_out === 0) {
  1448. return BS_NEED_MORE;
  1449. }
  1450. /***/
  1451. }
  1452. } else if (s.match_available) {
  1453. /* If there was no match at the previous position, output a
  1454. * single literal. If there was a match but the current match
  1455. * is longer, truncate the previous match to a single literal.
  1456. */
  1457. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1458. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1459. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  1460. if (bflush) {
  1461. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  1462. flush_block_only(s, false);
  1463. /***/
  1464. }
  1465. s.strstart++;
  1466. s.lookahead--;
  1467. if (s.strm.avail_out === 0) {
  1468. return BS_NEED_MORE;
  1469. }
  1470. } else {
  1471. /* There is no previous match to compare with, wait for
  1472. * the next step to decide.
  1473. */
  1474. s.match_available = 1;
  1475. s.strstart++;
  1476. s.lookahead--;
  1477. }
  1478. }
  1479. //Assert (flush != Z_NO_FLUSH, "no flush?");
  1480. if (s.match_available) {
  1481. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1482. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1483. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  1484. s.match_available = 0;
  1485. }
  1486. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  1487. if (flush === Z_FINISH) {
  1488. /*** FLUSH_BLOCK(s, 1); ***/
  1489. flush_block_only(s, true);
  1490. if (s.strm.avail_out === 0) {
  1491. return BS_FINISH_STARTED;
  1492. }
  1493. /***/
  1494. return BS_FINISH_DONE;
  1495. }
  1496. if (s.last_lit) {
  1497. /*** FLUSH_BLOCK(s, 0); ***/
  1498. flush_block_only(s, false);
  1499. if (s.strm.avail_out === 0) {
  1500. return BS_NEED_MORE;
  1501. }
  1502. /***/
  1503. }
  1504. return BS_BLOCK_DONE;
  1505. }
  1506. /* ===========================================================================
  1507. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1508. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1509. * deflate switches away from Z_RLE.)
  1510. */
  1511. function deflate_rle(s, flush) {
  1512. var bflush; /* set if current block must be flushed */
  1513. var prev; /* byte at distance one to match */
  1514. var scan, strend; /* scan goes up to strend for length of run */
  1515. var _win = s.window;
  1516. for (;;) {
  1517. /* Make sure that we always have enough lookahead, except
  1518. * at the end of the input file. We need MAX_MATCH bytes
  1519. * for the longest run, plus one for the unrolled loop.
  1520. */
  1521. if (s.lookahead <= MAX_MATCH) {
  1522. fill_window(s);
  1523. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
  1524. return BS_NEED_MORE;
  1525. }
  1526. if (s.lookahead === 0) { break; } /* flush the current block */
  1527. }
  1528. /* See how many times the previous byte repeats */
  1529. s.match_length = 0;
  1530. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  1531. scan = s.strstart - 1;
  1532. prev = _win[scan];
  1533. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  1534. strend = s.strstart + MAX_MATCH;
  1535. do {
  1536. /*jshint noempty:false*/
  1537. } while (prev === _win[++scan] && prev === _win[++scan] &&
  1538. prev === _win[++scan] && prev === _win[++scan] &&
  1539. prev === _win[++scan] && prev === _win[++scan] &&
  1540. prev === _win[++scan] && prev === _win[++scan] &&
  1541. scan < strend);
  1542. s.match_length = MAX_MATCH - (strend - scan);
  1543. if (s.match_length > s.lookahead) {
  1544. s.match_length = s.lookahead;
  1545. }
  1546. }
  1547. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  1548. }
  1549. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1550. if (s.match_length >= MIN_MATCH) {
  1551. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  1552. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  1553. bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
  1554. s.lookahead -= s.match_length;
  1555. s.strstart += s.match_length;
  1556. s.match_length = 0;
  1557. } else {
  1558. /* No match, output a literal byte */
  1559. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1560. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1561. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1562. s.lookahead--;
  1563. s.strstart++;
  1564. }
  1565. if (bflush) {
  1566. /*** FLUSH_BLOCK(s, 0); ***/
  1567. flush_block_only(s, false);
  1568. if (s.strm.avail_out === 0) {
  1569. return BS_NEED_MORE;
  1570. }
  1571. /***/
  1572. }
  1573. }
  1574. s.insert = 0;
  1575. if (flush === Z_FINISH) {
  1576. /*** FLUSH_BLOCK(s, 1); ***/
  1577. flush_block_only(s, true);
  1578. if (s.strm.avail_out === 0) {
  1579. return BS_FINISH_STARTED;
  1580. }
  1581. /***/
  1582. return BS_FINISH_DONE;
  1583. }
  1584. if (s.last_lit) {
  1585. /*** FLUSH_BLOCK(s, 0); ***/
  1586. flush_block_only(s, false);
  1587. if (s.strm.avail_out === 0) {
  1588. return BS_NEED_MORE;
  1589. }
  1590. /***/
  1591. }
  1592. return BS_BLOCK_DONE;
  1593. }
  1594. /* ===========================================================================
  1595. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  1596. * (It will be regenerated if this run of deflate switches away from Huffman.)
  1597. */
  1598. function deflate_huff(s, flush) {
  1599. var bflush; /* set if current block must be flushed */
  1600. for (;;) {
  1601. /* Make sure that we have a literal to write. */
  1602. if (s.lookahead === 0) {
  1603. fill_window(s);
  1604. if (s.lookahead === 0) {
  1605. if (flush === Z_NO_FLUSH) {
  1606. return BS_NEED_MORE;
  1607. }
  1608. break; /* flush the current block */
  1609. }
  1610. }
  1611. /* Output a literal byte */
  1612. s.match_length = 0;
  1613. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1614. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1615. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1616. s.lookahead--;
  1617. s.strstart++;
  1618. if (bflush) {
  1619. /*** FLUSH_BLOCK(s, 0); ***/
  1620. flush_block_only(s, false);
  1621. if (s.strm.avail_out === 0) {
  1622. return BS_NEED_MORE;
  1623. }
  1624. /***/
  1625. }
  1626. }
  1627. s.insert = 0;
  1628. if (flush === Z_FINISH) {
  1629. /*** FLUSH_BLOCK(s, 1); ***/
  1630. flush_block_only(s, true);
  1631. if (s.strm.avail_out === 0) {
  1632. return BS_FINISH_STARTED;
  1633. }
  1634. /***/
  1635. return BS_FINISH_DONE;
  1636. }
  1637. if (s.last_lit) {
  1638. /*** FLUSH_BLOCK(s, 0); ***/
  1639. flush_block_only(s, false);
  1640. if (s.strm.avail_out === 0) {
  1641. return BS_NEED_MORE;
  1642. }
  1643. /***/
  1644. }
  1645. return BS_BLOCK_DONE;
  1646. }
  1647. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  1648. * the desired pack level (0..9). The values given below have been tuned to
  1649. * exclude worst case performance for pathological files. Better values may be
  1650. * found for specific files.
  1651. */
  1652. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  1653. this.good_length = good_length;
  1654. this.max_lazy = max_lazy;
  1655. this.nice_length = nice_length;
  1656. this.max_chain = max_chain;
  1657. this.func = func;
  1658. }
  1659. var configuration_table;
  1660. configuration_table = [
  1661. /* good lazy nice chain */
  1662. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  1663. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  1664. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  1665. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  1666. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  1667. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  1668. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  1669. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  1670. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  1671. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  1672. ];
  1673. /* ===========================================================================
  1674. * Initialize the "longest match" routines for a new zlib stream
  1675. */
  1676. function lm_init(s) {
  1677. s.window_size = 2 * s.w_size;
  1678. /*** CLEAR_HASH(s); ***/
  1679. zero(s.head); // Fill with NIL (= 0);
  1680. /* Set the default configuration parameters:
  1681. */
  1682. s.max_lazy_match = configuration_table[s.level].max_lazy;
  1683. s.good_match = configuration_table[s.level].good_length;
  1684. s.nice_match = configuration_table[s.level].nice_length;
  1685. s.max_chain_length = configuration_table[s.level].max_chain;
  1686. s.strstart = 0;
  1687. s.block_start = 0;
  1688. s.lookahead = 0;
  1689. s.insert = 0;
  1690. s.match_length = s.prev_length = MIN_MATCH - 1;
  1691. s.match_available = 0;
  1692. s.ins_h = 0;
  1693. }
  1694. function DeflateState() {
  1695. this.strm = null; /* pointer back to this zlib stream */
  1696. this.status = 0; /* as the name implies */
  1697. this.pending_buf = null; /* output still pending */
  1698. this.pending_buf_size = 0; /* size of pending_buf */
  1699. this.pending_out = 0; /* next pending byte to output to the stream */
  1700. this.pending = 0; /* nb of bytes in the pending buffer */
  1701. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  1702. this.gzhead = null; /* gzip header information to write */
  1703. this.gzindex = 0; /* where in extra, name, or comment */
  1704. this.method = Z_DEFLATED; /* can only be DEFLATED */
  1705. this.last_flush = -1; /* value of flush param for previous deflate call */
  1706. this.w_size = 0; /* LZ77 window size (32K by default) */
  1707. this.w_bits = 0; /* log2(w_size) (8..16) */
  1708. this.w_mask = 0; /* w_size - 1 */
  1709. this.window = null;
  1710. /* Sliding window. Input bytes are read into the second half of the window,
  1711. * and move to the first half later to keep a dictionary of at least wSize
  1712. * bytes. With this organization, matches are limited to a distance of
  1713. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  1714. * performed with a length multiple of the block size.
  1715. */
  1716. this.window_size = 0;
  1717. /* Actual size of window: 2*wSize, except when the user input buffer
  1718. * is directly used as sliding window.
  1719. */
  1720. this.prev = null;
  1721. /* Link to older string with same hash index. To limit the size of this
  1722. * array to 64K, this link is maintained only for the last 32K strings.
  1723. * An index in this array is thus a window index modulo 32K.
  1724. */
  1725. this.head = null; /* Heads of the hash chains or NIL. */
  1726. this.ins_h = 0; /* hash index of string to be inserted */
  1727. this.hash_size = 0; /* number of elements in hash table */
  1728. this.hash_bits = 0; /* log2(hash_size) */
  1729. this.hash_mask = 0; /* hash_size-1 */
  1730. this.hash_shift = 0;
  1731. /* Number of bits by which ins_h must be shifted at each input
  1732. * step. It must be such that after MIN_MATCH steps, the oldest
  1733. * byte no longer takes part in the hash key, that is:
  1734. * hash_shift * MIN_MATCH >= hash_bits
  1735. */
  1736. this.block_start = 0;
  1737. /* Window position at the beginning of the current output block. Gets
  1738. * negative when the window is moved backwards.
  1739. */
  1740. this.match_length = 0; /* length of best match */
  1741. this.prev_match = 0; /* previous match */
  1742. this.match_available = 0; /* set if previous match exists */
  1743. this.strstart = 0; /* start of string to insert */
  1744. this.match_start = 0; /* start of matching string */
  1745. this.lookahead = 0; /* number of valid bytes ahead in window */
  1746. this.prev_length = 0;
  1747. /* Length of the best match at previous step. Matches not greater than this
  1748. * are discarded. This is used in the lazy match evaluation.
  1749. */
  1750. this.max_chain_length = 0;
  1751. /* To speed up deflation, hash chains are never searched beyond this
  1752. * length. A higher limit improves compression ratio but degrades the
  1753. * speed.
  1754. */
  1755. this.max_lazy_match = 0;
  1756. /* Attempt to find a better match only when the current match is strictly
  1757. * smaller than this value. This mechanism is used only for compression
  1758. * levels >= 4.
  1759. */
  1760. // That's alias to max_lazy_match, don't use directly
  1761. //this.max_insert_length = 0;
  1762. /* Insert new strings in the hash table only if the match length is not
  1763. * greater than this length. This saves time but degrades compression.
  1764. * max_insert_length is used only for compression levels <= 3.
  1765. */
  1766. this.level = 0; /* compression level (1..9) */
  1767. this.strategy = 0; /* favor or force Huffman coding*/
  1768. this.good_match = 0;
  1769. /* Use a faster search when the previous match is longer than this */
  1770. this.nice_match = 0; /* Stop searching when current match exceeds this */
  1771. /* used by trees.c: */
  1772. /* Didn't use ct_data typedef below to suppress compiler warning */
  1773. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  1774. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  1775. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  1776. // Use flat array of DOUBLE size, with interleaved fata,
  1777. // because JS does not support effective
  1778. this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
  1779. this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
  1780. this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
  1781. zero(this.dyn_ltree);
  1782. zero(this.dyn_dtree);
  1783. zero(this.bl_tree);
  1784. this.l_desc = null; /* desc. for literal tree */
  1785. this.d_desc = null; /* desc. for distance tree */
  1786. this.bl_desc = null; /* desc. for bit length tree */
  1787. //ush bl_count[MAX_BITS+1];
  1788. this.bl_count = new utils.Buf16(MAX_BITS + 1);
  1789. /* number of codes at each bit length for an optimal tree */
  1790. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  1791. this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */
  1792. zero(this.heap);
  1793. this.heap_len = 0; /* number of elements in the heap */
  1794. this.heap_max = 0; /* element of largest frequency */
  1795. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  1796. * The same heap array is used to build all trees.
  1797. */
  1798. this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
  1799. zero(this.depth);
  1800. /* Depth of each subtree used as tie breaker for trees of equal frequency
  1801. */
  1802. this.l_buf = 0; /* buffer index for literals or lengths */
  1803. this.lit_bufsize = 0;
  1804. /* Size of match buffer for literals/lengths. There are 4 reasons for
  1805. * limiting lit_bufsize to 64K:
  1806. * - frequencies can be kept in 16 bit counters
  1807. * - if compression is not successful for the first block, all input
  1808. * data is still in the window so we can still emit a stored block even
  1809. * when input comes from standard input. (This can also be done for
  1810. * all blocks if lit_bufsize is not greater than 32K.)
  1811. * - if compression is not successful for a file smaller than 64K, we can
  1812. * even emit a stored file instead of a stored block (saving 5 bytes).
  1813. * This is applicable only for zip (not gzip or zlib).
  1814. * - creating new Huffman trees less frequently may not provide fast
  1815. * adaptation to changes in the input data statistics. (Take for
  1816. * example a binary file with poorly compressible code followed by
  1817. * a highly compressible string table.) Smaller buffer sizes give
  1818. * fast adaptation but have of course the overhead of transmitting
  1819. * trees more frequently.
  1820. * - I can't count above 4
  1821. */
  1822. this.last_lit = 0; /* running index in l_buf */
  1823. this.d_buf = 0;
  1824. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  1825. * the same number of elements. To use different lengths, an extra flag
  1826. * array would be necessary.
  1827. */
  1828. this.opt_len = 0; /* bit length of current block with optimal trees */
  1829. this.static_len = 0; /* bit length of current block with static trees */
  1830. this.matches = 0; /* number of string matches in current block */
  1831. this.insert = 0; /* bytes at end of window left to insert */
  1832. this.bi_buf = 0;
  1833. /* Output buffer. bits are inserted starting at the bottom (least
  1834. * significant bits).
  1835. */
  1836. this.bi_valid = 0;
  1837. /* Number of valid bits in bi_buf. All bits above the last valid bit
  1838. * are always zero.
  1839. */
  1840. // Used for window memory init. We safely ignore it for JS. That makes
  1841. // sense only for pointers and memory check tools.
  1842. //this.high_water = 0;
  1843. /* High water mark offset in window for initialized bytes -- bytes above
  1844. * this are set to zero in order to avoid memory check warnings when
  1845. * longest match routines access bytes past the input. This is then
  1846. * updated to the new high water mark.
  1847. */
  1848. }
  1849. function deflateResetKeep(strm) {
  1850. var s;
  1851. if (!strm || !strm.state) {
  1852. return err(strm, Z_STREAM_ERROR);
  1853. }
  1854. strm.total_in = strm.total_out = 0;
  1855. strm.data_type = Z_UNKNOWN;
  1856. s = strm.state;
  1857. s.pending = 0;
  1858. s.pending_out = 0;
  1859. if (s.wrap < 0) {
  1860. s.wrap = -s.wrap;
  1861. /* was made negative by deflate(..., Z_FINISH); */
  1862. }
  1863. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  1864. strm.adler = (s.wrap === 2) ?
  1865. 0 // crc32(0, Z_NULL, 0)
  1866. :
  1867. 1; // adler32(0, Z_NULL, 0)
  1868. s.last_flush = Z_NO_FLUSH;
  1869. trees._tr_init(s);
  1870. return Z_OK;
  1871. }
  1872. function deflateReset(strm) {
  1873. var ret = deflateResetKeep(strm);
  1874. if (ret === Z_OK) {
  1875. lm_init(strm.state);
  1876. }
  1877. return ret;
  1878. }
  1879. function deflateSetHeader(strm, head) {
  1880. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  1881. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
  1882. strm.state.gzhead = head;
  1883. return Z_OK;
  1884. }
  1885. function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  1886. if (!strm) { // === Z_NULL
  1887. return Z_STREAM_ERROR;
  1888. }
  1889. var wrap = 1;
  1890. if (level === Z_DEFAULT_COMPRESSION) {
  1891. level = 6;
  1892. }
  1893. if (windowBits < 0) { /* suppress zlib wrapper */
  1894. wrap = 0;
  1895. windowBits = -windowBits;
  1896. }
  1897. else if (windowBits > 15) {
  1898. wrap = 2; /* write gzip wrapper instead */
  1899. windowBits -= 16;
  1900. }
  1901. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
  1902. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  1903. strategy < 0 || strategy > Z_FIXED) {
  1904. return err(strm, Z_STREAM_ERROR);
  1905. }
  1906. if (windowBits === 8) {
  1907. windowBits = 9;
  1908. }
  1909. /* until 256-byte window bug fixed */
  1910. var s = new DeflateState();
  1911. strm.state = s;
  1912. s.strm = strm;
  1913. s.wrap = wrap;
  1914. s.gzhead = null;
  1915. s.w_bits = windowBits;
  1916. s.w_size = 1 << s.w_bits;
  1917. s.w_mask = s.w_size - 1;
  1918. s.hash_bits = memLevel + 7;
  1919. s.hash_size = 1 << s.hash_bits;
  1920. s.hash_mask = s.hash_size - 1;
  1921. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  1922. s.window = new utils.Buf8(s.w_size * 2);
  1923. s.head = new utils.Buf16(s.hash_size);
  1924. s.prev = new utils.Buf16(s.w_size);
  1925. // Don't need mem init magic for JS.
  1926. //s.high_water = 0; /* nothing written to s->window yet */
  1927. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  1928. s.pending_buf_size = s.lit_bufsize * 4;
  1929. //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  1930. //s->pending_buf = (uchf *) overlay;
  1931. s.pending_buf = new utils.Buf8(s.pending_buf_size);
  1932. // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
  1933. //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  1934. s.d_buf = 1 * s.lit_bufsize;
  1935. //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  1936. s.l_buf = (1 + 2) * s.lit_bufsize;
  1937. s.level = level;
  1938. s.strategy = strategy;
  1939. s.method = method;
  1940. return deflateReset(strm);
  1941. }
  1942. function deflateInit(strm, level) {
  1943. return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  1944. }
  1945. function deflate(strm, flush) {
  1946. var old_flush, s;
  1947. var beg, val; // for gzip header write only
  1948. if (!strm || !strm.state ||
  1949. flush > Z_BLOCK || flush < 0) {
  1950. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  1951. }
  1952. s = strm.state;
  1953. if (!strm.output ||
  1954. (!strm.input && strm.avail_in !== 0) ||
  1955. (s.status === FINISH_STATE && flush !== Z_FINISH)) {
  1956. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
  1957. }
  1958. s.strm = strm; /* just in case */
  1959. old_flush = s.last_flush;
  1960. s.last_flush = flush;
  1961. /* Write the header */
  1962. if (s.status === INIT_STATE) {
  1963. if (s.wrap === 2) { // GZIP header
  1964. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  1965. put_byte(s, 31);
  1966. put_byte(s, 139);
  1967. put_byte(s, 8);
  1968. if (!s.gzhead) { // s->gzhead == Z_NULL
  1969. put_byte(s, 0);
  1970. put_byte(s, 0);
  1971. put_byte(s, 0);
  1972. put_byte(s, 0);
  1973. put_byte(s, 0);
  1974. put_byte(s, s.level === 9 ? 2 :
  1975. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  1976. 4 : 0));
  1977. put_byte(s, OS_CODE);
  1978. s.status = BUSY_STATE;
  1979. }
  1980. else {
  1981. put_byte(s, (s.gzhead.text ? 1 : 0) +
  1982. (s.gzhead.hcrc ? 2 : 0) +
  1983. (!s.gzhead.extra ? 0 : 4) +
  1984. (!s.gzhead.name ? 0 : 8) +
  1985. (!s.gzhead.comment ? 0 : 16)
  1986. );
  1987. put_byte(s, s.gzhead.time & 0xff);
  1988. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  1989. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  1990. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  1991. put_byte(s, s.level === 9 ? 2 :
  1992. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  1993. 4 : 0));
  1994. put_byte(s, s.gzhead.os & 0xff);
  1995. if (s.gzhead.extra && s.gzhead.extra.length) {
  1996. put_byte(s, s.gzhead.extra.length & 0xff);
  1997. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  1998. }
  1999. if (s.gzhead.hcrc) {
  2000. strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
  2001. }
  2002. s.gzindex = 0;
  2003. s.status = EXTRA_STATE;
  2004. }
  2005. }
  2006. else // DEFLATE header
  2007. {
  2008. var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
  2009. var level_flags = -1;
  2010. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  2011. level_flags = 0;
  2012. } else if (s.level < 6) {
  2013. level_flags = 1;
  2014. } else if (s.level === 6) {
  2015. level_flags = 2;
  2016. } else {
  2017. level_flags = 3;
  2018. }
  2019. header |= (level_flags << 6);
  2020. if (s.strstart !== 0) { header |= PRESET_DICT; }
  2021. header += 31 - (header % 31);
  2022. s.status = BUSY_STATE;
  2023. putShortMSB(s, header);
  2024. /* Save the adler32 of the preset dictionary: */
  2025. if (s.strstart !== 0) {
  2026. putShortMSB(s, strm.adler >>> 16);
  2027. putShortMSB(s, strm.adler & 0xffff);
  2028. }
  2029. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  2030. }
  2031. }
  2032. //#ifdef GZIP
  2033. if (s.status === EXTRA_STATE) {
  2034. if (s.gzhead.extra/* != Z_NULL*/) {
  2035. beg = s.pending; /* start of bytes to update crc */
  2036. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  2037. if (s.pending === s.pending_buf_size) {
  2038. if (s.gzhead.hcrc && s.pending > beg) {
  2039. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2040. }
  2041. flush_pending(strm);
  2042. beg = s.pending;
  2043. if (s.pending === s.pending_buf_size) {
  2044. break;
  2045. }
  2046. }
  2047. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  2048. s.gzindex++;
  2049. }
  2050. if (s.gzhead.hcrc && s.pending > beg) {
  2051. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2052. }
  2053. if (s.gzindex === s.gzhead.extra.length) {
  2054. s.gzindex = 0;
  2055. s.status = NAME_STATE;
  2056. }
  2057. }
  2058. else {
  2059. s.status = NAME_STATE;
  2060. }
  2061. }
  2062. if (s.status === NAME_STATE) {
  2063. if (s.gzhead.name/* != Z_NULL*/) {
  2064. beg = s.pending; /* start of bytes to update crc */
  2065. //int val;
  2066. do {
  2067. if (s.pending === s.pending_buf_size) {
  2068. if (s.gzhead.hcrc && s.pending > beg) {
  2069. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2070. }
  2071. flush_pending(strm);
  2072. beg = s.pending;
  2073. if (s.pending === s.pending_buf_size) {
  2074. val = 1;
  2075. break;
  2076. }
  2077. }
  2078. // JS specific: little magic to add zero terminator to end of string
  2079. if (s.gzindex < s.gzhead.name.length) {
  2080. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  2081. } else {
  2082. val = 0;
  2083. }
  2084. put_byte(s, val);
  2085. } while (val !== 0);
  2086. if (s.gzhead.hcrc && s.pending > beg) {
  2087. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2088. }
  2089. if (val === 0) {
  2090. s.gzindex = 0;
  2091. s.status = COMMENT_STATE;
  2092. }
  2093. }
  2094. else {
  2095. s.status = COMMENT_STATE;
  2096. }
  2097. }
  2098. if (s.status === COMMENT_STATE) {
  2099. if (s.gzhead.comment/* != Z_NULL*/) {
  2100. beg = s.pending; /* start of bytes to update crc */
  2101. //int val;
  2102. do {
  2103. if (s.pending === s.pending_buf_size) {
  2104. if (s.gzhead.hcrc && s.pending > beg) {
  2105. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2106. }
  2107. flush_pending(strm);
  2108. beg = s.pending;
  2109. if (s.pending === s.pending_buf_size) {
  2110. val = 1;
  2111. break;
  2112. }
  2113. }
  2114. // JS specific: little magic to add zero terminator to end of string
  2115. if (s.gzindex < s.gzhead.comment.length) {
  2116. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  2117. } else {
  2118. val = 0;
  2119. }
  2120. put_byte(s, val);
  2121. } while (val !== 0);
  2122. if (s.gzhead.hcrc && s.pending > beg) {
  2123. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2124. }
  2125. if (val === 0) {
  2126. s.status = HCRC_STATE;
  2127. }
  2128. }
  2129. else {
  2130. s.status = HCRC_STATE;
  2131. }
  2132. }
  2133. if (s.status === HCRC_STATE) {
  2134. if (s.gzhead.hcrc) {
  2135. if (s.pending + 2 > s.pending_buf_size) {
  2136. flush_pending(strm);
  2137. }
  2138. if (s.pending + 2 <= s.pending_buf_size) {
  2139. put_byte(s, strm.adler & 0xff);
  2140. put_byte(s, (strm.adler >> 8) & 0xff);
  2141. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2142. s.status = BUSY_STATE;
  2143. }
  2144. }
  2145. else {
  2146. s.status = BUSY_STATE;
  2147. }
  2148. }
  2149. //#endif
  2150. /* Flush as much pending output as possible */
  2151. if (s.pending !== 0) {
  2152. flush_pending(strm);
  2153. if (strm.avail_out === 0) {
  2154. /* Since avail_out is 0, deflate will be called again with
  2155. * more output space, but possibly with both pending and
  2156. * avail_in equal to zero. There won't be anything to do,
  2157. * but this is not an error situation so make sure we
  2158. * return OK instead of BUF_ERROR at next call of deflate:
  2159. */
  2160. s.last_flush = -1;
  2161. return Z_OK;
  2162. }
  2163. /* Make sure there is something to do and avoid duplicate consecutive
  2164. * flushes. For repeated and useless calls with Z_FINISH, we keep
  2165. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  2166. */
  2167. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  2168. flush !== Z_FINISH) {
  2169. return err(strm, Z_BUF_ERROR);
  2170. }
  2171. /* User must not provide more input after the first FINISH: */
  2172. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  2173. return err(strm, Z_BUF_ERROR);
  2174. }
  2175. /* Start a new block or continue the current one.
  2176. */
  2177. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  2178. (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
  2179. var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  2180. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  2181. configuration_table[s.level].func(s, flush));
  2182. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  2183. s.status = FINISH_STATE;
  2184. }
  2185. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  2186. if (strm.avail_out === 0) {
  2187. s.last_flush = -1;
  2188. /* avoid BUF_ERROR next call, see above */
  2189. }
  2190. return Z_OK;
  2191. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  2192. * of deflate should use the same flush parameter to make sure
  2193. * that the flush is complete. So we don't have to output an
  2194. * empty block here, this will be done at next call. This also
  2195. * ensures that for a very small output buffer, we emit at most
  2196. * one empty block.
  2197. */
  2198. }
  2199. if (bstate === BS_BLOCK_DONE) {
  2200. if (flush === Z_PARTIAL_FLUSH) {
  2201. trees._tr_align(s);
  2202. }
  2203. else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  2204. trees._tr_stored_block(s, 0, 0, false);
  2205. /* For a full flush, this empty block will be recognized
  2206. * as a special marker by inflate_sync().
  2207. */
  2208. if (flush === Z_FULL_FLUSH) {
  2209. /*** CLEAR_HASH(s); ***/ /* forget history */
  2210. zero(s.head); // Fill with NIL (= 0);
  2211. if (s.lookahead === 0) {
  2212. s.strstart = 0;
  2213. s.block_start = 0;
  2214. s.insert = 0;
  2215. }
  2216. }
  2217. }
  2218. flush_pending(strm);
  2219. if (strm.avail_out === 0) {
  2220. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  2221. return Z_OK;
  2222. }
  2223. }
  2224. }
  2225. //Assert(strm->avail_out > 0, "bug2");
  2226. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  2227. if (flush !== Z_FINISH) { return Z_OK; }
  2228. if (s.wrap <= 0) { return Z_STREAM_END; }
  2229. /* Write the trailer */
  2230. if (s.wrap === 2) {
  2231. put_byte(s, strm.adler & 0xff);
  2232. put_byte(s, (strm.adler >> 8) & 0xff);
  2233. put_byte(s, (strm.adler >> 16) & 0xff);
  2234. put_byte(s, (strm.adler >> 24) & 0xff);
  2235. put_byte(s, strm.total_in & 0xff);
  2236. put_byte(s, (strm.total_in >> 8) & 0xff);
  2237. put_byte(s, (strm.total_in >> 16) & 0xff);
  2238. put_byte(s, (strm.total_in >> 24) & 0xff);
  2239. }
  2240. else
  2241. {
  2242. putShortMSB(s, strm.adler >>> 16);
  2243. putShortMSB(s, strm.adler & 0xffff);
  2244. }
  2245. flush_pending(strm);
  2246. /* If avail_out is zero, the application will call deflate again
  2247. * to flush the rest.
  2248. */
  2249. if (s.wrap > 0) { s.wrap = -s.wrap; }
  2250. /* write the trailer only once! */
  2251. return s.pending !== 0 ? Z_OK : Z_STREAM_END;
  2252. }
  2253. function deflateEnd(strm) {
  2254. var status;
  2255. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2256. return Z_STREAM_ERROR;
  2257. }
  2258. status = strm.state.status;
  2259. if (status !== INIT_STATE &&
  2260. status !== EXTRA_STATE &&
  2261. status !== NAME_STATE &&
  2262. status !== COMMENT_STATE &&
  2263. status !== HCRC_STATE &&
  2264. status !== BUSY_STATE &&
  2265. status !== FINISH_STATE
  2266. ) {
  2267. return err(strm, Z_STREAM_ERROR);
  2268. }
  2269. strm.state = null;
  2270. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
  2271. }
  2272. /* =========================================================================
  2273. * Initializes the compression dictionary from the given byte
  2274. * sequence without producing any compressed output.
  2275. */
  2276. function deflateSetDictionary(strm, dictionary) {
  2277. var dictLength = dictionary.length;
  2278. var s;
  2279. var str, n;
  2280. var wrap;
  2281. var avail;
  2282. var next;
  2283. var input;
  2284. var tmpDict;
  2285. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2286. return Z_STREAM_ERROR;
  2287. }
  2288. s = strm.state;
  2289. wrap = s.wrap;
  2290. if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
  2291. return Z_STREAM_ERROR;
  2292. }
  2293. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  2294. if (wrap === 1) {
  2295. /* adler32(strm->adler, dictionary, dictLength); */
  2296. strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
  2297. }
  2298. s.wrap = 0; /* avoid computing Adler-32 in read_buf */
  2299. /* if dictionary would fill window, just replace the history */
  2300. if (dictLength >= s.w_size) {
  2301. if (wrap === 0) { /* already empty otherwise */
  2302. /*** CLEAR_HASH(s); ***/
  2303. zero(s.head); // Fill with NIL (= 0);
  2304. s.strstart = 0;
  2305. s.block_start = 0;
  2306. s.insert = 0;
  2307. }
  2308. /* use the tail */
  2309. // dictionary = dictionary.slice(dictLength - s.w_size);
  2310. tmpDict = new utils.Buf8(s.w_size);
  2311. utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
  2312. dictionary = tmpDict;
  2313. dictLength = s.w_size;
  2314. }
  2315. /* insert dictionary into window and hash */
  2316. avail = strm.avail_in;
  2317. next = strm.next_in;
  2318. input = strm.input;
  2319. strm.avail_in = dictLength;
  2320. strm.next_in = 0;
  2321. strm.input = dictionary;
  2322. fill_window(s);
  2323. while (s.lookahead >= MIN_MATCH) {
  2324. str = s.strstart;
  2325. n = s.lookahead - (MIN_MATCH - 1);
  2326. do {
  2327. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  2328. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  2329. s.prev[str & s.w_mask] = s.head[s.ins_h];
  2330. s.head[s.ins_h] = str;
  2331. str++;
  2332. } while (--n);
  2333. s.strstart = str;
  2334. s.lookahead = MIN_MATCH - 1;
  2335. fill_window(s);
  2336. }
  2337. s.strstart += s.lookahead;
  2338. s.block_start = s.strstart;
  2339. s.insert = s.lookahead;
  2340. s.lookahead = 0;
  2341. s.match_length = s.prev_length = MIN_MATCH - 1;
  2342. s.match_available = 0;
  2343. strm.next_in = next;
  2344. strm.input = input;
  2345. strm.avail_in = avail;
  2346. s.wrap = wrap;
  2347. return Z_OK;
  2348. }
  2349. exports.deflateInit = deflateInit;
  2350. exports.deflateInit2 = deflateInit2;
  2351. exports.deflateReset = deflateReset;
  2352. exports.deflateResetKeep = deflateResetKeep;
  2353. exports.deflateSetHeader = deflateSetHeader;
  2354. exports.deflate = deflate;
  2355. exports.deflateEnd = deflateEnd;
  2356. exports.deflateSetDictionary = deflateSetDictionary;
  2357. exports.deflateInfo = 'pako deflate (from Nodeca project)';
  2358. /* Not implemented
  2359. exports.deflateBound = deflateBound;
  2360. exports.deflateCopy = deflateCopy;
  2361. exports.deflateParams = deflateParams;
  2362. exports.deflatePending = deflatePending;
  2363. exports.deflatePrime = deflatePrime;
  2364. exports.deflateTune = deflateTune;
  2365. */
  2366. },{"../utils/common":5,"./adler32":7,"./crc32":8,"./messages":10,"./trees":11}],10:[function(require,module,exports){
  2367. 'use strict';
  2368. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2369. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2370. //
  2371. // This software is provided 'as-is', without any express or implied
  2372. // warranty. In no event will the authors be held liable for any damages
  2373. // arising from the use of this software.
  2374. //
  2375. // Permission is granted to anyone to use this software for any purpose,
  2376. // including commercial applications, and to alter it and redistribute it
  2377. // freely, subject to the following restrictions:
  2378. //
  2379. // 1. The origin of this software must not be misrepresented; you must not
  2380. // claim that you wrote the original software. If you use this software
  2381. // in a product, an acknowledgment in the product documentation would be
  2382. // appreciated but is not required.
  2383. // 2. Altered source versions must be plainly marked as such, and must not be
  2384. // misrepresented as being the original software.
  2385. // 3. This notice may not be removed or altered from any source distribution.
  2386. module.exports = {
  2387. 2: 'need dictionary', /* Z_NEED_DICT 2 */
  2388. 1: 'stream end', /* Z_STREAM_END 1 */
  2389. 0: '', /* Z_OK 0 */
  2390. '-1': 'file error', /* Z_ERRNO (-1) */
  2391. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  2392. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  2393. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  2394. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  2395. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  2396. };
  2397. },{}],11:[function(require,module,exports){
  2398. 'use strict';
  2399. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2400. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2401. //
  2402. // This software is provided 'as-is', without any express or implied
  2403. // warranty. In no event will the authors be held liable for any damages
  2404. // arising from the use of this software.
  2405. //
  2406. // Permission is granted to anyone to use this software for any purpose,
  2407. // including commercial applications, and to alter it and redistribute it
  2408. // freely, subject to the following restrictions:
  2409. //
  2410. // 1. The origin of this software must not be misrepresented; you must not
  2411. // claim that you wrote the original software. If you use this software
  2412. // in a product, an acknowledgment in the product documentation would be
  2413. // appreciated but is not required.
  2414. // 2. Altered source versions must be plainly marked as such, and must not be
  2415. // misrepresented as being the original software.
  2416. // 3. This notice may not be removed or altered from any source distribution.
  2417. /* eslint-disable space-unary-ops */
  2418. var utils = require('../utils/common');
  2419. /* Public constants ==========================================================*/
  2420. /* ===========================================================================*/
  2421. //var Z_FILTERED = 1;
  2422. //var Z_HUFFMAN_ONLY = 2;
  2423. //var Z_RLE = 3;
  2424. var Z_FIXED = 4;
  2425. //var Z_DEFAULT_STRATEGY = 0;
  2426. /* Possible values of the data_type field (though see inflate()) */
  2427. var Z_BINARY = 0;
  2428. var Z_TEXT = 1;
  2429. //var Z_ASCII = 1; // = Z_TEXT
  2430. var Z_UNKNOWN = 2;
  2431. /*============================================================================*/
  2432. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  2433. // From zutil.h
  2434. var STORED_BLOCK = 0;
  2435. var STATIC_TREES = 1;
  2436. var DYN_TREES = 2;
  2437. /* The three kinds of block type */
  2438. var MIN_MATCH = 3;
  2439. var MAX_MATCH = 258;
  2440. /* The minimum and maximum match lengths */
  2441. // From deflate.h
  2442. /* ===========================================================================
  2443. * Internal compression state.
  2444. */
  2445. var LENGTH_CODES = 29;
  2446. /* number of length codes, not counting the special END_BLOCK code */
  2447. var LITERALS = 256;
  2448. /* number of literal bytes 0..255 */
  2449. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  2450. /* number of Literal or Length codes, including the END_BLOCK code */
  2451. var D_CODES = 30;
  2452. /* number of distance codes */
  2453. var BL_CODES = 19;
  2454. /* number of codes used to transfer the bit lengths */
  2455. var HEAP_SIZE = 2 * L_CODES + 1;
  2456. /* maximum heap size */
  2457. var MAX_BITS = 15;
  2458. /* All codes must not exceed MAX_BITS bits */
  2459. var Buf_size = 16;
  2460. /* size of bit buffer in bi_buf */
  2461. /* ===========================================================================
  2462. * Constants
  2463. */
  2464. var MAX_BL_BITS = 7;
  2465. /* Bit length codes must not exceed MAX_BL_BITS bits */
  2466. var END_BLOCK = 256;
  2467. /* end of block literal code */
  2468. var REP_3_6 = 16;
  2469. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  2470. var REPZ_3_10 = 17;
  2471. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  2472. var REPZ_11_138 = 18;
  2473. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  2474. /* eslint-disable comma-spacing,array-bracket-spacing */
  2475. var extra_lbits = /* extra bits for each length code */
  2476. [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0];
  2477. var extra_dbits = /* extra bits for each distance code */
  2478. [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];
  2479. var extra_blbits = /* extra bits for each bit length code */
  2480. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
  2481. var bl_order =
  2482. [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
  2483. /* eslint-enable comma-spacing,array-bracket-spacing */
  2484. /* The lengths of the bit length codes are sent in order of decreasing
  2485. * probability, to avoid transmitting the lengths for unused bit length codes.
  2486. */
  2487. /* ===========================================================================
  2488. * Local data. These are initialized only once.
  2489. */
  2490. // We pre-fill arrays with 0 to avoid uninitialized gaps
  2491. var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  2492. // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
  2493. var static_ltree = new Array((L_CODES + 2) * 2);
  2494. zero(static_ltree);
  2495. /* The static literal tree. Since the bit lengths are imposed, there is no
  2496. * need for the L_CODES extra codes used during heap construction. However
  2497. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  2498. * below).
  2499. */
  2500. var static_dtree = new Array(D_CODES * 2);
  2501. zero(static_dtree);
  2502. /* The static distance tree. (Actually a trivial tree since all codes use
  2503. * 5 bits.)
  2504. */
  2505. var _dist_code = new Array(DIST_CODE_LEN);
  2506. zero(_dist_code);
  2507. /* Distance codes. The first 256 values correspond to the distances
  2508. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  2509. * the 15 bit distances.
  2510. */
  2511. var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
  2512. zero(_length_code);
  2513. /* length code for each normalized match length (0 == MIN_MATCH) */
  2514. var base_length = new Array(LENGTH_CODES);
  2515. zero(base_length);
  2516. /* First normalized length for each code (0 = MIN_MATCH) */
  2517. var base_dist = new Array(D_CODES);
  2518. zero(base_dist);
  2519. /* First normalized distance for each code (0 = distance of 1) */
  2520. function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
  2521. this.static_tree = static_tree; /* static tree or NULL */
  2522. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  2523. this.extra_base = extra_base; /* base index for extra_bits */
  2524. this.elems = elems; /* max number of elements in the tree */
  2525. this.max_length = max_length; /* max bit length for the codes */
  2526. // show if `static_tree` has data or dummy - needed for monomorphic objects
  2527. this.has_stree = static_tree && static_tree.length;
  2528. }
  2529. var static_l_desc;
  2530. var static_d_desc;
  2531. var static_bl_desc;
  2532. function TreeDesc(dyn_tree, stat_desc) {
  2533. this.dyn_tree = dyn_tree; /* the dynamic tree */
  2534. this.max_code = 0; /* largest code with non zero frequency */
  2535. this.stat_desc = stat_desc; /* the corresponding static tree */
  2536. }
  2537. function d_code(dist) {
  2538. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  2539. }
  2540. /* ===========================================================================
  2541. * Output a short LSB first on the stream.
  2542. * IN assertion: there is enough room in pendingBuf.
  2543. */
  2544. function put_short(s, w) {
  2545. // put_byte(s, (uch)((w) & 0xff));
  2546. // put_byte(s, (uch)((ush)(w) >> 8));
  2547. s.pending_buf[s.pending++] = (w) & 0xff;
  2548. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  2549. }
  2550. /* ===========================================================================
  2551. * Send a value on a given number of bits.
  2552. * IN assertion: length <= 16 and value fits in length bits.
  2553. */
  2554. function send_bits(s, value, length) {
  2555. if (s.bi_valid > (Buf_size - length)) {
  2556. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  2557. put_short(s, s.bi_buf);
  2558. s.bi_buf = value >> (Buf_size - s.bi_valid);
  2559. s.bi_valid += length - Buf_size;
  2560. } else {
  2561. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  2562. s.bi_valid += length;
  2563. }
  2564. }
  2565. function send_code(s, c, tree) {
  2566. send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
  2567. }
  2568. /* ===========================================================================
  2569. * Reverse the first len bits of a code, using straightforward code (a faster
  2570. * method would use a table)
  2571. * IN assertion: 1 <= len <= 15
  2572. */
  2573. function bi_reverse(code, len) {
  2574. var res = 0;
  2575. do {
  2576. res |= code & 1;
  2577. code >>>= 1;
  2578. res <<= 1;
  2579. } while (--len > 0);
  2580. return res >>> 1;
  2581. }
  2582. /* ===========================================================================
  2583. * Flush the bit buffer, keeping at most 7 bits in it.
  2584. */
  2585. function bi_flush(s) {
  2586. if (s.bi_valid === 16) {
  2587. put_short(s, s.bi_buf);
  2588. s.bi_buf = 0;
  2589. s.bi_valid = 0;
  2590. } else if (s.bi_valid >= 8) {
  2591. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  2592. s.bi_buf >>= 8;
  2593. s.bi_valid -= 8;
  2594. }
  2595. }
  2596. /* ===========================================================================
  2597. * Compute the optimal bit lengths for a tree and update the total bit length
  2598. * for the current block.
  2599. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  2600. * above are the tree nodes sorted by increasing frequency.
  2601. * OUT assertions: the field len is set to the optimal bit length, the
  2602. * array bl_count contains the frequencies for each bit length.
  2603. * The length opt_len is updated; static_len is also updated if stree is
  2604. * not null.
  2605. */
  2606. function gen_bitlen(s, desc)
  2607. // deflate_state *s;
  2608. // tree_desc *desc; /* the tree descriptor */
  2609. {
  2610. var tree = desc.dyn_tree;
  2611. var max_code = desc.max_code;
  2612. var stree = desc.stat_desc.static_tree;
  2613. var has_stree = desc.stat_desc.has_stree;
  2614. var extra = desc.stat_desc.extra_bits;
  2615. var base = desc.stat_desc.extra_base;
  2616. var max_length = desc.stat_desc.max_length;
  2617. var h; /* heap index */
  2618. var n, m; /* iterate over the tree elements */
  2619. var bits; /* bit length */
  2620. var xbits; /* extra bits */
  2621. var f; /* frequency */
  2622. var overflow = 0; /* number of elements with bit length too large */
  2623. for (bits = 0; bits <= MAX_BITS; bits++) {
  2624. s.bl_count[bits] = 0;
  2625. }
  2626. /* In a first pass, compute the optimal bit lengths (which may
  2627. * overflow in the case of the bit length tree).
  2628. */
  2629. tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
  2630. for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
  2631. n = s.heap[h];
  2632. bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  2633. if (bits > max_length) {
  2634. bits = max_length;
  2635. overflow++;
  2636. }
  2637. tree[n * 2 + 1]/*.Len*/ = bits;
  2638. /* We overwrite tree[n].Dad which is no longer needed */
  2639. if (n > max_code) { continue; } /* not a leaf node */
  2640. s.bl_count[bits]++;
  2641. xbits = 0;
  2642. if (n >= base) {
  2643. xbits = extra[n - base];
  2644. }
  2645. f = tree[n * 2]/*.Freq*/;
  2646. s.opt_len += f * (bits + xbits);
  2647. if (has_stree) {
  2648. s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
  2649. }
  2650. }
  2651. if (overflow === 0) { return; }
  2652. // Trace((stderr,"\nbit length overflow\n"));
  2653. /* This happens for example on obj2 and pic of the Calgary corpus */
  2654. /* Find the first bit length which could increase: */
  2655. do {
  2656. bits = max_length - 1;
  2657. while (s.bl_count[bits] === 0) { bits--; }
  2658. s.bl_count[bits]--; /* move one leaf down the tree */
  2659. s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
  2660. s.bl_count[max_length]--;
  2661. /* The brother of the overflow item also moves one step up,
  2662. * but this does not affect bl_count[max_length]
  2663. */
  2664. overflow -= 2;
  2665. } while (overflow > 0);
  2666. /* Now recompute all bit lengths, scanning in increasing frequency.
  2667. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  2668. * lengths instead of fixing only the wrong ones. This idea is taken
  2669. * from 'ar' written by Haruhiko Okumura.)
  2670. */
  2671. for (bits = max_length; bits !== 0; bits--) {
  2672. n = s.bl_count[bits];
  2673. while (n !== 0) {
  2674. m = s.heap[--h];
  2675. if (m > max_code) { continue; }
  2676. if (tree[m * 2 + 1]/*.Len*/ !== bits) {
  2677. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  2678. s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
  2679. tree[m * 2 + 1]/*.Len*/ = bits;
  2680. }
  2681. n--;
  2682. }
  2683. }
  2684. }
  2685. /* ===========================================================================
  2686. * Generate the codes for a given tree and bit counts (which need not be
  2687. * optimal).
  2688. * IN assertion: the array bl_count contains the bit length statistics for
  2689. * the given tree and the field len is set for all tree elements.
  2690. * OUT assertion: the field code is set for all tree elements of non
  2691. * zero code length.
  2692. */
  2693. function gen_codes(tree, max_code, bl_count)
  2694. // ct_data *tree; /* the tree to decorate */
  2695. // int max_code; /* largest code with non zero frequency */
  2696. // ushf *bl_count; /* number of codes at each bit length */
  2697. {
  2698. var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */
  2699. var code = 0; /* running code value */
  2700. var bits; /* bit index */
  2701. var n; /* code index */
  2702. /* The distribution counts are first used to generate the code values
  2703. * without bit reversal.
  2704. */
  2705. for (bits = 1; bits <= MAX_BITS; bits++) {
  2706. next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
  2707. }
  2708. /* Check that the bit counts in bl_count are consistent. The last code
  2709. * must be all ones.
  2710. */
  2711. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  2712. // "inconsistent bit counts");
  2713. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  2714. for (n = 0; n <= max_code; n++) {
  2715. var len = tree[n * 2 + 1]/*.Len*/;
  2716. if (len === 0) { continue; }
  2717. /* Now reverse the bits */
  2718. tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  2719. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  2720. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  2721. }
  2722. }
  2723. /* ===========================================================================
  2724. * Initialize the various 'constant' tables.
  2725. */
  2726. function tr_static_init() {
  2727. var n; /* iterates over tree elements */
  2728. var bits; /* bit counter */
  2729. var length; /* length value */
  2730. var code; /* code value */
  2731. var dist; /* distance index */
  2732. var bl_count = new Array(MAX_BITS + 1);
  2733. /* number of codes at each bit length for an optimal tree */
  2734. // do check in _tr_init()
  2735. //if (static_init_done) return;
  2736. /* For some embedded targets, global variables are not initialized: */
  2737. /*#ifdef NO_INIT_GLOBAL_POINTERS
  2738. static_l_desc.static_tree = static_ltree;
  2739. static_l_desc.extra_bits = extra_lbits;
  2740. static_d_desc.static_tree = static_dtree;
  2741. static_d_desc.extra_bits = extra_dbits;
  2742. static_bl_desc.extra_bits = extra_blbits;
  2743. #endif*/
  2744. /* Initialize the mapping length (0..255) -> length code (0..28) */
  2745. length = 0;
  2746. for (code = 0; code < LENGTH_CODES - 1; code++) {
  2747. base_length[code] = length;
  2748. for (n = 0; n < (1 << extra_lbits[code]); n++) {
  2749. _length_code[length++] = code;
  2750. }
  2751. }
  2752. //Assert (length == 256, "tr_static_init: length != 256");
  2753. /* Note that the length 255 (match length 258) can be represented
  2754. * in two different ways: code 284 + 5 bits or code 285, so we
  2755. * overwrite length_code[255] to use the best encoding:
  2756. */
  2757. _length_code[length - 1] = code;
  2758. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  2759. dist = 0;
  2760. for (code = 0; code < 16; code++) {
  2761. base_dist[code] = dist;
  2762. for (n = 0; n < (1 << extra_dbits[code]); n++) {
  2763. _dist_code[dist++] = code;
  2764. }
  2765. }
  2766. //Assert (dist == 256, "tr_static_init: dist != 256");
  2767. dist >>= 7; /* from now on, all distances are divided by 128 */
  2768. for (; code < D_CODES; code++) {
  2769. base_dist[code] = dist << 7;
  2770. for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
  2771. _dist_code[256 + dist++] = code;
  2772. }
  2773. }
  2774. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  2775. /* Construct the codes of the static literal tree */
  2776. for (bits = 0; bits <= MAX_BITS; bits++) {
  2777. bl_count[bits] = 0;
  2778. }
  2779. n = 0;
  2780. while (n <= 143) {
  2781. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  2782. n++;
  2783. bl_count[8]++;
  2784. }
  2785. while (n <= 255) {
  2786. static_ltree[n * 2 + 1]/*.Len*/ = 9;
  2787. n++;
  2788. bl_count[9]++;
  2789. }
  2790. while (n <= 279) {
  2791. static_ltree[n * 2 + 1]/*.Len*/ = 7;
  2792. n++;
  2793. bl_count[7]++;
  2794. }
  2795. while (n <= 287) {
  2796. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  2797. n++;
  2798. bl_count[8]++;
  2799. }
  2800. /* Codes 286 and 287 do not exist, but we must include them in the
  2801. * tree construction to get a canonical Huffman tree (longest code
  2802. * all ones)
  2803. */
  2804. gen_codes(static_ltree, L_CODES + 1, bl_count);
  2805. /* The static distance tree is trivial: */
  2806. for (n = 0; n < D_CODES; n++) {
  2807. static_dtree[n * 2 + 1]/*.Len*/ = 5;
  2808. static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
  2809. }
  2810. // Now data ready and we can init static trees
  2811. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
  2812. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
  2813. static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  2814. //static_init_done = true;
  2815. }
  2816. /* ===========================================================================
  2817. * Initialize a new block.
  2818. */
  2819. function init_block(s) {
  2820. var n; /* iterates over tree elements */
  2821. /* Initialize the trees. */
  2822. for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
  2823. for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
  2824. for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
  2825. s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
  2826. s.opt_len = s.static_len = 0;
  2827. s.last_lit = s.matches = 0;
  2828. }
  2829. /* ===========================================================================
  2830. * Flush the bit buffer and align the output on a byte boundary
  2831. */
  2832. function bi_windup(s)
  2833. {
  2834. if (s.bi_valid > 8) {
  2835. put_short(s, s.bi_buf);
  2836. } else if (s.bi_valid > 0) {
  2837. //put_byte(s, (Byte)s->bi_buf);
  2838. s.pending_buf[s.pending++] = s.bi_buf;
  2839. }
  2840. s.bi_buf = 0;
  2841. s.bi_valid = 0;
  2842. }
  2843. /* ===========================================================================
  2844. * Copy a stored block, storing first the length and its
  2845. * one's complement if requested.
  2846. */
  2847. function copy_block(s, buf, len, header)
  2848. //DeflateState *s;
  2849. //charf *buf; /* the input data */
  2850. //unsigned len; /* its length */
  2851. //int header; /* true if block header must be written */
  2852. {
  2853. bi_windup(s); /* align on byte boundary */
  2854. if (header) {
  2855. put_short(s, len);
  2856. put_short(s, ~len);
  2857. }
  2858. // while (len--) {
  2859. // put_byte(s, *buf++);
  2860. // }
  2861. utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
  2862. s.pending += len;
  2863. }
  2864. /* ===========================================================================
  2865. * Compares to subtrees, using the tree depth as tie breaker when
  2866. * the subtrees have equal frequency. This minimizes the worst case length.
  2867. */
  2868. function smaller(tree, n, m, depth) {
  2869. var _n2 = n * 2;
  2870. var _m2 = m * 2;
  2871. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  2872. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  2873. }
  2874. /* ===========================================================================
  2875. * Restore the heap property by moving down the tree starting at node k,
  2876. * exchanging a node with the smallest of its two sons if necessary, stopping
  2877. * when the heap property is re-established (each father smaller than its
  2878. * two sons).
  2879. */
  2880. function pqdownheap(s, tree, k)
  2881. // deflate_state *s;
  2882. // ct_data *tree; /* the tree to restore */
  2883. // int k; /* node to move down */
  2884. {
  2885. var v = s.heap[k];
  2886. var j = k << 1; /* left son of k */
  2887. while (j <= s.heap_len) {
  2888. /* Set j to the smallest of the two sons: */
  2889. if (j < s.heap_len &&
  2890. smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
  2891. j++;
  2892. }
  2893. /* Exit if v is smaller than both sons */
  2894. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  2895. /* Exchange v with the smallest son */
  2896. s.heap[k] = s.heap[j];
  2897. k = j;
  2898. /* And continue down the tree, setting j to the left son of k */
  2899. j <<= 1;
  2900. }
  2901. s.heap[k] = v;
  2902. }
  2903. // inlined manually
  2904. // var SMALLEST = 1;
  2905. /* ===========================================================================
  2906. * Send the block data compressed using the given Huffman trees
  2907. */
  2908. function compress_block(s, ltree, dtree)
  2909. // deflate_state *s;
  2910. // const ct_data *ltree; /* literal tree */
  2911. // const ct_data *dtree; /* distance tree */
  2912. {
  2913. var dist; /* distance of matched string */
  2914. var lc; /* match length or unmatched char (if dist == 0) */
  2915. var lx = 0; /* running index in l_buf */
  2916. var code; /* the code to send */
  2917. var extra; /* number of extra bits to send */
  2918. if (s.last_lit !== 0) {
  2919. do {
  2920. dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]);
  2921. lc = s.pending_buf[s.l_buf + lx];
  2922. lx++;
  2923. if (dist === 0) {
  2924. send_code(s, lc, ltree); /* send a literal byte */
  2925. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  2926. } else {
  2927. /* Here, lc is the match length - MIN_MATCH */
  2928. code = _length_code[lc];
  2929. send_code(s, code + LITERALS + 1, ltree); /* send the length code */
  2930. extra = extra_lbits[code];
  2931. if (extra !== 0) {
  2932. lc -= base_length[code];
  2933. send_bits(s, lc, extra); /* send the extra length bits */
  2934. }
  2935. dist--; /* dist is now the match distance - 1 */
  2936. code = d_code(dist);
  2937. //Assert (code < D_CODES, "bad d_code");
  2938. send_code(s, code, dtree); /* send the distance code */
  2939. extra = extra_dbits[code];
  2940. if (extra !== 0) {
  2941. dist -= base_dist[code];
  2942. send_bits(s, dist, extra); /* send the extra distance bits */
  2943. }
  2944. } /* literal or match pair ? */
  2945. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  2946. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  2947. // "pendingBuf overflow");
  2948. } while (lx < s.last_lit);
  2949. }
  2950. send_code(s, END_BLOCK, ltree);
  2951. }
  2952. /* ===========================================================================
  2953. * Construct one Huffman tree and assigns the code bit strings and lengths.
  2954. * Update the total bit length for the current block.
  2955. * IN assertion: the field freq is set for all tree elements.
  2956. * OUT assertions: the fields len and code are set to the optimal bit length
  2957. * and corresponding code. The length opt_len is updated; static_len is
  2958. * also updated if stree is not null. The field max_code is set.
  2959. */
  2960. function build_tree(s, desc)
  2961. // deflate_state *s;
  2962. // tree_desc *desc; /* the tree descriptor */
  2963. {
  2964. var tree = desc.dyn_tree;
  2965. var stree = desc.stat_desc.static_tree;
  2966. var has_stree = desc.stat_desc.has_stree;
  2967. var elems = desc.stat_desc.elems;
  2968. var n, m; /* iterate over heap elements */
  2969. var max_code = -1; /* largest code with non zero frequency */
  2970. var node; /* new node being created */
  2971. /* Construct the initial heap, with least frequent element in
  2972. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  2973. * heap[0] is not used.
  2974. */
  2975. s.heap_len = 0;
  2976. s.heap_max = HEAP_SIZE;
  2977. for (n = 0; n < elems; n++) {
  2978. if (tree[n * 2]/*.Freq*/ !== 0) {
  2979. s.heap[++s.heap_len] = max_code = n;
  2980. s.depth[n] = 0;
  2981. } else {
  2982. tree[n * 2 + 1]/*.Len*/ = 0;
  2983. }
  2984. }
  2985. /* The pkzip format requires that at least one distance code exists,
  2986. * and that at least one bit should be sent even if there is only one
  2987. * possible code. So to avoid special checks later on we force at least
  2988. * two codes of non zero frequency.
  2989. */
  2990. while (s.heap_len < 2) {
  2991. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  2992. tree[node * 2]/*.Freq*/ = 1;
  2993. s.depth[node] = 0;
  2994. s.opt_len--;
  2995. if (has_stree) {
  2996. s.static_len -= stree[node * 2 + 1]/*.Len*/;
  2997. }
  2998. /* node is 0 or 1 so it does not have extra bits */
  2999. }
  3000. desc.max_code = max_code;
  3001. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  3002. * establish sub-heaps of increasing lengths:
  3003. */
  3004. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  3005. /* Construct the Huffman tree by repeatedly combining the least two
  3006. * frequent nodes.
  3007. */
  3008. node = elems; /* next internal node of the tree */
  3009. do {
  3010. //pqremove(s, tree, n); /* n = node of least frequency */
  3011. /*** pqremove ***/
  3012. n = s.heap[1/*SMALLEST*/];
  3013. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  3014. pqdownheap(s, tree, 1/*SMALLEST*/);
  3015. /***/
  3016. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  3017. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  3018. s.heap[--s.heap_max] = m;
  3019. /* Create a new node father of n and m */
  3020. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  3021. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  3022. tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
  3023. /* and insert the new node in the heap */
  3024. s.heap[1/*SMALLEST*/] = node++;
  3025. pqdownheap(s, tree, 1/*SMALLEST*/);
  3026. } while (s.heap_len >= 2);
  3027. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  3028. /* At this point, the fields freq and dad are set. We can now
  3029. * generate the bit lengths.
  3030. */
  3031. gen_bitlen(s, desc);
  3032. /* The field len is now set, we can generate the bit codes */
  3033. gen_codes(tree, max_code, s.bl_count);
  3034. }
  3035. /* ===========================================================================
  3036. * Scan a literal or distance tree to determine the frequencies of the codes
  3037. * in the bit length tree.
  3038. */
  3039. function scan_tree(s, tree, max_code)
  3040. // deflate_state *s;
  3041. // ct_data *tree; /* the tree to be scanned */
  3042. // int max_code; /* and its largest code of non zero frequency */
  3043. {
  3044. var n; /* iterates over all tree elements */
  3045. var prevlen = -1; /* last emitted length */
  3046. var curlen; /* length of current code */
  3047. var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  3048. var count = 0; /* repeat count of the current code */
  3049. var max_count = 7; /* max repeat count */
  3050. var min_count = 4; /* min repeat count */
  3051. if (nextlen === 0) {
  3052. max_count = 138;
  3053. min_count = 3;
  3054. }
  3055. tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
  3056. for (n = 0; n <= max_code; n++) {
  3057. curlen = nextlen;
  3058. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  3059. if (++count < max_count && curlen === nextlen) {
  3060. continue;
  3061. } else if (count < min_count) {
  3062. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  3063. } else if (curlen !== 0) {
  3064. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  3065. s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
  3066. } else if (count <= 10) {
  3067. s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
  3068. } else {
  3069. s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
  3070. }
  3071. count = 0;
  3072. prevlen = curlen;
  3073. if (nextlen === 0) {
  3074. max_count = 138;
  3075. min_count = 3;
  3076. } else if (curlen === nextlen) {
  3077. max_count = 6;
  3078. min_count = 3;
  3079. } else {
  3080. max_count = 7;
  3081. min_count = 4;
  3082. }
  3083. }
  3084. }
  3085. /* ===========================================================================
  3086. * Send a literal or distance tree in compressed form, using the codes in
  3087. * bl_tree.
  3088. */
  3089. function send_tree(s, tree, max_code)
  3090. // deflate_state *s;
  3091. // ct_data *tree; /* the tree to be scanned */
  3092. // int max_code; /* and its largest code of non zero frequency */
  3093. {
  3094. var n; /* iterates over all tree elements */
  3095. var prevlen = -1; /* last emitted length */
  3096. var curlen; /* length of current code */
  3097. var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  3098. var count = 0; /* repeat count of the current code */
  3099. var max_count = 7; /* max repeat count */
  3100. var min_count = 4; /* min repeat count */
  3101. /* tree[max_code+1].Len = -1; */ /* guard already set */
  3102. if (nextlen === 0) {
  3103. max_count = 138;
  3104. min_count = 3;
  3105. }
  3106. for (n = 0; n <= max_code; n++) {
  3107. curlen = nextlen;
  3108. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  3109. if (++count < max_count && curlen === nextlen) {
  3110. continue;
  3111. } else if (count < min_count) {
  3112. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  3113. } else if (curlen !== 0) {
  3114. if (curlen !== prevlen) {
  3115. send_code(s, curlen, s.bl_tree);
  3116. count--;
  3117. }
  3118. //Assert(count >= 3 && count <= 6, " 3_6?");
  3119. send_code(s, REP_3_6, s.bl_tree);
  3120. send_bits(s, count - 3, 2);
  3121. } else if (count <= 10) {
  3122. send_code(s, REPZ_3_10, s.bl_tree);
  3123. send_bits(s, count - 3, 3);
  3124. } else {
  3125. send_code(s, REPZ_11_138, s.bl_tree);
  3126. send_bits(s, count - 11, 7);
  3127. }
  3128. count = 0;
  3129. prevlen = curlen;
  3130. if (nextlen === 0) {
  3131. max_count = 138;
  3132. min_count = 3;
  3133. } else if (curlen === nextlen) {
  3134. max_count = 6;
  3135. min_count = 3;
  3136. } else {
  3137. max_count = 7;
  3138. min_count = 4;
  3139. }
  3140. }
  3141. }
  3142. /* ===========================================================================
  3143. * Construct the Huffman tree for the bit lengths and return the index in
  3144. * bl_order of the last bit length code to send.
  3145. */
  3146. function build_bl_tree(s) {
  3147. var max_blindex; /* index of last bit length code of non zero freq */
  3148. /* Determine the bit length frequencies for literal and distance trees */
  3149. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  3150. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  3151. /* Build the bit length tree: */
  3152. build_tree(s, s.bl_desc);
  3153. /* opt_len now includes the length of the tree representations, except
  3154. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  3155. */
  3156. /* Determine the number of bit length codes to send. The pkzip format
  3157. * requires that at least 4 bit length codes be sent. (appnote.txt says
  3158. * 3 but the actual value used is 4.)
  3159. */
  3160. for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
  3161. if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
  3162. break;
  3163. }
  3164. }
  3165. /* Update opt_len to include the bit length tree and counts */
  3166. s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  3167. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  3168. // s->opt_len, s->static_len));
  3169. return max_blindex;
  3170. }
  3171. /* ===========================================================================
  3172. * Send the header for a block using dynamic Huffman trees: the counts, the
  3173. * lengths of the bit length codes, the literal tree and the distance tree.
  3174. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  3175. */
  3176. function send_all_trees(s, lcodes, dcodes, blcodes)
  3177. // deflate_state *s;
  3178. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  3179. {
  3180. var rank; /* index in bl_order */
  3181. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  3182. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  3183. // "too many codes");
  3184. //Tracev((stderr, "\nbl counts: "));
  3185. send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
  3186. send_bits(s, dcodes - 1, 5);
  3187. send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
  3188. for (rank = 0; rank < blcodes; rank++) {
  3189. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  3190. send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
  3191. }
  3192. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  3193. send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
  3194. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  3195. send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
  3196. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  3197. }
  3198. /* ===========================================================================
  3199. * Check if the data type is TEXT or BINARY, using the following algorithm:
  3200. * - TEXT if the two conditions below are satisfied:
  3201. * a) There are no non-portable control characters belonging to the
  3202. * "black list" (0..6, 14..25, 28..31).
  3203. * b) There is at least one printable character belonging to the
  3204. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  3205. * - BINARY otherwise.
  3206. * - The following partially-portable control characters form a
  3207. * "gray list" that is ignored in this detection algorithm:
  3208. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  3209. * IN assertion: the fields Freq of dyn_ltree are set.
  3210. */
  3211. function detect_data_type(s) {
  3212. /* black_mask is the bit mask of black-listed bytes
  3213. * set bits 0..6, 14..25, and 28..31
  3214. * 0xf3ffc07f = binary 11110011111111111100000001111111
  3215. */
  3216. var black_mask = 0xf3ffc07f;
  3217. var n;
  3218. /* Check for non-textual ("black-listed") bytes. */
  3219. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  3220. if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
  3221. return Z_BINARY;
  3222. }
  3223. }
  3224. /* Check for textual ("white-listed") bytes. */
  3225. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  3226. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  3227. return Z_TEXT;
  3228. }
  3229. for (n = 32; n < LITERALS; n++) {
  3230. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  3231. return Z_TEXT;
  3232. }
  3233. }
  3234. /* There are no "black-listed" or "white-listed" bytes:
  3235. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  3236. */
  3237. return Z_BINARY;
  3238. }
  3239. var static_init_done = false;
  3240. /* ===========================================================================
  3241. * Initialize the tree data structures for a new zlib stream.
  3242. */
  3243. function _tr_init(s)
  3244. {
  3245. if (!static_init_done) {
  3246. tr_static_init();
  3247. static_init_done = true;
  3248. }
  3249. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  3250. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  3251. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  3252. s.bi_buf = 0;
  3253. s.bi_valid = 0;
  3254. /* Initialize the first block of the first file: */
  3255. init_block(s);
  3256. }
  3257. /* ===========================================================================
  3258. * Send a stored block
  3259. */
  3260. function _tr_stored_block(s, buf, stored_len, last)
  3261. //DeflateState *s;
  3262. //charf *buf; /* input block */
  3263. //ulg stored_len; /* length of input block */
  3264. //int last; /* one if this is the last block for a file */
  3265. {
  3266. send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
  3267. copy_block(s, buf, stored_len, true); /* with header */
  3268. }
  3269. /* ===========================================================================
  3270. * Send one empty static block to give enough lookahead for inflate.
  3271. * This takes 10 bits, of which 7 may remain in the bit buffer.
  3272. */
  3273. function _tr_align(s) {
  3274. send_bits(s, STATIC_TREES << 1, 3);
  3275. send_code(s, END_BLOCK, static_ltree);
  3276. bi_flush(s);
  3277. }
  3278. /* ===========================================================================
  3279. * Determine the best encoding for the current block: dynamic trees, static
  3280. * trees or store, and output the encoded block to the zip file.
  3281. */
  3282. function _tr_flush_block(s, buf, stored_len, last)
  3283. //DeflateState *s;
  3284. //charf *buf; /* input block, or NULL if too old */
  3285. //ulg stored_len; /* length of input block */
  3286. //int last; /* one if this is the last block for a file */
  3287. {
  3288. var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  3289. var max_blindex = 0; /* index of last bit length code of non zero freq */
  3290. /* Build the Huffman trees unless a stored block is forced */
  3291. if (s.level > 0) {
  3292. /* Check if the file is binary or text */
  3293. if (s.strm.data_type === Z_UNKNOWN) {
  3294. s.strm.data_type = detect_data_type(s);
  3295. }
  3296. /* Construct the literal and distance trees */
  3297. build_tree(s, s.l_desc);
  3298. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  3299. // s->static_len));
  3300. build_tree(s, s.d_desc);
  3301. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  3302. // s->static_len));
  3303. /* At this point, opt_len and static_len are the total bit lengths of
  3304. * the compressed block data, excluding the tree representations.
  3305. */
  3306. /* Build the bit length tree for the above two trees, and get the index
  3307. * in bl_order of the last bit length code to send.
  3308. */
  3309. max_blindex = build_bl_tree(s);
  3310. /* Determine the best encoding. Compute the block lengths in bytes. */
  3311. opt_lenb = (s.opt_len + 3 + 7) >>> 3;
  3312. static_lenb = (s.static_len + 3 + 7) >>> 3;
  3313. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  3314. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  3315. // s->last_lit));
  3316. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  3317. } else {
  3318. // Assert(buf != (char*)0, "lost buf");
  3319. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  3320. }
  3321. if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
  3322. /* 4: two words for the lengths */
  3323. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  3324. * Otherwise we can't have processed more than WSIZE input bytes since
  3325. * the last block flush, because compression would have been
  3326. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  3327. * transform a block into a stored block.
  3328. */
  3329. _tr_stored_block(s, buf, stored_len, last);
  3330. } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
  3331. send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
  3332. compress_block(s, static_ltree, static_dtree);
  3333. } else {
  3334. send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
  3335. send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
  3336. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  3337. }
  3338. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  3339. /* The above check is made mod 2^32, for files larger than 512 MB
  3340. * and uLong implemented on 32 bits.
  3341. */
  3342. init_block(s);
  3343. if (last) {
  3344. bi_windup(s);
  3345. }
  3346. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  3347. // s->compressed_len-7*last));
  3348. }
  3349. /* ===========================================================================
  3350. * Save the match info and tally the frequency counts. Return true if
  3351. * the current block must be flushed.
  3352. */
  3353. function _tr_tally(s, dist, lc)
  3354. // deflate_state *s;
  3355. // unsigned dist; /* distance of matched string */
  3356. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  3357. {
  3358. //var out_length, in_length, dcode;
  3359. s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
  3360. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  3361. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  3362. s.last_lit++;
  3363. if (dist === 0) {
  3364. /* lc is the unmatched char */
  3365. s.dyn_ltree[lc * 2]/*.Freq*/++;
  3366. } else {
  3367. s.matches++;
  3368. /* Here, lc is the match length - MIN_MATCH */
  3369. dist--; /* dist = match distance - 1 */
  3370. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  3371. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  3372. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  3373. s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++;
  3374. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  3375. }
  3376. // (!) This block is disabled in zlib defaults,
  3377. // don't enable it for binary compatibility
  3378. //#ifdef TRUNCATE_BLOCK
  3379. // /* Try to guess if it is profitable to stop the current block here */
  3380. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  3381. // /* Compute an upper bound for the compressed length */
  3382. // out_length = s.last_lit*8;
  3383. // in_length = s.strstart - s.block_start;
  3384. //
  3385. // for (dcode = 0; dcode < D_CODES; dcode++) {
  3386. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  3387. // }
  3388. // out_length >>>= 3;
  3389. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  3390. // // s->last_lit, in_length, out_length,
  3391. // // 100L - out_length*100L/in_length));
  3392. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  3393. // return true;
  3394. // }
  3395. // }
  3396. //#endif
  3397. return (s.last_lit === s.lit_bufsize - 1);
  3398. /* We avoid equality with lit_bufsize because of wraparound at 64K
  3399. * on 16 bit machines and because stored blocks are restricted to
  3400. * 64K-1 bytes.
  3401. */
  3402. }
  3403. exports._tr_init = _tr_init;
  3404. exports._tr_stored_block = _tr_stored_block;
  3405. exports._tr_flush_block = _tr_flush_block;
  3406. exports._tr_tally = _tr_tally;
  3407. exports._tr_align = _tr_align;
  3408. },{"../utils/common":5}],12:[function(require,module,exports){
  3409. 'use strict';
  3410. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3411. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3412. //
  3413. // This software is provided 'as-is', without any express or implied
  3414. // warranty. In no event will the authors be held liable for any damages
  3415. // arising from the use of this software.
  3416. //
  3417. // Permission is granted to anyone to use this software for any purpose,
  3418. // including commercial applications, and to alter it and redistribute it
  3419. // freely, subject to the following restrictions:
  3420. //
  3421. // 1. The origin of this software must not be misrepresented; you must not
  3422. // claim that you wrote the original software. If you use this software
  3423. // in a product, an acknowledgment in the product documentation would be
  3424. // appreciated but is not required.
  3425. // 2. Altered source versions must be plainly marked as such, and must not be
  3426. // misrepresented as being the original software.
  3427. // 3. This notice may not be removed or altered from any source distribution.
  3428. function ZStream() {
  3429. /* next input byte */
  3430. this.input = null; // JS specific, because we have no pointers
  3431. this.next_in = 0;
  3432. /* number of bytes available at input */
  3433. this.avail_in = 0;
  3434. /* total number of input bytes read so far */
  3435. this.total_in = 0;
  3436. /* next output byte should be put there */
  3437. this.output = null; // JS specific, because we have no pointers
  3438. this.next_out = 0;
  3439. /* remaining free space at output */
  3440. this.avail_out = 0;
  3441. /* total number of bytes output so far */
  3442. this.total_out = 0;
  3443. /* last error message, NULL if no error */
  3444. this.msg = ''/*Z_NULL*/;
  3445. /* not visible by applications */
  3446. this.state = null;
  3447. /* best guess about the data type: binary or text */
  3448. this.data_type = 2/*Z_UNKNOWN*/;
  3449. /* adler32 value of the uncompressed data */
  3450. this.adler = 0;
  3451. }
  3452. module.exports = ZStream;
  3453. },{}]},{},[3])(3)
  3454. });