rawdeflate-0.5.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /*
  2. * $Id: rawdeflate.js,v 0.5 2013/04/09 14:25:38 dankogai Exp dankogai $
  3. *
  4. * GNU General Public License, version 2 (GPL-2.0)
  5. * https://opensource.org/licenses/GPL-2.0
  6. * Original:
  7. * http://www.onicos.com/staff/iz/amuse/javascript/expert/deflate.txt
  8. */
  9. (function(ctx){
  10. /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
  11. * Version: 1.0.1
  12. * LastModified: Dec 25 1999
  13. */
  14. /* Interface:
  15. * data = zip_deflate(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 deflate */
  23. var zip_DEFAULT_LEVEL = 6;
  24. var zip_FULL_SEARCH = true;
  25. var zip_INBUFSIZ = 32768; // Input buffer size
  26. var zip_INBUF_EXTRA = 64; // Extra buffer
  27. var zip_OUTBUFSIZ = 1024 * 8;
  28. var zip_window_size = 2 * zip_WSIZE;
  29. var zip_MIN_MATCH = 3;
  30. var zip_MAX_MATCH = 258;
  31. var zip_BITS = 16;
  32. // for SMALL_MEM
  33. var zip_LIT_BUFSIZE = 0x2000;
  34. var zip_HASH_BITS = 13;
  35. // for MEDIUM_MEM
  36. // var zip_LIT_BUFSIZE = 0x4000;
  37. // var zip_HASH_BITS = 14;
  38. // for BIG_MEM
  39. // var zip_LIT_BUFSIZE = 0x8000;
  40. // var zip_HASH_BITS = 15;
  41. if(zip_LIT_BUFSIZE > zip_INBUFSIZ)
  42. alert("error: zip_INBUFSIZ is too small");
  43. if((zip_WSIZE<<1) > (1<<zip_BITS))
  44. alert("error: zip_WSIZE is too large");
  45. if(zip_HASH_BITS > zip_BITS-1)
  46. alert("error: zip_HASH_BITS is too large");
  47. if(zip_HASH_BITS < 8 || zip_MAX_MATCH != 258)
  48. alert("error: Code too clever");
  49. var zip_DIST_BUFSIZE = zip_LIT_BUFSIZE;
  50. var zip_HASH_SIZE = 1 << zip_HASH_BITS;
  51. var zip_HASH_MASK = zip_HASH_SIZE - 1;
  52. var zip_WMASK = zip_WSIZE - 1;
  53. var zip_NIL = 0; // Tail of hash chains
  54. var zip_TOO_FAR = 4096;
  55. var zip_MIN_LOOKAHEAD = zip_MAX_MATCH + zip_MIN_MATCH + 1;
  56. var zip_MAX_DIST = zip_WSIZE - zip_MIN_LOOKAHEAD;
  57. var zip_SMALLEST = 1;
  58. var zip_MAX_BITS = 15;
  59. var zip_MAX_BL_BITS = 7;
  60. var zip_LENGTH_CODES = 29;
  61. var zip_LITERALS =256;
  62. var zip_END_BLOCK = 256;
  63. var zip_L_CODES = zip_LITERALS + 1 + zip_LENGTH_CODES;
  64. var zip_D_CODES = 30;
  65. var zip_BL_CODES = 19;
  66. var zip_REP_3_6 = 16;
  67. var zip_REPZ_3_10 = 17;
  68. var zip_REPZ_11_138 = 18;
  69. var zip_HEAP_SIZE = 2 * zip_L_CODES + 1;
  70. var zip_H_SHIFT = parseInt((zip_HASH_BITS + zip_MIN_MATCH - 1) /
  71. zip_MIN_MATCH);
  72. /* variables */
  73. var zip_free_queue;
  74. var zip_qhead, zip_qtail;
  75. var zip_initflag;
  76. var zip_outbuf = null;
  77. var zip_outcnt, zip_outoff;
  78. var zip_complete;
  79. var zip_window;
  80. var zip_d_buf;
  81. var zip_l_buf;
  82. var zip_prev;
  83. var zip_bi_buf;
  84. var zip_bi_valid;
  85. var zip_block_start;
  86. var zip_ins_h;
  87. var zip_hash_head;
  88. var zip_prev_match;
  89. var zip_match_available;
  90. var zip_match_length;
  91. var zip_prev_length;
  92. var zip_strstart;
  93. var zip_match_start;
  94. var zip_eofile;
  95. var zip_lookahead;
  96. var zip_max_chain_length;
  97. var zip_max_lazy_match;
  98. var zip_compr_level;
  99. var zip_good_match;
  100. var zip_nice_match;
  101. var zip_dyn_ltree;
  102. var zip_dyn_dtree;
  103. var zip_static_ltree;
  104. var zip_static_dtree;
  105. var zip_bl_tree;
  106. var zip_l_desc;
  107. var zip_d_desc;
  108. var zip_bl_desc;
  109. var zip_bl_count;
  110. var zip_heap;
  111. var zip_heap_len;
  112. var zip_heap_max;
  113. var zip_depth;
  114. var zip_length_code;
  115. var zip_dist_code;
  116. var zip_base_length;
  117. var zip_base_dist;
  118. var zip_flag_buf;
  119. var zip_last_lit;
  120. var zip_last_dist;
  121. var zip_last_flags;
  122. var zip_flags;
  123. var zip_flag_bit;
  124. var zip_opt_len;
  125. var zip_static_len;
  126. var zip_deflate_data;
  127. var zip_deflate_pos;
  128. /* objects (deflate) */
  129. var zip_DeflateCT = function() {
  130. this.fc = 0; // frequency count or bit string
  131. this.dl = 0; // father node in Huffman tree or length of bit string
  132. }
  133. var zip_DeflateTreeDesc = function() {
  134. this.dyn_tree = null; // the dynamic tree
  135. this.static_tree = null; // corresponding static tree or NULL
  136. this.extra_bits = null; // extra bits for each code or NULL
  137. this.extra_base = 0; // base index for extra_bits
  138. this.elems = 0; // max number of elements in the tree
  139. this.max_length = 0; // max bit length for the codes
  140. this.max_code = 0; // largest code with non zero frequency
  141. }
  142. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  143. * the desired pack level (0..9). The values given below have been tuned to
  144. * exclude worst case performance for pathological files. Better values may be
  145. * found for specific files.
  146. */
  147. var zip_DeflateConfiguration = function(a, b, c, d) {
  148. this.good_length = a; // reduce lazy search above this match length
  149. this.max_lazy = b; // do not perform lazy search above this match length
  150. this.nice_length = c; // quit search above this match length
  151. this.max_chain = d;
  152. }
  153. var zip_DeflateBuffer = function() {
  154. this.next = null;
  155. this.len = 0;
  156. this.ptr = new Array(zip_OUTBUFSIZ);
  157. this.off = 0;
  158. }
  159. /* constant tables */
  160. var zip_extra_lbits = new Array(
  161. 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);
  162. var zip_extra_dbits = new Array(
  163. 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);
  164. var zip_extra_blbits = new Array(
  165. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
  166. var zip_bl_order = new Array(
  167. 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
  168. var zip_configuration_table = new Array(
  169. new zip_DeflateConfiguration(0, 0, 0, 0),
  170. new zip_DeflateConfiguration(4, 4, 8, 4),
  171. new zip_DeflateConfiguration(4, 5, 16, 8),
  172. new zip_DeflateConfiguration(4, 6, 32, 32),
  173. new zip_DeflateConfiguration(4, 4, 16, 16),
  174. new zip_DeflateConfiguration(8, 16, 32, 32),
  175. new zip_DeflateConfiguration(8, 16, 128, 128),
  176. new zip_DeflateConfiguration(8, 32, 128, 256),
  177. new zip_DeflateConfiguration(32, 128, 258, 1024),
  178. new zip_DeflateConfiguration(32, 258, 258, 4096));
  179. /* routines (deflate) */
  180. var zip_deflate_start = function(level) {
  181. var i;
  182. if(!level)
  183. level = zip_DEFAULT_LEVEL;
  184. else if(level < 1)
  185. level = 1;
  186. else if(level > 9)
  187. level = 9;
  188. zip_compr_level = level;
  189. zip_initflag = false;
  190. zip_eofile = false;
  191. if(zip_outbuf != null)
  192. return;
  193. zip_free_queue = zip_qhead = zip_qtail = null;
  194. zip_outbuf = new Array(zip_OUTBUFSIZ);
  195. zip_window = new Array(zip_window_size);
  196. zip_d_buf = new Array(zip_DIST_BUFSIZE);
  197. zip_l_buf = new Array(zip_INBUFSIZ + zip_INBUF_EXTRA);
  198. zip_prev = new Array(1 << zip_BITS);
  199. zip_dyn_ltree = new Array(zip_HEAP_SIZE);
  200. for(i = 0; i < zip_HEAP_SIZE; i++)
  201. zip_dyn_ltree[i] = new zip_DeflateCT();
  202. zip_dyn_dtree = new Array(2*zip_D_CODES+1);
  203. for(i = 0; i < 2*zip_D_CODES+1; i++)
  204. zip_dyn_dtree[i] = new zip_DeflateCT();
  205. zip_static_ltree = new Array(zip_L_CODES+2);
  206. for(i = 0; i < zip_L_CODES+2; i++)
  207. zip_static_ltree[i] = new zip_DeflateCT();
  208. zip_static_dtree = new Array(zip_D_CODES);
  209. for(i = 0; i < zip_D_CODES; i++)
  210. zip_static_dtree[i] = new zip_DeflateCT();
  211. zip_bl_tree = new Array(2*zip_BL_CODES+1);
  212. for(i = 0; i < 2*zip_BL_CODES+1; i++)
  213. zip_bl_tree[i] = new zip_DeflateCT();
  214. zip_l_desc = new zip_DeflateTreeDesc();
  215. zip_d_desc = new zip_DeflateTreeDesc();
  216. zip_bl_desc = new zip_DeflateTreeDesc();
  217. zip_bl_count = new Array(zip_MAX_BITS+1);
  218. zip_heap = new Array(2*zip_L_CODES+1);
  219. zip_depth = new Array(2*zip_L_CODES+1);
  220. zip_length_code = new Array(zip_MAX_MATCH-zip_MIN_MATCH+1);
  221. zip_dist_code = new Array(512);
  222. zip_base_length = new Array(zip_LENGTH_CODES);
  223. zip_base_dist = new Array(zip_D_CODES);
  224. zip_flag_buf = new Array(parseInt(zip_LIT_BUFSIZE / 8));
  225. }
  226. var zip_deflate_end = function() {
  227. zip_free_queue = zip_qhead = zip_qtail = null;
  228. zip_outbuf = null;
  229. zip_window = null;
  230. zip_d_buf = null;
  231. zip_l_buf = null;
  232. zip_prev = null;
  233. zip_dyn_ltree = null;
  234. zip_dyn_dtree = null;
  235. zip_static_ltree = null;
  236. zip_static_dtree = null;
  237. zip_bl_tree = null;
  238. zip_l_desc = null;
  239. zip_d_desc = null;
  240. zip_bl_desc = null;
  241. zip_bl_count = null;
  242. zip_heap = null;
  243. zip_depth = null;
  244. zip_length_code = null;
  245. zip_dist_code = null;
  246. zip_base_length = null;
  247. zip_base_dist = null;
  248. zip_flag_buf = null;
  249. }
  250. var zip_reuse_queue = function(p) {
  251. p.next = zip_free_queue;
  252. zip_free_queue = p;
  253. }
  254. var zip_new_queue = function() {
  255. var p;
  256. if(zip_free_queue != null)
  257. {
  258. p = zip_free_queue;
  259. zip_free_queue = zip_free_queue.next;
  260. }
  261. else
  262. p = new zip_DeflateBuffer();
  263. p.next = null;
  264. p.len = p.off = 0;
  265. return p;
  266. }
  267. var zip_head1 = function(i) {
  268. return zip_prev[zip_WSIZE + i];
  269. }
  270. var zip_head2 = function(i, val) {
  271. return zip_prev[zip_WSIZE + i] = val;
  272. }
  273. /* put_byte is used for the compressed output, put_ubyte for the
  274. * uncompressed output. However unlzw() uses window for its
  275. * suffix table instead of its output buffer, so it does not use put_ubyte
  276. * (to be cleaned up).
  277. */
  278. var zip_put_byte = function(c) {
  279. zip_outbuf[zip_outoff + zip_outcnt++] = c;
  280. if(zip_outoff + zip_outcnt == zip_OUTBUFSIZ)
  281. zip_qoutbuf();
  282. }
  283. /* Output a 16 bit value, lsb first */
  284. var zip_put_short = function(w) {
  285. w &= 0xffff;
  286. if(zip_outoff + zip_outcnt < zip_OUTBUFSIZ - 2) {
  287. zip_outbuf[zip_outoff + zip_outcnt++] = (w & 0xff);
  288. zip_outbuf[zip_outoff + zip_outcnt++] = (w >>> 8);
  289. } else {
  290. zip_put_byte(w & 0xff);
  291. zip_put_byte(w >>> 8);
  292. }
  293. }
  294. /* ==========================================================================
  295. * Insert string s in the dictionary and set match_head to the previous head
  296. * of the hash chain (the most recent string with same hash key). Return
  297. * the previous length of the hash chain.
  298. * IN assertion: all calls to to INSERT_STRING are made with consecutive
  299. * input characters and the first MIN_MATCH bytes of s are valid
  300. * (except for the last MIN_MATCH-1 bytes of the input file).
  301. */
  302. var zip_INSERT_STRING = function() {
  303. zip_ins_h = ((zip_ins_h << zip_H_SHIFT)
  304. ^ (zip_window[zip_strstart + zip_MIN_MATCH - 1] & 0xff))
  305. & zip_HASH_MASK;
  306. zip_hash_head = zip_head1(zip_ins_h);
  307. zip_prev[zip_strstart & zip_WMASK] = zip_hash_head;
  308. zip_head2(zip_ins_h, zip_strstart);
  309. }
  310. /* Send a code of the given tree. c and tree must not have side effects */
  311. var zip_SEND_CODE = function(c, tree) {
  312. zip_send_bits(tree[c].fc, tree[c].dl);
  313. }
  314. /* Mapping from a distance to a distance code. dist is the distance - 1 and
  315. * must not have side effects. dist_code[256] and dist_code[257] are never
  316. * used.
  317. */
  318. var zip_D_CODE = function(dist) {
  319. return (dist < 256 ? zip_dist_code[dist]
  320. : zip_dist_code[256 + (dist>>7)]) & 0xff;
  321. }
  322. /* ==========================================================================
  323. * Compares to subtrees, using the tree depth as tie breaker when
  324. * the subtrees have equal frequency. This minimizes the worst case length.
  325. */
  326. var zip_SMALLER = function(tree, n, m) {
  327. return tree[n].fc < tree[m].fc ||
  328. (tree[n].fc == tree[m].fc && zip_depth[n] <= zip_depth[m]);
  329. }
  330. /* ==========================================================================
  331. * read string data
  332. */
  333. var zip_read_buff = function(buff, offset, n) {
  334. var i;
  335. for(i = 0; i < n && zip_deflate_pos < zip_deflate_data.length; i++)
  336. buff[offset + i] =
  337. zip_deflate_data.charCodeAt(zip_deflate_pos++) & 0xff;
  338. return i;
  339. }
  340. /* ==========================================================================
  341. * Initialize the "longest match" routines for a new file
  342. */
  343. var zip_lm_init = function() {
  344. var j;
  345. /* Initialize the hash table. */
  346. for(j = 0; j < zip_HASH_SIZE; j++)
  347. // zip_head2(j, zip_NIL);
  348. zip_prev[zip_WSIZE + j] = 0;
  349. /* prev will be initialized on the fly */
  350. /* Set the default configuration parameters:
  351. */
  352. zip_max_lazy_match = zip_configuration_table[zip_compr_level].max_lazy;
  353. zip_good_match = zip_configuration_table[zip_compr_level].good_length;
  354. if(!zip_FULL_SEARCH)
  355. zip_nice_match = zip_configuration_table[zip_compr_level].nice_length;
  356. zip_max_chain_length = zip_configuration_table[zip_compr_level].max_chain;
  357. zip_strstart = 0;
  358. zip_block_start = 0;
  359. zip_lookahead = zip_read_buff(zip_window, 0, 2 * zip_WSIZE);
  360. if(zip_lookahead <= 0) {
  361. zip_eofile = true;
  362. zip_lookahead = 0;
  363. return;
  364. }
  365. zip_eofile = false;
  366. /* Make sure that we always have enough lookahead. This is important
  367. * if input comes from a device such as a tty.
  368. */
  369. while(zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile)
  370. zip_fill_window();
  371. /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
  372. * not important since only literal bytes will be emitted.
  373. */
  374. zip_ins_h = 0;
  375. for(j = 0; j < zip_MIN_MATCH - 1; j++) {
  376. // UPDATE_HASH(ins_h, window[j]);
  377. zip_ins_h = ((zip_ins_h << zip_H_SHIFT) ^ (zip_window[j] & 0xff)) & zip_HASH_MASK;
  378. }
  379. }
  380. /* ==========================================================================
  381. * Set match_start to the longest match starting at the given string and
  382. * return its length. Matches shorter or equal to prev_length are discarded,
  383. * in which case the result is equal to prev_length and match_start is
  384. * garbage.
  385. * IN assertions: cur_match is the head of the hash chain for the current
  386. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  387. */
  388. var zip_longest_match = function(cur_match) {
  389. var chain_length = zip_max_chain_length; // max hash chain length
  390. var scanp = zip_strstart; // current string
  391. var matchp; // matched string
  392. var len; // length of current match
  393. var best_len = zip_prev_length; // best match length so far
  394. /* Stop when cur_match becomes <= limit. To simplify the code,
  395. * we prevent matches with the string of window index 0.
  396. */
  397. var limit = (zip_strstart > zip_MAX_DIST ? zip_strstart - zip_MAX_DIST : zip_NIL);
  398. var strendp = zip_strstart + zip_MAX_MATCH;
  399. var scan_end1 = zip_window[scanp + best_len - 1];
  400. var scan_end = zip_window[scanp + best_len];
  401. /* Do not waste too much time if we already have a good match: */
  402. if(zip_prev_length >= zip_good_match)
  403. chain_length >>= 2;
  404. // Assert(encoder->strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
  405. do {
  406. // Assert(cur_match < encoder->strstart, "no future");
  407. matchp = cur_match;
  408. /* Skip to next match if the match length cannot increase
  409. * or if the match length is less than 2:
  410. */
  411. if(zip_window[matchp + best_len] != scan_end ||
  412. zip_window[matchp + best_len - 1] != scan_end1 ||
  413. zip_window[matchp] != zip_window[scanp] ||
  414. zip_window[++matchp] != zip_window[scanp + 1]) {
  415. continue;
  416. }
  417. /* The check at best_len-1 can be removed because it will be made
  418. * again later. (This heuristic is not always a win.)
  419. * It is not necessary to compare scan[2] and match[2] since they
  420. * are always equal when the other bytes match, given that
  421. * the hash keys are equal and that HASH_BITS >= 8.
  422. */
  423. scanp += 2;
  424. matchp++;
  425. /* We check for insufficient lookahead only every 8th comparison;
  426. * the 256th check will be made at strstart+258.
  427. */
  428. do {
  429. } while(zip_window[++scanp] == zip_window[++matchp] &&
  430. zip_window[++scanp] == zip_window[++matchp] &&
  431. zip_window[++scanp] == zip_window[++matchp] &&
  432. zip_window[++scanp] == zip_window[++matchp] &&
  433. zip_window[++scanp] == zip_window[++matchp] &&
  434. zip_window[++scanp] == zip_window[++matchp] &&
  435. zip_window[++scanp] == zip_window[++matchp] &&
  436. zip_window[++scanp] == zip_window[++matchp] &&
  437. scanp < strendp);
  438. len = zip_MAX_MATCH - (strendp - scanp);
  439. scanp = strendp - zip_MAX_MATCH;
  440. if(len > best_len) {
  441. zip_match_start = cur_match;
  442. best_len = len;
  443. if(zip_FULL_SEARCH) {
  444. if(len >= zip_MAX_MATCH) break;
  445. } else {
  446. if(len >= zip_nice_match) break;
  447. }
  448. scan_end1 = zip_window[scanp + best_len-1];
  449. scan_end = zip_window[scanp + best_len];
  450. }
  451. } while((cur_match = zip_prev[cur_match & zip_WMASK]) > limit
  452. && --chain_length != 0);
  453. return best_len;
  454. }
  455. /* ==========================================================================
  456. * Fill the window when the lookahead becomes insufficient.
  457. * Updates strstart and lookahead, and sets eofile if end of input file.
  458. * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
  459. * OUT assertions: at least one byte has been read, or eofile is set;
  460. * file reads are performed for at least two bytes (required for the
  461. * translate_eol option).
  462. */
  463. var zip_fill_window = function() {
  464. var n, m;
  465. // Amount of free space at the end of the window.
  466. var more = zip_window_size - zip_lookahead - zip_strstart;
  467. /* If the window is almost full and there is insufficient lookahead,
  468. * move the upper half to the lower one to make room in the upper half.
  469. */
  470. if(more == -1) {
  471. /* Very unlikely, but possible on 16 bit machine if strstart == 0
  472. * and lookahead == 1 (input done one byte at time)
  473. */
  474. more--;
  475. } else if(zip_strstart >= zip_WSIZE + zip_MAX_DIST) {
  476. /* By the IN assertion, the window is not empty so we can't confuse
  477. * more == 0 with more == 64K on a 16 bit machine.
  478. */
  479. // Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
  480. // System.arraycopy(window, WSIZE, window, 0, WSIZE);
  481. for(n = 0; n < zip_WSIZE; n++)
  482. zip_window[n] = zip_window[n + zip_WSIZE];
  483. zip_match_start -= zip_WSIZE;
  484. zip_strstart -= zip_WSIZE; /* we now have strstart >= MAX_DIST: */
  485. zip_block_start -= zip_WSIZE;
  486. for(n = 0; n < zip_HASH_SIZE; n++) {
  487. m = zip_head1(n);
  488. zip_head2(n, m >= zip_WSIZE ? m - zip_WSIZE : zip_NIL);
  489. }
  490. for(n = 0; n < zip_WSIZE; n++) {
  491. /* If n is not on any hash chain, prev[n] is garbage but
  492. * its value will never be used.
  493. */
  494. m = zip_prev[n];
  495. zip_prev[n] = (m >= zip_WSIZE ? m - zip_WSIZE : zip_NIL);
  496. }
  497. more += zip_WSIZE;
  498. }
  499. // At this point, more >= 2
  500. if(!zip_eofile) {
  501. n = zip_read_buff(zip_window, zip_strstart + zip_lookahead, more);
  502. if(n <= 0)
  503. zip_eofile = true;
  504. else
  505. zip_lookahead += n;
  506. }
  507. }
  508. /* ==========================================================================
  509. * Processes a new input file and return its compressed length. This
  510. * function does not perform lazy evaluationof matches and inserts
  511. * new strings in the dictionary only for unmatched strings or for short
  512. * matches. It is used only for the fast compression options.
  513. */
  514. var zip_deflate_fast = function() {
  515. while(zip_lookahead != 0 && zip_qhead == null) {
  516. var flush; // set if current block must be flushed
  517. /* Insert the string window[strstart .. strstart+2] in the
  518. * dictionary, and set hash_head to the head of the hash chain:
  519. */
  520. zip_INSERT_STRING();
  521. /* Find the longest match, discarding those <= prev_length.
  522. * At this point we have always match_length < MIN_MATCH
  523. */
  524. if(zip_hash_head != zip_NIL &&
  525. zip_strstart - zip_hash_head <= zip_MAX_DIST) {
  526. /* To simplify the code, we prevent matches with the string
  527. * of window index 0 (in particular we have to avoid a match
  528. * of the string with itself at the start of the input file).
  529. */
  530. zip_match_length = zip_longest_match(zip_hash_head);
  531. /* longest_match() sets match_start */
  532. if(zip_match_length > zip_lookahead)
  533. zip_match_length = zip_lookahead;
  534. }
  535. if(zip_match_length >= zip_MIN_MATCH) {
  536. // check_match(strstart, match_start, match_length);
  537. flush = zip_ct_tally(zip_strstart - zip_match_start,
  538. zip_match_length - zip_MIN_MATCH);
  539. zip_lookahead -= zip_match_length;
  540. /* Insert new strings in the hash table only if the match length
  541. * is not too large. This saves time but degrades compression.
  542. */
  543. if(zip_match_length <= zip_max_lazy_match) {
  544. zip_match_length--; // string at strstart already in hash table
  545. do {
  546. zip_strstart++;
  547. zip_INSERT_STRING();
  548. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  549. * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  550. * these bytes are garbage, but it does not matter since
  551. * the next lookahead bytes will be emitted as literals.
  552. */
  553. } while(--zip_match_length != 0);
  554. zip_strstart++;
  555. } else {
  556. zip_strstart += zip_match_length;
  557. zip_match_length = 0;
  558. zip_ins_h = zip_window[zip_strstart] & 0xff;
  559. // UPDATE_HASH(ins_h, window[strstart + 1]);
  560. zip_ins_h = ((zip_ins_h<<zip_H_SHIFT) ^ (zip_window[zip_strstart + 1] & 0xff)) & zip_HASH_MASK;
  561. //#if MIN_MATCH != 3
  562. // Call UPDATE_HASH() MIN_MATCH-3 more times
  563. //#endif
  564. }
  565. } else {
  566. /* No match, output a literal byte */
  567. flush = zip_ct_tally(0, zip_window[zip_strstart] & 0xff);
  568. zip_lookahead--;
  569. zip_strstart++;
  570. }
  571. if(flush) {
  572. zip_flush_block(0);
  573. zip_block_start = zip_strstart;
  574. }
  575. /* Make sure that we always have enough lookahead, except
  576. * at the end of the input file. We need MAX_MATCH bytes
  577. * for the next match, plus MIN_MATCH bytes to insert the
  578. * string following the next match.
  579. */
  580. while(zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile)
  581. zip_fill_window();
  582. }
  583. }
  584. var zip_deflate_better = function() {
  585. /* Process the input block. */
  586. while(zip_lookahead != 0 && zip_qhead == null) {
  587. /* Insert the string window[strstart .. strstart+2] in the
  588. * dictionary, and set hash_head to the head of the hash chain:
  589. */
  590. zip_INSERT_STRING();
  591. /* Find the longest match, discarding those <= prev_length.
  592. */
  593. zip_prev_length = zip_match_length;
  594. zip_prev_match = zip_match_start;
  595. zip_match_length = zip_MIN_MATCH - 1;
  596. if(zip_hash_head != zip_NIL &&
  597. zip_prev_length < zip_max_lazy_match &&
  598. zip_strstart - zip_hash_head <= zip_MAX_DIST) {
  599. /* To simplify the code, we prevent matches with the string
  600. * of window index 0 (in particular we have to avoid a match
  601. * of the string with itself at the start of the input file).
  602. */
  603. zip_match_length = zip_longest_match(zip_hash_head);
  604. /* longest_match() sets match_start */
  605. if(zip_match_length > zip_lookahead)
  606. zip_match_length = zip_lookahead;
  607. /* Ignore a length 3 match if it is too distant: */
  608. if(zip_match_length == zip_MIN_MATCH &&
  609. zip_strstart - zip_match_start > zip_TOO_FAR) {
  610. /* If prev_match is also MIN_MATCH, match_start is garbage
  611. * but we will ignore the current match anyway.
  612. */
  613. zip_match_length--;
  614. }
  615. }
  616. /* If there was a match at the previous step and the current
  617. * match is not better, output the previous match:
  618. */
  619. if(zip_prev_length >= zip_MIN_MATCH &&
  620. zip_match_length <= zip_prev_length) {
  621. var flush; // set if current block must be flushed
  622. // check_match(strstart - 1, prev_match, prev_length);
  623. flush = zip_ct_tally(zip_strstart - 1 - zip_prev_match,
  624. zip_prev_length - zip_MIN_MATCH);
  625. /* Insert in hash table all strings up to the end of the match.
  626. * strstart-1 and strstart are already inserted.
  627. */
  628. zip_lookahead -= zip_prev_length - 1;
  629. zip_prev_length -= 2;
  630. do {
  631. zip_strstart++;
  632. zip_INSERT_STRING();
  633. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  634. * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  635. * these bytes are garbage, but it does not matter since the
  636. * next lookahead bytes will always be emitted as literals.
  637. */
  638. } while(--zip_prev_length != 0);
  639. zip_match_available = 0;
  640. zip_match_length = zip_MIN_MATCH - 1;
  641. zip_strstart++;
  642. if(flush) {
  643. zip_flush_block(0);
  644. zip_block_start = zip_strstart;
  645. }
  646. } else if(zip_match_available != 0) {
  647. /* If there was no match at the previous position, output a
  648. * single literal. If there was a match but the current match
  649. * is longer, truncate the previous match to a single literal.
  650. */
  651. if(zip_ct_tally(0, zip_window[zip_strstart - 1] & 0xff)) {
  652. zip_flush_block(0);
  653. zip_block_start = zip_strstart;
  654. }
  655. zip_strstart++;
  656. zip_lookahead--;
  657. } else {
  658. /* There is no previous match to compare with, wait for
  659. * the next step to decide.
  660. */
  661. zip_match_available = 1;
  662. zip_strstart++;
  663. zip_lookahead--;
  664. }
  665. /* Make sure that we always have enough lookahead, except
  666. * at the end of the input file. We need MAX_MATCH bytes
  667. * for the next match, plus MIN_MATCH bytes to insert the
  668. * string following the next match.
  669. */
  670. while(zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile)
  671. zip_fill_window();
  672. }
  673. }
  674. var zip_init_deflate = function() {
  675. if(zip_eofile)
  676. return;
  677. zip_bi_buf = 0;
  678. zip_bi_valid = 0;
  679. zip_ct_init();
  680. zip_lm_init();
  681. zip_qhead = null;
  682. zip_outcnt = 0;
  683. zip_outoff = 0;
  684. zip_match_available = 0;
  685. if(zip_compr_level <= 3)
  686. {
  687. zip_prev_length = zip_MIN_MATCH - 1;
  688. zip_match_length = 0;
  689. }
  690. else
  691. {
  692. zip_match_length = zip_MIN_MATCH - 1;
  693. zip_match_available = 0;
  694. zip_match_available = 0;
  695. }
  696. zip_complete = false;
  697. }
  698. /* ==========================================================================
  699. * Same as above, but achieves better compression. We use a lazy
  700. * evaluation for matches: a match is finally adopted only if there is
  701. * no better match at the next window position.
  702. */
  703. var zip_deflate_internal = function(buff, off, buff_size) {
  704. var n;
  705. if(!zip_initflag)
  706. {
  707. zip_init_deflate();
  708. zip_initflag = true;
  709. if(zip_lookahead == 0) { // empty
  710. zip_complete = true;
  711. return 0;
  712. }
  713. }
  714. if((n = zip_qcopy(buff, off, buff_size)) == buff_size)
  715. return buff_size;
  716. if(zip_complete)
  717. return n;
  718. if(zip_compr_level <= 3) // optimized for speed
  719. zip_deflate_fast();
  720. else
  721. zip_deflate_better();
  722. if(zip_lookahead == 0) {
  723. if(zip_match_available != 0)
  724. zip_ct_tally(0, zip_window[zip_strstart - 1] & 0xff);
  725. zip_flush_block(1);
  726. zip_complete = true;
  727. }
  728. return n + zip_qcopy(buff, n + off, buff_size - n);
  729. }
  730. var zip_qcopy = function(buff, off, buff_size) {
  731. var n, i, j;
  732. n = 0;
  733. while(zip_qhead != null && n < buff_size)
  734. {
  735. i = buff_size - n;
  736. if(i > zip_qhead.len)
  737. i = zip_qhead.len;
  738. // System.arraycopy(qhead.ptr, qhead.off, buff, off + n, i);
  739. for(j = 0; j < i; j++)
  740. buff[off + n + j] = zip_qhead.ptr[zip_qhead.off + j];
  741. zip_qhead.off += i;
  742. zip_qhead.len -= i;
  743. n += i;
  744. if(zip_qhead.len == 0) {
  745. var p;
  746. p = zip_qhead;
  747. zip_qhead = zip_qhead.next;
  748. zip_reuse_queue(p);
  749. }
  750. }
  751. if(n == buff_size)
  752. return n;
  753. if(zip_outoff < zip_outcnt) {
  754. i = buff_size - n;
  755. if(i > zip_outcnt - zip_outoff)
  756. i = zip_outcnt - zip_outoff;
  757. // System.arraycopy(outbuf, outoff, buff, off + n, i);
  758. for(j = 0; j < i; j++)
  759. buff[off + n + j] = zip_outbuf[zip_outoff + j];
  760. zip_outoff += i;
  761. n += i;
  762. if(zip_outcnt == zip_outoff)
  763. zip_outcnt = zip_outoff = 0;
  764. }
  765. return n;
  766. }
  767. /* ==========================================================================
  768. * Allocate the match buffer, initialize the various tables and save the
  769. * location of the internal file attribute (ascii/binary) and method
  770. * (DEFLATE/STORE).
  771. */
  772. var zip_ct_init = function() {
  773. var n; // iterates over tree elements
  774. var bits; // bit counter
  775. var length; // length value
  776. var code; // code value
  777. var dist; // distance index
  778. if(zip_static_dtree[0].dl != 0) return; // ct_init already called
  779. zip_l_desc.dyn_tree = zip_dyn_ltree;
  780. zip_l_desc.static_tree = zip_static_ltree;
  781. zip_l_desc.extra_bits = zip_extra_lbits;
  782. zip_l_desc.extra_base = zip_LITERALS + 1;
  783. zip_l_desc.elems = zip_L_CODES;
  784. zip_l_desc.max_length = zip_MAX_BITS;
  785. zip_l_desc.max_code = 0;
  786. zip_d_desc.dyn_tree = zip_dyn_dtree;
  787. zip_d_desc.static_tree = zip_static_dtree;
  788. zip_d_desc.extra_bits = zip_extra_dbits;
  789. zip_d_desc.extra_base = 0;
  790. zip_d_desc.elems = zip_D_CODES;
  791. zip_d_desc.max_length = zip_MAX_BITS;
  792. zip_d_desc.max_code = 0;
  793. zip_bl_desc.dyn_tree = zip_bl_tree;
  794. zip_bl_desc.static_tree = null;
  795. zip_bl_desc.extra_bits = zip_extra_blbits;
  796. zip_bl_desc.extra_base = 0;
  797. zip_bl_desc.elems = zip_BL_CODES;
  798. zip_bl_desc.max_length = zip_MAX_BL_BITS;
  799. zip_bl_desc.max_code = 0;
  800. // Initialize the mapping length (0..255) -> length code (0..28)
  801. length = 0;
  802. for(code = 0; code < zip_LENGTH_CODES-1; code++) {
  803. zip_base_length[code] = length;
  804. for(n = 0; n < (1<<zip_extra_lbits[code]); n++)
  805. zip_length_code[length++] = code;
  806. }
  807. // Assert (length == 256, "ct_init: length != 256");
  808. /* Note that the length 255 (match length 258) can be represented
  809. * in two different ways: code 284 + 5 bits or code 285, so we
  810. * overwrite length_code[255] to use the best encoding:
  811. */
  812. zip_length_code[length-1] = code;
  813. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  814. dist = 0;
  815. for(code = 0 ; code < 16; code++) {
  816. zip_base_dist[code] = dist;
  817. for(n = 0; n < (1<<zip_extra_dbits[code]); n++) {
  818. zip_dist_code[dist++] = code;
  819. }
  820. }
  821. // Assert (dist == 256, "ct_init: dist != 256");
  822. dist >>= 7; // from now on, all distances are divided by 128
  823. for( ; code < zip_D_CODES; code++) {
  824. zip_base_dist[code] = dist << 7;
  825. for(n = 0; n < (1<<(zip_extra_dbits[code]-7)); n++)
  826. zip_dist_code[256 + dist++] = code;
  827. }
  828. // Assert (dist == 256, "ct_init: 256+dist != 512");
  829. // Construct the codes of the static literal tree
  830. for(bits = 0; bits <= zip_MAX_BITS; bits++)
  831. zip_bl_count[bits] = 0;
  832. n = 0;
  833. while(n <= 143) { zip_static_ltree[n++].dl = 8; zip_bl_count[8]++; }
  834. while(n <= 255) { zip_static_ltree[n++].dl = 9; zip_bl_count[9]++; }
  835. while(n <= 279) { zip_static_ltree[n++].dl = 7; zip_bl_count[7]++; }
  836. while(n <= 287) { zip_static_ltree[n++].dl = 8; zip_bl_count[8]++; }
  837. /* Codes 286 and 287 do not exist, but we must include them in the
  838. * tree construction to get a canonical Huffman tree (longest code
  839. * all ones)
  840. */
  841. zip_gen_codes(zip_static_ltree, zip_L_CODES + 1);
  842. /* The static distance tree is trivial: */
  843. for(n = 0; n < zip_D_CODES; n++) {
  844. zip_static_dtree[n].dl = 5;
  845. zip_static_dtree[n].fc = zip_bi_reverse(n, 5);
  846. }
  847. // Initialize the first block of the first file:
  848. zip_init_block();
  849. }
  850. /* ==========================================================================
  851. * Initialize a new block.
  852. */
  853. var zip_init_block = function() {
  854. var n; // iterates over tree elements
  855. // Initialize the trees.
  856. for(n = 0; n < zip_L_CODES; n++) zip_dyn_ltree[n].fc = 0;
  857. for(n = 0; n < zip_D_CODES; n++) zip_dyn_dtree[n].fc = 0;
  858. for(n = 0; n < zip_BL_CODES; n++) zip_bl_tree[n].fc = 0;
  859. zip_dyn_ltree[zip_END_BLOCK].fc = 1;
  860. zip_opt_len = zip_static_len = 0;
  861. zip_last_lit = zip_last_dist = zip_last_flags = 0;
  862. zip_flags = 0;
  863. zip_flag_bit = 1;
  864. }
  865. /* ==========================================================================
  866. * Restore the heap property by moving down the tree starting at node k,
  867. * exchanging a node with the smallest of its two sons if necessary, stopping
  868. * when the heap property is re-established (each father smaller than its
  869. * two sons).
  870. */
  871. var zip_pqdownheap = function(
  872. tree, // the tree to restore
  873. k) { // node to move down
  874. var v = zip_heap[k];
  875. var j = k << 1; // left son of k
  876. while(j <= zip_heap_len) {
  877. // Set j to the smallest of the two sons:
  878. if(j < zip_heap_len &&
  879. zip_SMALLER(tree, zip_heap[j + 1], zip_heap[j]))
  880. j++;
  881. // Exit if v is smaller than both sons
  882. if(zip_SMALLER(tree, v, zip_heap[j]))
  883. break;
  884. // Exchange v with the smallest son
  885. zip_heap[k] = zip_heap[j];
  886. k = j;
  887. // And continue down the tree, setting j to the left son of k
  888. j <<= 1;
  889. }
  890. zip_heap[k] = v;
  891. }
  892. /* ==========================================================================
  893. * Compute the optimal bit lengths for a tree and update the total bit length
  894. * for the current block.
  895. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  896. * above are the tree nodes sorted by increasing frequency.
  897. * OUT assertions: the field len is set to the optimal bit length, the
  898. * array bl_count contains the frequencies for each bit length.
  899. * The length opt_len is updated; static_len is also updated if stree is
  900. * not null.
  901. */
  902. var zip_gen_bitlen = function(desc) { // the tree descriptor
  903. var tree = desc.dyn_tree;
  904. var extra = desc.extra_bits;
  905. var base = desc.extra_base;
  906. var max_code = desc.max_code;
  907. var max_length = desc.max_length;
  908. var stree = desc.static_tree;
  909. var h; // heap index
  910. var n, m; // iterate over the tree elements
  911. var bits; // bit length
  912. var xbits; // extra bits
  913. var f; // frequency
  914. var overflow = 0; // number of elements with bit length too large
  915. for(bits = 0; bits <= zip_MAX_BITS; bits++)
  916. zip_bl_count[bits] = 0;
  917. /* In a first pass, compute the optimal bit lengths (which may
  918. * overflow in the case of the bit length tree).
  919. */
  920. tree[zip_heap[zip_heap_max]].dl = 0; // root of the heap
  921. for(h = zip_heap_max + 1; h < zip_HEAP_SIZE; h++) {
  922. n = zip_heap[h];
  923. bits = tree[tree[n].dl].dl + 1;
  924. if(bits > max_length) {
  925. bits = max_length;
  926. overflow++;
  927. }
  928. tree[n].dl = bits;
  929. // We overwrite tree[n].dl which is no longer needed
  930. if(n > max_code)
  931. continue; // not a leaf node
  932. zip_bl_count[bits]++;
  933. xbits = 0;
  934. if(n >= base)
  935. xbits = extra[n - base];
  936. f = tree[n].fc;
  937. zip_opt_len += f * (bits + xbits);
  938. if(stree != null)
  939. zip_static_len += f * (stree[n].dl + xbits);
  940. }
  941. if(overflow == 0)
  942. return;
  943. // This happens for example on obj2 and pic of the Calgary corpus
  944. // Find the first bit length which could increase:
  945. do {
  946. bits = max_length - 1;
  947. while(zip_bl_count[bits] == 0)
  948. bits--;
  949. zip_bl_count[bits]--; // move one leaf down the tree
  950. zip_bl_count[bits + 1] += 2; // move one overflow item as its brother
  951. zip_bl_count[max_length]--;
  952. /* The brother of the overflow item also moves one step up,
  953. * but this does not affect bl_count[max_length]
  954. */
  955. overflow -= 2;
  956. } while(overflow > 0);
  957. /* Now recompute all bit lengths, scanning in increasing frequency.
  958. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  959. * lengths instead of fixing only the wrong ones. This idea is taken
  960. * from 'ar' written by Haruhiko Okumura.)
  961. */
  962. for(bits = max_length; bits != 0; bits--) {
  963. n = zip_bl_count[bits];
  964. while(n != 0) {
  965. m = zip_heap[--h];
  966. if(m > max_code)
  967. continue;
  968. if(tree[m].dl != bits) {
  969. zip_opt_len += (bits - tree[m].dl) * tree[m].fc;
  970. tree[m].fc = bits;
  971. }
  972. n--;
  973. }
  974. }
  975. }
  976. /* ==========================================================================
  977. * Generate the codes for a given tree and bit counts (which need not be
  978. * optimal).
  979. * IN assertion: the array bl_count contains the bit length statistics for
  980. * the given tree and the field len is set for all tree elements.
  981. * OUT assertion: the field code is set for all tree elements of non
  982. * zero code length.
  983. */
  984. var zip_gen_codes = function(tree, // the tree to decorate
  985. max_code) { // largest code with non zero frequency
  986. var next_code = new Array(zip_MAX_BITS+1); // next code value for each bit length
  987. var code = 0; // running code value
  988. var bits; // bit index
  989. var n; // code index
  990. /* The distribution counts are first used to generate the code values
  991. * without bit reversal.
  992. */
  993. for(bits = 1; bits <= zip_MAX_BITS; bits++) {
  994. code = ((code + zip_bl_count[bits-1]) << 1);
  995. next_code[bits] = code;
  996. }
  997. /* Check that the bit counts in bl_count are consistent. The last code
  998. * must be all ones.
  999. */
  1000. // Assert (code + encoder->bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  1001. // "inconsistent bit counts");
  1002. // Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  1003. for(n = 0; n <= max_code; n++) {
  1004. var len = tree[n].dl;
  1005. if(len == 0)
  1006. continue;
  1007. // Now reverse the bits
  1008. tree[n].fc = zip_bi_reverse(next_code[len]++, len);
  1009. // Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  1010. // n, (isgraph(n) ? n : ' '), len, tree[n].fc, next_code[len]-1));
  1011. }
  1012. }
  1013. /* ==========================================================================
  1014. * Construct one Huffman tree and assigns the code bit strings and lengths.
  1015. * Update the total bit length for the current block.
  1016. * IN assertion: the field freq is set for all tree elements.
  1017. * OUT assertions: the fields len and code are set to the optimal bit length
  1018. * and corresponding code. The length opt_len is updated; static_len is
  1019. * also updated if stree is not null. The field max_code is set.
  1020. */
  1021. var zip_build_tree = function(desc) { // the tree descriptor
  1022. var tree = desc.dyn_tree;
  1023. var stree = desc.static_tree;
  1024. var elems = desc.elems;
  1025. var n, m; // iterate over heap elements
  1026. var max_code = -1; // largest code with non zero frequency
  1027. var node = elems; // next internal node of the tree
  1028. /* Construct the initial heap, with least frequent element in
  1029. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  1030. * heap[0] is not used.
  1031. */
  1032. zip_heap_len = 0;
  1033. zip_heap_max = zip_HEAP_SIZE;
  1034. for(n = 0; n < elems; n++) {
  1035. if(tree[n].fc != 0) {
  1036. zip_heap[++zip_heap_len] = max_code = n;
  1037. zip_depth[n] = 0;
  1038. } else
  1039. tree[n].dl = 0;
  1040. }
  1041. /* The pkzip format requires that at least one distance code exists,
  1042. * and that at least one bit should be sent even if there is only one
  1043. * possible code. So to avoid special checks later on we force at least
  1044. * two codes of non zero frequency.
  1045. */
  1046. while(zip_heap_len < 2) {
  1047. var xnew = zip_heap[++zip_heap_len] = (max_code < 2 ? ++max_code : 0);
  1048. tree[xnew].fc = 1;
  1049. zip_depth[xnew] = 0;
  1050. zip_opt_len--;
  1051. if(stree != null)
  1052. zip_static_len -= stree[xnew].dl;
  1053. // new is 0 or 1 so it does not have extra bits
  1054. }
  1055. desc.max_code = max_code;
  1056. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  1057. * establish sub-heaps of increasing lengths:
  1058. */
  1059. for(n = zip_heap_len >> 1; n >= 1; n--)
  1060. zip_pqdownheap(tree, n);
  1061. /* Construct the Huffman tree by repeatedly combining the least two
  1062. * frequent nodes.
  1063. */
  1064. do {
  1065. n = zip_heap[zip_SMALLEST];
  1066. zip_heap[zip_SMALLEST] = zip_heap[zip_heap_len--];
  1067. zip_pqdownheap(tree, zip_SMALLEST);
  1068. m = zip_heap[zip_SMALLEST]; // m = node of next least frequency
  1069. // keep the nodes sorted by frequency
  1070. zip_heap[--zip_heap_max] = n;
  1071. zip_heap[--zip_heap_max] = m;
  1072. // Create a new node father of n and m
  1073. tree[node].fc = tree[n].fc + tree[m].fc;
  1074. // depth[node] = (char)(MAX(depth[n], depth[m]) + 1);
  1075. if(zip_depth[n] > zip_depth[m] + 1)
  1076. zip_depth[node] = zip_depth[n];
  1077. else
  1078. zip_depth[node] = zip_depth[m] + 1;
  1079. tree[n].dl = tree[m].dl = node;
  1080. // and insert the new node in the heap
  1081. zip_heap[zip_SMALLEST] = node++;
  1082. zip_pqdownheap(tree, zip_SMALLEST);
  1083. } while(zip_heap_len >= 2);
  1084. zip_heap[--zip_heap_max] = zip_heap[zip_SMALLEST];
  1085. /* At this point, the fields freq and dad are set. We can now
  1086. * generate the bit lengths.
  1087. */
  1088. zip_gen_bitlen(desc);
  1089. // The field len is now set, we can generate the bit codes
  1090. zip_gen_codes(tree, max_code);
  1091. }
  1092. /* ==========================================================================
  1093. * Scan a literal or distance tree to determine the frequencies of the codes
  1094. * in the bit length tree. Updates opt_len to take into account the repeat
  1095. * counts. (The contribution of the bit length codes will be added later
  1096. * during the construction of bl_tree.)
  1097. */
  1098. var zip_scan_tree = function(tree,// the tree to be scanned
  1099. max_code) { // and its largest code of non zero frequency
  1100. var n; // iterates over all tree elements
  1101. var prevlen = -1; // last emitted length
  1102. var curlen; // length of current code
  1103. var nextlen = tree[0].dl; // length of next code
  1104. var count = 0; // repeat count of the current code
  1105. var max_count = 7; // max repeat count
  1106. var min_count = 4; // min repeat count
  1107. if(nextlen == 0) {
  1108. max_count = 138;
  1109. min_count = 3;
  1110. }
  1111. tree[max_code + 1].dl = 0xffff; // guard
  1112. for(n = 0; n <= max_code; n++) {
  1113. curlen = nextlen;
  1114. nextlen = tree[n + 1].dl;
  1115. if(++count < max_count && curlen == nextlen)
  1116. continue;
  1117. else if(count < min_count)
  1118. zip_bl_tree[curlen].fc += count;
  1119. else if(curlen != 0) {
  1120. if(curlen != prevlen)
  1121. zip_bl_tree[curlen].fc++;
  1122. zip_bl_tree[zip_REP_3_6].fc++;
  1123. } else if(count <= 10)
  1124. zip_bl_tree[zip_REPZ_3_10].fc++;
  1125. else
  1126. zip_bl_tree[zip_REPZ_11_138].fc++;
  1127. count = 0; prevlen = curlen;
  1128. if(nextlen == 0) {
  1129. max_count = 138;
  1130. min_count = 3;
  1131. } else if(curlen == nextlen) {
  1132. max_count = 6;
  1133. min_count = 3;
  1134. } else {
  1135. max_count = 7;
  1136. min_count = 4;
  1137. }
  1138. }
  1139. }
  1140. /* ==========================================================================
  1141. * Send a literal or distance tree in compressed form, using the codes in
  1142. * bl_tree.
  1143. */
  1144. var zip_send_tree = function(tree, // the tree to be scanned
  1145. max_code) { // and its largest code of non zero frequency
  1146. var n; // iterates over all tree elements
  1147. var prevlen = -1; // last emitted length
  1148. var curlen; // length of current code
  1149. var nextlen = tree[0].dl; // length of next code
  1150. var count = 0; // repeat count of the current code
  1151. var max_count = 7; // max repeat count
  1152. var min_count = 4; // min repeat count
  1153. /* tree[max_code+1].dl = -1; */ /* guard already set */
  1154. if(nextlen == 0) {
  1155. max_count = 138;
  1156. min_count = 3;
  1157. }
  1158. for(n = 0; n <= max_code; n++) {
  1159. curlen = nextlen;
  1160. nextlen = tree[n+1].dl;
  1161. if(++count < max_count && curlen == nextlen) {
  1162. continue;
  1163. } else if(count < min_count) {
  1164. do { zip_SEND_CODE(curlen, zip_bl_tree); } while(--count != 0);
  1165. } else if(curlen != 0) {
  1166. if(curlen != prevlen) {
  1167. zip_SEND_CODE(curlen, zip_bl_tree);
  1168. count--;
  1169. }
  1170. // Assert(count >= 3 && count <= 6, " 3_6?");
  1171. zip_SEND_CODE(zip_REP_3_6, zip_bl_tree);
  1172. zip_send_bits(count - 3, 2);
  1173. } else if(count <= 10) {
  1174. zip_SEND_CODE(zip_REPZ_3_10, zip_bl_tree);
  1175. zip_send_bits(count-3, 3);
  1176. } else {
  1177. zip_SEND_CODE(zip_REPZ_11_138, zip_bl_tree);
  1178. zip_send_bits(count-11, 7);
  1179. }
  1180. count = 0;
  1181. prevlen = curlen;
  1182. if(nextlen == 0) {
  1183. max_count = 138;
  1184. min_count = 3;
  1185. } else if(curlen == nextlen) {
  1186. max_count = 6;
  1187. min_count = 3;
  1188. } else {
  1189. max_count = 7;
  1190. min_count = 4;
  1191. }
  1192. }
  1193. }
  1194. /* ==========================================================================
  1195. * Construct the Huffman tree for the bit lengths and return the index in
  1196. * bl_order of the last bit length code to send.
  1197. */
  1198. var zip_build_bl_tree = function() {
  1199. var max_blindex; // index of last bit length code of non zero freq
  1200. // Determine the bit length frequencies for literal and distance trees
  1201. zip_scan_tree(zip_dyn_ltree, zip_l_desc.max_code);
  1202. zip_scan_tree(zip_dyn_dtree, zip_d_desc.max_code);
  1203. // Build the bit length tree:
  1204. zip_build_tree(zip_bl_desc);
  1205. /* opt_len now includes the length of the tree representations, except
  1206. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  1207. */
  1208. /* Determine the number of bit length codes to send. The pkzip format
  1209. * requires that at least 4 bit length codes be sent. (appnote.txt says
  1210. * 3 but the actual value used is 4.)
  1211. */
  1212. for(max_blindex = zip_BL_CODES-1; max_blindex >= 3; max_blindex--) {
  1213. if(zip_bl_tree[zip_bl_order[max_blindex]].dl != 0) break;
  1214. }
  1215. /* Update opt_len to include the bit length tree and counts */
  1216. zip_opt_len += 3*(max_blindex+1) + 5+5+4;
  1217. // Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  1218. // encoder->opt_len, encoder->static_len));
  1219. return max_blindex;
  1220. }
  1221. /* ==========================================================================
  1222. * Send the header for a block using dynamic Huffman trees: the counts, the
  1223. * lengths of the bit length codes, the literal tree and the distance tree.
  1224. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  1225. */
  1226. var zip_send_all_trees = function(lcodes, dcodes, blcodes) { // number of codes for each tree
  1227. var rank; // index in bl_order
  1228. // Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  1229. // Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  1230. // "too many codes");
  1231. // Tracev((stderr, "\nbl counts: "));
  1232. zip_send_bits(lcodes-257, 5); // not +255 as stated in appnote.txt
  1233. zip_send_bits(dcodes-1, 5);
  1234. zip_send_bits(blcodes-4, 4); // not -3 as stated in appnote.txt
  1235. for(rank = 0; rank < blcodes; rank++) {
  1236. // Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  1237. zip_send_bits(zip_bl_tree[zip_bl_order[rank]].dl, 3);
  1238. }
  1239. // send the literal tree
  1240. zip_send_tree(zip_dyn_ltree,lcodes-1);
  1241. // send the distance tree
  1242. zip_send_tree(zip_dyn_dtree,dcodes-1);
  1243. }
  1244. /* ==========================================================================
  1245. * Determine the best encoding for the current block: dynamic trees, static
  1246. * trees or store, and output the encoded block to the zip file.
  1247. */
  1248. var zip_flush_block = function(eof) { // true if this is the last block for a file
  1249. var opt_lenb, static_lenb; // opt_len and static_len in bytes
  1250. var max_blindex; // index of last bit length code of non zero freq
  1251. var stored_len; // length of input block
  1252. stored_len = zip_strstart - zip_block_start;
  1253. zip_flag_buf[zip_last_flags] = zip_flags; // Save the flags for the last 8 items
  1254. // Construct the literal and distance trees
  1255. zip_build_tree(zip_l_desc);
  1256. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld",
  1257. // encoder->opt_len, encoder->static_len));
  1258. zip_build_tree(zip_d_desc);
  1259. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld",
  1260. // encoder->opt_len, encoder->static_len));
  1261. /* At this point, opt_len and static_len are the total bit lengths of
  1262. * the compressed block data, excluding the tree representations.
  1263. */
  1264. /* Build the bit length tree for the above two trees, and get the index
  1265. * in bl_order of the last bit length code to send.
  1266. */
  1267. max_blindex = zip_build_bl_tree();
  1268. // Determine the best encoding. Compute first the block length in bytes
  1269. opt_lenb = (zip_opt_len +3+7)>>3;
  1270. static_lenb = (zip_static_len+3+7)>>3;
  1271. // Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
  1272. // opt_lenb, encoder->opt_len,
  1273. // static_lenb, encoder->static_len, stored_len,
  1274. // encoder->last_lit, encoder->last_dist));
  1275. if(static_lenb <= opt_lenb)
  1276. opt_lenb = static_lenb;
  1277. if(stored_len + 4 <= opt_lenb // 4: two words for the lengths
  1278. && zip_block_start >= 0) {
  1279. var i;
  1280. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1281. * Otherwise we can't have processed more than WSIZE input bytes since
  1282. * the last block flush, because compression would have been
  1283. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1284. * transform a block into a stored block.
  1285. */
  1286. zip_send_bits((zip_STORED_BLOCK<<1)+eof, 3); /* send block type */
  1287. zip_bi_windup(); /* align on byte boundary */
  1288. zip_put_short(stored_len);
  1289. zip_put_short(~stored_len);
  1290. // copy block
  1291. /*
  1292. p = &window[block_start];
  1293. for(i = 0; i < stored_len; i++)
  1294. put_byte(p[i]);
  1295. */
  1296. for(i = 0; i < stored_len; i++)
  1297. zip_put_byte(zip_window[zip_block_start + i]);
  1298. } else if(static_lenb == opt_lenb) {
  1299. zip_send_bits((zip_STATIC_TREES<<1)+eof, 3);
  1300. zip_compress_block(zip_static_ltree, zip_static_dtree);
  1301. } else {
  1302. zip_send_bits((zip_DYN_TREES<<1)+eof, 3);
  1303. zip_send_all_trees(zip_l_desc.max_code+1,
  1304. zip_d_desc.max_code+1,
  1305. max_blindex+1);
  1306. zip_compress_block(zip_dyn_ltree, zip_dyn_dtree);
  1307. }
  1308. zip_init_block();
  1309. if(eof != 0)
  1310. zip_bi_windup();
  1311. }
  1312. /* ==========================================================================
  1313. * Save the match info and tally the frequency counts. Return true if
  1314. * the current block must be flushed.
  1315. */
  1316. var zip_ct_tally = function(
  1317. dist, // distance of matched string
  1318. lc) { // match length-MIN_MATCH or unmatched char (if dist==0)
  1319. zip_l_buf[zip_last_lit++] = lc;
  1320. if(dist == 0) {
  1321. // lc is the unmatched char
  1322. zip_dyn_ltree[lc].fc++;
  1323. } else {
  1324. // Here, lc is the match length - MIN_MATCH
  1325. dist--; // dist = match distance - 1
  1326. // Assert((ush)dist < (ush)MAX_DIST &&
  1327. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  1328. // (ush)D_CODE(dist) < (ush)D_CODES, "ct_tally: bad match");
  1329. zip_dyn_ltree[zip_length_code[lc]+zip_LITERALS+1].fc++;
  1330. zip_dyn_dtree[zip_D_CODE(dist)].fc++;
  1331. zip_d_buf[zip_last_dist++] = dist;
  1332. zip_flags |= zip_flag_bit;
  1333. }
  1334. zip_flag_bit <<= 1;
  1335. // Output the flags if they fill a byte
  1336. if((zip_last_lit & 7) == 0) {
  1337. zip_flag_buf[zip_last_flags++] = zip_flags;
  1338. zip_flags = 0;
  1339. zip_flag_bit = 1;
  1340. }
  1341. // Try to guess if it is profitable to stop the current block here
  1342. if(zip_compr_level > 2 && (zip_last_lit & 0xfff) == 0) {
  1343. // Compute an upper bound for the compressed length
  1344. var out_length = zip_last_lit * 8;
  1345. var in_length = zip_strstart - zip_block_start;
  1346. var dcode;
  1347. for(dcode = 0; dcode < zip_D_CODES; dcode++) {
  1348. out_length += zip_dyn_dtree[dcode].fc * (5 + zip_extra_dbits[dcode]);
  1349. }
  1350. out_length >>= 3;
  1351. // Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
  1352. // encoder->last_lit, encoder->last_dist, in_length, out_length,
  1353. // 100L - out_length*100L/in_length));
  1354. if(zip_last_dist < parseInt(zip_last_lit/2) &&
  1355. out_length < parseInt(in_length/2))
  1356. return true;
  1357. }
  1358. return (zip_last_lit == zip_LIT_BUFSIZE-1 ||
  1359. zip_last_dist == zip_DIST_BUFSIZE);
  1360. /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
  1361. * on 16 bit machines and because stored blocks are restricted to
  1362. * 64K-1 bytes.
  1363. */
  1364. }
  1365. /* ==========================================================================
  1366. * Send the block data compressed using the given Huffman trees
  1367. */
  1368. var zip_compress_block = function(
  1369. ltree, // literal tree
  1370. dtree) { // distance tree
  1371. var dist; // distance of matched string
  1372. var lc; // match length or unmatched char (if dist == 0)
  1373. var lx = 0; // running index in l_buf
  1374. var dx = 0; // running index in d_buf
  1375. var fx = 0; // running index in flag_buf
  1376. var flag = 0; // current flags
  1377. var code; // the code to send
  1378. var extra; // number of extra bits to send
  1379. if(zip_last_lit != 0) do {
  1380. if((lx & 7) == 0)
  1381. flag = zip_flag_buf[fx++];
  1382. lc = zip_l_buf[lx++] & 0xff;
  1383. if((flag & 1) == 0) {
  1384. zip_SEND_CODE(lc, ltree); /* send a literal byte */
  1385. // Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  1386. } else {
  1387. // Here, lc is the match length - MIN_MATCH
  1388. code = zip_length_code[lc];
  1389. zip_SEND_CODE(code+zip_LITERALS+1, ltree); // send the length code
  1390. extra = zip_extra_lbits[code];
  1391. if(extra != 0) {
  1392. lc -= zip_base_length[code];
  1393. zip_send_bits(lc, extra); // send the extra length bits
  1394. }
  1395. dist = zip_d_buf[dx++];
  1396. // Here, dist is the match distance - 1
  1397. code = zip_D_CODE(dist);
  1398. // Assert (code < D_CODES, "bad d_code");
  1399. zip_SEND_CODE(code, dtree); // send the distance code
  1400. extra = zip_extra_dbits[code];
  1401. if(extra != 0) {
  1402. dist -= zip_base_dist[code];
  1403. zip_send_bits(dist, extra); // send the extra distance bits
  1404. }
  1405. } // literal or match pair ?
  1406. flag >>= 1;
  1407. } while(lx < zip_last_lit);
  1408. zip_SEND_CODE(zip_END_BLOCK, ltree);
  1409. }
  1410. /* ==========================================================================
  1411. * Send a value on a given number of bits.
  1412. * IN assertion: length <= 16 and value fits in length bits.
  1413. */
  1414. var zip_Buf_size = 16; // bit size of bi_buf
  1415. var zip_send_bits = function(
  1416. value, // value to send
  1417. length) { // number of bits
  1418. /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  1419. * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  1420. * unused bits in value.
  1421. */
  1422. if(zip_bi_valid > zip_Buf_size - length) {
  1423. zip_bi_buf |= (value << zip_bi_valid);
  1424. zip_put_short(zip_bi_buf);
  1425. zip_bi_buf = (value >> (zip_Buf_size - zip_bi_valid));
  1426. zip_bi_valid += length - zip_Buf_size;
  1427. } else {
  1428. zip_bi_buf |= value << zip_bi_valid;
  1429. zip_bi_valid += length;
  1430. }
  1431. }
  1432. /* ==========================================================================
  1433. * Reverse the first len bits of a code, using straightforward code (a faster
  1434. * method would use a table)
  1435. * IN assertion: 1 <= len <= 15
  1436. */
  1437. var zip_bi_reverse = function(
  1438. code, // the value to invert
  1439. len) { // its bit length
  1440. var res = 0;
  1441. do {
  1442. res |= code & 1;
  1443. code >>= 1;
  1444. res <<= 1;
  1445. } while(--len > 0);
  1446. return res >> 1;
  1447. }
  1448. /* ==========================================================================
  1449. * Write out any remaining bits in an incomplete byte.
  1450. */
  1451. var zip_bi_windup = function() {
  1452. if(zip_bi_valid > 8) {
  1453. zip_put_short(zip_bi_buf);
  1454. } else if(zip_bi_valid > 0) {
  1455. zip_put_byte(zip_bi_buf);
  1456. }
  1457. zip_bi_buf = 0;
  1458. zip_bi_valid = 0;
  1459. }
  1460. var zip_qoutbuf = function() {
  1461. if(zip_outcnt != 0) {
  1462. var q, i;
  1463. q = zip_new_queue();
  1464. if(zip_qhead == null)
  1465. zip_qhead = zip_qtail = q;
  1466. else
  1467. zip_qtail = zip_qtail.next = q;
  1468. q.len = zip_outcnt - zip_outoff;
  1469. // System.arraycopy(zip_outbuf, zip_outoff, q.ptr, 0, q.len);
  1470. for(i = 0; i < q.len; i++)
  1471. q.ptr[i] = zip_outbuf[zip_outoff + i];
  1472. zip_outcnt = zip_outoff = 0;
  1473. }
  1474. }
  1475. var zip_deflate = function(str, level) {
  1476. var i, j;
  1477. zip_deflate_data = str;
  1478. zip_deflate_pos = 0;
  1479. if(typeof level == "undefined")
  1480. level = zip_DEFAULT_LEVEL;
  1481. zip_deflate_start(level);
  1482. var buff = new Array(1024);
  1483. var aout = [];
  1484. while((i = zip_deflate_internal(buff, 0, buff.length)) > 0) {
  1485. var cbuf = new Array(i);
  1486. for(j = 0; j < i; j++){
  1487. cbuf[j] = String.fromCharCode(buff[j]);
  1488. }
  1489. aout[aout.length] = cbuf.join("");
  1490. }
  1491. zip_deflate_data = null; // G.C.
  1492. return aout.join("");
  1493. }
  1494. if (! ctx.RawDeflate) ctx.RawDeflate = {};
  1495. ctx.RawDeflate.deflate = zip_deflate;
  1496. })(this);