Skip to content
Snippets Groups Projects
base64zip.js 305 KiB
Newer Older
Recolic's avatar
.
Recolic committed
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
                  }
                  /***/
            
            
                }
                /* Flush if we may have to slide, otherwise block_start may become
                 * negative and the data will be gone:
                 */
                if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
                  /*** FLUSH_BLOCK(s, 0); ***/
                  flush_block_only(s, false);
                  if (s.strm.avail_out === 0) {
                    return BS_NEED_MORE;
                  }
                  /***/
                }
              }
            
              s.insert = 0;
            
              if (flush === Z_FINISH) {
                /*** FLUSH_BLOCK(s, 1); ***/
                flush_block_only(s, true);
                if (s.strm.avail_out === 0) {
                  return BS_FINISH_STARTED;
                }
                /***/
                return BS_FINISH_DONE;
              }
            
              if (s.strstart > s.block_start) {
                /*** FLUSH_BLOCK(s, 0); ***/
                flush_block_only(s, false);
                if (s.strm.avail_out === 0) {
                  return BS_NEED_MORE;
                }
                /***/
              }
            
              return BS_NEED_MORE;
            }
            
            /* ===========================================================================
             * Compress as much as possible from the input stream, return the current
             * block state.
             * This function does not perform lazy evaluation of matches and inserts
             * new strings in the dictionary only for unmatched strings or for short
             * matches. It is used only for the fast compression options.
             */
            function deflate_fast(s, flush) {
              var hash_head;        /* head of the hash chain */
              var bflush;           /* set if current block must be flushed */
            
              for (;;) {
                /* Make sure that we always have enough lookahead, except
                 * at the end of the input file. We need MAX_MATCH bytes
                 * for the next match, plus MIN_MATCH bytes to insert the
                 * string following the next match.
                 */
                if (s.lookahead < MIN_LOOKAHEAD) {
                  fill_window(s);
                  if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
                    return BS_NEED_MORE;
                  }
                  if (s.lookahead === 0) {
                    break; /* flush the current block */
                  }
                }
            
                /* Insert the string window[strstart .. strstart+2] in the
                 * dictionary, and set hash_head to the head of the hash chain:
                 */
                hash_head = 0/*NIL*/;
                if (s.lookahead >= MIN_MATCH) {
                  /*** INSERT_STRING(s, s.strstart, hash_head); ***/
                  s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
                  hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
                  s.head[s.ins_h] = s.strstart;
                  /***/
                }
            
                /* Find the longest match, discarding those <= prev_length.
                 * At this point we have always match_length < MIN_MATCH
                 */
                if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
                  /* To simplify the code, we prevent matches with the string
                   * of window index 0 (in particular we have to avoid a match
                   * of the string with itself at the start of the input file).
                   */
                  s.match_length = longest_match(s, hash_head);
                  /* longest_match() sets match_start */
                }
                if (s.match_length >= MIN_MATCH) {
                  // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
            
                  /*** _tr_tally_dist(s, s.strstart - s.match_start,
                                 s.match_length - MIN_MATCH, bflush); ***/
                  bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
            
                  s.lookahead -= s.match_length;
            
                  /* Insert new strings in the hash table only if the match length
                   * is not too large. This saves time but degrades compression.
                   */
                  if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
                    s.match_length--; /* string at strstart already in table */
                    do {
                      s.strstart++;
                      /*** INSERT_STRING(s, s.strstart, hash_head); ***/
                      s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
                      hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
                      s.head[s.ins_h] = s.strstart;
                      /***/
                      /* strstart never exceeds WSIZE-MAX_MATCH, so there are
                       * always MIN_MATCH bytes ahead.
                       */
                    } while (--s.match_length !== 0);
                    s.strstart++;
                  } else
                  {
                    s.strstart += s.match_length;
                    s.match_length = 0;
                    s.ins_h = s.window[s.strstart];
                    /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
                    s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
            
            //#if MIN_MATCH != 3
            //                Call UPDATE_HASH() MIN_MATCH-3 more times
            //#endif
                    /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
                     * matter since it will be recomputed at next deflate call.
                     */
                  }
                } else {
                  /* No match, output a literal byte */
                  //Tracevv((stderr,"%c", s.window[s.strstart]));
                  /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
                  bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
            
                  s.lookahead--;
                  s.strstart++;
                }
                if (bflush) {
                  /*** FLUSH_BLOCK(s, 0); ***/
                  flush_block_only(s, false);
                  if (s.strm.avail_out === 0) {
                    return BS_NEED_MORE;
                  }
                  /***/
                }
              }
              s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
              if (flush === Z_FINISH) {
                /*** FLUSH_BLOCK(s, 1); ***/
                flush_block_only(s, true);
                if (s.strm.avail_out === 0) {
                  return BS_FINISH_STARTED;
                }
                /***/
                return BS_FINISH_DONE;
              }
              if (s.last_lit) {
                /*** FLUSH_BLOCK(s, 0); ***/
                flush_block_only(s, false);
                if (s.strm.avail_out === 0) {
                  return BS_NEED_MORE;
                }
                /***/
              }
              return BS_BLOCK_DONE;
            }
            
            /* ===========================================================================
             * Same as above, but achieves better compression. We use a lazy
             * evaluation for matches: a match is finally adopted only if there is
             * no better match at the next window position.
             */
            function deflate_slow(s, flush) {
              var hash_head;          /* head of hash chain */
              var bflush;              /* set if current block must be flushed */
            
              var max_insert;
            
              /* Process the input block. */
              for (;;) {
                /* Make sure that we always have enough lookahead, except
                 * at the end of the input file. We need MAX_MATCH bytes
                 * for the next match, plus MIN_MATCH bytes to insert the
                 * string following the next match.
                 */
                if (s.lookahead < MIN_LOOKAHEAD) {
                  fill_window(s);
                  if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
                    return BS_NEED_MORE;
                  }
                  if (s.lookahead === 0) { break; } /* flush the current block */
                }
            
                /* Insert the string window[strstart .. strstart+2] in the
                 * dictionary, and set hash_head to the head of the hash chain:
                 */
                hash_head = 0/*NIL*/;
                if (s.lookahead >= MIN_MATCH) {
                  /*** INSERT_STRING(s, s.strstart, hash_head); ***/
                  s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
                  hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
                  s.head[s.ins_h] = s.strstart;
                  /***/
                }
            
                /* Find the longest match, discarding those <= prev_length.
                 */
                s.prev_length = s.match_length;
                s.prev_match = s.match_start;
                s.match_length = MIN_MATCH - 1;
            
                if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
                    s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
                  /* To simplify the code, we prevent matches with the string
                   * of window index 0 (in particular we have to avoid a match
                   * of the string with itself at the start of the input file).
                   */
                  s.match_length = longest_match(s, hash_head);
                  /* longest_match() sets match_start */
            
                  if (s.match_length <= 5 &&
                     (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
            
                    /* If prev_match is also MIN_MATCH, match_start is garbage
                     * but we will ignore the current match anyway.
                     */
                    s.match_length = MIN_MATCH - 1;
                  }
                }
                /* If there was a match at the previous step and the current
                 * match is not better, output the previous match:
                 */
                if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
                  max_insert = s.strstart + s.lookahead - MIN_MATCH;
                  /* Do not insert strings in hash table beyond this. */
            
                  //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
            
                  /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
                                 s.prev_length - MIN_MATCH, bflush);***/
                  bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
                  /* Insert in hash table all strings up to the end of the match.
                   * strstart-1 and strstart are already inserted. If there is not
                   * enough lookahead, the last two strings are not inserted in
                   * the hash table.
                   */
                  s.lookahead -= s.prev_length - 1;
                  s.prev_length -= 2;
                  do {
                    if (++s.strstart <= max_insert) {
                      /*** INSERT_STRING(s, s.strstart, hash_head); ***/
                      s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
                      hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
                      s.head[s.ins_h] = s.strstart;
                      /***/
                    }
                  } while (--s.prev_length !== 0);
                  s.match_available = 0;
                  s.match_length = MIN_MATCH - 1;
                  s.strstart++;
            
                  if (bflush) {
                    /*** FLUSH_BLOCK(s, 0); ***/
                    flush_block_only(s, false);
                    if (s.strm.avail_out === 0) {
                      return BS_NEED_MORE;
                    }
                    /***/
                  }
            
                } else if (s.match_available) {
                  /* If there was no match at the previous position, output a
                   * single literal. If there was a match but the current match
                   * is longer, truncate the previous match to a single literal.
                   */
                  //Tracevv((stderr,"%c", s->window[s->strstart-1]));
                  /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
                  bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
            
                  if (bflush) {
                    /*** FLUSH_BLOCK_ONLY(s, 0) ***/
                    flush_block_only(s, false);
                    /***/
                  }
                  s.strstart++;
                  s.lookahead--;
                  if (s.strm.avail_out === 0) {
                    return BS_NEED_MORE;
                  }
                } else {
                  /* There is no previous match to compare with, wait for
                   * the next step to decide.
                   */
                  s.match_available = 1;
                  s.strstart++;
                  s.lookahead--;
                }
              }
              //Assert (flush != Z_NO_FLUSH, "no flush?");
              if (s.match_available) {
                //Tracevv((stderr,"%c", s->window[s->strstart-1]));
                /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
                bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
            
                s.match_available = 0;
              }
              s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
              if (flush === Z_FINISH) {
                /*** FLUSH_BLOCK(s, 1); ***/
                flush_block_only(s, true);
                if (s.strm.avail_out === 0) {
                  return BS_FINISH_STARTED;
                }
                /***/
                return BS_FINISH_DONE;
              }
              if (s.last_lit) {
                /*** FLUSH_BLOCK(s, 0); ***/
                flush_block_only(s, false);
                if (s.strm.avail_out === 0) {
                  return BS_NEED_MORE;
                }
                /***/
              }
            
              return BS_BLOCK_DONE;
            }
            
            
            /* ===========================================================================
             * For Z_RLE, simply look for runs of bytes, generate matches only of distance
             * one.  Do not maintain a hash table.  (It will be regenerated if this run of
             * deflate switches away from Z_RLE.)
             */
            function deflate_rle(s, flush) {
              var bflush;            /* set if current block must be flushed */
              var prev;              /* byte at distance one to match */
              var scan, strend;      /* scan goes up to strend for length of run */
            
              var _win = s.window;
            
              for (;;) {
                /* Make sure that we always have enough lookahead, except
                 * at the end of the input file. We need MAX_MATCH bytes
                 * for the longest run, plus one for the unrolled loop.
                 */
                if (s.lookahead <= MAX_MATCH) {
                  fill_window(s);
                  if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
                    return BS_NEED_MORE;
                  }
                  if (s.lookahead === 0) { break; } /* flush the current block */
                }
            
                /* See how many times the previous byte repeats */
                s.match_length = 0;
                if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
                  scan = s.strstart - 1;
                  prev = _win[scan];
                  if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
                    strend = s.strstart + MAX_MATCH;
                    do {
                      /*jshint noempty:false*/
                    } while (prev === _win[++scan] && prev === _win[++scan] &&
                             prev === _win[++scan] && prev === _win[++scan] &&
                             prev === _win[++scan] && prev === _win[++scan] &&
                             prev === _win[++scan] && prev === _win[++scan] &&
                             scan < strend);
                    s.match_length = MAX_MATCH - (strend - scan);
                    if (s.match_length > s.lookahead) {
                      s.match_length = s.lookahead;
                    }
                  }
                  //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
                }
            
                /* Emit match if have run of MIN_MATCH or longer, else emit literal */
                if (s.match_length >= MIN_MATCH) {
                  //check_match(s, s.strstart, s.strstart - 1, s.match_length);
            
                  /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
                  bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
            
                  s.lookahead -= s.match_length;
                  s.strstart += s.match_length;
                  s.match_length = 0;
                } else {
                  /* No match, output a literal byte */
                  //Tracevv((stderr,"%c", s->window[s->strstart]));
                  /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
                  bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
            
                  s.lookahead--;
                  s.strstart++;
                }
                if (bflush) {
                  /*** FLUSH_BLOCK(s, 0); ***/
                  flush_block_only(s, false);
                  if (s.strm.avail_out === 0) {
                    return BS_NEED_MORE;
                  }
                  /***/
                }
              }
              s.insert = 0;
              if (flush === Z_FINISH) {
                /*** FLUSH_BLOCK(s, 1); ***/
                flush_block_only(s, true);
                if (s.strm.avail_out === 0) {
                  return BS_FINISH_STARTED;
                }
                /***/
                return BS_FINISH_DONE;
              }
              if (s.last_lit) {
                /*** FLUSH_BLOCK(s, 0); ***/
                flush_block_only(s, false);
                if (s.strm.avail_out === 0) {
                  return BS_NEED_MORE;
                }
                /***/
              }
              return BS_BLOCK_DONE;
            }
            
            /* ===========================================================================
             * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
             * (It will be regenerated if this run of deflate switches away from Huffman.)
             */
            function deflate_huff(s, flush) {
              var bflush;             /* set if current block must be flushed */
            
              for (;;) {
                /* Make sure that we have a literal to write. */
                if (s.lookahead === 0) {
                  fill_window(s);
                  if (s.lookahead === 0) {
                    if (flush === Z_NO_FLUSH) {
                      return BS_NEED_MORE;
                    }
                    break;      /* flush the current block */
                  }
                }
            
                /* Output a literal byte */
                s.match_length = 0;
                //Tracevv((stderr,"%c", s->window[s->strstart]));
                /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
                bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
                s.lookahead--;
                s.strstart++;
                if (bflush) {
                  /*** FLUSH_BLOCK(s, 0); ***/
                  flush_block_only(s, false);
                  if (s.strm.avail_out === 0) {
                    return BS_NEED_MORE;
                  }
                  /***/
                }
              }
              s.insert = 0;
              if (flush === Z_FINISH) {
                /*** FLUSH_BLOCK(s, 1); ***/
                flush_block_only(s, true);
                if (s.strm.avail_out === 0) {
                  return BS_FINISH_STARTED;
                }
                /***/
                return BS_FINISH_DONE;
              }
              if (s.last_lit) {
                /*** FLUSH_BLOCK(s, 0); ***/
                flush_block_only(s, false);
                if (s.strm.avail_out === 0) {
                  return BS_NEED_MORE;
                }
                /***/
              }
              return BS_BLOCK_DONE;
            }
            
            /* Values for max_lazy_match, good_match and max_chain_length, depending on
             * the desired pack level (0..9). The values given below have been tuned to
             * exclude worst case performance for pathological files. Better values may be
             * found for specific files.
             */
            function Config(good_length, max_lazy, nice_length, max_chain, func) {
              this.good_length = good_length;
              this.max_lazy = max_lazy;
              this.nice_length = nice_length;
              this.max_chain = max_chain;
              this.func = func;
            }
            
            var configuration_table;
            
            configuration_table = [
              /*      good lazy nice chain */
              new Config(0, 0, 0, 0, deflate_stored),          /* 0 store only */
              new Config(4, 4, 8, 4, deflate_fast),            /* 1 max speed, no lazy matches */
              new Config(4, 5, 16, 8, deflate_fast),           /* 2 */
              new Config(4, 6, 32, 32, deflate_fast),          /* 3 */
            
              new Config(4, 4, 16, 16, deflate_slow),          /* 4 lazy matches */
              new Config(8, 16, 32, 32, deflate_slow),         /* 5 */
              new Config(8, 16, 128, 128, deflate_slow),       /* 6 */
              new Config(8, 32, 128, 256, deflate_slow),       /* 7 */
              new Config(32, 128, 258, 1024, deflate_slow),    /* 8 */
              new Config(32, 258, 258, 4096, deflate_slow)     /* 9 max compression */
            ];
            
            
            /* ===========================================================================
             * Initialize the "longest match" routines for a new zlib stream
             */
            function lm_init(s) {
              s.window_size = 2 * s.w_size;
            
              /*** CLEAR_HASH(s); ***/
              zero(s.head); // Fill with NIL (= 0);
            
              /* Set the default configuration parameters:
               */
              s.max_lazy_match = configuration_table[s.level].max_lazy;
              s.good_match = configuration_table[s.level].good_length;
              s.nice_match = configuration_table[s.level].nice_length;
              s.max_chain_length = configuration_table[s.level].max_chain;
            
              s.strstart = 0;
              s.block_start = 0;
              s.lookahead = 0;
              s.insert = 0;
              s.match_length = s.prev_length = MIN_MATCH - 1;
              s.match_available = 0;
              s.ins_h = 0;
            }
            
            
            function DeflateState() {
              this.strm = null;            /* pointer back to this zlib stream */
              this.status = 0;            /* as the name implies */
              this.pending_buf = null;      /* output still pending */
              this.pending_buf_size = 0;  /* size of pending_buf */
              this.pending_out = 0;       /* next pending byte to output to the stream */
              this.pending = 0;           /* nb of bytes in the pending buffer */
              this.wrap = 0;              /* bit 0 true for zlib, bit 1 true for gzip */
              this.gzhead = null;         /* gzip header information to write */
              this.gzindex = 0;           /* where in extra, name, or comment */
              this.method = Z_DEFLATED; /* can only be DEFLATED */
              this.last_flush = -1;   /* value of flush param for previous deflate call */
            
              this.w_size = 0;  /* LZ77 window size (32K by default) */
              this.w_bits = 0;  /* log2(w_size)  (8..16) */
              this.w_mask = 0;  /* w_size - 1 */
            
              this.window = null;
              /* Sliding window. Input bytes are read into the second half of the window,
               * and move to the first half later to keep a dictionary of at least wSize
               * bytes. With this organization, matches are limited to a distance of
               * wSize-MAX_MATCH bytes, but this ensures that IO is always
               * performed with a length multiple of the block size.
               */
            
              this.window_size = 0;
              /* Actual size of window: 2*wSize, except when the user input buffer
               * is directly used as sliding window.
               */
            
              this.prev = null;
              /* Link to older string with same hash index. To limit the size of this
               * array to 64K, this link is maintained only for the last 32K strings.
               * An index in this array is thus a window index modulo 32K.
               */
            
              this.head = null;   /* Heads of the hash chains or NIL. */
            
              this.ins_h = 0;       /* hash index of string to be inserted */
              this.hash_size = 0;   /* number of elements in hash table */
              this.hash_bits = 0;   /* log2(hash_size) */
              this.hash_mask = 0;   /* hash_size-1 */
            
              this.hash_shift = 0;
              /* Number of bits by which ins_h must be shifted at each input
               * step. It must be such that after MIN_MATCH steps, the oldest
               * byte no longer takes part in the hash key, that is:
               *   hash_shift * MIN_MATCH >= hash_bits
               */
            
              this.block_start = 0;
              /* Window position at the beginning of the current output block. Gets
               * negative when the window is moved backwards.
               */
            
              this.match_length = 0;      /* length of best match */
              this.prev_match = 0;        /* previous match */
              this.match_available = 0;   /* set if previous match exists */
              this.strstart = 0;          /* start of string to insert */
              this.match_start = 0;       /* start of matching string */
              this.lookahead = 0;         /* number of valid bytes ahead in window */
            
              this.prev_length = 0;
              /* Length of the best match at previous step. Matches not greater than this
               * are discarded. This is used in the lazy match evaluation.
               */
            
              this.max_chain_length = 0;
              /* To speed up deflation, hash chains are never searched beyond this
               * length.  A higher limit improves compression ratio but degrades the
               * speed.
               */
            
              this.max_lazy_match = 0;
              /* Attempt to find a better match only when the current match is strictly
               * smaller than this value. This mechanism is used only for compression
               * levels >= 4.
               */
              // That's alias to max_lazy_match, don't use directly
              //this.max_insert_length = 0;
              /* Insert new strings in the hash table only if the match length is not
               * greater than this length. This saves time but degrades compression.
               * max_insert_length is used only for compression levels <= 3.
               */
            
              this.level = 0;     /* compression level (1..9) */
              this.strategy = 0;  /* favor or force Huffman coding*/
            
              this.good_match = 0;
              /* Use a faster search when the previous match is longer than this */
            
              this.nice_match = 0; /* Stop searching when current match exceeds this */
            
                          /* used by trees.c: */
            
              /* Didn't use ct_data typedef below to suppress compiler warning */
            
              // struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
              // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
              // struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
            
              // Use flat array of DOUBLE size, with interleaved fata,
              // because JS does not support effective
              this.dyn_ltree  = new utils.Buf16(HEAP_SIZE * 2);
              this.dyn_dtree  = new utils.Buf16((2 * D_CODES + 1) * 2);
              this.bl_tree    = new utils.Buf16((2 * BL_CODES + 1) * 2);
              zero(this.dyn_ltree);
              zero(this.dyn_dtree);
              zero(this.bl_tree);
            
              this.l_desc   = null;         /* desc. for literal tree */
              this.d_desc   = null;         /* desc. for distance tree */
              this.bl_desc  = null;         /* desc. for bit length tree */
            
              //ush bl_count[MAX_BITS+1];
              this.bl_count = new utils.Buf16(MAX_BITS + 1);
              /* number of codes at each bit length for an optimal tree */
            
              //int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
              this.heap = new utils.Buf16(2 * L_CODES + 1);  /* heap used to build the Huffman trees */
              zero(this.heap);
            
              this.heap_len = 0;               /* number of elements in the heap */
              this.heap_max = 0;               /* element of largest frequency */
              /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
               * The same heap array is used to build all trees.
               */
            
              this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
              zero(this.depth);
              /* Depth of each subtree used as tie breaker for trees of equal frequency
               */
            
              this.l_buf = 0;          /* buffer index for literals or lengths */
            
              this.lit_bufsize = 0;
              /* Size of match buffer for literals/lengths.  There are 4 reasons for
               * limiting lit_bufsize to 64K:
               *   - frequencies can be kept in 16 bit counters
               *   - if compression is not successful for the first block, all input
               *     data is still in the window so we can still emit a stored block even
               *     when input comes from standard input.  (This can also be done for
               *     all blocks if lit_bufsize is not greater than 32K.)
               *   - if compression is not successful for a file smaller than 64K, we can
               *     even emit a stored file instead of a stored block (saving 5 bytes).
               *     This is applicable only for zip (not gzip or zlib).
               *   - creating new Huffman trees less frequently may not provide fast
               *     adaptation to changes in the input data statistics. (Take for
               *     example a binary file with poorly compressible code followed by
               *     a highly compressible string table.) Smaller buffer sizes give
               *     fast adaptation but have of course the overhead of transmitting
               *     trees more frequently.
               *   - I can't count above 4
               */
            
              this.last_lit = 0;      /* running index in l_buf */
            
              this.d_buf = 0;
              /* Buffer index for distances. To simplify the code, d_buf and l_buf have
               * the same number of elements. To use different lengths, an extra flag
               * array would be necessary.
               */
            
              this.opt_len = 0;       /* bit length of current block with optimal trees */
              this.static_len = 0;    /* bit length of current block with static trees */
              this.matches = 0;       /* number of string matches in current block */
              this.insert = 0;        /* bytes at end of window left to insert */
            
            
              this.bi_buf = 0;
              /* Output buffer. bits are inserted starting at the bottom (least
               * significant bits).
               */
              this.bi_valid = 0;
              /* Number of valid bits in bi_buf.  All bits above the last valid bit
               * are always zero.
               */
            
              // Used for window memory init. We safely ignore it for JS. That makes
              // sense only for pointers and memory check tools.
              //this.high_water = 0;
              /* High water mark offset in window for initialized bytes -- bytes above
               * this are set to zero in order to avoid memory check warnings when
               * longest match routines access bytes past the input.  This is then
               * updated to the new high water mark.
               */
            }
            
            
            function deflateResetKeep(strm) {
              var s;
            
              if (!strm || !strm.state) {
                return err(strm, Z_STREAM_ERROR);
              }
            
              strm.total_in = strm.total_out = 0;
              strm.data_type = Z_UNKNOWN;
            
              s = strm.state;
              s.pending = 0;
              s.pending_out = 0;
            
              if (s.wrap < 0) {
                s.wrap = -s.wrap;
                /* was made negative by deflate(..., Z_FINISH); */
              }
              s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
              strm.adler = (s.wrap === 2) ?
                0  // crc32(0, Z_NULL, 0)
              :
                1; // adler32(0, Z_NULL, 0)
              s.last_flush = Z_NO_FLUSH;
              trees._tr_init(s);
              return Z_OK;
            }
            
            
            function deflateReset(strm) {
              var ret = deflateResetKeep(strm);
              if (ret === Z_OK) {
                lm_init(strm.state);
              }
              return ret;
            }
            
            
            function deflateSetHeader(strm, head) {
              if (!strm || !strm.state) { return Z_STREAM_ERROR; }
              if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
              strm.state.gzhead = head;
              return Z_OK;
            }
            
            
            function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
              if (!strm) { // === Z_NULL
                return Z_STREAM_ERROR;
              }
              var wrap = 1;
            
              if (level === Z_DEFAULT_COMPRESSION) {
                level = 6;
              }
            
              if (windowBits < 0) { /* suppress zlib wrapper */
                wrap = 0;
                windowBits = -windowBits;
              }
            
              else if (windowBits > 15) {
                wrap = 2;           /* write gzip wrapper instead */
                windowBits -= 16;
              }
            
            
              if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
                windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
                strategy < 0 || strategy > Z_FIXED) {
                return err(strm, Z_STREAM_ERROR);
              }
            
            
              if (windowBits === 8) {
                windowBits = 9;
              }
              /* until 256-byte window bug fixed */
            
              var s = new DeflateState();
            
              strm.state = s;
              s.strm = strm;
            
              s.wrap = wrap;
              s.gzhead = null;
              s.w_bits = windowBits;
              s.w_size = 1 << s.w_bits;
              s.w_mask = s.w_size - 1;
            
              s.hash_bits = memLevel + 7;
              s.hash_size = 1 << s.hash_bits;
              s.hash_mask = s.hash_size - 1;
              s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
            
              s.window = new utils.Buf8(s.w_size * 2);
              s.head = new utils.Buf16(s.hash_size);
              s.prev = new utils.Buf16(s.w_size);
            
              // Don't need mem init magic for JS.
              //s.high_water = 0;  /* nothing written to s->window yet */
            
              s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
            
              s.pending_buf_size = s.lit_bufsize * 4;
            
              //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
              //s->pending_buf = (uchf *) overlay;
              s.pending_buf = new utils.Buf8(s.pending_buf_size);
            
              // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
              //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
              s.d_buf = 1 * s.lit_bufsize;
            
              //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
              s.l_buf = (1 + 2) * s.lit_bufsize;
            
              s.level = level;
              s.strategy = strategy;
              s.method = method;
            
              return deflateReset(strm);
            }
            
            function deflateInit(strm, level) {
              return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
            }
            
            
            function deflate(strm, flush) {
              var old_flush, s;
              var beg, val; // for gzip header write only
            
              if (!strm || !strm.state ||
                flush > Z_BLOCK || flush < 0) {
                return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
              }
            
              s = strm.state;
            
              if (!strm.output ||
                  (!strm.input && strm.avail_in !== 0) ||
                  (s.status === FINISH_STATE && flush !== Z_FINISH)) {
                return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
              }
            
              s.strm = strm; /* just in case */
              old_flush = s.last_flush;
              s.last_flush = flush;
            
              /* Write the header */
              if (s.status === INIT_STATE) {
            
                if (s.wrap === 2) { // GZIP header
                  strm.adler = 0;  //crc32(0L, Z_NULL, 0);
                  put_byte(s, 31);
                  put_byte(s, 139);
                  put_byte(s, 8);
                  if (!s.gzhead) { // s->gzhead == Z_NULL
                    put_byte(s, 0);
                    put_byte(s, 0);
                    put_byte(s, 0);
                    put_byte(s, 0);
                    put_byte(s, 0);
                    put_byte(s, s.level === 9 ? 2 :
                                (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
                                 4 : 0));
                    put_byte(s, OS_CODE);
                    s.status = BUSY_STATE;
                  }
                  else {
                    put_byte(s, (s.gzhead.text ? 1 : 0) +
                                (s.gzhead.hcrc ? 2 : 0) +
                                (!s.gzhead.extra ? 0 : 4) +
                                (!s.gzhead.name ? 0 : 8) +
                                (!s.gzhead.comment ? 0 : 16)
                    );
                    put_byte(s, s.gzhead.time & 0xff);
                    put_byte(s, (s.gzhead.time >> 8) & 0xff);
                    put_byte(s, (s.gzhead.time >> 16) & 0xff);
                    put_byte(s, (s.gzhead.time >> 24) & 0xff);
                    put_byte(s, s.level === 9 ? 2 :
                                (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
                                 4 : 0));
                    put_byte(s, s.gzhead.os & 0xff);
                    if (s.gzhead.extra && s.gzhead.extra.length) {
                      put_byte(s, s.gzhead.extra.length & 0xff);
                      put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
                    }
                    if (s.gzhead.hcrc) {
                      strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
                    }
                    s.gzindex = 0;
                    s.status = EXTRA_STATE;
                  }
                }
                else // DEFLATE header
                {
                  var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
                  var level_flags = -1;
            
                  if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
                    level_flags = 0;
                  } else if (s.level < 6) {
                    level_flags = 1;
                  } else if (s.level === 6) {
                    level_flags = 2;
                  } else {
                    level_flags = 3;
                  }
                  header |= (level_flags << 6);
                  if (s.strstart !== 0) { header |= PRESET_DICT; }
                  header += 31 - (header % 31);
            
                  s.status = BUSY_STATE;
                  putShortMSB(s, header);
            
                  /* Save the adler32 of the preset dictionary: */
                  if (s.strstart !== 0) {
                    putShortMSB(s, strm.adler >>> 16);
                    putShortMSB(s, strm.adler & 0xffff);
                  }
                  strm.adler = 1; // adler32(0L, Z_NULL, 0);
                }
              }
            
            //#ifdef GZIP
              if (s.status === EXTRA_STATE) {
                if (s.gzhead.extra/* != Z_NULL*/) {
                  beg = s.pending;  /* start of bytes to update crc */
            
                  while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
                    if (s.pending === s.pending_buf_size) {
                      if (s.gzhead.hcrc && s.pending > beg) {
                        strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
                      }
                      flush_pending(strm);
                      beg = s.pending;
                      if (s.pending === s.pending_buf_size) {
                        break;
                      }
                    }
                    put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
                    s.gzindex++;
                  }
                  if (s.gzhead.hcrc && s.pending > beg) {
                    strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
                  }
                  if (s.gzindex === s.gzhead.extra.length) {
                    s.gzindex = 0;
                    s.status = NAME_STATE;
                  }
                }
                else {
                  s.status = NAME_STATE;
                }
              }
              if (s.status === NAME_STATE) {
                if (s.gzhead.name/* != Z_NULL*/) {
                  beg = s.pending;  /* start of bytes to update crc */
                  //int val;
            
                  do {
                    if (s.pending === s.pending_buf_size) {
                      if (s.gzhead.hcrc && s.pending > beg) {
                        strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
                      }
                      flush_pending(strm);