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