~ubuntu-branches/ubuntu/feisty/fpc/feisty

« back to all changes in this revision

Viewing changes to packages/base/paszlib/zdeflate.pas

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-01-27 20:08:50 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070127200850-9mrptaqqjsx9nwa7
Tags: 2.0.4-5
* Fixed Build-Depends.
* Add myself to Uploaders in debian/control.
* Make sure that the sources are really patched before building them.
* Build unit 'libc' on powerpc too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Unit zDeflate;
 
1
unit zdeflate;
2
2
 
3
 
{$ifdef fpc}
4
3
{$goto on}
5
 
{$endif}
6
4
 
7
5
{ Orginal: deflate.h -- internal compression state
8
6
           deflate.c -- compress data using the deflation algorithm
55
53
       Fiala,E.R., and Greene,D.H.
56
54
          Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595}
57
55
 
58
 
{ $Id: zdeflate.pas,v 1.2 2002/09/07 15:42:53 peter Exp $ }
 
56
{ $Id: deflate.c,v 1.14 1996/07/02 12:40:55 me Exp $ }
59
57
 
60
58
interface
61
59
 
62
60
{$I zconf.inc}
63
61
 
64
62
uses
65
 
  zutil, zbase;
 
63
 zbase;
66
64
 
67
65
 
68
66
function deflateInit_(strm : z_streamp;
69
 
                      level : int;
 
67
                      level : integer;
70
68
                      const version : string;
71
 
                      stream_size : int) : int;
72
 
 
73
 
 
74
 
function deflateInit (var strm : z_stream; level : int) : int;
 
69
                      stream_size : integer) : integer;
 
70
 
 
71
 
 
72
function deflateInit (var strm : z_stream; level : integer) : integer;
75
73
 
76
74
{  Initializes the internal stream state for compression. The fields
77
75
   zalloc, zfree and opaque must be initialized before by the caller.
93
91
 
94
92
 
95
93
{EXPORT}
96
 
function deflate (var strm : z_stream; flush : int) : int;
 
94
function deflate (var strm : z_stream; flush : integer) : integer;
97
95
 
98
96
{ Performs one or both of the following actions:
99
97
 
161
159
  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible. }
162
160
 
163
161
 
164
 
function deflateEnd (var strm : z_stream) : int;
 
162
function deflateEnd (var strm : z_stream) : integer;
165
163
 
166
164
{     All dynamically allocated data structures for this stream are freed.
167
165
   This function discards any unprocessed input and does not flush any
180
178
 
181
179
{ The following functions are needed only in some special applications. }
182
180
 
183
 
function deflateInit2_(var strm : z_stream;
184
 
                       level : int;
185
 
                       method : int;
186
 
                       windowBits : int;
187
 
                       memLevel : int;
188
 
                       strategy : int;
189
 
                       const version : string;
190
 
                       stream_size : int) : int;
191
181
 
192
182
{EXPORT}
193
183
function deflateInit2 (var strm : z_stream;
194
 
                       level : int;
195
 
                       method : int;
196
 
                       windowBits : int;
197
 
                       memLevel : int;
198
 
                       strategy : int) : int;
 
184
                       level : integer;
 
185
                       method : integer;
 
186
                       windowBits : integer;
 
187
                       memLevel : integer;
 
188
                       strategy : integer) : integer;
 
189
function deflateInit2_(var strm : z_stream;
 
190
                       level : integer;
 
191
                       method : integer;
 
192
                       windowBits : integer;
 
193
                       memLevel : integer;
 
194
                       strategy : integer;
 
195
                       const version : string;
 
196
                       stream_size : integer) : integer;
199
197
 
200
198
{  This is another version of deflateInit with more compression options. The
201
199
   fields next_in, zalloc, zfree and opaque must be initialized before by
250
248
 
251
249
{EXPORT}
252
250
function deflateSetDictionary (var strm : z_stream;
253
 
                               dictionary : pBytef; {const bytes}
254
 
                               dictLength : uint) : int;
 
251
                               dictionary : Pbyte; {const bytes}
 
252
                               dictLength : cardinal) : integer;
255
253
 
256
254
{    Initializes the compression dictionary (history buffer) from the given
257
255
   byte sequence without producing any compressed output. This function must
279
277
 
280
278
{EXPORT}
281
279
function deflateCopy (dest : z_streamp;
282
 
                      source : z_streamp) : int;
 
280
                      source : z_streamp) : integer;
283
281
 
284
282
{  Sets the destination stream as a complete copy of the source stream.  If
285
283
   the source stream is using an application-supplied history buffer, a new
301
299
   destination. }
302
300
 
303
301
{EXPORT}
304
 
function deflateReset (var strm : z_stream) : int;
 
302
function deflateReset (var strm : z_stream) : integer;
305
303
 
306
304
{   This function is equivalent to deflateEnd followed by deflateInit,
307
305
   but does not free and reallocate all the internal compression state.
313
311
 
314
312
 
315
313
{EXPORT}
316
 
function deflateParams (var strm : z_stream; level : int; strategy : int) : int;
 
314
function deflateParams (var strm : z_stream; level : integer; strategy : integer) : integer;
317
315
 
318
316
{    Dynamically update the compression level and compression strategy.
319
317
   This can be used to switch between compression and straight copy of
356
354
 
357
355
{ Compression function. Returns the block state after the call. }
358
356
type
359
 
  compress_func = function(var s : deflate_state; flush : int) : block_state;
 
357
  compress_func = function(var s : deflate_state; flush : integer) : block_state;
360
358
 
361
359
{local}
362
360
procedure fill_window(var s : deflate_state); forward;
363
361
{local}
364
 
function deflate_stored(var s : deflate_state; flush : int) : block_state;{$ifndef fpc}far;{$endif} forward;
365
 
{local}
366
 
function deflate_fast(var s : deflate_state; flush : int) : block_state;{$ifndef fpc}far;{$endif} forward;
367
 
{local}
368
 
function deflate_slow(var s : deflate_state; flush : int) : block_state;{$ifndef fpc}far;{$endif} forward;
 
362
function deflate_stored(var s : deflate_state; flush : integer) : block_state; far; forward;
 
363
{local}
 
364
function deflate_fast(var s : deflate_state; flush : integer) : block_state; far; forward;
 
365
{local}
 
366
function deflate_slow(var s : deflate_state; flush : integer) : block_state; far; forward;
369
367
{local}
370
368
procedure lm_init(var s : deflate_state); forward;
371
369
 
372
370
{local}
373
 
procedure putShortMSB(var s : deflate_state; b : uInt); forward;
 
371
procedure putShortMSB(var s : deflate_state; b : cardinal); forward;
374
372
{local}
375
373
procedure  flush_pending (var strm : z_stream); forward;
376
374
{local}
377
375
function read_buf(strm : z_streamp;
378
 
                  buf : pBytef;
379
 
                  size : unsigned) : int; forward;
 
376
                  buf : Pbyte;
 
377
                  size : cardinal) : cardinal; forward;
380
378
{$ifdef ASMV}
381
379
procedure match_init; { asm code initialization }
382
 
function longest_match(var deflate_state; cur_match : IPos) : uInt; forward;
 
380
function longest_match(var deflate_state; cur_match : IPos) : cardinal; forward;
383
381
{$else}
384
382
{local}
385
 
function longest_match(var s : deflate_state; cur_match : IPos) : uInt;
 
383
function longest_match(var s : deflate_state; cur_match : IPos) : cardinal;
386
384
  forward;
387
385
{$endif}
388
386
 
389
 
{$ifdef DEBUG}
 
387
{$ifdef ZLIB_DEBUG}
390
388
{local}
391
389
procedure check_match(var s : deflate_state;
392
390
                      start, match : IPos;
393
 
                      length : int); forward;
 
391
                      length : integer); forward;
394
392
{$endif}
395
393
 
396
394
{  ==========================================================================
409
407
{ Minimum amount of lookahead, except at the end of the input file.
410
408
  See deflate.c for comments about the MIN_MATCH+1. }
411
409
 
412
 
{macro MAX_DIST(var s : deflate_state) : uInt;
 
410
{macro MAX_DIST(var s : deflate_state) : cardinal;
413
411
begin
414
412
  MAX_DIST := (s.w_size - MIN_LOOKAHEAD);
415
413
end;
424
422
 
425
423
type
426
424
  config = record
427
 
   good_length : ush; { reduce lazy search above this match length }
428
 
   max_lazy : ush;    { do not perform lazy search above this match length }
429
 
   nice_length : ush; { quit search above this match length }
430
 
   max_chain : ush;
 
425
   good_length : word; { reduce lazy search above this match length }
 
426
   max_lazy : word;    { do not perform lazy search above this match length }
 
427
   nice_length : word; { quit search above this match length }
 
428
   max_chain : word;
431
429
   func : compress_func;
432
430
  end;
433
431
 
476
474
     (except for the last MIN_MATCH-1 bytes of the input file). }
477
475
 
478
476
procedure INSERT_STRING(var s : deflate_state;
479
 
                        str : uInt;
 
477
                        str : cardinal;
480
478
                        var match_head : IPos);
481
479
begin
482
480
{$ifdef FASTEST}
502
500
 
503
501
macro CLEAR_HASH(s)
504
502
    s^.head[s^.hash_size-1] := ZNIL;
505
 
    zmemzero(pBytef(s^.head), unsigned(s^.hash_size-1)*sizeof(s^.head^[0]));
 
503
    zmemzero(Pbyte(s^.head), cardinal(s^.hash_size-1)*sizeof(s^.head^[0]));
506
504
}
507
505
 
508
506
{  ======================================================================== }
509
507
 
510
508
function deflateInit2_(var strm : z_stream;
511
 
                       level : int;
512
 
                       method : int;
513
 
                       windowBits : int;
514
 
                       memLevel : int;
515
 
                       strategy : int;
 
509
                       level : integer;
 
510
                       method : integer;
 
511
                       windowBits : integer;
 
512
                       memLevel : integer;
 
513
                       strategy : integer;
516
514
                       const version : string;
517
 
                       stream_size : int) : int;
 
515
                       stream_size : integer) : integer;
518
516
var
519
517
  s : deflate_state_ptr;
520
 
  noheader : int;
 
518
  noheader : integer;
521
519
 
522
 
  overlay : pushfArray;
 
520
  overlay : Pwordarray;
523
521
  { We overlay pending_buf and d_buf+l_buf. This works since the average
524
522
    output size for (length,distance) codes is <= 24 bits. }
525
523
begin
539
537
  }
