xref: /onnv-gate/usr/src/uts/common/zmod/inftrees.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * inftrees.c -- generate Huffman trees for efficient decoding
10*0Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
11*0Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
12*0Sstevel@tonic-gate  */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #include "zutil.h"
15*0Sstevel@tonic-gate #include "inftrees.h"
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate local const uInt fixed_bl = 9;
18*0Sstevel@tonic-gate local const uInt fixed_bd = 5;
19*0Sstevel@tonic-gate local const inflate_huft fixed_tl[] = {
20*0Sstevel@tonic-gate     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
21*0Sstevel@tonic-gate     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
22*0Sstevel@tonic-gate     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
23*0Sstevel@tonic-gate     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
24*0Sstevel@tonic-gate     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
25*0Sstevel@tonic-gate     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
26*0Sstevel@tonic-gate     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
27*0Sstevel@tonic-gate     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
28*0Sstevel@tonic-gate     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
29*0Sstevel@tonic-gate     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
30*0Sstevel@tonic-gate     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
31*0Sstevel@tonic-gate     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
32*0Sstevel@tonic-gate     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
33*0Sstevel@tonic-gate     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
34*0Sstevel@tonic-gate     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
35*0Sstevel@tonic-gate     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
36*0Sstevel@tonic-gate     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
37*0Sstevel@tonic-gate     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
38*0Sstevel@tonic-gate     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
39*0Sstevel@tonic-gate     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
40*0Sstevel@tonic-gate     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
41*0Sstevel@tonic-gate     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
42*0Sstevel@tonic-gate     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
43*0Sstevel@tonic-gate     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
44*0Sstevel@tonic-gate     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
45*0Sstevel@tonic-gate     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
46*0Sstevel@tonic-gate     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
47*0Sstevel@tonic-gate     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
48*0Sstevel@tonic-gate     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
49*0Sstevel@tonic-gate     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
50*0Sstevel@tonic-gate     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
51*0Sstevel@tonic-gate     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
52*0Sstevel@tonic-gate     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
53*0Sstevel@tonic-gate     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
54*0Sstevel@tonic-gate     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
55*0Sstevel@tonic-gate     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
56*0Sstevel@tonic-gate     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
57*0Sstevel@tonic-gate     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
58*0Sstevel@tonic-gate     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
59*0Sstevel@tonic-gate     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
60*0Sstevel@tonic-gate     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
61*0Sstevel@tonic-gate     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
62*0Sstevel@tonic-gate     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
63*0Sstevel@tonic-gate     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
64*0Sstevel@tonic-gate     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
65*0Sstevel@tonic-gate     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
66*0Sstevel@tonic-gate     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
67*0Sstevel@tonic-gate     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
68*0Sstevel@tonic-gate     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
69*0Sstevel@tonic-gate     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
70*0Sstevel@tonic-gate     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
71*0Sstevel@tonic-gate     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
72*0Sstevel@tonic-gate     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
73*0Sstevel@tonic-gate     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
74*0Sstevel@tonic-gate     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
75*0Sstevel@tonic-gate     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
76*0Sstevel@tonic-gate     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
77*0Sstevel@tonic-gate     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
78*0Sstevel@tonic-gate     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
79*0Sstevel@tonic-gate     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
80*0Sstevel@tonic-gate     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
81*0Sstevel@tonic-gate     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
82*0Sstevel@tonic-gate     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
83*0Sstevel@tonic-gate     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
84*0Sstevel@tonic-gate     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
85*0Sstevel@tonic-gate     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
86*0Sstevel@tonic-gate     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
87*0Sstevel@tonic-gate     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
88*0Sstevel@tonic-gate     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
89*0Sstevel@tonic-gate     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
90*0Sstevel@tonic-gate     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
91*0Sstevel@tonic-gate     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
92*0Sstevel@tonic-gate     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
93*0Sstevel@tonic-gate     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
94*0Sstevel@tonic-gate     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
95*0Sstevel@tonic-gate     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
96*0Sstevel@tonic-gate     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
97*0Sstevel@tonic-gate     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
98*0Sstevel@tonic-gate     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
99*0Sstevel@tonic-gate     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
100*0Sstevel@tonic-gate     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
101*0Sstevel@tonic-gate     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
102*0Sstevel@tonic-gate     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
103*0Sstevel@tonic-gate     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
104*0Sstevel@tonic-gate     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
105*0Sstevel@tonic-gate     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
106*0Sstevel@tonic-gate     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
107*0Sstevel@tonic-gate     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
108*0Sstevel@tonic-gate     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
109*0Sstevel@tonic-gate     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
110*0Sstevel@tonic-gate     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
111*0Sstevel@tonic-gate     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
112*0Sstevel@tonic-gate     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
113*0Sstevel@tonic-gate     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
114*0Sstevel@tonic-gate     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
115*0Sstevel@tonic-gate     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
116*0Sstevel@tonic-gate     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
117*0Sstevel@tonic-gate     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
118*0Sstevel@tonic-gate     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
119*0Sstevel@tonic-gate     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
120*0Sstevel@tonic-gate     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
121*0Sstevel@tonic-gate     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
122*0Sstevel@tonic-gate     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
123*0Sstevel@tonic-gate     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
124*0Sstevel@tonic-gate     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
125*0Sstevel@tonic-gate     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
126*0Sstevel@tonic-gate     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
127*0Sstevel@tonic-gate     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
128*0Sstevel@tonic-gate     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
129*0Sstevel@tonic-gate     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
130*0Sstevel@tonic-gate     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
131*0Sstevel@tonic-gate     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
132*0Sstevel@tonic-gate     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
133*0Sstevel@tonic-gate     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
134*0Sstevel@tonic-gate     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
135*0Sstevel@tonic-gate     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
136*0Sstevel@tonic-gate     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
137*0Sstevel@tonic-gate     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
138*0Sstevel@tonic-gate     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
139*0Sstevel@tonic-gate     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
140*0Sstevel@tonic-gate     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
141*0Sstevel@tonic-gate     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
142*0Sstevel@tonic-gate     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
143*0Sstevel@tonic-gate     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
144*0Sstevel@tonic-gate     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
145*0Sstevel@tonic-gate     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
146*0Sstevel@tonic-gate     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
147*0Sstevel@tonic-gate     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
148*0Sstevel@tonic-gate };
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate local const inflate_huft fixed_td[] = {
151*0Sstevel@tonic-gate     {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
152*0Sstevel@tonic-gate     {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
153*0Sstevel@tonic-gate     {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
154*0Sstevel@tonic-gate     {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
155*0Sstevel@tonic-gate     {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
156*0Sstevel@tonic-gate     {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
157*0Sstevel@tonic-gate     {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
158*0Sstevel@tonic-gate     {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
159*0Sstevel@tonic-gate };
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
162*0Sstevel@tonic-gate #define exop word.what.Exop
163*0Sstevel@tonic-gate #define bits word.what.Bits
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate local int huft_build OF((
167*0Sstevel@tonic-gate     uIntf *,            /* code lengths in bits */
168*0Sstevel@tonic-gate     uInt,               /* number of codes */
169*0Sstevel@tonic-gate     uInt,               /* number of "simple" codes */
170*0Sstevel@tonic-gate     const uIntf *,      /* list of base values for non-simple codes */
171*0Sstevel@tonic-gate     const uIntf *,      /* list of extra bits for non-simple codes */
172*0Sstevel@tonic-gate     inflate_huft * FAR*,/* result: starting table */
173*0Sstevel@tonic-gate     uIntf *,            /* maximum lookup bits (returns actual) */
174*0Sstevel@tonic-gate     inflate_huft *,     /* space for trees */
175*0Sstevel@tonic-gate     uInt *,             /* hufts used in space */
176*0Sstevel@tonic-gate     uIntf * ));         /* space for values */
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate /* Tables for deflate from PKZIP's appnote.txt. */
179*0Sstevel@tonic-gate local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
180*0Sstevel@tonic-gate         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
181*0Sstevel@tonic-gate         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
182*0Sstevel@tonic-gate         /* see note #13 above about 258 */
183*0Sstevel@tonic-gate local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
184*0Sstevel@tonic-gate         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
185*0Sstevel@tonic-gate         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
186*0Sstevel@tonic-gate local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
187*0Sstevel@tonic-gate         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
188*0Sstevel@tonic-gate         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
189*0Sstevel@tonic-gate         8193, 12289, 16385, 24577};
190*0Sstevel@tonic-gate local const uInt cpdext[30] = { /* Extra bits for distance codes */
191*0Sstevel@tonic-gate         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
192*0Sstevel@tonic-gate         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
193*0Sstevel@tonic-gate         12, 12, 13, 13};
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate    Huffman code decoding is performed using a multi-level table lookup.
197*0Sstevel@tonic-gate    The fastest way to decode is to simply build a lookup table whose
198*0Sstevel@tonic-gate    size is determined by the longest code.  However, the time it takes
199*0Sstevel@tonic-gate    to build this table can also be a factor if the data being decoded
200*0Sstevel@tonic-gate    is not very long.  The most common codes are necessarily the
201*0Sstevel@tonic-gate    shortest codes, so those codes dominate the decoding time, and hence
202*0Sstevel@tonic-gate    the speed.  The idea is you can have a shorter table that decodes the
203*0Sstevel@tonic-gate    shorter, more probable codes, and then point to subsidiary tables for
204*0Sstevel@tonic-gate    the longer codes.  The time it costs to decode the longer codes is
205*0Sstevel@tonic-gate    then traded against the time it takes to make longer tables.
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate    This results of this trade are in the variables lbits and dbits
208*0Sstevel@tonic-gate    below.  lbits is the number of bits the first level table for literal/
209*0Sstevel@tonic-gate    length codes can decode in one step, and dbits is the same thing for
210*0Sstevel@tonic-gate    the distance codes.  Subsequent tables are also less than or equal to
211*0Sstevel@tonic-gate    those sizes.  These values may be adjusted either when all of the
212*0Sstevel@tonic-gate    codes are shorter than that, in which case the longest code length in
213*0Sstevel@tonic-gate    bits is used, or when the shortest code is *longer* than the requested
214*0Sstevel@tonic-gate    table size, in which case the length of the shortest code in bits is
215*0Sstevel@tonic-gate    used.
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate    There are two different values for the two tables, since they code a
218*0Sstevel@tonic-gate    different number of possibilities each.  The literal/length table
219*0Sstevel@tonic-gate    codes 286 possible values, or in a flat code, a little over eight
220*0Sstevel@tonic-gate    bits.  The distance table codes 30 possible values, or a little less
221*0Sstevel@tonic-gate    than five bits, flat.  The optimum values for speed end up being
222*0Sstevel@tonic-gate    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
223*0Sstevel@tonic-gate    The optimum values may differ though from machine to machine, and
224*0Sstevel@tonic-gate    possibly even between compilers.  Your mileage may vary.
225*0Sstevel@tonic-gate  */
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
229*0Sstevel@tonic-gate #define BMAX 15         /* maximum bit length of any code */
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
232*0Sstevel@tonic-gate uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
233*0Sstevel@tonic-gate uInt n;                 /* number of codes (assumed <= 288) */
234*0Sstevel@tonic-gate uInt s;                 /* number of simple-valued codes (0..s-1) */
235*0Sstevel@tonic-gate const uIntf *d;         /* list of base values for non-simple codes */
236*0Sstevel@tonic-gate const uIntf *e;         /* list of extra bits for non-simple codes */
237*0Sstevel@tonic-gate inflate_huft * FAR *t;  /* result: starting table */
238*0Sstevel@tonic-gate uIntf *m;               /* maximum lookup bits, returns actual */
239*0Sstevel@tonic-gate inflate_huft *hp;       /* space for trees */
240*0Sstevel@tonic-gate uInt *hn;               /* hufts used in space */
241*0Sstevel@tonic-gate uIntf *v;               /* working area: values in order of bit length */
242*0Sstevel@tonic-gate /* Given a list of code lengths and a maximum table size, make a set of
243*0Sstevel@tonic-gate    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
244*0Sstevel@tonic-gate    if the given code set is incomplete (the tables are still built in this
245*0Sstevel@tonic-gate    case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
246*0Sstevel@tonic-gate    lengths), or Z_MEM_ERROR if not enough memory. */
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate   uInt a;                       /* counter for codes of length k */
250*0Sstevel@tonic-gate   uInt c[BMAX+1];               /* bit length count table */
251*0Sstevel@tonic-gate   uInt f;                       /* i repeats in table every f entries */
252*0Sstevel@tonic-gate   int g;                        /* maximum code length */
253*0Sstevel@tonic-gate   int h;                        /* table level */
254*0Sstevel@tonic-gate   register uInt i;              /* counter, current code */
255*0Sstevel@tonic-gate   register uInt j;              /* counter */
256*0Sstevel@tonic-gate   register int k;               /* number of bits in current code */
257*0Sstevel@tonic-gate   int l;                        /* bits per table (returned in m) */
258*0Sstevel@tonic-gate   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
259*0Sstevel@tonic-gate   register uIntf *p;            /* pointer into c[], b[], or v[] */
260*0Sstevel@tonic-gate   inflate_huft *q;              /* points to current table */
261*0Sstevel@tonic-gate   struct inflate_huft_s r;      /* table entry for structure assignment */
262*0Sstevel@tonic-gate   inflate_huft *u[BMAX];        /* table stack */
263*0Sstevel@tonic-gate   register int w;               /* bits before this table == (l * h) */
264*0Sstevel@tonic-gate   uInt x[BMAX+1];               /* bit offsets, then code stack */
265*0Sstevel@tonic-gate   uIntf *xp;                    /* pointer into x */
266*0Sstevel@tonic-gate   int y;                        /* number of dummy codes added */
267*0Sstevel@tonic-gate   uInt z;                       /* number of entries in current table */
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate   /* Generate counts for each bit length */
271*0Sstevel@tonic-gate   p = c;
272*0Sstevel@tonic-gate #define C0 *p++ = 0;
273*0Sstevel@tonic-gate #define C2 C0 C0 C0 C0
274*0Sstevel@tonic-gate #define C4 C2 C2 C2 C2
275*0Sstevel@tonic-gate   C4                            /* clear c[]--assume BMAX+1 is 16 */
276*0Sstevel@tonic-gate   p = b;  i = n;
277*0Sstevel@tonic-gate   do {
278*0Sstevel@tonic-gate     c[*p++]++;                  /* assume all entries <= BMAX */
279*0Sstevel@tonic-gate   } while (--i);
280*0Sstevel@tonic-gate   if (c[0] == n)                /* null input--all zero length codes */
281*0Sstevel@tonic-gate   {
282*0Sstevel@tonic-gate     *t = (inflate_huft *)Z_NULL;
283*0Sstevel@tonic-gate     *m = 0;
284*0Sstevel@tonic-gate     return Z_OK;
285*0Sstevel@tonic-gate   }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate   /* Find minimum and maximum length, bound *m by those */
289*0Sstevel@tonic-gate   l = *m;
290*0Sstevel@tonic-gate   for (j = 1; j <= BMAX; j++)
291*0Sstevel@tonic-gate     if (c[j])
292*0Sstevel@tonic-gate       break;
293*0Sstevel@tonic-gate   k = j;                        /* minimum code length */
294*0Sstevel@tonic-gate   if ((uInt)l < j)
295*0Sstevel@tonic-gate     l = j;
296*0Sstevel@tonic-gate   for (i = BMAX; i; i--)
297*0Sstevel@tonic-gate     if (c[i])
298*0Sstevel@tonic-gate       break;
299*0Sstevel@tonic-gate   g = i;                        /* maximum code length */
300*0Sstevel@tonic-gate   if ((uInt)l > i)
301*0Sstevel@tonic-gate     l = i;
302*0Sstevel@tonic-gate   *m = l;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate   /* Adjust last length count to fill out codes, if needed */
306*0Sstevel@tonic-gate   for (y = 1 << j; j < i; j++, y <<= 1)
307*0Sstevel@tonic-gate     if ((y -= c[j]) < 0)
308*0Sstevel@tonic-gate       return Z_DATA_ERROR;
309*0Sstevel@tonic-gate   if ((y -= c[i]) < 0)
310*0Sstevel@tonic-gate     return Z_DATA_ERROR;
311*0Sstevel@tonic-gate   c[i] += y;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate   /* Generate starting offsets into the value table for each length */
315*0Sstevel@tonic-gate   x[1] = j = 0;
316*0Sstevel@tonic-gate   p = c + 1;  xp = x + 2;
317*0Sstevel@tonic-gate   while (--i) {                 /* note that i == g from above */
318*0Sstevel@tonic-gate     *xp++ = (j += *p++);
319*0Sstevel@tonic-gate   }
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate   /* Make a table of values in order of bit lengths */
323*0Sstevel@tonic-gate   p = b;  i = 0;
324*0Sstevel@tonic-gate   do {
325*0Sstevel@tonic-gate     if ((j = *p++) != 0)
326*0Sstevel@tonic-gate       v[x[j]++] = i;
327*0Sstevel@tonic-gate   } while (++i < n);
328*0Sstevel@tonic-gate   n = x[g];                     /* set n to length of v */
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate   /* Generate the Huffman codes and for each, make the table entries */
332*0Sstevel@tonic-gate   x[0] = i = 0;                 /* first Huffman code is zero */
333*0Sstevel@tonic-gate   p = v;                        /* grab values in bit order */
334*0Sstevel@tonic-gate   h = -1;                       /* no tables yet--level -1 */
335*0Sstevel@tonic-gate   w = -l;                       /* bits decoded == (l * h) */
336*0Sstevel@tonic-gate   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
337*0Sstevel@tonic-gate   q = (inflate_huft *)Z_NULL;   /* ditto */
338*0Sstevel@tonic-gate   z = 0;                        /* ditto */
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate   /* go through the bit lengths (k already is bits in shortest code) */
341*0Sstevel@tonic-gate   for (; k <= g; k++)
342*0Sstevel@tonic-gate   {
343*0Sstevel@tonic-gate     a = c[k];
344*0Sstevel@tonic-gate     while (a--)
345*0Sstevel@tonic-gate     {
346*0Sstevel@tonic-gate       /* here i is the Huffman code of length k bits for value *p */
347*0Sstevel@tonic-gate       /* make tables up to required level */
348*0Sstevel@tonic-gate       while (k > w + l)
349*0Sstevel@tonic-gate       {
350*0Sstevel@tonic-gate         h++;
351*0Sstevel@tonic-gate         w += l;                 /* previous table always l bits */
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate         /* compute minimum size table less than or equal to l bits */
354*0Sstevel@tonic-gate         z = g - w;
355*0Sstevel@tonic-gate         z = z > (uInt)l ? l : z;        /* table size upper limit */
356*0Sstevel@tonic-gate         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
357*0Sstevel@tonic-gate         {                       /* too few codes for k-w bit table */
358*0Sstevel@tonic-gate           f -= a + 1;           /* deduct codes from patterns left */
359*0Sstevel@tonic-gate           xp = c + k;
360*0Sstevel@tonic-gate           if (j < z)
361*0Sstevel@tonic-gate             while (++j < z)     /* try smaller tables up to z bits */
362*0Sstevel@tonic-gate             {
363*0Sstevel@tonic-gate               if ((f <<= 1) <= *++xp)
364*0Sstevel@tonic-gate                 break;          /* enough codes to use up j bits */
365*0Sstevel@tonic-gate               f -= *xp;         /* else deduct codes from patterns */
366*0Sstevel@tonic-gate             }
367*0Sstevel@tonic-gate         }
368*0Sstevel@tonic-gate         z = 1 << j;             /* table entries for j-bit table */
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate         /* allocate new table */
371*0Sstevel@tonic-gate         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
372*0Sstevel@tonic-gate           return Z_MEM_ERROR;   /* not enough memory */
373*0Sstevel@tonic-gate         u[h] = q = hp + *hn;
374*0Sstevel@tonic-gate         *hn += z;
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate         /* connect to last table, if there is one */
377*0Sstevel@tonic-gate         if (h)
378*0Sstevel@tonic-gate         {
379*0Sstevel@tonic-gate           x[h] = i;             /* save pattern for backing up */
380*0Sstevel@tonic-gate           r.bits = (Byte)l;     /* bits to dump before this table */
381*0Sstevel@tonic-gate           r.exop = (Byte)j;     /* bits in this table */
382*0Sstevel@tonic-gate           j = i >> (w - l);
383*0Sstevel@tonic-gate           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
384*0Sstevel@tonic-gate           u[h-1][j] = r;        /* connect to last table */
385*0Sstevel@tonic-gate         }
386*0Sstevel@tonic-gate         else
387*0Sstevel@tonic-gate           *t = q;               /* first table is returned result */
388*0Sstevel@tonic-gate       }
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate       /* set up table entry in r */
391*0Sstevel@tonic-gate       r.bits = (Byte)(k - w);
392*0Sstevel@tonic-gate       if (p >= v + n)
393*0Sstevel@tonic-gate         r.exop = 128 + 64;      /* out of values--invalid code */
394*0Sstevel@tonic-gate       else if (*p < s)
395*0Sstevel@tonic-gate       {
396*0Sstevel@tonic-gate         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
397*0Sstevel@tonic-gate         r.base = *p++;          /* simple code is just the value */
398*0Sstevel@tonic-gate       }
399*0Sstevel@tonic-gate       else
400*0Sstevel@tonic-gate       {
401*0Sstevel@tonic-gate         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
402*0Sstevel@tonic-gate         r.base = d[*p++ - s];
403*0Sstevel@tonic-gate       }
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate       /* fill code-like entries with r */
406*0Sstevel@tonic-gate       f = 1 << (k - w);
407*0Sstevel@tonic-gate       for (j = i >> w; j < z; j += f)
408*0Sstevel@tonic-gate         q[j] = r;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate       /* backwards increment the k-bit code i */
411*0Sstevel@tonic-gate       for (j = 1 << (k - 1); i & j; j >>= 1)
412*0Sstevel@tonic-gate         i ^= j;
413*0Sstevel@tonic-gate       i ^= j;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate       /* backup over finished tables */
416*0Sstevel@tonic-gate       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
417*0Sstevel@tonic-gate       while ((i & mask) != x[h])
418*0Sstevel@tonic-gate       {
419*0Sstevel@tonic-gate         h--;                    /* don't need to update q */
420*0Sstevel@tonic-gate         w -= l;
421*0Sstevel@tonic-gate         mask = (1 << w) - 1;
422*0Sstevel@tonic-gate       }
423*0Sstevel@tonic-gate     }
424*0Sstevel@tonic-gate   }
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate   /* Return Z_BUF_ERROR if we were given an incomplete table */
428*0Sstevel@tonic-gate   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
429*0Sstevel@tonic-gate }
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate int inflate_trees_bits(c, bb, tb, hp, z)
433*0Sstevel@tonic-gate uIntf *c;               /* 19 code lengths */
434*0Sstevel@tonic-gate uIntf *bb;              /* bits tree desired/actual depth */
435*0Sstevel@tonic-gate inflate_huft * FAR *tb; /* bits tree result */
436*0Sstevel@tonic-gate inflate_huft *hp;       /* space for trees */
437*0Sstevel@tonic-gate z_streamp z;            /* for messages */
438*0Sstevel@tonic-gate {
439*0Sstevel@tonic-gate   int r;
440*0Sstevel@tonic-gate   uInt hn = 0;          /* hufts used in space */
441*0Sstevel@tonic-gate   uIntf *v;             /* work area for huft_build */
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate   if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
444*0Sstevel@tonic-gate     return Z_MEM_ERROR;
445*0Sstevel@tonic-gate   r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
446*0Sstevel@tonic-gate                  tb, bb, hp, &hn, v);
447*0Sstevel@tonic-gate   if (r == Z_DATA_ERROR)
448*0Sstevel@tonic-gate     z->msg = (char*)"oversubscribed dynamic bit lengths tree";
449*0Sstevel@tonic-gate   else if (r == Z_BUF_ERROR || *bb == 0)
450*0Sstevel@tonic-gate   {
451*0Sstevel@tonic-gate     z->msg = (char*)"incomplete dynamic bit lengths tree";
452*0Sstevel@tonic-gate     r = Z_DATA_ERROR;
453*0Sstevel@tonic-gate   }
454*0Sstevel@tonic-gate   ZFREE(z, v);
455*0Sstevel@tonic-gate   return r;
456*0Sstevel@tonic-gate }
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
460*0Sstevel@tonic-gate uInt nl;                /* number of literal/length codes */
461*0Sstevel@tonic-gate uInt nd;                /* number of distance codes */
462*0Sstevel@tonic-gate uIntf *c;               /* that many (total) code lengths */
463*0Sstevel@tonic-gate uIntf *bl;              /* literal desired/actual bit depth */
464*0Sstevel@tonic-gate uIntf *bd;              /* distance desired/actual bit depth */
465*0Sstevel@tonic-gate inflate_huft * FAR *tl; /* literal/length tree result */
466*0Sstevel@tonic-gate inflate_huft * FAR *td; /* distance tree result */
467*0Sstevel@tonic-gate inflate_huft *hp;       /* space for trees */
468*0Sstevel@tonic-gate z_streamp z;            /* for messages */
469*0Sstevel@tonic-gate {
470*0Sstevel@tonic-gate   int r;
471*0Sstevel@tonic-gate   uInt hn = 0;          /* hufts used in space */
472*0Sstevel@tonic-gate   uIntf *v;             /* work area for huft_build */
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate   /* allocate work area */
475*0Sstevel@tonic-gate   if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
476*0Sstevel@tonic-gate     return Z_MEM_ERROR;
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate   /* build literal/length tree */
479*0Sstevel@tonic-gate   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
480*0Sstevel@tonic-gate   if (r != Z_OK || *bl == 0)
481*0Sstevel@tonic-gate   {
482*0Sstevel@tonic-gate     if (r == Z_DATA_ERROR)
483*0Sstevel@tonic-gate       z->msg = (char*)"oversubscribed literal/length tree";
484*0Sstevel@tonic-gate     else if (r != Z_MEM_ERROR)
485*0Sstevel@tonic-gate     {
486*0Sstevel@tonic-gate       z->msg = (char*)"incomplete literal/length tree";
487*0Sstevel@tonic-gate       r = Z_DATA_ERROR;
488*0Sstevel@tonic-gate     }
489*0Sstevel@tonic-gate     ZFREE(z, v);
490*0Sstevel@tonic-gate     return r;
491*0Sstevel@tonic-gate   }
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate   /* build distance tree */
494*0Sstevel@tonic-gate   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
495*0Sstevel@tonic-gate   if (r != Z_OK || (*bd == 0 && nl > 257))
496*0Sstevel@tonic-gate   {
497*0Sstevel@tonic-gate     if (r == Z_DATA_ERROR)
498*0Sstevel@tonic-gate       z->msg = (char*)"oversubscribed distance tree";
499*0Sstevel@tonic-gate     else if (r == Z_BUF_ERROR) {
500*0Sstevel@tonic-gate       z->msg = (char*)"incomplete distance tree";
501*0Sstevel@tonic-gate       r = Z_DATA_ERROR;
502*0Sstevel@tonic-gate     }
503*0Sstevel@tonic-gate     else if (r != Z_MEM_ERROR)
504*0Sstevel@tonic-gate     {
505*0Sstevel@tonic-gate       z->msg = (char*)"empty distance tree with lengths";
506*0Sstevel@tonic-gate       r = Z_DATA_ERROR;
507*0Sstevel@tonic-gate     }
508*0Sstevel@tonic-gate     ZFREE(z, v);
509*0Sstevel@tonic-gate     return r;
510*0Sstevel@tonic-gate   }
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate   /* done */
513*0Sstevel@tonic-gate   ZFREE(z, v);
514*0Sstevel@tonic-gate   return Z_OK;
515*0Sstevel@tonic-gate }
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate /*ARGSUSED*/
518*0Sstevel@tonic-gate int inflate_trees_fixed(bl, bd, tl, td, z)
519*0Sstevel@tonic-gate uIntf *bl;               /* literal desired/actual bit depth */
520*0Sstevel@tonic-gate uIntf *bd;               /* distance desired/actual bit depth */
521*0Sstevel@tonic-gate inflate_huft * FAR *tl;  /* literal/length tree result */
522*0Sstevel@tonic-gate inflate_huft * FAR *td;  /* distance tree result */
523*0Sstevel@tonic-gate z_streamp z;             /* for memory allocation */
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate   *bl = fixed_bl;
526*0Sstevel@tonic-gate   *bd = fixed_bd;
527*0Sstevel@tonic-gate   *tl = (inflate_huft *)fixed_tl;
528*0Sstevel@tonic-gate   *td = (inflate_huft *)fixed_td;
529*0Sstevel@tonic-gate   return Z_OK;
530*0Sstevel@tonic-gate }
531