forked from neurolabusc/MRIcroGL10_OLD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzio2.pas
executable file
·2148 lines (1805 loc) · 58.7 KB
/
gzio2.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unit gzio2;
{
Pascal unit based on gzio.c -- IO on .gz files
Copyright (C) 1995-1998 Jean-loup Gailly.
Define NO_DEFLATE to compile this file without the compression code
Pascal tranlastion based on code contributed by Francisco Javier Crespo
Copyright (C) 1998,1999,2000 by Jacques Nomssi Nzali
This software is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
This version was altered by Chris Rorden to allow extracting to memory (e.g. GZipBuffer) and retains the same license.
}
{$H+}
interface
{$D-,L-,O+,Q-,R-,Y-,S-}
{$I zconflaz.inc}
uses
{$IFDEF FPC}
{$ifdef UNIX}
baseunix,
{$else}
dos,
{$endif}
{$ENDIF}
crc,sysutils,zbase, zdeflate, zinflate,define_types; //
type gzFile = pointer;
type z_off_t = longint;
function Gunzip (var FFileSource,FFileDestination: string): integer;
procedure GZipBuffer(var FGzipFilename,FFileDestination: String;lxInBuffer: byteP;lInSize: Integer; lOverwritewarn: boolean);overload;
procedure GZipBuffer(var FFileDestination: String;lxInBuffer: byteP;lInSize: Integer; lOverwritewarn: boolean);overload;
procedure UnGZip2 (var lInFname: string; var lBuf: ByteP{}; lOffset,lMaxSz,Skip: integer); //unzip
procedure UnGZip (var lInFname: string; var lBuf: ByteP{}; lOffset,lMaxSz: integer); //unzip
procedure UnGZipCore (var infile : gzFile; var lBuf: ByteP; lReadBytes: integer; lWrite: boolean);
procedure UnGZipFile (var lFname,lOUtname: string); //unzip
function gzopen (path:string; mode:string) : gzFile; overload;
function gzopen (path:string; mode:string; skip: integer) : gzFile; overload;
function gzread (f:gzFile; buf:pointer; len:cardinal) : integer;
function gzgetc (f:gzfile) : integer;
function gzgets (f:gzfile; buf:Pchar; len:integer) : Pchar;
procedure GZipFile(lSrcName,lDestName: String); overload;
procedure GZipFile(lSrcName,lDestName: String;lDeleteSrc: boolean);overload;
{$ifndef NO_DEFLATE}
function gzwrite (f:gzFile; buf:pointer; len:cardinal) : integer;
function gzputc (f:gzfile; c:char) : integer;
function gzputs (f:gzfile; s:Pchar) : integer;
function gzflush (f:gzFile; flush:integer) : integer;
{$ifdef GZ_FORMAT_STRING}
function gzprintf (zfile : gzFile;
const format : string;
a : array of integer); { doesn't compile }
{$endif}
{$endif}
function gzseek (f:gzfile; offset:z_off_t; whence:integer) : z_off_t;
function gztell (f:gzfile) : z_off_t;
function gzclose (f:gzFile) : integer;
function gzerror (f:gzFile; var errnum:smallint) : string;
function gzsetparams (f:gzfile; level:integer; strategy:integer) : integer;
function gzrewind (f:gzFile) : integer;
function gzeof (f:gzfile) : boolean;
const
SEEK_SET {: z_off_t} = 0; { seek from beginning of file }
SEEK_CUR {: z_off_t} = 1; { seek from current position }
SEEK_END {: z_off_t} = 2;
implementation
{$include isgui.inc}
{$IFDEF GUI}uses dialogs;{$ELSE} uses dialogsx;{$ENDIF}
const
Z_EOF = -1; { same value as in STDIO.H }
Z_BUFSIZE = 16384;
{ Z_PRINTF_BUFSIZE = 4096; }
gz_magic : array[0..1] of byte = ($1F, $8B); { gzip magic header }
{ gzip flag byte }
ASCII_FLAG = $01; { bit 0 set: file probably ascii text }
HEAD_CRC = $02; { bit 1 set: header CRC present }
EXTRA_FIELD = $04; { bit 2 set: extra field present }
ORIG_NAME = $08; { bit 3 set: original file name present }
COMMENT = $10; { bit 4 set: file comment present }
RESERVED = $E0; { bits 5..7: reserved }
type gz_stream = record
stream : z_stream;
z_err : integer; { error code for last stream operation }
z_eof : boolean; { set if end of input file }
gzfile : file; { .gz file }
inbuf : Pbyte; { input buffer }
outbuf : Pbyte; { output buffer }
crc : cardinal; { crc32 of uncompressed data }
msg, { error message - limit 79 chars }
path : string[79]; { path name for debugging only - limit 79 chars }
transparent : boolean; { true if input file is not a .gz file }
mode : char; { 'w' or 'r' }
startpos : longint; { start of compressed data in file (header skipped) }
end;
type gz_streamp = ^gz_stream;
function destroy (var s:gz_streamp) : integer; forward;
procedure check_header(s:gz_streamp); forward;
procedure showmessagex(lStr: string);
begin
{$IFDEF GUI}
showmessage(lStr);
{$ELSE}
msg(lStr);
{$ENDIF}
end;
procedure UnGZip2 (var lInFname: string; var lBuf: ByteP; lOffset,lMaxSz,Skip: integer); //unzip
const
BUFLEN = 16384;
var
infile : gzFile;
lFname : ansistring;
lbufsz,len,lI : integer;
written : integer;
buf : packed array [0..BUFLEN-1] of byte; { Global uses BSS instead of stack }
begin
lFName := lInFName;
infile := gzopen (lFName, 'r',Skip);
written := 0;
if lOffset > 0 then begin
Len := lOffset div BUFLEN;
if Len > 0 then
for lI := 1 to Len do
gzread (infile, @buf, BUFLEN {1388});
Len := lOffset mod BUFLEN;
gzread (infile, @buf, Len);
end;
lbufsz := BUFLEN;
if lMaxSz < BUFLEN then
lbufsz := lMaxSz;
while true do begin
len := gzread (infile, @buf, lbufsz);
if (len < 0) then begin
break
end;
if (len = 0)
then break;
if (Written+len) > lMaxSz then begin
if Written < lMaxSz then
Move(buf,lbuf^[Written+1],lMaxSz-Written); //cr2007
break;
end;
Move(buf,lbuf^[Written+1],len);
Written := Written + len;
end; {WHILE}
gzclose (infile);
//filemode := 2;
end;
procedure UnGZip (var lInFname: string; var lBuf: ByteP; lOffset,lMaxSz: integer);
begin
UnGZip2 ( lInFname, lBuf, lOffset,lMaxSz,0);
end;
(*procedure UnGZip (var lInFname: string; var lBuf: ByteP; lOffset,lMaxSz: integer); //unzip
const
BUFLEN = 16384;
var
infile : gzFile;
lFname : ansistring;
lbufsz,len,lI : integer;
written : integer;
buf : packed array [0..BUFLEN-1] of byte; { Global uses BSS instead of stack }
begin
lFName := lInFName;
infile := gzopen (lFName, 'r');
written := 0;
if lOffset > 0 then begin
Len := lOffset div BUFLEN;
if Len > 0 then
for lI := 1 to Len do
gzread (infile, @buf, BUFLEN {1388});
Len := lOffset mod BUFLEN;
gzread (infile, @buf, Len);
end;
lbufsz := BUFLEN;
if lMaxSz < BUFLEN then
lbufsz := lMaxSz;
while true do begin
len := gzread (infile, @buf, lbufsz);
if (len < 0) then begin
break
end;
if (len = 0)
then break;
if (Written+len) > lMaxSz then begin
if Written < lMaxSz then
Move(buf,lbuf^[Written+1],lMaxSz-Written); //cr2007
break;
end;
Move(buf,lbuf^[Written+1],len);
Written := Written + len;
end; {WHILE}
gzclose (infile);
//filemode := 2;
end; *)
procedure UnGZipCore (var infile : gzFile; var lBuf: ByteP; lReadBytes: integer; lWrite: boolean);
const
BUFLEN = 16384;
var
buf : packed array [0..BUFLEN-1] of byte; { Global uses BSS instead of stack }
len,lI,written : integer;
begin
written := 0;
if lReadBytes < 1 then exit;
Len := lReadBytes div BUFLEN;
if Len > 0 then
for lI := 1 to Len do begin
gzread (infile, @buf, BUFLEN {1388});
if lWrite then
Move(buf,lbuf[Written+1],BUFLEN);
Written := Written + BUFLEN;
end;
Len := lReadBytes mod BUFLEN;
if Len = 0 then exit;
gzread (infile, @buf, Len);
if lWrite then
Move(buf,lbuf[Written+1],len);
end; //ungzipCore
procedure UnGZipFile (var lFname,lOUtname: string); //unzip
const
bufsz = 16384;
var
infile : gzFile;
len,lI : integer;
//written : integer;
lF: File;
buf : packed array [0..bufsz-1] of byte; { Global uses BSS instead of stack }
begin
//infile := gzopenZ (lFName, 'r', 0);
infile := gzopen (lFName, 'r');
//written := 0;
//lbufsz := BUFLEN;
Filemode := 1;
AssignFile(lF, lOUtname);
Rewrite(lF,1);
while true do begin
len := gzread (infile, @buf, bufsz);
if (len < 0) then begin
break
end;
if (len = 0)
then break;
BlockWrite(lF,buf, len);
//Move(buf,lbuf[Written+1],len);
//Written := Written + len;
end; {WHILE}
gzclose (infile);
CloseFile(lF);
Filemode := 2; //1366
end;
function gz_compress (var infile:file; outfile:gzFile): integer;
var
len : cardinal;
ioerr : integer;
buf : packed array [0..Z_BUFSIZE-1] of byte; { Global uses BSS instead of stack }
errorcode : byte;
fsize {, lensize} : DWord;
begin
errorcode := 0;
//Progress := 0;
fsize := FileSize(infile);
//lensize := 0;
//if FProgressStep > 0 then DoOnProgress;
while true do begin
{$I-}blockread (infile, buf, Z_BUFSIZE, len);{$I+}
ioerr := IOResult;
if (ioerr <> 0) then begin
errorcode := 1;
break
end;
if (len = 0) then break;
{$WARNINGS OFF}{Comparing signed and unsigned types}
if (gzwrite (outfile, @buf, len) <> len) then begin
{$WARNINGS OFF}
errorcode := 2;
break
end;
end;
closeFile (infile);
if (gzclose (outfile) <> 0{Z_OK}) then errorcode := 3;
gz_compress := errorcode;
end; // proc gz_compress
procedure GZipFile(lSrcName,lDestName: String; lDeleteSrc: boolean); overload;
var
infile : file;
outfile : gzFile;
ioerr : integer;
mode : string;
begin
//Msg('GZip ' + extractfilename(lSrcName));
//writeln(lSrcName+' -> GZ -> '+lDestName);
mode := 'w6 ';
Assign (infile, lSrcName);
{$I-}
Reset (infile,1);
{$I+}
ioerr := IOResult;
if (ioerr <> 0) then begin
Showmessagex ('GZipFile error: '+inttostr(ioerr));
halt(1);
end;
outfile := gzopen (lDestName, mode);
if (outfile = NIL) then begin
Showmessagex('unable to create '+lDestName);
exit;
end;
gz_compress(infile, outfile);
if lDeleteSrc then
erase (infile);
end;
procedure GZipFile(lSrcName,lDestName: String); overload;
var
FGzipFilename : string;
FGzipComments : string;
outmode : string;
s,FFileDestination : string;
infile : file;
outfile : gzFile;
FCompressionLevel{,errorcode} : integer;
flags : Integer;
stream : gz_streamp;
//p : PChar;
ioerr : integer;
begin
//FGzipHeader := [zFilename];
FGzipFilename:= lSrcName;
FGzipComments := '';
FCompressionLevel := 6;
//MainForm.ProgressBar1.position :=1;
//Gzip (lFile,lMulti);
FFileDestination := lDestName;
//result := 2; //return error if user aborts
(* if fileexists(FFileDestination) then begin
case MessageDlg('Overwrite the file '+FFileDestination+'?', mtConfirmation,[mbYes, mbAbort], 0) of { produce the message dialog box }
mrAbort: exit;
end;
end;*)
AssignFile (infile, lSrcName);
{$I-}
Reset (infile,1);
{$I+}
ioerr := IOResult;
if (ioerr <> 0) then begin
// Showmessage('Can''t open: '+lSrcName);
//errorcode := 1
end
else begin
outmode := 'w ';
//s := IntToStr(FCompressionLevel);
outmode[2] := '6';//s[1];
outmode[3] := ' ';
(*case FCompressionType of
Standard : outmode[3] := ' ';
HuffmanOnly : outmode[3] := 'h';
Filtered : outmode[3] := 'f';
end;*)
//flags := 0;
//if (zfilename in FGzipHeader) then
flags := ORIG_NAME;
//if (comment in FGzipHeader) then flags := flags + COMMENT_;
outfile := gzopen (lSrcName, outmode);
if (outfile = NIL) then begin
//Showmessage('Can''t open: '+lSrcName);
close( infile);
exit;
end
else begin
{ if flags are set then write them }
stream := gz_streamp(outfile);
if {(zfilename in FGzipHeader)} true then begin
s := lSrcName;//999 ExtractFilename(lSrcName);
//p := PChar(s);
blockWrite( stream^.gzfile, {p[0]}s, length(s)+1);
stream^.startpos := stream^.startpos + length(s) + 1
end;
gz_compress(infile, outfile);
end
end;
end;
procedure file_compress2 (filename,outname:string);
var
infile : file;
outfile : gzFile;
ioerr : integer;
mode : string;
begin
mode := 'w6 ';
Assign (infile, filename);
{$I-}
Reset (infile,1);
{$I+}
ioerr := IOResult;
if (ioerr <> 0) then begin
writeln ('open error: ',ioerr);
halt(1);
end;
outfile := gzopen (outname, mode);
if (outfile = NIL) then begin
//999 showmessage(' can''t gzopen '+outname);
halt(1);
end;
gz_compress(infile, outfile);
erase (infile);
end;
(*function gz_compressBuffer (lxInBuffer: ByteP;lInSize: integer;outfile:gzFile): integer;
var
len : Integer;
lInBufferPos,ioerr : integer;
buf : packed array [0..Z_BUFSIZE-1] of byte; { Global uses BSS instead of stack }
//lInBufPtr,lOutbufPtr: pointer;
errorcode : byte;
//fsize, lensize : DWord;
function blocktransfer(var lInBuffer: ByteP; lSizeRequested: integer; var lSizeTransferred:integer): integer;
begin
result := 0;
if lInBufferPos > lInSize then begin
result := 666;
exit;
end else if (lInBufferPos + lSizeRequested) <= lInSize then
lSizeTransferred := lSizeRequested
else
lSizeTransferred := lInSize-lInBufferPos;
//for lC := 1 to lSizeTransferred do
// buf[lC-1] := lInBuffer[lInBufferPos+lC] ;
move(lInbuffer[lInBufferPos+1],buf,lSizeTransferred);
//move(src,dest,count);
lInBufferPos := lInBufferPos+lSizeTransferred;
end;
begin
lInBufferPos := 0;
errorcode := 0;
//Progress := 0;
//fsize := lInSize;
//lensize := 0;
//if FProgressStep > 0 then DoOnProgress;
while true do begin
//lll{$I-}blockread (infile, buf, Z_BUFSIZE, len);{$I+}
ioerr := blocktransfer(lxInBuffer,Z_BUFSIZE, len);
if (ioerr <> 0) then begin
errorcode := 1;
break
end;
if (len = 0) then break;
{$WARNINGS OFF}{Comparing signed and unsigned types}
if (gzwrite (outfile, @buf, len) <> len) then begin
{$WARNINGS OFF}
errorcode := 2;
break
end;
end; {WHILE}
if (gzclose (outfile) <> 0{Z_OK}) then errorcode := 3;
result := errorcode;
end;
procedure GZipBuffer(var FGzipFilename,FFileDestination: String;lxInBuffer: byteP;lInSize: Integer; lOverwritewarn: boolean);
var
FGzipComments ,outmode,s : string;
infile : file;
outfile : gzFile;
FCompressionLevel : integer;
flags : uInt;
stream : gz_streamp;
p : PChar;
begin
FGzipComments := '';
FCompressionLevel := 6;
if (FCompressionLevel > 9) or (FCompressionLevel<0) then FCompressionLevel := 6;
if lOverwritewarn and fileexists(FFileDestination) then begin
case MessageDlg('Overwrite the file '+FFileDestination+'?', mtConfirmation,[mbYes, mbAbort], 0) of { produce the message dialog box }
mrAbort: exit;
end;
end;
//w adds .gz extension-> outmode := 'w ';
outmode := 'w ';
s := IntToStr(FCompressionLevel);
outmode[2] := s[1];
outmode[3] := ' ';
flags := ORIG_NAME;
outfile := gzopenZ (FFileDestination, outmode, flags);
if (outfile = NIL) then begin
MessageDlg('Can''t open: '+FFileDestination, mtError, [mbAbort], 0);
close( infile);
exit;
end
else begin
stream := gz_streamp(outfile);
if {(zfilename in FGzipHeader)} true then begin
//s := ExtractFilename(lInFileName);
s := ExtractFilename(FGzipFilename);
//p := PChar(s);
blockWrite( stream^.gzfile,s , length(s)+1);
stream^.startpos := stream^.startpos + length(s) + 1
end;
gz_compressBuffer (lxInBuffer,lInSize,outfile);
end
end; *)
{ GZOPEN ====================================================================
Opens a gzip (.gz) file for reading or writing. As Pascal does not use
file descriptors, the code has been changed to accept only path names.
The mode parameter defaults to BINARY read or write operations ('r' or 'w')
but can also include a compression level ('w9') or a strategy: Z_FILTERED
as in 'w6f' or Z_HUFFMAN_ONLY as in 'w1h'. (See the description of
deflateInit2 for more information about the strategy parameter.)
gzopen can be used to open a file which is not in gzip format; in this
case, gzread will directly read from the file without decompression.
gzopen returns NIL if the file could not be opened (non-zero IOResult)
or if there was insufficient memory to allocate the (de)compression state
(zlib error is Z_MEM_ERROR).
============================================================================}
(*function gzopenZ(sourceFilename:string; mode:string; flags:uInt) : gzFile;
var
i : uInt;
err: int;
level: int;
strategy : int; { compression strategy }
s : gz_streamp;
path: string;
{$IFDEF MSDOS}
attr : word; { file attributes }
{$ENDIF}
{$IFNDEF NO_DEFLATE}
gzheader : array [0..9] of byte;
{$ENDIF}
begin
//wait(30);
path := sourceFilename;
GetMem (s,sizeof(gz_stream));
if not Assigned (s) then begin
result := Z_NULL;
exit;
end;
if (path='') then begin
//999 Showmessage('Error with path');
result := Z_NULL;
exit;
end;
//showmessage('gzOpenCompleted');
level := Z_DEFAULT_COMPRESSION;
strategy := Z_DEFAULT_STRATEGY;
s^.stream.zalloc := NIL; { (alloc_func)0 }
s^.stream.zfree := NIL; { (free_func)0 }
s^.stream.opaque := NIL; { (voidpf)0 }
s^.stream.next_in := Z_NULL;
s^.stream.next_out := Z_NULL;
s^.stream.avail_in := 0;
s^.stream.avail_out := 0;
s^.z_err := Z_OK;
s^.z_eof := false;
s^.inbuf := Z_NULL;
s^.outbuf := Z_NULL;
s^.crc := crc32(0, Z_NULL, 0);
s^.msg := '';
s^.transparent := false;
//showmessage(s^.path+inttostr(length(path)));
s^.mode := chr(0);
for i:=1 to Length(mode) do begin
case mode[i] of
'r' : s^.mode := 'r';
'w' : s^.mode := 'w';
'0'..'9' : level := Ord(mode[i])-Ord('0');
'f' : strategy := Z_FILTERED;
'h' : strategy := Z_HUFFMAN_ONLY;
end;
end;
//if (s^.mode='w') then begin path := path+'.gz'; end;
s^.path := path; { limit to 255 chars }
if (s^.mode=chr(0)) then begin
destroyS(s);
result := gzFile(Z_NULL);
exit;
end;
if (s^.mode='w') then begin
{$IFDEF NO_DEFLATE}
err := Z_STREAM_ERROR;
{$ELSE}
err := deflateInit2 (s^.stream, level, Z_DEFLATED, -MAX_WBITS,
DEF_MEM_LEVEL, strategy);
{ windowBits is passed < 0 to suppress zlib header }
GetMem (s^.outbuf, Z_BUFSIZE);
s^.stream.next_out := s^.outbuf;
{$ENDIF}
if (err <> Z_OK) or (s^.outbuf = Z_NULL) then begin
destroyS(s);
result := gzFile(Z_NULL);
exit;
end;
end
else begin
GetMem (s^.inbuf, Z_BUFSIZE);
s^.stream.next_in := s^.inbuf;
err := inflateInit2_ (s^.stream, -MAX_WBITS, ZLIB_VERSION, sizeof(z_stream));
{ windowBits is passed < 0 to tell that there is no zlib header }
if (err <> Z_OK) or (s^.inbuf = Z_NULL) then begin
destroyS(s);
result := gzFile(Z_NULL);
exit;
end;
end;
s^.stream.avail_out := Z_BUFSIZE;
{$IFOPT I+} {$I-} {$define IOcheck} {$ENDIF}
Assign (s^.gzfile, path {10/10/2006 s^.path});
//Assign (s^.gzfile, s^.path);
{$ifdef MSDOS}
GetFAttr(s^.gzfile, Attr);
if (DosError <> 0) and (s^.mode='w') then
ReWrite (s^.gzfile,1)
else
Reset (s^.gzfile,1);
{$else}
if {(not FileExists(s^.path)) and} (s^.mode='w') then
// Vincent: changed IF because I don't want old data behind my
// new made .gz-file
ReWrite (s^.gzfile,1)
else
Reset (s^.gzfile,1);
{$endif}
{$IFDEF IOCheck} {$I+} {$ENDIF}
if (IOResult <> 0) then begin
destroyS(s);
result := gzFile(Z_NULL);
exit;
end;
if (s^.mode = 'w') then begin { Write a very simple .gz header }
{$IFNDEF NO_DEFLATE}
gzheader [0] := gz_magic [0];
gzheader [1] := gz_magic [1];
gzheader [2] := Z_DEFLATED; { method }
gzheader [3] := flags; { flags }
gzheader [4] := 0; { time[0] }
gzheader [5] := 0; { time[1] }
gzheader [6] := 0; { time[2] }
gzheader [7] := 0; { time[3] }
gzheader [8] := 0; { xflags }
gzheader [9] := 0; { OS code = MS-DOS }
blockwrite (s^.gzfile, gzheader, 10);
s^.startpos := LONG(10);
{$ENDIF}
end
else begin
check_header(s); { skip the .gz header }
{$WARNINGS OFF} { combining signed and unsigned types }
s^.startpos := FilePos(s^.gzfile) - s^.stream.avail_in;
{$WARNINGS ON}
end;
result := gzFile(s);
end;//gzopenZ *)
function StringMaxLen (var lInStr: ansistring; lMaxSz: integer): ansistring;
var
lPos: integer;
begin
//note: strings shortened to lMaxSz-1
//null termination requires one byte
//e.g. size of 80 bytes can become a null terminated string with up to 79 characters
if length(lInStr) >= lMaxSz then begin//crop string
result := '';
for lPos := 1 to (lMaxSz-1) do
result := result + lInStr[lPos];
end else
result := lInStr;
end;
{ GZOPEN ====================================================================
Opens a gzip (.gz) file for reading or writing. As Pascal does not use
file descriptors, the code has been changed to accept only path names.
The mode parameter defaults to BINARY read or write operations ('r' or 'w')
but can also include a compression level ('w9') or a strategy: Z_FILTERED
as in 'w6f' or Z_HUFFMAN_ONLY as in 'w1h'. (See the description of
deflateInit2 for more information about the strategy parameter.)
gzopen can be used to open a file which is not in gzip format; in this
case, gzread will directly read from the file without decompression.
gzopen returns nil if the file could not be opened (non-zero IOResult)
or if there was insufficient memory to allocate the (de)compression state
(zlib error is Z_MEM_ERROR).
============================================================================}
function gzopen (path:string; mode:string; skip: integer) : gzFile;
var
i : cardinal;
err : integer;
level : integer; { compression level }
strategy : integer; { compression strategy }
s : gz_streamp;
{$ifdef UNIX}
info: stat;
{$else}
attr: word;
{$endif}
{$IFNDEF NO_DEFLATE}
gzheader : array [0..9] of byte;
{$ENDIF}
begin
if (path='') or (mode='') then begin
gzopen := nil;
exit;
end;
GetMem (s,sizeof(gz_stream));
if not Assigned (s) then begin
gzopen := nil;
exit;
end;
level := Z_DEFAULT_COMPRESSION;
strategy := Z_DEFAULT_STRATEGY;
s^.stream.next_in := nil;
s^.stream.next_out := nil;
s^.stream.avail_in := 0;
s^.stream.avail_out := 0;
s^.z_err := Z_OK;
s^.z_eof := false;
s^.inbuf := nil;
s^.outbuf := nil;
s^.crc := crc32(0, nil, 0);
s^.msg := '';
s^.transparent := false;
s^.path := path; { limit to 255 chars }
s^.mode := chr(0);
for i:=1 to Length(mode) do begin
case mode[i] of
'r' : s^.mode := 'r';
'w' : s^.mode := 'w';
'0'..'9' : level := Ord(mode[i])-Ord('0');
'f' : strategy := Z_FILTERED;
'h' : strategy := Z_HUFFMAN_ONLY;
end;
end;
if (s^.mode=chr(0)) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
if (s^.mode='w') then begin
{$IFDEF NO_DEFLATE}
err := Z_STREAM_ERROR;
{$ELSE}
err := deflateInit2 (s^.stream, level, Z_DEFLATED, -MAX_WBITS,
DEF_MEM_LEVEL, strategy);
{ windowBits is passed < 0 to suppress zlib header }
GetMem (s^.outbuf, Z_BUFSIZE);
s^.stream.next_out := s^.outbuf;
{$ENDIF}
if (err <> Z_OK) or (s^.outbuf = nil) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
end
else begin
GetMem (s^.inbuf, Z_BUFSIZE);
s^.stream.next_in := s^.inbuf;
err := inflateInit2_ (s^.stream, -MAX_WBITS, ZLIB_VERSION, sizeof(z_stream));
{ windowBits is passed < 0 to tell that there is no zlib header }
if (err <> Z_OK) or (s^.inbuf = nil) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
end;
s^.stream.avail_out := Z_BUFSIZE;
//showmessage(s^.path+' '+inttostr(length(path)));
{$IFOPT I+} {$I-} {$define IOcheck} {$ENDIF}
//11/11/07 Assign (s^.gzfile, s^.path);
Assign (s^.gzfile, path);
{$ifdef unix}
if (fpstat(s^.path,info)<0) and (s^.mode='w') then
ReWrite (s^.gzfile,1)
else begin
Reset (s^.gzfile,1);
if skip > 0 then Seek(s^.gzfile,skip);
end;
{$else}
GetFAttr(s^.gzfile, Attr);
if (DosError <> 0) and (s^.mode='w') then
ReWrite (s^.gzfile,1)
else begin
Reset (s^.gzfile,1);
if skip > 0 then Seek(s^.gzfile,skip);
end;
{$endif}
{$IFDEF IOCheck} {$I+} {$ENDIF}
if (IOResult <> 0) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
if (s^.mode = 'w') then begin { Write a very simple .gz header }
{$IFNDEF NO_DEFLATE}
gzheader [0] := gz_magic [0];
gzheader [1] := gz_magic [1];
gzheader [2] := Z_DEFLATED; { method }
gzheader [3] := 0; { flags }
gzheader [4] := 0; { time[0] }
gzheader [5] := 0; { time[1] }
gzheader [6] := 0; { time[2] }
gzheader [7] := 0; { time[3] }
gzheader [8] := 0; { xflags }
gzheader [9] := 0; { OS code = MS-DOS }
blockwrite (s^.gzfile, gzheader, 10);
s^.startpos := longint(10);
{$ENDIF}
end
else begin
check_header(s); { skip the .gz header }
s^.startpos := FilePos(s^.gzfile) - s^.stream.avail_in;
end;
gzopen := gzFile(s);
end;
function gzopen (path:string; mode:string) : gzFile;
begin
result := gzopen(path,mode,0);
end;
(*2015 function gzopen (path:string; mode:string) : gzFile; overload
var
i : cardinal;
err : integer;
level : integer; { compression level }
strategy : integer; { compression strategy }
s : gz_streamp;
{$ifdef UNIX}
info: stat;
{$else}
attr: word;
{$endif}
{$IFNDEF NO_DEFLATE}
gzheader : array [0..9] of byte;
{$ENDIF}
begin
if (path='') or (mode='') then begin
gzopen := nil;
exit;
end;
GetMem (s,sizeof(gz_stream));
if not Assigned (s) then begin
gzopen := nil;
exit;
end;
level := Z_DEFAULT_COMPRESSION;
strategy := Z_DEFAULT_STRATEGY;
s^.stream.next_in := nil;
s^.stream.next_out := nil;
s^.stream.avail_in := 0;
s^.stream.avail_out := 0;
s^.z_err := Z_OK;
s^.z_eof := false;
s^.inbuf := nil;
s^.outbuf := nil;
s^.crc := crc32(0, nil, 0);
s^.msg := '';
s^.transparent := false;
s^.path := path; { limit to 255 chars }
s^.mode := chr(0);
for i:=1 to Length(mode) do begin
case mode[i] of
'r' : s^.mode := 'r';
'w' : s^.mode := 'w';
'0'..'9' : level := Ord(mode[i])-Ord('0');
'f' : strategy := Z_FILTERED;
'h' : strategy := Z_HUFFMAN_ONLY;
end;
end;
if (s^.mode=chr(0)) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
if (s^.mode='w') then begin
{$IFDEF NO_DEFLATE}
err := Z_STREAM_ERROR;
{$ELSE}
err := deflateInit2 (s^.stream, level, Z_DEFLATED, -MAX_WBITS,
DEF_MEM_LEVEL, strategy);
{ windowBits is passed < 0 to suppress zlib header }
GetMem (s^.outbuf, Z_BUFSIZE);
s^.stream.next_out := s^.outbuf;
{$ENDIF}
if (err <> Z_OK) or (s^.outbuf = nil) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
end
else begin
GetMem (s^.inbuf, Z_BUFSIZE);
s^.stream.next_in := s^.inbuf;
err := inflateInit2_ (s^.stream, -MAX_WBITS, ZLIB_VERSION, sizeof(z_stream));
{ windowBits is passed < 0 to tell that there is no zlib header }
if (err <> Z_OK) or (s^.inbuf = nil) then begin
destroy(s);
gzopen := gzFile(nil);
exit;
end;
end;