選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1746 行
56 KiB

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