Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/InfTree.java
8650 views
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */1/*2Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28/*29* This program is based on zlib-1.1.3, so all credit should go authors30* Jean-loup Gailly([email protected]) and Mark Adler([email protected])31* and contributors of zlib.32*/3334package com.jcraft.jzlib;3536final class InfTree{3738static final private int MANY=1440;3940static final private int Z_OK=0;41static final private int Z_STREAM_END=1;42static final private int Z_NEED_DICT=2;43static final private int Z_ERRNO=-1;44static final private int Z_STREAM_ERROR=-2;45static final private int Z_DATA_ERROR=-3;46static final private int Z_MEM_ERROR=-4;47static final private int Z_BUF_ERROR=-5;48static final private int Z_VERSION_ERROR=-6;4950static final int fixed_bl = 9;51static final int fixed_bd = 5;5253static final int[] fixed_tl = {5496,7,256, 0,8,80, 0,8,16, 84,8,115,5582,7,31, 0,8,112, 0,8,48, 0,9,192,5680,7,10, 0,8,96, 0,8,32, 0,9,160,570,8,0, 0,8,128, 0,8,64, 0,9,224,5880,7,6, 0,8,88, 0,8,24, 0,9,144,5983,7,59, 0,8,120, 0,8,56, 0,9,208,6081,7,17, 0,8,104, 0,8,40, 0,9,176,610,8,8, 0,8,136, 0,8,72, 0,9,240,6280,7,4, 0,8,84, 0,8,20, 85,8,227,6383,7,43, 0,8,116, 0,8,52, 0,9,200,6481,7,13, 0,8,100, 0,8,36, 0,9,168,650,8,4, 0,8,132, 0,8,68, 0,9,232,6680,7,8, 0,8,92, 0,8,28, 0,9,152,6784,7,83, 0,8,124, 0,8,60, 0,9,216,6882,7,23, 0,8,108, 0,8,44, 0,9,184,690,8,12, 0,8,140, 0,8,76, 0,9,248,7080,7,3, 0,8,82, 0,8,18, 85,8,163,7183,7,35, 0,8,114, 0,8,50, 0,9,196,7281,7,11, 0,8,98, 0,8,34, 0,9,164,730,8,2, 0,8,130, 0,8,66, 0,9,228,7480,7,7, 0,8,90, 0,8,26, 0,9,148,7584,7,67, 0,8,122, 0,8,58, 0,9,212,7682,7,19, 0,8,106, 0,8,42, 0,9,180,770,8,10, 0,8,138, 0,8,74, 0,9,244,7880,7,5, 0,8,86, 0,8,22, 192,8,0,7983,7,51, 0,8,118, 0,8,54, 0,9,204,8081,7,15, 0,8,102, 0,8,38, 0,9,172,810,8,6, 0,8,134, 0,8,70, 0,9,236,8280,7,9, 0,8,94, 0,8,30, 0,9,156,8384,7,99, 0,8,126, 0,8,62, 0,9,220,8482,7,27, 0,8,110, 0,8,46, 0,9,188,850,8,14, 0,8,142, 0,8,78, 0,9,252,8696,7,256, 0,8,81, 0,8,17, 85,8,131,8782,7,31, 0,8,113, 0,8,49, 0,9,194,8880,7,10, 0,8,97, 0,8,33, 0,9,162,890,8,1, 0,8,129, 0,8,65, 0,9,226,9080,7,6, 0,8,89, 0,8,25, 0,9,146,9183,7,59, 0,8,121, 0,8,57, 0,9,210,9281,7,17, 0,8,105, 0,8,41, 0,9,178,930,8,9, 0,8,137, 0,8,73, 0,9,242,9480,7,4, 0,8,85, 0,8,21, 80,8,258,9583,7,43, 0,8,117, 0,8,53, 0,9,202,9681,7,13, 0,8,101, 0,8,37, 0,9,170,970,8,5, 0,8,133, 0,8,69, 0,9,234,9880,7,8, 0,8,93, 0,8,29, 0,9,154,9984,7,83, 0,8,125, 0,8,61, 0,9,218,10082,7,23, 0,8,109, 0,8,45, 0,9,186,1010,8,13, 0,8,141, 0,8,77, 0,9,250,10280,7,3, 0,8,83, 0,8,19, 85,8,195,10383,7,35, 0,8,115, 0,8,51, 0,9,198,10481,7,11, 0,8,99, 0,8,35, 0,9,166,1050,8,3, 0,8,131, 0,8,67, 0,9,230,10680,7,7, 0,8,91, 0,8,27, 0,9,150,10784,7,67, 0,8,123, 0,8,59, 0,9,214,10882,7,19, 0,8,107, 0,8,43, 0,9,182,1090,8,11, 0,8,139, 0,8,75, 0,9,246,11080,7,5, 0,8,87, 0,8,23, 192,8,0,11183,7,51, 0,8,119, 0,8,55, 0,9,206,11281,7,15, 0,8,103, 0,8,39, 0,9,174,1130,8,7, 0,8,135, 0,8,71, 0,9,238,11480,7,9, 0,8,95, 0,8,31, 0,9,158,11584,7,99, 0,8,127, 0,8,63, 0,9,222,11682,7,27, 0,8,111, 0,8,47, 0,9,190,1170,8,15, 0,8,143, 0,8,79, 0,9,254,11896,7,256, 0,8,80, 0,8,16, 84,8,115,11982,7,31, 0,8,112, 0,8,48, 0,9,193,12012180,7,10, 0,8,96, 0,8,32, 0,9,161,1220,8,0, 0,8,128, 0,8,64, 0,9,225,12380,7,6, 0,8,88, 0,8,24, 0,9,145,12483,7,59, 0,8,120, 0,8,56, 0,9,209,12581,7,17, 0,8,104, 0,8,40, 0,9,177,1260,8,8, 0,8,136, 0,8,72, 0,9,241,12780,7,4, 0,8,84, 0,8,20, 85,8,227,12883,7,43, 0,8,116, 0,8,52, 0,9,201,12981,7,13, 0,8,100, 0,8,36, 0,9,169,1300,8,4, 0,8,132, 0,8,68, 0,9,233,13180,7,8, 0,8,92, 0,8,28, 0,9,153,13284,7,83, 0,8,124, 0,8,60, 0,9,217,13382,7,23, 0,8,108, 0,8,44, 0,9,185,1340,8,12, 0,8,140, 0,8,76, 0,9,249,13580,7,3, 0,8,82, 0,8,18, 85,8,163,13683,7,35, 0,8,114, 0,8,50, 0,9,197,13781,7,11, 0,8,98, 0,8,34, 0,9,165,1380,8,2, 0,8,130, 0,8,66, 0,9,229,13980,7,7, 0,8,90, 0,8,26, 0,9,149,14084,7,67, 0,8,122, 0,8,58, 0,9,213,14182,7,19, 0,8,106, 0,8,42, 0,9,181,1420,8,10, 0,8,138, 0,8,74, 0,9,245,14380,7,5, 0,8,86, 0,8,22, 192,8,0,14483,7,51, 0,8,118, 0,8,54, 0,9,205,14581,7,15, 0,8,102, 0,8,38, 0,9,173,1460,8,6, 0,8,134, 0,8,70, 0,9,237,14780,7,9, 0,8,94, 0,8,30, 0,9,157,14884,7,99, 0,8,126, 0,8,62, 0,9,221,14982,7,27, 0,8,110, 0,8,46, 0,9,189,1500,8,14, 0,8,142, 0,8,78, 0,9,253,15196,7,256, 0,8,81, 0,8,17, 85,8,131,15282,7,31, 0,8,113, 0,8,49, 0,9,195,15380,7,10, 0,8,97, 0,8,33, 0,9,163,1540,8,1, 0,8,129, 0,8,65, 0,9,227,15580,7,6, 0,8,89, 0,8,25, 0,9,147,15683,7,59, 0,8,121, 0,8,57, 0,9,211,15781,7,17, 0,8,105, 0,8,41, 0,9,179,1580,8,9, 0,8,137, 0,8,73, 0,9,243,15980,7,4, 0,8,85, 0,8,21, 80,8,258,16083,7,43, 0,8,117, 0,8,53, 0,9,203,16181,7,13, 0,8,101, 0,8,37, 0,9,171,1620,8,5, 0,8,133, 0,8,69, 0,9,235,16380,7,8, 0,8,93, 0,8,29, 0,9,155,16484,7,83, 0,8,125, 0,8,61, 0,9,219,16582,7,23, 0,8,109, 0,8,45, 0,9,187,1660,8,13, 0,8,141, 0,8,77, 0,9,251,16780,7,3, 0,8,83, 0,8,19, 85,8,195,16883,7,35, 0,8,115, 0,8,51, 0,9,199,16981,7,11, 0,8,99, 0,8,35, 0,9,167,1700,8,3, 0,8,131, 0,8,67, 0,9,231,17180,7,7, 0,8,91, 0,8,27, 0,9,151,17284,7,67, 0,8,123, 0,8,59, 0,9,215,17382,7,19, 0,8,107, 0,8,43, 0,9,183,1740,8,11, 0,8,139, 0,8,75, 0,9,247,17580,7,5, 0,8,87, 0,8,23, 192,8,0,17683,7,51, 0,8,119, 0,8,55, 0,9,207,17781,7,15, 0,8,103, 0,8,39, 0,9,175,1780,8,7, 0,8,135, 0,8,71, 0,9,239,17980,7,9, 0,8,95, 0,8,31, 0,9,159,18084,7,99, 0,8,127, 0,8,63, 0,9,223,18182,7,27, 0,8,111, 0,8,47, 0,9,191,1820,8,15, 0,8,143, 0,8,79, 0,9,255183};184static final int[] fixed_td = {18580,5,1, 87,5,257, 83,5,17, 91,5,4097,18681,5,5, 89,5,1025, 85,5,65, 93,5,16385,18780,5,3, 88,5,513, 84,5,33, 92,5,8193,18882,5,9, 90,5,2049, 86,5,129, 192,5,24577,18980,5,2, 87,5,385, 83,5,25, 91,5,6145,19081,5,7, 89,5,1537, 85,5,97, 93,5,24577,19180,5,4, 88,5,769, 84,5,49, 92,5,12289,19282,5,13, 90,5,3073, 86,5,193, 192,5,24577193};194195// Tables for deflate from PKZIP's appnote.txt.196static final int[] cplens = { // Copy lengths for literal codes 257..2851973, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,19835, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0199};200201// see note #13 above about 258202static final int[] cplext = { // Extra bits for literal codes 257..2852030, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,2043, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 // 112==invalid205};206207static final int[] cpdist = { // Copy offsets for distance codes 0..292081, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,209257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,2108193, 12289, 16385, 24577211};212213static final int[] cpdext = { // Extra bits for distance codes2140, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,2157, 7, 8, 8, 9, 9, 10, 10, 11, 11,21612, 12, 13, 13};217218// If BMAX needs to be larger than 16, then h and x[] should be uLong.219static final int BMAX=15; // maximum bit length of any code220221int[] hn = null; // hufts used in space222int[] v = null; // work area for huft_build223int[] c = null; // bit length count table224int[] r = null; // table entry for structure assignment225int[] u = null; // table stack226int[] x = null; // bit offsets, then code stack227228private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX)229int bindex,230int n, // number of codes (assumed <= 288)231int s, // number of simple-valued codes (0..s-1)232int[] d, // list of base values for non-simple codes233int[] e, // list of extra bits for non-simple codes234int[] t, // result: starting table235int[] m, // maximum lookup bits, returns actual236int[] hp,// space for trees237int[] hn,// hufts used in space238int[] v // working area: values in order of bit length239){240// Given a list of code lengths and a maximum table size, make a set of241// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR242// if the given code set is incomplete (the tables are still built in this243// case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of244// lengths), or Z_MEM_ERROR if not enough memory.245246int a; // counter for codes of length k247int f; // i repeats in table every f entries248int g; // maximum code length249int h; // table level250int i; // counter, current code251int j; // counter252int k; // number of bits in current code253int l; // bits per table (returned in m)254int mask; // (1 << w) - 1, to avoid cc -O bug on HP255int p; // pointer into c[], b[], or v[]256int q; // points to current table257int w; // bits before this table == (l * h)258int xp; // pointer into x259int y; // number of dummy codes added260int z; // number of entries in current table261262// Generate counts for each bit length263264p = 0; i = n;265do {266c[b[bindex+p]]++; p++; i--; // assume all entries <= BMAX267}while(i!=0);268269if(c[0] == n){ // null input--all zero length codes270t[0] = -1;271m[0] = 0;272return Z_OK;273}274275// Find minimum and maximum length, bound *m by those276l = m[0];277for (j = 1; j <= BMAX; j++)278if(c[j]!=0) break;279k = j; // minimum code length280if(l < j){281l = j;282}283for (i = BMAX; i!=0; i--){284if(c[i]!=0) break;285}286g = i; // maximum code length287if(l > i){288l = i;289}290m[0] = l;291292// Adjust last length count to fill out codes, if needed293for (y = 1 << j; j < i; j++, y <<= 1){294if ((y -= c[j]) < 0){295return Z_DATA_ERROR;296}297}298if ((y -= c[i]) < 0){299return Z_DATA_ERROR;300}301c[i] += y;302303// Generate starting offsets into the value table for each length304x[1] = j = 0;305p = 1; xp = 2;306while (--i!=0) { // note that i == g from above307x[xp] = (j += c[p]);308xp++;309p++;310}311312// Make a table of values in order of bit lengths313i = 0; p = 0;314do {315if ((j = b[bindex+p]) != 0){316v[x[j]++] = i;317}318p++;319}320while (++i < n);321n = x[g]; // set n to length of v322323// Generate the Huffman codes and for each, make the table entries324x[0] = i = 0; // first Huffman code is zero325p = 0; // grab values in bit order326h = -1; // no tables yet--level -1327w = -l; // bits decoded == (l * h)328u[0] = 0; // just to keep compilers happy329q = 0; // ditto330z = 0; // ditto331332// go through the bit lengths (k already is bits in shortest code)333for (; k <= g; k++){334a = c[k];335while (a--!=0){336// here i is the Huffman code of length k bits for value *p337// make tables up to required level338while (k > w + l){339h++;340w += l; // previous table always l bits341// compute minimum size table less than or equal to l bits342z = g - w;343z = (z > l) ? l : z; // table size upper limit344if((f=1<<(j=k-w))>a+1){ // try a k-w bit table345// too few codes for k-w bit table346f -= a + 1; // deduct codes from patterns left347xp = k;348if(j < z){349while (++j < z){ // try smaller tables up to z bits350if((f <<= 1) <= c[++xp])351break; // enough codes to use up j bits352f -= c[xp]; // else deduct codes from patterns353}354}355}356z = 1 << j; // table entries for j-bit table357358// allocate new table359if (hn[0] + z > MANY){ // (note: doesn't matter for fixed)360return Z_DATA_ERROR; // overflow of MANY361}362u[h] = q = /*hp+*/ hn[0]; // DEBUG363hn[0] += z;364365// connect to last table, if there is one366if(h!=0){367x[h]=i; // save pattern for backing up368r[0]=(byte)j; // bits in this table369r[1]=(byte)l; // bits to dump before this table370j=i>>>(w - l);371r[2] = (int)(q - u[h-1] - j); // offset to this table372System.arraycopy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table373}374else{375t[0] = q; // first table is returned result376}377}378379// set up table entry in r380r[1] = (byte)(k - w);381if (p >= n){382r[0] = 128 + 64; // out of values--invalid code383}384else if (v[p] < s){385r[0] = (byte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block386r[2] = v[p++]; // simple code is just the value387}388else{389r[0]=(byte)(e[v[p]-s]+16+64); // non-simple--look up in lists390r[2]=d[v[p++] - s];391}392393// fill code-like entries with r394f=1<<(k-w);395for (j=i>>>w;j<z;j+=f){396System.arraycopy(r, 0, hp, (q+j)*3, 3);397}398399// backwards increment the k-bit code i400for (j = 1 << (k - 1); (i & j)!=0; j >>>= 1){401i ^= j;402}403i ^= j;404405// backup over finished tables406mask = (1 << w) - 1; // needed on HP, cc -O bug407while ((i & mask) != x[h]){408h--; // don't need to update q409w -= l;410mask = (1 << w) - 1;411}412}413}414// Return Z_BUF_ERROR if we were given an incomplete table415return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;416}417418int inflate_trees_bits(int[] c, // 19 code lengths419int[] bb, // bits tree desired/actual depth420int[] tb, // bits tree result421int[] hp, // space for trees422ZStream z // for messages423){424int result;425initWorkArea(19);426hn[0]=0;427result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);428429if(result == Z_DATA_ERROR){430z.msg = "oversubscribed dynamic bit lengths tree";431}432else if(result == Z_BUF_ERROR || bb[0] == 0){433z.msg = "incomplete dynamic bit lengths tree";434result = Z_DATA_ERROR;435}436return result;437}438439int inflate_trees_dynamic(int nl, // number of literal/length codes440int nd, // number of distance codes441int[] c, // that many (total) code lengths442int[] bl, // literal desired/actual bit depth443int[] bd, // distance desired/actual bit depth444int[] tl, // literal/length tree result445int[] td, // distance tree result446int[] hp, // space for trees447ZStream z // for messages448){449int result;450451// build literal/length tree452initWorkArea(288);453hn[0]=0;454result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);455if (result != Z_OK || bl[0] == 0){456if(result == Z_DATA_ERROR){457z.msg = "oversubscribed literal/length tree";458}459else if (result != Z_MEM_ERROR){460z.msg = "incomplete literal/length tree";461result = Z_DATA_ERROR;462}463return result;464}465466// build distance tree467initWorkArea(288);468result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);469470if (result != Z_OK || (bd[0] == 0 && nl > 257)){471if (result == Z_DATA_ERROR){472z.msg = "oversubscribed distance tree";473}474else if (result == Z_BUF_ERROR) {475z.msg = "incomplete distance tree";476result = Z_DATA_ERROR;477}478else if (result != Z_MEM_ERROR){479z.msg = "empty distance tree with lengths";480result = Z_DATA_ERROR;481}482return result;483}484485return Z_OK;486}487488static int inflate_trees_fixed(int[] bl, //literal desired/actual bit depth489int[] bd, //distance desired/actual bit depth490int[][] tl,//literal/length tree result491int[][] td,//distance tree result492ZStream z //for memory allocation493){494bl[0]=fixed_bl;495bd[0]=fixed_bd;496tl[0]=fixed_tl;497td[0]=fixed_td;498return Z_OK;499}500501private void initWorkArea(int vsize){502if(hn==null){503hn=new int[1];504v=new int[vsize];505c=new int[BMAX+1];506r=new int[3];507u=new int[BMAX];508x=new int[BMAX+1];509}510if(v.length<vsize){ v=new int[vsize]; }511for(int i=0; i<vsize; i++){v[i]=0;}512for(int i=0; i<BMAX+1; i++){c[i]=0;}513for(int i=0; i<3; i++){r[i]=0;}514System.arraycopy(c, 0, u, 0, BMAX);515System.arraycopy(c, 0, x, 0, BMAX+1);516}517}518519520