540
538
  { SetLength(strm.msg, 255); }
541
539
  strm.msg := '';
542
 
  if not Assigned(strm.zalloc) then
543
 
  begin
544
 
{$ifdef fpc}
545
 
    strm.zalloc := @zcalloc;
546
 
{$else}
547
 
    strm.zalloc := zcalloc;
548
 
{$endif}
549
 
    strm.opaque := voidpf(0);
550
 
  end;
551
 
  if not Assigned(strm.zfree) then
552
 
{$ifdef fpc}
553
 
    strm.zfree := @zcfree;
554
 
{$else}
555
 
    strm.zfree := zcfree;
556
 
{$endif}
 
540
 
557
541
  if (level  =  Z_DEFAULT_COMPRESSION) then
558
542
    level := 6;
559
543
{$ifdef FASTEST}
574
558
  end;
575
559
 
576
560
  s := deflate_state_ptr (ZALLOC(strm, 1, sizeof(deflate_state)));
577
 
  if (s = Z_NULL) then
 
561
  if (s = nil) then
578
562
  begin
579
563
    deflateInit2_ := Z_MEM_ERROR;
580
564
    exit;
592
576
  s^.hash_mask := s^.hash_size - 1;
593
577
  s^.hash_shift :=  ((s^.hash_bits+MIN_MATCH-1) div MIN_MATCH);
594
578
 
595
 
  s^.window := pzByteArray (ZALLOC(strm, s^.w_size, 2*sizeof(Byte)));
 
579
  s^.window := Pbytearray (ZALLOC(strm, s^.w_size, 2*sizeof(Byte)));
596
580
  s^.prev   := pzPosfArray (ZALLOC(strm, s^.w_size, sizeof(Pos)));
597
581
  s^.head   := pzPosfArray (ZALLOC(strm, s^.hash_size, sizeof(Pos)));
598
582
 
599
583
  s^.lit_bufsize := 1 shl (memLevel + 6); { 16K elements by default }
600
584
 
601
 
  overlay := pushfArray (ZALLOC(strm, s^.lit_bufsize, sizeof(ush)+2));
602
 
  s^.pending_buf := pzByteArray (overlay);
603
 
  s^.pending_buf_size := ulg(s^.lit_bufsize) * (sizeof(ush)+Long(2));
 
585
  overlay := Pwordarray (ZALLOC(strm, s^.lit_bufsize, sizeof(word)+2));
 
586
  s^.pending_buf := Pbytearray(overlay);
 
587
  s^.pending_buf_size := longint(s^.lit_bufsize) * (sizeof(word)+longint(2));
604
588
 
605
589
  if (s^.window = Z_NULL) or (s^.prev = Z_NULL) or (s^.head = Z_NULL)
606
590
   or (s^.pending_buf = Z_NULL) then
611
595
    deflateInit2_ := Z_MEM_ERROR;
612
596
    exit;
613
597
  end;
614
 
  s^.d_buf := pushfArray( @overlay^[s^.lit_bufsize div sizeof(ush)] );
615
 
  s^.l_buf := puchfArray( @s^.pending_buf^[(1+sizeof(ush))*s^.lit_bufsize] );
 
598
  s^.d_buf := Pwordarray( @overlay^[s^.lit_bufsize div sizeof(word)] );
 
599
  s^.l_buf := Pbytearray( @s^.pending_buf^[(1+sizeof(word))*s^.lit_bufsize] );
616
600
 
617
601
  s^.level := level;
618
602
  s^.strategy := strategy;
624
608
{  ========================================================================= }
625
609
 
626
610
function deflateInit2(var strm : z_stream;
627
 
                      level : int;
628
 
                      method : int;
629
 
                      windowBits : int;
630
 
                      memLevel : int;
631
 
                      strategy : int) : int;
 
611
                      level : integer;
 
612
                      method : integer;
 
613
                      windowBits : integer;
 
614
                      memLevel : integer;
 
615
                      strategy : integer) : integer;
632
616
{ a macro }
633
617
begin
634
618
  deflateInit2 := deflateInit2_(strm, level, method, windowBits,
638
622
{  ========================================================================= }
639
623
 
640
624
function deflateInit_(strm : z_streamp;
641
 
                      level : int;
 
625
                      level : integer;
642
626
                      const version : string;
643
 
                      stream_size : int) : int;
 
627
                      stream_size : integer) : integer;
644
628
begin
645
629
  if (strm = Z_NULL) then
646
630
    deflateInit_ := Z_STREAM_ERROR
652
636
 
653
637
{  ========================================================================= }
654
638
 
655
 
function deflateInit(var strm : z_stream; level : int) : int;
 
639
function deflateInit(var strm : z_stream; level : integer) : integer;
656
640
{ deflateInit is a macro to allow checking the zlib version
657
641
  and the compiler's view of z_stream: }
658
642
begin
662
646
 
663
647
{  ======================================================================== }
664
648
function deflateSetDictionary (var strm : z_stream;
665
 
                               dictionary : pBytef;
666
 
                               dictLength : uInt) : int;
 
649
                               dictionary : Pbyte;
 
650
                               dictLength : cardinal) : integer;
667
651
var
668
652
  s : deflate_state_ptr;
669
 
  length : uInt;
670
 
  n : uInt;
 
653
  length : cardinal;
 
654
  n : cardinal;
671
655
  hash_head : IPos;
672
656
var
673
 
  MAX_DIST : uInt;  {macro}
 
657
  MAX_DIST : cardinal;  {macro}
674
658
begin
675
659
  length := dictLength;
676
660
  hash_head := 0;
696
680
  begin
697
681
    length := MAX_DIST;
698
682
{$ifndef USE_DICT_HEAD}
699
 
    Inc(dictionary, dictLength - length);  { use the tail of the dictionary }
 
683
    inc(dictionary, dictLength - length);  { use the tail of the dictionary }
700
684
{$endif}
701
685
  end;
702
686
 
703
 
  zmemcpy( pBytef(s^.window), dictionary, length);
 
687
  move(dictionary^,Pbyte(s^.window)^,length);
704
688
  s^.strstart := length;
705
 
  s^.block_start := long(length);
 
689
  s^.block_start := longint(length);
706
690
 
707
691
  { Insert all strings in the hash table (except for the last two bytes).
708
692
    s^.lookahead stays null, so s^.ins_h will be recomputed at the next
714
698
              and s^.hash_mask;
715
699
 
716
700
  for n := 0 to length - MIN_MATCH do
717
 
  begin
718
701
    INSERT_STRING(s^, n, hash_head);
719
 
  end;
720
702
  {if (hash_head <> 0) then
721
703
    hash_head := 0;  - to make compiler happy }
722
704
  deflateSetDictionary := Z_OK;
723
705
end;
724
706
 
725
707
{  ======================================================================== }
726
 
function deflateReset (var strm : z_stream) : int;
 
708
function deflateReset (var strm : z_stream) : integer;
727
709
var
728
710
  s : deflate_state_ptr;
729
711
begin
730
712
  if {(@strm = Z_NULL) or}
731
 
   (strm.state = Z_NULL)
732
 
   or (not Assigned(strm.zalloc)) or (not Assigned(strm.zfree)) then
 
713
   (strm.state = nil) then
733
714
  begin
734
715
    deflateReset := Z_STREAM_ERROR;
735
716
    exit;
742
723
 
743
724
  s := deflate_state_ptr(strm.state);
744
725
  s^.pending := 0;
745
 
  s^.pending_out := pBytef(s^.pending_buf);
 
726
  s^.pending_out := Pbyte(s^.pending_buf);
746
727
 
747
728
  if (s^.noheader < 0) then
748
729
  begin
763
744
 
764
745
{  ======================================================================== }
765
746
function deflateParams(var strm : z_stream;
766
 
                       level : int;
767
 
                       strategy : int) : int;
 
747
                       level : integer;
 
748
                       strategy : integer) : integer;
768
749
var
769
750
  s : deflate_state_ptr;
770
751
  func : compress_func;
771
 
  err : int;
 
752
  err : integer;
772
753
begin
773
754
  err := Z_OK;
774
755
  if {(@strm  =  Z_NULL) or} (strm.state  =  Z_NULL) then
