emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java
branchemul
changeset 694 0d277415ed02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java	Thu Feb 07 12:58:12 2013 +0100
     1.3 @@ -0,0 +1,520 @@
     1.4 +/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
     1.5 +/*
     1.6 +Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
     1.7 +
     1.8 +Redistribution and use in source and binary forms, with or without
     1.9 +modification, are permitted provided that the following conditions are met:
    1.10 +
    1.11 +  1. Redistributions of source code must retain the above copyright notice,
    1.12 +     this list of conditions and the following disclaimer.
    1.13 +
    1.14 +  2. Redistributions in binary form must reproduce the above copyright 
    1.15 +     notice, this list of conditions and the following disclaimer in 
    1.16 +     the documentation and/or other materials provided with the distribution.
    1.17 +
    1.18 +  3. The names of the authors may not be used to endorse or promote products
    1.19 +     derived from this software without specific prior written permission.
    1.20 +
    1.21 +THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    1.22 +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.23 +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
    1.24 +INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.25 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.26 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    1.27 +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.28 +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.29 +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    1.30 +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.31 + */
    1.32 +/*
    1.33 + * This program is based on zlib-1.1.3, so all credit should go authors
    1.34 + * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
    1.35 + * and contributors of zlib.
    1.36 + */
    1.37 +
    1.38 +package org.apidesign.bck2brwsr.emul.zip;
    1.39 +
    1.40 +import org.apidesign.bck2brwsr.emul.lang.System;
    1.41 +
    1.42 +final class InfTree{
    1.43 +
    1.44 +  static final private int MANY=1440;
    1.45 +
    1.46 +  static final private int Z_OK=0;
    1.47 +  static final private int Z_STREAM_END=1;
    1.48 +  static final private int Z_NEED_DICT=2;
    1.49 +  static final private int Z_ERRNO=-1;
    1.50 +  static final private int Z_STREAM_ERROR=-2;
    1.51 +  static final private int Z_DATA_ERROR=-3;
    1.52 +  static final private int Z_MEM_ERROR=-4;
    1.53 +  static final private int Z_BUF_ERROR=-5;
    1.54 +  static final private int Z_VERSION_ERROR=-6;
    1.55 +
    1.56 +  static final int fixed_bl = 9;
    1.57 +  static final int fixed_bd = 5;
    1.58 +
    1.59 +  static final int[] fixed_tl = {
    1.60 +    96,7,256, 0,8,80, 0,8,16, 84,8,115,
    1.61 +    82,7,31, 0,8,112, 0,8,48, 0,9,192,
    1.62 +    80,7,10, 0,8,96, 0,8,32, 0,9,160,
    1.63 +    0,8,0, 0,8,128, 0,8,64, 0,9,224,
    1.64 +    80,7,6, 0,8,88, 0,8,24, 0,9,144,
    1.65 +    83,7,59, 0,8,120, 0,8,56, 0,9,208,
    1.66 +    81,7,17, 0,8,104, 0,8,40, 0,9,176,
    1.67 +    0,8,8, 0,8,136, 0,8,72, 0,9,240,
    1.68 +    80,7,4, 0,8,84, 0,8,20, 85,8,227,
    1.69 +    83,7,43, 0,8,116, 0,8,52, 0,9,200,
    1.70 +    81,7,13, 0,8,100, 0,8,36, 0,9,168,
    1.71 +    0,8,4, 0,8,132, 0,8,68, 0,9,232,
    1.72 +    80,7,8, 0,8,92, 0,8,28, 0,9,152,
    1.73 +    84,7,83, 0,8,124, 0,8,60, 0,9,216,
    1.74 +    82,7,23, 0,8,108, 0,8,44, 0,9,184,
    1.75 +    0,8,12, 0,8,140, 0,8,76, 0,9,248,
    1.76 +    80,7,3, 0,8,82, 0,8,18, 85,8,163,
    1.77 +    83,7,35, 0,8,114, 0,8,50, 0,9,196,
    1.78 +    81,7,11, 0,8,98, 0,8,34, 0,9,164,
    1.79 +    0,8,2, 0,8,130, 0,8,66, 0,9,228,
    1.80 +    80,7,7, 0,8,90, 0,8,26, 0,9,148,
    1.81 +    84,7,67, 0,8,122, 0,8,58, 0,9,212,
    1.82 +    82,7,19, 0,8,106, 0,8,42, 0,9,180,
    1.83 +    0,8,10, 0,8,138, 0,8,74, 0,9,244,
    1.84 +    80,7,5, 0,8,86, 0,8,22, 192,8,0,
    1.85 +    83,7,51, 0,8,118, 0,8,54, 0,9,204,
    1.86 +    81,7,15, 0,8,102, 0,8,38, 0,9,172,
    1.87 +    0,8,6, 0,8,134, 0,8,70, 0,9,236,
    1.88 +    80,7,9, 0,8,94, 0,8,30, 0,9,156,
    1.89 +    84,7,99, 0,8,126, 0,8,62, 0,9,220,
    1.90 +    82,7,27, 0,8,110, 0,8,46, 0,9,188,
    1.91 +    0,8,14, 0,8,142, 0,8,78, 0,9,252,
    1.92 +    96,7,256, 0,8,81, 0,8,17, 85,8,131,
    1.93 +    82,7,31, 0,8,113, 0,8,49, 0,9,194,
    1.94 +    80,7,10, 0,8,97, 0,8,33, 0,9,162,
    1.95 +    0,8,1, 0,8,129, 0,8,65, 0,9,226,
    1.96 +    80,7,6, 0,8,89, 0,8,25, 0,9,146,
    1.97 +    83,7,59, 0,8,121, 0,8,57, 0,9,210,
    1.98 +    81,7,17, 0,8,105, 0,8,41, 0,9,178,
    1.99 +    0,8,9, 0,8,137, 0,8,73, 0,9,242,
   1.100 +    80,7,4, 0,8,85, 0,8,21, 80,8,258,
   1.101 +    83,7,43, 0,8,117, 0,8,53, 0,9,202,
   1.102 +    81,7,13, 0,8,101, 0,8,37, 0,9,170,
   1.103 +    0,8,5, 0,8,133, 0,8,69, 0,9,234,
   1.104 +    80,7,8, 0,8,93, 0,8,29, 0,9,154,
   1.105 +    84,7,83, 0,8,125, 0,8,61, 0,9,218,
   1.106 +    82,7,23, 0,8,109, 0,8,45, 0,9,186,
   1.107 +    0,8,13, 0,8,141, 0,8,77, 0,9,250,
   1.108 +    80,7,3, 0,8,83, 0,8,19, 85,8,195,
   1.109 +    83,7,35, 0,8,115, 0,8,51, 0,9,198,
   1.110 +    81,7,11, 0,8,99, 0,8,35, 0,9,166,
   1.111 +    0,8,3, 0,8,131, 0,8,67, 0,9,230,
   1.112 +    80,7,7, 0,8,91, 0,8,27, 0,9,150,
   1.113 +    84,7,67, 0,8,123, 0,8,59, 0,9,214,
   1.114 +    82,7,19, 0,8,107, 0,8,43, 0,9,182,
   1.115 +    0,8,11, 0,8,139, 0,8,75, 0,9,246,
   1.116 +    80,7,5, 0,8,87, 0,8,23, 192,8,0,
   1.117 +    83,7,51, 0,8,119, 0,8,55, 0,9,206,
   1.118 +    81,7,15, 0,8,103, 0,8,39, 0,9,174,
   1.119 +    0,8,7, 0,8,135, 0,8,71, 0,9,238,
   1.120 +    80,7,9, 0,8,95, 0,8,31, 0,9,158,
   1.121 +    84,7,99, 0,8,127, 0,8,63, 0,9,222,
   1.122 +    82,7,27, 0,8,111, 0,8,47, 0,9,190,
   1.123 +    0,8,15, 0,8,143, 0,8,79, 0,9,254,
   1.124 +    96,7,256, 0,8,80, 0,8,16, 84,8,115,
   1.125 +    82,7,31, 0,8,112, 0,8,48, 0,9,193,
   1.126 +
   1.127 +    80,7,10, 0,8,96, 0,8,32, 0,9,161,
   1.128 +    0,8,0, 0,8,128, 0,8,64, 0,9,225,
   1.129 +    80,7,6, 0,8,88, 0,8,24, 0,9,145,
   1.130 +    83,7,59, 0,8,120, 0,8,56, 0,9,209,
   1.131 +    81,7,17, 0,8,104, 0,8,40, 0,9,177,
   1.132 +    0,8,8, 0,8,136, 0,8,72, 0,9,241,
   1.133 +    80,7,4, 0,8,84, 0,8,20, 85,8,227,
   1.134 +    83,7,43, 0,8,116, 0,8,52, 0,9,201,
   1.135 +    81,7,13, 0,8,100, 0,8,36, 0,9,169,
   1.136 +    0,8,4, 0,8,132, 0,8,68, 0,9,233,
   1.137 +    80,7,8, 0,8,92, 0,8,28, 0,9,153,
   1.138 +    84,7,83, 0,8,124, 0,8,60, 0,9,217,
   1.139 +    82,7,23, 0,8,108, 0,8,44, 0,9,185,
   1.140 +    0,8,12, 0,8,140, 0,8,76, 0,9,249,
   1.141 +    80,7,3, 0,8,82, 0,8,18, 85,8,163,
   1.142 +    83,7,35, 0,8,114, 0,8,50, 0,9,197,
   1.143 +    81,7,11, 0,8,98, 0,8,34, 0,9,165,
   1.144 +    0,8,2, 0,8,130, 0,8,66, 0,9,229,
   1.145 +    80,7,7, 0,8,90, 0,8,26, 0,9,149,
   1.146 +    84,7,67, 0,8,122, 0,8,58, 0,9,213,
   1.147 +    82,7,19, 0,8,106, 0,8,42, 0,9,181,
   1.148 +    0,8,10, 0,8,138, 0,8,74, 0,9,245,
   1.149 +    80,7,5, 0,8,86, 0,8,22, 192,8,0,
   1.150 +    83,7,51, 0,8,118, 0,8,54, 0,9,205,
   1.151 +    81,7,15, 0,8,102, 0,8,38, 0,9,173,
   1.152 +    0,8,6, 0,8,134, 0,8,70, 0,9,237,
   1.153 +    80,7,9, 0,8,94, 0,8,30, 0,9,157,
   1.154 +    84,7,99, 0,8,126, 0,8,62, 0,9,221,
   1.155 +    82,7,27, 0,8,110, 0,8,46, 0,9,189,
   1.156 +    0,8,14, 0,8,142, 0,8,78, 0,9,253,
   1.157 +    96,7,256, 0,8,81, 0,8,17, 85,8,131,
   1.158 +    82,7,31, 0,8,113, 0,8,49, 0,9,195,
   1.159 +    80,7,10, 0,8,97, 0,8,33, 0,9,163,
   1.160 +    0,8,1, 0,8,129, 0,8,65, 0,9,227,
   1.161 +    80,7,6, 0,8,89, 0,8,25, 0,9,147,
   1.162 +    83,7,59, 0,8,121, 0,8,57, 0,9,211,
   1.163 +    81,7,17, 0,8,105, 0,8,41, 0,9,179,
   1.164 +    0,8,9, 0,8,137, 0,8,73, 0,9,243,
   1.165 +    80,7,4, 0,8,85, 0,8,21, 80,8,258,
   1.166 +    83,7,43, 0,8,117, 0,8,53, 0,9,203,
   1.167 +    81,7,13, 0,8,101, 0,8,37, 0,9,171,
   1.168 +    0,8,5, 0,8,133, 0,8,69, 0,9,235,
   1.169 +    80,7,8, 0,8,93, 0,8,29, 0,9,155,
   1.170 +    84,7,83, 0,8,125, 0,8,61, 0,9,219,
   1.171 +    82,7,23, 0,8,109, 0,8,45, 0,9,187,
   1.172 +    0,8,13, 0,8,141, 0,8,77, 0,9,251,
   1.173 +    80,7,3, 0,8,83, 0,8,19, 85,8,195,
   1.174 +    83,7,35, 0,8,115, 0,8,51, 0,9,199,
   1.175 +    81,7,11, 0,8,99, 0,8,35, 0,9,167,
   1.176 +    0,8,3, 0,8,131, 0,8,67, 0,9,231,
   1.177 +    80,7,7, 0,8,91, 0,8,27, 0,9,151,
   1.178 +    84,7,67, 0,8,123, 0,8,59, 0,9,215,
   1.179 +    82,7,19, 0,8,107, 0,8,43, 0,9,183,
   1.180 +    0,8,11, 0,8,139, 0,8,75, 0,9,247,
   1.181 +    80,7,5, 0,8,87, 0,8,23, 192,8,0,
   1.182 +    83,7,51, 0,8,119, 0,8,55, 0,9,207,
   1.183 +    81,7,15, 0,8,103, 0,8,39, 0,9,175,
   1.184 +    0,8,7, 0,8,135, 0,8,71, 0,9,239,
   1.185 +    80,7,9, 0,8,95, 0,8,31, 0,9,159,
   1.186 +    84,7,99, 0,8,127, 0,8,63, 0,9,223,
   1.187 +    82,7,27, 0,8,111, 0,8,47, 0,9,191,
   1.188 +    0,8,15, 0,8,143, 0,8,79, 0,9,255
   1.189 +  };
   1.190 +  static final int[] fixed_td = {
   1.191 +    80,5,1, 87,5,257, 83,5,17, 91,5,4097,
   1.192 +    81,5,5, 89,5,1025, 85,5,65, 93,5,16385,
   1.193 +    80,5,3, 88,5,513, 84,5,33, 92,5,8193,
   1.194 +    82,5,9, 90,5,2049, 86,5,129, 192,5,24577,
   1.195 +    80,5,2, 87,5,385, 83,5,25, 91,5,6145,
   1.196 +    81,5,7, 89,5,1537, 85,5,97, 93,5,24577,
   1.197 +    80,5,4, 88,5,769, 84,5,49, 92,5,12289,
   1.198 +    82,5,13, 90,5,3073, 86,5,193, 192,5,24577
   1.199 +  };
   1.200 +
   1.201 +  // Tables for deflate from PKZIP's appnote.txt.
   1.202 +  static final int[] cplens = { // Copy lengths for literal codes 257..285
   1.203 +        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
   1.204 +        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
   1.205 +  };
   1.206 +
   1.207 +  // see note #13 above about 258
   1.208 +  static final int[] cplext = { // Extra bits for literal codes 257..285
   1.209 +        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
   1.210 +        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112  // 112==invalid
   1.211 +  };
   1.212 +
   1.213 +  static final int[] cpdist = { // Copy offsets for distance codes 0..29
   1.214 +        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
   1.215 +        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
   1.216 +        8193, 12289, 16385, 24577
   1.217 +  };
   1.218 +
   1.219 +  static final int[] cpdext = { // Extra bits for distance codes
   1.220 +        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
   1.221 +        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
   1.222 +        12, 12, 13, 13};
   1.223 +
   1.224 +  // If BMAX needs to be larger than 16, then h and x[] should be uLong.
   1.225 +  static final int BMAX=15;         // maximum bit length of any code
   1.226 +
   1.227 +  int[] hn = null;  // hufts used in space
   1.228 +  int[] v = null;   // work area for huft_build 
   1.229 +  int[] c = null;   // bit length count table
   1.230 +  int[] r = null;   // table entry for structure assignment
   1.231 +  int[] u = null;   // table stack
   1.232 +  int[] x = null;   // bit offsets, then code stack
   1.233 +
   1.234 +  private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX)
   1.235 +                         int bindex, 
   1.236 +                         int n,   // number of codes (assumed <= 288)
   1.237 +                         int s,   // number of simple-valued codes (0..s-1)
   1.238 +                         int[] d, // list of base values for non-simple codes
   1.239 +                         int[] e, // list of extra bits for non-simple codes
   1.240 +                         int[] t, // result: starting table
   1.241 +                         int[] m, // maximum lookup bits, returns actual
   1.242 +                         int[] hp,// space for trees
   1.243 +                         int[] hn,// hufts used in space
   1.244 +                         int[] v  // working area: values in order of bit length
   1.245 +                         ){
   1.246 +    // Given a list of code lengths and a maximum table size, make a set of
   1.247 +    // tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
   1.248 +    // if the given code set is incomplete (the tables are still built in this
   1.249 +    // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
   1.250 +    // lengths), or Z_MEM_ERROR if not enough memory.
   1.251 +
   1.252 +    int a;                       // counter for codes of length k
   1.253 +    int f;                       // i repeats in table every f entries
   1.254 +    int g;                       // maximum code length
   1.255 +    int h;                       // table level
   1.256 +    int i;                       // counter, current code
   1.257 +    int j;                       // counter
   1.258 +    int k;                       // number of bits in current code
   1.259 +    int l;                       // bits per table (returned in m)
   1.260 +    int mask;                    // (1 << w) - 1, to avoid cc -O bug on HP
   1.261 +    int p;                       // pointer into c[], b[], or v[]
   1.262 +    int q;                       // points to current table
   1.263 +    int w;                       // bits before this table == (l * h)
   1.264 +    int xp;                      // pointer into x
   1.265 +    int y;                       // number of dummy codes added
   1.266 +    int z;                       // number of entries in current table
   1.267 +
   1.268 +    // Generate counts for each bit length
   1.269 +
   1.270 +    p = 0; i = n;
   1.271 +    do {
   1.272 +      c[b[bindex+p]]++; p++; i--;   // assume all entries <= BMAX
   1.273 +    }while(i!=0);
   1.274 +
   1.275 +    if(c[0] == n){                // null input--all zero length codes
   1.276 +      t[0] = -1;
   1.277 +      m[0] = 0;
   1.278 +      return Z_OK;
   1.279 +    }
   1.280 +
   1.281 +    // Find minimum and maximum length, bound *m by those
   1.282 +    l = m[0];
   1.283 +    for (j = 1; j <= BMAX; j++)
   1.284 +      if(c[j]!=0) break;
   1.285 +    k = j;                        // minimum code length
   1.286 +    if(l < j){
   1.287 +      l = j;
   1.288 +    }
   1.289 +    for (i = BMAX; i!=0; i--){
   1.290 +      if(c[i]!=0) break;
   1.291 +    }
   1.292 +    g = i;                        // maximum code length
   1.293 +    if(l > i){
   1.294 +      l = i;
   1.295 +    }
   1.296 +    m[0] = l;
   1.297 +
   1.298 +    // Adjust last length count to fill out codes, if needed
   1.299 +    for (y = 1 << j; j < i; j++, y <<= 1){
   1.300 +      if ((y -= c[j]) < 0){
   1.301 +        return Z_DATA_ERROR;
   1.302 +      }
   1.303 +    }
   1.304 +    if ((y -= c[i]) < 0){
   1.305 +      return Z_DATA_ERROR;
   1.306 +    }
   1.307 +    c[i] += y;
   1.308 +
   1.309 +    // Generate starting offsets into the value table for each length
   1.310 +    x[1] = j = 0;
   1.311 +    p = 1;  xp = 2;
   1.312 +    while (--i!=0) {                 // note that i == g from above
   1.313 +      x[xp] = (j += c[p]);
   1.314 +      xp++;
   1.315 +      p++;
   1.316 +    }
   1.317 +
   1.318 +    // Make a table of values in order of bit lengths
   1.319 +    i = 0; p = 0;
   1.320 +    do {
   1.321 +      if ((j = b[bindex+p]) != 0){
   1.322 +        v[x[j]++] = i;
   1.323 +      }
   1.324 +      p++;
   1.325 +    }
   1.326 +    while (++i < n);
   1.327 +    n = x[g];                     // set n to length of v
   1.328 +
   1.329 +    // Generate the Huffman codes and for each, make the table entries
   1.330 +    x[0] = i = 0;                 // first Huffman code is zero
   1.331 +    p = 0;                        // grab values in bit order
   1.332 +    h = -1;                       // no tables yet--level -1
   1.333 +    w = -l;                       // bits decoded == (l * h)
   1.334 +    u[0] = 0;                     // just to keep compilers happy
   1.335 +    q = 0;                        // ditto
   1.336 +    z = 0;                        // ditto
   1.337 +
   1.338 +    // go through the bit lengths (k already is bits in shortest code)
   1.339 +    for (; k <= g; k++){
   1.340 +      a = c[k];
   1.341 +      while (a--!=0){
   1.342 +	// here i is the Huffman code of length k bits for value *p
   1.343 +	// make tables up to required level
   1.344 +        while (k > w + l){
   1.345 +          h++;
   1.346 +          w += l;                 // previous table always l bits
   1.347 +	  // compute minimum size table less than or equal to l bits
   1.348 +          z = g - w;
   1.349 +          z = (z > l) ? l : z;        // table size upper limit
   1.350 +          if((f=1<<(j=k-w))>a+1){     // try a k-w bit table
   1.351 +                                      // too few codes for k-w bit table
   1.352 +            f -= a + 1;               // deduct codes from patterns left
   1.353 +            xp = k;
   1.354 +            if(j < z){
   1.355 +              while (++j < z){        // try smaller tables up to z bits
   1.356 +                if((f <<= 1) <= c[++xp])
   1.357 +                  break;              // enough codes to use up j bits
   1.358 +                f -= c[xp];           // else deduct codes from patterns
   1.359 +              }
   1.360 +	    }
   1.361 +          }
   1.362 +          z = 1 << j;                 // table entries for j-bit table
   1.363 +
   1.364 +	  // allocate new table
   1.365 +          if (hn[0] + z > MANY){       // (note: doesn't matter for fixed)
   1.366 +            return Z_DATA_ERROR;       // overflow of MANY
   1.367 +          }
   1.368 +          u[h] = q = /*hp+*/ hn[0];   // DEBUG
   1.369 +          hn[0] += z;
   1.370 + 
   1.371 +	  // connect to last table, if there is one
   1.372 +	  if(h!=0){
   1.373 +            x[h]=i;           // save pattern for backing up
   1.374 +            r[0]=(byte)j;     // bits in this table
   1.375 +            r[1]=(byte)l;     // bits to dump before this table
   1.376 +            j=i>>>(w - l);
   1.377 +            r[2] = (int)(q - u[h-1] - j);               // offset to this table
   1.378 +            System.arraycopy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table
   1.379 +          }
   1.380 +          else{
   1.381 +            t[0] = q;               // first table is returned result
   1.382 +	  }
   1.383 +        }
   1.384 +
   1.385 +	// set up table entry in r
   1.386 +        r[1] = (byte)(k - w);
   1.387 +        if (p >= n){
   1.388 +          r[0] = 128 + 64;      // out of values--invalid code
   1.389 +	}
   1.390 +        else if (v[p] < s){
   1.391 +          r[0] = (byte)(v[p] < 256 ? 0 : 32 + 64);  // 256 is end-of-block
   1.392 +          r[2] = v[p++];          // simple code is just the value
   1.393 +        }
   1.394 +        else{
   1.395 +          r[0]=(byte)(e[v[p]-s]+16+64); // non-simple--look up in lists
   1.396 +          r[2]=d[v[p++] - s];
   1.397 +        }
   1.398 +
   1.399 +        // fill code-like entries with r
   1.400 +        f=1<<(k-w);
   1.401 +        for (j=i>>>w;j<z;j+=f){
   1.402 +          System.arraycopy(r, 0, hp, (q+j)*3, 3);
   1.403 +	}
   1.404 +
   1.405 +	// backwards increment the k-bit code i
   1.406 +        for (j = 1 << (k - 1); (i & j)!=0; j >>>= 1){
   1.407 +          i ^= j;
   1.408 +	}
   1.409 +        i ^= j;
   1.410 +
   1.411 +	// backup over finished tables
   1.412 +        mask = (1 << w) - 1;      // needed on HP, cc -O bug
   1.413 +        while ((i & mask) != x[h]){
   1.414 +          h--;                    // don't need to update q
   1.415 +          w -= l;
   1.416 +          mask = (1 << w) - 1;
   1.417 +        }
   1.418 +      }
   1.419 +    }
   1.420 +    // Return Z_BUF_ERROR if we were given an incomplete table
   1.421 +    return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
   1.422 +  }
   1.423 +
   1.424 +  int inflate_trees_bits(int[] c,  // 19 code lengths
   1.425 +                         int[] bb, // bits tree desired/actual depth
   1.426 +                         int[] tb, // bits tree result
   1.427 +                         int[] hp, // space for trees
   1.428 +                         ZStream z // for messages
   1.429 +                         ){
   1.430 +    int result;
   1.431 +    initWorkArea(19);
   1.432 +    hn[0]=0;
   1.433 +    result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
   1.434 +
   1.435 +    if(result == Z_DATA_ERROR){
   1.436 +      z.msg = "oversubscribed dynamic bit lengths tree";
   1.437 +    }
   1.438 +    else if(result == Z_BUF_ERROR || bb[0] == 0){
   1.439 +      z.msg = "incomplete dynamic bit lengths tree";
   1.440 +      result = Z_DATA_ERROR;
   1.441 +    }
   1.442 +    return result;
   1.443 +  }
   1.444 +
   1.445 +  int inflate_trees_dynamic(int nl,   // number of literal/length codes
   1.446 +                            int nd,   // number of distance codes
   1.447 +                            int[] c,  // that many (total) code lengths
   1.448 +                            int[] bl, // literal desired/actual bit depth
   1.449 +                            int[] bd, // distance desired/actual bit depth 
   1.450 +                            int[] tl, // literal/length tree result
   1.451 +                            int[] td, // distance tree result
   1.452 +                            int[] hp, // space for trees
   1.453 +                            ZStream z // for messages
   1.454 +                            ){
   1.455 +    int result;
   1.456 +
   1.457 +    // build literal/length tree
   1.458 +    initWorkArea(288);
   1.459 +    hn[0]=0;
   1.460 +    result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
   1.461 +    if (result != Z_OK || bl[0] == 0){
   1.462 +      if(result == Z_DATA_ERROR){
   1.463 +        z.msg = "oversubscribed literal/length tree";
   1.464 +      }
   1.465 +      else if (result != Z_MEM_ERROR){
   1.466 +        z.msg = "incomplete literal/length tree";
   1.467 +        result = Z_DATA_ERROR;
   1.468 +      }
   1.469 +      return result;
   1.470 +    }
   1.471 +
   1.472 +    // build distance tree
   1.473 +    initWorkArea(288);
   1.474 +    result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
   1.475 +
   1.476 +    if (result != Z_OK || (bd[0] == 0 && nl > 257)){
   1.477 +      if (result == Z_DATA_ERROR){
   1.478 +        z.msg = "oversubscribed distance tree";
   1.479 +      }
   1.480 +      else if (result == Z_BUF_ERROR) {
   1.481 +        z.msg = "incomplete distance tree";
   1.482 +        result = Z_DATA_ERROR;
   1.483 +      }
   1.484 +      else if (result != Z_MEM_ERROR){
   1.485 +        z.msg = "empty distance tree with lengths";
   1.486 +        result = Z_DATA_ERROR;
   1.487 +      }
   1.488 +      return result;
   1.489 +    }
   1.490 +
   1.491 +    return Z_OK;
   1.492 +  }
   1.493 +
   1.494 +  static int inflate_trees_fixed(int[] bl,  //literal desired/actual bit depth
   1.495 +                                 int[] bd,  //distance desired/actual bit depth
   1.496 +                                 int[][] tl,//literal/length tree result
   1.497 +                                 int[][] td,//distance tree result 
   1.498 +                                 ZStream z  //for memory allocation
   1.499 +				 ){
   1.500 +    bl[0]=fixed_bl;
   1.501 +    bd[0]=fixed_bd;
   1.502 +    tl[0]=fixed_tl;
   1.503 +    td[0]=fixed_td;
   1.504 +    return Z_OK;
   1.505 +  }
   1.506 +
   1.507 +  private void initWorkArea(int vsize){
   1.508 +    if(hn==null){
   1.509 +      hn=new int[1];
   1.510 +      v=new int[vsize];
   1.511 +      c=new int[BMAX+1];
   1.512 +      r=new int[3];
   1.513 +      u=new int[BMAX];
   1.514 +      x=new int[BMAX+1];
   1.515 +    }
   1.516 +    if(v.length<vsize){ v=new int[vsize]; }
   1.517 +    for(int i=0; i<vsize; i++){v[i]=0;}
   1.518 +    for(int i=0; i<BMAX+1; i++){c[i]=0;}
   1.519 +    for(int i=0; i<3; i++){r[i]=0;}
   1.520 +    System.arraycopy(c, 0, u, 0, BMAX);
   1.521 +    System.arraycopy(c, 0, x, 0, BMAX+1);
   1.522 +  }
   1.523 +}