deflater.js 50 KB

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