815
796
  pending_buf. }
816
797
 
817
798
{local}
818
 
procedure putShortMSB (var s : deflate_state; b : uInt);
 
799
procedure putShortMSB (var s : deflate_state; b : cardinal);
819
800
begin
820
801
  s.pending_buf^[s.pending] := Byte(b shr 8);
821
 
  Inc(s.pending);
 
802
  inc(s.pending);
822
803
  s.pending_buf^[s.pending] := Byte(b and $ff);
823
 
  Inc(s.pending);
 
804
  inc(s.pending);
824
805
end;
825
806
 
826
807
{ =========================================================================
832
813
{local}
833
814
procedure flush_pending(var strm : z_stream);
834
815
var
835
 
  len : unsigned;
 
816
  len : cardinal;
836
817
  s : deflate_state_ptr;
837
818
begin
838
819
  s := deflate_state_ptr(strm.state);
843
824
  if (len = 0) then
844
825
    exit;
845
826
 
846
 
  zmemcpy(strm.next_out, s^.pending_out, len);
847
 
  Inc(strm.next_out, len);
848
 
  Inc(s^.pending_out, len);
849
 
  Inc(strm.total_out, len);
850
 
  Dec(strm.avail_out, len);
851
 
  Dec(s^.pending, len);
 
827
  move(s^.pending_out^,strm.next_out^,len);
 
828
  inc(strm.next_out, len);
 
829
  inc(s^.pending_out, len);
 
830
  inc(strm.total_out, len);
 
831
  dec(strm.avail_out, len);
 
832
  dec(s^.pending, len);
852
833
  if (s^.pending = 0) then
853
834
  begin
854
 
    s^.pending_out := pBytef(s^.pending_buf);
 
835
    s^.pending_out := Pbyte(s^.pending_buf);
855
836
  end;
856
837
end;
857
838
 
858
839
{ ========================================================================= }
859
 
function deflate (var strm : z_stream; flush : int) : int;
 
840
function deflate (var strm : z_stream; flush : integer) : integer;
860
841
var
861
 
  old_flush : int; { value of flush param for previous deflate call }
 
842
  old_flush : integer; { value of flush param for previous deflate call }
862
843
  s : deflate_state_ptr;
863
844
var
864
 
  header : uInt;
865
 
  level_flags : uInt;
 
845
  header : cardinal;
 
846
  level_flags : cardinal;
866
847
var
867
848
  bstate : block_state;
868
849
begin
907
888
    header := header or (level_flags shl 6);
908
889
    if (s^.strstart <> 0) then
909
890
      header := header or PRESET_DICT;
910
 
    Inc(header, 31 - (header mod 31));
 
891
    inc(header, 31 - (header mod 31));
911
892
 
912
893
    s^.status := BUSY_STATE;
913
894
    putShortMSB(s^, header);
915
896
    { Save the adler32 of the preset dictionary: }
916
897
    if (s^.strstart <> 0) then
917
898
    begin
918
 
      putShortMSB(s^, uInt(strm.adler shr 16));
919
 
      putShortMSB(s^, uInt(strm.adler and $ffff));
 
899
      putShortMSB(s^, cardinal(strm.adler shr 16));
 
900
      putShortMSB(s^, cardinal(strm.adler and $ffff));
920
901
    end;
921
 
    strm.adler := long(1);
 
902
    strm.adler := longint(1);
922
903
  end;
923
904
 
924
905
  { Flush as much pending output as possible }
928
909
    if (strm.avail_out = 0) then
929
910
    begin
930
911
      { Since avail_out is 0, deflate will be called again with
931
 
        more output space, but possibly with both pending and
932
 
        avail_in equal to zero. There won't be anything to do,
933
 
        but this is not an error situation so make sure we
934
 
        return OK instead of BUF_ERROR at next call of deflate: }
 
912
        more output space, but possibly with both pending and
 
913
        avail_in equal to zero. There won't be anything to do,
 
914
        but this is not an error situation so make sure we
 
915
        return OK instead of BUF_ERROR at next call of deflate: }
935
916
 
936
917
      s^.last_flush := -1;
937
918
      deflate := Z_OK;
979
960
      deflate := Z_OK;
980
961
      exit;
981
962
      { If flush != Z_NO_FLUSH && avail_out == 0, the next call
982
 
        of deflate should use the same flush parameter to make sure
983
 
        that the flush is complete. So we don't have to output an
984
 
        empty block here, this will be done at next call. This also
985
 
        ensures that for a very small output buffer, we emit at most
986
 
         one empty block. }
 
963
        of deflate should use the same flush parameter to make sure
 
964
        that the flush is complete. So we don't have to output an
 
965
        empty block here, this will be done at next call. This also
 
966
        ensures that for a very small output buffer, we emit at most
 
967
         one empty block. }
987
968
    end;
988
969
    if (bstate = block_done) then
989
970
    begin
991
972
        _tr_align(s^)
992
973
      else
993
974
      begin  { FULL_FLUSH or SYNC_FLUSH }
994
 
        _tr_stored_block(s^, pcharf(NIL), Long(0), FALSE);
 
975
        _tr_stored_block(s^, Pchar(NIL), longint(0), FALSE);
995
976
        { For a full flush, this empty block will be recognized
996
977
          as a special marker by inflate_sync(). }
997
978
 
999
980
        begin
1000
981
          {macro CLEAR_HASH(s);}             { forget history }
1001
982
          s^.head^[s^.hash_size-1] := ZNIL;
1002
 
          zmemzero(pBytef(s^.head), unsigned(s^.hash_size-1)*sizeof(s^.head^[0]));
 
983
          fillchar(Pbyte(s^.head)^,cardinal(s^.hash_size-1)*sizeof(s^.head^[0]),0);
1003
984
        end;
1004
985
      end;
1005
986
 
1007
988
      if (strm.avail_out = 0) then
1008
989
      begin
1009
990
        s^.last_flush := -1; { avoid BUF_ERROR at next call, see above }
1010
 
        deflate := Z_OK;
 
991
        deflate := Z_OK;
1011
992
        exit;
1012
993
      end;
1013
994
 
1014
995
    end;
1015
996
  end;
1016
 
  {$IFDEF DEBUG}
 
997
  {$IFDEF ZLIB_DEBUG}
1017
998
  Assert(strm.avail_out > 0, 'bug2');
1018
999
  {$ENDIF}
1019
1000
  if (flush <> Z_FINISH) then
1029
1010
  end;
1030
1011
 
1031
1012
  { Write the zlib trailer (adler32) }
1032
 
  putShortMSB(s^, uInt(strm.adler shr 16));
1033
 
  putShortMSB(s^, uInt(strm.adler and $ffff));
 
1013
  putShortMSB(s^, cardinal(strm.adler shr 16));
 
1014
  putShortMSB(s^, cardinal(strm.adler and $ffff));
1034
1015
  flush_pending(strm);
1035
1016
  { If avail_out is zero, the application will call deflate again
1036
1017
    to flush the rest. }
1043
1024
end;
1044
1025
 
1045
1026
{ ========================================================================= }
1046
 
function deflateEnd (var strm : z_stream) : int;
 
1027
function deflateEnd (var strm : z_stream) : integer;
1047
1028
var
1048
 
  status : int;
 
1029
  status : integer;
1049
1030
  s : deflate_state_ptr;
1050
1031
begin
1051
1032
  if {(@strm = Z_NULL) or} (strm.state = Z_NULL) then
1085
1066
 
1086
1067
 
1087
1068
{ ========================================================================= }
1088
 
function deflateCopy (dest, source : z_streamp) : int;
 
1069
function deflateCopy (dest, source : z_streamp) : integer;
1089
1070
{$ifndef MAXSEG_64K}
1090
1071
var
1091
1072
  ds : deflate_state_ptr;
1092
1073
  ss : deflate_state_ptr;
1093
 
  overlay : pushfArray;
 
1074
  overlay : Pwordarray;
1094
1075
{$endif}
1095
1076
begin
1096
1077
{$ifdef MAXSEG_64K}
1116
1097
  ds^ := ss^;
1117
1098
  ds^.strm := dest;
1118
1099
 
1119
 
  ds^.window := pzByteArray ( ZALLOC(dest^, ds^.w_size, 2*sizeof(Byte)) );
 
1100
  ds^.window := Pbytearray ( ZALLOC(dest^, ds^.w_size, 2*sizeof(Byte)) );
1120
1101
  ds^.prev   := pzPosfArray ( ZALLOC(dest^, ds^.w_size, sizeof(Pos)) );
1121
1102
  ds^.head   := pzPosfArray ( ZALLOC(dest^, ds^.hash_size, sizeof(Pos)) );
1122
 
  overlay := pushfArray ( ZALLOC(dest^, ds^.lit_bufsize, sizeof(ush)+2) );
1123
 
  ds^.pending_buf := pzByteArray ( overlay );
 
1103
  overlay := Pwordarray ( ZALLOC(dest^, ds^.lit_bufsize, sizeof(word)+2) );
 
1104
  ds^.pending_buf := Pbytearray ( overlay );
1124
1105
 
1125
1106
  if (ds^.window = Z_NULL) or (ds^.prev = Z_NULL) or (ds^.head = Z_NULL)
1126
1107
     or (ds^.pending_buf = Z_NULL) then
