You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pako.bundle.js 115KB

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