deflate.js 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  1. /*
  2. * $Id: rawinflate.js,v 0.3 2013/04/09 14:25:38 dankogai Exp dankogai $
  3. *
  4. * GNU General Public License, version 2 (GPL-2.0)
  5. * http://opensource.org/licenses/GPL-2.0
  6. * original:
  7. * http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
  8. */
  9. (function(ctx){
  10. /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
  11. * Version: 1.0.0.1
  12. * LastModified: Dec 25 1999
  13. */
  14. /* Interface:
  15. * data = zip_inflate(src);
  16. */
  17. /* constant parameters */
  18. var zip_WSIZE = 32768; // Sliding Window size
  19. var zip_STORED_BLOCK = 0;
  20. var zip_STATIC_TREES = 1;
  21. var zip_DYN_TREES = 2;
  22. /* for inflate */
  23. var zip_lbits = 9; // bits in base literal/length lookup table
  24. var zip_dbits = 6; // bits in base distance lookup table
  25. var zip_INBUFSIZ = 32768; // Input buffer size
  26. var zip_INBUF_EXTRA = 64; // Extra buffer
  27. /* variables (inflate) */
  28. var zip_slide;
  29. var zip_wp; // current position in slide
  30. var zip_fixed_tl = null; // inflate static
  31. var zip_fixed_td; // inflate static
  32. var zip_fixed_bl, zip_fixed_bd; // inflate static
  33. var zip_bit_buf; // bit buffer
  34. var zip_bit_len; // bits in bit buffer
  35. var zip_method;
  36. var zip_eof;
  37. var zip_copy_leng;
  38. var zip_copy_dist;
  39. var zip_tl, zip_td; // literal/length and distance decoder tables
  40. var zip_bl, zip_bd; // number of bits decoded by tl and td
  41. var zip_inflate_data;
  42. var zip_inflate_pos;
  43. /* constant tables (inflate) */
  44. var zip_MASK_BITS = new Array(
  45. 0x0000,
  46. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  47. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff);
  48. // Tables for deflate from PKZIP's appnote.txt.
  49. var zip_cplens = new Array( // Copy lengths for literal codes 257..285
  50. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  51. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0);
  52. /* note: see note #13 above about the 258 in this list. */
  53. var zip_cplext = new Array( // Extra bits for literal codes 257..285
  54. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  55. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99); // 99==invalid
  56. var zip_cpdist = new Array( // Copy offsets for distance codes 0..29
  57. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  58. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  59. 8193, 12289, 16385, 24577);
  60. var zip_cpdext = new Array( // Extra bits for distance codes
  61. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  62. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  63. 12, 12, 13, 13);
  64. var zip_border = new Array( // Order of the bit length code lengths
  65. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
  66. /* objects (inflate) */
  67. var zip_HuftList = function() {
  68. this.next = null;
  69. this.list = null;
  70. }
  71. var zip_HuftNode = function() {
  72. this.e = 0; // number of extra bits or operation
  73. this.b = 0; // number of bits in this code or subcode
  74. // union
  75. this.n = 0; // literal, length base, or distance base
  76. this.t = null; // (zip_HuftNode) pointer to next level of table
  77. }
  78. var zip_HuftBuild = function(b, // code lengths in bits (all assumed <= BMAX)
  79. n, // number of codes (assumed <= N_MAX)
  80. s, // number of simple-valued codes (0..s-1)
  81. d, // list of base values for non-simple codes
  82. e, // list of extra bits for non-simple codes
  83. mm // maximum lookup bits
  84. ) {
  85. this.BMAX = 16; // maximum bit length of any code
  86. this.N_MAX = 288; // maximum number of codes in any set
  87. this.status = 0; // 0: success, 1: incomplete table, 2: bad input
  88. this.root = null; // (zip_HuftList) starting table
  89. this.m = 0; // maximum lookup bits, returns actual
  90. /* Given a list of code lengths and a maximum table size, make a set of
  91. tables to decode that set of codes. Return zero on success, one if
  92. the given code set is incomplete (the tables are still built in this
  93. case), two if the input is invalid (all zero length codes or an
  94. oversubscribed set of lengths), and three if not enough memory.
  95. The code with value 256 is special, and the tables are constructed
  96. so that no bits beyond that code are fetched when that code is
  97. decoded. */
  98. {
  99. var a; // counter for codes of length k
  100. var c = new Array(this.BMAX+1); // bit length count table
  101. var el; // length of EOB code (value 256)
  102. var f; // i repeats in table every f entries
  103. var g; // maximum code length
  104. var h; // table level
  105. var i; // counter, current code
  106. var j; // counter
  107. var k; // number of bits in current code
  108. var lx = new Array(this.BMAX+1); // stack of bits per table
  109. var p; // pointer into c[], b[], or v[]
  110. var pidx; // index of p
  111. var q; // (zip_HuftNode) points to current table
  112. var r = new zip_HuftNode(); // table entry for structure assignment
  113. var u = new Array(this.BMAX); // zip_HuftNode[BMAX][] table stack
  114. var v = new Array(this.N_MAX); // values in order of bit length
  115. var w;
  116. var x = new Array(this.BMAX+1);// bit offsets, then code stack
  117. var xp; // pointer into x or c
  118. var y; // number of dummy codes added
  119. var z; // number of entries in current table
  120. var o;
  121. var tail; // (zip_HuftList)
  122. tail = this.root = null;
  123. for(i = 0; i < c.length; i++)
  124. c[i] = 0;
  125. for(i = 0; i < lx.length; i++)
  126. lx[i] = 0;
  127. for(i = 0; i < u.length; i++)
  128. u[i] = null;
  129. for(i = 0; i < v.length; i++)
  130. v[i] = 0;
  131. for(i = 0; i < x.length; i++)
  132. x[i] = 0;
  133. // Generate counts for each bit length
  134. el = n > 256 ? b[256] : this.BMAX; // set length of EOB code, if any
  135. p = b; pidx = 0;
  136. i = n;
  137. do {
  138. c[p[pidx]]++; // assume all entries <= BMAX
  139. pidx++;
  140. } while(--i > 0);
  141. if(c[0] == n) { // null input--all zero length codes
  142. this.root = null;
  143. this.m = 0;
  144. this.status = 0;
  145. return;
  146. }
  147. // Find minimum and maximum length, bound *m by those
  148. for(j = 1; j <= this.BMAX; j++)
  149. if(c[j] != 0)
  150. break;
  151. k = j; // minimum code length
  152. if(mm < j)
  153. mm = j;
  154. for(i = this.BMAX; i != 0; i--)
  155. if(c[i] != 0)
  156. break;
  157. g = i; // maximum code length
  158. if(mm > i)
  159. mm = i;
  160. // Adjust last length count to fill out codes, if needed
  161. for(y = 1 << j; j < i; j++, y <<= 1)
  162. if((y -= c[j]) < 0) {
  163. this.status = 2; // bad input: more codes than bits
  164. this.m = mm;
  165. return;
  166. }
  167. if((y -= c[i]) < 0) {
  168. this.status = 2;
  169. this.m = mm;
  170. return;
  171. }
  172. c[i] += y;
  173. // Generate starting offsets into the value table for each length
  174. x[1] = j = 0;
  175. p = c;
  176. pidx = 1;
  177. xp = 2;
  178. while(--i > 0) // note that i == g from above
  179. x[xp++] = (j += p[pidx++]);
  180. // Make a table of values in order of bit lengths
  181. p = b; pidx = 0;
  182. i = 0;
  183. do {
  184. if((j = p[pidx++]) != 0)
  185. v[x[j]++] = i;
  186. } while(++i < n);
  187. n = x[g]; // set n to length of v
  188. // Generate the Huffman codes and for each, make the table entries
  189. x[0] = i = 0; // first Huffman code is zero
  190. p = v; pidx = 0; // grab values in bit order
  191. h = -1; // no tables yet--level -1
  192. w = lx[0] = 0; // no bits decoded yet
  193. q = null; // ditto
  194. z = 0; // ditto
  195. // go through the bit lengths (k already is bits in shortest code)
  196. for(; k <= g; k++) {
  197. a = c[k];
  198. while(a-- > 0) {
  199. // here i is the Huffman code of length k bits for value p[pidx]
  200. // make tables up to required level
  201. while(k > w + lx[1 + h]) {
  202. w += lx[1 + h]; // add bits already decoded
  203. h++;
  204. // compute minimum size table less than or equal to *m bits
  205. z = (z = g - w) > mm ? mm : z; // upper limit
  206. if((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
  207. // too few codes for k-w bit table
  208. f -= a + 1; // deduct codes from patterns left
  209. xp = k;
  210. while(++j < z) { // try smaller tables up to z bits
  211. if((f <<= 1) <= c[++xp])
  212. break; // enough codes to use up j bits
  213. f -= c[xp]; // else deduct codes from patterns
  214. }
  215. }
  216. if(w + j > el && w < el)
  217. j = el - w; // make EOB code end at table
  218. z = 1 << j; // table entries for j-bit table
  219. lx[1 + h] = j; // set table size in stack
  220. // allocate and link in new table
  221. q = new Array(z);
  222. for(o = 0; o < z; o++) {
  223. q[o] = new zip_HuftNode();
  224. }
  225. if(tail == null)
  226. tail = this.root = new zip_HuftList();
  227. else
  228. tail = tail.next = new zip_HuftList();
  229. tail.next = null;
  230. tail.list = q;
  231. u[h] = q; // table starts after link
  232. /* connect to last table, if there is one */
  233. if(h > 0) {
  234. x[h] = i; // save pattern for backing up
  235. r.b = lx[h]; // bits to dump before this table
  236. r.e = 16 + j; // bits in this table
  237. r.t = q; // pointer to this table
  238. j = (i & ((1 << w) - 1)) >> (w - lx[h]);
  239. u[h-1][j].e = r.e;
  240. u[h-1][j].b = r.b;
  241. u[h-1][j].n = r.n;
  242. u[h-1][j].t = r.t;
  243. }
  244. }
  245. // set up table entry in r
  246. r.b = k - w;
  247. if(pidx >= n)
  248. r.e = 99; // out of values--invalid code
  249. else if(p[pidx] < s) {
  250. r.e = (p[pidx] < 256 ? 16 : 15); // 256 is end-of-block code
  251. r.n = p[pidx++]; // simple code is just the value
  252. } else {
  253. r.e = e[p[pidx] - s]; // non-simple--look up in lists
  254. r.n = d[p[pidx++] - s];
  255. }
  256. // fill code-like entries with r //
  257. f = 1 << (k - w);
  258. for(j = i >> w; j < z; j += f) {
  259. q[j].e = r.e;
  260. q[j].b = r.b;
  261. q[j].n = r.n;
  262. q[j].t = r.t;
  263. }
  264. // backwards increment the k-bit code i
  265. for(j = 1 << (k - 1); (i & j) != 0; j >>= 1)
  266. i ^= j;
  267. i ^= j;
  268. // backup over finished tables
  269. while((i & ((1 << w) - 1)) != x[h]) {
  270. w -= lx[h]; // don't need to update q
  271. h--;
  272. }
  273. }
  274. }
  275. /* return actual size of base table */
  276. this.m = lx[1];
  277. /* Return true (1) if we were given an incomplete table */
  278. this.status = ((y != 0 && g != 1) ? 1 : 0);
  279. } /* end of constructor */
  280. }
  281. /* routines (inflate) */
  282. var zip_GET_BYTE = function() {
  283. if(zip_inflate_data.length == zip_inflate_pos)
  284. return -1;
  285. return zip_inflate_data.charCodeAt(zip_inflate_pos++) & 0xff;
  286. }
  287. var zip_NEEDBITS = function(n) {
  288. while(zip_bit_len < n) {
  289. zip_bit_buf |= zip_GET_BYTE() << zip_bit_len;
  290. zip_bit_len += 8;
  291. }
  292. }
  293. var zip_GETBITS = function(n) {
  294. return zip_bit_buf & zip_MASK_BITS[n];
  295. }
  296. var zip_DUMPBITS = function(n) {
  297. zip_bit_buf >>= n;
  298. zip_bit_len -= n;
  299. }
  300. var zip_inflate_codes = function(buff, off, size) {
  301. /* inflate (decompress) the codes in a deflated (compressed) block.
  302. Return an error code or zero if it all goes ok. */
  303. var e; // table entry flag/number of extra bits
  304. var t; // (zip_HuftNode) pointer to table entry
  305. var n;
  306. if(size == 0)
  307. return 0;
  308. // inflate the coded data
  309. n = 0;
  310. for(;;) { // do until end of block
  311. zip_NEEDBITS(zip_bl);
  312. t = zip_tl.list[zip_GETBITS(zip_bl)];
  313. e = t.e;
  314. while(e > 16) {
  315. if(e == 99)
  316. return -1;
  317. zip_DUMPBITS(t.b);
  318. e -= 16;
  319. zip_NEEDBITS(e);
  320. t = t.t[zip_GETBITS(e)];
  321. e = t.e;
  322. }
  323. zip_DUMPBITS(t.b);
  324. if(e == 16) { // then it's a literal
  325. zip_wp &= zip_WSIZE - 1;
  326. buff[off + n++] = zip_slide[zip_wp++] = t.n;
  327. if(n == size)
  328. return size;
  329. continue;
  330. }
  331. // exit if end of block
  332. if(e == 15)
  333. break;
  334. // it's an EOB or a length
  335. // get length of block to copy
  336. zip_NEEDBITS(e);
  337. zip_copy_leng = t.n + zip_GETBITS(e);
  338. zip_DUMPBITS(e);
  339. // decode distance of block to copy
  340. zip_NEEDBITS(zip_bd);
  341. t = zip_td.list[zip_GETBITS(zip_bd)];
  342. e = t.e;
  343. while(e > 16) {
  344. if(e == 99)
  345. return -1;
  346. zip_DUMPBITS(t.b);
  347. e -= 16;
  348. zip_NEEDBITS(e);
  349. t = t.t[zip_GETBITS(e)];
  350. e = t.e;
  351. }
  352. zip_DUMPBITS(t.b);
  353. zip_NEEDBITS(e);
  354. zip_copy_dist = zip_wp - t.n - zip_GETBITS(e);
  355. zip_DUMPBITS(e);
  356. // do the copy
  357. while(zip_copy_leng > 0 && n < size) {
  358. zip_copy_leng--;
  359. zip_copy_dist &= zip_WSIZE - 1;
  360. zip_wp &= zip_WSIZE - 1;
  361. buff[off + n++] = zip_slide[zip_wp++]
  362. = zip_slide[zip_copy_dist++];
  363. }
  364. if(n == size)
  365. return size;
  366. }
  367. zip_method = -1; // done
  368. return n;
  369. }
  370. var zip_inflate_stored = function(buff, off, size) {
  371. /* "decompress" an inflated type 0 (stored) block. */
  372. var n;
  373. // go to byte boundary
  374. n = zip_bit_len & 7;
  375. zip_DUMPBITS(n);
  376. // get the length and its complement
  377. zip_NEEDBITS(16);
  378. n = zip_GETBITS(16);
  379. zip_DUMPBITS(16);
  380. zip_NEEDBITS(16);
  381. if(n != ((~zip_bit_buf) & 0xffff))
  382. return -1; // error in compressed data
  383. zip_DUMPBITS(16);
  384. // read and output the compressed data
  385. zip_copy_leng = n;
  386. n = 0;
  387. while(zip_copy_leng > 0 && n < size) {
  388. zip_copy_leng--;
  389. zip_wp &= zip_WSIZE - 1;
  390. zip_NEEDBITS(8);
  391. buff[off + n++] = zip_slide[zip_wp++] =
  392. zip_GETBITS(8);
  393. zip_DUMPBITS(8);
  394. }
  395. if(zip_copy_leng == 0)
  396. zip_method = -1; // done
  397. return n;
  398. }
  399. var zip_inflate_fixed = function(buff, off, size) {
  400. /* decompress an inflated type 1 (fixed Huffman codes) block. We should
  401. either replace this with a custom decoder, or at least precompute the
  402. Huffman tables. */
  403. // if first time, set up tables for fixed blocks
  404. if(zip_fixed_tl == null) {
  405. var i; // temporary variable
  406. var l = new Array(288); // length list for huft_build
  407. var h; // zip_HuftBuild
  408. // literal table
  409. for(i = 0; i < 144; i++)
  410. l[i] = 8;
  411. for(; i < 256; i++)
  412. l[i] = 9;
  413. for(; i < 280; i++)
  414. l[i] = 7;
  415. for(; i < 288; i++) // make a complete, but wrong code set
  416. l[i] = 8;
  417. zip_fixed_bl = 7;
  418. h = new zip_HuftBuild(l, 288, 257, zip_cplens, zip_cplext,
  419. zip_fixed_bl);
  420. if(h.status != 0) {
  421. alert("HufBuild error: "+h.status);
  422. return -1;
  423. }
  424. zip_fixed_tl = h.root;
  425. zip_fixed_bl = h.m;
  426. // distance table
  427. for(i = 0; i < 30; i++) // make an incomplete code set
  428. l[i] = 5;
  429. zip_fixed_bd = 5;
  430. h = new zip_HuftBuild(l, 30, 0, zip_cpdist, zip_cpdext, zip_fixed_bd);
  431. if(h.status > 1) {
  432. zip_fixed_tl = null;
  433. alert("HufBuild error: "+h.status);
  434. return -1;
  435. }
  436. zip_fixed_td = h.root;
  437. zip_fixed_bd = h.m;
  438. }
  439. zip_tl = zip_fixed_tl;
  440. zip_td = zip_fixed_td;
  441. zip_bl = zip_fixed_bl;
  442. zip_bd = zip_fixed_bd;
  443. return zip_inflate_codes(buff, off, size);
  444. }
  445. var zip_inflate_dynamic = function(buff, off, size) {
  446. // decompress an inflated type 2 (dynamic Huffman codes) block.
  447. var i; // temporary variables
  448. var j;
  449. var l; // last length
  450. var n; // number of lengths to get
  451. var t; // (zip_HuftNode) literal/length code table
  452. var nb; // number of bit length codes
  453. var nl; // number of literal/length codes
  454. var nd; // number of distance codes
  455. var ll = new Array(286+30); // literal/length and distance code lengths
  456. var h; // (zip_HuftBuild)
  457. for(i = 0; i < ll.length; i++)
  458. ll[i] = 0;
  459. // read in table lengths
  460. zip_NEEDBITS(5);
  461. nl = 257 + zip_GETBITS(5); // number of literal/length codes
  462. zip_DUMPBITS(5);
  463. zip_NEEDBITS(5);
  464. nd = 1 + zip_GETBITS(5); // number of distance codes
  465. zip_DUMPBITS(5);
  466. zip_NEEDBITS(4);
  467. nb = 4 + zip_GETBITS(4); // number of bit length codes
  468. zip_DUMPBITS(4);
  469. if(nl > 286 || nd > 30)
  470. return -1; // bad lengths
  471. // read in bit-length-code lengths
  472. for(j = 0; j < nb; j++)
  473. {
  474. zip_NEEDBITS(3);
  475. ll[zip_border[j]] = zip_GETBITS(3);
  476. zip_DUMPBITS(3);
  477. }
  478. for(; j < 19; j++)
  479. ll[zip_border[j]] = 0;
  480. // build decoding table for trees--single level, 7 bit lookup
  481. zip_bl = 7;
  482. h = new zip_HuftBuild(ll, 19, 19, null, null, zip_bl);
  483. if(h.status != 0)
  484. return -1; // incomplete code set
  485. zip_tl = h.root;
  486. zip_bl = h.m;
  487. // read in literal and distance code lengths
  488. n = nl + nd;
  489. i = l = 0;
  490. while(i < n) {
  491. zip_NEEDBITS(zip_bl);
  492. t = zip_tl.list[zip_GETBITS(zip_bl)];
  493. j = t.b;
  494. zip_DUMPBITS(j);
  495. j = t.n;
  496. if(j < 16) // length of code in bits (0..15)
  497. ll[i++] = l = j; // save last length in l
  498. else if(j == 16) { // repeat last length 3 to 6 times
  499. zip_NEEDBITS(2);
  500. j = 3 + zip_GETBITS(2);
  501. zip_DUMPBITS(2);
  502. if(i + j > n)
  503. return -1;
  504. while(j-- > 0)
  505. ll[i++] = l;
  506. } else if(j == 17) { // 3 to 10 zero length codes
  507. zip_NEEDBITS(3);
  508. j = 3 + zip_GETBITS(3);
  509. zip_DUMPBITS(3);
  510. if(i + j > n)
  511. return -1;
  512. while(j-- > 0)
  513. ll[i++] = 0;
  514. l = 0;
  515. } else { // j == 18: 11 to 138 zero length codes
  516. zip_NEEDBITS(7);
  517. j = 11 + zip_GETBITS(7);
  518. zip_DUMPBITS(7);
  519. if(i + j > n)
  520. return -1;
  521. while(j-- > 0)
  522. ll[i++] = 0;
  523. l = 0;
  524. }
  525. }
  526. // build the decoding tables for literal/length and distance codes
  527. zip_bl = zip_lbits;
  528. h = new zip_HuftBuild(ll, nl, 257, zip_cplens, zip_cplext, zip_bl);
  529. if(zip_bl == 0) // no literals or lengths
  530. h.status = 1;
  531. if(h.status != 0) {
  532. if(h.status == 1)
  533. //;// **incomplete literal tree**
  534. return -1; // incomplete code set
  535. }
  536. zip_tl = h.root;
  537. zip_bl = h.m;
  538. for(i = 0; i < nd; i++)
  539. ll[i] = ll[i + nl];
  540. zip_bd = zip_dbits;
  541. h = new zip_HuftBuild(ll, nd, 0, zip_cpdist, zip_cpdext, zip_bd);
  542. zip_td = h.root;
  543. zip_bd = h.m;
  544. if(zip_bd == 0 && nl > 257) { // lengths but no distances
  545. // **incomplete distance tree**
  546. return -1;
  547. }
  548. // if(h.status == 1) {
  549. // ;// **incomplete distance tree**
  550. // }
  551. if(h.status != 0)
  552. return -1;
  553. // decompress until an end-of-block code
  554. return zip_inflate_codes(buff, off, size);
  555. }
  556. var zip_inflate_start = function() {
  557. var i;
  558. if(zip_slide == null)
  559. zip_slide = new Array(2 * zip_WSIZE);
  560. zip_wp = 0;
  561. zip_bit_buf = 0;
  562. zip_bit_len = 0;
  563. zip_method = -1;
  564. zip_eof = false;
  565. zip_copy_leng = zip_copy_dist = 0;
  566. zip_tl = null;
  567. }
  568. var zip_inflate_internal = function(buff, off, size) {
  569. // decompress an inflated entry
  570. var n, i;
  571. n = 0;
  572. while(n < size) {
  573. if(zip_eof && zip_method == -1)
  574. return n;
  575. if(zip_copy_leng > 0) {
  576. if(zip_method != zip_STORED_BLOCK) {
  577. // STATIC_TREES or DYN_TREES
  578. while(zip_copy_leng > 0 && n < size) {
  579. zip_copy_leng--;
  580. zip_copy_dist &= zip_WSIZE - 1;
  581. zip_wp &= zip_WSIZE - 1;
  582. buff[off + n++] = zip_slide[zip_wp++] =
  583. zip_slide[zip_copy_dist++];
  584. }
  585. } else {
  586. while(zip_copy_leng > 0 && n < size) {
  587. zip_copy_leng--;
  588. zip_wp &= zip_WSIZE - 1;
  589. zip_NEEDBITS(8);
  590. buff[off + n++] = zip_slide[zip_wp++] = zip_GETBITS(8);
  591. zip_DUMPBITS(8);
  592. }
  593. if(zip_copy_leng == 0)
  594. zip_method = -1; // done
  595. }
  596. if(n == size)
  597. return n;
  598. }
  599. if(zip_method == -1) {
  600. if(zip_eof)
  601. break;
  602. // read in last block bit
  603. zip_NEEDBITS(1);
  604. if(zip_GETBITS(1) != 0)
  605. zip_eof = true;
  606. zip_DUMPBITS(1);
  607. // read in block type
  608. zip_NEEDBITS(2);
  609. zip_method = zip_GETBITS(2);
  610. zip_DUMPBITS(2);
  611. zip_tl = null;
  612. zip_copy_leng = 0;
  613. }
  614. switch(zip_method) {
  615. case 0: // zip_STORED_BLOCK
  616. i = zip_inflate_stored(buff, off + n, size - n);
  617. break;
  618. case 1: // zip_STATIC_TREES
  619. if(zip_tl != null)
  620. i = zip_inflate_codes(buff, off + n, size - n);
  621. else
  622. i = zip_inflate_fixed(buff, off + n, size - n);
  623. break;
  624. case 2: // zip_DYN_TREES
  625. if(zip_tl != null)
  626. i = zip_inflate_codes(buff, off + n, size - n);
  627. else
  628. i = zip_inflate_dynamic(buff, off + n, size - n);
  629. break;
  630. default: // error
  631. i = -1;
  632. break;
  633. }
  634. if(i == -1) {
  635. if(zip_eof)
  636. return 0;
  637. return -1;
  638. }
  639. n += i;
  640. }
  641. return n;
  642. }
  643. var zip_inflate = function(str) {
  644. var i, j;
  645. zip_inflate_start();
  646. zip_inflate_data = str;
  647. zip_inflate_pos = 0;
  648. var buff = new Array(1024);
  649. var aout = [];
  650. while((i = zip_inflate_internal(buff, 0, buff.length)) > 0) {
  651. var cbuf = new Array(i);
  652. for(j = 0; j < i; j++){
  653. cbuf[j] = String.fromCharCode(buff[j]);
  654. }
  655. aout[aout.length] = cbuf.join("");
  656. }
  657. zip_inflate_data = null; // G.C.
  658. return aout.join("");
  659. }
  660. if (! ctx.RawDeflate) ctx.RawDeflate = {};
  661. ctx.RawDeflate.inflate = zip_inflate;
  662. })(this);
  663. /*
  664. * $Id: rawdeflate.js,v 0.5 2013/04/09 14:25:38 dankogai Exp dankogai $
  665. *
  666. * GNU General Public License, version 2 (GPL-2.0)
  667. * http://opensource.org/licenses/GPL-2.0
  668. * Original:
  669. * http://www.onicos.com/staff/iz/amuse/javascript/expert/deflate.txt
  670. */
  671. (function(ctx){
  672. /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
  673. * Version: 1.0.1
  674. * LastModified: Dec 25 1999
  675. */
  676. /* Interface:
  677. * data = zip_deflate(src);
  678. */
  679. /* constant parameters */
  680. var zip_WSIZE = 32768; // Sliding Window size
  681. var zip_STORED_BLOCK = 0;
  682. var zip_STATIC_TREES = 1;
  683. var zip_DYN_TREES = 2;
  684. /* for deflate */
  685. var zip_DEFAULT_LEVEL = 6;
  686. var zip_FULL_SEARCH = true;
  687. var zip_INBUFSIZ = 32768; // Input buffer size
  688. var zip_INBUF_EXTRA = 64; // Extra buffer
  689. var zip_OUTBUFSIZ = 1024 * 8;
  690. var zip_window_size = 2 * zip_WSIZE;
  691. var zip_MIN_MATCH = 3;
  692. var zip_MAX_MATCH = 258;
  693. var zip_BITS = 16;
  694. // for SMALL_MEM
  695. var zip_LIT_BUFSIZE = 0x2000;
  696. var zip_HASH_BITS = 13;
  697. // for MEDIUM_MEM
  698. // var zip_LIT_BUFSIZE = 0x4000;
  699. // var zip_HASH_BITS = 14;
  700. // for BIG_MEM
  701. // var zip_LIT_BUFSIZE = 0x8000;
  702. // var zip_HASH_BITS = 15;
  703. if(zip_LIT_BUFSIZE > zip_INBUFSIZ)
  704. alert("error: zip_INBUFSIZ is too small");
  705. if((zip_WSIZE<<1) > (1<<zip_BITS))
  706. alert("error: zip_WSIZE is too large");
  707. if(zip_HASH_BITS > zip_BITS-1)
  708. alert("error: zip_HASH_BITS is too large");
  709. if(zip_HASH_BITS < 8 || zip_MAX_MATCH != 258)
  710. alert("error: Code too clever");
  711. var zip_DIST_BUFSIZE = zip_LIT_BUFSIZE;
  712. var zip_HASH_SIZE = 1 << zip_HASH_BITS;
  713. var zip_HASH_MASK = zip_HASH_SIZE - 1;
  714. var zip_WMASK = zip_WSIZE - 1;
  715. var zip_NIL = 0; // Tail of hash chains
  716. var zip_TOO_FAR = 4096;
  717. var zip_MIN_LOOKAHEAD = zip_MAX_MATCH + zip_MIN_MATCH + 1;
  718. var zip_MAX_DIST = zip_WSIZE - zip_MIN_LOOKAHEAD;
  719. var zip_SMALLEST = 1;
  720. var zip_MAX_BITS = 15;
  721. var zip_MAX_BL_BITS = 7;
  722. var zip_LENGTH_CODES = 29;
  723. var zip_LITERALS =256;
  724. var zip_END_BLOCK = 256;
  725. var zip_L_CODES = zip_LITERALS + 1 + zip_LENGTH_CODES;
  726. var zip_D_CODES = 30;
  727. var zip_BL_CODES = 19;
  728. var zip_REP_3_6 = 16;
  729. var zip_REPZ_3_10 = 17;
  730. var zip_REPZ_11_138 = 18;
  731. var zip_HEAP_SIZE = 2 * zip_L_CODES + 1;
  732. var zip_H_SHIFT = parseInt((zip_HASH_BITS + zip_MIN_MATCH - 1) /
  733. zip_MIN_MATCH);
  734. /* variables */
  735. var zip_free_queue;
  736. var zip_qhead, zip_qtail;
  737. var zip_initflag;
  738. var zip_outbuf = null;
  739. var zip_outcnt, zip_outoff;
  740. var zip_complete;
  741. var zip_window;
  742. var zip_d_buf;
  743. var zip_l_buf;
  744. var zip_prev;
  745. var zip_bi_buf;
  746. var zip_bi_valid;
  747. var zip_block_start;
  748. var zip_ins_h;
  749. var zip_hash_head;
  750. var zip_prev_match;
  751. var zip_match_available;
  752. var zip_match_length;
  753. var zip_prev_length;
  754. var zip_strstart;
  755. var zip_match_start;
  756. var zip_eofile;
  757. var zip_lookahead;
  758. var zip_max_chain_length;
  759. var zip_max_lazy_match;
  760. var zip_compr_level;
  761. var zip_good_match;
  762. var zip_nice_match;
  763. var zip_dyn_ltree;
  764. var zip_dyn_dtree;
  765. var zip_static_ltree;
  766. var zip_static_dtree;
  767. var zip_bl_tree;
  768. var zip_l_desc;
  769. var zip_d_desc;
  770. var zip_bl_desc;
  771. var zip_bl_count;
  772. var zip_heap;
  773. var zip_heap_len;
  774. var zip_heap_max;
  775. var zip_depth;
  776. var zip_length_code;
  777. var zip_dist_code;
  778. var zip_base_length;
  779. var zip_base_dist;
  780. var zip_flag_buf;
  781. var zip_last_lit;
  782. var zip_last_dist;
  783. var zip_last_flags;
  784. var zip_flags;
  785. var zip_flag_bit;
  786. var zip_opt_len;
  787. var zip_static_len;
  788. var zip_deflate_data;
  789. var zip_deflate_pos;
  790. /* objects (deflate) */
  791. var zip_DeflateCT = function() {
  792. this.fc = 0; // frequency count or bit string
  793. this.dl = 0; // father node in Huffman tree or length of bit string
  794. }
  795. var zip_DeflateTreeDesc = function() {
  796. this.dyn_tree = null; // the dynamic tree
  797. this.static_tree = null; // corresponding static tree or NULL
  798. this.extra_bits = null; // extra bits for each code or NULL
  799. this.extra_base = 0; // base index for extra_bits
  800. this.elems = 0; // max number of elements in the tree
  801. this.max_length = 0; // max bit length for the codes
  802. this.max_code = 0; // largest code with non zero frequency
  803. }
  804. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  805. * the desired pack level (0..9). The values given below have been tuned to
  806. * exclude worst case performance for pathological files. Better values may be
  807. * found for specific files.
  808. */
  809. var zip_DeflateConfiguration = function(a, b, c, d) {
  810. this.good_length = a; // reduce lazy search above this match length
  811. this.max_lazy = b; // do not perform lazy search above this match length
  812. this.nice_length = c; // quit search above this match length
  813. this.max_chain = d;
  814. }
  815. var zip_DeflateBuffer = function() {
  816. this.next = null;
  817. this.len = 0;
  818. this.ptr = new Array(zip_OUTBUFSIZ);
  819. this.off = 0;
  820. }
  821. /* constant tables */
  822. var zip_extra_lbits = new Array(
  823. 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);
  824. var zip_extra_dbits = new Array(
  825. 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);
  826. var zip_extra_blbits = new Array(
  827. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
  828. var zip_bl_order = new Array(
  829. 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
  830. var zip_configuration_table = new Array(
  831. new zip_DeflateConfiguration(0, 0, 0, 0),
  832. new zip_DeflateConfiguration(4, 4, 8, 4),
  833. new zip_DeflateConfiguration(4, 5, 16, 8),
  834. new zip_DeflateConfiguration(4, 6, 32, 32),
  835. new zip_DeflateConfiguration(4, 4, 16, 16),
  836. new zip_DeflateConfiguration(8, 16, 32, 32),
  837. new zip_DeflateConfiguration(8, 16, 128, 128),
  838. new zip_DeflateConfiguration(8, 32, 128, 256),
  839. new zip_DeflateConfiguration(32, 128, 258, 1024),
  840. new zip_DeflateConfiguration(32, 258, 258, 4096));
  841. /* routines (deflate) */
  842. var zip_deflate_start = function(level) {
  843. var i;
  844. if(!level)
  845. level = zip_DEFAULT_LEVEL;
  846. else if(level < 1)
  847. level = 1;
  848. else if(level > 9)
  849. level = 9;
  850. zip_compr_level = level;
  851. zip_initflag = false;
  852. zip_eofile = false;
  853. if(zip_outbuf != null)
  854. return;
  855. zip_free_queue = zip_qhead = zip_qtail = null;
  856. zip_outbuf = new Array(zip_OUTBUFSIZ);
  857. zip_window = new Array(zip_window_size);
  858. zip_d_buf = new Array(zip_DIST_BUFSIZE);
  859. zip_l_buf = new Array(zip_INBUFSIZ + zip_INBUF_EXTRA);
  860. zip_prev = new Array(1 << zip_BITS);
  861. zip_dyn_ltree = new Array(zip_HEAP_SIZE);
  862. for(i = 0; i < zip_HEAP_SIZE; i++)
  863. zip_dyn_ltree[i] = new zip_DeflateCT();
  864. zip_dyn_dtree = new Array(2*zip_D_CODES+1);
  865. for(i = 0; i < 2*zip_D_CODES+1; i++)
  866. zip_dyn_dtree[i] = new zip_DeflateCT();
  867. zip_static_ltree = new Array(zip_L_CODES+2);
  868. for(i = 0; i < zip_L_CODES+2; i++)
  869. zip_static_ltree[i] = new zip_DeflateCT();
  870. zip_static_dtree = new Array(zip_D_CODES);
  871. for(i = 0; i < zip_D_CODES; i++)
  872. zip_static_dtree[i] = new zip_DeflateCT();
  873. zip_bl_tree = new Array(2*zip_BL_CODES+1);
  874. for(i = 0; i < 2*zip_BL_CODES+1; i++)
  875. zip_bl_tree[i] = new zip_DeflateCT();
  876. zip_l_desc = new zip_DeflateTreeDesc();
  877. zip_d_desc = new zip_DeflateTreeDesc();
  878. zip_bl_desc = new zip_DeflateTreeDesc();
  879. zip_bl_count = new Array(zip_MAX_BITS+1);
  880. zip_heap = new Array(2*zip_L_CODES+1);
  881. zip_depth = new Array(2*zip_L_CODES+1);
  882. zip_length_code = new Array(zip_MAX_MATCH-zip_MIN_MATCH+1);
  883. zip_dist_code = new Array(512);
  884. zip_base_length = new Array(zip_LENGTH_CODES);
  885. zip_base_dist = new Array(zip_D_CODES);
  886. zip_flag_buf = new Array(parseInt(zip_LIT_BUFSIZE / 8));
  887. }
  888. var zip_deflate_end = function() {
  889. zip_free_queue = zip_qhead = zip_qtail = null;
  890. zip_outbuf = null;
  891. zip_window = null;
  892. zip_d_buf = null;
  893. zip_l_buf = null;
  894. zip_prev = null;
  895. zip_dyn_ltree = null;
  896. zip_dyn_dtree = null;
  897. zip_static_ltree = null;
  898. zip_static_dtree = null;
  899. zip_bl_tree = null;
  900. zip_l_desc = null;
  901. zip_d_desc = null;
  902. zip_bl_desc = null;
  903. zip_bl_count = null;
  904. zip_heap = null;
  905. zip_depth = null;
  906. zip_length_code = null;
  907. zip_dist_code = null;
  908. zip_base_length = null;
  909. zip_base_dist = null;
  910. zip_flag_buf = null;
  911. }
  912. var zip_reuse_queue = function(p) {
  913. p.next = zip_free_queue;
  914. zip_free_queue = p;
  915. }
  916. var zip_new_queue = function() {
  917. var p;
  918. if(zip_free_queue != null)
  919. {
  920. p = zip_free_queue;
  921. zip_free_queue = zip_free_queue.next;
  922. }
  923. else
  924. p = new zip_DeflateBuffer();
  925. p.next = null;
  926. p.len = p.off = 0;
  927. return p;
  928. }
  929. var zip_head1 = function(i) {
  930. return zip_prev[zip_WSIZE + i];
  931. }
  932. var zip_head2 = function(i, val) {
  933. return zip_prev[zip_WSIZE + i] = val;
  934. }
  935. /* put_byte is used for the compressed output, put_ubyte for the
  936. * uncompressed output. However unlzw() uses window for its
  937. * suffix table instead of its output buffer, so it does not use put_ubyte
  938. * (to be cleaned up).
  939. */
  940. var zip_put_byte = function(c) {
  941. zip_outbuf[zip_outoff + zip_outcnt++] = c;
  942. if(zip_outoff + zip_outcnt == zip_OUTBUFSIZ)
  943. zip_qoutbuf();
  944. }
  945. /* Output a 16 bit value, lsb first */
  946. var zip_put_short = function(w) {
  947. w &= 0xffff;
  948. if(zip_outoff + zip_outcnt < zip_OUTBUFSIZ - 2) {
  949. zip_outbuf[zip_outoff + zip_outcnt++] = (w & 0xff);
  950. zip_outbuf[zip_outoff + zip_outcnt++] = (w >>> 8);
  951. } else {
  952. zip_put_byte(w & 0xff);
  953. zip_put_byte(w >>> 8);
  954. }
  955. }
  956. /* ==========================================================================
  957. * Insert string s in the dictionary and set match_head to the previous head
  958. * of the hash chain (the most recent string with same hash key). Return
  959. * the previous length of the hash chain.
  960. * IN assertion: all calls to to INSERT_STRING are made with consecutive
  961. * input characters and the first MIN_MATCH bytes of s are valid
  962. * (except for the last MIN_MATCH-1 bytes of the input file).
  963. */
  964. var zip_INSERT_STRING = function() {
  965. zip_ins_h = ((zip_ins_h << zip_H_SHIFT)
  966. ^ (zip_window[zip_strstart + zip_MIN_MATCH - 1] & 0xff))
  967. & zip_HASH_MASK;
  968. zip_hash_head = zip_head1(zip_ins_h);
  969. zip_prev[zip_strstart & zip_WMASK] = zip_hash_head;
  970. zip_head2(zip_ins_h, zip_strstart);
  971. }
  972. /* Send a code of the given tree. c and tree must not have side effects */
  973. var zip_SEND_CODE = function(c, tree) {
  974. zip_send_bits(tree[c].fc, tree[c].dl);
  975. }
  976. /* Mapping from a distance to a distance code. dist is the distance - 1 and
  977. * must not have side effects. dist_code[256] and dist_code[257] are never
  978. * used.
  979. */
  980. var zip_D_CODE = function(dist) {
  981. return (dist < 256 ? zip_dist_code[dist]
  982. : zip_dist_code[256 + (dist>>7)]) & 0xff;
  983. }
  984. /* ==========================================================================
  985. * Compares to subtrees, using the tree depth as tie breaker when
  986. * the subtrees have equal frequency. This minimizes the worst case length.
  987. */
  988. var zip_SMALLER = function(tree, n, m) {
  989. return tree[n].fc < tree[m].fc ||
  990. (tree[n].fc == tree[m].fc && zip_depth[n] <= zip_depth[m]);
  991. }
  992. /* ==========================================================================
  993. * read string data
  994. */
  995. var zip_read_buff = function(buff, offset, n) {
  996. var i;
  997. for(i = 0; i < n && zip_deflate_pos < zip_deflate_data.length; i++)
  998. buff[offset + i] =
  999. zip_deflate_data.charCodeAt(zip_deflate_pos++) & 0xff;
  1000. return i;
  1001. }
  1002. /* ==========================================================================
  1003. * Initialize the "longest match" routines for a new file
  1004. */
  1005. var zip_lm_init = function() {
  1006. var j;
  1007. /* Initialize the hash table. */
  1008. for(j = 0; j < zip_HASH_SIZE; j++)
  1009. // zip_head2(j, zip_NIL);
  1010. zip_prev[zip_WSIZE + j] = 0;
  1011. /* prev will be initialized on the fly */
  1012. /* Set the default configuration parameters:
  1013. */
  1014. zip_max_lazy_match = zip_configuration_table[zip_compr_level].max_lazy;
  1015. zip_good_match = zip_configuration_table[zip_compr_level].good_length;
  1016. if(!zip_FULL_SEARCH)
  1017. zip_nice_match = zip_configuration_table[zip_compr_level].nice_length;
  1018. zip_max_chain_length = zip_configuration_table[zip_compr_level].max_chain;
  1019. zip_strstart = 0;
  1020. zip_block_start = 0;
  1021. zip_lookahead = zip_read_buff(zip_window, 0, 2 * zip_WSIZE);
  1022. if(zip_lookahead <= 0) {
  1023. zip_eofile = true;
  1024. zip_lookahead = 0;
  1025. return;
  1026. }
  1027. zip_eofile = false;
  1028. /* Make sure that we always have enough lookahead. This is important
  1029. * if input comes from a device such as a tty.
  1030. */
  1031. while(zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile)
  1032. zip_fill_window();
  1033. /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
  1034. * not important since only literal bytes will be emitted.
  1035. */
  1036. zip_ins_h = 0;
  1037. for(j = 0; j < zip_MIN_MATCH - 1; j++) {
  1038. // UPDATE_HASH(ins_h, window[j]);
  1039. zip_ins_h = ((zip_ins_h << zip_H_SHIFT) ^ (zip_window[j] & 0xff)) & zip_HASH_MASK;
  1040. }
  1041. }
  1042. /* ==========================================================================
  1043. * Set match_start to the longest match starting at the given string and
  1044. * return its length. Matches shorter or equal to prev_length are discarded,
  1045. * in which case the result is equal to prev_length and match_start is
  1046. * garbage.
  1047. * IN assertions: cur_match is the head of the hash chain for the current
  1048. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1049. */
  1050. var zip_longest_match = function(cur_match) {
  1051. var chain_length = zip_max_chain_length; // max hash chain length
  1052. var scanp = zip_strstart; // current string
  1053. var matchp; // matched string
  1054. var len; // length of current match
  1055. var best_len = zip_prev_length; // best match length so far
  1056. /* Stop when cur_match becomes <= limit. To simplify the code,
  1057. * we prevent matches with the string of window index 0.
  1058. */
  1059. var limit = (zip_strstart > zip_MAX_DIST ? zip_strstart - zip_MAX_DIST : zip_NIL);
  1060. var strendp = zip_strstart + zip_MAX_MATCH;
  1061. var scan_end1 = zip_window[scanp + best_len - 1];
  1062. var scan_end = zip_window[scanp + best_len];
  1063. /* Do not waste too much time if we already have a good match: */
  1064. if(zip_prev_length >= zip_good_match)
  1065. chain_length >>= 2;
  1066. // Assert(encoder->strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
  1067. do {
  1068. // Assert(cur_match < encoder->strstart, "no future");
  1069. matchp = cur_match;
  1070. /* Skip to next match if the match length cannot increase
  1071. * or if the match length is less than 2:
  1072. */
  1073. if(zip_window[matchp + best_len] != scan_end ||
  1074. zip_window[matchp + best_len - 1] != scan_end1 ||
  1075. zip_window[matchp] != zip_window[scanp] ||
  1076. zip_window[++matchp] != zip_window[scanp + 1]) {
  1077. continue;
  1078. }
  1079. /* The check at best_len-1 can be removed because it will be made
  1080. * again later. (This heuristic is not always a win.)
  1081. * It is not necessary to compare scan[2] and match[2] since they
  1082. * are always equal when the other bytes match, given that
  1083. * the hash keys are equal and that HASH_BITS >= 8.
  1084. */
  1085. scanp += 2;
  1086. matchp++;
  1087. /* We check for insufficient lookahead only every 8th comparison;
  1088. * the 256th check will be made at strstart+258.
  1089. */
  1090. do {
  1091. } while(zip_window[++scanp] == zip_window[++matchp] &&
  1092. zip_window[++scanp] == zip_window[++matchp] &&
  1093. zip_window[++scanp] == zip_window[++matchp] &&
  1094. zip_window[++scanp] == zip_window[++matchp] &&
  1095. zip_window[++scanp] == zip_window[++matchp] &&
  1096. zip_window[++scanp] == zip_window[++matchp] &&
  1097. zip_window[++scanp] == zip_window[++matchp] &&
  1098. zip_window[++scanp] == zip_window[++matchp] &&
  1099. scanp < strendp);
  1100. len = zip_MAX_MATCH - (strendp - scanp);
  1101. scanp = strendp - zip_MAX_MATCH;
  1102. if(len > best_len) {
  1103. zip_match_start = cur_match;
  1104. best_len = len;
  1105. if(zip_FULL_SEARCH) {
  1106. if(len >= zip_MAX_MATCH) break;
  1107. } else {
  1108. if(len >= zip_nice_match) break;
  1109. }
  1110. scan_end1 = zip_window[scanp + best_len-1];
  1111. scan_end = zip_window[scanp + best_len];
  1112. }
  1113. } while((cur_match = zip_prev[cur_match & zip_WMASK]) > limit
  1114. && --chain_length != 0);
  1115. return best_len;
  1116. }
  1117. /* ==========================================================================
  1118. * Fill the window when the lookahead becomes insufficient.
  1119. * Updates strstart and lookahead, and sets eofile if end of input file.
  1120. * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
  1121. * OUT assertions: at least one byte has been read, or eofile is set;
  1122. * file reads are performed for at least two bytes (required for the
  1123. * translate_eol option).
  1124. */
  1125. var zip_fill_window = function() {
  1126. var n, m;
  1127. // Amount of free space at the end of the window.
  1128. var more = zip_window_size - zip_lookahead - zip_strstart;
  1129. /* If the window is almost full and there is insufficient lookahead,
  1130. * move the upper half to the lower one to make room in the upper half.
  1131. */
  1132. if(more == -1) {
  1133. /* Very unlikely, but possible on 16 bit machine if strstart == 0
  1134. * and lookahead == 1 (input done one byte at time)
  1135. */
  1136. more--;
  1137. } else if(zip_strstart >= zip_WSIZE + zip_MAX_DIST) {
  1138. /* By the IN assertion, the window is not empty so we can't confuse
  1139. * more == 0 with more == 64K on a 16 bit machine.
  1140. */
  1141. // Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
  1142. // System.arraycopy(window, WSIZE, window, 0, WSIZE);
  1143. for(n = 0; n < zip_WSIZE; n++)
  1144. zip_window[n] = zip_window[n + zip_WSIZE];
  1145. zip_match_start -= zip_WSIZE;
  1146. zip_strstart -= zip_WSIZE; /* we now have strstart >= MAX_DIST: */
  1147. zip_block_start -= zip_WSIZE;
  1148. for(n = 0; n < zip_HASH_SIZE; n++) {
  1149. m = zip_head1(n);
  1150. zip_head2(n, m >= zip_WSIZE ? m - zip_WSIZE : zip_NIL);
  1151. }
  1152. for(n = 0; n < zip_WSIZE; n++) {
  1153. /* If n is not on any hash chain, prev[n] is garbage but
  1154. * its value will never be used.
  1155. */
  1156. m = zip_prev[n];
  1157. zip_prev[n] = (m >= zip_WSIZE ? m - zip_WSIZE : zip_NIL);
  1158. }
  1159. more += zip_WSIZE;
  1160. }
  1161. // At this point, more >= 2
  1162. if(!zip_eofile) {
  1163. n = zip_read_buff(zip_window, zip_strstart + zip_lookahead, more);
  1164. if(n <= 0)
  1165. zip_eofile = true;
  1166. else
  1167. zip_lookahead += n;
  1168. }
  1169. }
  1170. /* ==========================================================================
  1171. * Processes a new input file and return its compressed length. This
  1172. * function does not perform lazy evaluationof matches and inserts
  1173. * new strings in the dictionary only for unmatched strings or for short
  1174. * matches. It is used only for the fast compression options.
  1175. */
  1176. var zip_deflate_fast = function() {
  1177. while(zip_lookahead != 0 && zip_qhead == null) {
  1178. var flush; // set if current block must be flushed
  1179. /* Insert the string window[strstart .. strstart+2] in the
  1180. * dictionary, and set hash_head to the head of the hash chain:
  1181. */
  1182. zip_INSERT_STRING();
  1183. /* Find the longest match, discarding those <= prev_length.
  1184. * At this point we have always match_length < MIN_MATCH
  1185. */
  1186. if(zip_hash_head != zip_NIL &&
  1187. zip_strstart - zip_hash_head <= zip_MAX_DIST) {
  1188. /* To simplify the code, we prevent matches with the string
  1189. * of window index 0 (in particular we have to avoid a match
  1190. * of the string with itself at the start of the input file).
  1191. */
  1192. zip_match_length = zip_longest_match(zip_hash_head);
  1193. /* longest_match() sets match_start */
  1194. if(zip_match_length > zip_lookahead)
  1195. zip_match_length = zip_lookahead;
  1196. }
  1197. if(zip_match_length >= zip_MIN_MATCH) {
  1198. // check_match(strstart, match_start, match_length);
  1199. flush = zip_ct_tally(zip_strstart - zip_match_start,
  1200. zip_match_length - zip_MIN_MATCH);
  1201. zip_lookahead -= zip_match_length;
  1202. /* Insert new strings in the hash table only if the match length
  1203. * is not too large. This saves time but degrades compression.
  1204. */
  1205. if(zip_match_length <= zip_max_lazy_match) {
  1206. zip_match_length--; // string at strstart already in hash table
  1207. do {
  1208. zip_strstart++;
  1209. zip_INSERT_STRING();
  1210. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1211. * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  1212. * these bytes are garbage, but it does not matter since
  1213. * the next lookahead bytes will be emitted as literals.
  1214. */
  1215. } while(--zip_match_length != 0);
  1216. zip_strstart++;
  1217. } else {
  1218. zip_strstart += zip_match_length;
  1219. zip_match_length = 0;
  1220. zip_ins_h = zip_window[zip_strstart] & 0xff;
  1221. // UPDATE_HASH(ins_h, window[strstart + 1]);
  1222. zip_ins_h = ((zip_ins_h<<zip_H_SHIFT) ^ (zip_window[zip_strstart + 1] & 0xff)) & zip_HASH_MASK;
  1223. //#if MIN_MATCH != 3
  1224. // Call UPDATE_HASH() MIN_MATCH-3 more times
  1225. //#endif
  1226. }
  1227. } else {
  1228. /* No match, output a literal byte */
  1229. flush = zip_ct_tally(0, zip_window[zip_strstart] & 0xff);
  1230. zip_lookahead--;
  1231. zip_strstart++;
  1232. }
  1233. if(flush) {
  1234. zip_flush_block(0);
  1235. zip_block_start = zip_strstart;
  1236. }
  1237. /* Make sure that we always have enough lookahead, except
  1238. * at the end of the input file. We need MAX_MATCH bytes
  1239. * for the next match, plus MIN_MATCH bytes to insert the
  1240. * string following the next match.
  1241. */
  1242. while(zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile)
  1243. zip_fill_window();
  1244. }
  1245. }
  1246. var zip_deflate_better = function() {
  1247. /* Process the input block. */
  1248. while(zip_lookahead != 0 && zip_qhead == null) {
  1249. /* Insert the string window[strstart .. strstart+2] in the
  1250. * dictionary, and set hash_head to the head of the hash chain:
  1251. */
  1252. zip_INSERT_STRING();
  1253. /* Find the longest match, discarding those <= prev_length.
  1254. */
  1255. zip_prev_length = zip_match_length;
  1256. zip_prev_match = zip_match_start;
  1257. zip_match_length = zip_MIN_MATCH - 1;
  1258. if(zip_hash_head != zip_NIL &&
  1259. zip_prev_length < zip_max_lazy_match &&
  1260. zip_strstart - zip_hash_head <= zip_MAX_DIST) {
  1261. /* To simplify the code, we prevent matches with the string
  1262. * of window index 0 (in particular we have to avoid a match
  1263. * of the string with itself at the start of the input file).
  1264. */
  1265. zip_match_length = zip_longest_match(zip_hash_head);
  1266. /* longest_match() sets match_start */
  1267. if(zip_match_length > zip_lookahead)
  1268. zip_match_length = zip_lookahead;
  1269. /* Ignore a length 3 match if it is too distant: */
  1270. if(zip_match_length == zip_MIN_MATCH &&
  1271. zip_strstart - zip_match_start > zip_TOO_FAR) {
  1272. /* If prev_match is also MIN_MATCH, match_start is garbage
  1273. * but we will ignore the current match anyway.
  1274. */
  1275. zip_match_length--;
  1276. }
  1277. }
  1278. /* If there was a match at the previous step and the current
  1279. * match is not better, output the previous match:
  1280. */
  1281. if(zip_prev_length >= zip_MIN_MATCH &&
  1282. zip_match_length <= zip_prev_length) {
  1283. var flush; // set if current block must be flushed
  1284. // check_match(strstart - 1, prev_match, prev_length);
  1285. flush = zip_ct_tally(zip_strstart - 1 - zip_prev_match,
  1286. zip_prev_length - zip_MIN_MATCH);
  1287. /* Insert in hash table all strings up to the end of the match.
  1288. * strstart-1 and strstart are already inserted.
  1289. */
  1290. zip_lookahead -= zip_prev_length - 1;
  1291. zip_prev_length -= 2;
  1292. do {
  1293. zip_strstart++;
  1294. zip_INSERT_STRING();
  1295. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1296. * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  1297. * these bytes are garbage, but it does not matter since the
  1298. * next lookahead bytes will always be emitted as literals.
  1299. */
  1300. } while(--zip_prev_length != 0);
  1301. zip_match_available = 0;
  1302. zip_match_length = zip_MIN_MATCH - 1;
  1303. zip_strstart++;
  1304. if(flush) {
  1305. zip_flush_block(0);
  1306. zip_block_start = zip_strstart;
  1307. }
  1308. } else if(zip_match_available != 0) {
  1309. /* If there was no match at the previous position, output a
  1310. * single literal. If there was a match but the current match
  1311. * is longer, truncate the previous match to a single literal.
  1312. */
  1313. if(zip_ct_tally(0, zip_window[zip_strstart - 1] & 0xff)) {
  1314. zip_flush_block(0);
  1315. zip_block_start = zip_strstart;
  1316. }
  1317. zip_strstart++;
  1318. zip_lookahead--;
  1319. } else {
  1320. /* There is no previous match to compare with, wait for
  1321. * the next step to decide.
  1322. */
  1323. zip_match_available = 1;
  1324. zip_strstart++;
  1325. zip_lookahead--;
  1326. }
  1327. /* Make sure that we always have enough lookahead, except
  1328. * at the end of the input file. We need MAX_MATCH bytes
  1329. * for the next match, plus MIN_MATCH bytes to insert the
  1330. * string following the next match.
  1331. */
  1332. while(zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile)
  1333. zip_fill_window();
  1334. }
  1335. }
  1336. var zip_init_deflate = function() {
  1337. if(zip_eofile)
  1338. return;
  1339. zip_bi_buf = 0;
  1340. zip_bi_valid = 0;
  1341. zip_ct_init();
  1342. zip_lm_init();
  1343. zip_qhead = null;
  1344. zip_outcnt = 0;
  1345. zip_outoff = 0;
  1346. zip_match_available = 0;
  1347. if(zip_compr_level <= 3)
  1348. {
  1349. zip_prev_length = zip_MIN_MATCH - 1;
  1350. zip_match_length = 0;
  1351. }
  1352. else
  1353. {
  1354. zip_match_length = zip_MIN_MATCH - 1;
  1355. zip_match_available = 0;
  1356. zip_match_available = 0;
  1357. }
  1358. zip_complete = false;
  1359. }
  1360. /* ==========================================================================
  1361. * Same as above, but achieves better compression. We use a lazy
  1362. * evaluation for matches: a match is finally adopted only if there is
  1363. * no better match at the next window position.
  1364. */
  1365. var zip_deflate_internal = function(buff, off, buff_size) {
  1366. var n;
  1367. if(!zip_initflag)
  1368. {
  1369. zip_init_deflate();
  1370. zip_initflag = true;
  1371. if(zip_lookahead == 0) { // empty
  1372. zip_complete = true;
  1373. return 0;
  1374. }
  1375. }
  1376. if((n = zip_qcopy(buff, off, buff_size)) == buff_size)
  1377. return buff_size;
  1378. if(zip_complete)
  1379. return n;
  1380. if(zip_compr_level <= 3) // optimized for speed
  1381. zip_deflate_fast();
  1382. else
  1383. zip_deflate_better();
  1384. if(zip_lookahead == 0) {
  1385. if(zip_match_available != 0)
  1386. zip_ct_tally(0, zip_window[zip_strstart - 1] & 0xff);
  1387. zip_flush_block(1);
  1388. zip_complete = true;
  1389. }
  1390. return n + zip_qcopy(buff, n + off, buff_size - n);
  1391. }
  1392. var zip_qcopy = function(buff, off, buff_size) {
  1393. var n, i, j;
  1394. n = 0;
  1395. while(zip_qhead != null && n < buff_size)
  1396. {
  1397. i = buff_size - n;
  1398. if(i > zip_qhead.len)
  1399. i = zip_qhead.len;
  1400. // System.arraycopy(qhead.ptr, qhead.off, buff, off + n, i);
  1401. for(j = 0; j < i; j++)
  1402. buff[off + n + j] = zip_qhead.ptr[zip_qhead.off + j];
  1403. zip_qhead.off += i;
  1404. zip_qhead.len -= i;
  1405. n += i;
  1406. if(zip_qhead.len == 0) {
  1407. var p;
  1408. p = zip_qhead;
  1409. zip_qhead = zip_qhead.next;
  1410. zip_reuse_queue(p);
  1411. }
  1412. }
  1413. if(n == buff_size)
  1414. return n;
  1415. if(zip_outoff < zip_outcnt) {
  1416. i = buff_size - n;
  1417. if(i > zip_outcnt - zip_outoff)
  1418. i = zip_outcnt - zip_outoff;
  1419. // System.arraycopy(outbuf, outoff, buff, off + n, i);
  1420. for(j = 0; j < i; j++)
  1421. buff[off + n + j] = zip_outbuf[zip_outoff + j];
  1422. zip_outoff += i;
  1423. n += i;
  1424. if(zip_outcnt == zip_outoff)
  1425. zip_outcnt = zip_outoff = 0;
  1426. }
  1427. return n;
  1428. }
  1429. /* ==========================================================================
  1430. * Allocate the match buffer, initialize the various tables and save the
  1431. * location of the internal file attribute (ascii/binary) and method
  1432. * (DEFLATE/STORE).
  1433. */
  1434. var zip_ct_init = function() {
  1435. var n; // iterates over tree elements
  1436. var bits; // bit counter
  1437. var length; // length value
  1438. var code; // code value
  1439. var dist; // distance index
  1440. if(zip_static_dtree[0].dl != 0) return; // ct_init already called
  1441. zip_l_desc.dyn_tree = zip_dyn_ltree;
  1442. zip_l_desc.static_tree = zip_static_ltree;
  1443. zip_l_desc.extra_bits = zip_extra_lbits;
  1444. zip_l_desc.extra_base = zip_LITERALS + 1;
  1445. zip_l_desc.elems = zip_L_CODES;
  1446. zip_l_desc.max_length = zip_MAX_BITS;
  1447. zip_l_desc.max_code = 0;
  1448. zip_d_desc.dyn_tree = zip_dyn_dtree;
  1449. zip_d_desc.static_tree = zip_static_dtree;
  1450. zip_d_desc.extra_bits = zip_extra_dbits;
  1451. zip_d_desc.extra_base = 0;
  1452. zip_d_desc.elems = zip_D_CODES;
  1453. zip_d_desc.max_length = zip_MAX_BITS;
  1454. zip_d_desc.max_code = 0;
  1455. zip_bl_desc.dyn_tree = zip_bl_tree;
  1456. zip_bl_desc.static_tree = null;
  1457. zip_bl_desc.extra_bits = zip_extra_blbits;
  1458. zip_bl_desc.extra_base = 0;
  1459. zip_bl_desc.elems = zip_BL_CODES;
  1460. zip_bl_desc.max_length = zip_MAX_BL_BITS;
  1461. zip_bl_desc.max_code = 0;
  1462. // Initialize the mapping length (0..255) -> length code (0..28)
  1463. length = 0;
  1464. for(code = 0; code < zip_LENGTH_CODES-1; code++) {
  1465. zip_base_length[code] = length;
  1466. for(n = 0; n < (1<<zip_extra_lbits[code]); n++)
  1467. zip_length_code[length++] = code;
  1468. }
  1469. // Assert (length == 256, "ct_init: length != 256");
  1470. /* Note that the length 255 (match length 258) can be represented
  1471. * in two different ways: code 284 + 5 bits or code 285, so we
  1472. * overwrite length_code[255] to use the best encoding:
  1473. */
  1474. zip_length_code[length-1] = code;
  1475. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  1476. dist = 0;
  1477. for(code = 0 ; code < 16; code++) {
  1478. zip_base_dist[code] = dist;
  1479. for(n = 0; n < (1<<zip_extra_dbits[code]); n++) {
  1480. zip_dist_code[dist++] = code;
  1481. }
  1482. }
  1483. // Assert (dist == 256, "ct_init: dist != 256");
  1484. dist >>= 7; // from now on, all distances are divided by 128
  1485. for( ; code < zip_D_CODES; code++) {
  1486. zip_base_dist[code] = dist << 7;
  1487. for(n = 0; n < (1<<(zip_extra_dbits[code]-7)); n++)
  1488. zip_dist_code[256 + dist++] = code;
  1489. }
  1490. // Assert (dist == 256, "ct_init: 256+dist != 512");
  1491. // Construct the codes of the static literal tree
  1492. for(bits = 0; bits <= zip_MAX_BITS; bits++)
  1493. zip_bl_count[bits] = 0;
  1494. n = 0;
  1495. while(n <= 143) { zip_static_ltree[n++].dl = 8; zip_bl_count[8]++; }
  1496. while(n <= 255) { zip_static_ltree[n++].dl = 9; zip_bl_count[9]++; }
  1497. while(n <= 279) { zip_static_ltree[n++].dl = 7; zip_bl_count[7]++; }
  1498. while(n <= 287) { zip_static_ltree[n++].dl = 8; zip_bl_count[8]++; }
  1499. /* Codes 286 and 287 do not exist, but we must include them in the
  1500. * tree construction to get a canonical Huffman tree (longest code
  1501. * all ones)
  1502. */
  1503. zip_gen_codes(zip_static_ltree, zip_L_CODES + 1);
  1504. /* The static distance tree is trivial: */
  1505. for(n = 0; n < zip_D_CODES; n++) {
  1506. zip_static_dtree[n].dl = 5;
  1507. zip_static_dtree[n].fc = zip_bi_reverse(n, 5);
  1508. }
  1509. // Initialize the first block of the first file:
  1510. zip_init_block();
  1511. }
  1512. /* ==========================================================================
  1513. * Initialize a new block.
  1514. */
  1515. var zip_init_block = function() {
  1516. var n; // iterates over tree elements
  1517. // Initialize the trees.
  1518. for(n = 0; n < zip_L_CODES; n++) zip_dyn_ltree[n].fc = 0;
  1519. for(n = 0; n < zip_D_CODES; n++) zip_dyn_dtree[n].fc = 0;
  1520. for(n = 0; n < zip_BL_CODES; n++) zip_bl_tree[n].fc = 0;
  1521. zip_dyn_ltree[zip_END_BLOCK].fc = 1;
  1522. zip_opt_len = zip_static_len = 0;
  1523. zip_last_lit = zip_last_dist = zip_last_flags = 0;
  1524. zip_flags = 0;
  1525. zip_flag_bit = 1;
  1526. }
  1527. /* ==========================================================================
  1528. * Restore the heap property by moving down the tree starting at node k,
  1529. * exchanging a node with the smallest of its two sons if necessary, stopping
  1530. * when the heap property is re-established (each father smaller than its
  1531. * two sons).
  1532. */
  1533. var zip_pqdownheap = function(
  1534. tree, // the tree to restore
  1535. k) { // node to move down
  1536. var v = zip_heap[k];
  1537. var j = k << 1; // left son of k
  1538. while(j <= zip_heap_len) {
  1539. // Set j to the smallest of the two sons:
  1540. if(j < zip_heap_len &&
  1541. zip_SMALLER(tree, zip_heap[j + 1], zip_heap[j]))
  1542. j++;
  1543. // Exit if v is smaller than both sons
  1544. if(zip_SMALLER(tree, v, zip_heap[j]))
  1545. break;
  1546. // Exchange v with the smallest son
  1547. zip_heap[k] = zip_heap[j];
  1548. k = j;
  1549. // And continue down the tree, setting j to the left son of k
  1550. j <<= 1;
  1551. }
  1552. zip_heap[k] = v;
  1553. }
  1554. /* ==========================================================================
  1555. * Compute the optimal bit lengths for a tree and update the total bit length
  1556. * for the current block.
  1557. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  1558. * above are the tree nodes sorted by increasing frequency.
  1559. * OUT assertions: the field len is set to the optimal bit length, the
  1560. * array bl_count contains the frequencies for each bit length.
  1561. * The length opt_len is updated; static_len is also updated if stree is
  1562. * not null.
  1563. */
  1564. var zip_gen_bitlen = function(desc) { // the tree descriptor
  1565. var tree = desc.dyn_tree;
  1566. var extra = desc.extra_bits;
  1567. var base = desc.extra_base;
  1568. var max_code = desc.max_code;
  1569. var max_length = desc.max_length;
  1570. var stree = desc.static_tree;
  1571. var h; // heap index
  1572. var n, m; // iterate over the tree elements
  1573. var bits; // bit length
  1574. var xbits; // extra bits
  1575. var f; // frequency
  1576. var overflow = 0; // number of elements with bit length too large
  1577. for(bits = 0; bits <= zip_MAX_BITS; bits++)
  1578. zip_bl_count[bits] = 0;
  1579. /* In a first pass, compute the optimal bit lengths (which may
  1580. * overflow in the case of the bit length tree).
  1581. */
  1582. tree[zip_heap[zip_heap_max]].dl = 0; // root of the heap
  1583. for(h = zip_heap_max + 1; h < zip_HEAP_SIZE; h++) {
  1584. n = zip_heap[h];
  1585. bits = tree[tree[n].dl].dl + 1;
  1586. if(bits > max_length) {
  1587. bits = max_length;
  1588. overflow++;
  1589. }
  1590. tree[n].dl = bits;
  1591. // We overwrite tree[n].dl which is no longer needed
  1592. if(n > max_code)
  1593. continue; // not a leaf node
  1594. zip_bl_count[bits]++;
  1595. xbits = 0;
  1596. if(n >= base)
  1597. xbits = extra[n - base];
  1598. f = tree[n].fc;
  1599. zip_opt_len += f * (bits + xbits);
  1600. if(stree != null)
  1601. zip_static_len += f * (stree[n].dl + xbits);
  1602. }
  1603. if(overflow == 0)
  1604. return;
  1605. // This happens for example on obj2 and pic of the Calgary corpus
  1606. // Find the first bit length which could increase:
  1607. do {
  1608. bits = max_length - 1;
  1609. while(zip_bl_count[bits] == 0)
  1610. bits--;
  1611. zip_bl_count[bits]--; // move one leaf down the tree
  1612. zip_bl_count[bits + 1] += 2; // move one overflow item as its brother
  1613. zip_bl_count[max_length]--;
  1614. /* The brother of the overflow item also moves one step up,
  1615. * but this does not affect bl_count[max_length]
  1616. */
  1617. overflow -= 2;
  1618. } while(overflow > 0);
  1619. /* Now recompute all bit lengths, scanning in increasing frequency.
  1620. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  1621. * lengths instead of fixing only the wrong ones. This idea is taken
  1622. * from 'ar' written by Haruhiko Okumura.)
  1623. */
  1624. for(bits = max_length; bits != 0; bits--) {
  1625. n = zip_bl_count[bits];
  1626. while(n != 0) {
  1627. m = zip_heap[--h];
  1628. if(m > max_code)
  1629. continue;
  1630. if(tree[m].dl != bits) {
  1631. zip_opt_len += (bits - tree[m].dl) * tree[m].fc;
  1632. tree[m].fc = bits;
  1633. }
  1634. n--;
  1635. }
  1636. }
  1637. }
  1638. /* ==========================================================================
  1639. * Generate the codes for a given tree and bit counts (which need not be
  1640. * optimal).
  1641. * IN assertion: the array bl_count contains the bit length statistics for
  1642. * the given tree and the field len is set for all tree elements.
  1643. * OUT assertion: the field code is set for all tree elements of non
  1644. * zero code length.
  1645. */
  1646. var zip_gen_codes = function(tree, // the tree to decorate
  1647. max_code) { // largest code with non zero frequency
  1648. var next_code = new Array(zip_MAX_BITS+1); // next code value for each bit length
  1649. var code = 0; // running code value
  1650. var bits; // bit index
  1651. var n; // code index
  1652. /* The distribution counts are first used to generate the code values
  1653. * without bit reversal.
  1654. */
  1655. for(bits = 1; bits <= zip_MAX_BITS; bits++) {
  1656. code = ((code + zip_bl_count[bits-1]) << 1);
  1657. next_code[bits] = code;
  1658. }
  1659. /* Check that the bit counts in bl_count are consistent. The last code
  1660. * must be all ones.
  1661. */
  1662. // Assert (code + encoder->bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  1663. // "inconsistent bit counts");
  1664. // Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  1665. for(n = 0; n <= max_code; n++) {
  1666. var len = tree[n].dl;
  1667. if(len == 0)
  1668. continue;
  1669. // Now reverse the bits
  1670. tree[n].fc = zip_bi_reverse(next_code[len]++, len);
  1671. // Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  1672. // n, (isgraph(n) ? n : ' '), len, tree[n].fc, next_code[len]-1));
  1673. }
  1674. }
  1675. /* ==========================================================================
  1676. * Construct one Huffman tree and assigns the code bit strings and lengths.
  1677. * Update the total bit length for the current block.
  1678. * IN assertion: the field freq is set for all tree elements.
  1679. * OUT assertions: the fields len and code are set to the optimal bit length
  1680. * and corresponding code. The length opt_len is updated; static_len is
  1681. * also updated if stree is not null. The field max_code is set.
  1682. */
  1683. var zip_build_tree = function(desc) { // the tree descriptor
  1684. var tree = desc.dyn_tree;
  1685. var stree = desc.static_tree;
  1686. var elems = desc.elems;
  1687. var n, m; // iterate over heap elements
  1688. var max_code = -1; // largest code with non zero frequency
  1689. var node = elems; // next internal node of the tree
  1690. /* Construct the initial heap, with least frequent element in
  1691. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  1692. * heap[0] is not used.
  1693. */
  1694. zip_heap_len = 0;
  1695. zip_heap_max = zip_HEAP_SIZE;
  1696. for(n = 0; n < elems; n++) {
  1697. if(tree[n].fc != 0) {
  1698. zip_heap[++zip_heap_len] = max_code = n;
  1699. zip_depth[n] = 0;
  1700. } else
  1701. tree[n].dl = 0;
  1702. }
  1703. /* The pkzip format requires that at least one distance code exists,
  1704. * and that at least one bit should be sent even if there is only one
  1705. * possible code. So to avoid special checks later on we force at least
  1706. * two codes of non zero frequency.
  1707. */
  1708. while(zip_heap_len < 2) {
  1709. var xnew = zip_heap[++zip_heap_len] = (max_code < 2 ? ++max_code : 0);
  1710. tree[xnew].fc = 1;
  1711. zip_depth[xnew] = 0;
  1712. zip_opt_len--;
  1713. if(stree != null)
  1714. zip_static_len -= stree[xnew].dl;
  1715. // new is 0 or 1 so it does not have extra bits
  1716. }
  1717. desc.max_code = max_code;
  1718. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  1719. * establish sub-heaps of increasing lengths:
  1720. */
  1721. for(n = zip_heap_len >> 1; n >= 1; n--)
  1722. zip_pqdownheap(tree, n);
  1723. /* Construct the Huffman tree by repeatedly combining the least two
  1724. * frequent nodes.
  1725. */
  1726. do {
  1727. n = zip_heap[zip_SMALLEST];
  1728. zip_heap[zip_SMALLEST] = zip_heap[zip_heap_len--];
  1729. zip_pqdownheap(tree, zip_SMALLEST);
  1730. m = zip_heap[zip_SMALLEST]; // m = node of next least frequency
  1731. // keep the nodes sorted by frequency
  1732. zip_heap[--zip_heap_max] = n;
  1733. zip_heap[--zip_heap_max] = m;
  1734. // Create a new node father of n and m
  1735. tree[node].fc = tree[n].fc + tree[m].fc;
  1736. // depth[node] = (char)(MAX(depth[n], depth[m]) + 1);
  1737. if(zip_depth[n] > zip_depth[m] + 1)
  1738. zip_depth[node] = zip_depth[n];
  1739. else
  1740. zip_depth[node] = zip_depth[m] + 1;
  1741. tree[n].dl = tree[m].dl = node;
  1742. // and insert the new node in the heap
  1743. zip_heap[zip_SMALLEST] = node++;
  1744. zip_pqdownheap(tree, zip_SMALLEST);
  1745. } while(zip_heap_len >= 2);
  1746. zip_heap[--zip_heap_max] = zip_heap[zip_SMALLEST];
  1747. /* At this point, the fields freq and dad are set. We can now
  1748. * generate the bit lengths.
  1749. */
  1750. zip_gen_bitlen(desc);
  1751. // The field len is now set, we can generate the bit codes
  1752. zip_gen_codes(tree, max_code);
  1753. }
  1754. /* ==========================================================================
  1755. * Scan a literal or distance tree to determine the frequencies of the codes
  1756. * in the bit length tree. Updates opt_len to take into account the repeat
  1757. * counts. (The contribution of the bit length codes will be added later
  1758. * during the construction of bl_tree.)
  1759. */
  1760. var zip_scan_tree = function(tree,// the tree to be scanned
  1761. max_code) { // and its largest code of non zero frequency
  1762. var n; // iterates over all tree elements
  1763. var prevlen = -1; // last emitted length
  1764. var curlen; // length of current code
  1765. var nextlen = tree[0].dl; // length of next code
  1766. var count = 0; // repeat count of the current code
  1767. var max_count = 7; // max repeat count
  1768. var min_count = 4; // min repeat count
  1769. if(nextlen == 0) {
  1770. max_count = 138;
  1771. min_count = 3;
  1772. }
  1773. tree[max_code + 1].dl = 0xffff; // guard
  1774. for(n = 0; n <= max_code; n++) {
  1775. curlen = nextlen;
  1776. nextlen = tree[n + 1].dl;
  1777. if(++count < max_count && curlen == nextlen)
  1778. continue;
  1779. else if(count < min_count)
  1780. zip_bl_tree[curlen].fc += count;
  1781. else if(curlen != 0) {
  1782. if(curlen != prevlen)
  1783. zip_bl_tree[curlen].fc++;
  1784. zip_bl_tree[zip_REP_3_6].fc++;
  1785. } else if(count <= 10)
  1786. zip_bl_tree[zip_REPZ_3_10].fc++;
  1787. else
  1788. zip_bl_tree[zip_REPZ_11_138].fc++;
  1789. count = 0; prevlen = curlen;
  1790. if(nextlen == 0) {
  1791. max_count = 138;
  1792. min_count = 3;
  1793. } else if(curlen == nextlen) {
  1794. max_count = 6;
  1795. min_count = 3;
  1796. } else {
  1797. max_count = 7;
  1798. min_count = 4;
  1799. }
  1800. }
  1801. }
  1802. /* ==========================================================================
  1803. * Send a literal or distance tree in compressed form, using the codes in
  1804. * bl_tree.
  1805. */
  1806. var zip_send_tree = function(tree, // the tree to be scanned
  1807. max_code) { // and its largest code of non zero frequency
  1808. var n; // iterates over all tree elements
  1809. var prevlen = -1; // last emitted length
  1810. var curlen; // length of current code
  1811. var nextlen = tree[0].dl; // length of next code
  1812. var count = 0; // repeat count of the current code
  1813. var max_count = 7; // max repeat count
  1814. var min_count = 4; // min repeat count
  1815. /* tree[max_code+1].dl = -1; */ /* guard already set */
  1816. if(nextlen == 0) {
  1817. max_count = 138;
  1818. min_count = 3;
  1819. }
  1820. for(n = 0; n <= max_code; n++) {
  1821. curlen = nextlen;
  1822. nextlen = tree[n+1].dl;
  1823. if(++count < max_count && curlen == nextlen) {
  1824. continue;
  1825. } else if(count < min_count) {
  1826. do { zip_SEND_CODE(curlen, zip_bl_tree); } while(--count != 0);
  1827. } else if(curlen != 0) {
  1828. if(curlen != prevlen) {
  1829. zip_SEND_CODE(curlen, zip_bl_tree);
  1830. count--;
  1831. }
  1832. // Assert(count >= 3 && count <= 6, " 3_6?");
  1833. zip_SEND_CODE(zip_REP_3_6, zip_bl_tree);
  1834. zip_send_bits(count - 3, 2);
  1835. } else if(count <= 10) {
  1836. zip_SEND_CODE(zip_REPZ_3_10, zip_bl_tree);
  1837. zip_send_bits(count-3, 3);
  1838. } else {
  1839. zip_SEND_CODE(zip_REPZ_11_138, zip_bl_tree);
  1840. zip_send_bits(count-11, 7);
  1841. }
  1842. count = 0;
  1843. prevlen = curlen;
  1844. if(nextlen == 0) {
  1845. max_count = 138;
  1846. min_count = 3;
  1847. } else if(curlen == nextlen) {
  1848. max_count = 6;
  1849. min_count = 3;
  1850. } else {
  1851. max_count = 7;
  1852. min_count = 4;
  1853. }
  1854. }
  1855. }
  1856. /* ==========================================================================
  1857. * Construct the Huffman tree for the bit lengths and return the index in
  1858. * bl_order of the last bit length code to send.
  1859. */
  1860. var zip_build_bl_tree = function() {
  1861. var max_blindex; // index of last bit length code of non zero freq
  1862. // Determine the bit length frequencies for literal and distance trees
  1863. zip_scan_tree(zip_dyn_ltree, zip_l_desc.max_code);
  1864. zip_scan_tree(zip_dyn_dtree, zip_d_desc.max_code);
  1865. // Build the bit length tree:
  1866. zip_build_tree(zip_bl_desc);
  1867. /* opt_len now includes the length of the tree representations, except
  1868. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  1869. */
  1870. /* Determine the number of bit length codes to send. The pkzip format
  1871. * requires that at least 4 bit length codes be sent. (appnote.txt says
  1872. * 3 but the actual value used is 4.)
  1873. */
  1874. for(max_blindex = zip_BL_CODES-1; max_blindex >= 3; max_blindex--) {
  1875. if(zip_bl_tree[zip_bl_order[max_blindex]].dl != 0) break;
  1876. }
  1877. /* Update opt_len to include the bit length tree and counts */
  1878. zip_opt_len += 3*(max_blindex+1) + 5+5+4;
  1879. // Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  1880. // encoder->opt_len, encoder->static_len));
  1881. return max_blindex;
  1882. }
  1883. /* ==========================================================================
  1884. * Send the header for a block using dynamic Huffman trees: the counts, the
  1885. * lengths of the bit length codes, the literal tree and the distance tree.
  1886. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  1887. */
  1888. var zip_send_all_trees = function(lcodes, dcodes, blcodes) { // number of codes for each tree
  1889. var rank; // index in bl_order
  1890. // Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  1891. // Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  1892. // "too many codes");
  1893. // Tracev((stderr, "\nbl counts: "));
  1894. zip_send_bits(lcodes-257, 5); // not +255 as stated in appnote.txt
  1895. zip_send_bits(dcodes-1, 5);
  1896. zip_send_bits(blcodes-4, 4); // not -3 as stated in appnote.txt
  1897. for(rank = 0; rank < blcodes; rank++) {
  1898. // Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  1899. zip_send_bits(zip_bl_tree[zip_bl_order[rank]].dl, 3);
  1900. }
  1901. // send the literal tree
  1902. zip_send_tree(zip_dyn_ltree,lcodes-1);
  1903. // send the distance tree
  1904. zip_send_tree(zip_dyn_dtree,dcodes-1);
  1905. }
  1906. /* ==========================================================================
  1907. * Determine the best encoding for the current block: dynamic trees, static
  1908. * trees or store, and output the encoded block to the zip file.
  1909. */
  1910. var zip_flush_block = function(eof) { // true if this is the last block for a file
  1911. var opt_lenb, static_lenb; // opt_len and static_len in bytes
  1912. var max_blindex; // index of last bit length code of non zero freq
  1913. var stored_len; // length of input block
  1914. stored_len = zip_strstart - zip_block_start;
  1915. zip_flag_buf[zip_last_flags] = zip_flags; // Save the flags for the last 8 items
  1916. // Construct the literal and distance trees
  1917. zip_build_tree(zip_l_desc);
  1918. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld",
  1919. // encoder->opt_len, encoder->static_len));
  1920. zip_build_tree(zip_d_desc);
  1921. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld",
  1922. // encoder->opt_len, encoder->static_len));
  1923. /* At this point, opt_len and static_len are the total bit lengths of
  1924. * the compressed block data, excluding the tree representations.
  1925. */
  1926. /* Build the bit length tree for the above two trees, and get the index
  1927. * in bl_order of the last bit length code to send.
  1928. */
  1929. max_blindex = zip_build_bl_tree();
  1930. // Determine the best encoding. Compute first the block length in bytes
  1931. opt_lenb = (zip_opt_len +3+7)>>3;
  1932. static_lenb = (zip_static_len+3+7)>>3;
  1933. // Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
  1934. // opt_lenb, encoder->opt_len,
  1935. // static_lenb, encoder->static_len, stored_len,
  1936. // encoder->last_lit, encoder->last_dist));
  1937. if(static_lenb <= opt_lenb)
  1938. opt_lenb = static_lenb;
  1939. if(stored_len + 4 <= opt_lenb // 4: two words for the lengths
  1940. && zip_block_start >= 0) {
  1941. var i;
  1942. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1943. * Otherwise we can't have processed more than WSIZE input bytes since
  1944. * the last block flush, because compression would have been
  1945. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1946. * transform a block into a stored block.
  1947. */
  1948. zip_send_bits((zip_STORED_BLOCK<<1)+eof, 3); /* send block type */
  1949. zip_bi_windup(); /* align on byte boundary */
  1950. zip_put_short(stored_len);
  1951. zip_put_short(~stored_len);
  1952. // copy block
  1953. /*
  1954. p = &window[block_start];
  1955. for(i = 0; i < stored_len; i++)
  1956. put_byte(p[i]);
  1957. */
  1958. for(i = 0; i < stored_len; i++)
  1959. zip_put_byte(zip_window[zip_block_start + i]);
  1960. } else if(static_lenb == opt_lenb) {
  1961. zip_send_bits((zip_STATIC_TREES<<1)+eof, 3);
  1962. zip_compress_block(zip_static_ltree, zip_static_dtree);
  1963. } else {
  1964. zip_send_bits((zip_DYN_TREES<<1)+eof, 3);
  1965. zip_send_all_trees(zip_l_desc.max_code+1,
  1966. zip_d_desc.max_code+1,
  1967. max_blindex+1);
  1968. zip_compress_block(zip_dyn_ltree, zip_dyn_dtree);
  1969. }
  1970. zip_init_block();
  1971. if(eof != 0)
  1972. zip_bi_windup();
  1973. }
  1974. /* ==========================================================================
  1975. * Save the match info and tally the frequency counts. Return true if
  1976. * the current block must be flushed.
  1977. */
  1978. var zip_ct_tally = function(
  1979. dist, // distance of matched string
  1980. lc) { // match length-MIN_MATCH or unmatched char (if dist==0)
  1981. zip_l_buf[zip_last_lit++] = lc;
  1982. if(dist == 0) {
  1983. // lc is the unmatched char
  1984. zip_dyn_ltree[lc].fc++;
  1985. } else {
  1986. // Here, lc is the match length - MIN_MATCH
  1987. dist--; // dist = match distance - 1
  1988. // Assert((ush)dist < (ush)MAX_DIST &&
  1989. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  1990. // (ush)D_CODE(dist) < (ush)D_CODES, "ct_tally: bad match");
  1991. zip_dyn_ltree[zip_length_code[lc]+zip_LITERALS+1].fc++;
  1992. zip_dyn_dtree[zip_D_CODE(dist)].fc++;
  1993. zip_d_buf[zip_last_dist++] = dist;
  1994. zip_flags |= zip_flag_bit;
  1995. }
  1996. zip_flag_bit <<= 1;
  1997. // Output the flags if they fill a byte
  1998. if((zip_last_lit & 7) == 0) {
  1999. zip_flag_buf[zip_last_flags++] = zip_flags;
  2000. zip_flags = 0;
  2001. zip_flag_bit = 1;
  2002. }
  2003. // Try to guess if it is profitable to stop the current block here
  2004. if(zip_compr_level > 2 && (zip_last_lit & 0xfff) == 0) {
  2005. // Compute an upper bound for the compressed length
  2006. var out_length = zip_last_lit * 8;
  2007. var in_length = zip_strstart - zip_block_start;
  2008. var dcode;
  2009. for(dcode = 0; dcode < zip_D_CODES; dcode++) {
  2010. out_length += zip_dyn_dtree[dcode].fc * (5 + zip_extra_dbits[dcode]);
  2011. }
  2012. out_length >>= 3;
  2013. // Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
  2014. // encoder->last_lit, encoder->last_dist, in_length, out_length,
  2015. // 100L - out_length*100L/in_length));
  2016. if(zip_last_dist < parseInt(zip_last_lit/2) &&
  2017. out_length < parseInt(in_length/2))
  2018. return true;
  2019. }
  2020. return (zip_last_lit == zip_LIT_BUFSIZE-1 ||
  2021. zip_last_dist == zip_DIST_BUFSIZE);
  2022. /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
  2023. * on 16 bit machines and because stored blocks are restricted to
  2024. * 64K-1 bytes.
  2025. */
  2026. }
  2027. /* ==========================================================================
  2028. * Send the block data compressed using the given Huffman trees
  2029. */
  2030. var zip_compress_block = function(
  2031. ltree, // literal tree
  2032. dtree) { // distance tree
  2033. var dist; // distance of matched string
  2034. var lc; // match length or unmatched char (if dist == 0)
  2035. var lx = 0; // running index in l_buf
  2036. var dx = 0; // running index in d_buf
  2037. var fx = 0; // running index in flag_buf
  2038. var flag = 0; // current flags
  2039. var code; // the code to send
  2040. var extra; // number of extra bits to send
  2041. if(zip_last_lit != 0) do {
  2042. if((lx & 7) == 0)
  2043. flag = zip_flag_buf[fx++];
  2044. lc = zip_l_buf[lx++] & 0xff;
  2045. if((flag & 1) == 0) {
  2046. zip_SEND_CODE(lc, ltree); /* send a literal byte */
  2047. // Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  2048. } else {
  2049. // Here, lc is the match length - MIN_MATCH
  2050. code = zip_length_code[lc];
  2051. zip_SEND_CODE(code+zip_LITERALS+1, ltree); // send the length code
  2052. extra = zip_extra_lbits[code];
  2053. if(extra != 0) {
  2054. lc -= zip_base_length[code];
  2055. zip_send_bits(lc, extra); // send the extra length bits
  2056. }
  2057. dist = zip_d_buf[dx++];
  2058. // Here, dist is the match distance - 1
  2059. code = zip_D_CODE(dist);
  2060. // Assert (code < D_CODES, "bad d_code");
  2061. zip_SEND_CODE(code, dtree); // send the distance code
  2062. extra = zip_extra_dbits[code];
  2063. if(extra != 0) {
  2064. dist -= zip_base_dist[code];
  2065. zip_send_bits(dist, extra); // send the extra distance bits
  2066. }
  2067. } // literal or match pair ?
  2068. flag >>= 1;
  2069. } while(lx < zip_last_lit);
  2070. zip_SEND_CODE(zip_END_BLOCK, ltree);
  2071. }
  2072. /* ==========================================================================
  2073. * Send a value on a given number of bits.
  2074. * IN assertion: length <= 16 and value fits in length bits.
  2075. */
  2076. var zip_Buf_size = 16; // bit size of bi_buf
  2077. var zip_send_bits = function(
  2078. value, // value to send
  2079. length) { // number of bits
  2080. /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  2081. * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  2082. * unused bits in value.
  2083. */
  2084. if(zip_bi_valid > zip_Buf_size - length) {
  2085. zip_bi_buf |= (value << zip_bi_valid);
  2086. zip_put_short(zip_bi_buf);
  2087. zip_bi_buf = (value >> (zip_Buf_size - zip_bi_valid));
  2088. zip_bi_valid += length - zip_Buf_size;
  2089. } else {
  2090. zip_bi_buf |= value << zip_bi_valid;
  2091. zip_bi_valid += length;
  2092. }
  2093. }
  2094. /* ==========================================================================
  2095. * Reverse the first len bits of a code, using straightforward code (a faster
  2096. * method would use a table)
  2097. * IN assertion: 1 <= len <= 15
  2098. */
  2099. var zip_bi_reverse = function(
  2100. code, // the value to invert
  2101. len) { // its bit length
  2102. var res = 0;
  2103. do {
  2104. res |= code & 1;
  2105. code >>= 1;
  2106. res <<= 1;
  2107. } while(--len > 0);
  2108. return res >> 1;
  2109. }
  2110. /* ==========================================================================
  2111. * Write out any remaining bits in an incomplete byte.
  2112. */
  2113. var zip_bi_windup = function() {
  2114. if(zip_bi_valid > 8) {
  2115. zip_put_short(zip_bi_buf);
  2116. } else if(zip_bi_valid > 0) {
  2117. zip_put_byte(zip_bi_buf);
  2118. }
  2119. zip_bi_buf = 0;
  2120. zip_bi_valid = 0;
  2121. }
  2122. var zip_qoutbuf = function() {
  2123. if(zip_outcnt != 0) {
  2124. var q, i;
  2125. q = zip_new_queue();
  2126. if(zip_qhead == null)
  2127. zip_qhead = zip_qtail = q;
  2128. else
  2129. zip_qtail = zip_qtail.next = q;
  2130. q.len = zip_outcnt - zip_outoff;
  2131. // System.arraycopy(zip_outbuf, zip_outoff, q.ptr, 0, q.len);
  2132. for(i = 0; i < q.len; i++)
  2133. q.ptr[i] = zip_outbuf[zip_outoff + i];
  2134. zip_outcnt = zip_outoff = 0;
  2135. }
  2136. }
  2137. var zip_deflate = function(str, level) {
  2138. var i, j;
  2139. zip_deflate_data = str;
  2140. zip_deflate_pos = 0;
  2141. if(typeof level == "undefined")
  2142. level = zip_DEFAULT_LEVEL;
  2143. zip_deflate_start(level);
  2144. var buff = new Array(1024);
  2145. var aout = [];
  2146. while((i = zip_deflate_internal(buff, 0, buff.length)) > 0) {
  2147. var cbuf = new Array(i);
  2148. for(j = 0; j < i; j++){
  2149. cbuf[j] = String.fromCharCode(buff[j]);
  2150. }
  2151. aout[aout.length] = cbuf.join("");
  2152. }
  2153. zip_deflate_data = null; // G.C.
  2154. return aout.join("");
  2155. }
  2156. if (! ctx.RawDeflate) ctx.RawDeflate = {};
  2157. ctx.RawDeflate.deflate = zip_deflate;
  2158. })(this);
  2159. /*
  2160. * $Id: base64.js,v 2.11 2013/04/08 12:27:14 dankogai Exp dankogai $
  2161. *
  2162. * Licensed under the MIT license.
  2163. * http://opensource.org/licenses/mit-license
  2164. *
  2165. * References:
  2166. * http://en.wikipedia.org/wiki/Base64
  2167. */
  2168. (function(global) {
  2169. 'use strict';
  2170. if (global.Base64) return;
  2171. var version = "2.1.1";
  2172. // if node.js, we use Buffer
  2173. var buffer;
  2174. if (typeof module !== 'undefined' && module.exports) {
  2175. buffer = require('buffer').Buffer;
  2176. }
  2177. // constants
  2178. var b64chars
  2179. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  2180. var b64tab = function(bin) {
  2181. var t = {};
  2182. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  2183. return t;
  2184. }(b64chars);
  2185. var fromCharCode = String.fromCharCode;
  2186. // encoder stuff
  2187. var cb_utob = function(c) {
  2188. if (c.length < 2) {
  2189. var cc = c.charCodeAt(0);
  2190. return cc < 0x80 ? c
  2191. : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
  2192. + fromCharCode(0x80 | (cc & 0x3f)))
  2193. : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
  2194. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  2195. + fromCharCode(0x80 | ( cc & 0x3f)));
  2196. } else {
  2197. var cc = 0x10000
  2198. + (c.charCodeAt(0) - 0xD800) * 0x400
  2199. + (c.charCodeAt(1) - 0xDC00);
  2200. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
  2201. + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
  2202. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  2203. + fromCharCode(0x80 | ( cc & 0x3f)));
  2204. }
  2205. };
  2206. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  2207. var utob = function(u) {
  2208. return u.replace(re_utob, cb_utob);
  2209. };
  2210. var cb_encode = function(ccc) {
  2211. var padlen = [0, 2, 1][ccc.length % 3],
  2212. ord = ccc.charCodeAt(0) << 16
  2213. | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
  2214. | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
  2215. chars = [
  2216. b64chars.charAt( ord >>> 18),
  2217. b64chars.charAt((ord >>> 12) & 63),
  2218. padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
  2219. padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
  2220. ];
  2221. return chars.join('');
  2222. };
  2223. var btoa = global.btoa || function(b) {
  2224. return b.replace(/[\s\S]{1,3}/g, cb_encode);
  2225. };
  2226. var _encode = buffer
  2227. ? function (u) { return (new buffer(u)).toString('base64') }
  2228. : function (u) { return btoa(utob(u)) }
  2229. ;
  2230. var encode = function(u, urisafe) {
  2231. return !urisafe
  2232. ? _encode(u)
  2233. : _encode(u).replace(/[+\/]/g, function(m0) {
  2234. return m0 == '+' ? '-' : '_';
  2235. }).replace(/=/g, '');
  2236. };
  2237. var encodeURI = function(u) { return encode(u, true) };
  2238. // decoder stuff
  2239. var re_btou = new RegExp([
  2240. '[\xC0-\xDF][\x80-\xBF]',
  2241. '[\xE0-\xEF][\x80-\xBF]{2}',
  2242. '[\xF0-\xF7][\x80-\xBF]{3}'
  2243. ].join('|'), 'g');
  2244. var cb_btou = function(cccc) {
  2245. switch(cccc.length) {
  2246. case 4:
  2247. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  2248. | ((0x3f & cccc.charCodeAt(1)) << 12)
  2249. | ((0x3f & cccc.charCodeAt(2)) << 6)
  2250. | (0x3f & cccc.charCodeAt(3)),
  2251. offset = cp - 0x10000;
  2252. return (fromCharCode((offset >>> 10) + 0xD800)
  2253. + fromCharCode((offset & 0x3FF) + 0xDC00));
  2254. case 3:
  2255. return fromCharCode(
  2256. ((0x0f & cccc.charCodeAt(0)) << 12)
  2257. | ((0x3f & cccc.charCodeAt(1)) << 6)
  2258. | (0x3f & cccc.charCodeAt(2))
  2259. );
  2260. default:
  2261. return fromCharCode(
  2262. ((0x1f & cccc.charCodeAt(0)) << 6)
  2263. | (0x3f & cccc.charCodeAt(1))
  2264. );
  2265. }
  2266. };
  2267. var btou = function(b) {
  2268. return b.replace(re_btou, cb_btou);
  2269. };
  2270. var cb_decode = function(cccc) {
  2271. var len = cccc.length,
  2272. padlen = len % 4,
  2273. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  2274. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  2275. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  2276. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  2277. chars = [
  2278. fromCharCode( n >>> 16),
  2279. fromCharCode((n >>> 8) & 0xff),
  2280. fromCharCode( n & 0xff)
  2281. ];
  2282. chars.length -= [0, 0, 2, 1][padlen];
  2283. return chars.join('');
  2284. };
  2285. var atob = global.atob || function(a){
  2286. return a.replace(/[\s\S]{1,4}/g, cb_decode);
  2287. };
  2288. var _decode = buffer
  2289. ? function(a) { return (new buffer(a, 'base64')).toString() }
  2290. : function(a) { return btou(atob(a)) };
  2291. var decode = function(a){
  2292. return _decode(
  2293. a.replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
  2294. .replace(/[^A-Za-z0-9\+\/]/g, '')
  2295. );
  2296. };
  2297. // export Base64
  2298. global.Base64 = {
  2299. VERSION: version,
  2300. atob: atob,
  2301. btoa: btoa,
  2302. fromBase64: decode,
  2303. toBase64: encode,
  2304. utob: utob,
  2305. encode: encode,
  2306. encodeURI: encodeURI,
  2307. btou: btou,
  2308. decode: decode
  2309. };
  2310. // if ES5 is available, make Base64.extendString() available
  2311. if (typeof Object.defineProperty === 'function') {
  2312. var noEnum = function(v){
  2313. return {value:v,enumerable:false,writable:true,configurable:true};
  2314. };
  2315. global.Base64.extendString = function () {
  2316. Object.defineProperty(
  2317. String.prototype, 'fromBase64', noEnum(function () {
  2318. return decode(this)
  2319. }));
  2320. Object.defineProperty(
  2321. String.prototype, 'toBase64', noEnum(function (urisafe) {
  2322. return encode(this, urisafe)
  2323. }));
  2324. Object.defineProperty(
  2325. String.prototype, 'toBase64URI', noEnum(function () {
  2326. return encode(this, true)
  2327. }));
  2328. };
  2329. }
  2330. // that's it!
  2331. })(this);