pako_deflate.js 125 KB

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