rawdeflate.js 53 KB

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