Geant4  10.01.p03
csz_inflate.cc
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 /*G.Barrand :
7 #ifdef WIN32
8 #define __STDC__
9 #endif
10 #ifdef __MWERKS__
11 #define __STDC__
12 #endif
13 
14 #ifndef NULL
15 #define NULL 0L
16 #endif
17 
18 #define DEFLATE 8
19 */
20 
21 /*G.Barrand : static int qflag = 0; */
22 
23 /* inflate.c -- put in the public domain by Mark Adler
24  version c14o, 23 August 1994 */
25 
26 
27 /* You can do whatever you like with this source file, though I would
28  prefer that if you modify it and redistribute it that you include
29  comments to that effect with your name and the date. Thank you.
30 
31  History:
32  vers date who what
33  ---- --------- -------------- ------------------------------------
34  a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
35  b1 21 Mar 92 M. Adler first version with partial lookup tables
36  b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks
37  b3 22 Mar 92 M. Adler sped up match copies, cleaned up some
38  b4 25 Mar 92 M. Adler added prototypes; removed window[] (now
39  is the responsibility of unzip.h--also
40  changed name to slide[]), so needs diffs
41  for unzip.c and unzip.h (this allows
42  compiling in the small model on MSDOS);
43  fixed cast of q in huft_build();
44  b5 26 Mar 92 M. Adler got rid of unintended macro recursion.
45  b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed
46  bug in inflate_fixed().
47  c1 30 Mar 92 M. Adler removed lbits, dbits environment variables.
48  changed BMAX to 16 for explode. Removed
49  OUTB usage, and replaced it with flush()--
50  this was a 20% speed improvement! Added
51  an explode.c (to replace unimplod.c) that
52  uses the huft routines here. Removed
53  register union.
54  c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
55  c3 10 Apr 92 M. Adler reduced memory of code tables made by
56  huft_build significantly (factor of two to
57  three).
58  c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy().
59  worked around a Turbo C optimization bug.
60  c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
61  the 32K window size for specialized
62  applications.
63  c6 31 May 92 M. Adler added some typecasts to eliminate warnings
64  c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug).
65  c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug.
66  c9 9 Oct 92 M. Adler removed a memory error message (~line 416).
67  c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch,
68  removed old inflate, renamed inflate_entry
69  to inflate, added Mark's fix to a comment.
70  c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees.
71  c11 2 Jan 93 M. Adler fixed bug in detection of incomplete
72  tables, and removed assumption that EOB is
73  the longest code (bad assumption).
74  c12 3 Jan 93 M. Adler make tables for fixed blocks only once.
75  c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c
76  outputs one zero length code for an empty
77  distance tree).
78  c14 12 Mar 93 M. Adler made inflate.c standalone with the
79  introduction of inflate.h.
80  c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470.
81  c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays
82  to static for Amiga.
83  c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing.
84  c14e 8 Oct 93 G. Roelofs changed memset() to memzero().
85  c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace()
86  conditional; added inflate_free().
87  c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug)
88  c14h 7 Dec 93 C. Ghisler huft_build() optimizations.
89  c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing;
90  G. Roelofs check NEXTBYTE macro for EOF.
91  c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd
92  EOF check.
93  c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings.
94  c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
95  to avoid bug in Encore compiler.
96  c14m 7 Jul 94 P. Kienitz modified to allow assembler version of
97  inflate_codes() (define ASM_INFLATECODES)
98  c14n 22 Jul 94 G. Roelofs changed fprintf to FPRINTF for DLL versions
99  c14o 23 Aug 94 C. Spieler added a newline to a debug statement;
100  G. Roelofs added another typecast to avoid MSC warning
101  */
102 
103 
104 /*
105  Inflate deflated (PKZIP's method 8 compressed) data. The compression
106  method searches for as much of the current string of bytes (up to a
107  length of 258) in the previous 32K bytes. If it doesn't find any
108  matches (of at least length 3), it codes the next byte. Otherwise, it
109  codes the length of the matched string and its distance backwards from
110  the current position. There is a single Huffman code that codes both
111  single bytes (called "literals") and match lengths. A second Huffman
112  code codes the distance information, which follows a length code. Each
113  length or distance code actually represents a base value and a number
114  of "extra" (sometimes zero) bits to get to add to the base value. At
115  the end of each deflated block is a special end-of-block (EOB) literal/
116  length code. The decoding process is basically: get a literal/length
117  code; if EOB then done; if a literal, emit the decoded byte; if a
118  length then get the distance and emit the referred-to bytes from the
119  sliding window of previously emitted data.
120 
121  There are (currently) three kinds of inflate blocks: stored, fixed, and
122  dynamic. The compressor outputs a chunk of data at a time and decides
123  which method to use on a chunk-by-chunk basis. A chunk might typically
124  be 32K to 64K, uncompressed. If the chunk is uncompressible, then the
125  "stored" method is used. In this case, the bytes are simply stored as
126  is, eight bits per byte, with none of the above coding. The bytes are
127  preceded by a count, since there is no longer an EOB code.
128 
129  If the data is compressible, then either the fixed or dynamic methods
130  are used. In the dynamic method, the compressed data is preceded by
131  an encoding of the literal/length and distance Huffman codes that are
132  to be used to decode this block. The representation is itself Huffman
133  coded, and so is preceded by a description of that code. These code
134  descriptions take up a little space, and so for small blocks, there is
135  a predefined set of codes, called the fixed codes. The fixed method is
136  used if the block ends up smaller that way (usually for quite small
137  chunks); otherwise the dynamic method is used. In the latter case, the
138  codes are customized to the probabilities in the current block and so
139  can code it much better than the pre-determined fixed codes can.
140 
141  The Huffman codes themselves are decoded using a mutli-level table
142  lookup, in order to maximize the speed of decoding plus the speed of
143  building the decoding tables. See the comments below that precede the
144  lbits and dbits tuning parameters.
145  */
146 
147 
148 /*
149  Notes beyond the 1.93a appnote.txt:
150 
151  1. Distance pointers never point before the beginning of the output
152  stream.
153  2. Distance pointers can point back across blocks, up to 32k away.
154  3. There is an implied maximum of 7 bits for the bit length table and
155  15 bits for the actual data.
156  4. If only one code exists, then it is encoded using one bit. (Zero
157  would be more efficient, but perhaps a little confusing.) If two
158  codes exist, they are coded using one bit each (0 and 1).
159  5. There is no way of sending zero distance codes--a dummy must be
160  sent if there are none. (History: a pre 2.0 version of PKZIP would
161  store blocks with no distance codes, but this was discovered to be
162  too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
163  zero distance codes, which is sent as one code of zero bits in
164  length.
165  6. There are up to 286 literal/length codes. Code 256 represents the
166  end-of-block. Note however that the static length tree defines
167  288 codes just to fill out the Huffman codes. Codes 286 and 287
168  cannot be used though, since there is no length base or extra bits
169  defined for them. Similarily, there are up to 30 distance codes.
170  However, static trees define 32 codes (all 5 bits) to fill out the
171  Huffman codes, but the last two had better not show up in the data.
172  7. Unzip can check dynamic Huffman blocks for complete code sets.
173  The exception is that a single code would not be complete (see #4).
174  8. The five bits following the block type is really the number of
175  literal codes sent minus 257.
176  9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
177  (1+6+6). Therefore, to output three times the length, you output
178  three codes (1+1+1), whereas to output four times the same length,
179  you only need two codes (1+3). Hmm.
180  10. In the tree reconstruction algorithm, Code = Code + Increment
181  only if BitLength(i) is not zero. (Pretty obvious.)
182  11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
183  12. Note: length code 284 can represent 227-258, but length code 285
184  really is 258. The last length deserves its own, short code
185  since it gets used a lot in very redundant files. The length
186  258 is special since 258 - 3 (the min match length) is 255.
187  13. The literal/length and distance code bit lengths are read as a
188  single stream of lengths. It is possible (and advantageous) for
189  a repeat code (16, 17, or 18) to go across the boundary between
190  the two sets of lengths.
191  */
192 
193 
194 #if 0
195 /*G.Barrand #define PKZIP_BUG_WORKAROUND */ /* PKZIP 1.93a problem--live with it */
196 #endif
197 
198 /*
199  inflate.h must supply the uch slide[WSIZE] array and the NEXTBYTE,
200  FLUSH() and memzero macros. If the window size is not 32K, it
201  should also define WSIZE. If INFMOD is defined, it can include
202  compiled functions to support the NEXTBYTE and/or FLUSH() macros.
203  There are defaults for NEXTBYTE and FLUSH() below for use as
204  examples of what those functions need to do. Normally, you would
205  also want FLUSH() to compute a crc on the data. inflate.h also
206  needs to provide these typedefs:
207 
208  typedef unsigned char uch;
209  typedef unsigned short ush;
210  typedef unsigned long ulg;
211 
212  This module uses the external functions malloc() and free() (and
213  probably memset() or bzero() in the memzero() macro). Their
214  prototypes are normally found in <string.h> and <stdlib.h>.
215  */
216 /*G.Barrand
217 #define INFMOD
218 #if 0
219 #include "Inflate.h"
220 #endif
221 */
222 
223 typedef char boolean;
224 typedef unsigned char uch; /* code assumes unsigned bytes; these type- */
225 typedef unsigned short ush; /* defs replace byte/UWORD/ULONG (which are */
226 typedef unsigned long ulg; /* predefined on some systems) & match zip */
227 
228 #ifndef WSIZE /* default is 32K */
229 # define WSIZE 0x8000 /* window size--must be a power of two, and at least */
230 #endif /* 32K for zip's deflate method */
231 
232 #ifndef NEXTBYTE /* default is to simply get a byte from stdin */
233 # define NEXTBYTE csz__ReadByte()
234 #endif
235 
236 #ifndef FPRINTF
237 # define FPRINTF fprintf
238 #endif
239 
240 #ifndef FLUSH /* default is to simply write the buffer to stdout */
241 # define FLUSH(n) csz__WriteData(n) /* return value not used */
242 #endif
243 /* Warning: the fwrite above might not work on 16-bit compilers, since
244  0x8000 might be interpreted as -32,768 by the library function. */
245 
246 #ifndef Trace
247 # ifdef DEBUG
248 # define Trace(x) fprintf x
249 # else
250 # define Trace(x)
251 # endif
252 #endif
253 
254 
255 /* Huffman code lookup table entry--this entry is four bytes for machines
256  that have 16-bit pointers (e.g. PC's in the small or medium model).
257  Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
258  means that v is a literal, 16 < e < 32 means that v is a pointer to
259  the next table, which codes e - 16 bits, and lastly e == 99 indicates
260  an unused code. If a code with e == 99 is looked up, this implies an
261  error in the data. */
262 struct huft {
263  uch e; /* number of extra bits or operation */
264  uch b; /* number of bits in this code or subcode */
265  union {
266  ush n; /* literal, length base, or distance base */
267  struct huft *t; /* pointer to next level of table */
268  } v;
269 };
270 
271 
272 /* Function prototypes */
273 /*G.Barrand
274 #ifndef OF
275 # ifdef __STDC__
276 # define OF(a) a
277 # else
278 # define OF(a) ()
279 # endif
280 #endif
281 */
282 int csz__huft_build(unsigned *, unsigned, unsigned, ush *, ush *,
283  struct huft **, int *);
284 int csz__huft_free(struct huft *);
285 int csz__Inflate_codes(struct huft *, struct huft *, int, int);
286 int csz__Inflate_stored(void);
287 int csz__Inflate_fixed(void);
288 int csz__Inflate_dynamic(void);
289 int csz__Inflate_block(int *);
290 #ifdef __cplusplus /*G.Barrand*/
291 extern "C" {
292 #endif
293 int csz__Inflate(void);
294 int csz__Inflate_free(void);
295 #ifdef __cplusplus /*G.Barrand*/
296 }
297 #endif
298 
299 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
300  stream to find repeated byte strings. This is implemented here as a
301  circular buffer. The index is updated simply by incrementing and then
302  and'ing with 0x7fff (32K-1). */
303 /* It is left to other modules to supply the 32K area. It is assumed
304  to be usable as if it were declared "uch slide[32768];" or as just
305  "uch *slide;" and then malloc'ed in the latter case. The definition
306  must be in unzip.h, included above. */
307 
308 static uch csz__slide [32768];
309 static unsigned wp; /* current position in slide */
310 
311 
312 /* Tables for deflate from PKZIP's appnote.txt. */
313 static unsigned border[] = { /* Order of the bit length code lengths */
314  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
315 static ush cplens[] = { /* Copy lengths for literal codes 257..285 */
316  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
317  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
318  /* note: see note #13 above about the 258 in this list. */
319 static ush cplext[] = { /* Extra bits for literal codes 257..285 */
320  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
321  3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
322 static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
323  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
324  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
325  8193, 12289, 16385, 24577};
326 static ush cpdext[] = { /* Extra bits for distance codes */
327  0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
328  7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
329  12, 12, 13, 13};
330 
331 /* And'ing with mask[n] masks the lower n bits */
332 static ush mask[] = {
333  0x0000,
334  0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
335  0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
336 };
337 
338 
339 /* Macros for inflate() bit peeking and grabbing.
340  The usage is:
341 
342  NEEDBITS(j)
343  x = b & mask[j];
344  DUMPBITS(j)
345 
346  where NEEDBITS makes sure that b has at least j bits in it, and
347  DUMPBITS removes the bits from b. The macros use the variable k
348  for the number of bits in b. Normally, b and k are register
349  variables for speed, and are initialized at the begining of a
350  routine that uses these macros from a global bit buffer and count.
351 
352  In order to not ask for more bits than there are in the compressed
353  stream, the Huffman tables are constructed to only ask for just
354  enough bits to make up the end-of-block code (value 256). Then no
355  bytes need to be "returned" to the buffer at the end of the last
356  block. See the huft_build() routine.
357  */
358 
359 static ulg bb; /* bit buffer */
360 static unsigned bk; /* bits in bit buffer */
361 static uch *ibufptr,*obufptr;
362 static long ibufcnt, obufcnt;
363 
364 #define CHECK_EOF
365 
366 #ifndef CHECK_EOF
367 static int csz__ReadByte();
368 #endif
369 static void csz__WriteData(int);
370 
371 #ifndef CHECK_EOF
372 # define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
373 #else
374 # define NEEDBITS(n) {while(k<(n)){if(ibufcnt-- <= 0)return 1;b|=((ulg) *ibufptr++)<<k;k+=8;}}
375 #endif /* Piet Plomp: change "return 1" to "break" */
376 
377 #define DUMPBITS(n) {b>>=(n);k-=(n);}
378 
379 
380 /*
381  Huffman code decoding is performed using a multi-level table lookup.
382  The fastest way to decode is to simply build a lookup table whose
383  size is determined by the longest code. However, the time it takes
384  to build this table can also be a factor if the data being decoded
385  is not very long. The most common codes are necessarily the
386  shortest codes, so those codes dominate the decoding time, and hence
387  the speed. The idea is you can have a shorter table that decodes the
388  shorter, more probable codes, and then point to subsidiary tables for
389  the longer codes. The time it costs to decode the longer codes is
390  then traded against the time it takes to make longer tables.
391 
392  This results of this trade are in the variables lbits and dbits
393  below. lbits is the number of bits the first level table for literal/
394  length codes can decode in one step, and dbits is the same thing for
395  the distance codes. Subsequent tables are also less than or equal to
396  those sizes. These values may be adjusted either when all of the
397  codes are shorter than that, in which case the longest code length in
398  bits is used, or when the shortest code is *longer* than the requested
399  table size, in which case the length of the shortest code in bits is
400  used.
401 
402  There are two different values for the two tables, since they code a
403  different number of possibilities each. The literal/length table
404  codes 286 possible values, or in a flat code, a little over eight
405  bits. The distance table codes 30 possible values, or a little less
406  than five bits, flat. The optimum values for speed end up being
407  about one bit more than those, so lbits is 8+1 and dbits is 5+1.
408  The optimum values may differ though from machine to machine, and
409  possibly even between compilers. Your mileage may vary.
410  */
411 
412 
413 static int lbits = 9; /* bits in base literal/length lookup table */
414 static int dbits = 6; /* bits in base distance lookup table */
415 
416 
417 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
418 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
419 #define N_MAX 288 /* maximum number of codes in any set */
420 
421 
422 static unsigned hufts; /* track memory usage */
423 
424 
425 int csz__huft_build(unsigned *b, unsigned n, unsigned s, ush *d, ush *e, struct huft **t, int *m)
426 /* unsigned *b; code lengths in bits (all assumed <= BMAX) */
427 /* unsigned n; number of codes (assumed <= N_MAX) */
428 /* unsigned s; number of simple-valued codes (0..s-1) */
429 /* ush *d; list of base values for non-simple codes */
430 /* ush *e; list of extra bits for non-simple codes */
431 /* struct huft **t; result: starting table */
432 /* int *m; maximum lookup bits, returns actual */
433 /* Given a list of code lengths and a maximum table size, make a set of
434  tables to decode that set of codes. Return zero on success, one if
435  the given code set is incomplete (the tables are still built in this
436  case), two if the input is invalid (all zero length codes or an
437  oversubscribed set of lengths), and three if not enough memory.
438  The code with value 256 is special, and the tables are constructed
439  so that no bits beyond that code are fetched when that code is
440  decoded. */
441 {
442  unsigned a; /* counter for codes of length k */
443  unsigned c[BMAX+1]; /* bit length count table */
444  unsigned el; /* length of EOB code (value 256) */
445  unsigned f; /* i repeats in table every f entries */
446  int g; /* maximum code length */
447  int h; /* table level */
448  /*G.Barrand : comment out register keyword.*/
449  /*register*/ unsigned i; /* counter, current code */
450  /*register*/ unsigned j; /* counter */
451  /*register*/ int k; /* number of bits in current code */
452  int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */
453  int *l = lx+1; /* stack of bits per table */
454  /*register*/ unsigned *p; /* pointer into c[], b[], or v[] */
455  /*register*/ struct huft *q; /* points to current table */
456  struct huft r; /* table entry for structure assignment */
457  struct huft *u[BMAX]; /* table stack */
458  static unsigned v[N_MAX]; /* values in order of bit length */
459  /*register*/ int w; /* bits before this table == (l * h) */
460  unsigned x[BMAX+1]; /* bit offsets, then code stack */
461  unsigned *xp; /* pointer into x */
462  int y; /* number of dummy codes added */
463  unsigned z; /* number of entries in current table */
464 
465 
466  /* Generate counts for each bit length */
467  el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
468  memset((char *)c,0,sizeof(c));
469  p = b; i = n;
470  do {
471  c[*p]++; p++; /* assume all entries <= BMAX */
472  } while (--i);
473  if (c[0] == n) /* null input--all zero length codes */
474  {
475  *t = (struct huft *)NULL;
476  *m = 0;
477  return 0;
478  }
479 
480 
481  /* Find minimum and maximum length, bound *m by those */
482  for (j = 1; j <= BMAX; j++)
483  if (c[j])
484  break;
485  k = j; /* minimum code length */
486  if ((unsigned)*m < j)
487  *m = j;
488  for (i = BMAX; i; i--)
489  if (c[i])
490  break;
491  g = i; /* maximum code length */
492  if ((unsigned)*m > i)
493  *m = i;
494 
495 
496  /* Adjust last length count to fill out codes, if needed */
497  for (y = 1 << j; j < i; j++, y <<= 1)
498  if ((y -= c[j]) < 0)
499  return 2; /* bad input: more codes than bits */
500  if ((y -= c[i]) < 0)
501  return 2;
502  c[i] += y;
503 
504 
505  /* Generate starting offsets into the value table for each length */
506  x[1] = j = 0;
507  p = c + 1; xp = x + 2;
508  while (--i) { /* note that i == g from above */
509  *xp++ = (j += *p++);
510  }
511 
512 
513  /* Make a table of values in order of bit lengths */
514  p = b; i = 0;
515  do {
516  if ((j = *p++) != 0)
517  v[x[j]++] = i;
518  } while (++i < n);
519 
520 
521  /* Generate the Huffman codes and for each, make the table entries */
522  x[0] = i = 0; /* first Huffman code is zero */
523  p = v; /* grab values in bit order */
524  h = -1; /* no tables yet--level -1 */
525  w = l[-1] = 0; /* no bits decoded yet */
526  u[0] = (struct huft *)NULL; /* just to keep compilers happy */
527  q = (struct huft *)NULL; /* ditto */
528  z = 0; /* ditto */
529 
530  /* go through the bit lengths (k already is bits in shortest code) */
531  for (; k <= g; k++)
532  {
533  a = c[k];
534  while (a--)
535  {
536  /* here i is the Huffman code of length k bits for value *p */
537  /* make tables up to required level */
538  while (k > w + l[h])
539  {
540  w += l[h++]; /* add bits already decoded */
541 
542  /* compute minimum size table less than or equal to *m bits */
543  z = (z = g - w) > (unsigned)*m ? (unsigned) *m : z; /* upper limit */
544  if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
545  { /* too few codes for k-w bit table */
546  f -= a + 1; /* deduct codes from patterns left */
547  xp = c + k;
548  while (++j < z) /* try smaller tables up to z bits */
549  {
550  if ((f <<= 1) <= *++xp)
551  break; /* enough codes to use up j bits */
552  f -= *xp; /* else deduct codes from patterns */
553  }
554  }
555  if ((unsigned)w + j > el && (unsigned)w < el)
556  j = el - w; /* make EOB code end at table */
557  z = 1 << j; /* table entries for j-bit table */
558  l[h] = j; /* set table size in stack */
559 
560  /* allocate and link in new table */
561  if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
562  (struct huft *)NULL)
563  {
564  if (h)
565  csz__huft_free(u[0]);
566  return 3; /* not enough memory */
567  }
568  hufts += z + 1; /* track memory usage */
569  *t = q + 1; /* link to list for huft_free() */
570  *(t = &(q->v.t)) = (struct huft *)NULL;
571  u[h] = ++q; /* table starts after link */
572 
573  /* connect to last table, if there is one */
574  if (h)
575  {
576  x[h] = i; /* save pattern for backing up */
577  r.b = (uch)l[h-1]; /* bits to dump before this table */
578  r.e = (uch)(16 + j); /* bits in this table */
579  r.v.t = q; /* pointer to this table */
580  j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
581  u[h-1][j] = r; /* connect to last table */
582  }
583  }
584 
585  /* set up table entry in r */
586  r.b = (uch)(k - w);
587  if (p >= v + n)
588  r.e = 99; /* out of values--invalid code */
589  else if (*p < s) {
590  r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
591  r.v.n = *p++; /* simple code is just the value */
592  } else if(e && d) {
593  r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
594  r.v.n = d[*p++ - s];
595  } else return 1;
596 
597  /* fill code-like entries with r */
598  f = 1 << (k - w);
599  for (j = i >> w; j < z; j += f)
600  q[j] = r;
601 
602  /* backwards increment the k-bit code i */
603  for (j = 1 << (k - 1); i & j; j >>= 1)
604  i ^= j;
605  i ^= j;
606 
607  /* backup over finished tables */
608  while ((i & ((1 << w) - 1)) != x[h])
609  w -= l[--h]; /* don't need to update q */
610  }
611  }
612 
613 
614  /* return actual size of base table */
615  *m = l[0];
616 
617 
618  /* Return true (1) if we were given an incomplete table */
619  return y != 0 && g != 1;
620 }
621 
622 
623 
624 int csz__huft_free(struct huft *t)
625 /* struct huft *t; table to free */
626 /* Free the malloc'ed tables built by huft_build(), which makes a linked
627  list of the tables it made, with the links in a dummy first entry of
628  each table. */
629 {
630  /*register*/ struct huft *p, *q;
631 
632 
633  /* Go through linked list, freeing from the malloced (t[-1]) address. */
634  p = t;
635  while (p != (struct huft *)NULL)
636  {
637  q = (--p)->v.t;
638  free(p);
639  p = q;
640  }
641  return 0;
642 }
643 
644 
645 
646 /*G.Barrand
647 #ifdef ASM_INFLATECODES
648 # define csz__Inflate_codes(tl,td,bl,bd) csz__Flate_codes(tl,td,bl,bd,(uch *)csz__slide)
649  int csz__Flate_codes(struct huft *, struct huft *, int, int, uch *);
650 #else
651 */
652 
653 int csz__Inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
654 /* struct huft *tl, *td; literal/length and distance decoder tables */
655 /* int bl, bd; number of bits decoded by tl[] and td[] */
656 /* inflate (decompress) the codes in a deflated (compressed) block.
657  Return an error code or zero if it all goes ok. */
658 {
659  /*register*/ unsigned e; /* table entry flag/number of extra bits */
660  unsigned n, d; /* length and index for copy */
661  unsigned w; /* current window position */
662  struct huft *t; /* pointer to table entry */
663  unsigned ml, md; /* masks for bl and bd bits */
664  /*register*/ ulg b; /* bit buffer */
665  /*register*/ unsigned k; /* number of bits in bit buffer */
666 
667 
668  /* make local copies of globals */
669  b = bb; /* initialize bit buffer */
670  k = bk;
671  w = wp; /* initialize window position */
672 
673 
674  /* inflate the coded data */
675  ml = mask[bl]; /* precompute masks for speed */
676  md = mask[bd];
677  while (1) /* do until end of block */
678  {
679  NEEDBITS((unsigned)bl)
680  if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
681  do {
682  if (e == 99)
683  return 1;
684  DUMPBITS(t->b)
685  e -= 16;
686  NEEDBITS(e)
687  } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16);
688  DUMPBITS(t->b)
689  if (e == 16) /* then it's a literal */
690  {
691  csz__slide[w++] = (uch)t->v.n;
692  if (w == WSIZE)
693  {
694  FLUSH(w);
695  w = 0;
696  }
697  }
698  else /* it's an EOB or a length */
699  {
700  /* exit if end of block */
701  if (e == 15)
702  break;
703 
704  /* get length of block to copy */
705  NEEDBITS(e)
706  n = t->v.n + ((unsigned)b & mask[e]);
707  DUMPBITS(e);
708 
709  /* decode distance of block to copy */
710  NEEDBITS((unsigned)bd)
711  if ((e = (t = td + ((unsigned)b & md))->e) > 16)
712  do {
713  if (e == 99)
714  return 1;
715  DUMPBITS(t->b)
716  e -= 16;
717  NEEDBITS(e)
718  } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16);
719  DUMPBITS(t->b)
720  NEEDBITS(e)
721  d = w - t->v.n - ((unsigned)b & mask[e]);
722  DUMPBITS(e)
723 
724  /* do the copy */
725  do {
726  n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
727 #ifndef NOMEMCPY
728  if (w - d >= e) /* (this test assumes unsigned comparison) */
729  {
730  memcpy(csz__slide + w, csz__slide + d, e);
731  w += e;
732  d += e;
733  }
734  else /* do it slow to avoid memcpy() overlap */
735 #endif /* !NOMEMCPY */
736  do {
737  csz__slide[w++] = csz__slide[d++];
738  } while (--e);
739  if (w == WSIZE)
740  {
741  FLUSH(w);
742  w = 0;
743  }
744  } while (n);
745  }
746  }
747 
748 
749  /* restore the globals from the locals */
750  wp = w; /* restore global window pointer */
751  bb = b; /* restore global bit buffer */
752  bk = k;
753 
754 
755  /* done */
756  return 0;
757 }
758 
759 /*#endif G.Barrand*/ /* ASM_INFLATECODES */
760 
761 
762 
764 /* "decompress" an inflated type 0 (stored) block. */
765 {
766  unsigned n; /* number of bytes in block */
767  unsigned w; /* current window position */
768  /*register*/ ulg b; /* bit buffer */
769  /*register*/ unsigned k; /* number of bits in bit buffer */
770 
771 
772  /* make local copies of globals */
773  Trace((stderr, "\nstored block"));
774  b = bb; /* initialize bit buffer */
775  k = bk;
776  w = wp; /* initialize window position */
777 
778 
779  /* go to byte boundary */
780  n = k & 7;
781  DUMPBITS(n);
782 
783 
784  /* get the length and its complement */
785  NEEDBITS(16)
786  n = ((unsigned)b & 0xffff);
787  DUMPBITS(16)
788  NEEDBITS(16)
789  if (n != (unsigned)((~b) & 0xffff))
790  return 1; /* error in compressed data */
791  DUMPBITS(16)
792 
793 
794  /* read and output the compressed data */
795  while (n--)
796  {
797  NEEDBITS(8)
798  csz__slide[w++] = (uch)b;
799  if (w == WSIZE)
800  {
801  FLUSH(w);
802  w = 0;
803  }
804  DUMPBITS(8)
805  }
806 
807 
808  /* restore the globals from the locals */
809  wp = w; /* restore global window pointer */
810  bb = b; /* restore global bit buffer */
811  bk = k;
812  return 0;
813 }
814 
815 
816 /* Globals for literal tables (built once) */
817 struct huft *csz__fixed_tl = (struct huft *)NULL;
820 
822 /* decompress an inflated type 1 (fixed Huffman codes) block. We should
823  either replace this with a custom decoder, or at least precompute the
824  Huffman tables. */
825 {
826  /* if first time, set up tables for fixed blocks */
827  Trace((stderr, "\nliteral block"));
828  if (csz__fixed_tl == (struct huft *)NULL)
829  {
830  int i; /* temporary variable */
831  static unsigned l[288]; /* length list for huft_build */
832 
833  /* literal table */
834  for (i = 0; i < 144; i++)
835  l[i] = 8;
836  for (; i < 256; i++)
837  l[i] = 9;
838  for (; i < 280; i++)
839  l[i] = 7;
840  for (; i < 288; i++) /* make a complete, but wrong code set */
841  l[i] = 8;
842  csz__fixed_bl = 7;
843  if ((i = csz__huft_build(l, 288, 257, cplens, cplext,
844  &csz__fixed_tl, &csz__fixed_bl)) != 0)
845  {
846  csz__fixed_tl = (struct huft *)NULL;
847  return i;
848  }
849 
850  /* distance table */
851  for (i = 0; i < 30; i++) /* make an incomplete code set */
852  l[i] = 5;
853  csz__fixed_bd = 5;
854  if ((i = csz__huft_build(l, 30, 0, cpdist, cpdext, &csz__fixed_td, &csz__fixed_bd)) > 1)
855  {
856  csz__huft_free(csz__fixed_tl);
857  csz__fixed_tl = (struct huft *)NULL;
858  return i;
859  }
860  }
861 
862 
863  /* decompress until an end-of-block code */
864  return csz__Inflate_codes(csz__fixed_tl, csz__fixed_td, csz__fixed_bl, csz__fixed_bd) != 0;
865 }
866 
867 
868 
870 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
871 {
872  int i; /* temporary variables */
873  unsigned j;
874  unsigned l; /* last length */
875  unsigned m; /* mask for bit lengths table */
876  unsigned n; /* number of lengths to get */
877  struct huft *tl; /* literal/length code table */
878  struct huft *td; /* distance code table */
879  int bl; /* lookup bits for tl */
880  int bd; /* lookup bits for td */
881  unsigned nb; /* number of bit length codes */
882  unsigned nl; /* number of literal/length codes */
883  unsigned nd; /* number of distance codes */
884 #ifdef PKZIP_BUG_WORKAROUND
885  static unsigned ll[288+32]; /* literal/length and distance code lengths */
886 #else
887  static unsigned ll[286+30]; /* literal/length and distance code lengths */
888 #endif
889  /*register*/ ulg b; /* bit buffer */
890  /*register*/ unsigned k; /* number of bits in bit buffer */
891 
892  static int qflag = 0; /*G.Barrand*/
893 
894  /* make local bit buffer */
895  Trace((stderr, "\ndynamic block"));
896  b = bb;
897  k = bk;
898 
899 
900  /* read in table lengths */
901  NEEDBITS(5)
902  nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
903  DUMPBITS(5)
904  NEEDBITS(5)
905  nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
906  DUMPBITS(5)
907  NEEDBITS(4)
908  nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
909  DUMPBITS(4)
910 #ifdef PKZIP_BUG_WORKAROUND
911  if (nl > 288 || nd > 32)
912 #else
913  if (nl > 286 || nd > 30)
914 #endif
915  return 1; /* bad lengths */
916 
917 
918  /* read in bit-length-code lengths */
919  for (j = 0; j < nb; j++)
920  {
921  NEEDBITS(3)
922  ll[border[j]] = (unsigned)b & 7;
923  DUMPBITS(3)
924  }
925  for (; j < 19; j++)
926  ll[border[j]] = 0;
927 
928 
929  /* build decoding table for trees--single level, 7 bit lookup */
930  bl = 7;
931  if ((i = csz__huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
932  {
933  if (i == 1)
934  csz__huft_free(tl);
935  return i; /* incomplete code set */
936  }
937 
938 /*G.Barrand : to quiet Coverity : */
939 #define NEEDBITS_free_tl(n) {while(k<(n)){if(ibufcnt-- <= 0){csz__huft_free(tl);return 1;} b|=((ulg) *ibufptr++)<<k;k+=8;}}
940 
941  /* read in literal and distance code lengths */
942  n = nl + nd;
943  m = mask[bl];
944  i = l = 0;
945  while ((unsigned)i < n)
946  {
947  NEEDBITS_free_tl((unsigned)bl)
948  j = (td = tl + ((unsigned)b & m))->b;
949  DUMPBITS(j)
950  j = td->v.n;
951  if (j < 16) /* length of code in bits (0..15) */
952  ll[i++] = l = j; /* save last length in l */
953  else if (j == 16) /* repeat last length 3 to 6 times */
954  {
956  j = 3 + ((unsigned)b & 3);
957  DUMPBITS(2)
958  if ((unsigned)i + j > n) {
959  csz__huft_free(tl); /*G.Barrand : quiet Coverity*/
960  return 1;
961  }
962  while (j--)
963  ll[i++] = l;
964  }
965  else if (j == 17) /* 3 to 10 zero length codes */
966  {
968  j = 3 + ((unsigned)b & 7);
969  DUMPBITS(3)
970  if ((unsigned)i + j > n){
971  csz__huft_free(tl); /*G.Barrand : quiet Coverity*/
972  return 1;
973  }
974  while (j--)
975  ll[i++] = 0;
976  l = 0;
977  }
978  else /* j == 18: 11 to 138 zero length codes */
979  {
981  j = 11 + ((unsigned)b & 0x7f);
982  DUMPBITS(7)
983  if ((unsigned)i + j > n) {
984  csz__huft_free(tl); /*G.Barrand : quiet Coverity*/
985  return 1;
986  }
987  while (j--)
988  ll[i++] = 0;
989  l = 0;
990  }
991  }
992 
993 
994  /* free decoding table for trees */
995  csz__huft_free(tl);
996 
997 
998  /* restore the global bit buffer */
999  bb = b;
1000  bk = k;
1001 
1002 
1003  /* build the decoding tables for literal/length and distance codes */
1004  bl = lbits;
1005  if ((i = csz__huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
1006  {
1007  if (i == 1 && !qflag) {
1008  FPRINTF(stderr, "(incomplete l-tree) ");
1009  csz__huft_free(tl);
1010  }
1011  return i; /* incomplete code set */
1012  }
1013  bd = dbits;
1014  if ((i = csz__huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
1015  {
1016  if (i == 1 && !qflag) {
1017  FPRINTF(stderr, "(incomplete d-tree) ");
1018 #ifdef PKZIP_BUG_WORKAROUND
1019  i = 0;
1020  }
1021 #else
1022  csz__huft_free(td);
1023  }
1024  csz__huft_free(tl);
1025  return i; /* incomplete code set */
1026 #endif
1027  }
1028 
1029 
1030  /* decompress until an end-of-block code */
1031  if (csz__Inflate_codes(tl, td, bl, bd))
1032  return 1;
1033 
1034 
1035  /* free the decoding tables, return */
1036  csz__huft_free(tl);
1037  csz__huft_free(td);
1038  return 0;
1039 }
1040 
1041 
1042 
1044 /* int *e; last block flag */
1045 /* decompress an inflated block */
1046 {
1047  unsigned t; /* block type */
1048  /*register*/ ulg b; /* bit buffer */
1049  /*register*/ unsigned k; /* number of bits in bit buffer */
1050 
1051 
1052  /* make local bit buffer */
1053  b = bb;
1054  k = bk;
1055 
1056 
1057  /* read in last block bit */
1058  NEEDBITS(1)
1059  *e = (int)b & 1;
1060  DUMPBITS(1)
1061 
1062 
1063  /* read in block type */
1064  NEEDBITS(2)
1065  t = (unsigned)b & 3;
1066  DUMPBITS(2)
1067 
1068 
1069  /* restore the global bit buffer */
1070  bb = b;
1071  bk = k;
1072 
1073 
1074  /* inflate that block type */
1075  if (t == 2)
1076  return csz__Inflate_dynamic();
1077  if (t == 0)
1078  return csz__Inflate_stored();
1079  if (t == 1)
1080  return csz__Inflate_fixed();
1081 
1082 
1083  /* bad block type */
1084  return 2;
1085 }
1086 
1087 #ifdef __cplusplus /*G.Barrand*/
1088 extern "C" {
1089 #endif
1090 
1092 /* decompress an inflated entry */
1093 {
1094  int e; /* last block flag */
1095  int r; /* result code */
1096  unsigned h; /* maximum struct huft's malloc'ed */
1097 
1098 
1099  /* initialize window, bit buffer */
1100  wp = 0;
1101  bk = 0;
1102  bb = 0;
1103 
1104 
1105  /* decompress until the last block */
1106  h = 0;
1107  do {
1108  hufts = 0;
1109  if ((r = csz__Inflate_block(&e)) != 0)
1110  return r;
1111  if (hufts > h)
1112  h = hufts;
1113  } while (!e);
1114 
1115 
1116  /* flush out slide */
1117  FLUSH(wp);
1118 
1119 
1120  /* return success */
1121  Trace((stderr, "\n%lu bytes in Huffman tables (%lu/entry)\n",
1122  h * sizeof(struct huft), sizeof(struct huft)));
1123  return 0;
1124 }
1125 
1127 {
1128  if (csz__fixed_tl != (struct huft *)NULL)
1129  {
1130  csz__huft_free(csz__fixed_td);
1131  csz__huft_free(csz__fixed_tl);
1132  csz__fixed_td = csz__fixed_tl = (struct huft *)NULL;
1133  }
1134  return 0;
1135 }
1136 
1137 /* G.Barrand */
1138 void csz__Init_Inflate(long a_ibufcnt,unsigned char* a_ibufptr,
1139  long a_obufcnt,unsigned char* a_obufptr) {
1140  ibufcnt = a_ibufcnt;
1141  ibufptr = a_ibufptr;
1142 
1143  obufcnt = a_obufcnt;
1144  obufptr = a_obufptr;
1145 }
1146 unsigned char* csz__obufptr() {return obufptr;}
1147 
1148 #ifdef __cplusplus /*G.Barrand*/
1149 }
1150 #endif
1151 
1152 #ifndef CHECK_EOF
1153 static int csz__ReadByte ()
1154 {
1155  int k;
1156  if(ibufcnt-- <= 0)
1157  k = -1;
1158  else
1159  k = *ibufptr++;
1160  return k;
1161 }
1162 #endif
1163 
1164 static void csz__WriteData(int n)
1165 {
1166  if( obufcnt >= n ) memcpy(obufptr, csz__slide, n);
1167  obufptr += n;
1168  obufcnt -= n;
1169 }
1170 
static ush cplens[]
Definition: csz_inflate.cc:315
unsigned long ulg
Definition: csz_inflate.cc:226
struct huft * csz__fixed_td
Definition: csz_inflate.cc:818
static unsigned border[]
Definition: csz_inflate.cc:313
static long obufcnt
Definition: csz_inflate.cc:362
uch e
Definition: csz_inflate.cc:263
struct huft * csz__fixed_tl
Definition: csz_inflate.cc:817
int csz__Inflate_free(void)
static ush mask[]
Definition: csz_inflate.cc:332
G4double z
Definition: TRTMaterials.hh:39
#define FPRINTF
Definition: csz_inflate.cc:237
int csz__Inflate_codes(struct huft *, struct huft *, int, int)
Definition: csz_inflate.cc:653
ush n
Definition: csz_inflate.cc:266
void csz__Init_Inflate(long a_ibufcnt, unsigned char *a_ibufptr, long a_obufcnt, unsigned char *a_obufptr)
#define Trace(x)
Definition: csz_inflate.cc:250
static ush cplext[]
Definition: csz_inflate.cc:319
#define BMAX
Definition: csz_inflate.cc:418
G4double a
Definition: TRTMaterials.hh:39
#define N_MAX
Definition: csz_inflate.cc:419
static unsigned wp
Definition: csz_inflate.cc:309
#define NEEDBITS_free_tl(n)
#define WSIZE
Definition: csz_inflate.cc:229
static ulg bb
Definition: csz_inflate.cc:359
static const double s
Definition: G4SIunits.hh:150
static ush cpdext[]
Definition: csz_inflate.cc:326
static int lbits
Definition: csz_inflate.cc:413
static uch csz__slide[32768]
Definition: csz_inflate.cc:308
#define NEEDBITS(n)
Definition: csz_inflate.cc:374
uch b
Definition: csz_inflate.cc:264
static unsigned hufts
Definition: csz_inflate.cc:422
int csz__Inflate_dynamic(void)
Definition: csz_inflate.cc:869
int csz__Inflate_block(int *)
int csz__fixed_bl
Definition: csz_inflate.cc:819
int csz__Inflate_fixed(void)
Definition: csz_inflate.cc:821
char boolean
Definition: csz_inflate.cc:223
const G4int n
int csz__huft_build(unsigned *, unsigned, unsigned, ush *, ush *, struct huft **, int *)
Definition: csz_inflate.cc:425
#define DUMPBITS(n)
Definition: csz_inflate.cc:377
unsigned char * csz__obufptr()
union huft::@10 v
static long ibufcnt
Definition: csz_inflate.cc:362
unsigned char uch
Definition: csz_inflate.cc:224
static const double g
Definition: G4SIunits.hh:162
unsigned short ush
Definition: csz_inflate.cc:225
int csz__fixed_bd
Definition: csz_inflate.cc:819
static const double m
Definition: G4SIunits.hh:110
struct huft * t
Definition: csz_inflate.cc:267
static ush cpdist[]
Definition: csz_inflate.cc:322
static uch * obufptr
Definition: csz_inflate.cc:361
static unsigned bk
Definition: csz_inflate.cc:360
static int dbits
Definition: csz_inflate.cc:414
#define FLUSH(n)
Definition: csz_inflate.cc:241
int csz__Inflate(void)
static uch * ibufptr
Definition: csz_inflate.cc:361
static void csz__WriteData(int)
int csz__Inflate_stored(void)
Definition: csz_inflate.cc:763
int csz__huft_free(struct huft *)
Definition: csz_inflate.cc:624