Advertisement
rg443

javascript deflate

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