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