1129
1110
    deflateCopy := Z_MEM_ERROR;
1130
1111
    exit;
1131
1112
  end;
1132
 
  { following zmemcpy do not work for 16-bit MSDOS }
1133
 
  zmemcpy(pBytef(ds^.window), pBytef(ss^.window), ds^.w_size * 2 * sizeof(Byte));
1134
 
  zmemcpy(pBytef(ds^.prev), pBytef(ss^.prev), ds^.w_size * sizeof(Pos));
1135
 
  zmemcpy(pBytef(ds^.head), pBytef(ss^.head), ds^.hash_size * sizeof(Pos));
1136
 
  zmemcpy(pBytef(ds^.pending_buf), pBytef(ss^.pending_buf), uInt(ds^.pending_buf_size));
1137
 
 
1138
 
  ds^.pending_out := @ds^.pending_buf^[ptr2int(ss^.pending_out) - ptr2int(ss^.pending_buf)];
1139
 
  ds^.d_buf := pushfArray (@overlay^[ds^.lit_bufsize div sizeof(ush)] );
1140
 
  ds^.l_buf := puchfArray (@ds^.pending_buf^[(1+sizeof(ush))*ds^.lit_bufsize]);
 
1113
 
 
1114
  move(Pbyte(ss^.window)^,Pbyte(ds^.window)^,ds^.w_size * 2 * sizeof(byte));
 
1115
  move(Pbyte(ss^.prev)^,Pbyte(ds^.prev)^,ds^.w_size * sizeof(pos));
 
1116
  move(Pbyte(ss^.head)^,Pbyte(ds^.head)^,ds^.hash_size * sizeof(pos));
 
1117
  move(Pbyte(ss^.pending_buf)^,Pbyte(ds^.pending_buf)^,cardinal(ds^.pending_buf_size));
 
1118
 
 
1119
  ds^.pending_out := @ds^.pending_buf^[ptrint(ss^.pending_out) - ptrint(ss^.pending_buf)];
 
1120
  ds^.d_buf := Pwordarray(@overlay^[ds^.lit_bufsize div sizeof(word)] );
 
1121
  ds^.l_buf := Pbytearray(@ds^.pending_buf^[(1+sizeof(word))*ds^.lit_bufsize]);
1141
1122
 
1142
1123
  ds^.l_desc.dyn_tree := tree_ptr(@ds^.dyn_ltree);
1143
1124
  ds^.d_desc.dyn_tree := tree_ptr(@ds^.dyn_dtree);
1156
1137
  (See also flush_pending()). }
1157
1138
 
1158
1139
{local}
1159
 
function read_buf(strm : z_streamp; buf : pBytef; size : unsigned) : int;
1160
 
var
1161
 
  len : unsigned;
 
1140
function read_buf(strm:z_streamp;buf:Pbyte;size:cardinal):cardinal;
 
1141
 
 
1142
var len:cardinal;
 
1143
 
1162
1144
begin
1163
 
  len := strm^.avail_in;
1164
 
 
1165
 
  if (len > size) then
1166
 
    len := size;
1167
 
  if (len = 0) then
1168
 
  begin
1169
 
    read_buf := 0;
1170
 
    exit;
1171
 
  end;
1172
 
 
1173
 
  Dec(strm^.avail_in, len);
1174
 
 
1175
 
  if deflate_state_ptr(strm^.state)^.noheader = 0 then
1176
 
  begin
1177
 
    strm^.adler := adler32(strm^.adler, strm^.next_in, len);
1178
 
  end;
1179
 
  zmemcpy(buf, strm^.next_in, len);
1180
 
  Inc(strm^.next_in, len);
1181
 
  Inc(strm^.total_in, len);
1182
 
 
1183
 
  read_buf := int(len);
 
1145
  len:=strm^.avail_in;
 
1146
  if len>size then
 
1147
    len:=size;
 
1148
  dec(strm^.avail_in, len);
 
1149
 
 
1150
  if len<>0 then
 
1151
    begin
 
1152
      if deflate_state_ptr(strm^.state)^.noheader=0 then
 
1153
        strm^.adler:=adler32(strm^.adler,strm^.next_in,len);
 
1154
      move(strm^.next_in^,buf^,len);
 
1155
      inc(strm^.next_in,len);
 
1156
      inc(strm^.total_in,len);
 
1157
    end;
 
1158
  read_buf:=len;
1184
1159
end;
1185
1160
 
1186
1161
{ ===========================================================================
1189
1164
{local}
1190
1165
procedure lm_init (var s : deflate_state);
1191
1166
begin
1192
 
  s.window_size := ulg( uLong(2)*s.w_size);
 
1167
  s.window_size := longint( 2*s.w_size);
1193
1168
 
1194
1169
  {macro CLEAR_HASH(s);}
1195
1170
  s.head^[s.hash_size-1] := ZNIL;
1196
 
  zmemzero(pBytef(s.head), unsigned(s.hash_size-1)*sizeof(s.head^[0]));
 
1171
  fillchar(Pbyte(s.head)^, cardinal(s.hash_size-1)*sizeof(s.head^[0]),0);
1197
1172
 
1198
1173
  { Set the default configuration parameters: }
1199
1174
 
1203
1178
  s.max_chain_length := configuration_table[s.level].max_chain;
1204
1179
 
1205
1180
  s.strstart := 0;
1206
 
  s.block_start := long(0);
 
1181
  s.block_start := longint(0);
1207
1182
  s.lookahead := 0;
1208
1183
  s.prev_length := MIN_MATCH-1;
1209
1184
  s.match_length := MIN_MATCH-1;
1233
1208
{local}
1234
1209
function longest_match(var s : deflate_state;
1235
1210
                       cur_match : IPos  { current match }
1236
 
                       ) : uInt;
 
1211
                       ) : cardinal;
1237
1212
label
1238
1213
  nextstep;
1239
1214
var
1240
 
  chain_length : unsigned;    { max hash chain length }
1241
 
  {register} scan : pBytef;   { current string }
1242
 
  {register} match : pBytef;  { matched string }
1243
 
  {register} len : int;       { length of current match }
1244
 
  best_len : int;             { best match length so far }
1245
 
  nice_match : int;           { stop if match long enough }
 
1215
  chain_length : cardinal;    { max hash chain length }
 
1216
  {register} scan : Pbyte;   { current string }
 
1217
  {register} match : Pbyte;  { matched string }
 
1218
  {register} len : integer;       { length of current match }
 
1219
  best_len : integer;             { best match length so far }
 
1220
  nice_match : integer;           { stop if match longint enough }
1246
1221
  limit : IPos;
1247
1222
 
1248
1223
  prev : pzPosfArray;
1249
 
  wmask : uInt;
 
1224
  wmask : cardinal;
1250
1225
{$ifdef UNALIGNED_OK}
1251
 
  {register} strend : pBytef;
1252
 
  {register} scan_start : ush;
1253
 
  {register} scan_end : ush;
 
1226
  {register} strend : Pbyte;
 
1227
  {register} scan_start : word;
 
1228
  {register} scan_end : word;
1254
1229
{$else}
1255
 
  {register} strend : pBytef;
 
1230
  {register} strend : Pbyte;
1256
1231
  {register} scan_end1 : Byte;
1257
1232
  {register} scan_end : Byte;
1258
1233
{$endif}
1259
1234
var
1260
 
  MAX_DIST : uInt;
 
1235
  MAX_DIST : cardinal;
1261
1236
begin
1262
1237
  chain_length := s.max_chain_length; { max hash chain length }
1263
1238
  scan := @(s.window^[s.strstart]);
1264
1239
  best_len := s.prev_length;              { best match length so far }
1265
 
  nice_match := s.nice_match;             { stop if match long enough }
 
1240
  nice_match := s.nice_match;             { stop if match longint enough }
1266
1241
 
1267
1242
 
1268
1243
  MAX_DIST := s.w_size - MIN_LOOKAHEAD;
1283
1258
  { Compare two bytes at a time. Note: this is not always beneficial.
1284
1259
    Try with and without -DUNALIGNED_OK to check. }
1285
1260
 
1286
 
  strend := pBytef(@(s.window^[s.strstart + MAX_MATCH - 1]));
 
1261
  strend := Pbyte(@(s.window^[s.strstart + MAX_MATCH - 1]));
1287
1262
  scan_start := pushf(scan)^;
1288
 
  scan_end   := pushfArray(scan)^[best_len-1];   { fix }
 
1263
  scan_end   := Pwordarray(scan)^[best_len-1];   { fix }
1289
1264
{$else}
1290
 
  strend := pBytef(@(s.window^[s.strstart + MAX_MATCH]));
 
1265
  strend := Pbyte(@(s.window^[s.strstart + MAX_MATCH]));
