home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / unity / d5 / JRZIP.ZIP / Zlib / inftrees.pas < prev    next >
Pascal/Delphi Source File  |  1999-07-01  |  29KB  |  780 lines

  1. Unit InfTrees;
  2.  
  3. { inftrees.h -- header to use inftrees.c
  4.   inftrees.c -- generate Huffman trees for efficient decoding
  5.   Copyright (C) 1995-1998 Mark Adler
  6.  
  7.   WARNING: this file should *not* be used by applications. It is
  8.    part of the implementation of the compression library and is
  9.    subject to change.
  10.  
  11.   Pascal tranlastion
  12.   Copyright (C) 1998 by Jacques Nomssi Nzali
  13.   For conditions of distribution and use, see copyright notice in readme.txt
  14. }
  15.  
  16. Interface
  17.  
  18. {$I zconf.inc}
  19.  
  20. uses
  21.   zutil, zlib;
  22.  
  23.  
  24. { Maximum size of dynamic tree.  The maximum found in a long but non-
  25.   exhaustive search was 1004 huft structures (850 for length/literals
  26.   and 154 for distances, the latter actually the result of an
  27.   exhaustive search).  The actual maximum is not known, but the
  28.   value below is more than safe. }
  29. const
  30.   MANY = 1440;
  31.  
  32.  
  33. {$ifdef DEBUG}
  34. var
  35.   inflate_hufts : uInt;
  36. {$endif}
  37.  
  38. function inflate_trees_bits(
  39.   var c : array of uIntf;  { 19 code lengths }
  40.   var bb : uIntf;          { bits tree desired/actual depth }
  41.   var tb : pinflate_huft;  { bits tree result }
  42.   var hp : array of Inflate_huft;      { space for trees }
  43.   var z : z_stream         { for messages }
  44.     ) : int;
  45.  
  46. function inflate_trees_dynamic(
  47.     nl : uInt;                    { number of literal/length codes }
  48.     nd : uInt;                    { number of distance codes }
  49.     var c : Array of uIntf;           { that many (total) code lengths }
  50.     var bl : uIntf;               { literal desired/actual bit depth }
  51.     var bd : uIntf;               { distance desired/actual bit depth }
  52. var tl : pInflate_huft;           { literal/length tree result }
  53. var td : pInflate_huft;           { distance tree result }
  54. var hp : array of Inflate_huft;   { space for trees }
  55. var z : z_stream                  { for messages }
  56.      ) : int;
  57.  
  58. function inflate_trees_fixed (
  59.     var bl : uInt;                { literal desired/actual bit depth }
  60.     var bd : uInt;                { distance desired/actual bit depth }
  61.     var tl : pInflate_huft;       { literal/length tree result }
  62.     var td : pInflate_huft;       { distance tree result }
  63.     var z : z_stream              { for memory allocation }
  64.      ) : int;
  65.  
  66.  
  67. implementation
  68.  
  69. const
  70.  inflate_copyright = 'inflate 1.1.2 Copyright 1995-1998 Mark Adler';
  71.  
  72. {
  73.   If you use the zlib library in a product, an acknowledgment is welcome
  74.   in the documentation of your product. If for some reason you cannot
  75.   include such an acknowledgment, I would appreciate that you keep this
  76.   copyright string in the executable of your product.
  77. }
  78.  
  79.  
  80. const
  81. { Tables for deflate from PKZIP's appnote.txt. }
  82.   cplens : Array [0..30] Of uInt  { Copy lengths for literal codes 257..285 }
  83.      = (3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  84.         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0);
  85.         { actually lengths - 2; also see note #13 above about 258 }
  86.  
  87.   invalid_code = 112;
  88.  
  89.   cplext : Array [0..30] Of uInt  { Extra bits for literal codes 257..285 }
  90.      = (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  91.         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, invalid_code, invalid_code);
  92.  
  93.   cpdist : Array [0..29] Of uInt { Copy offsets for distance codes 0..29 }
  94.      = (1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  95.         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  96.         8193, 12289, 16385, 24577);
  97.  
  98.   cpdext : Array [0..29] Of uInt { Extra bits for distance codes }
  99.      = (0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  100.         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  101.         12, 12, 13, 13);
  102.  
  103. {  Huffman code decoding is performed using a multi-level table lookup.
  104.    The fastest way to decode is to simply build a lookup table whose
  105.    size is determined by the longest code.  However, the time it takes
  106.    to build this table can also be a factor if the data being decoded
  107.    is not very long.  The most common codes are necessarily the
  108.    shortest codes, so those codes dominate the decoding time, and hence
  109.    the speed.  The idea is you can have a shorter table that decodes the
  110.    shorter, more probable codes, and then point to subsidiary tables for
  111.    the longer codes.  The time it costs to decode the longer codes is
  112.    then traded against the time it takes to make longer tables.
  113.  
  114.    This results of this trade are in the variables lbits and dbits
  115.    below.  lbits is the number of bits the first level table for literal/
  116.    length codes can decode in one step, and dbits is the same thing for
  117.    the distance codes.  Subsequent tables are also less than or equal to
  118.    those sizes.  These values may be adjusted either when all of the
  119.    codes are shorter than that, in which case the longest code length in
  120.    bits is used, or when the shortest code is *longer* than the requested
  121.    table size, in which case the length of the shortest code in bits is
  122.    used.
  123.  
  124.    There are two different values for the two tables, since they code a
  125.    different number of possibilities each.  The literal/length table
  126.    codes 286 possible values, or in a flat code, a little over eight
  127.    bits.  The distance table codes 30 possible values, or a little less
  128.    than five bits, flat.  The optimum values for speed end up being
  129.    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  130.    The optimum values may differ though from machine to machine, and
  131.    possibly even between compilers.  Your mileage may vary. }
  132.  
  133.  
  134. { If BMAX needs to be larger than 16, then h and x[] should be uLong. }
  135. const
  136.   BMAX = 15;         { maximum bit length of any code }
  137.  
  138. {$DEFINE USE_PTR}
  139.  
  140. function huft_build(
  141. var b : array of uIntf;    { code lengths in bits (all assumed <= BMAX) }
  142.     n : uInt;              { number of codes (assumed <= N_MAX) }
  143.     s : uInt;              { number of simple-valued codes (0..s-1) }
  144. const d : array of uIntf;  { list of base values for non-simple codes }
  145. { array of word }
  146. const e : array of uIntf;  { list of extra bits for non-simple codes }
  147. { array of byte }
  148.   t : ppInflate_huft;     { result: starting table }
  149. var m : uIntf;             { maximum lookup bits, returns actual }
  150. var hp : array of inflate_huft;  { space for trees }
  151. var hn : uInt;             { hufts used in space }
  152. var v : array of uIntf     { working area: values in order of bit length }
  153.    ) : int;
  154. { Given a list of code lengths and a maximum table size, make a set of
  155.   tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
  156.   if the given code set is incomplete (the tables are still built in this
  157.   case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
  158.   lengths), or Z_MEM_ERROR if not enough memory. }
  159. Var
  160.   a : uInt;                     { counter for codes of length k }
  161.   c : Array [0..BMAX] Of uInt;  { bit length count table }
  162.   f : uInt;                     { i repeats in table every f entries }
  163.   g : int;                      { maximum code length }
  164.   h : int;                      { table level }
  165.   i : uInt;  {register}         { counter, current code }
  166.   j : uInt;  {register}         { counter }
  167.   k : Int;   {register}         { number of bits in current code }
  168.   l : int;            { bits per table (returned in m) }
  169.   mask : uInt;                  { (1 shl w) - 1, to avoid cc -O bug on HP }
  170.   p : ^uIntf; {register}        { pointer into c[], b[], or v[] }
  171.   q : pInflate_huft;            { points to current table }
  172.   r : inflate_huft;             { table entry for structure assignment }
  173.   u : Array [0..BMAX-1] Of pInflate_huft; { table stack }
  174.   w : int;   {register}         { bits before this table = (l*h) }
  175.   x : Array [0..BMAX] Of uInt;  { bit offsets, then code stack }
  176.   {$IFDEF USE_PTR}
  177.   xp : puIntf;                  { pointer into x }
  178.   {$ELSE}
  179.   xp : uInt;
  180.   {$ENDIF}
  181.   y : int;                      { number of dummy codes added }
  182.   z : uInt;                     { number of entries in current table }
  183. Begin
  184.   { Generate counts for each bit length }
  185.   FillChar(c,SizeOf(c),0) ;     { clear c[] }
  186.  
  187.   for i := 0 to n-1 do
  188.     Inc (c[b[i]]);              { assume all entries <= BMAX }
  189.  
  190.   If (c[0] = n) Then            { null input--all zero length codes }
  191.   Begin
  192.     t^ := pInflate_huft(NIL);
  193.     m := 0 ;
  194.     huft_build := Z_OK ;
  195.     Exit;
  196.   End ;
  197.  
  198.   { Find minimum and maximum length, bound [m] by those }
  199.   l := m;
  200.   for j:=1 To BMAX do
  201.     if (c[j] <> 0) then
  202.       break;
  203.   k := j ;                      { minimum code length }
  204.   if (uInt(l) < j) then
  205.     l := j;
  206.   for i := BMAX downto 1 do
  207.     if (c[i] <> 0) then
  208.       break ;
  209.   g := i ;                      { maximum code length }
  210.   if (uInt(l) > i) then
  211.      l := i;
  212.   m := l;
  213.  
  214.   { Adjust last length count to fill out codes, if needed }
  215.   y := 1 shl j ;
  216.   while (j < i) do
  217.   begin
  218.     Dec(y, c[j]) ;
  219.     if (y < 0) then
  220.     begin
  221.       huft_build := Z_DATA_ERROR;   { bad input: more codes than bits }
  222.       exit;
  223.     end ;
  224.     Inc(j) ;
  225.     y := y shl 1
  226.   end;
  227.   Dec (y, c[i]) ;
  228.   if (y < 0) then
  229.   begin
  230.     huft_build := Z_DATA_ERROR;     { bad input: more codes than bits }
  231.     exit;
  232.   end;
  233.   Inc(c[i], y);
  234.  
  235.   { Generate starting offsets into the value table FOR each length }
  236.   {$IFDEF USE_PTR}
  237.   x[1] := 0;
  238.   j := 0;
  239.  
  240.   p := @c[1];
  241.   xp := @x[2];
  242.  
  243.   dec(i);               { note that i = g from above }
  244.   WHILE (i > 0) DO
  245.   BEGIN
  246.     inc(j, p^);
  247.     xp^ := j;
  248.     inc(p);
  249.     inc(xp);
  250.     dec(i);
  251.   END;
  252.   {$ELSE}
  253.   x[1] := 0;
  254.   j := 0 ;
  255.   for i := 1 to g do
  256.   begin
  257.     x[i] := j;
  258.     Inc(j, c[i]);
  259.   end;
  260.   {$ENDIF}
  261.  
  262.   { Make a table of values in order of bit lengths }
  263.   for i := 0 to n-1 do
  264.   begin
  265.     j := b[i];
  266.     if (j <> 0) then
  267.     begin
  268.       v[ x[j] ] := i;
  269.       Inc(x[j]);
  270.     end;
  271.   end;
  272.   n := x[g];                     { set n to length of v }
  273.  
  274.   { Generate the Huffman codes and for each, make the table entries }
  275.   i := 0 ;
  276.   x[0] := 0 ;                   { first Huffman code is zero }
  277.   p := Addr(v) ;                { grab values in bit order }
  278.   h := -1 ;                     { no tables yet--level -1 }
  279.   w := -l ;                     { bits decoded = (l*h) }
  280.  
  281.   u[0] := pInflate_huft(NIL);   { just to keep compilers happy }
  282.   q := pInflate_huft(NIL);      { ditto }
  283.   z := 0 ;                      { ditto }
  284.  
  285.   { go through the bit lengths (k already is bits in shortest code) }
  286.   while (k <= g) Do
  287.   begin
  288.     a := c[k] ;
  289.     while (a<>0) Do
  290.     begin
  291.       Dec (a) ;
  292.       { here i is the Huffman code of length k bits for value p^ }
  293.       { make tables up to required level }
  294.       while (k > w + l) do
  295.       begin
  296.  
  297.         Inc (h) ;
  298.         Inc (w, l);              { add bits already decoded }
  299.                                  { previous table always l bits }
  300.         { compute minimum size table less than or equal to l bits }
  301.  
  302.         { table size upper limit }
  303.         z := g - w;
  304.         If (z > uInt(l)) Then
  305.           z := l;
  306.  
  307.         { try a k-w bit table }
  308.         j := k - w;
  309.         f := 1 shl j;
  310.         if (f > a+1) Then        { too few codes for k-w bit table }
  311.         begin
  312.           Dec(f, a+1);           { deduct codes from patterns left }
  313.           {$IFDEF USE_PTR}
  314.           xp := Addr(c[k]);
  315.  
  316.           if (j < z) then
  317.           begin
  318.             Inc(j);
  319.             while (j < z) do
  320.             begin                { try smaller tables up to z bits }
  321.               f := f shl 1;
  322.               Inc (xp) ;
  323.               If (f <= xp^) Then
  324.                 break;           { enough codes to use up j bits }
  325.               Dec(f, xp^);       { else deduct codes from patterns }
  326.               Inc(j);
  327.             end;
  328.           end;
  329.           {$ELSE}
  330.           xp := k;
  331.  
  332.           if (j < z) then
  333.           begin
  334.             Inc (j) ;
  335.             While (j < z) Do
  336.             begin                 { try smaller tables up to z bits }
  337.               f := f * 2;
  338.               Inc (xp) ;
  339.               if (f <= c[xp]) then
  340.                 Break ;           { enough codes to use up j bits }
  341.               Dec (f, c[xp]) ;      { else deduct codes from patterns }
  342.               Inc (j);
  343.             end;
  344.           end;
  345.           {$ENDIF}
  346.         end;
  347.  
  348.         z := 1 shl j;            { table entries for j-bit table }
  349.  
  350.         { allocate new table }
  351.         if (hn + z > MANY) then { (note: doesn't matter for fixed) }
  352.         begin
  353.           huft_build := Z_MEM_ERROR;     { not enough memory }
  354.           exit;
  355.         end;
  356.  
  357.         q := @hp[hn];
  358.         u[h] := q;
  359.         Inc(hn, z);
  360.  
  361.         { connect to last table, if there is one }
  362.         if (h <> 0) then
  363.         begin
  364.           x[h] := i;             { save pattern for backing up }
  365.           r.bits := Byte(l);     { bits to dump before this table }
  366.           r.exop := Byte(j);     { bits in this table }
  367.           j := i shr (w - l);
  368.           {r.base := uInt( q - u[h-1] -j);}   { offset to this table }
  369.           r.base := (ptr2int(q) - ptr2int(u[h-1]) ) div sizeof(q^) - j;
  370.           huft_Ptr(u[h-1])^[j] := r;  { connect to last table }
  371.         end
  372.         else
  373.           t^ := q;               { first table is returned result }
  374.       end;
  375.  
  376.       { set up table entry in r }
  377.       r.bits := Byte(k - w);
  378.  
  379.       { C-code: if (p >= v + n) - see ZUTIL.PAS for comments }
  380.  
  381.       if ptr2int(p)>=ptr2int(@(v[n])) then  { also works under DPMI ?? }
  382.         r.exop := 128 + 64                  { out of values--invalid code }
  383.       else
  384.         if (p^ < s) then
  385.         begin
  386.           if (p^ < 256) then     { 256 is end-of-block code }
  387.             r.exop := 0
  388.           Else
  389.             r.exop := 32 + 64;   { EOB_code; }
  390.           r.base := p^;          { simple code is just the value }
  391.           Inc(p);
  392.         end
  393.         Else
  394.         begin
  395.           r.exop := Byte(e[p^-s] + 16 + 64);  { non-simple--look up in lists }
  396.           r.base := d[p^-s];
  397.           Inc (p);
  398.         end ;
  399.  
  400.       { fill code-like entries with r }
  401.       f := 1 shl (k - w);
  402.       j := i shr w;
  403.       while (j < z) do
  404.       begin
  405.         huft_Ptr(q)^[j] := r;
  406.         Inc(j, f);
  407.       end;
  408.  
  409.       { backwards increment the k-bit code i }
  410.       j := 1 shl (k-1) ;
  411.       while (i and j) <> 0 do
  412.       begin
  413.         i := i xor j;         { bitwise exclusive or }
  414.         j := j shr 1
  415.       end ;
  416.       i := i xor j;
  417.  
  418.       { backup over finished tables }
  419.       mask := (1 shl w) - 1;   { needed on HP, cc -O bug }
  420.       while ((i and mask) <> x[h]) do
  421.       begin
  422.         Dec(h);                { don't need to update q }
  423.         Dec(w, l);
  424.         mask := (1 shl w) - 1;
  425.       end;
  426.  
  427.     end;
  428.  
  429.     Inc(k);
  430.   end;
  431.  
  432.   { Return Z_BUF_ERROR if we were given an incomplete table }
  433.   if (y <> 0) And (g <> 1) then
  434.     huft_build := Z_BUF_ERROR
  435.   else
  436.     huft_build := Z_OK;
  437. end; { huft_build}
  438.  
  439.  
  440. function inflate_trees_bits(
  441.   var c : array of uIntf;  { 19 code lengths }
  442.   var bb : uIntf;          { bits tree desired/actual depth }
  443.   var tb : pinflate_huft;  { bits tree result }
  444.   var hp : array of Inflate_huft;      { space for trees }
  445.   var z : z_stream         { for messages }
  446.     ) : int;
  447. var
  448.   r : int;
  449.   hn : uInt;          { hufts used in space }
  450.   v : PuIntArray;     { work area for huft_build }
  451. begin
  452.   hn := 0;
  453.   v := PuIntArray( ZALLOC(z, 19, sizeof(uInt)) );
  454.   if (v = Z_NULL) then
  455.   begin
  456.     inflate_trees_bits := Z_MEM_ERROR;
  457.     exit;
  458.   end;
  459.  
  460.   r := huft_build(c, 19, 19, cplens, cplext,
  461.                              {puIntf(Z_NULL), puIntf(Z_NULL),}
  462.                   @tb, bb, hp, hn, v^);
  463.   if (r = Z_DATA_ERROR) then
  464.     z.msg := 'oversubscribed dynamic bit lengths tree'
  465.   else
  466.     if (r = Z_BUF_ERROR) or (bb = 0) then
  467.     begin
  468.       z.msg := 'incomplete dynamic bit lengths tree';
  469.       r := Z_DATA_ERROR;
  470.     end;
  471.   ZFREE(z, v);
  472.   inflate_trees_bits := r;
  473. end;
  474.  
  475.  
  476. function inflate_trees_dynamic(
  477.     nl : uInt;                    { number of literal/length codes }
  478.     nd : uInt;                    { number of distance codes }
  479.     var c : Array of uIntf;           { that many (total) code lengths }
  480.     var bl : uIntf;          { literal desired/actual bit depth }
  481.     var bd : uIntf;          { distance desired/actual bit depth }
  482. var tl : pInflate_huft;           { literal/length tree result }
  483. var td : pInflate_huft;           { distance tree result }
  484. var hp : array of Inflate_huft;   { space for trees }
  485. var z : z_stream                  { for messages }
  486.      ) : int;
  487. var
  488.   r : int;
  489.   hn : uInt;          { hufts used in space }
  490.   v : PuIntArray;     { work area for huft_build }
  491. begin
  492.   hn := 0;
  493.   { allocate work area }
  494.   v := PuIntArray( ZALLOC(z, 288, sizeof(uInt)) );
  495.   if (v = Z_NULL) then
  496.   begin
  497.     inflate_trees_dynamic := Z_MEM_ERROR;
  498.     exit;
  499.   end;
  500.  
  501.   { build literal/length tree }
  502.   r := huft_build(c, nl, 257, cplens, cplext, @tl, bl, hp, hn, v^);
  503.   if (r <> Z_OK) or (bl = 0) then
  504.   begin
  505.     if (r = Z_DATA_ERROR) then
  506.       z.msg := 'oversubscribed literal/length tree'
  507.     else
  508.       if (r <> Z_MEM_ERROR) then
  509.       begin
  510.         z.msg := 'incomplete literal/length tree';
  511.         r := Z_DATA_ERROR;
  512.       end;
  513.  
  514.     ZFREE(z, v);
  515.     inflate_trees_dynamic := r;
  516.     exit;
  517.   end;
  518.  
  519.   { build distance tree }
  520.   r := huft_build(puIntArray(@c[nl])^, nd, 0,
  521.                   cpdist, cpdext, @td, bd, hp, hn, v^);
  522.   if (r <> Z_OK) or ((bd = 0) and (nl > 257)) then
  523.   begin
  524.     if (r = Z_DATA_ERROR) then
  525.       z.msg := 'oversubscribed literal/length tree'
  526.     else
  527.       if (r = Z_BUF_ERROR) then
  528.       begin
  529. {$ifdef PKZIP_BUG_WORKAROUND}
  530.         r := Z_OK;
  531.       end;
  532. {$else}
  533.         z.msg := 'incomplete literal/length tree';
  534.         r := Z_DATA_ERROR;
  535.       end
  536.       else
  537.         if (r <> Z_MEM_ERROR) then
  538.         begin
  539.           z.msg := 'empty distance tree with lengths';
  540.           r := Z_DATA_ERROR;
  541.         end;
  542.     ZFREE(z, v);
  543.     inflate_trees_dynamic := r;
  544.     exit;
  545. {$endif}
  546.   end;
  547.  
  548.   { done }
  549.   ZFREE(z, v);
  550.   inflate_trees_dynamic := Z_OK;
  551. end;
  552.  
  553. {$UNDEF BUILDFIXED}
  554.  
  555. { build fixed tables only once--keep them here }
  556. {$IFNDEF BUILDFIXED}
  557. { locals }
  558. const
  559.   fixed_built : Boolean = false;
  560.   FIXEDH = 544;      { number of hufts used by fixed tables }
  561. var
  562.   fixed_mem : array[0..FIXEDH-1] of inflate_huft;
  563.   fixed_bl : uInt;
  564.   fixed_bd : uInt;
  565.   fixed_tl : pInflate_huft;
  566.   fixed_td : pInflate_huft;
  567.  
  568. {$ELSE}
  569.  
  570. { inffixed.h -- table for decoding fixed codes }
  571.  
  572. {local}
  573. const
  574.   fixed_bl = uInt(9);
  575. {local}
  576. const
  577.   fixed_bd = uInt(5);
  578. {local}
  579. const
  580.   fixed_tl : array [0..288-1] of inflate_huft = (
  581.     Exop,             { number of extra bits or operation }
  582.     bits : Byte;      { number of bits in this code or subcode }
  583.     {pad : uInt;}       { pad structure to a power of 2 (4 bytes for }
  584.                       {  16-bit, 8 bytes for 32-bit int's) }
  585.     base : uInt;      { literal, length base, or distance base }
  586.                       { or table offset }
  587.  
  588.     ((96,7),256), ((0,8),80), ((0,8),16), ((84,8),115), ((82,7),31),
  589.     ((0,8),112), ((0,8),48), ((0,9),192), ((80,7),10), ((0,8),96),
  590.     ((0,8),32), ((0,9),160), ((0,8),0), ((0,8),128), ((0,8),64),
  591.     ((0,9),224), ((80,7),6), ((0,8),88), ((0,8),24), ((0,9),144),
  592.     ((83,7),59), ((0,8),120), ((0,8),56), ((0,9),208), ((81,7),17),
  593.     ((0,8),104), ((0,8),40), ((0,9),176), ((0,8),8), ((0,8),136),
  594.     ((0,8),72), ((0,9),240), ((80,7),4), ((0,8),84), ((0,8),20),
  595.     ((85,8),227), ((83,7),43), ((0,8),116), ((0,8),52), ((0,9),200),
  596.     ((81,7),13), ((0,8),100), ((0,8),36), ((0,9),168), ((0,8),4),
  597.     ((0,8),132), ((0,8),68), ((0,9),232), ((80,7),8), ((0,8),92),
  598.     ((0,8),28), ((0,9),152), ((84,7),83), ((0,8),124), ((0,8),60),
  599.     ((0,9),216), ((82,7),23), ((0,8),108), ((0,8),44), ((0,9),184),
  600.     ((0,8),12), ((0,8),140), ((0,8),76), ((0,9),248), ((80,7),3),
  601.     ((0,8),82), ((0,8),18), ((85,8),163), ((83,7),35), ((0,8),114),
  602.     ((0,8),50), ((0,9),196), ((81,7),11), ((0,8),98), ((0,8),34),
  603.     ((0,9),164), ((0,8),2), ((0,8),130), ((0,8),66), ((0,9),228),
  604.     ((80,7),7), ((0,8),90), ((0,8),26), ((0,9),148), ((84,7),67),
  605.     ((0,8),122), ((0,8),58), ((0,9),212), ((82,7),19), ((0,8),106),
  606.     ((0,8),42), ((0,9),180), ((0,8),10), ((0,8),138), ((0,8),74),
  607.     ((0,9),244), ((80,7),5), ((0,8),86), ((0,8),22), ((192,8),0),
  608.     ((83,7),51), ((0,8),118), ((0,8),54), ((0,9),204), ((81,7),15),
  609.     ((0,8),102), ((0,8),38), ((0,9),172), ((0,8),6), ((0,8),134),
  610.     ((0,8),70), ((0,9),236), ((80,7),9), ((0,8),94), ((0,8),30),
  611.     ((0,9),156), ((84,7),99), ((0,8),126), ((0,8),62), ((0,9),220),
  612.     ((82,7),27), ((0,8),110), ((0,8),46), ((0,9),188), ((0,8),14),
  613.     ((0,8),142), ((0,8),78), ((0,9),252), ((96,7),256), ((0,8),81),
  614.     ((0,8),17), ((85,8),131), ((82,7),31), ((0,8),113), ((0,8),49),
  615.     ((0,9),194), ((80,7),10), ((0,8),97), ((0,8),33), ((0,9),162),
  616.     ((0,8),1), ((0,8),129), ((0,8),65), ((0,9),226), ((80,7),6),
  617.     ((0,8),89), ((0,8),25), ((0,9),146), ((83,7),59), ((0,8),121),
  618.     ((0,8),57), ((0,9),210), ((81,7),17), ((0,8),105), ((0,8),41),
  619.     ((0,9),178), ((0,8),9), ((0,8),137), ((0,8),73), ((0,9),242),
  620.     ((80,7),4), ((0,8),85), ((0,8),21), ((80,8),258), ((83,7),43),
  621.     ((0,8),117), ((0,8),53), ((0,9),202), ((81,7),13), ((0,8),101),
  622.     ((0,8),37), ((0,9),170), ((0,8),5), ((0,8),133), ((0,8),69),
  623.     ((0,9),234), ((80,7),8), ((0,8),93), ((0,8),29), ((0,9),154),
  624.     ((84,7),83), ((0,8),125), ((0,8),61), ((0,9),218), ((82,7),23),
  625.     ((0,8),109), ((0,8),45), ((0,9),186), ((0,8),13), ((0,8),141),
  626.     ((0,8),77), ((0,9),250), ((80,7),3), ((0,8),83), ((0,8),19),
  627.     ((85,8),195), ((83,7),35), ((0,8),115), ((0,8),51), ((0,9),198),
  628.     ((81,7),11), ((0,8),99), ((0,8),35), ((0,9),166), ((0,8),3),
  629.     ((0,8),131), ((0,8),67), ((0,9),230), ((80,7),7), ((0,8),91),
  630.     ((0,8),27), ((0,9),150), ((84,7),67), ((0,8),123), ((0,8),59),
  631.     ((0,9),214), ((82,7),19), ((0,8),107), ((0,8),43), ((0,9),182),
  632.     ((0,8),11), ((0,8),139), ((0,8),75), ((0,9),246), ((80,7),5),
  633.     ((0,8),87), ((0,8),23), ((192,8),0), ((83,7),51), ((0,8),119),
  634.     ((0,8),55), ((0,9),206), ((81,7),15), ((0,8),103), ((0,8),39),
  635.     ((0,9),174), ((0,8),7), ((0,8),135), ((0,8),71), ((0,9),238),
  636.     ((80,7),9), ((0,8),95), ((0,8),31), ((0,9),158), ((84,7),99),
  637.     ((0,8),127), ((0,8),63), ((0,9),222), ((82,7),27), ((0,8),111),
  638.     ((0,8),47), ((0,9),190), ((0,8),15), ((0,8),143), ((0,8),79),
  639.     ((0,9),254), ((96,7),256), ((0,8),80), ((0,8),16), ((84,8),115),
  640.     ((82,7),31), ((0,8),112), ((0,8),48), ((0,9),193), ((80,7),10),
  641.     ((0,8),96), ((0,8),32), ((0,9),161), ((0,8),0), ((0,8),128),
  642.     ((0,8),64), ((0,9),225), ((80,7),6), ((0,8),88), ((0,8),24),
  643.     ((0,9),145), ((83,7),59), ((0,8),120), ((0,8),56), ((0,9),209),
  644.     ((81,7),17), ((0,8),104), ((0,8),40), ((0,9),177), ((0,8),8),
  645.     ((0,8),136), ((0,8),72), ((0,9),241), ((80,7),4), ((0,8),84),
  646.     ((0,8),20), ((85,8),227), ((83,7),43), ((0,8),116), ((0,8),52),
  647.     ((0,9),201), ((81,7),13), ((0,8),100), ((0,8),36), ((0,9),169),
  648.     ((0,8),4), ((0,8),132), ((0,8),68), ((0,9),233), ((80,7),8),
  649.     ((0,8),92), ((0,8),28), ((0,9),153), ((84,7),83), ((0,8),124),
  650.     ((0,8),60), ((0,9),217), ((82,7),23), ((0,8),108), ((0,8),44),
  651.     ((0,9),185), ((0,8),12), ((0,8),140), ((0,8),76), ((0,9),249),
  652.     ((80,7),3), ((0,8),82), ((0,8),18), ((85,8),163), ((83,7),35),
  653.     ((0,8),114), ((0,8),50), ((0,9),197), ((81,7),11), ((0,8),98),
  654.     ((0,8),34), ((0,9),165), ((0,8),2), ((0,8),130), ((0,8),66),
  655.     ((0,9),229), ((80,7),7), ((0,8),90), ((0,8),26), ((0,9),149),
  656.     ((84,7),67), ((0,8),122), ((0,8),58), ((0,9),213), ((82,7),19),
  657.     ((0,8),106), ((0,8),42), ((0,9),181), ((0,8),10), ((0,8),138),
  658.     ((0,8),74), ((0,9),245), ((80,7),5), ((0,8),86), ((0,8),22),
  659.     ((192,8),0), ((83,7),51), ((0,8),118), ((0,8),54), ((0,9),205),
  660.     ((81,7),15), ((0,8),102), ((0,8),38), ((0,9),173), ((0,8),6),
  661.     ((0,8),134), ((0,8),70), ((0,9),237), ((80,7),9), ((0,8),94),
  662.     ((0,8),30), ((0,9),157), ((84,7),99), ((0,8),126), ((0,8),62),
  663.     ((0,9),221), ((82,7),27), ((0,8),110), ((0,8),46), ((0,9),189),
  664.     ((0,8),14), ((0,8),142), ((0,8),78), ((0,9),253), ((96,7),256),
  665.     ((0,8),81), ((0,8),17), ((85,8),131), ((82,7),31), ((0,8),113),
  666.     ((0,8),49), ((0,9),195), ((80,7),10), ((0,8),97), ((0,8),33),
  667.     ((0,9),163), ((0,8),1), ((0,8),129), ((0,8),65), ((0,9),227),
  668.     ((80,7),6), ((0,8),89), ((0,8),25), ((0,9),147), ((83,7),59),
  669.     ((0,8),121), ((0,8),57), ((0,9),211), ((81,7),17), ((0,8),105),
  670.     ((0,8),41), ((0,9),179), ((0,8),9), ((0,8),137), ((0,8),73),
  671.     ((0,9),243), ((80,7),4), ((0,8),85), ((0,8),21), ((80,8),258),
  672.     ((83,7),43), ((0,8),117), ((0,8),53), ((0,9),203), ((81,7),13),
  673.     ((0,8),101), ((0,8),37), ((0,9),171), ((0,8),5), ((0,8),133),
  674.     ((0,8),69), ((0,9),235), ((80,7),8), ((0,8),93), ((0,8),29),
  675.     ((0,9),155), ((84,7),83), ((0,8),125), ((0,8),61), ((0,9),219),
  676.     ((82,7),23), ((0,8),109), ((0,8),45), ((0,9),187), ((0,8),13),
  677.     ((0,8),141), ((0,8),77), ((0,9),251), ((80,7),3), ((0,8),83),
  678.     ((0,8),19), ((85,8),195), ((83,7),35), ((0,8),115), ((0,8),51),
  679.     ((0,9),199), ((81,7),11), ((0,8),99), ((0,8),35), ((0,9),167),
  680.     ((0,8),3), ((0,8),131), ((0,8),67), ((0,9),231), ((80,7),7),
  681.     ((0,8),91), ((0,8),27), ((0,9),151), ((84,7),67), ((0,8),123),
  682.     ((0,8),59), ((0,9),215), ((82,7),19), ((0,8),107), ((0,8),43),
  683.     ((0,9),183), ((0,8),11), ((0,8),139), ((0,8),75), ((0,9),247),
  684.     ((80,7),5), ((0,8),87), ((0,8),23), ((192,8),0), ((83,7),51),
  685.     ((0,8),119), ((0,8),55), ((0,9),207), ((81,7),15), ((0,8),103),
  686.     ((0,8),39), ((0,9),175), ((0,8),7), ((0,8),135), ((0,8),71),
  687.     ((0,9),239), ((80,7),9), ((0,8),95), ((0,8),31), ((0,9),159),
  688.     ((84,7),99), ((0,8),127), ((0,8),63), ((0,9),223), ((82,7),27),
  689.     ((0,8),111), ((0,8),47), ((0,9),191), ((0,8),15), ((0,8),143),
  690.     ((0,8),79), ((0,9),255)
  691.   );
  692.  
  693. {local}
  694. const
  695.   fixed_td : array[0..32-1] of inflate_huft = (
  696. (Exop:80;bits:5;base:1),      (Exop:87;bits:5;base:257),   (Exop:83;bits:5;base:17),
  697. (Exop:91;bits:5;base:4097),   (Exop:81;bits:5;base),       (Exop:89;bits:5;base:1025),
  698. (Exop:85;bits:5;base:65),     (Exop:93;bits:5;base:16385), (Exop:80;bits:5;base:3),
  699. (Exop:88;bits:5;base:513),    (Exop:84;bits:5;base:33),    (Exop:92;bits:5;base:8193),
  700. (Exop:82;bits:5;base:9),      (Exop:90;bits:5;base:2049),  (Exop:86;bits:5;base:129),
  701. (Exop:192;bits:5;base:24577), (Exop:80;bits:5;base:2),     (Exop:87;bits:5;base:385),
  702. (Exop:83;bits:5;base:25),     (Exop:91;bits:5;base:6145),  (Exop:81;bits:5;base:7),
  703. (Exop:89;bits:5;base:1537),   (Exop:85;bits:5;base:97),    (Exop:93;bits:5;base:24577),
  704. (Exop:80;bits:5;base:4),      (Exop:88;bits:5;base:769),   (Exop:84;bits:5;base:49),
  705. (Exop:92;bits:5;base:12289),  (Exop:82;bits:5;base:13),    (Exop:90;bits:5;base:3073),
  706. (Exop:86;bits:5;base:193),    (Exop:192;bits:5;base:24577)
  707.   );
  708. {$ENDIF}
  709.  
  710. function inflate_trees_fixed(
  711. var bl : uInt;               { literal desired/actual bit depth }
  712. var bd : uInt;               { distance desired/actual bit depth }
  713. var tl : pInflate_huft;      { literal/length tree result }
  714. var td : pInflate_huft;      { distance tree result }
  715. var  z : z_stream            { for memory allocation }
  716.       ) : int;
  717. type
  718.   pFixed_table = ^fixed_table;
  719.   fixed_table = array[0..288-1] of uIntf;
  720. var
  721.   k : int;                   { temporary variable }
  722.   c : pFixed_table;          { length list for huft_build }
  723.   v : PuIntArray;            { work area for huft_build }
  724. var
  725.   f : uInt;                  { number of hufts used in fixed_mem }
  726. begin
  727.   { build fixed tables if not already (multiple overlapped executions ok) }
  728.   if not fixed_built then
  729.   begin
  730.     f := 0;
  731.  
  732.     { allocate memory }
  733.     c := pFixed_table( ZALLOC(z, 288, sizeof(uInt)) );
  734.     if (c = Z_NULL) then
  735.     begin
  736.       inflate_trees_fixed := Z_MEM_ERROR;
  737.       exit;
  738.     end;
  739.     v := PuIntArray( ZALLOC(z, 288, sizeof(uInt)) );
  740.     if (v = Z_NULL) then
  741.     begin
  742.       ZFREE(z, c);
  743.       inflate_trees_fixed := Z_MEM_ERROR;
  744.       exit;
  745.     end;
  746.  
  747.     { literal table }
  748.     for k := 0 to Pred(144) do
  749.       c^[k] := 8;
  750.     for k := 144 to Pred(256) do
  751.       c^[k] := 9;
  752.     for k := 256 to Pred(280) do
  753.       c^[k] := 7;
  754.     for k := 280 to Pred(288) do
  755.       c^[k] := 8;
  756.     fixed_bl := 9;
  757.     huft_build(c^, 288, 257, cplens, cplext, @fixed_tl, fixed_bl,
  758.                fixed_mem, f, v^);
  759.  
  760.     { distance table }
  761.     for k := 0 to Pred(30) do
  762.       c^[k] := 5;
  763.     fixed_bd := 5;
  764.     huft_build(c^, 30, 0, cpdist, cpdext, @fixed_td, fixed_bd,
  765.                fixed_mem, f, v^);
  766.  
  767.     { done }
  768.     ZFREE(z, v);
  769.     ZFREE(z, c);
  770.     fixed_built := True;
  771.   end;
  772.   bl := fixed_bl;
  773.   bd := fixed_bd;
  774.   tl := fixed_tl;
  775.   td := fixed_td;
  776.   inflate_trees_fixed := Z_OK;
  777. end; { inflate_trees_fixed }
  778.  
  779.  
  780. end.