rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 694 emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java@0d277415ed02
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
     2 /*
     3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
     4 
     5 Redistribution and use in source and binary forms, with or without
     6 modification, are permitted provided that the following conditions are met:
     7 
     8   1. Redistributions of source code must retain the above copyright notice,
     9      this list of conditions and the following disclaimer.
    10 
    11   2. Redistributions in binary form must reproduce the above copyright 
    12      notice, this list of conditions and the following disclaimer in 
    13      the documentation and/or other materials provided with the distribution.
    14 
    15   3. The names of the authors may not be used to endorse or promote products
    16      derived from this software without specific prior written permission.
    17 
    18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
    21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
    22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    28  */
    29 /*
    30  * This program is based on zlib-1.1.3, so all credit should go authors
    31  * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
    32  * and contributors of zlib.
    33  */
    34 
    35 package org.apidesign.bck2brwsr.emul.zip;
    36 
    37 import org.apidesign.bck2brwsr.emul.lang.System;
    38 
    39 final class InfTree{
    40 
    41   static final private int MANY=1440;
    42 
    43   static final private int Z_OK=0;
    44   static final private int Z_STREAM_END=1;
    45   static final private int Z_NEED_DICT=2;
    46   static final private int Z_ERRNO=-1;
    47   static final private int Z_STREAM_ERROR=-2;
    48   static final private int Z_DATA_ERROR=-3;
    49   static final private int Z_MEM_ERROR=-4;
    50   static final private int Z_BUF_ERROR=-5;
    51   static final private int Z_VERSION_ERROR=-6;
    52 
    53   static final int fixed_bl = 9;
    54   static final int fixed_bd = 5;
    55 
    56   static final int[] fixed_tl = {
    57     96,7,256, 0,8,80, 0,8,16, 84,8,115,
    58     82,7,31, 0,8,112, 0,8,48, 0,9,192,
    59     80,7,10, 0,8,96, 0,8,32, 0,9,160,
    60     0,8,0, 0,8,128, 0,8,64, 0,9,224,
    61     80,7,6, 0,8,88, 0,8,24, 0,9,144,
    62     83,7,59, 0,8,120, 0,8,56, 0,9,208,
    63     81,7,17, 0,8,104, 0,8,40, 0,9,176,
    64     0,8,8, 0,8,136, 0,8,72, 0,9,240,
    65     80,7,4, 0,8,84, 0,8,20, 85,8,227,
    66     83,7,43, 0,8,116, 0,8,52, 0,9,200,
    67     81,7,13, 0,8,100, 0,8,36, 0,9,168,
    68     0,8,4, 0,8,132, 0,8,68, 0,9,232,
    69     80,7,8, 0,8,92, 0,8,28, 0,9,152,
    70     84,7,83, 0,8,124, 0,8,60, 0,9,216,
    71     82,7,23, 0,8,108, 0,8,44, 0,9,184,
    72     0,8,12, 0,8,140, 0,8,76, 0,9,248,
    73     80,7,3, 0,8,82, 0,8,18, 85,8,163,
    74     83,7,35, 0,8,114, 0,8,50, 0,9,196,
    75     81,7,11, 0,8,98, 0,8,34, 0,9,164,
    76     0,8,2, 0,8,130, 0,8,66, 0,9,228,
    77     80,7,7, 0,8,90, 0,8,26, 0,9,148,
    78     84,7,67, 0,8,122, 0,8,58, 0,9,212,
    79     82,7,19, 0,8,106, 0,8,42, 0,9,180,
    80     0,8,10, 0,8,138, 0,8,74, 0,9,244,
    81     80,7,5, 0,8,86, 0,8,22, 192,8,0,
    82     83,7,51, 0,8,118, 0,8,54, 0,9,204,
    83     81,7,15, 0,8,102, 0,8,38, 0,9,172,
    84     0,8,6, 0,8,134, 0,8,70, 0,9,236,
    85     80,7,9, 0,8,94, 0,8,30, 0,9,156,
    86     84,7,99, 0,8,126, 0,8,62, 0,9,220,
    87     82,7,27, 0,8,110, 0,8,46, 0,9,188,
    88     0,8,14, 0,8,142, 0,8,78, 0,9,252,
    89     96,7,256, 0,8,81, 0,8,17, 85,8,131,
    90     82,7,31, 0,8,113, 0,8,49, 0,9,194,
    91     80,7,10, 0,8,97, 0,8,33, 0,9,162,
    92     0,8,1, 0,8,129, 0,8,65, 0,9,226,
    93     80,7,6, 0,8,89, 0,8,25, 0,9,146,
    94     83,7,59, 0,8,121, 0,8,57, 0,9,210,
    95     81,7,17, 0,8,105, 0,8,41, 0,9,178,
    96     0,8,9, 0,8,137, 0,8,73, 0,9,242,
    97     80,7,4, 0,8,85, 0,8,21, 80,8,258,
    98     83,7,43, 0,8,117, 0,8,53, 0,9,202,
    99     81,7,13, 0,8,101, 0,8,37, 0,9,170,
   100     0,8,5, 0,8,133, 0,8,69, 0,9,234,
   101     80,7,8, 0,8,93, 0,8,29, 0,9,154,
   102     84,7,83, 0,8,125, 0,8,61, 0,9,218,
   103     82,7,23, 0,8,109, 0,8,45, 0,9,186,
   104     0,8,13, 0,8,141, 0,8,77, 0,9,250,
   105     80,7,3, 0,8,83, 0,8,19, 85,8,195,
   106     83,7,35, 0,8,115, 0,8,51, 0,9,198,
   107     81,7,11, 0,8,99, 0,8,35, 0,9,166,
   108     0,8,3, 0,8,131, 0,8,67, 0,9,230,
   109     80,7,7, 0,8,91, 0,8,27, 0,9,150,
   110     84,7,67, 0,8,123, 0,8,59, 0,9,214,
   111     82,7,19, 0,8,107, 0,8,43, 0,9,182,
   112     0,8,11, 0,8,139, 0,8,75, 0,9,246,
   113     80,7,5, 0,8,87, 0,8,23, 192,8,0,
   114     83,7,51, 0,8,119, 0,8,55, 0,9,206,
   115     81,7,15, 0,8,103, 0,8,39, 0,9,174,
   116     0,8,7, 0,8,135, 0,8,71, 0,9,238,
   117     80,7,9, 0,8,95, 0,8,31, 0,9,158,
   118     84,7,99, 0,8,127, 0,8,63, 0,9,222,
   119     82,7,27, 0,8,111, 0,8,47, 0,9,190,
   120     0,8,15, 0,8,143, 0,8,79, 0,9,254,
   121     96,7,256, 0,8,80, 0,8,16, 84,8,115,
   122     82,7,31, 0,8,112, 0,8,48, 0,9,193,
   123 
   124     80,7,10, 0,8,96, 0,8,32, 0,9,161,
   125     0,8,0, 0,8,128, 0,8,64, 0,9,225,
   126     80,7,6, 0,8,88, 0,8,24, 0,9,145,
   127     83,7,59, 0,8,120, 0,8,56, 0,9,209,
   128     81,7,17, 0,8,104, 0,8,40, 0,9,177,
   129     0,8,8, 0,8,136, 0,8,72, 0,9,241,
   130     80,7,4, 0,8,84, 0,8,20, 85,8,227,
   131     83,7,43, 0,8,116, 0,8,52, 0,9,201,
   132     81,7,13, 0,8,100, 0,8,36, 0,9,169,
   133     0,8,4, 0,8,132, 0,8,68, 0,9,233,
   134     80,7,8, 0,8,92, 0,8,28, 0,9,153,
   135     84,7,83, 0,8,124, 0,8,60, 0,9,217,
   136     82,7,23, 0,8,108, 0,8,44, 0,9,185,
   137     0,8,12, 0,8,140, 0,8,76, 0,9,249,
   138     80,7,3, 0,8,82, 0,8,18, 85,8,163,
   139     83,7,35, 0,8,114, 0,8,50, 0,9,197,
   140     81,7,11, 0,8,98, 0,8,34, 0,9,165,
   141     0,8,2, 0,8,130, 0,8,66, 0,9,229,
   142     80,7,7, 0,8,90, 0,8,26, 0,9,149,
   143     84,7,67, 0,8,122, 0,8,58, 0,9,213,
   144     82,7,19, 0,8,106, 0,8,42, 0,9,181,
   145     0,8,10, 0,8,138, 0,8,74, 0,9,245,
   146     80,7,5, 0,8,86, 0,8,22, 192,8,0,
   147     83,7,51, 0,8,118, 0,8,54, 0,9,205,
   148     81,7,15, 0,8,102, 0,8,38, 0,9,173,
   149     0,8,6, 0,8,134, 0,8,70, 0,9,237,
   150     80,7,9, 0,8,94, 0,8,30, 0,9,157,
   151     84,7,99, 0,8,126, 0,8,62, 0,9,221,
   152     82,7,27, 0,8,110, 0,8,46, 0,9,189,
   153     0,8,14, 0,8,142, 0,8,78, 0,9,253,
   154     96,7,256, 0,8,81, 0,8,17, 85,8,131,
   155     82,7,31, 0,8,113, 0,8,49, 0,9,195,
   156     80,7,10, 0,8,97, 0,8,33, 0,9,163,
   157     0,8,1, 0,8,129, 0,8,65, 0,9,227,
   158     80,7,6, 0,8,89, 0,8,25, 0,9,147,
   159     83,7,59, 0,8,121, 0,8,57, 0,9,211,
   160     81,7,17, 0,8,105, 0,8,41, 0,9,179,
   161     0,8,9, 0,8,137, 0,8,73, 0,9,243,
   162     80,7,4, 0,8,85, 0,8,21, 80,8,258,
   163     83,7,43, 0,8,117, 0,8,53, 0,9,203,
   164     81,7,13, 0,8,101, 0,8,37, 0,9,171,
   165     0,8,5, 0,8,133, 0,8,69, 0,9,235,
   166     80,7,8, 0,8,93, 0,8,29, 0,9,155,
   167     84,7,83, 0,8,125, 0,8,61, 0,9,219,
   168     82,7,23, 0,8,109, 0,8,45, 0,9,187,
   169     0,8,13, 0,8,141, 0,8,77, 0,9,251,
   170     80,7,3, 0,8,83, 0,8,19, 85,8,195,
   171     83,7,35, 0,8,115, 0,8,51, 0,9,199,
   172     81,7,11, 0,8,99, 0,8,35, 0,9,167,
   173     0,8,3, 0,8,131, 0,8,67, 0,9,231,
   174     80,7,7, 0,8,91, 0,8,27, 0,9,151,
   175     84,7,67, 0,8,123, 0,8,59, 0,9,215,
   176     82,7,19, 0,8,107, 0,8,43, 0,9,183,
   177     0,8,11, 0,8,139, 0,8,75, 0,9,247,
   178     80,7,5, 0,8,87, 0,8,23, 192,8,0,
   179     83,7,51, 0,8,119, 0,8,55, 0,9,207,
   180     81,7,15, 0,8,103, 0,8,39, 0,9,175,
   181     0,8,7, 0,8,135, 0,8,71, 0,9,239,
   182     80,7,9, 0,8,95, 0,8,31, 0,9,159,
   183     84,7,99, 0,8,127, 0,8,63, 0,9,223,
   184     82,7,27, 0,8,111, 0,8,47, 0,9,191,
   185     0,8,15, 0,8,143, 0,8,79, 0,9,255
   186   };
   187   static final int[] fixed_td = {
   188     80,5,1, 87,5,257, 83,5,17, 91,5,4097,
   189     81,5,5, 89,5,1025, 85,5,65, 93,5,16385,
   190     80,5,3, 88,5,513, 84,5,33, 92,5,8193,
   191     82,5,9, 90,5,2049, 86,5,129, 192,5,24577,
   192     80,5,2, 87,5,385, 83,5,25, 91,5,6145,
   193     81,5,7, 89,5,1537, 85,5,97, 93,5,24577,
   194     80,5,4, 88,5,769, 84,5,49, 92,5,12289,
   195     82,5,13, 90,5,3073, 86,5,193, 192,5,24577
   196   };
   197 
   198   // Tables for deflate from PKZIP's appnote.txt.
   199   static final int[] cplens = { // Copy lengths for literal codes 257..285
   200         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
   201         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
   202   };
   203 
   204   // see note #13 above about 258
   205   static final int[] cplext = { // Extra bits for literal codes 257..285
   206         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
   207         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112  // 112==invalid
   208   };
   209 
   210   static final int[] cpdist = { // Copy offsets for distance codes 0..29
   211         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
   212         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
   213         8193, 12289, 16385, 24577
   214   };
   215 
   216   static final int[] cpdext = { // Extra bits for distance codes
   217         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
   218         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
   219         12, 12, 13, 13};
   220 
   221   // If BMAX needs to be larger than 16, then h and x[] should be uLong.
   222   static final int BMAX=15;         // maximum bit length of any code
   223 
   224   int[] hn = null;  // hufts used in space
   225   int[] v = null;   // work area for huft_build 
   226   int[] c = null;   // bit length count table
   227   int[] r = null;   // table entry for structure assignment
   228   int[] u = null;   // table stack
   229   int[] x = null;   // bit offsets, then code stack
   230 
   231   private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX)
   232                          int bindex, 
   233                          int n,   // number of codes (assumed <= 288)
   234                          int s,   // number of simple-valued codes (0..s-1)
   235                          int[] d, // list of base values for non-simple codes
   236                          int[] e, // list of extra bits for non-simple codes
   237                          int[] t, // result: starting table
   238                          int[] m, // maximum lookup bits, returns actual
   239                          int[] hp,// space for trees
   240                          int[] hn,// hufts used in space
   241                          int[] v  // working area: values in order of bit length
   242                          ){
   243     // Given a list of code lengths and a maximum table size, make a set of
   244     // tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
   245     // if the given code set is incomplete (the tables are still built in this
   246     // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
   247     // lengths), or Z_MEM_ERROR if not enough memory.
   248 
   249     int a;                       // counter for codes of length k
   250     int f;                       // i repeats in table every f entries
   251     int g;                       // maximum code length
   252     int h;                       // table level
   253     int i;                       // counter, current code
   254     int j;                       // counter
   255     int k;                       // number of bits in current code
   256     int l;                       // bits per table (returned in m)
   257     int mask;                    // (1 << w) - 1, to avoid cc -O bug on HP
   258     int p;                       // pointer into c[], b[], or v[]
   259     int q;                       // points to current table
   260     int w;                       // bits before this table == (l * h)
   261     int xp;                      // pointer into x
   262     int y;                       // number of dummy codes added
   263     int z;                       // number of entries in current table
   264 
   265     // Generate counts for each bit length
   266 
   267     p = 0; i = n;
   268     do {
   269       c[b[bindex+p]]++; p++; i--;   // assume all entries <= BMAX
   270     }while(i!=0);
   271 
   272     if(c[0] == n){                // null input--all zero length codes
   273       t[0] = -1;
   274       m[0] = 0;
   275       return Z_OK;
   276     }
   277 
   278     // Find minimum and maximum length, bound *m by those
   279     l = m[0];
   280     for (j = 1; j <= BMAX; j++)
   281       if(c[j]!=0) break;
   282     k = j;                        // minimum code length
   283     if(l < j){
   284       l = j;
   285     }
   286     for (i = BMAX; i!=0; i--){
   287       if(c[i]!=0) break;
   288     }
   289     g = i;                        // maximum code length
   290     if(l > i){
   291       l = i;
   292     }
   293     m[0] = l;
   294 
   295     // Adjust last length count to fill out codes, if needed
   296     for (y = 1 << j; j < i; j++, y <<= 1){
   297       if ((y -= c[j]) < 0){
   298         return Z_DATA_ERROR;
   299       }
   300     }
   301     if ((y -= c[i]) < 0){
   302       return Z_DATA_ERROR;
   303     }
   304     c[i] += y;
   305 
   306     // Generate starting offsets into the value table for each length
   307     x[1] = j = 0;
   308     p = 1;  xp = 2;
   309     while (--i!=0) {                 // note that i == g from above
   310       x[xp] = (j += c[p]);
   311       xp++;
   312       p++;
   313     }
   314 
   315     // Make a table of values in order of bit lengths
   316     i = 0; p = 0;
   317     do {
   318       if ((j = b[bindex+p]) != 0){
   319         v[x[j]++] = i;
   320       }
   321       p++;
   322     }
   323     while (++i < n);
   324     n = x[g];                     // set n to length of v
   325 
   326     // Generate the Huffman codes and for each, make the table entries
   327     x[0] = i = 0;                 // first Huffman code is zero
   328     p = 0;                        // grab values in bit order
   329     h = -1;                       // no tables yet--level -1
   330     w = -l;                       // bits decoded == (l * h)
   331     u[0] = 0;                     // just to keep compilers happy
   332     q = 0;                        // ditto
   333     z = 0;                        // ditto
   334 
   335     // go through the bit lengths (k already is bits in shortest code)
   336     for (; k <= g; k++){
   337       a = c[k];
   338       while (a--!=0){
   339 	// here i is the Huffman code of length k bits for value *p
   340 	// make tables up to required level
   341         while (k > w + l){
   342           h++;
   343           w += l;                 // previous table always l bits
   344 	  // compute minimum size table less than or equal to l bits
   345           z = g - w;
   346           z = (z > l) ? l : z;        // table size upper limit
   347           if((f=1<<(j=k-w))>a+1){     // try a k-w bit table
   348                                       // too few codes for k-w bit table
   349             f -= a + 1;               // deduct codes from patterns left
   350             xp = k;
   351             if(j < z){
   352               while (++j < z){        // try smaller tables up to z bits
   353                 if((f <<= 1) <= c[++xp])
   354                   break;              // enough codes to use up j bits
   355                 f -= c[xp];           // else deduct codes from patterns
   356               }
   357 	    }
   358           }
   359           z = 1 << j;                 // table entries for j-bit table
   360 
   361 	  // allocate new table
   362           if (hn[0] + z > MANY){       // (note: doesn't matter for fixed)
   363             return Z_DATA_ERROR;       // overflow of MANY
   364           }
   365           u[h] = q = /*hp+*/ hn[0];   // DEBUG
   366           hn[0] += z;
   367  
   368 	  // connect to last table, if there is one
   369 	  if(h!=0){
   370             x[h]=i;           // save pattern for backing up
   371             r[0]=(byte)j;     // bits in this table
   372             r[1]=(byte)l;     // bits to dump before this table
   373             j=i>>>(w - l);
   374             r[2] = (int)(q - u[h-1] - j);               // offset to this table
   375             System.arraycopy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table
   376           }
   377           else{
   378             t[0] = q;               // first table is returned result
   379 	  }
   380         }
   381 
   382 	// set up table entry in r
   383         r[1] = (byte)(k - w);
   384         if (p >= n){
   385           r[0] = 128 + 64;      // out of values--invalid code
   386 	}
   387         else if (v[p] < s){
   388           r[0] = (byte)(v[p] < 256 ? 0 : 32 + 64);  // 256 is end-of-block
   389           r[2] = v[p++];          // simple code is just the value
   390         }
   391         else{
   392           r[0]=(byte)(e[v[p]-s]+16+64); // non-simple--look up in lists
   393           r[2]=d[v[p++] - s];
   394         }
   395 
   396         // fill code-like entries with r
   397         f=1<<(k-w);
   398         for (j=i>>>w;j<z;j+=f){
   399           System.arraycopy(r, 0, hp, (q+j)*3, 3);
   400 	}
   401 
   402 	// backwards increment the k-bit code i
   403         for (j = 1 << (k - 1); (i & j)!=0; j >>>= 1){
   404           i ^= j;
   405 	}
   406         i ^= j;
   407 
   408 	// backup over finished tables
   409         mask = (1 << w) - 1;      // needed on HP, cc -O bug
   410         while ((i & mask) != x[h]){
   411           h--;                    // don't need to update q
   412           w -= l;
   413           mask = (1 << w) - 1;
   414         }
   415       }
   416     }
   417     // Return Z_BUF_ERROR if we were given an incomplete table
   418     return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
   419   }
   420 
   421   int inflate_trees_bits(int[] c,  // 19 code lengths
   422                          int[] bb, // bits tree desired/actual depth
   423                          int[] tb, // bits tree result
   424                          int[] hp, // space for trees
   425                          ZStream z // for messages
   426                          ){
   427     int result;
   428     initWorkArea(19);
   429     hn[0]=0;
   430     result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
   431 
   432     if(result == Z_DATA_ERROR){
   433       z.msg = "oversubscribed dynamic bit lengths tree";
   434     }
   435     else if(result == Z_BUF_ERROR || bb[0] == 0){
   436       z.msg = "incomplete dynamic bit lengths tree";
   437       result = Z_DATA_ERROR;
   438     }
   439     return result;
   440   }
   441 
   442   int inflate_trees_dynamic(int nl,   // number of literal/length codes
   443                             int nd,   // number of distance codes
   444                             int[] c,  // that many (total) code lengths
   445                             int[] bl, // literal desired/actual bit depth
   446                             int[] bd, // distance desired/actual bit depth 
   447                             int[] tl, // literal/length tree result
   448                             int[] td, // distance tree result
   449                             int[] hp, // space for trees
   450                             ZStream z // for messages
   451                             ){
   452     int result;
   453 
   454     // build literal/length tree
   455     initWorkArea(288);
   456     hn[0]=0;
   457     result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
   458     if (result != Z_OK || bl[0] == 0){
   459       if(result == Z_DATA_ERROR){
   460         z.msg = "oversubscribed literal/length tree";
   461       }
   462       else if (result != Z_MEM_ERROR){
   463         z.msg = "incomplete literal/length tree";
   464         result = Z_DATA_ERROR;
   465       }
   466       return result;
   467     }
   468 
   469     // build distance tree
   470     initWorkArea(288);
   471     result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
   472 
   473     if (result != Z_OK || (bd[0] == 0 && nl > 257)){
   474       if (result == Z_DATA_ERROR){
   475         z.msg = "oversubscribed distance tree";
   476       }
   477       else if (result == Z_BUF_ERROR) {
   478         z.msg = "incomplete distance tree";
   479         result = Z_DATA_ERROR;
   480       }
   481       else if (result != Z_MEM_ERROR){
   482         z.msg = "empty distance tree with lengths";
   483         result = Z_DATA_ERROR;
   484       }
   485       return result;
   486     }
   487 
   488     return Z_OK;
   489   }
   490 
   491   static int inflate_trees_fixed(int[] bl,  //literal desired/actual bit depth
   492                                  int[] bd,  //distance desired/actual bit depth
   493                                  int[][] tl,//literal/length tree result
   494                                  int[][] td,//distance tree result 
   495                                  ZStream z  //for memory allocation
   496 				 ){
   497     bl[0]=fixed_bl;
   498     bd[0]=fixed_bd;
   499     tl[0]=fixed_tl;
   500     td[0]=fixed_td;
   501     return Z_OK;
   502   }
   503 
   504   private void initWorkArea(int vsize){
   505     if(hn==null){
   506       hn=new int[1];
   507       v=new int[vsize];
   508       c=new int[BMAX+1];
   509       r=new int[3];
   510       u=new int[BMAX];
   511       x=new int[BMAX+1];
   512     }
   513     if(v.length<vsize){ v=new int[vsize]; }
   514     for(int i=0; i<vsize; i++){v[i]=0;}
   515     for(int i=0; i<BMAX+1; i++){c[i]=0;}
   516     for(int i=0; i<3; i++){r[i]=0;}
   517     System.arraycopy(c, 0, u, 0, BMAX);
   518     System.arraycopy(c, 0, x, 0, BMAX+1);
   519   }
   520 }