1291
1266
  {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
1292
 
  scan_end1  := pzByteArray(scan)^[best_len-1];
 
1267
  scan_end1  := Pbytearray(scan)^[best_len-1];
1293
1268
  {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
1294
 
  scan_end   := pzByteArray(scan)^[best_len];
 
1269
  scan_end   := Pbytearray(scan)^[best_len];
1295
1270
{$endif}
1296
1271
 
1297
1272
    { The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1298
1273
      It is easy to get rid of this optimization if necessary. }
1299
 
    {$IFDEF DEBUG}
 
1274
    {$IFDEF ZLIB_DEBUG}
1300
1275
    Assert((s.hash_bits >= 8) and (MAX_MATCH = 258), 'Code too clever');
1301
1276
    {$ENDIF}
1302
1277
    { Do not waste too much time if we already have a good match: }
1308
1283
    { Do not look for matches beyond the end of the input. This is necessary
1309
1284
      to make deflate deterministic. }
1310
1285
 
1311
 
    if (uInt(nice_match) > s.lookahead) then
 
1286
    if (cardinal(nice_match) > s.lookahead) then
1312
1287
      nice_match := s.lookahead;
1313
 
    {$IFDEF DEBUG}
1314
 
    Assert(ulg(s.strstart) <= s.window_size-MIN_LOOKAHEAD, 'need lookahead');
 
1288
    {$IFDEF ZLIB_DEBUG}
 
1289
    Assert(longint(s.strstart) <= s.window_size-MIN_LOOKAHEAD, 'need lookahead');
1315
1290
    {$ENDIF}
1316
1291
    repeat
1317
 
        {$IFDEF DEBUG}
 
1292
        {$IFDEF ZLIB_DEBUG}
1318
1293
        Assert(cur_match < s.strstart, 'no future');
1319
1294
        {$ENDIF}
1320
1295
        match := @(s.window^[cur_match]);
1330
1305
{$endif}
1331
1306
 
1332
1307
{$ifdef DO_UNALIGNED_OK}
1333
 
        { This code assumes sizeof(unsigned short) = 2. Do not use
 
1308
        { This code assumes sizeof(cardinal short) = 2. Do not use
1334
1309
          UNALIGNED_OK if your compiler uses a different size. }
1335
1310
  {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
1336
1311
        if (pushfArray(match)^[best_len-1] <> scan_end) or
1346
1321
          at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1347
1322
          necessary to put more guard bytes at the end of the window, or
1348
1323
          to check more often for insufficient lookahead. }
1349
 
        {$IFDEF DEBUG}
 
1324
        {$IFDEF ZLIB_DEBUG}
1350
1325
        Assert(pzByteArray(scan)^[2] = pzByteArray(match)^[2], 'scan[2]?');
1351
1326
        {$ENDIF}
1352
 
        Inc(scan);
1353
 
        Inc(match);
 
1327
        inc(scan);
 
1328
        inc(match);
1354
1329
 
1355
1330
        repeat
1356
 
          Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
1357
 
          Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
1358
 
          Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
1359
 
          Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
1360
 
        until (ptr2int(scan) >= ptr2int(strend));
 
1331
          inc(scan,2); inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
 
1332
          inc(scan,2); inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
 
1333
          inc(scan,2); inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
 
1334
          inc(scan,2); inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
 
1335
        until (ptrint(scan) >= ptrint(strend));
1361
1336
        { The funny "do while" generates better code on most compilers }
1362
1337
 
1363
1338
        { Here, scan <= window+strstart+257 }
1364
 
        {$IFDEF DEBUG}
1365
 
        Assert(ptr2int(scan) <=
1366
 
               ptr2int(@(s.window^[unsigned(s.window_size-1)])),
 
1339
        {$IFDEF ZLIB_DEBUG}
 
1340
        {$ifopt R+} {$define RangeCheck} {$endif} {$R-}
 
1341
        Assert(ptrint(scan) <=
 
1342
               ptrint(@(s.window^[cardinal(s.window_size-1)])),
1367
1343
               'wild scan');
 
1344
        {$ifdef RangeCheck} {$R+} {$undef RangeCheck} {$endif}
1368
1345
        {$ENDIF}
1369
1346
        if (scan^ = match^) then
1370
 
          Inc(scan);
 
1347
          inc(scan);
1371
1348
 
1372
 
        len := (MAX_MATCH - 1) - int(ptr2int(strend)-ptr2int(scan));
 
1349
        len := (MAX_MATCH - 1) - integer(ptrint(strend)) + integer(ptrint(scan));
1373
1350
        scan := strend;
1374
 
        Dec(scan, (MAX_MATCH-1));
 
1351
        dec(scan, (MAX_MATCH-1));
1375
1352
 
1376
1353
{$else} { UNALIGNED_OK }
1377
1354
 
1378
1355
  {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
1379
 
        if (pzByteArray(match)^[best_len]   <> scan_end) or
1380
 
           (pzByteArray(match)^[best_len-1] <> scan_end1) or
 
1356
        if (Pbytearray(match)^[best_len]   <> scan_end) or
 
1357
           (Pbytearray(match)^[best_len-1] <> scan_end1) or
1381
1358
           (match^ <> scan^) then
1382
1359
          goto nextstep; {continue;}
1383
1360
  {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
1384
 
        Inc(match);
1385
 
        if (match^ <> pzByteArray(scan)^[1]) then
 
1361
        inc(match);
 
1362
        if (match^ <> Pbytearray(scan)^[1]) then
1386
1363
          goto nextstep; {continue;}
1387
1364
 
1388
1365
        { The check at best_len-1 can be removed because it will be made
1391
1368
          are always equal when the other bytes match, given that
1392
1369
          the hash keys are equal and that HASH_BITS >= 8. }
1393
1370
 
1394
 
        Inc(scan, 2);
1395
 
        Inc(match);
1396
 
        {$IFDEF DEBUG}
 
1371
        inc(scan, 2);
 
1372
        inc(match);
 
1373
        {$IFDEF ZLIB_DEBUG}
1397
1374
        Assert( scan^ = match^, 'match[2]?');
1398
1375
        {$ENDIF}
1399
1376
        { We check for insufficient lookahead only every 8th comparison;
1400
1377
          the 256th check will be made at strstart+258. }
1401
1378
 
1402
1379
        repeat
1403
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1404
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1405
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1406
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1407
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1408
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1409
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1410
 
          Inc(scan); Inc(match); if (scan^ <> match^) then break;
1411
 
        until (ptr2int(scan) >= ptr2int(strend));
 
1380
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1381
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1382
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1383
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1384
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1385
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1386
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1387
          inc(scan); inc(match); if (scan^ <> match^) then break;
 
1388
        until (ptrint(scan) >= ptrint(strend));
1412
1389
 
1413
 
        {$IFDEF DEBUG}
1414
 
        Assert(ptr2int(scan) <=
1415
 
               ptr2int(@(s.window^[unsigned(s.window_size-1)])),
 
1390
        {$IFDEF ZLIB_DEBUG}
 
1391
        Assert(ptrint(scan) <=
 
1392
               ptrint(@(s.window^[cardinal(s.window_size-1)])),
1416
1393
               'wild scan');
1417
1394
        {$ENDIF}
1418
1395
 
1419
 
        len := MAX_MATCH - int(ptr2int(strend) - ptr2int(scan));
 
1396
        len := MAX_MATCH - integer(ptrint(strend) - ptrint(scan));
1420
1397
        scan := strend;
1421
 
        Dec(scan, MAX_MATCH);
 
1398
        dec(scan, MAX_MATCH);
1422
1399
 
1423
1400
{$endif} { UNALIGNED_OK }
1424
1401
 
1430
1407
              break;
1431
1408
  {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
1432
1409
{$ifdef UNALIGNED_OK}
1433
 
            scan_end   := pzByteArray(scan)^[best_len-1];
 
1410
            scan_end   := Pbytearray(scan)^[best_len-1];
1434
1411
{$else}
1435
 
            scan_end1  := pzByteArray(scan)^[best_len-1];
1436
 
            scan_end   := pzByteArray(scan)^[best_len];
 
1412
            scan_end1  := Pbytearray(scan)^[best_len-1];
 
1413
            scan_end   := Pbytearray(scan)^[best_len];
1437
1414
{$endif}
1438
1415
  {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
1439
1416
        end;
1440
1417
    nextstep:
1441
1418
      cur_match := prev^[cur_match and wmask];
1442
 
      Dec(chain_length);
 
1419
      dec(chain_length);
1443
1420
    until (cur_match <= limit) or (chain_length = 0);
1444
1421
 
1445
 
    if (uInt(best_len) <= s.lookahead) then
1446
 
      longest_match := uInt(best_len)
 
1422
    if (cardinal(best_len) <= s.lookahead) then
 
1423
      longest_match := cardinal(best_len)
1447
1424
    else
1448
1425
      longest_match := s.lookahead;
1449
1426
end;
1456
1433
{local}
1457
1434
function longest_match(var s : deflate_state;
1458
1435
                       cur_match : IPos  { current match }
1459
 
                       ) : uInt;
 
1436
                       ) : cardinal;
1460
1437
var
1461
 
  {register} scan : pBytef;   { current string }
1462
 
  {register} match : pBytef;  { matched string }
1463
 
  {register} len : int;       { length of current match }
1464
 
  {register} strend : pBytef;
 
1438
  {register} scan : Pbyte;   { current string }
 
1439
  {register} match : Pbyte;  { matched string }
 
1440
  {register} len : integer;       { length of current match }
 
1441
  {register} strend : Pbyte;
1465
1442
begin
1466
1443
  scan := @s.window^[s.strstart];
1467
1444
  strend := @s.window^[s.strstart + MAX_MATCH];
1469
1446
 
1470
1447
    { The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1471
1448
      It is easy to get rid of this optimization if necessary. }
1472
 
    {$IFDEF DEBUG}
 
1449
    {$IFDEF ZLIB_DEBUG}
1473
1450
    Assert((s.hash_bits >= 8) and (MAX_MATCH = 258), 'Code too clever');
1474
1451
 
1475
 
    Assert(ulg(s.strstart) <= s.window_size-MIN_LOOKAHEAD, 'need lookahead');
 
1452
    Assert(longint(s.strstart) <= s.window_size-MIN_LOOKAHEAD, 'need lookahead');
1476
1453
 
1477
1454
    Assert(cur_match < s.strstart, 'no future');
1478
1455
    {$ENDIF}
1499
1476
      the 256th check will be made at strstart+258. }
1500
1477
 
1501
1478
    repeat
1502
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1503
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1504
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1505
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1506
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1507
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1508
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1509
 
      Inc(scan); Inc(match); if scan^<>match^ then break;
1510
 
    until (ptr2int(scan) >= ptr2int(strend));
1511
 
 
1512
 
    Assert(scan <= s.window+unsigned(s.window_size-1), 'wild scan');
1513
 
 
1514
 
    len := MAX_MATCH - int(strend - scan);
 
1479
      inc(scan); inc(match); if scan^<>match^ then break;
 
1480
      inc(scan); inc(match); if scan^<>match^ then break;
 
1481
      inc(scan); inc(match); if scan^<>match^ then break;
 
1482
      inc(scan); inc(match); if scan^<>match^ then break;
 
1483
      inc(scan); inc(match); if scan^<>match^ then break;
 
1484
      inc(scan); inc(match); if scan^<>match^ then break;
 
1485
      inc(scan); inc(match); if scan^<>match^ then break;
 
1486
      inc(scan); inc(match); if scan^<>match^ then break;
 
1487
    until (ptrint(scan) >= ptrint(strend));
 
1488
 
 
1489
    Assert(scan <= s.window+cardinal(s.window_size-1), 'wild scan');
 
1490
 
 
1491
    len := MAX_MATCH - integer(strend - scan);
1515
1492
 
1516
1493
    if (len < MIN_MATCH) then
1517
1494
    begin
1527
1504
end;
1528
1505
{$endif} { FASTEST }
1529
1506
 
1530
 
{$ifdef DEBUG}
 
1507
{$ifdef ZLIB_DEBUG}
1531
1508
{ ===========================================================================
1532
1509
  Check that the match at match_start is indeed a match. }
1533
1510
 
1534
1511
{local}
1535
1512
procedure check_match(var s : deflate_state;
1536
1513
                      start, match : IPos;
1537
 
                      length : int);
 
1514
                      length : integer);
1538
1515
begin
1539
1516
  exit;
1540
1517
  { check that the match is indeed a match }
1541
 
  if (zmemcmp(pBytef(@s.window^[match]),
1542
 
              pBytef(@s.window^[start]), length) <> EQUAL) then
 
1518
  if (zmemcmp(Pbyte(@s.window^[match]),
 
1519
              Pbyte(@s.window^[start]), length) <> EQUAL) then
1543
1520
  begin
1544
1521
    WriteLn(' start ',start,', match ',match ,' length ', length);
1545
1522
    repeat
1546
1523
      Write(char(s.window^[match]), char(s.window^[start]));
1547
 
      Inc(match);
1548
 
      Inc(start);
1549
 
      Dec(length);
 
1524
      inc(match);
 
1525
      inc(start);
 
1526
      dec(length);
1550
1527
    Until (length = 0);
1551
1528
    z_error('invalid match');
1552
1529
  end;
1555
1532
    Write('\\[',start-match,',',length,']');
1556
1533
    repeat
1557
1534
       Write(char(s.window^[start]));
1558
 
       Inc(start);
1559
 
       Dec(length);
 
1535
       inc(start);
 
1536
       dec(length);
1560
1537
    Until (length = 0);
1561
1538
  end;
1562
1539
end;
1575
1552
{local}
1576
1553
procedure fill_window(var s : deflate_state);
1577
1554
var
1578
 
  {register} n, m : unsigned;
 
1555
  {register} n, m : cardinal;
1579
1556
  {register} p : pPosf;
1580
 
  more : unsigned;    { Amount of free space at the end of the window. }
1581
 
  wsize : uInt;
 
1557
  more : cardinal;    { Amount of free space at the end of the window. }
 
1558
  wsize : cardinal;
1582
1559
begin
1583
1560
   wsize := s.w_size;
1584
1561
   repeat
1585
 
     more := unsigned(s.window_size -ulg(s.lookahead) -ulg(s.strstart));
 
1562
     more := cardinal(s.window_size -longint(s.lookahead) -longint(s.strstart));
1586
1563
 
1587
1564
     { Deal with !@#$% 64K limit: }
1588
1565
     if (more = 0) and (s.strstart = 0) and (s.lookahead = 0) then
1589
1566
       more := wsize
1590
1567
     else
1591
 
     if (more = unsigned(-1)) then
 
1568
     if (more = cardinal(-1)) then
1592
1569
     begin
1593
1570
       { Very unlikely, but possible on 16 bit machine if strstart = 0
1594
1571
         and lookahead = 1 (input done one byte at time) }
1595
 
       Dec(more);
 
1572
       dec(more);
1596
1573
 
1597
1574
       { If the window is almost full and there is insufficient lookahead,
1598
1575
         move the upper half to the lower one to make room in the upper half.}
1599
1576
     end
1600
1577
     else
1601
 
       if (s.strstart >= wsize+ {MAX_DIST}wsize-MIN_LOOKAHEAD) then
 
1578
       if (s.strstart >= wsize+ {MAX_DIST}(wsize-MIN_LOOKAHEAD)) then
1602
1579
       begin
1603
 
         zmemcpy( pBytef(s.window), pBytef(@(s.window^[wsize])),
1604
 
                 unsigned(wsize));
1605
 
         Dec(s.match_start, wsize);
1606
 
         Dec(s.strstart, wsize); { we now have strstart >= MAX_DIST }
1607
 
         Dec(s.block_start, long(wsize));
 
1580
         move(s.window^[wsize],Pbyte(s.window)^,wsize);
 
1581
         dec(s.match_start, wsize);
 
1582
         dec(s.strstart, wsize); { we now have strstart >= MAX_DIST }
 
1583
         dec(s.block_start, longint(wsize));
1608
1584
 
1609
1585
         { Slide the hash table (could be avoided with 32 bit values
1610
1586
           at the expense of memory usage). We slide even when level = 0
1615
1591
         n := s.hash_size;
1616
1592
         p := @s.head^[n];
1617
1593
         repeat
1618
 
           Dec(p);
 
1594
           dec(p);
1619
1595
           m := p^;
1620
1596
           if (m >= wsize) then
1621
1597
             p^ := Pos(m-wsize)
1622
1598
           else
1623
1599
             p^ := Pos(ZNIL);
1624
 
           Dec(n);
 
1600
           dec(n);
1625
1601
         Until (n=0);
1626
1602
 
1627
1603
         n := wsize;
1628
1604
{$ifndef FASTEST}
1629
1605
         p := @s.prev^[n];
1630
1606
         repeat
1631
 
           Dec(p);
 
1607
           dec(p);
1632
1608
           m := p^;
1633
1609
           if (m >= wsize) then
1634
1610
             p^ := Pos(m-wsize)
1636
1612
             p^:= Pos(ZNIL);
1637
1613
             { If n is not on any hash chain, prev^[n] is garbage but
1638
1614
               its value will never be used. }
1639
 
           Dec(n);
 
1615
           dec(n);
1640
1616
         Until (n=0);
1641
1617
{$endif}
1642
 
         Inc(more, wsize);
 
1618
         inc(more, wsize);
1643
1619
     end;
1644
1620
     if (s.strm^.avail_in = 0) then
1645
1621
       exit;
1655
1631
      * Otherwise, window_size == 2*WSIZE so more >= 2.
1656
1632
      * If there was sliding, more >= WSIZE. So in all cases, more >= 2. }
1657
1633
 
1658
 
     {$IFDEF DEBUG}
 
1634
     {$IFDEF ZLIB_DEBUG}
1659
1635
     Assert(more >= 2, 'more < 2');
1660
1636
     {$ENDIF}
1661
1637
 
1662
 
     n := read_buf(s.strm, pBytef(@(s.window^[s.strstart + s.lookahead])),
 
1638
     n := read_buf(s.strm, Pbyte(@(s.window^[s.strstart + s.lookahead])),
1663
1639
                  more);
1664
 
     Inc(s.lookahead, n);
 
1640
     inc(s.lookahead, n);
1665
1641
 
1666
1642
     { Initialize the hash value now that we have some input: }
1667
1643
     if (s.lookahead >= MIN_MATCH) then
1686
1662
 
1687
1663
procedure FLUSH_BLOCK_ONLY(var s : deflate_state; eof : boolean); {macro}
1688
1664
begin
1689
 
  if (s.block_start >= Long(0)) then
1690
 
    _tr_flush_block(s, pcharf(@s.window^[unsigned(s.block_start)]),
1691
 
                    ulg(long(s.strstart) - s.block_start), eof)
 
1665
  if (s.block_start >= 0) then
 
1666
    _tr_flush_block(s, Pchar(@s.window^[cardinal(s.block_start)]),
 
1667
                    longint(longint(s.strstart) - s.block_start), eof)
1692
1668
  else
1693
 
    _tr_flush_block(s, pcharf(Z_NULL),
1694
 
                    ulg(long(s.strstart) - s.block_start), eof);
 
1669
    _tr_flush_block(s, nil,
 
1670
                    longint(longint(s.strstart) - s.block_start), eof);
1695
1671
 
1696
1672
  s.block_start := s.strstart;
1697
1673
  flush_pending(s.strm^);
1698
 
  {$IFDEF DEBUG}
 
1674
  {$IFDEF ZLIB_DEBUG}
1699
1675
  Tracev('[FLUSH]');
1700
1676
  {$ENDIF}
1701
1677
end;
1728
1704
 
1729
1705
 
1730
1706
{local}
1731
 
function deflate_stored(var s : deflate_state; flush : int) : block_state;
 
1707
function deflate_stored(var s : deflate_state; flush : integer) : block_state;
1732
1708
{ Stored blocks are limited to 0xffff bytes, pending_buf is limited
1733
1709
  to pending_buf_size, and each stored block has a 5 byte header: }
1734
1710
var
1735
 
  max_block_size : ulg;
1736
 
  max_start : ulg;
 
1711
  max_block_size : longint;
 
1712
  max_start : longint;
1737
1713
begin
1738
1714
  max_block_size := $ffff;
1739
1715
  if (max_block_size > s.pending_buf_size - 5) then
1745
1721
    { Fill the window as much as possible: }
1746
1722
    if (s.lookahead <= 1) then
1747
1723
    begin
1748
 
      {$IFDEF DEBUG}
 
1724
      {$IFDEF ZLIB_DEBUG}
1749
1725
      Assert( (s.strstart < s.w_size + {MAX_DIST}s.w_size-MIN_LOOKAHEAD) or
1750
 
              (s.block_start >= long(s.w_size)), 'slide too late');
 
1726
              (s.block_start >= longint(s.w_size)), 'slide too late');
1751
1727
      {$ENDIF}
1752
1728
      fill_window(s);
1753
1729
      if (s.lookahead = 0) and (flush = Z_NO_FLUSH) then
1759
1735
      if (s.lookahead = 0) then
1760
1736
        break; { flush the current block }
1761
1737
    end;
1762
 
    {$IFDEF DEBUG}
1763
 
    Assert(s.block_start >= long(0), 'block gone');
 
1738
    {$IFDEF ZLIB_DEBUG}
 
1739
    Assert(s.block_start >= 0, 'block gone');
1764
1740
    {$ENDIF}
1765
 
    Inc(s.strstart, s.lookahead);
 
1741
    inc(s.strstart, s.lookahead);
1766
1742
    s.lookahead := 0;
1767
1743
 
1768
1744
    { Emit a stored block if pending_buf will be full: }
1769
1745
    max_start := s.block_start + max_block_size;
1770
 
    if (s.strstart = 0) or (ulg(s.strstart) >= max_start) then
 
1746
    if (s.strstart = 0) or (longint(s.strstart) >= max_start) then
1771
1747
    begin
1772
1748
      { strstart = 0 is possible when wraparound on 16-bit machine }
1773
 
      s.lookahead := uInt(s.strstart - max_start);
1774
 
      s.strstart := uInt(max_start);
 
1749
      s.lookahead := cardinal(s.strstart) - cardinal(max_start);
 
1750
      s.strstart := cardinal(max_start);
1775
1751
      {FLUSH_BLOCK(s, FALSE);}
1776
1752
      FLUSH_BLOCK_ONLY(s, FALSE);
1777
1753
      if (s.strm^.avail_out = 0) then
1784
1760
    { Flush if we may have to slide, otherwise block_start may become
1785
1761
      negative and the data will be gone: }
1786
1762
 
1787
 
    if (s.strstart - uInt(s.block_start) >= {MAX_DIST}
 
1763
    if (s.strstart - cardinal(s.block_start) >= {MAX_DIST}
1788
1764
        s.w_size-MIN_LOOKAHEAD) then
1789
1765
    begin
1790
1766
      {FLUSH_BLOCK(s, FALSE);}
1822
1798
  matches. It is used only for the fast compression options. }
1823
1799
 
1824
1800
{local}
1825
 
function deflate_fast(var s : deflate_state; flush : int) : block_state;
 
1801
function deflate_fast(var s : deflate_state; flush : integer) : block_state;
1826
1802
var
1827
1803
  hash_head : IPos;     { head of the hash chain }
1828
1804
  bflush : boolean;     { set if current block must be flushed }
1853
1829
      dictionary, and set hash_head to the head of the hash chain: }
1854
1830
 
1855
1831
    if (s.lookahead >= MIN_MATCH) then
1856
 
    begin
1857
1832
      INSERT_STRING(s, s.strstart, hash_head);
1858
 
    end;
1859
1833
 
1860
1834
    { Find the longest match, discarding those <= prev_length.
1861
1835
      At this point we have always match_length < MIN_MATCH }
1873
1847
    end;
1874
1848
    if (s.match_length >= MIN_MATCH) then
1875
1849
    begin
1876
 
      {$IFDEF DEBUG}
 
1850
      {$IFDEF ZLIB_DEBUG}
1877
1851
      check_match(s, s.strstart, s.match_start, s.match_length);
1878
1852
      {$ENDIF}
1879
1853
 
1882
1856
      bflush := _tr_tally(s, s.strstart - s.match_start,
1883
1857
                        s.match_length - MIN_MATCH);
1884
1858
 
1885
 
      Dec(s.lookahead, s.match_length);
 
1859
      dec(s.lookahead, s.match_length);
1886
1860
 
1887
1861
      { Insert new strings in the hash table only if the match length
1888
1862
        is not too large. This saves time but degrades compression. }
1891
1865
      if (s.match_length <= s.max_insert_length)
1892
1866
       and (s.lookahead >= MIN_MATCH) then
1893
1867
      begin
1894
 
        Dec(s.match_length); { string at strstart already in hash table }
 
1868
        dec(s.match_length); { string at strstart already in hash table }
1895
1869
        repeat
1896
 
          Inc(s.strstart);
 
1870
          inc(s.strstart);
1897
1871
          INSERT_STRING(s, s.strstart, hash_head);
1898
1872
          { strstart never exceeds WSIZE-MAX_MATCH, so there are
1899
1873
            always MIN_MATCH bytes ahead. }
1900
 
          Dec(s.match_length);
 
1874
          dec(s.match_length);
1901
1875
        until (s.match_length = 0);
1902
 
        Inc(s.strstart);
 
1876
        inc(s.strstart);
1903
1877
      end
1904
1878
      else
1905
1879
{$endif}
1906
1880
 
1907
1881
      begin
1908
 
        Inc(s.strstart, s.match_length);
 
1882
        inc(s.strstart, s.match_length);
1909
1883
        s.match_length := 0;
1910
1884
        s.ins_h := s.window^[s.strstart];
1911
1885
        {UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]);}
1924
1898
    else
1925
1899
    begin
1926
1900
      { No match, output a literal byte }
1927
 
      {$IFDEF DEBUG}
 
1901
      {$IFDEF ZLIB_DEBUG}
1928
1902
      Tracevv(char(s.window^[s.strstart]));
1929
1903
      {$ENDIF}
1930
1904
      {_tr_tally_lit (s, 0, s.window^[s.strstart], bflush);}
1931
1905
      bflush := _tr_tally (s, 0, s.window^[s.strstart]);
1932
1906
 
1933
 
      Dec(s.lookahead);
1934
 
      Inc(s.strstart);
 
1907
      dec(s.lookahead);
 
1908
      inc(s.strstart);
1935
1909
    end;
1936
1910
    if bflush then
1937
1911
    begin  {FLUSH_BLOCK(s, FALSE);}
1966
1940
  no better match at the next window position. }
1967
1941
 
1968
1942
{local}
1969
 
function deflate_slow(var s : deflate_state; flush : int) : block_state;
 
1943
function deflate_slow(var s : deflate_state; flush : integer) : block_state;
1970
1944
var
1971
1945
  hash_head : IPos;       { head of hash chain }
1972
1946
  bflush : boolean;       { set if current block must be flushed }
1973
1947
var
1974
 
  max_insert : uInt;
 
1948
  max_insert : cardinal;
1975
1949
begin
1976
1950
  hash_head := ZNIL;
1977
1951
 
1978
1952
  { Process the input block. }
1979
 
  while TRUE do
1980
 
  begin
 
1953
  repeat
1981
1954
    { Make sure that we always have enough lookahead, except
1982
1955
      at the end of the input file. We need MAX_MATCH bytes
1983
1956
      for the next match, plus MIN_MATCH bytes to insert the
1984
1957
      string following the next match. }
1985
1958
 
1986
1959
    if (s.lookahead < MIN_LOOKAHEAD) then
1987
 
    begin
1988
 
      fill_window(s);
1989
 
      if (s.lookahead < MIN_LOOKAHEAD) and (flush = Z_NO_FLUSH) then
1990
1960
      begin
1991
 
        deflate_slow := need_more;
1992
 
        exit;
 
1961
        fill_window(s);
 
1962
        if (s.lookahead < MIN_LOOKAHEAD) and (flush = Z_NO_FLUSH) then
 
1963
          begin
 
1964
            deflate_slow := need_more;
 
1965
            exit;
 
1966
          end;
 
1967
 
 
1968
        if s.lookahead=0 then
 
1969
          break; { flush the current block }
1993
1970
      end;
1994
1971
 
1995
 
      if (s.lookahead = 0) then
1996
 
        break; { flush the current block }
1997
 
    end;
1998
 
 
1999
1972
    { Insert the string window[strstart .. strstart+2] in the
2000
1973
      dictionary, and set hash_head to the head of the hash chain: }
2001
1974
 
2002
1975
    if (s.lookahead >= MIN_MATCH) then
2003
 
    begin
2004
1976
      INSERT_STRING(s, s.strstart, hash_head);
2005
 
    end;
2006
1977
 
2007
1978
    { Find the longest match, discarding those <= prev_length. }
2008
1979
 
2012
1983
 
2013
1984
    if (hash_head <> ZNIL) and (s.prev_length < s.max_lazy_match) and
2014
1985
       (s.strstart - hash_head <= {MAX_DIST}(s.w_size-MIN_LOOKAHEAD)) then
2015
 
    begin
 
1986
      begin
2016
1987
        { To simplify the code, we prevent matches with the string
2017
1988
          of window index 0 (in particular we have to avoid a match
2018
1989
          of the string with itself at the start of the input file). }
2019
1990
 
2020
1991
        if (s.strategy <> Z_HUFFMAN_ONLY) then
2021
 
        begin
2022
1992
          s.match_length := longest_match (s, hash_head);
2023
 
        end;
2024
1993
        { longest_match() sets match_start }
2025
1994
 
2026
1995
        if (s.match_length <= 5) and ((s.strategy = Z_FILTERED) or
2027
1996
             ((s.match_length = MIN_MATCH) and
2028
1997
              (s.strstart - s.match_start > TOO_FAR))) then
2029
 
        begin
 
1998
          begin
2030
1999
            { If prev_match is also MIN_MATCH, match_start is garbage
2031
2000
              but we will ignore the current match anyway. }
2032
2001
 
2033
2002
            s.match_length := MIN_MATCH-1;
2034
 
        end;
2035
 
    end;
 
2003
          end;
 
2004
      end;
2036
2005
    { If there was a match at the previous step and the current
2037
2006
      match is not better, output the previous match: }
2038
2007
 
2039
 
    if (s.prev_length >= MIN_MATCH)
2040
 
      and (s.match_length <= s.prev_length) then
2041
 
    begin
2042
 
      max_insert := s.strstart + s.lookahead - MIN_MATCH;
2043
 
      { Do not insert strings in hash table beyond this. }
2044
 
      {$ifdef DEBUG}
2045
 
      check_match(s, s.strstart-1, s.prev_match, s.prev_length);
2046
 
      {$endif}
 
2008
    if (s.prev_length>=MIN_MATCH) and (s.match_length<=s.prev_length) then
 
2009
      begin
 
2010
        max_insert := s.strstart + s.lookahead - MIN_MATCH;
 
2011
        { Do not insert strings in hash table beyond this. }
 
2012
        {$ifdef ZLIB_DEBUG}
 
2013
        check_match(s, s.strstart-1, s.prev_match, s.prev_length);
 
2014
        {$endif}
2047
2015
 
2048
 
      {_tr_tally_dist(s, s->strstart -1 - s->prev_match,
2049
 
                        s->prev_length - MIN_MATCH, bflush);}
2050
 
      bflush := _tr_tally(s, s.strstart -1 - s.prev_match,
2051
 
                           s.prev_length - MIN_MATCH);
 
2016
        {_tr_tally_dist(s, s->strstart -1 - s->prev_match,
 
2017
                          s->prev_length - MIN_MATCH, bflush);}
 
2018
        bflush := _tr_tally(s, s.strstart -1 - s.prev_match,
 
2019
                             s.prev_length - MIN_MATCH);
2052
2020
 
2053
2021
      { Insert in hash table all strings up to the end of the match.
2054
2022
        strstart-1 and strstart are already inserted. If there is not
2055
2023
        enough lookahead, the last two strings are not inserted in
2056
2024
        the hash table. }
2057
2025
 
2058
 
      Dec(s.lookahead, s.prev_length-1);
2059
 
      Dec(s.prev_length, 2);
2060
 
      repeat
2061
 
        Inc(s.strstart);
2062
 
        if (s.strstart <= max_insert) then
2063
 
        begin
2064
 
          INSERT_STRING(s, s.strstart, hash_head);
2065
 
        end;
2066
 
        Dec(s.prev_length);
2067
 
      until (s.prev_length = 0);
2068
 
      s.match_available := FALSE;
2069
 
      s.match_length := MIN_MATCH-1;
2070
 
      Inc(s.strstart);
 
2026
{$ifdef ZLIB_DEBUG}
 
2027
        if s.lookahead<s.prev_length-1 then
 
2028
           runerror(255);
 
2029
{$endif}
 
2030
        dec(s.lookahead, s.prev_length-1);
 
2031
        dec(s.prev_length, 2);
 
2032
        repeat
 
2033
          inc(s.strstart);
 
2034
          if s.strstart<=max_insert then
 
2035
            INSERT_STRING(s, s.strstart, hash_head);
 
2036
          dec(s.prev_length);
 
2037
        until s.prev_length = 0;
 
2038
        s.match_available := false;
 
2039
        s.match_length := MIN_MATCH-1;
 
2040
        inc(s.strstart);
2071
2041
 
2072
 
      if (bflush) then  {FLUSH_BLOCK(s, FALSE);}
2073
 
      begin
2074
 
        FLUSH_BLOCK_ONLY(s, FALSE);
2075
 
        if (s.strm^.avail_out = 0) then
2076
 
        begin
2077
 
          deflate_slow := need_more;
2078
 
          exit;
2079
 
        end;
2080
 
      end;
2081
 
    end
 
2042
        if bflush then  {FLUSH_BLOCK(s, FALSE);}
 
2043
          begin
 
2044
            FLUSH_BLOCK_ONLY(s,false);
 
2045
            if s.strm^.avail_out=0 then
 
2046
              begin
 
2047
                deflate_slow := need_more;
 
2048
                exit;
 
2049
              end;
 
2050
          end;
 
2051
      end
2082
2052
    else
2083
 
      if (s.match_available) then
2084
 
      begin
2085
 
        { If there was no match at the previous position, output a
2086
 
          single literal. If there was a match but the current match
2087
 
          is longer, truncate the previous match to a single literal. }
2088
 
        {$IFDEF DEBUG}
2089
 
        Tracevv(char(s.window^[s.strstart-1]));
2090
 
        {$ENDIF}
2091
 
        bflush := _tr_tally (s, 0, s.window^[s.strstart-1]);
 
2053
      if s.match_available then
 
2054
        begin
 
2055
          { If there was no match at the previous position, output a
 
2056
            single literal. If there was a match but the current match
 
2057
            is longer, truncate the previous match to a single literal. }
 
2058
          {$IFDEF ZLIB_DEBUG}
 
2059
          Tracevv(char(s.window^[s.strstart-1]));
 
2060
          {$ENDIF}
 
2061
          bflush := _tr_tally (s, 0, s.window^[s.strstart-1]);
2092
2062
 
2093
 
        if bflush then
2094
 
        begin
2095
 
          FLUSH_BLOCK_ONLY(s, FALSE);
2096
 
        end;
2097
 
        Inc(s.strstart);
2098
 
        Dec(s.lookahead);
2099
 
        if (s.strm^.avail_out = 0) then
2100
 
        begin
2101
 
          deflate_slow := need_more;
2102
 
          exit;
2103
 
        end;
2104
 
      end
 
2063
          if bflush then
 
2064
            FLUSH_BLOCK_ONLY(s, FALSE);
 
2065
          inc(s.strstart);
 
2066
{$ifdef ZLIB_DEBUG}
 
2067
          if s.lookahead=0 then
 
2068
             runerror(255);
 
2069
{$endif}
 
2070
          dec(s.lookahead);
 
2071
          if (s.strm^.avail_out = 0) then
 
2072
            begin
 
2073
              deflate_slow := need_more;
 
2074
              exit;
 
2075
            end;
 
2076
        end
2105
2077
      else
2106
 
      begin
 
2078
        begin
2107
2079
        { There is no previous match to compare with, wait for
2108
2080
          the next step to decide. }
2109
2081
 
2110
 
        s.match_available := TRUE;
2111
 
        Inc(s.strstart);
2112
 
        Dec(s.lookahead);
2113
 
      end;
2114
 
  end;
 
2082
          s.match_available := TRUE;
 
2083
          inc(s.strstart);
 
2084
{$ifdef ZLIB_DEBUG}
 
2085
          if s.lookahead=0 then
 
2086
             runerror(255);
 
2087
{$endif}
 
2088
          dec(s.lookahead);
 
2089
        end;
 
2090
  until false;
2115
2091
 
2116
 
  {$IFDEF DEBUG}
 
2092
  {$IFDEF ZLIB_DEBUG}
2117
2093
  Assert (flush <> Z_NO_FLUSH, 'no flush?');
2118
2094
  {$ENDIF}
2119
2095
  if (s.match_available) then
2120
2096
  begin
2121
 
    {$IFDEF DEBUG}
 
2097
    {$IFDEF ZLIB_DEBUG}
2122
2098
    Tracevv(char(s.window^[s.strstart-1]));
2123
2099
    bflush :=
2124
2100
    {$ENDIF}