pako_deflate.js 119 KB

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