abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
inftrees.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "zutil.h"
#include "inftrees.h"

Go to the source code of this file.

Macros

#define MAXBITS   15
 

Functions

int ZLIB_INTERNAL inflate_table (codetype type, unsigned short FAR *lens, unsigned codes, code FAR *FAR *table, unsigned FAR *bits, unsigned short FAR *work)
 

Variables

const char inflate_copyright []
 

Macro Definition Documentation

#define MAXBITS   15

Definition at line 16 of file inftrees.c.

Function Documentation

int ZLIB_INTERNAL inflate_table ( codetype  type,
unsigned short FAR lens,
unsigned  codes,
code FAR *FAR table,
unsigned FAR bits,
unsigned short FAR work 
)

Definition at line 39 of file inftrees.c.

40 {
41  unsigned len; /* a code's length in bits */
42  unsigned sym; /* index of code symbols */
43  unsigned min, max; /* minimum and maximum code lengths */
44  unsigned root; /* number of index bits for root table */
45  unsigned curr; /* number of index bits for current table */
46  unsigned drop; /* code bits to drop for sub-table */
47  int left; /* number of prefix codes available */
48  unsigned used; /* code entries in table used */
49  unsigned huff; /* Huffman code */
50  unsigned incr; /* for incrementing code, index */
51  unsigned fill; /* index for replicating entries */
52  unsigned low; /* low bits for current root entry */
53  unsigned mask; /* mask for low root bits */
54  code here; /* table entry for duplication */
55  code FAR *next; /* next available space in table */
56  const unsigned short FAR *base; /* base value table to use */
57  const unsigned short FAR *extra; /* extra bits table to use */
58  int end; /* use base and extra for symbol > end */
59  unsigned short count[MAXBITS+1]; /* number of codes of each length */
60  unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
61  static const unsigned short lbase[31] = { /* Length codes 257..285 base */
62  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
63  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
64  static const unsigned short lext[31] = { /* Length codes 257..285 extra */
65  16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
66  19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195};
67  static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
68  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
69  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
70  8193, 12289, 16385, 24577, 0, 0};
71  static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
72  16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
73  23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
74  28, 28, 29, 29, 64, 64};
75 
76  /*
77  Process a set of code lengths to create a canonical Huffman code. The
78  code lengths are lens[0..codes-1]. Each length corresponds to the
79  symbols 0..codes-1. The Huffman code is generated by first sorting the
80  symbols by length from short to long, and retaining the symbol order
81  for codes with equal lengths. Then the code starts with all zero bits
82  for the first code of the shortest length, and the codes are integer
83  increments for the same length, and zeros are appended as the length
84  increases. For the deflate format, these bits are stored backwards
85  from their more natural integer increment ordering, and so when the
86  decoding tables are built in the large loop below, the integer codes
87  are incremented backwards.
88 
89  This routine assumes, but does not check, that all of the entries in
90  lens[] are in the range 0..MAXBITS. The caller must assure this.
91  1..MAXBITS is interpreted as that code length. zero means that that
92  symbol does not occur in this code.
93 
94  The codes are sorted by computing a count of codes for each length,
95  creating from that a table of starting indices for each length in the
96  sorted table, and then entering the symbols in order in the sorted
97  table. The sorted table is work[], with that space being provided by
98  the caller.
99 
100  The length counts are used for other purposes as well, i.e. finding
101  the minimum and maximum length codes, determining if there are any
102  codes at all, checking for a valid set of lengths, and looking ahead
103  at length counts to determine sub-table sizes when building the
104  decoding tables.
105  */
106 
107  /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
108  for (len = 0; len <= MAXBITS; len++)
109  count[len] = 0;
110  for (sym = 0; sym < codes; sym++)
111  count[lens[sym]]++;
112 
113  /* bound code lengths, force root to be within code lengths */
114  root = *bits;
115  for (max = MAXBITS; max >= 1; max--)
116  if (count[max] != 0) break;
117  if (root > max) root = max;
118  if (max == 0) { /* no symbols to code at all */
119  here.op = (unsigned char)64; /* invalid code marker */
120  here.bits = (unsigned char)1;
121  here.val = (unsigned short)0;
122  *(*table)++ = here; /* make a table to force an error */
123  *(*table)++ = here;
124  *bits = 1;
125  return 0; /* no symbols, but wait for decoding to report error */
126  }
127  for (min = 1; min < max; min++)
128  if (count[min] != 0) break;
129  if (root < min) root = min;
130 
131  /* check for an over-subscribed or incomplete set of lengths */
132  left = 1;
133  for (len = 1; len <= MAXBITS; len++) {
134  left <<= 1;
135  left -= count[len];
136  if (left < 0) return -1; /* over-subscribed */
137  }
138  if (left > 0 && (type == CODES || max != 1))
139  return -1; /* incomplete set */
140 
141  /* generate offsets into symbol table for each length for sorting */
142  offs[1] = 0;
143  for (len = 1; len < MAXBITS; len++)
144  offs[len + 1] = offs[len] + count[len];
145 
146  /* sort symbols by length, by symbol order within each length */
147  for (sym = 0; sym < codes; sym++)
148  if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
149 
150  /*
151  Create and fill in decoding tables. In this loop, the table being
152  filled is at next and has curr index bits. The code being used is huff
153  with length len. That code is converted to an index by dropping drop
154  bits off of the bottom. For codes where len is less than drop + curr,
155  those top drop + curr - len bits are incremented through all values to
156  fill the table with replicated entries.
157 
158  root is the number of index bits for the root table. When len exceeds
159  root, sub-tables are created pointed to by the root entry with an index
160  of the low root bits of huff. This is saved in low to check for when a
161  new sub-table should be started. drop is zero when the root table is
162  being filled, and drop is root when sub-tables are being filled.
163 
164  When a new sub-table is needed, it is necessary to look ahead in the
165  code lengths to determine what size sub-table is needed. The length
166  counts are used for this, and so count[] is decremented as codes are
167  entered in the tables.
168 
169  used keeps track of how many table entries have been allocated from the
170  provided *table space. It is checked for LENS and DIST tables against
171  the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
172  the initial root table size constants. See the comments in inftrees.h
173  for more information.
174 
175  sym increments through all symbols, and the loop terminates when
176  all codes of length max, i.e. all codes, have been processed. This
177  routine permits incomplete codes, so another loop after this one fills
178  in the rest of the decoding tables with invalid code markers.
179  */
180 
181  /* set up for code type */
182  switch (type) {
183  case CODES:
184  base = extra = work; /* dummy value--not used */
185  end = 19;
186  break;
187  case LENS:
188  base = lbase;
189  base -= 257;
190  extra = lext;
191  extra -= 257;
192  end = 256;
193  break;
194  default: /* DISTS */
195  base = dbase;
196  extra = dext;
197  end = -1;
198  }
199 
200  /* initialize state for loop */
201  huff = 0; /* starting code */
202  sym = 0; /* starting code symbol */
203  len = min; /* starting code length */
204  next = *table; /* current table to fill in */
205  curr = root; /* current table index bits */
206  drop = 0; /* current bits to drop from code for index */
207  low = (unsigned)(-1); /* trigger new sub-table when len > root */
208  used = 1U << root; /* use root table entries */
209  mask = used - 1; /* mask for comparing low */
210 
211  /* check available table space */
212  if ((type == LENS && used >= ENOUGH_LENS) ||
213  (type == DISTS && used >= ENOUGH_DISTS))
214  return 1;
215 
216  /* process all codes and make table entries */
217  for (;;) {
218  /* create table entry */
219  here.bits = (unsigned char)(len - drop);
220  if ((int)(work[sym]) < end) {
221  here.op = (unsigned char)0;
222  here.val = work[sym];
223  }
224  else if ((int)(work[sym]) > end) {
225  here.op = (unsigned char)(extra[work[sym]]);
226  here.val = base[work[sym]];
227  }
228  else {
229  here.op = (unsigned char)(32 + 64); /* end of block */
230  here.val = 0;
231  }
232 
233  /* replicate for those indices with low len bits equal to huff */
234  incr = 1U << (len - drop);
235  fill = 1U << curr;
236  min = fill; /* save offset to next table */
237  do {
238  fill -= incr;
239  next[(huff >> drop) + fill] = here;
240  } while (fill != 0);
241 
242  /* backwards increment the len-bit code huff */
243  incr = 1U << (len - 1);
244  while (huff & incr)
245  incr >>= 1;
246  if (incr != 0) {
247  huff &= incr - 1;
248  huff += incr;
249  }
250  else
251  huff = 0;
252 
253  /* go to next symbol, update count, len */
254  sym++;
255  if (--(count[len]) == 0) {
256  if (len == max) break;
257  len = lens[work[sym]];
258  }
259 
260  /* create new sub-table if needed */
261  if (len > root && (huff & mask) != low) {
262  /* if first time, transition to sub-tables */
263  if (drop == 0)
264  drop = root;
265 
266  /* increment past last table */
267  next += min; /* here min is 1 << curr */
268 
269  /* determine length of next table */
270  curr = len - drop;
271  left = (int)(1 << curr);
272  while (curr + drop < max) {
273  left -= count[curr + drop];
274  if (left <= 0) break;
275  curr++;
276  left <<= 1;
277  }
278 
279  /* check for enough space */
280  used += 1U << curr;
281  if ((type == LENS && used >= ENOUGH_LENS) ||
282  (type == DISTS && used >= ENOUGH_DISTS))
283  return 1;
284 
285  /* point entry in root table to sub-table */
286  low = huff & mask;
287  (*table)[low].op = (unsigned char)curr;
288  (*table)[low].bits = (unsigned char)root;
289  (*table)[low].val = (unsigned short)(next - *table);
290  }
291  }
292 
293  /*
294  Fill in rest of table for incomplete codes. This loop is similar to the
295  loop above in incrementing huff for table indices. It is assumed that
296  len is equal to curr + drop, so there is no loop needed to increment
297  through high index bits. When the current sub-table is filled, the loop
298  drops back to the root table to fill in any remaining entries there.
299  */
300  here.op = (unsigned char)64; /* invalid code marker */
301  here.bits = (unsigned char)(len - drop);
302  here.val = (unsigned short)0;
303  while (huff != 0) {
304  /* when done with sub-table, drop back to root table */
305  if (drop != 0 && (huff & mask) != low) {
306  drop = 0;
307  len = root;
308  next = *table;
309  here.bits = (unsigned char)len;
310  }
311 
312  /* put invalid code marker in table */
313  next[huff >> drop] = here;
314 
315  /* backwards increment the len-bit code huff */
316  incr = 1U << (len - 1);
317  while (huff & incr)
318  incr >>= 1;
319  if (incr != 0) {
320  huff &= incr - 1;
321  huff += incr;
322  }
323  else
324  huff = 0;
325  }
326 
327  /* set return parameters */
328  *table += used;
329  *bits = root;
330  return 0;
331 }
unsigned short val
Definition: inftrees.h:29
unsigned extra
Definition: inflate.h:106
Definition: inftrees.h:59
unsigned char op
Definition: inftrees.h:27
unsigned short lens[320]
Definition: inflate.h:118
code FAR * next
Definition: inflate.h:117
#define ENOUGH_LENS
Definition: inftrees.h:51
code codes[ENOUGH]
Definition: inflate.h:120
Definition: inftrees.h:57
static double max
Definition: cuddSubsetHB.c:134
unsigned short work[288]
Definition: inflate.h:119
unsigned char bits
Definition: inftrees.h:28
#define ENOUGH_DISTS
Definition: inftrees.h:52
Definition: inftrees.h:26
#define MAXBITS
Definition: inftrees.c:16
Definition: inftrees.h:58
#define FAR
Definition: zconf.h:329
static char * bits(int n)
Definition: abcSaucy.c:201

Variable Documentation

const char inflate_copyright[]
Initial value:
=
" inflate 1.2.5 Copyright 1995-2010 Mark Adler "

Definition at line 18 of file inftrees.c.