pako_deflate.es5.js 139 KB

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