home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / unity / d5 / JRZIP.ZIP / Zlib / infutil.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-04-03  |  5.0 KB  |  223 lines

  1. Unit infutil;
  2.  
  3. { types and macros common to blocks and codes
  4.   Copyright (C) 1995-1998 Mark Adler
  5.  
  6.    WARNING: this file should *not* be used by applications. It is
  7.    part of the implementation of the compression library and is
  8.    subject to change.
  9.  
  10.   Pascal tranlastion
  11.   Copyright (C) 1998 by Jacques Nomssi Nzali
  12.   For conditions of distribution and use, see copyright notice in readme.txt
  13. }
  14.  
  15. interface
  16.  
  17. {$I zconf.inc}
  18.  
  19. uses
  20.   zutil, zlib;
  21.  
  22. { copy as much as possible from the sliding window to the output area }
  23. function inflate_flush(var s : inflate_blocks_state;
  24.                        var z : z_stream;
  25.                        r : int) : int;
  26.  
  27. { And'ing with mask[n] masks the lower n bits }
  28. const
  29.   inflate_mask : array[0..17-1] of uInt = (
  30.     $0000,
  31.     $0001, $0003, $0007, $000f, $001f, $003f, $007f, $00ff,
  32.     $01ff, $03ff, $07ff, $0fff, $1fff, $3fff, $7fff, $ffff);
  33.  
  34. {procedure GRABBITS(j : int);}
  35. {procedure DUMPBITS(j : int);}
  36. {procedure NEEDBITS(j : int);}
  37.  
  38. implementation
  39.  
  40. { macros for bit input with no checking and for returning unused bytes }
  41. //procedure GRABBITS(j : int);
  42. //begin
  43.   {while (k < j) do
  44.   begin
  45.     Dec(z^.avail_in);
  46.     Inc(z^.total_in);
  47.     b := b or (uLong(z^.next_in^) shl k);
  48.     Inc(z^.next_in);
  49.     Inc(k, 8);
  50.   end;}
  51. //end;
  52.  
  53. //procedure DUMPBITS(j : int);
  54. //begin
  55.   {b := b shr j;
  56.   Dec(k, j);}
  57. //end;
  58.  
  59. //procedure NEEDBITS(j : int);
  60. //begin
  61.  (*
  62.           while (k < j) do
  63.           begin
  64.             {NEEDBYTE;}
  65.             if (n <> 0) then
  66.               r :=Z_OK
  67.             else
  68.             begin
  69.               {UPDATE}
  70.               s.bitb := b;
  71.               s.bitk := k;
  72.               z.avail_in := n;
  73.               Inc(z.total_in, LongInt(p)-LongInt(z.next_in));
  74.               z.next_in := p;
  75.               s.write := q;
  76.               result := inflate_flush(s,z,r);
  77.               exit;
  78.             end;
  79.             Dec(n);
  80.             b := b or (uLong(p^) shl k);
  81.             Inc(p);
  82.             Inc(k, 8);
  83.           end;
  84.  *)
  85. //end;
  86.  
  87. //procedure NEEDOUT;
  88. //begin
  89.  (*
  90.   if (m = 0) then
  91.   begin
  92.     {WRAP}
  93.     if (q = s.zend) and (s.read <> s.window) then
  94.     begin
  95.       q := s.window;
  96.       if LongInt(q) < LongInt(s.read) then
  97.         m := uInt(LongInt(s.read)-LongInt(q)-1)
  98.       else
  99.         m := uInt(LongInt(s.zend)-LongInt(q));
  100.     end;
  101.  
  102.     if (m = 0) then
  103.     begin
  104.       {FLUSH}
  105.       s.write := q;
  106.       r := inflate_flush(s,z,r);
  107.       q := s.write;
  108.       if LongInt(q) < LongInt(s.read) then
  109.         m := uInt(LongInt(s.read)-LongInt(q)-1)
  110.       else
  111.         m := uInt(LongInt(s.zend)-LongInt(q));
  112.  
  113.       {WRAP}
  114.       if (q = s.zend) and (s.read <> s.window) then
  115.       begin
  116.         q := s.window;
  117.         if LongInt(q) < LongInt(s.read) then
  118.           m := uInt(LongInt(s.read)-LongInt(q)-1)
  119.         else
  120.           m := uInt(LongInt(s.zend)-LongInt(q));
  121.       end;
  122.  
  123.       if (m = 0) then
  124.       begin
  125.         {UPDATE}
  126.         s.bitb := b;
  127.         s.bitk := k;
  128.         z.avail_in := n;
  129.         Inc(z.total_in, LongInt(p)-LongInt(z.next_in));
  130.         z.next_in := p;
  131.         s.write := q;
  132.         result := inflate_flush(s,z,r);
  133.         exit;
  134.       end;
  135.     end;
  136.   end;
  137.   r := Z_OK;
  138.  *)
  139. //end;
  140.  
  141. { copy as much as possible from the sliding window to the output area }
  142. function inflate_flush(var s : inflate_blocks_state;
  143.                        var z : z_stream;
  144.                        r : int) : int;
  145. var
  146.   n : uInt;
  147.   p : pBytef;
  148.   q : pBytef;
  149. begin
  150.   { local copies of source and destination pointers }
  151.   p := z.next_out;
  152.   q := s.read;
  153.  
  154.   { compute number of bytes to copy as far as end of window }
  155.   if ptr2int(q) <= ptr2int(s.write) then
  156.     n := uInt(ptr2int(s.write) - ptr2int(q))
  157.   else
  158.     n := uInt(ptr2int(s.zend) - ptr2int(q));
  159.   if (n > z.avail_out) then
  160.     n := z.avail_out;
  161.   if (n <> 0) and (r = Z_BUF_ERROR) then
  162.     r := Z_OK;
  163.  
  164.   { update counters }
  165.   Dec(z.avail_out, n);
  166.   Inc(z.total_out, n);
  167.  
  168.  
  169.   { update check information }
  170.   if Assigned(s.checkfn) then
  171.   begin
  172.     s.check := s.checkfn(s.check, q, n);
  173.     z.adler := s.check;
  174.   end;
  175.  
  176.   { copy as far as end of window }
  177.   zmemcpy(p, q, n);
  178.   Inc(p, n);
  179.   Inc(q, n);
  180.  
  181.   { see if more to copy at beginning of window }
  182.   if (q = s.zend) then
  183.   begin
  184.     { wrap pointers }
  185.     q := s.window;
  186.     if (s.write = s.zend) then
  187.       s.write := s.window;
  188.  
  189.     { compute bytes to copy }
  190.     n := uInt(ptr2int(s.write) - ptr2int(q));
  191.     if (n > z.avail_out) then
  192.       n := z.avail_out;
  193.     if (n <> 0) and (r = Z_BUF_ERROR) then
  194.       r := Z_OK;
  195.  
  196.     { update counters }
  197.     Dec( z.avail_out, n);
  198.     Inc( z.total_out, n);
  199.  
  200.     { update check information }
  201.     if Assigned(s.checkfn) then
  202.     begin
  203.       s.check := s.checkfn(s.check, q, n);
  204.       z.adler := s.check;
  205.     end;
  206.  
  207.     { copy }
  208.     zmemcpy(p, q, n);
  209.     Inc(p, n);
  210.     Inc(q, n);
  211.   end;
  212.  
  213.  
  214.   { update pointers }
  215.   z.next_out := p;
  216.   s.read := q;
  217.  
  218.   { done }
  219.   inflate_flush := r;
  220. end;
  221.  
  222. end.
  223.