bszlib.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:275k
- result := Z_STREAM_ERROR; { Some dumb compilers complain without this }
- {$endif}
- end;
- function inflateSetDictionary(var z : z_stream;
- dictionary : pBytef; {const array of byte}
- dictLength : uInt) : int;
- var
- length : uInt;
- begin
- length := dictLength;
- if (z.state = Z_NULL) or (z.state^.mode <> DICT0) then
- begin
- inflateSetDictionary := Z_STREAM_ERROR;
- exit;
- end;
- if (adler32(Long(1), dictionary, dictLength) <> z.adler) then
- begin
- inflateSetDictionary := Z_DATA_ERROR;
- exit;
- end;
- z.adler := Long(1);
- if (length >= (uInt(1) shl z.state^.wbits)) then
- begin
- length := (1 shl z.state^.wbits)-1;
- Inc( dictionary, dictLength - length);
- end;
- inflate_set_dictionary(z.state^.blocks^, dictionary^, length);
- z.state^.mode := BLOCKS;
- inflateSetDictionary := Z_OK;
- end;
- function inflateSync(var z : z_stream) : int;
- const
- mark : packed array[0..3] of byte = (0, 0, $ff, $ff);
- var
- n : uInt; { number of bytes to look at }
- p : pBytef; { pointer to bytes }
- m : uInt; { number of marker bytes found in a row }
- r, w : uLong; { temporaries to save total_in and total_out }
- begin
- { set up }
- if (z.state = Z_NULL) then
- begin
- inflateSync := Z_STREAM_ERROR;
- exit;
- end;
- if (z.state^.mode <> BAD) then
- begin
- z.state^.mode := BAD;
- z.state^.sub.marker := 0;
- end;
- n := z.avail_in;
- if (n = 0) then
- begin
- inflateSync := Z_BUF_ERROR;
- exit;
- end;
- p := z.next_in;
- m := z.state^.sub.marker;
- { search }
- while (n <> 0) and (m < 4) do
- begin
- if (p^ = mark[m]) then
- Inc(m)
- else
- if (p^ <> 0) then
- m := 0
- else
- m := 4 - m;
- Inc(p);
- Dec(n);
- end;
- { restore }
- Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
- z.next_in := p;
- z.avail_in := n;
- z.state^.sub.marker := m;
- { return no joy or set up to restart on a new block }
- if (m <> 4) then
- begin
- inflateSync := Z_DATA_ERROR;
- exit;
- end;
- r := z.total_in;
- w := z.total_out;
- inflateReset(z);
- z.total_in := r;
- z.total_out := w;
- z.state^.mode := BLOCKS;
- inflateSync := Z_OK;
- end;
- {
- returns true if inflate is currently at the end of a block generated
- by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
- implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
- but removes the length bytes of the resulting empty stored block. When
- decompressing, PPP checks that at the end of input packet, inflate is
- waiting for these length bytes.
- }
- function inflateSyncPoint(var z : z_stream) : int;
- begin
- if (z.state = Z_NULL) or (z.state^.blocks = Z_NULL) then
- begin
- inflateSyncPoint := Z_STREAM_ERROR;
- exit;
- end;
- inflateSyncPoint := inflate_blocks_sync_point(z.state^.blocks^);
- end;
- const
- inflate_copyright = 'inflate 1.1.2 Copyright 1995-1998 Mark Adler';
- {
- If you use the zlib library in a product, an acknowledgment is welcome
- in the documentation of your product. If for some reason you cannot
- include such an acknowledgment, I would appreciate that you keep this
- copyright string in the executable of your product.
- }
- const
- { Tables for deflate from PKZIP's appnote.txt. }
- cplens : Array [0..30] Of uInt { Copy lengths for literal codes 257..285 }
- = (3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
- 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0);
- { actually lengths - 2; also see note #13 above about 258 }
- invalid_code = 112;
- cplext : Array [0..30] Of uInt { Extra bits for literal codes 257..285 }
- = (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
- 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, invalid_code, invalid_code);
- cpdist : Array [0..29] Of uInt { Copy offsets for distance codes 0..29 }
- = (1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
- 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
- 8193, 12289, 16385, 24577);
- cpdext : Array [0..29] Of uInt { Extra bits for distance codes }
- = (0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
- 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
- 12, 12, 13, 13);
- { Huffman code decoding is performed using a multi-level table lookup.
- The fastest way to decode is to simply build a lookup table whose
- size is determined by the longest code. However, the time it takes
- to build this table can also be a factor if the data being decoded
- is not very long. The most common codes are necessarily the
- shortest codes, so those codes dominate the decoding time, and hence
- the speed. The idea is you can have a shorter table that decodes the
- shorter, more probable codes, and then point to subsidiary tables for
- the longer codes. The time it costs to decode the longer codes is
- then traded against the time it takes to make longer tables.
- This results of this trade are in the variables lbits and dbits
- below. lbits is the number of bits the first level table for literal/
- length codes can decode in one step, and dbits is the same thing for
- the distance codes. Subsequent tables are also less than or equal to
- those sizes. These values may be adjusted either when all of the
- codes are shorter than that, in which case the longest code length in
- bits is used, or when the shortest code is *longer* than the requested
- table size, in which case the length of the shortest code in bits is
- used.
- There are two different values for the two tables, since they code a
- different number of possibilities each. The literal/length table
- codes 286 possible values, or in a flat code, a little over eight
- bits. The distance table codes 30 possible values, or a little less
- than five bits, flat. The optimum values for speed end up being
- about one bit more than those, so lbits is 8+1 and dbits is 5+1.
- The optimum values may differ though from machine to machine, and
- possibly even between compilers. Your mileage may vary. }
- { If BMAX needs to be larger than 16, then h and x[] should be uLong. }
- const
- BMAX = 15; { maximum bit length of any code }
- {$DEFINE USE_PTR}
- function huft_build(
- var b : array of uIntf; { code lengths in bits (all assumed <= BMAX) }
- n : uInt; { number of codes (assumed <= N_MAX) }
- s : uInt; { number of simple-valued codes (0..s-1) }
- const d : array of uIntf; { list of base values for non-simple codes }
- { array of word }
- const e : array of uIntf; { list of extra bits for non-simple codes }
- { array of byte }
- t : ppInflate_huft; { result: starting table }
- var m : uIntf; { maximum lookup bits, returns actual }
- var hp : array of inflate_huft; { space for trees }
- var hn : uInt; { hufts used in space }
- var v : array of uIntf { working area: values in order of bit length }
- ) : int;
- { Given a list of code lengths and a maximum table size, make a set of
- tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
- if the given code set is incomplete (the tables are still built in this
- case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
- lengths), or Z_MEM_ERROR if not enough memory. }
- Var
- a : uInt; { counter for codes of length k }
- c : Array [0..BMAX] Of uInt; { bit length count table }
- f : uInt; { i repeats in table every f entries }
- g : int; { maximum code length }
- h : int; { table level }
- i : uInt; {register} { counter, current code }
- j : uInt; {register} { counter }
- k : Int; {register} { number of bits in current code }
- l : int; { bits per table (returned in m) }
- mask : uInt; { (1 shl w) - 1, to avoid cc -O bug on HP }
- p : ^uIntf; {register} { pointer into c[], b[], or v[] }
- q : pInflate_huft; { points to current table }
- r : inflate_huft; { table entry for structure assignment }
- u : Array [0..BMAX-1] Of pInflate_huft; { table stack }
- w : int; {register} { bits before this table = (l*h) }
- x : Array [0..BMAX] Of uInt; { bit offsets, then code stack }
- {$IFDEF USE_PTR}
- xp : puIntf; { pointer into x }
- {$ELSE}
- xp : uInt;
- {$ENDIF}
- y : int; { number of dummy codes added }
- z : uInt; { number of entries in current table }
- Begin
- { Generate counts for each bit length }
- FillChar(c,SizeOf(c),0) ; { clear c[] }
- for i := 0 to n-1 do
- Inc (c[b[i]]); { assume all entries <= BMAX }
- If (c[0] = n) Then { null input--all zero length codes }
- Begin
- t^ := pInflate_huft(NIL);
- m := 0 ;
- huft_build := Z_OK ;
- Exit;
- End ;
- { Find minimum and maximum length, bound [m] by those }
- l := m;
- for j:=1 To BMAX do
- if (c[j] <> 0) then
- break;
- k := j ; { minimum code length }
- if (uInt(l) < j) then
- l := j;
- for i := BMAX downto 1 do
- if (c[i] <> 0) then
- break ;
- g := i ; { maximum code length }
- if (uInt(l) > i) then
- l := i;
- m := l;
- { Adjust last length count to fill out codes, if needed }
- y := 1 shl j ;
- while (j < i) do
- begin
- Dec(y, c[j]) ;
- if (y < 0) then
- begin
- huft_build := Z_DATA_ERROR; { bad input: more codes than bits }
- exit;
- end ;
- Inc(j) ;
- y := y shl 1
- end;
- Dec (y, c[i]) ;
- if (y < 0) then
- begin
- huft_build := Z_DATA_ERROR; { bad input: more codes than bits }
- exit;
- end;
- Inc(c[i], y);
- { Generate starting offsets into the value table FOR each length }
- {$IFDEF USE_PTR}
- x[1] := 0;
- j := 0;
- p := @c[1];
- xp := @x[2];
- dec(i); { note that i = g from above }
- WHILE (i > 0) DO
- BEGIN
- inc(j, p^);
- xp^ := j;
- inc(p);
- inc(xp);
- dec(i);
- END;
- {$ELSE}
- x[1] := 0;
- j := 0 ;
- for i := 1 to g do
- begin
- x[i] := j;
- Inc(j, c[i]);
- end;
- {$ENDIF}
- { Make a table of values in order of bit lengths }
- for i := 0 to n-1 do
- begin
- j := b[i];
- if (j <> 0) then
- begin
- v[ x[j] ] := i;
- Inc(x[j]);
- end;
- end;
- n := x[g]; { set n to length of v }
- { Generate the Huffman codes and for each, make the table entries }
- i := 0 ;
- x[0] := 0 ; { first Huffman code is zero }
- p := Addr(v) ; { grab values in bit order }
- h := -1 ; { no tables yet--level -1 }
- w := -l ; { bits decoded = (l*h) }
- u[0] := pInflate_huft(NIL); { just to keep compilers happy }
- q := pInflate_huft(NIL); { ditto }
- z := 0 ; { ditto }
- { go through the bit lengths (k already is bits in shortest code) }
- while (k <= g) Do
- begin
- a := c[k] ;
- while (a<>0) Do
- begin
- Dec (a) ;
- { here i is the Huffman code of length k bits for value p^ }
- { make tables up to required level }
- while (k > w + l) do
- begin
- Inc (h) ;
- Inc (w, l); { add bits already decoded }
- { previous table always l bits }
- { compute minimum size table less than or equal to l bits }
- { table size upper limit }
- z := g - w;
- If (z > uInt(l)) Then
- z := l;
- { try a k-w bit table }
- j := k - w;
- f := 1 shl j;
- if (f > a+1) Then { too few codes for k-w bit table }
- begin
- Dec(f, a+1); { deduct codes from patterns left }
- {$IFDEF USE_PTR}
- xp := Addr(c[k]);
- if (j < z) then
- begin
- Inc(j);
- while (j < z) do
- begin { try smaller tables up to z bits }
- f := f shl 1;
- Inc (xp) ;
- If (f <= xp^) Then
- break; { enough codes to use up j bits }
- Dec(f, xp^); { else deduct codes from patterns }
- Inc(j);
- end;
- end;
- {$ELSE}
- xp := k;
- if (j < z) then
- begin
- Inc (j) ;
- While (j < z) Do
- begin { try smaller tables up to z bits }
- f := f * 2;
- Inc (xp) ;
- if (f <= c[xp]) then
- Break ; { enough codes to use up j bits }
- Dec (f, c[xp]) ; { else deduct codes from patterns }
- Inc (j);
- end;
- end;
- {$ENDIF}
- end;
- z := 1 shl j; { table entries for j-bit table }
- { allocate new table }
- if (hn + z > MANY) then { (note: doesn't matter for fixed) }
- begin
- huft_build := Z_MEM_ERROR; { not enough memory }
- exit;
- end;
- q := @hp[hn];
- u[h] := q;
- Inc(hn, z);
- { connect to last table, if there is one }
- if (h <> 0) then
- begin
- x[h] := i; { save pattern for backing up }
- r.bits := Byte(l); { bits to dump before this table }
- r.exop := Byte(j); { bits in this table }
- j := i shr (w - l);
- {r.base := uInt( q - u[h-1] -j);} { offset to this table }
- r.base := (ptr2int(q) - ptr2int(u[h-1]) ) div sizeof(q^) - j;
- huft_Ptr(u[h-1])^[j] := r; { connect to last table }
- end
- else
- t^ := q; { first table is returned result }
- end;
- { set up table entry in r }
- r.bits := Byte(k - w);
- { C-code: if (p >= v + n) - see ZUTIL.PAS for comments }
- if ptr2int(p)>=ptr2int(@(v[n])) then { also works under DPMI ?? }
- r.exop := 128 + 64 { out of values--invalid code }
- else
- if (p^ < s) then
- begin
- if (p^ < 256) then { 256 is end-of-block code }
- r.exop := 0
- Else
- r.exop := 32 + 64; { EOB_code; }
- r.base := p^; { simple code is just the value }
- Inc(p);
- end
- Else
- begin
- r.exop := Byte(e[p^-s] + 16 + 64); { non-simple--look up in lists }
- r.base := d[p^-s];
- Inc (p);
- end ;
- { fill code-like entries with r }
- f := 1 shl (k - w);
- j := i shr w;
- while (j < z) do
- begin
- huft_Ptr(q)^[j] := r;
- Inc(j, f);
- end;
- { backwards increment the k-bit code i }
- j := 1 shl (k-1) ;
- while (i and j) <> 0 do
- begin
- i := i xor j; { bitwise exclusive or }
- j := j shr 1
- end ;
- i := i xor j;
- { backup over finished tables }
- mask := (1 shl w) - 1; { needed on HP, cc -O bug }
- while ((i and mask) <> x[h]) do
- begin
- Dec(h); { don't need to update q }
- Dec(w, l);
- mask := (1 shl w) - 1;
- end;
- end;
- Inc(k);
- end;
- { Return Z_BUF_ERROR if we were given an incomplete table }
- if (y <> 0) And (g <> 1) then
- huft_build := Z_BUF_ERROR
- else
- huft_build := Z_OK;
- end; { huft_build}
- function inflate_trees_bits(
- var c : array of uIntf; { 19 code lengths }
- var bb : uIntf; { bits tree desired/actual depth }
- var tb : pinflate_huft; { bits tree result }
- var hp : array of Inflate_huft; { space for trees }
- var z : z_stream { for messages }
- ) : int;
- var
- r : int;
- hn : uInt; { hufts used in space }
- v : PuIntArray; { work area for huft_build }
- begin
- hn := 0;
- v := PuIntArray( ZALLOC(z, 19, sizeof(uInt)) );
- if (v = Z_NULL) then
- begin
- inflate_trees_bits := Z_MEM_ERROR;
- exit;
- end;
- r := huft_build(c, 19, 19, cplens, cplext,
- {puIntf(Z_NULL), puIntf(Z_NULL),}
- @tb, bb, hp, hn, v^);
- if (r = Z_DATA_ERROR) then
- z.msg := 'oversubscribed dynamic bit lengths tree'
- else
- if (r = Z_BUF_ERROR) or (bb = 0) then
- begin
- z.msg := 'incomplete dynamic bit lengths tree';
- r := Z_DATA_ERROR;
- end;
- ZFREE(z, v);
- inflate_trees_bits := r;
- end;
- function inflate_trees_dynamic(
- nl : uInt; { number of literal/length codes }
- nd : uInt; { number of distance codes }
- var c : Array of uIntf; { that many (total) code lengths }
- var bl : uIntf; { literal desired/actual bit depth }
- var bd : uIntf; { distance desired/actual bit depth }
- var tl : pInflate_huft; { literal/length tree result }
- var td : pInflate_huft; { distance tree result }
- var hp : array of Inflate_huft; { space for trees }
- var z : z_stream { for messages }
- ) : int;
- var
- r : int;
- hn : uInt; { hufts used in space }
- v : PuIntArray; { work area for huft_build }
- begin
- hn := 0;
- { allocate work area }
- v := PuIntArray( ZALLOC(z, 288, sizeof(uInt)) );
- if (v = Z_NULL) then
- begin
- inflate_trees_dynamic := Z_MEM_ERROR;
- exit;
- end;
- { build literal/length tree }
- r := huft_build(c, nl, 257, cplens, cplext, @tl, bl, hp, hn, v^);
- if (r <> Z_OK) or (bl = 0) then
- begin
- if (r = Z_DATA_ERROR) then
- z.msg := 'oversubscribed literal/length tree'
- else
- if (r <> Z_MEM_ERROR) then
- begin
- z.msg := 'incomplete literal/length tree';
- r := Z_DATA_ERROR;
- end;
- ZFREE(z, v);
- inflate_trees_dynamic := r;
- exit;
- end;
- { build distance tree }
- r := huft_build(puIntArray(@c[nl])^, nd, 0,
- cpdist, cpdext, @td, bd, hp, hn, v^);
- if (r <> Z_OK) or ((bd = 0) and (nl > 257)) then
- begin
- if (r = Z_DATA_ERROR) then
- z.msg := 'oversubscribed literal/length tree'
- else
- if (r = Z_BUF_ERROR) then
- begin
- {$ifdef PKZIP_BUG_WORKAROUND}
- r := Z_OK;
- end;
- {$else}
- z.msg := 'incomplete literal/length tree';
- r := Z_DATA_ERROR;
- end
- else
- if (r <> Z_MEM_ERROR) then
- begin
- z.msg := 'empty distance tree with lengths';
- r := Z_DATA_ERROR;
- end;
- ZFREE(z, v);
- inflate_trees_dynamic := r;
- exit;
- {$endif}
- end;
- { done }
- ZFREE(z, v);
- inflate_trees_dynamic := Z_OK;
- end;
- {$UNDEF BUILDFIXED}
- { build fixed tables only once--keep them here }
- {$IFNDEF BUILDFIXED}
- { locals }
- const
- {$WRITEABLECONST ON}
- fixed_built : Boolean = false;
- {$WRITEABLECONST OFF}
- FIXEDH = 544; { number of hufts used by fixed tables }
- var
- fixed_mem : array[0..FIXEDH-1] of inflate_huft;
- fixed_bl : uInt;
- fixed_bd : uInt;
- fixed_tl : pInflate_huft;
- fixed_td : pInflate_huft;
- {$ELSE}
- { inffixed.h -- table for decoding fixed codes }
- {local}
- const
- fixed_bl = uInt(9);
- {local}
- const
- fixed_bd = uInt(5);
- {local}
- const
- fixed_tl : array [0..288-1] of inflate_huft = (
- Exop, { number of extra bits or operation }
- bits : Byte; { number of bits in this code or subcode }
- {pad : uInt;} { pad structure to a power of 2 (4 bytes for }
- { 16-bit, 8 bytes for 32-bit int's) }
- base : uInt; { literal, length base, or distance base }
- { or table offset }
- ((96,7),256), ((0,8),80), ((0,8),16), ((84,8),115), ((82,7),31),
- ((0,8),112), ((0,8),48), ((0,9),192), ((80,7),10), ((0,8),96),
- ((0,8),32), ((0,9),160), ((0,8),0), ((0,8),128), ((0,8),64),
- ((0,9),224), ((80,7),6), ((0,8),88), ((0,8),24), ((0,9),144),
- ((83,7),59), ((0,8),120), ((0,8),56), ((0,9),208), ((81,7),17),
- ((0,8),104), ((0,8),40), ((0,9),176), ((0,8),8), ((0,8),136),
- ((0,8),72), ((0,9),240), ((80,7),4), ((0,8),84), ((0,8),20),
- ((85,8),227), ((83,7),43), ((0,8),116), ((0,8),52), ((0,9),200),
- ((81,7),13), ((0,8),100), ((0,8),36), ((0,9),168), ((0,8),4),
- ((0,8),132), ((0,8),68), ((0,9),232), ((80,7),8), ((0,8),92),
- ((0,8),28), ((0,9),152), ((84,7),83), ((0,8),124), ((0,8),60),
- ((0,9),216), ((82,7),23), ((0,8),108), ((0,8),44), ((0,9),184),
- ((0,8),12), ((0,8),140), ((0,8),76), ((0,9),248), ((80,7),3),
- ((0,8),82), ((0,8),18), ((85,8),163), ((83,7),35), ((0,8),114),
- ((0,8),50), ((0,9),196), ((81,7),11), ((0,8),98), ((0,8),34),
- ((0,9),164), ((0,8),2), ((0,8),130), ((0,8),66), ((0,9),228),
- ((80,7),7), ((0,8),90), ((0,8),26), ((0,9),148), ((84,7),67),
- ((0,8),122), ((0,8),58), ((0,9),212), ((82,7),19), ((0,8),106),
- ((0,8),42), ((0,9),180), ((0,8),10), ((0,8),138), ((0,8),74),
- ((0,9),244), ((80,7),5), ((0,8),86), ((0,8),22), ((192,8),0),
- ((83,7),51), ((0,8),118), ((0,8),54), ((0,9),204), ((81,7),15),
- ((0,8),102), ((0,8),38), ((0,9),172), ((0,8),6), ((0,8),134),
- ((0,8),70), ((0,9),236), ((80,7),9), ((0,8),94), ((0,8),30),
- ((0,9),156), ((84,7),99), ((0,8),126), ((0,8),62), ((0,9),220),
- ((82,7),27), ((0,8),110), ((0,8),46), ((0,9),188), ((0,8),14),
- ((0,8),142), ((0,8),78), ((0,9),252), ((96,7),256), ((0,8),81),
- ((0,8),17), ((85,8),131), ((82,7),31), ((0,8),113), ((0,8),49),
- ((0,9),194), ((80,7),10), ((0,8),97), ((0,8),33), ((0,9),162),
- ((0,8),1), ((0,8),129), ((0,8),65), ((0,9),226), ((80,7),6),
- ((0,8),89), ((0,8),25), ((0,9),146), ((83,7),59), ((0,8),121),
- ((0,8),57), ((0,9),210), ((81,7),17), ((0,8),105), ((0,8),41),
- ((0,9),178), ((0,8),9), ((0,8),137), ((0,8),73), ((0,9),242),
- ((80,7),4), ((0,8),85), ((0,8),21), ((80,8),258), ((83,7),43),
- ((0,8),117), ((0,8),53), ((0,9),202), ((81,7),13), ((0,8),101),
- ((0,8),37), ((0,9),170), ((0,8),5), ((0,8),133), ((0,8),69),
- ((0,9),234), ((80,7),8), ((0,8),93), ((0,8),29), ((0,9),154),
- ((84,7),83), ((0,8),125), ((0,8),61), ((0,9),218), ((82,7),23),
- ((0,8),109), ((0,8),45), ((0,9),186), ((0,8),13), ((0,8),141),
- ((0,8),77), ((0,9),250), ((80,7),3), ((0,8),83), ((0,8),19),
- ((85,8),195), ((83,7),35), ((0,8),115), ((0,8),51), ((0,9),198),
- ((81,7),11), ((0,8),99), ((0,8),35), ((0,9),166), ((0,8),3),
- ((0,8),131), ((0,8),67), ((0,9),230), ((80,7),7), ((0,8),91),
- ((0,8),27), ((0,9),150), ((84,7),67), ((0,8),123), ((0,8),59),
- ((0,9),214), ((82,7),19), ((0,8),107), ((0,8),43), ((0,9),182),
- ((0,8),11), ((0,8),139), ((0,8),75), ((0,9),246), ((80,7),5),
- ((0,8),87), ((0,8),23), ((192,8),0), ((83,7),51), ((0,8),119),
- ((0,8),55), ((0,9),206), ((81,7),15), ((0,8),103), ((0,8),39),
- ((0,9),174), ((0,8),7), ((0,8),135), ((0,8),71), ((0,9),238),
- ((80,7),9), ((0,8),95), ((0,8),31), ((0,9),158), ((84,7),99),
- ((0,8),127), ((0,8),63), ((0,9),222), ((82,7),27), ((0,8),111),
- ((0,8),47), ((0,9),190), ((0,8),15), ((0,8),143), ((0,8),79),
- ((0,9),254), ((96,7),256), ((0,8),80), ((0,8),16), ((84,8),115),
- ((82,7),31), ((0,8),112), ((0,8),48), ((0,9),193), ((80,7),10),
- ((0,8),96), ((0,8),32), ((0,9),161), ((0,8),0), ((0,8),128),
- ((0,8),64), ((0,9),225), ((80,7),6), ((0,8),88), ((0,8),24),
- ((0,9),145), ((83,7),59), ((0,8),120), ((0,8),56), ((0,9),209),
- ((81,7),17), ((0,8),104), ((0,8),40), ((0,9),177), ((0,8),8),
- ((0,8),136), ((0,8),72), ((0,9),241), ((80,7),4), ((0,8),84),
- ((0,8),20), ((85,8),227), ((83,7),43), ((0,8),116), ((0,8),52),
- ((0,9),201), ((81,7),13), ((0,8),100), ((0,8),36), ((0,9),169),
- ((0,8),4), ((0,8),132), ((0,8),68), ((0,9),233), ((80,7),8),
- ((0,8),92), ((0,8),28), ((0,9),153), ((84,7),83), ((0,8),124),
- ((0,8),60), ((0,9),217), ((82,7),23), ((0,8),108), ((0,8),44),
- ((0,9),185), ((0,8),12), ((0,8),140), ((0,8),76), ((0,9),249),
- ((80,7),3), ((0,8),82), ((0,8),18), ((85,8),163), ((83,7),35),
- ((0,8),114), ((0,8),50), ((0,9),197), ((81,7),11), ((0,8),98),
- ((0,8),34), ((0,9),165), ((0,8),2), ((0,8),130), ((0,8),66),
- ((0,9),229), ((80,7),7), ((0,8),90), ((0,8),26), ((0,9),149),
- ((84,7),67), ((0,8),122), ((0,8),58), ((0,9),213), ((82,7),19),
- ((0,8),106), ((0,8),42), ((0,9),181), ((0,8),10), ((0,8),138),
- ((0,8),74), ((0,9),245), ((80,7),5), ((0,8),86), ((0,8),22),
- ((192,8),0), ((83,7),51), ((0,8),118), ((0,8),54), ((0,9),205),
- ((81,7),15), ((0,8),102), ((0,8),38), ((0,9),173), ((0,8),6),
- ((0,8),134), ((0,8),70), ((0,9),237), ((80,7),9), ((0,8),94),
- ((0,8),30), ((0,9),157), ((84,7),99), ((0,8),126), ((0,8),62),
- ((0,9),221), ((82,7),27), ((0,8),110), ((0,8),46), ((0,9),189),
- ((0,8),14), ((0,8),142), ((0,8),78), ((0,9),253), ((96,7),256),
- ((0,8),81), ((0,8),17), ((85,8),131), ((82,7),31), ((0,8),113),
- ((0,8),49), ((0,9),195), ((80,7),10), ((0,8),97), ((0,8),33),
- ((0,9),163), ((0,8),1), ((0,8),129), ((0,8),65), ((0,9),227),
- ((80,7),6), ((0,8),89), ((0,8),25), ((0,9),147), ((83,7),59),
- ((0,8),121), ((0,8),57), ((0,9),211), ((81,7),17), ((0,8),105),
- ((0,8),41), ((0,9),179), ((0,8),9), ((0,8),137), ((0,8),73),
- ((0,9),243), ((80,7),4), ((0,8),85), ((0,8),21), ((80,8),258),
- ((83,7),43), ((0,8),117), ((0,8),53), ((0,9),203), ((81,7),13),
- ((0,8),101), ((0,8),37), ((0,9),171), ((0,8),5), ((0,8),133),
- ((0,8),69), ((0,9),235), ((80,7),8), ((0,8),93), ((0,8),29),
- ((0,9),155), ((84,7),83), ((0,8),125), ((0,8),61), ((0,9),219),
- ((82,7),23), ((0,8),109), ((0,8),45), ((0,9),187), ((0,8),13),
- ((0,8),141), ((0,8),77), ((0,9),251), ((80,7),3), ((0,8),83),
- ((0,8),19), ((85,8),195), ((83,7),35), ((0,8),115), ((0,8),51),
- ((0,9),199), ((81,7),11), ((0,8),99), ((0,8),35), ((0,9),167),
- ((0,8),3), ((0,8),131), ((0,8),67), ((0,9),231), ((80,7),7),
- ((0,8),91), ((0,8),27), ((0,9),151), ((84,7),67), ((0,8),123),
- ((0,8),59), ((0,9),215), ((82,7),19), ((0,8),107), ((0,8),43),
- ((0,9),183), ((0,8),11), ((0,8),139), ((0,8),75), ((0,9),247),
- ((80,7),5), ((0,8),87), ((0,8),23), ((192,8),0), ((83,7),51),
- ((0,8),119), ((0,8),55), ((0,9),207), ((81,7),15), ((0,8),103),
- ((0,8),39), ((0,9),175), ((0,8),7), ((0,8),135), ((0,8),71),
- ((0,9),239), ((80,7),9), ((0,8),95), ((0,8),31), ((0,9),159),
- ((84,7),99), ((0,8),127), ((0,8),63), ((0,9),223), ((82,7),27),
- ((0,8),111), ((0,8),47), ((0,9),191), ((0,8),15), ((0,8),143),
- ((0,8),79), ((0,9),255)
- );
- {local}
- const
- fixed_td : array[0..32-1] of inflate_huft = (
- (Exop:80;bits:5;base:1), (Exop:87;bits:5;base:257), (Exop:83;bits:5;base:17),
- (Exop:91;bits:5;base:4097), (Exop:81;bits:5;base), (Exop:89;bits:5;base:1025),
- (Exop:85;bits:5;base:65), (Exop:93;bits:5;base:16385), (Exop:80;bits:5;base:3),
- (Exop:88;bits:5;base:513), (Exop:84;bits:5;base:33), (Exop:92;bits:5;base:8193),
- (Exop:82;bits:5;base:9), (Exop:90;bits:5;base:2049), (Exop:86;bits:5;base:129),
- (Exop:192;bits:5;base:24577), (Exop:80;bits:5;base:2), (Exop:87;bits:5;base:385),
- (Exop:83;bits:5;base:25), (Exop:91;bits:5;base:6145), (Exop:81;bits:5;base:7),
- (Exop:89;bits:5;base:1537), (Exop:85;bits:5;base:97), (Exop:93;bits:5;base:24577),
- (Exop:80;bits:5;base:4), (Exop:88;bits:5;base:769), (Exop:84;bits:5;base:49),
- (Exop:92;bits:5;base:12289), (Exop:82;bits:5;base:13), (Exop:90;bits:5;base:3073),
- (Exop:86;bits:5;base:193), (Exop:192;bits:5;base:24577)
- );
- {$ENDIF}
- function inflate_trees_fixed(
- var bl : uIntf; { literal desired/actual bit depth }
- var bd : uIntf; { distance desired/actual bit depth }
- var tl : pInflate_huft; { literal/length tree result }
- var td : pInflate_huft; { distance tree result }
- var z : z_stream { for memory allocation }
- ) : int;
- type
- pFixed_table = ^fixed_table;
- fixed_table = array[0..288-1] of uIntf;
- var
- k : int; { temporary variable }
- c : pFixed_table; { length list for huft_build }
- v : PuIntArray; { work area for huft_build }
- var
- f : uInt; { number of hufts used in fixed_mem }
- begin
- { build fixed tables if not already (multiple overlapped executions ok) }
- if not fixed_built then
- begin
- f := 0;
- { allocate memory }
- c := pFixed_table( ZALLOC(z, 288, sizeof(uInt)) );
- if (c = Z_NULL) then
- begin
- inflate_trees_fixed := Z_MEM_ERROR;
- exit;
- end;
- v := PuIntArray( ZALLOC(z, 288, sizeof(uInt)) );
- if (v = Z_NULL) then
- begin
- ZFREE(z, c);
- inflate_trees_fixed := Z_MEM_ERROR;
- exit;
- end;
- { literal table }
- for k := 0 to Pred(144) do
- c^[k] := 8;
- for k := 144 to Pred(256) do
- c^[k] := 9;
- for k := 256 to Pred(280) do
- c^[k] := 7;
- for k := 280 to Pred(288) do
- c^[k] := 8;
- fixed_bl := 9;
- huft_build(c^, 288, 257, cplens, cplext, @fixed_tl, fixed_bl,
- fixed_mem, f, v^);
- { distance table }
- for k := 0 to Pred(30) do
- c^[k] := 5;
- fixed_bd := 5;
- huft_build(c^, 30, 0, cpdist, cpdext, @fixed_td, fixed_bd,
- fixed_mem, f, v^);
- { done }
- ZFREE(z, v);
- ZFREE(z, c);
- fixed_built := True;
- end;
- bl := fixed_bl;
- bd := fixed_bd;
- tl := fixed_tl;
- td := fixed_td;
- inflate_trees_fixed := Z_OK;
- end; { inflate_trees_fixed }
- { macros for bit input with no checking and for returning unused bytes }
- procedure GRABBITS(j : int);
- begin
- {while (k < j) do
- begin
- Dec(z^.avail_in);
- Inc(z^.total_in);
- b := b or (uLong(z^.next_in^) shl k);
- Inc(z^.next_in);
- Inc(k, 8);
- end;}
- end;
- procedure DUMPBITS(j : int);
- begin
- {b := b shr j;
- Dec(k, j);}
- end;
- procedure NEEDBITS(j : int);
- begin
- (*
- while (k < j) do
- begin
- {NEEDBYTE;}
- if (n <> 0) then
- r :=Z_OK
- else
- begin
- {UPDATE}
- s.bitb := b;
- s.bitk := k;
- z.avail_in := n;
- Inc(z.total_in, LongInt(p)-LongInt(z.next_in));
- z.next_in := p;
- s.write := q;
- result := inflate_flush(s,z,r);
- exit;
- end;
- Dec(n);
- b := b or (uLong(p^) shl k);
- Inc(p);
- Inc(k, 8);
- end;
- *)
- end;
- procedure NEEDOUT;
- begin
- (*
- if (m = 0) then
- begin
- {WRAP}
- if (q = s.zend) and (s.read <> s.window) then
- begin
- q := s.window;
- if LongInt(q) < LongInt(s.read) then
- m := uInt(LongInt(s.read)-LongInt(q)-1)
- else
- m := uInt(LongInt(s.zend)-LongInt(q));
- end;
- if (m = 0) then
- begin
- {FLUSH}
- s.write := q;
- r := inflate_flush(s,z,r);
- q := s.write;
- if LongInt(q) < LongInt(s.read) then
- m := uInt(LongInt(s.read)-LongInt(q)-1)
- else
- m := uInt(LongInt(s.zend)-LongInt(q));
- {WRAP}
- if (q = s.zend) and (s.read <> s.window) then
- begin
- q := s.window;
- if LongInt(q) < LongInt(s.read) then
- m := uInt(LongInt(s.read)-LongInt(q)-1)
- else
- m := uInt(LongInt(s.zend)-LongInt(q));
- end;
- if (m = 0) then
- begin
- {UPDATE}
- s.bitb := b;
- s.bitk := k;
- z.avail_in := n;
- Inc(z.total_in, LongInt(p)-LongInt(z.next_in));
- z.next_in := p;
- s.write := q;
- result := inflate_flush(s,z,r);
- exit;
- end;
- end;
- end;
- r := Z_OK;
- *)
- end;
- { copy as much as possible from the sliding window to the output area }
- function inflate_flush(var s : inflate_blocks_state;
- var z : z_stream;
- r : int) : int;
- var
- n : uInt;
- p : pBytef;
- q : pBytef;
- begin
- { local copies of source and destination pointers }
- p := z.next_out;
- q := s.read;
- { compute number of bytes to copy as far as end of window }
- if ptr2int(q) <= ptr2int(s.write) then
- n := uInt(ptr2int(s.write) - ptr2int(q))
- else
- n := uInt(ptr2int(s.zend) - ptr2int(q));
- if (n > z.avail_out) then
- n := z.avail_out;
- if (n <> 0) and (r = Z_BUF_ERROR) then
- r := Z_OK;
- { update counters }
- Dec(z.avail_out, n);
- Inc(z.total_out, n);
- { update check information }
- if Assigned(s.checkfn) then
- begin
- s.check := s.checkfn(s.check, q, n);
- z.adler := s.check;
- end;
- { copy as far as end of window }
- zmemcpy(p, q, n);
- Inc(p, n);
- Inc(q, n);
- { see if more to copy at beginning of window }
- if (q = s.zend) then
- begin
- { wrap pointers }
- q := s.window;
- if (s.write = s.zend) then
- s.write := s.window;
- { compute bytes to copy }
- n := uInt(ptr2int(s.write) - ptr2int(q));
- if (n > z.avail_out) then
- n := z.avail_out;
- if (n <> 0) and (r = Z_BUF_ERROR) then
- r := Z_OK;
- { update counters }
- Dec( z.avail_out, n);
- Inc( z.total_out, n);
- { update check information }
- if Assigned(s.checkfn) then
- begin
- s.check := s.checkfn(s.check, q, n);
- z.adler := s.check;
- end;
- { copy }
- zmemcpy(p, q, n);
- Inc(p, n);
- Inc(q, n);
- end;
- { update pointers }
- z.next_out := p;
- s.read := q;
- { done }
- inflate_flush := r;
- end;
- { #define GEN_TREES_H }
- {$ifndef GEN_TREES_H}
- { header created automatically with -DGEN_TREES_H }
- const
- DIST_CODE_LEN = 512; { see definition of array dist_code below }
- { The static literal tree. Since the bit lengths are imposed, there is no
- need for the L_CODES extra codes used during heap construction. However
- The codes 286 and 287 are needed to build a canonical tree (see _tr_init
- below). }
- type
- tstatic_ltree = ARRAY[0..L_CODES+2-1] of ct_data;
- const
- static_ltree : tstatic_ltree = (
- { fc:(freq, code) dl:(dad,len) }
- (fc:(freq: 12);dl:(len: 8)), (fc:(freq:140);dl:(len: 8)), (fc:(freq: 76);dl:(len: 8)),
- (fc:(freq:204);dl:(len: 8)), (fc:(freq: 44);dl:(len: 8)), (fc:(freq:172);dl:(len: 8)),
- (fc:(freq:108);dl:(len: 8)), (fc:(freq:236);dl:(len: 8)), (fc:(freq: 28);dl:(len: 8)),
- (fc:(freq:156);dl:(len: 8)), (fc:(freq: 92);dl:(len: 8)), (fc:(freq:220);dl:(len: 8)),
- (fc:(freq: 60);dl:(len: 8)), (fc:(freq:188);dl:(len: 8)), (fc:(freq:124);dl:(len: 8)),
- (fc:(freq:252);dl:(len: 8)), (fc:(freq: 2);dl:(len: 8)), (fc:(freq:130);dl:(len: 8)),
- (fc:(freq: 66);dl:(len: 8)), (fc:(freq:194);dl:(len: 8)), (fc:(freq: 34);dl:(len: 8)),
- (fc:(freq:162);dl:(len: 8)), (fc:(freq: 98);dl:(len: 8)), (fc:(freq:226);dl:(len: 8)),
- (fc:(freq: 18);dl:(len: 8)), (fc:(freq:146);dl:(len: 8)), (fc:(freq: 82);dl:(len: 8)),
- (fc:(freq:210);dl:(len: 8)), (fc:(freq: 50);dl:(len: 8)), (fc:(freq:178);dl:(len: 8)),
- (fc:(freq:114);dl:(len: 8)), (fc:(freq:242);dl:(len: 8)), (fc:(freq: 10);dl:(len: 8)),
- (fc:(freq:138);dl:(len: 8)), (fc:(freq: 74);dl:(len: 8)), (fc:(freq:202);dl:(len: 8)),
- (fc:(freq: 42);dl:(len: 8)), (fc:(freq:170);dl:(len: 8)), (fc:(freq:106);dl:(len: 8)),
- (fc:(freq:234);dl:(len: 8)), (fc:(freq: 26);dl:(len: 8)), (fc:(freq:154);dl:(len: 8)),
- (fc:(freq: 90);dl:(len: 8)), (fc:(freq:218);dl:(len: 8)), (fc:(freq: 58);dl:(len: 8)),
- (fc:(freq:186);dl:(len: 8)), (fc:(freq:122);dl:(len: 8)), (fc:(freq:250);dl:(len: 8)),
- (fc:(freq: 6);dl:(len: 8)), (fc:(freq:134);dl:(len: 8)), (fc:(freq: 70);dl:(len: 8)),
- (fc:(freq:198);dl:(len: 8)), (fc:(freq: 38);dl:(len: 8)), (fc:(freq:166);dl:(len: 8)),
- (fc:(freq:102);dl:(len: 8)), (fc:(freq:230);dl:(len: 8)), (fc:(freq: 22);dl:(len: 8)),
- (fc:(freq:150);dl:(len: 8)), (fc:(freq: 86);dl:(len: 8)), (fc:(freq:214);dl:(len: 8)),
- (fc:(freq: 54);dl:(len: 8)), (fc:(freq:182);dl:(len: 8)), (fc:(freq:118);dl:(len: 8)),
- (fc:(freq:246);dl:(len: 8)), (fc:(freq: 14);dl:(len: 8)), (fc:(freq:142);dl:(len: 8)),
- (fc:(freq: 78);dl:(len: 8)), (fc:(freq:206);dl:(len: 8)), (fc:(freq: 46);dl:(len: 8)),
- (fc:(freq:174);dl:(len: 8)), (fc:(freq:110);dl:(len: 8)), (fc:(freq:238);dl:(len: 8)),
- (fc:(freq: 30);dl:(len: 8)), (fc:(freq:158);dl:(len: 8)), (fc:(freq: 94);dl:(len: 8)),
- (fc:(freq:222);dl:(len: 8)), (fc:(freq: 62);dl:(len: 8)), (fc:(freq:190);dl:(len: 8)),
- (fc:(freq:126);dl:(len: 8)), (fc:(freq:254);dl:(len: 8)), (fc:(freq: 1);dl:(len: 8)),
- (fc:(freq:129);dl:(len: 8)), (fc:(freq: 65);dl:(len: 8)), (fc:(freq:193);dl:(len: 8)),
- (fc:(freq: 33);dl:(len: 8)), (fc:(freq:161);dl:(len: 8)), (fc:(freq: 97);dl:(len: 8)),
- (fc:(freq:225);dl:(len: 8)), (fc:(freq: 17);dl:(len: 8)), (fc:(freq:145);dl:(len: 8)),
- (fc:(freq: 81);dl:(len: 8)), (fc:(freq:209);dl:(len: 8)), (fc:(freq: 49);dl:(len: 8)),
- (fc:(freq:177);dl:(len: 8)), (fc:(freq:113);dl:(len: 8)), (fc:(freq:241);dl:(len: 8)),
- (fc:(freq: 9);dl:(len: 8)), (fc:(freq:137);dl:(len: 8)), (fc:(freq: 73);dl:(len: 8)),
- (fc:(freq:201);dl:(len: 8)), (fc:(freq: 41);dl:(len: 8)), (fc:(freq:169);dl:(len: 8)),
- (fc:(freq:105);dl:(len: 8)), (fc:(freq:233);dl:(len: 8)), (fc:(freq: 25);dl:(len: 8)),
- (fc:(freq:153);dl:(len: 8)), (fc:(freq: 89);dl:(len: 8)), (fc:(freq:217);dl:(len: 8)),
- (fc:(freq: 57);dl:(len: 8)), (fc:(freq:185);dl:(len: 8)), (fc:(freq:121);dl:(len: 8)),
- (fc:(freq:249);dl:(len: 8)), (fc:(freq: 5);dl:(len: 8)), (fc:(freq:133);dl:(len: 8)),
- (fc:(freq: 69);dl:(len: 8)), (fc:(freq:197);dl:(len: 8)), (fc:(freq: 37);dl:(len: 8)),
- (fc:(freq:165);dl:(len: 8)), (fc:(freq:101);dl:(len: 8)), (fc:(freq:229);dl:(len: 8)),
- (fc:(freq: 21);dl:(len: 8)), (fc:(freq:149);dl:(len: 8)), (fc:(freq: 85);dl:(len: 8)),
- (fc:(freq:213);dl:(len: 8)), (fc:(freq: 53);dl:(len: 8)), (fc:(freq:181);dl:(len: 8)),
- (fc:(freq:117);dl:(len: 8)), (fc:(freq:245);dl:(len: 8)), (fc:(freq: 13);dl:(len: 8)),
- (fc:(freq:141);dl:(len: 8)), (fc:(freq: 77);dl:(len: 8)), (fc:(freq:205);dl:(len: 8)),
- (fc:(freq: 45);dl:(len: 8)), (fc:(freq:173);dl:(len: 8)), (fc:(freq:109);dl:(len: 8)),
- (fc:(freq:237);dl:(len: 8)), (fc:(freq: 29);dl:(len: 8)), (fc:(freq:157);dl:(len: 8)),
- (fc:(freq: 93);dl:(len: 8)), (fc:(freq:221);dl:(len: 8)), (fc:(freq: 61);dl:(len: 8)),
- (fc:(freq:189);dl:(len: 8)), (fc:(freq:125);dl:(len: 8)), (fc:(freq:253);dl:(len: 8)),
- (fc:(freq: 19);dl:(len: 9)), (fc:(freq:275);dl:(len: 9)), (fc:(freq:147);dl:(len: 9)),
- (fc:(freq:403);dl:(len: 9)), (fc:(freq: 83);dl:(len: 9)), (fc:(freq:339);dl:(len: 9)),
- (fc:(freq:211);dl:(len: 9)), (fc:(freq:467);dl:(len: 9)), (fc:(freq: 51);dl:(len: 9)),
- (fc:(freq:307);dl:(len: 9)), (fc:(freq:179);dl:(len: 9)), (fc:(freq:435);dl:(len: 9)),
- (fc:(freq:115);dl:(len: 9)), (fc:(freq:371);dl:(len: 9)), (fc:(freq:243);dl:(len: 9)),
- (fc:(freq:499);dl:(len: 9)), (fc:(freq: 11);dl:(len: 9)), (fc:(freq:267);dl:(len: 9)),
- (fc:(freq:139);dl:(len: 9)), (fc:(freq:395);dl:(len: 9)), (fc:(freq: 75);dl:(len: 9)),
- (fc:(freq:331);dl:(len: 9)), (fc:(freq:203);dl:(len: 9)), (fc:(freq:459);dl:(len: 9)),
- (fc:(freq: 43);dl:(len: 9)), (fc:(freq:299);dl:(len: 9)), (fc:(freq:171);dl:(len: 9)),
- (fc:(freq:427);dl:(len: 9)), (fc:(freq:107);dl:(len: 9)), (fc:(freq:363);dl:(len: 9)),
- (fc:(freq:235);dl:(len: 9)), (fc:(freq:491);dl:(len: 9)), (fc:(freq: 27);dl:(len: 9)),
- (fc:(freq:283);dl:(len: 9)), (fc:(freq:155);dl:(len: 9)), (fc:(freq:411);dl:(len: 9)),
- (fc:(freq: 91);dl:(len: 9)), (fc:(freq:347);dl:(len: 9)), (fc:(freq:219);dl:(len: 9)),
- (fc:(freq:475);dl:(len: 9)), (fc:(freq: 59);dl:(len: 9)), (fc:(freq:315);dl:(len: 9)),
- (fc:(freq:187);dl:(len: 9)), (fc:(freq:443);dl:(len: 9)), (fc:(freq:123);dl:(len: 9)),
- (fc:(freq:379);dl:(len: 9)), (fc:(freq:251);dl:(len: 9)), (fc:(freq:507);dl:(len: 9)),
- (fc:(freq: 7);dl:(len: 9)), (fc:(freq:263);dl:(len: 9)), (fc:(freq:135);dl:(len: 9)),
- (fc:(freq:391);dl:(len: 9)), (fc:(freq: 71);dl:(len: 9)), (fc:(freq:327);dl:(len: 9)),
- (fc:(freq:199);dl:(len: 9)), (fc:(freq:455);dl:(len: 9)), (fc:(freq: 39);dl:(len: 9)),
- (fc:(freq:295);dl:(len: 9)), (fc:(freq:167);dl:(len: 9)), (fc:(freq:423);dl:(len: 9)),
- (fc:(freq:103);dl:(len: 9)), (fc:(freq:359);dl:(len: 9)), (fc:(freq:231);dl:(len: 9)),
- (fc:(freq:487);dl:(len: 9)), (fc:(freq: 23);dl:(len: 9)), (fc:(freq:279);dl:(len: 9)),
- (fc:(freq:151);dl:(len: 9)), (fc:(freq:407);dl:(len: 9)), (fc:(freq: 87);dl:(len: 9)),
- (fc:(freq:343);dl:(len: 9)), (fc:(freq:215);dl:(len: 9)), (fc:(freq:471);dl:(len: 9)),
- (fc:(freq: 55);dl:(len: 9)), (fc:(freq:311);dl:(len: 9)), (fc:(freq:183);dl:(len: 9)),
- (fc:(freq:439);dl:(len: 9)), (fc:(freq:119);dl:(len: 9)), (fc:(freq:375);dl:(len: 9)),
- (fc:(freq:247);dl:(len: 9)), (fc:(freq:503);dl:(len: 9)), (fc:(freq: 15);dl:(len: 9)),
- (fc:(freq:271);dl:(len: 9)), (fc:(freq:143);dl:(len: 9)), (fc:(freq:399);dl:(len: 9)),
- (fc:(freq: 79);dl:(len: 9)), (fc:(freq:335);dl:(len: 9)), (fc:(freq:207);dl:(len: 9)),
- (fc:(freq:463);dl:(len: 9)), (fc:(freq: 47);dl:(len: 9)), (fc:(freq:303);dl:(len: 9)),
- (fc:(freq:175);dl:(len: 9)), (fc:(freq:431);dl:(len: 9)), (fc:(freq:111);dl:(len: 9)),
- (fc:(freq:367);dl:(len: 9)), (fc:(freq:239);dl:(len: 9)), (fc:(freq:495);dl:(len: 9)),
- (fc:(freq: 31);dl:(len: 9)), (fc:(freq:287);dl:(len: 9)), (fc:(freq:159);dl:(len: 9)),
- (fc:(freq:415);dl:(len: 9)), (fc:(freq: 95);dl:(len: 9)), (fc:(freq:351);dl:(len: 9)),
- (fc:(freq:223);dl:(len: 9)), (fc:(freq:479);dl:(len: 9)), (fc:(freq: 63);dl:(len: 9)),
- (fc:(freq:319);dl:(len: 9)), (fc:(freq:191);dl:(len: 9)), (fc:(freq:447);dl:(len: 9)),
- (fc:(freq:127);dl:(len: 9)), (fc:(freq:383);dl:(len: 9)), (fc:(freq:255);dl:(len: 9)),
- (fc:(freq:511);dl:(len: 9)), (fc:(freq: 0);dl:(len: 7)), (fc:(freq: 64);dl:(len: 7)),
- (fc:(freq: 32);dl:(len: 7)), (fc:(freq: 96);dl:(len: 7)), (fc:(freq: 16);dl:(len: 7)),
- (fc:(freq: 80);dl:(len: 7)), (fc:(freq: 48);dl:(len: 7)), (fc:(freq:112);dl:(len: 7)),
- (fc:(freq: 8);dl:(len: 7)), (fc:(freq: 72);dl:(len: 7)), (fc:(freq: 40);dl:(len: 7)),
- (fc:(freq:104);dl:(len: 7)), (fc:(freq: 24);dl:(len: 7)), (fc:(freq: 88);dl:(len: 7)),
- (fc:(freq: 56);dl:(len: 7)), (fc:(freq:120);dl:(len: 7)), (fc:(freq: 4);dl:(len: 7)),
- (fc:(freq: 68);dl:(len: 7)), (fc:(freq: 36);dl:(len: 7)), (fc:(freq:100);dl:(len: 7)),
- (fc:(freq: 20);dl:(len: 7)), (fc:(freq: 84);dl:(len: 7)), (fc:(freq: 52);dl:(len: 7)),
- (fc:(freq:116);dl:(len: 7)), (fc:(freq: 3);dl:(len: 8)), (fc:(freq:131);dl:(len: 8)),
- (fc:(freq: 67);dl:(len: 8)), (fc:(freq:195);dl:(len: 8)), (fc:(freq: 35);dl:(len: 8)),
- (fc:(freq:163);dl:(len: 8)), (fc:(freq: 99);dl:(len: 8)), (fc:(freq:227);dl:(len: 8))
- );
- type
- tstatic_dtree = array[0..D_CODES-1] of ct_data;
- { The static distance tree. (Actually a trivial tree since all lens use
- 5 bits.) }
- const
- static_dtree : tstatic_dtree = (
- (fc:(freq: 0); dl:(len:5)), (fc:(freq:16); dl:(len:5)), (fc:(freq: 8); dl:(len:5)),
- (fc:(freq:24); dl:(len:5)), (fc:(freq: 4); dl:(len:5)), (fc:(freq:20); dl:(len:5)),
- (fc:(freq:12); dl:(len:5)), (fc:(freq:28); dl:(len:5)), (fc:(freq: 2); dl:(len:5)),
- (fc:(freq:18); dl:(len:5)), (fc:(freq:10); dl:(len:5)), (fc:(freq:26); dl:(len:5)),
- (fc:(freq: 6); dl:(len:5)), (fc:(freq:22); dl:(len:5)), (fc:(freq:14); dl:(len:5)),
- (fc:(freq:30); dl:(len:5)), (fc:(freq: 1); dl:(len:5)), (fc:(freq:17); dl:(len:5)),
- (fc:(freq: 9); dl:(len:5)), (fc:(freq:25); dl:(len:5)), (fc:(freq: 5); dl:(len:5)),
- (fc:(freq:21); dl:(len:5)), (fc:(freq:13); dl:(len:5)), (fc:(freq:29); dl:(len:5)),
- (fc:(freq: 3); dl:(len:5)), (fc:(freq:19); dl:(len:5)), (fc:(freq:11); dl:(len:5)),
- (fc:(freq:27); dl:(len:5)), (fc:(freq: 7); dl:(len:5)), (fc:(freq:23); dl:(len:5))
- );
- { Distance codes. The first 256 values correspond to the distances
- 3 .. 258, the last 256 values correspond to the top 8 bits of
- the 15 bit distances. }
- _dist_code : array[0..DIST_CODE_LEN-1] of uch = (
- 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
- 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
- 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
- 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
- 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
- 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
- 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
- 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
- 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
- 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
- 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
- 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
- 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
- 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
- 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
- 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
- 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
- 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
- );
- { length code for each normalized match length (0 == MIN_MATCH) }
- _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch = (
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
- 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
- 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
- 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
- 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
- 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
- 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
- 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
- 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
- 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
- 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
- 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
- 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
- );
-
- { First normalized length for each code (0 = MIN_MATCH) }
- base_length : array[0..LENGTH_CODES-1] of int = (
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
- 64, 80, 96, 112, 128, 160, 192, 224, 0
- );
- { First normalized distance for each code (0 = distance of 1) }
- base_dist : array[0..D_CODES-1] of int = (
- 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
- 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
- 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
- );
- {$endif}
- { ===========================================================================
- Constants }
- const
- MAX_BL_BITS = 7;
- { Bit length codes must not exceed MAX_BL_BITS bits }
- const
- END_BLOCK = 256;
- { end of block literal code }
- const
- REP_3_6 = 16;
- { repeat previous bit length 3-6 times (2 bits of repeat count) }
- const
- REPZ_3_10 = 17;
- { repeat a zero length 3-10 times (3 bits of repeat count) }
- const
- REPZ_11_138 = 18;
- { repeat a zero length 11-138 times (7 bits of repeat count) }
- {local}
- const
- extra_lbits : array[0..LENGTH_CODES-1] of int
- { extra bits for each length code }
- = (0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0);
- {local}
- const
- extra_dbits : array[0..D_CODES-1] of int
- { extra bits for each distance code }
- = (0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13);
- {local}
- const
- extra_blbits : array[0..BL_CODES-1] of int { extra bits for each bit length code }
- = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
- {local}
- const
- bl_order : array[0..BL_CODES-1] of uch
- = (16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
- { The lengths of the bit length codes are sent in order of decreasing
- probability, to avoid transmitting the lengths for unused bit length codes.
- }
- const
- Buf_size = (8 * 2*sizeof(char));
- { Number of bits used within bi_buf. (bi_buf might be implemented on
- more than 16 bits on some systems.) }
- { ===========================================================================
- Local data. These are initialized only once. }
- {$ifdef GEN_TREES_H)}
- { non ANSI compilers may not accept trees.h }
- const
- DIST_CODE_LEN = 512; { see definition of array dist_code below }
- {local}
- var
- static_ltree : array[0..L_CODES+2-1] of ct_data;
- { The static literal tree. Since the bit lengths are imposed, there is no
- need for the L_CODES extra codes used during heap construction. However
- The codes 286 and 287 are needed to build a canonical tree (see _tr_init
- below). }
- {local}
- static_dtree : array[0..D_CODES-1] of ct_data;
- { The static distance tree. (Actually a trivial tree since all codes use
- 5 bits.) }
- _dist_code : array[0..DIST_CODE_LEN-1] of uch;
- { Distance codes. The first 256 values correspond to the distances
- 3 .. 258, the last 256 values correspond to the top 8 bits of
- the 15 bit distances. }
- _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of uch;
- { length code for each normalized match length (0 == MIN_MATCH) }
- {local}
- base_length : array[0..LENGTH_CODES-1] of int;
- { First normalized length for each code (0 = MIN_MATCH) }
- {local}
- base_dist : array[0..D_CODES-1] of int;
- { First normalized distance for each code (0 = distance of 1) }
- {$endif} { GEN_TREES_H }
- {local}
- const
- static_l_desc : static_tree_desc =
- (static_tree: {tree_ptr}(@(static_ltree)); { pointer to array of ct_data }
- extra_bits: {pzIntfArray}(@(extra_lbits)); { pointer to array of int }
- extra_base: LITERALS+1;
- elems: L_CODES;
- max_length: MAX_BITS);
- {local}
- const
- static_d_desc : static_tree_desc =
- (static_tree: {tree_ptr}(@(static_dtree));
- extra_bits: {pzIntfArray}(@(extra_dbits));
- extra_base : 0;
- elems: D_CODES;
- max_length: MAX_BITS);
- {local}
- const
- static_bl_desc : static_tree_desc =
- (static_tree: {tree_ptr}(NIL);
- extra_bits: {pzIntfArray}@(extra_blbits);
- extra_base : 0;
- elems: BL_CODES;
- max_length: MAX_BL_BITS);
- (* ===========================================================================
- Local (static) routines in this file. }
- procedure tr_static_init;
- procedure init_block(var deflate_state);
- procedure pqdownheap(var s : deflate_state;
- var tree : ct_data;
- k : int);
- procedure gen_bitlen(var s : deflate_state;
- var desc : tree_desc);
- procedure gen_codes(var tree : ct_data;
- max_code : int;
- bl_count : pushf);
- procedure build_tree(var s : deflate_state;
- var desc : tree_desc);
- procedure scan_tree(var s : deflate_state;
- var tree : ct_data;
- max_code : int);
- procedure send_tree(var s : deflate_state;
- var tree : ct_data;
- max_code : int);
- function build_bl_tree(var deflate_state) : int;
- procedure send_all_trees(var deflate_state;
- lcodes : int;
- dcodes : int;
- blcodes : int);
- procedure compress_block(var s : deflate_state;
- var ltree : ct_data;
- var dtree : ct_data);
- procedure set_data_type(var s : deflate_state);
- function bi_reverse(value : unsigned;
- length : int) : unsigned;
- procedure bi_windup(var deflate_state);
- procedure bi_flush(var deflate_state);
- procedure copy_block(var deflate_state;
- buf : pcharf;
- len : unsigned;
- header : int);
- *)
- {$ifdef GEN_TREES_H}
- {local}
- procedure gen_trees_header;
- {$endif}
- (*
- { ===========================================================================
- Output a short LSB first on the stream.
- IN assertion: there is enough room in pendingBuf. }
- macro put_short(s, w)
- begin
- {put_byte(s, (uch)((w) & 0xff));}
- s.pending_buf^[s.pending] := uch((w) and $ff);
- Inc(s.pending);
- {put_byte(s, (uch)((ush)(w) >> 8));}
- s.pending_buf^[s.pending] := uch(ush(w) shr 8);;
- Inc(s.pending);
- end
- *)
- { ===========================================================================
- Send a value on a given number of bits.
- IN assertion: length <= 16 and value fits in length bits. }
- {local}
- procedure send_bits(var s : deflate_state;
- value : int; { value to send }
- length : int); { number of bits }
- begin
- {$ifdef DEBUG}
- Tracevv(' l '+IntToStr(length)+ ' v '+IntToStr(value));
- Assert((length > 0) and (length <= 15), 'invalid length');
- Inc(s.bits_sent, ulg(length));
- {$ENDIF}
- { If not enough room in bi_buf, use (valid) bits from bi_buf and
- (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
- unused bits in value. }
- {$IFOPT Q+} {$Q-} {$DEFINE NoOverflowCheck} {$ENDIF}
- {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
- if (s.bi_valid > int(Buf_size) - length) then
- begin
- s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
- {put_short(s, s.bi_buf);}
- s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
- Inc(s.pending);
- s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
- Inc(s.pending);
- s.bi_buf := ush(value) shr (Buf_size - s.bi_valid);
- Inc(s.bi_valid, length - Buf_size);
- end
- else
- begin
- s.bi_buf := s.bi_buf or int(value shl s.bi_valid);
- Inc(s.bi_valid, length);
- end;
- {$IFDEF NoOverflowCheck} {$Q+} {$UNDEF NoOverflowCheck} {$ENDIF}
- {$IFDEF NoRangeCheck} {$Q+} {$UNDEF NoRangeCheck} {$ENDIF}
- end;
- { ===========================================================================
- Reverse the first len bits of a code, using straightforward code (a faster
- method would use a table)
- IN assertion: 1 <= len <= 15 }
- {local}
- function bi_reverse(code : unsigned; { the value to invert }
- len : int) : unsigned; { its bit length }
- var
- res : unsigned; {register}
- begin
- res := 0;
- repeat
- res := res or (code and 1);
- code := code shr 1;
- res := res shl 1;
- Dec(len);
- until (len <= 0);
- bi_reverse := res shr 1;
- end;
- { ===========================================================================
- Generate the codes for a given tree and bit counts (which need not be
- optimal).
- IN assertion: the array bl_count contains the bit length statistics for
- the given tree and the field len is set for all tree elements.
- OUT assertion: the field code is set for all tree elements of non
- zero code length. }
- {local}
- procedure gen_codes(tree : tree_ptr; { the tree to decorate }
- max_code : int; { largest code with non zero frequency }
- var bl_count : array of ushf); { number of codes at each bit length }
- var
- next_code : array[0..MAX_BITS+1-1] of ush; { next code value for each bit length }
- code : ush; { running code value }
- bits : int; { bit index }
- n : int; { code index }
- var
- len : int;
- begin
- code := 0;
- { The distribution counts are first used to generate the code values
- without bit reversal. }
- for bits := 1 to MAX_BITS do
- begin
- code := ((code + bl_count[bits-1]) shl 1);
- next_code[bits] := code;
- end;
- { Check that the bit counts in bl_count are consistent. The last code
- must be all ones. }
- {$IFDEF DEBUG}
- Assert (code + bl_count[MAX_BITS]-1 = (1 shl MAX_BITS)-1,
- 'inconsistent bit counts');
- Tracev(#13'gen_codes: max_code '+IntToStr(max_code));
- {$ENDIF}
- for n := 0 to max_code do
- begin
- len := tree^[n].dl.Len;
- if (len = 0) then
- continue;
- { Now reverse the bits }
- tree^[n].fc.Code := bi_reverse(next_code[len], len);
- Inc(next_code[len]);
- {$ifdef DEBUG}
- if (n>31) and (n<128) then
- Tracecv(tree <> tree_ptr(@static_ltree),
- (^M'n #'+IntToStr(n)+' '+char(n)+' l '+IntToStr(len)+' c '+
- IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'))
- else
- Tracecv(tree <> tree_ptr(@static_ltree),
- (^M'n #'+IntToStr(n)+' l '+IntToStr(len)+' c '+
- IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'));
- {$ENDIF}
- end;
- end;
- { ===========================================================================
- Genererate the file trees.h describing the static trees. }
- {$ifdef GEN_TREES_H}
- macro SEPARATOR(i, last, width)
- if (i) = (last) then
- ( ^M');'^M^M
- else
- if (i) mod (width) = (width)-1 then
- ','^M
- else
- ', '
- procedure gen_trees_header;
- var
- header : system.text;
- i : int;
- begin
- system.assign(header, 'trees.inc');
- {$I-}
- ReWrite(header);
- {$I+}
- Assert (IOresult <> 0, 'Can''t open trees.h');
- WriteLn(header,
- '{ header created automatically with -DGEN_TREES_H }'^M);
- WriteLn(header, 'local const ct_data static_ltree[L_CODES+2] := (');
- for i := 0 to L_CODES+2-1 do
- begin
- WriteLn(header, '((%3u),(%3u))%s', static_ltree[i].Code,
- static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
- end;
- WriteLn(header, 'local const ct_data static_dtree[D_CODES] := (');
- for i := 0 to D_CODES-1 do
- begin
- WriteLn(header, '((%2u),(%2u))%s', static_dtree[i].Code,
- static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
- end;
- WriteLn(header, 'const uch _dist_code[DIST_CODE_LEN] := (');
- for i := 0 to DIST_CODE_LEN-1 do
- begin
- WriteLn(header, '%2u%s', _dist_code[i],
- SEPARATOR(i, DIST_CODE_LEN-1, 20));
- end;
- WriteLn(header, 'const uch _length_code[MAX_MATCH-MIN_MATCH+1]= (');
- for i := 0 to MAX_MATCH-MIN_MATCH+1-1 do
- begin
- WriteLn(header, '%2u%s', _length_code[i],
- SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
- end;
- WriteLn(header, 'local const int base_length[LENGTH_CODES] := (');
- for i := 0 to LENGTH_CODES-1 do
- begin
- WriteLn(header, '%1u%s', base_length[i],
- SEPARATOR(i, LENGTH_CODES-1, 20));
- end;
- WriteLn(header, 'local const int base_dist[D_CODES] := (');
- for i := 0 to D_CODES-1 do
- begin
- WriteLn(header, '%5u%s', base_dist[i],
- SEPARATOR(i, D_CODES-1, 10));
- end;
- close(header);
- end;
- {$endif} { GEN_TREES_H }
- { ===========================================================================
- Initialize the various 'constant' tables. }
- {local}
- procedure tr_static_init;
- {$ifdef GEN_TREES_H}
- const
- static_init_done : boolean = FALSE;
- var
- n : int; { iterates over tree elements }
- bits : int; { bit counter }
- length : int; { length value }
- code : int; { code value }
- dist : int; { distance index }
- bl_count : array[0..MAX_BITS+1-1] of ush;
- { number of codes at each bit length for an optimal tree }
- begin
- if (static_init_done) then
- exit;
- { Initialize the mapping length (0..255) -> length code (0..28) }
- length := 0;
- for code := 0 to LENGTH_CODES-1-1 do
- begin
- base_length[code] := length;
- for n := 0 to (1 shl extra_lbits[code])-1 do
- begin
- _length_code[length] := uch(code);
- Inc(length);
- end;
- end;
- Assert (length = 256, 'tr_static_init: length <> 256');
- { Note that the length 255 (match length 258) can be represented
- in two different ways: code 284 + 5 bits or code 285, so we
- overwrite length_code[255] to use the best encoding: }
- _length_code[length-1] := uch(code);
- { Initialize the mapping dist (0..32K) -> dist code (0..29) }
- dist := 0;
- for code := 0 to 16-1 do
- begin
- base_dist[code] := dist;
- for n := 0 to (1 shl extra_dbits[code])-1 do
- begin
- _dist_code[dist] := uch(code);
- Inc(dist);
- end;
- end;
- Assert (dist = 256, 'tr_static_init: dist <> 256');
- dist := dist shr 7; { from now on, all distances are divided by 128 }
- for code := 16 to D_CODES-1 do
- begin
- base_dist[code] := dist shl 7;
- for n := 0 to (1 shl (extra_dbits[code]-7))-1 do
- begin
- _dist_code[256 + dist] := uch(code);
- Inc(dist);
- end;
- end;
- Assert (dist = 256, 'tr_static_init: 256+dist <> 512');
- { Construct the codes of the static literal tree }
- for bits := 0 to MAX_BITS do
- bl_count[bits] := 0;
- n := 0;
- while (n <= 143) do
- begin
- static_ltree[n].dl.Len := 8;
- Inc(n);
- Inc(bl_count[8]);
- end;
- while (n <= 255) do
- begin
- static_ltree[n].dl.Len := 9;
- Inc(n);
- Inc(bl_count[9]);
- end;
- while (n <= 279) do
- begin
- static_ltree[n].dl.Len := 7;
- Inc(n);
- Inc(bl_count[7]);
- end;
- while (n <= 287) do
- begin
- static_ltree[n].dl.Len := 8;
- Inc(n);
- Inc(bl_count[8]);
- end;
- { Codes 286 and 287 do not exist, but we must include them in the
- tree construction to get a canonical Huffman tree (longest code
- all ones) }
- gen_codes(tree_ptr(@static_ltree), L_CODES+1, bl_count);
- { The static distance tree is trivial: }
- for n := 0 to D_CODES-1 do
- begin
- static_dtree[n].dl.Len := 5;
- static_dtree[n].fc.Code := bi_reverse(unsigned(n), 5);
- end;
- static_init_done := TRUE;
- gen_trees_header; { save to include file }
- {$else}
- begin
- {$endif} { GEN_TREES_H) }
- end;
- { ===========================================================================
- Initialize a new block. }
- {local}
- procedure init_block(var s : deflate_state);
- var
- n : int; { iterates over tree elements }
- begin
- { Initialize the trees. }
- for n := 0 to L_CODES-1 do
- s.dyn_ltree[n].fc.Freq := 0;
- for n := 0 to D_CODES-1 do
- s.dyn_dtree[n].fc.Freq := 0;
- for n := 0 to BL_CODES-1 do
- s.bl_tree[n].fc.Freq := 0;
- s.dyn_ltree[END_BLOCK].fc.Freq := 1;
- s.static_len := Long(0);
- s.opt_len := Long(0);
- s.matches := 0;
- s.last_lit := 0;
- end;
- const
- SMALLEST = 1;
- { Index within the heap array of least frequent node in the Huffman tree }
- { ===========================================================================
- Initialize the tree data structures for a new zlib stream. }
- procedure _tr_init(var s : deflate_state);
- begin
- tr_static_init;
- s.compressed_len := Long(0);
- s.l_desc.dyn_tree := tree_ptr(@s.dyn_ltree);
- s.l_desc.stat_desc := @static_l_desc;
- s.d_desc.dyn_tree := tree_ptr(@s.dyn_dtree);
- s.d_desc.stat_desc := @static_d_desc;
- s.bl_desc.dyn_tree := tree_ptr(@s.bl_tree);
- s.bl_desc.stat_desc := @static_bl_desc;
- s.bi_buf := 0;
- s.bi_valid := 0;
- s.last_eob_len := 8; { enough lookahead for inflate }
- {$ifdef DEBUG}
- s.bits_sent := Long(0);
- {$endif}
- { Initialize the first block of the first file: }
- init_block(s);
- end;
- { ===========================================================================
- Remove the smallest element from the heap and recreate the heap with
- one less element. Updates heap and heap_len.
- macro pqremove(s, tree, top)
- begin
- top := s.heap[SMALLEST];
- s.heap[SMALLEST] := s.heap[s.heap_len];
- Dec(s.heap_len);
- pqdownheap(s, tree, SMALLEST);
- end
- }
- { ===========================================================================
- Compares to subtrees, using the tree depth as tie breaker when
- the subtrees have equal frequency. This minimizes the worst case length.
- macro smaller(tree, n, m, depth)
- ( (tree[n].Freq < tree[m].Freq) or
- ((tree[n].Freq = tree[m].Freq) and (depth[n] <= depth[m])) )
- }
- { ===========================================================================
- Restore the heap property by moving down the tree starting at node k,
- exchanging a node with the smallest of its two sons if necessary, stopping
- when the heap property is re-established (each father smaller than its
- two sons). }
- {local}
- procedure pqdownheap(var s : deflate_state;
- var tree : tree_type; { the tree to restore }
- k : int); { node to move down }
- var
- v : int;
- j : int;
- begin
- v := s.heap[k];
- j := k shl 1; { left son of k }
- while (j <= s.heap_len) do
- begin
- { Set j to the smallest of the two sons: }
- if (j < s.heap_len) and
- {smaller(tree, s.heap[j+1], s.heap[j], s.depth)}
- ( (tree[s.heap[j+1]].fc.Freq < tree[s.heap[j]].fc.Freq) or
- ((tree[s.heap[j+1]].fc.Freq = tree[s.heap[j]].fc.Freq) and
- (s.depth[s.heap[j+1]] <= s.depth[s.heap[j]])) ) then
- begin
- Inc(j);
- end;
- { Exit if v is smaller than both sons }
- if {(smaller(tree, v, s.heap[j], s.depth))}
- ( (tree[v].fc.Freq < tree[s.heap[j]].fc.Freq) or
- ((tree[v].fc.Freq = tree[s.heap[j]].fc.Freq) and
- (s.depth[v] <= s.depth[s.heap[j]])) ) then
- break;
- { Exchange v with the smallest son }
- s.heap[k] := s.heap[j];
- k := j;
- { And continue down the tree, setting j to the left son of k }
- j := j shl 1;
- end;
- s.heap[k] := v;
- end;
- { ===========================================================================
- Compute the optimal bit lengths for a tree and update the total bit length
- for the current block.
- IN assertion: the fields freq and dad are set, heap[heap_max] and
- above are the tree nodes sorted by increasing frequency.
- OUT assertions: the field len is set to the optimal bit length, the
- array bl_count contains the frequencies for each bit length.
- The length opt_len is updated; static_len is also updated if stree is
- not null. }
- {local}
- procedure gen_bitlen(var s : deflate_state;
- var desc : tree_desc); { the tree descriptor }
- var
- tree : tree_ptr;
- max_code : int;
- stree : tree_ptr; {const}
- extra : pzIntfArray; {const}
- base : int;
- max_length : int;
- h : int; { heap index }
- n, m : int; { iterate over the tree elements }
- bits : int; { bit length }
- xbits : int; { extra bits }
- f : ush; { frequency }
- overflow : int; { number of elements with bit length too large }
- begin
- tree := desc.dyn_tree;
- max_code := desc.max_code;
- stree := desc.stat_desc^.static_tree;
- extra := desc.stat_desc^.extra_bits;
- base := desc.stat_desc^.extra_base;
- max_length := desc.stat_desc^.max_length;
- overflow := 0;
- for bits := 0 to MAX_BITS do
- s.bl_count[bits] := 0;
- { In a first pass, compute the optimal bit lengths (which may
- overflow in the case of the bit length tree). }
- tree^[s.heap[s.heap_max]].dl.Len := 0; { root of the heap }
- for h := s.heap_max+1 to HEAP_SIZE-1 do
- begin
- n := s.heap[h];
- bits := tree^[tree^[n].dl.Dad].dl.Len + 1;
- if (bits > max_length) then
- begin
- bits := max_length;
- Inc(overflow);
- end;
- tree^[n].dl.Len := ush(bits);
- { We overwrite tree[n].dl.Dad which is no longer needed }
- if (n > max_code) then
- continue; { not a leaf node }
- Inc(s.bl_count[bits]);
- xbits := 0;
- if (n >= base) then
- xbits := extra^[n-base];
- f := tree^[n].fc.Freq;
- Inc(s.opt_len, ulg(f) * (bits + xbits));
- if (stree <> NIL) then
- Inc(s.static_len, ulg(f) * (stree^[n].dl.Len + xbits));
- end;
- if (overflow = 0) then
- exit;
- {$ifdef DEBUG}
- Tracev(^M'bit length overflow');
- {$endif}
- { This happens for example on obj2 and pic of the Calgary corpus }
- { Find the first bit length which could increase: }
- repeat
- bits := max_length-1;
- while (s.bl_count[bits] = 0) do
- Dec(bits);
- Dec(s.bl_count[bits]); { move one leaf down the tree }
- Inc(s.bl_count[bits+1], 2); { move one overflow item as its brother }
- Dec(s.bl_count[max_length]);
- { The brother of the overflow item also moves one step up,
- but this does not affect bl_count[max_length] }
- Dec(overflow, 2);
- until (overflow <= 0);
- { Now recompute all bit lengths, scanning in increasing frequency.
- h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
- lengths instead of fixing only the wrong ones. This idea is taken
- from 'ar' written by Haruhiko Okumura.) }
- h := HEAP_SIZE; { Delphi3: compiler warning w/o this }
- for bits := max_length downto 1 do
- begin
- n := s.bl_count[bits];
- while (n <> 0) do
- begin
- Dec(h);
- m := s.heap[h];
- if (m > max_code) then
- continue;
- if (tree^[m].dl.Len <> unsigned(bits)) then
- begin
- {$ifdef DEBUG}
- Trace('code '+IntToStr(m)+' bits '+IntToStr(tree^[m].dl.Len)
- +'.'+IntToStr(bits));
- {$ENDIF}
- Inc(s.opt_len, (long(bits) - long(tree^[m].dl.Len))
- * long(tree^[m].fc.Freq) );
- tree^[m].dl.Len := ush(bits);
- end;
- Dec(n);
- end;
- end;
- end;
- { ===========================================================================
- Construct one Huffman tree and assigns the code bit strings and lengths.
- Update the total bit length for the current block.
- IN assertion: the field freq is set for all tree elements.
- OUT assertions: the fields len and code are set to the optimal bit length
- and corresponding code. The length opt_len is updated; static_len is
- also updated if stree is not null. The field max_code is set. }
- {local}
- procedure build_tree(var s : deflate_state;
- var desc : tree_desc); { the tree descriptor }
- var
- tree : tree_ptr;
- stree : tree_ptr; {const}
- elems : int;
- n, m : int; { iterate over heap elements }
- max_code : int; { largest code with non zero frequency }
- node : int; { new node being created }
- begin
- tree := desc.dyn_tree;
- stree := desc.stat_desc^.static_tree;
- elems := desc.stat_desc^.elems;
- max_code := -1;
- { Construct the initial heap, with least frequent element in
- heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
- heap[0] is not used. }
- s.heap_len := 0;
- s.heap_max := HEAP_SIZE;
- for n := 0 to elems-1 do
- begin
- if (tree^[n].fc.Freq <> 0) then
- begin
- max_code := n;
- Inc(s.heap_len);
- s.heap[s.heap_len] := n;
- s.depth[n] := 0;
- end
- else
- begin
- tree^[n].dl.Len := 0;
- end;
- end;
- { The pkzip format requires that at least one distance code exists,
- and that at least one bit should be sent even if there is only one
- possible code. So to avoid special checks later on we force at least
- two codes of non zero frequency. }
- while (s.heap_len < 2) do
- begin
- Inc(s.heap_len);
- if (max_code < 2) then
- begin
- Inc(max_code);
- s.heap[s.heap_len] := max_code;
- node := max_code;
- end
- else
- begin
- s.heap[s.heap_len] := 0;
- node := 0;
- end;
- tree^[node].fc.Freq := 1;
- s.depth[node] := 0;
- Dec(s.opt_len);
- if (stree <> NIL) then
- Dec(s.static_len, stree^[node].dl.Len);
- { node is 0 or 1 so it does not have extra bits }
- end;
- desc.max_code := max_code;
- { The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
- establish sub-heaps of increasing lengths: }
- for n := s.heap_len div 2 downto 1 do
- pqdownheap(s, tree^, n);
- { Construct the Huffman tree by repeatedly combining the least two
- frequent nodes. }
- node := elems; { next internal node of the tree }
- repeat
- {pqremove(s, tree, n);} { n := node of least frequency }
- n := s.heap[SMALLEST];
- s.heap[SMALLEST] := s.heap[s.heap_len];
- Dec(s.heap_len);
- pqdownheap(s, tree^, SMALLEST);
- m := s.heap[SMALLEST]; { m := node of next least frequency }
- Dec(s.heap_max);
- s.heap[s.heap_max] := n; { keep the nodes sorted by frequency }
- Dec(s.heap_max);
- s.heap[s.heap_max] := m;
- { Create a new node father of n and m }
- tree^[node].fc.Freq := tree^[n].fc.Freq + tree^[m].fc.Freq;
- { maximum }
- if (s.depth[n] >= s.depth[m]) then
- s.depth[node] := uch (s.depth[n] + 1)
- else
- s.depth[node] := uch (s.depth[m] + 1);
- tree^[m].dl.Dad := ush(node);
- tree^[n].dl.Dad := ush(node);
- {$ifdef DUMP_BL_TREE}
- if (tree = tree_ptr(@s.bl_tree)) then
- begin
- WriteLn(#13'node ',node,'(',tree^[node].fc.Freq,') sons ',n,
- '(',tree^[n].fc.Freq,') ', m, '(',tree^[m].fc.Freq,')');
- end;
- {$endif}
- { and insert the new node in the heap }
- s.heap[SMALLEST] := node;
- Inc(node);
- pqdownheap(s, tree^, SMALLEST);
- until (s.heap_len < 2);
- Dec(s.heap_max);
- s.heap[s.heap_max] := s.heap[SMALLEST];
- { At this point, the fields freq and dad are set. We can now
- generate the bit lengths. }
- gen_bitlen(s, desc);
- { The field len is now set, we can generate the bit codes }
- gen_codes (tree, max_code, s.bl_count);
- end;
- { ===========================================================================
- Scan a literal or distance tree to determine the frequencies of the codes
- in the bit length tree. }
- {local}
- procedure scan_tree(var s : deflate_state;
- var tree : array of ct_data; { the tree to be scanned }
- max_code : int); { and its largest code of non zero frequency }
- var
- n : int; { iterates over all tree elements }
- prevlen : int; { last emitted length }
- curlen : int; { length of current code }
- nextlen : int; { length of next code }
- count : int; { repeat count of the current code }
- max_count : int; { max repeat count }
- min_count : int; { min repeat count }
- begin
- prevlen := -1;
- nextlen := tree[0].dl.Len;
- count := 0;
- max_count := 7;
- min_count := 4;
- if (nextlen = 0) then
- begin
- max_count := 138;
- min_count := 3;
- end;
- tree[max_code+1].dl.Len := ush($ffff); { guard }
- for n := 0 to max_code do
- begin
- curlen := nextlen;
- nextlen := tree[n+1].dl.Len;
- Inc(count);
- if (count < max_count) and (curlen = nextlen) then
- continue
- else
- if (count < min_count) then
- Inc(s.bl_tree[curlen].fc.Freq, count)
- else
- if (curlen <> 0) then
- begin
- if (curlen <> prevlen) then
- Inc(s.bl_tree[curlen].fc.Freq);
- Inc(s.bl_tree[REP_3_6].fc.Freq);
- end
- else
- if (count <= 10) then
- Inc(s.bl_tree[REPZ_3_10].fc.Freq)
- else
- Inc(s.bl_tree[REPZ_11_138].fc.Freq);
- count := 0;
- prevlen := curlen;
- if (nextlen = 0) then
- begin
- max_count := 138;
- min_count := 3;
- end
- else
- if (curlen = nextlen) then
- begin
- max_count := 6;
- min_count := 3;
- end
- else
- begin
- max_count := 7;
- min_count := 4;
- end;
- end;
- end;
- { ===========================================================================
- Send a literal or distance tree in compressed form, using the codes in
- bl_tree. }
- {local}
- procedure send_tree(var s : deflate_state;
- var tree : array of ct_data; { the tree to be scanned }
- max_code : int); { and its largest code of non zero frequency }
- var
- n : int; { iterates over all tree elements }
- prevlen : int; { last emitted length }
- curlen : int; { length of current code }
- nextlen : int; { length of next code }
- count : int; { repeat count of the current code }
- max_count : int; { max repeat count }
- min_count : int; { min repeat count }
- begin
- prevlen := -1;
- nextlen := tree[0].dl.Len;
- count := 0;
- max_count := 7;
- min_count := 4;
- { tree[max_code+1].dl.Len := -1; } { guard already set }
- if (nextlen = 0) then
- begin
- max_count := 138;
- min_count := 3;
- end;
- for n := 0 to max_code do
- begin
- curlen := nextlen;
- nextlen := tree[n+1].dl.Len;
- Inc(count);
- if (count < max_count) and (curlen = nextlen) then
- continue
- else
- if (count < min_count) then
- begin
- repeat
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(curlen));
- {$ENDIF}
- send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
- Dec(count);
- until (count = 0);
- end
- else
- if (curlen <> 0) then
- begin
- if (curlen <> prevlen) then
- begin
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(curlen));
- {$ENDIF}
- send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
- Dec(count);
- end;
- {$IFDEF DEBUG}
- Assert((count >= 3) and (count <= 6), ' 3_6?');
- {$ENDIF}
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(REP_3_6));
- {$ENDIF}
- send_bits(s, s.bl_tree[REP_3_6].fc.Code, s.bl_tree[REP_3_6].dl.Len);
- send_bits(s, count-3, 2);
- end
- else
- if (count <= 10) then
- begin
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(REPZ_3_10));
- {$ENDIF}
- send_bits(s, s.bl_tree[REPZ_3_10].fc.Code, s.bl_tree[REPZ_3_10].dl.Len);
- send_bits(s, count-3, 3);
- end
- else
- begin
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(REPZ_11_138));
- {$ENDIF}
- send_bits(s, s.bl_tree[REPZ_11_138].fc.Code, s.bl_tree[REPZ_11_138].dl.Len);
- send_bits(s, count-11, 7);
- end;
- count := 0;
- prevlen := curlen;
- if (nextlen = 0) then
- begin
- max_count := 138;
- min_count := 3;
- end
- else
- if (curlen = nextlen) then
- begin
- max_count := 6;
- min_count := 3;
- end
- else
- begin
- max_count := 7;
- min_count := 4;
- end;
- end;
- end;
- { ===========================================================================
- Construct the Huffman tree for the bit lengths and return the index in
- bl_order of the last bit length code to send. }
- {local}
- function build_bl_tree(var s : deflate_state) : int;
- var
- max_blindex : int; { index of last bit length code of non zero freq }
- begin
- { Determine the bit length frequencies for literal and distance trees }
- scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
- scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
- { Build the bit length tree: }
- build_tree(s, s.bl_desc);
- { opt_len now includes the length of the tree representations, except
- the lengths of the bit lengths codes and the 5+5+4 bits for the counts. }
- { Determine the number of bit length codes to send. The pkzip format
- requires that at least 4 bit length codes be sent. (appnote.txt says
- 3 but the actual value used is 4.) }
- for max_blindex := BL_CODES-1 downto 3 do
- begin
- if (s.bl_tree[bl_order[max_blindex]].dl.Len <> 0) then
- break;
- end;
- { Update opt_len to include the bit length tree and counts }
- Inc(s.opt_len, 3*(max_blindex+1) + 5+5+4);
- {$ifdef DEBUG}
- Tracev(^M'dyn trees: dyn %ld, stat %ld {s.opt_len, s.static_len}');
- {$ENDIF}
- build_bl_tree := max_blindex;
- end;
- { ===========================================================================
- Send the header for a block using dynamic Huffman trees: the counts, the
- lengths of the bit length codes, the literal tree and the distance tree.
- IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. }
- {local}
- procedure send_all_trees(var s : deflate_state;
- lcodes : int;
- dcodes : int;
- blcodes : int); { number of codes for each tree }
- var
- rank : int; { index in bl_order }
- begin
- {$IFDEF DEBUG}
- Assert ((lcodes >= 257) and (dcodes >= 1) and (blcodes >= 4),
- 'not enough codes');
- Assert ((lcodes <= L_CODES) and (dcodes <= D_CODES)
- and (blcodes <= BL_CODES), 'too many codes');
- Tracev(^M'bl counts: ');
- {$ENDIF}
- send_bits(s, lcodes-257, 5); { not +255 as stated in appnote.txt }
- send_bits(s, dcodes-1, 5);
- send_bits(s, blcodes-4, 4); { not -3 as stated in appnote.txt }
- for rank := 0 to blcodes-1 do
- begin
- {$ifdef DEBUG}
- Tracev(^M'bl code '+IntToStr(bl_order[rank]));
- {$ENDIF}
- send_bits(s, s.bl_tree[bl_order[rank]].dl.Len, 3);
- end;
- {$ifdef DEBUG}
- Tracev(^M'bl tree: sent '+IntToStr(s.bits_sent));
- {$ENDIF}
- send_tree(s, s.dyn_ltree, lcodes-1); { literal tree }
- {$ifdef DEBUG}
- Tracev(^M'lit tree: sent '+IntToStr(s.bits_sent));
- {$ENDIF}
- send_tree(s, s.dyn_dtree, dcodes-1); { distance tree }
- {$ifdef DEBUG}
- Tracev(^M'dist tree: sent '+IntToStr(s.bits_sent));
- {$ENDIF}
- end;
- { ===========================================================================
- Flush the bit buffer and align the output on a byte boundary }
- {local}
- procedure bi_windup(var s : deflate_state);
- begin
- if (s.bi_valid > 8) then
- begin
- {put_short(s, s.bi_buf);}
- s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
- Inc(s.pending);
- s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
- Inc(s.pending);
- end
- else
- if (s.bi_valid > 0) then
- begin
- {put_byte(s, (Byte)s^.bi_buf);}
- s.pending_buf^[s.pending] := Byte(s.bi_buf);
- Inc(s.pending);
- end;
- s.bi_buf := 0;
- s.bi_valid := 0;
- {$ifdef DEBUG}
- s.bits_sent := (s.bits_sent+7) and (not 7);
- {$endif}
- end;
- { ===========================================================================
- Copy a stored block, storing first the length and its
- one's complement if requested. }
- {local}
- procedure copy_block(var s : deflate_state;
- buf : pcharf; { the input data }
- len : unsigned; { its length }
- header : boolean); { true if block header must be written }
- begin
- bi_windup(s); { align on byte boundary }
- s.last_eob_len := 8; { enough lookahead for inflate }
- if (header) then
- begin
- {put_short(s, (ush)len);}
- s.pending_buf^[s.pending] := uch(ush(len) and $ff);
- Inc(s.pending);
- s.pending_buf^[s.pending] := uch(ush(len) shr 8);;
- Inc(s.pending);
- {put_short(s, (ush)~len);}
- s.pending_buf^[s.pending] := uch(ush(not len) and $ff);
- Inc(s.pending);
- s.pending_buf^[s.pending] := uch(ush(not len) shr 8);;
- Inc(s.pending);
- {$ifdef DEBUG}
- Inc(s.bits_sent, 2*16);
- {$endif}
- end;
- {$ifdef DEBUG}
- Inc(s.bits_sent, ulg(len shl 3));
- {$endif}
- while (len <> 0) do
- begin
- Dec(len);
- {put_byte(s, *buf++);}
- s.pending_buf^[s.pending] := buf^;
- Inc(buf);
- Inc(s.pending);
- end;
- end;
- { ===========================================================================
- Send a stored block }
- procedure _tr_stored_block(var s : deflate_state;
- buf : pcharf; { input block }
- stored_len : ulg; { length of input block }
- eof : boolean); { true if this is the last block for a file }
- begin
- send_bits(s, (STORED_BLOCK shl 1)+ord(eof), 3); { send block type }
- s.compressed_len := (s.compressed_len + 3 + 7) and ulg(not Long(7));
- Inc(s.compressed_len, (stored_len + 4) shl 3);
- copy_block(s, buf, unsigned(stored_len), TRUE); { with header }
- end;
- { ===========================================================================
- Flush the bit buffer, keeping at most 7 bits in it. }
- {local}
- procedure bi_flush(var s : deflate_state);
- begin
- if (s.bi_valid = 16) then
- begin
- {put_short(s, s.bi_buf);}
- s.pending_buf^[s.pending] := uch(s.bi_buf and $ff);
- Inc(s.pending);
- s.pending_buf^[s.pending] := uch(ush(s.bi_buf) shr 8);;
- Inc(s.pending);
- s.bi_buf := 0;
- s.bi_valid := 0;
- end
- else
- if (s.bi_valid >= 8) then
- begin
- {put_byte(s, (Byte)s^.bi_buf);}
- s.pending_buf^[s.pending] := Byte(s.bi_buf);
- Inc(s.pending);
- s.bi_buf := s.bi_buf shr 8;
- Dec(s.bi_valid, 8);
- end;
- end;
- { ===========================================================================
- Send one empty static block to give enough lookahead for inflate.
- This takes 10 bits, of which 7 may remain in the bit buffer.
- The current inflate code requires 9 bits of lookahead. If the
- last two codes for the previous block (real code plus EOB) were coded
- on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
- the last real code. In this case we send two empty static blocks instead
- of one. (There are no problems if the previous block is stored or fixed.)
- To simplify the code, we assume the worst case of last real code encoded
- on one bit only. }
- procedure _tr_align(var s : deflate_state);
- begin
- send_bits(s, STATIC_TREES shl 1, 3);
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(END_BLOCK));
- {$ENDIF}
- send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
- Inc(s.compressed_len, Long(10)); { 3 for block type, 7 for EOB }
- bi_flush(s);
- { Of the 10 bits for the empty block, we have already sent
- (10 - bi_valid) bits. The lookahead for the last real code (before
- the EOB of the previous block) was thus at least one plus the length
- of the EOB plus what we have just sent of the empty static block. }
- if (1 + s.last_eob_len + 10 - s.bi_valid < 9) then
- begin
- send_bits(s, STATIC_TREES shl 1, 3);
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(END_BLOCK));
- {$ENDIF}
- send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
- Inc(s.compressed_len, Long(10));
- bi_flush(s);
- end;
- s.last_eob_len := 7;
- end;
- { ===========================================================================
- Set the data type to ASCII or BINARY, using a crude approximation:
- binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
- IN assertion: the fields freq of dyn_ltree are set and the total of all
- frequencies does not exceed 64K (to fit in an int on 16 bit machines). }
- {local}
- procedure set_data_type(var s : deflate_state);
- var
- n : int;
- ascii_freq : unsigned;
- bin_freq : unsigned;
- begin
- n := 0;
- ascii_freq := 0;
- bin_freq := 0;
- while (n < 7) do
- begin
- Inc(bin_freq, s.dyn_ltree[n].fc.Freq);
- Inc(n);
- end;
- while (n < 128) do
- begin
- Inc(ascii_freq, s.dyn_ltree[n].fc.Freq);
- Inc(n);
- end;
- while (n < LITERALS) do
- begin
- Inc(bin_freq, s.dyn_ltree[n].fc.Freq);
- Inc(n);
- end;
- if (bin_freq > (ascii_freq shr 2)) then
- s.data_type := Byte(Z_BINARY)
- else
- s.data_type := Byte(Z_ASCII);
- end;
- { ===========================================================================
- Send the block data compressed using the given Huffman trees }
- {local}
- procedure compress_block(var s : deflate_state;
- var ltree : array of ct_data; { literal tree }
- var dtree : array of ct_data); { distance tree }
- var
- dist : unsigned; { distance of matched string }
- lc : int; { match length or unmatched char (if dist == 0) }
- lx : unsigned; { running index in l_buf }
- code : unsigned; { the code to send }
- extra : int; { number of extra bits to send }
- begin
- lx := 0;
- if (s.last_lit <> 0) then
- repeat
- dist := s.d_buf^[lx];
- lc := s.l_buf^[lx];
- Inc(lx);
- if (dist = 0) then
- begin
- { send a literal byte }
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(lc));
- Tracecv((lc > 31) and (lc < 128), ' '+char(lc)+' ');
- {$ENDIF}
- send_bits(s, ltree[lc].fc.Code, ltree[lc].dl.Len);
- end
- else
- begin
- { Here, lc is the match length - MIN_MATCH }
- code := _length_code[lc];
- { send the length code }
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(code+LITERALS+1));
- {$ENDIF}
- send_bits(s, ltree[code+LITERALS+1].fc.Code, ltree[code+LITERALS+1].dl.Len);
- extra := extra_lbits[code];
- if (extra <> 0) then
- begin
- Dec(lc, base_length[code]);
- send_bits(s, lc, extra); { send the extra length bits }
- end;
- Dec(dist); { dist is now the match distance - 1 }
- {code := d_code(dist);}
- if (dist < 256) then
- code := _dist_code[dist]
- else
- code := _dist_code[256+(dist shr 7)];
- {$IFDEF DEBUG}
- Assert (code < D_CODES, 'bad d_code');
- {$ENDIF}
- { send the distance code }
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(code));
- {$ENDIF}
- send_bits(s, dtree[code].fc.Code, dtree[code].dl.Len);
- extra := extra_dbits[code];
- if (extra <> 0) then
- begin
- Dec(dist, base_dist[code]);
- send_bits(s, dist, extra); { send the extra distance bits }
- end;
- end; { literal or match pair ? }
- { Check that the overlay between pending_buf and d_buf+l_buf is ok: }
- {$IFDEF DEBUG}
- Assert(s.pending < s.lit_bufsize + 2*lx, 'pendingBuf overflow');
- {$ENDIF}
- until (lx >= s.last_lit);
- {$ifdef DEBUG}
- Tracevvv(#13'cd '+IntToStr(END_BLOCK));
- {$ENDIF}
- send_bits(s, ltree[END_BLOCK].fc.Code, ltree[END_BLOCK].dl.Len);
- s.last_eob_len := ltree[END_BLOCK].dl.Len;
- end;
- { ===========================================================================
- Determine the best encoding for the current block: dynamic trees, static
- trees or store, and output the encoded block to the zip file. This function
- returns the total compressed length for the file so far. }
- function _tr_flush_block (var s : deflate_state;
- buf : pcharf; { input block, or NULL if too old }
- stored_len : ulg; { length of input block }
- eof : boolean) : ulg; { true if this is the last block for a file }
- var
- opt_lenb, static_lenb : ulg; { opt_len and static_len in bytes }
- max_blindex : int; { index of last bit length code of non zero freq }
- vstatic_ltree : tstatic_ltree;
- vstatic_dtree : tstatic_dtree;
- begin
- max_blindex := 0;
- { Build the Huffman trees unless a stored block is forced }
- if (s.level > 0) then
- begin
- { Check if the file is ascii or binary }
- if (s.data_type = Z_UNKNOWN) then
- set_data_type(s);
- { Construct the literal and distance trees }
- build_tree(s, s.l_desc);
- {$ifdef DEBUG}
- Tracev(^M'lit data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
- {$ENDIF}
- build_tree(s, s.d_desc);
- {$ifdef DEBUG}
- Tracev(^M'dist data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
- {$ENDIF}
- { At this point, opt_len and static_len are the total bit lengths of
- the compressed block data, excluding the tree representations. }
- { Build the bit length tree for the above two trees, and get the index
- in bl_order of the last bit length code to send. }
- max_blindex := build_bl_tree(s);
- { Determine the best encoding. Compute first the block length in bytes}
- opt_lenb := (s.opt_len+3+7) shr 3;
- static_lenb := (s.static_len+3+7) shr 3;
- {$ifdef DEBUG}
- Tracev(^M'opt %lu(%lu) stat %lu(%lu) stored %lu lit %u '+
- '{opt_lenb, s.opt_len, static_lenb, s.static_len, stored_len,'+
- 's.last_lit}');
- {$ENDIF}
- if (static_lenb <= opt_lenb) then
- opt_lenb := static_lenb;
- end
- else
- begin
- {$IFDEF DEBUG}
- Assert(buf <> pcharf(NIL), 'lost buf');
- {$ENDIF}
- static_lenb := stored_len + 5;
- opt_lenb := static_lenb; { force a stored block }
- end;
- { If compression failed and this is the first and last block,
- and if the .zip file can be seeked (to rewrite the local header),
- the whole file is transformed into a stored file: }
- {$ifdef STORED_FILE_OK}
- {$ifdef FORCE_STORED_FILE}
- if eof and (s.compressed_len = Long(0)) then
- begin { force stored file }
- {$else}
- if (stored_len <= opt_lenb) and eof and (s.compressed_len=Long(0))
- and seekable()) do
- begin
- {$endif}
- { Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: }
- if (buf = pcharf(0)) then
- error ('block vanished');
- copy_block(buf, unsigned(stored_len), 0); { without header }
- s.compressed_len := stored_len shl 3;
- s.method := STORED;
- end
- else
- {$endif} { STORED_FILE_OK }
- {$ifdef FORCE_STORED}
- if (buf <> pchar(0)) then
- begin { force stored block }
- {$else}
- if (stored_len+4 <= opt_lenb) and (buf <> pcharf(0)) then
- begin
- { 4: two words for the lengths }
- {$endif}
- { The test buf <> NULL is only necessary if LIT_BUFSIZE > WSIZE.
- Otherwise we can't have processed more than WSIZE input bytes since
- the last block flush, because compression would have been
- successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
- transform a block into a stored block. }
- _tr_stored_block(s, buf, stored_len, eof);
- {$ifdef FORCE_STATIC}
- end
- else
- if (static_lenb >= 0) then
- begin { force static trees }
- {$else}
- end
- else
- if (static_lenb = opt_lenb) then
- begin
- {$endif}
- send_bits(s, (STATIC_TREES shl 1)+ord(eof), 3);
- vstatic_ltree := static_ltree;
- vstatic_dtree := static_dtree;
- compress_block(s, vstatic_ltree, vstatic_dtree);
- Inc(s.compressed_len, 3 + s.static_len);
- end
- else
- begin
- send_bits(s, (DYN_TREES shl 1)+ord(eof), 3);
- send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1,
- max_blindex+1);
- compress_block(s, s.dyn_ltree, s.dyn_dtree);
- Inc(s.compressed_len, 3 + s.opt_len);
- end;
- {$ifdef DEBUG}
- Assert (s.compressed_len = s.bits_sent, 'bad compressed size');
- {$ENDIF}
- init_block(s);
- if (eof) then
- begin
- bi_windup(s);
- Inc(s.compressed_len, 7); { align on byte boundary }
- end;
- {$ifdef DEBUG}
- Tracev(#13'comprlen %lu(%lu) {s.compressed_len shr 3,'+
- 's.compressed_len-7*ord(eof)}');
- {$ENDIF}
- _tr_flush_block := s.compressed_len shr 3;
- end;
- { ===========================================================================
- Save the match info and tally the frequency counts. Return true if
- the current block must be flushed. }
- function _tr_tally (var s : deflate_state;
- dist : unsigned; { distance of matched string }
- lc : unsigned) : boolean; { match length-MIN_MATCH or unmatched char (if dist=0) }
- var
- {$IFDEF DEBUG}
- MAX_DIST : ush;
- {$ENDIF}
- code : ush;
- {$ifdef TRUNCATE_BLOCK}
- var
- out_length : ulg;
- in_length : ulg;
- dcode : int;
- {$endif}
- begin
- s.d_buf^[s.last_lit] := ush(dist);
- s.l_buf^[s.last_lit] := uch(lc);
- Inc(s.last_lit);
- if (dist = 0) then
- begin
- { lc is the unmatched char }
- Inc(s.dyn_ltree[lc].fc.Freq);
- end
- else
- begin
- Inc(s.matches);
- { Here, lc is the match length - MIN_MATCH }
- Dec(dist); { dist := match distance - 1 }
- {macro d_code(dist)}
- if (dist) < 256 then
- code := _dist_code[dist]
- else
- code := _dist_code[256+(dist shr 7)];
- {$IFDEF DEBUG}
- {macro MAX_DIST(s) <=> ((s)^.w_size-MIN_LOOKAHEAD)
- In order to simplify the code, particularly on 16 bit machines, match
- distances are limited to MAX_DIST instead of WSIZE. }
- MAX_DIST := ush(s.w_size-MIN_LOOKAHEAD);
- Assert((dist < ush(MAX_DIST)) and
- (ush(lc) <= ush(MAX_MATCH-MIN_MATCH)) and
- (ush(code) < ush(D_CODES)), '_tr_tally: bad match');
- {$ENDIF}
- Inc(s.dyn_ltree[_length_code[lc]+LITERALS+1].fc.Freq);
- {s.dyn_dtree[d_code(dist)].Freq++;}
- Inc(s.dyn_dtree[code].fc.Freq);
- end;
- {$ifdef TRUNCATE_BLOCK}
- { Try to guess if it is profitable to stop the current block here }
- if (s.last_lit and $1fff = 0) and (s.level > 2) then
- begin
- { Compute an upper bound for the compressed length }
- out_length := ulg(s.last_lit)*Long(8);
- in_length := ulg(long(s.strstart) - s.block_start);
- for dcode := 0 to D_CODES-1 do
- begin
- Inc(out_length, ulg(s.dyn_dtree[dcode].fc.Freq *
- (Long(5)+extra_dbits[dcode])) );
- end;
- out_length := out_length shr 3;
- if (s.matches < s.last_lit div 2) and (out_length < in_length div 2) then
- begin
- _tr_tally := TRUE;
- exit;
- end;
- end;
- {$endif}
- _tr_tally := (s.last_lit = s.lit_bufsize-1);
- { We avoid equality with lit_bufsize because of wraparound at 64K
- on 16 bit machines and because stored blocks are restricted to
- 64K-1 bytes. }
- end;
- end.