forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynZip.pas
5212 lines (4842 loc) · 170 KB
/
SynZip.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
/// low-level access to ZLib compression (1.2.5 engine version)
// - this unit is a part of the freeware Synopse framework,
// licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynZip;
{
This file is part of Synopse framework.
Synopse framework. Copyright (C) 2015 Arnaud Bouchez
Synopse Informatique - http://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is Synopse framework.
The Initial Developer of the Original Code is Arnaud Bouchez.
Portions created by the Initial Developer are Copyright (C) 2015
the Initial Developer. All Rights Reserved.
Contributor(s):
- Alf
- jpdk
- Gigo
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
ORIGINAL LICENSE:
zlib.h -- interface of the 'zlib' general purpose compression library
version 1.2.5, April 19th, 2010
Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors 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.
Jean-loup Gailly
Mark Adler
Cross-platform ZLib 1.2.5 implementation
==========================================
Link to original C-compiled ZLib library
- Win32: use fast obj and inline asm
- Linux: use available system library libz.so
Also defines .zip file structure (TFileInfo TFileHeader TLastHeader)
Version 1.3
- Delphi 2009/2010 compatibility (Unicode)
Version 1.3.1 - January 23, 2010
- issue corrected in CompressStream()
- compilation of TSynZipCompressor under Delphi 2009/2010, without any
Internal Error DT5830 (triggered with my Delphi 2009 Update 3)
Version 1.3.2 - February 5, 2010
- added .zip direct reading class
Version 1.4 - February 8, 2010
- whole Synopse SQLite3 database framework released under the GNU Lesser
General Public License version 3, instead of generic "Public Domain"
Version 1.5 - February 11, 2010
- added .zip direct writing class
Version 1.9
- crc32 is now coded in inlined fast asm (crc32.obj is no longer necessary)
- crc32 hashing is performed using 8 tables, for better CPU pipelining and
faster execution
- crc32 tables are created on the fly during unit initialization, therefore
save 8 KB of code size from standard crc32.obj, with no speed penalty
Version 1.9.2
- both obj files (i.e. deflate.obj and trees.obj) updated to version 1.2.5
Version 1.13
- code modifications to compile with Delphi 5 compiler
- new CompressGZip and CompressDeflate functions, for THttpSocket.RegisterCompress
- now handle Unicode file names UTF-8 encoded inside .Zip archive
- new TZipWrite.CreateFrom constructor, to add some new content to an
existing .Zip archive
- EventArchiveZip function can be used as a TSynLogArchiveEvent handler to
compress old .log files into a .zip standard archive
Version 1.15
- unit now tested with Delphi XE2 (32 Bit)
Version 1.16
- unit now compiles with Delphi XE2 (64 Bit)
- TZipWrite.AddDeflated(const aFileName) method will use streaming instead
of in-memory compression (will handle huge files much efficiently, e.g.
log files as for EventArchiveZip)
Version 1.18
- defined ZipString dedicated type, to store data in a Unicode-neutral manner
- introducing new TZipWriteToStream class, able to create a zip without file
- added TFileHeader.IsFolder and TLocalFileHeader.LocalData methods
- added TZipRead.UnZip() overloaded methods using a file name parameter
- added DestDirIsFileName optional parameter to TZipRead.UnZip() methods
- added TZipRead.UnZipAll() method
- fixed CompressDeflate() function, which was in fact creating zlib content
- fixed TZipWrite.AddDeflated() to handle data > 200 MB - thanks jpdk!
- fixed unexpected error when adding files e.g. via TZipWrite.CreateForm()
to an empty archive - thanks Gigo for the feedback!
- addded CompressZLib() function, as expected by web browsers
- any zip-related error will now raise a ESynZipException
- fixed ticket [2e22dd25aa] about TZipRead.UnMap
- fixed ticket [431b8b3dd9d] about gzread() overoptimistic assertion
- fixed UnZip() when crc and sizes are stored not within the file header,
but in a separate data descriptor block, after the compressed data (this
may occur e.g. if the .zip is created with latest Java JRE) - also added
corresponding TZipRead.RetrieveFileInfo() method and renamed TZipEntry
info field into infoLocal, and introduced infoDirectory new field
- renamed ZipFormat parameter to ZlibFormat, and introduce it also for
uncompression, so that both deflate and zlib layout are handled
- unit fixed and tested with Delphi XE2 (and up) 64-bit compiler
}
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64
{$ifdef FPC}
{$define USEZLIB}
{$ifdef MSWINDOWS} // avoid link to zlib1.dll (will use zlib.so under Linux)
{$define USEPASZLIB}
{$endif}
{$else}
{$ifdef Win32}
{$define USEINLINEASM}
// if defined, we use a special inlined asm version for uncompress:
// seems 50% faster than BC++ generated .obj, and is 3KB smaller in code size
{$else}
{$define USEZLIB} // e.g. for Kylix
{$endif}
{$endif}
interface
uses
SysUtils,
Classes,
{$ifdef USEZLIB}
{$ifdef USEPASZLIB}
zbase,
paszlib,
{$else}
ZLib,
{$endif}
{$endif}
{$ifdef MSWINDOWS}
Windows;
{$else}
{$ifdef KYLIX3}
LibC,
{$else}
{$ifndef ANDROID}
clocale,
{$endif}
{$endif}
Types;
{$endif}
type
/// the format used for storing data
TSynZipCompressorFormat = (szcfRaw, szcfZip, szcfGZ);
{$ifdef DELPHI5OROLDER}
type // Delphi 5 doesn't have those base types defined :(
PInteger = ^Integer;
PCardinal = ^Cardinal;
IntegerArray = array[0..$effffff] of Integer;
const
soCurrent = soFromCurrent;
{$endif}
/// in-memory ZLib DEFLATE compression
// - by default, will use the deflate/.zip header-less format, but you may set
// ZlibFormat=true to add an header, as expected by zlib (and pdf)
function CompressMem(src, dst: pointer; srcLen, dstLen: integer;
CompressionLevel: integer=6; ZlibFormat: Boolean=false) : integer;
/// in-memory ZLib INFLATE decompression
// - by default, will use the deflate/.zip header-less format, but you may set
// ZlibFormat=true to add an header, as expected by zlib (and pdf)
function UnCompressMem(src, dst: pointer; srcLen, dstLen: integer; ZlibFormat: Boolean=false) : integer;
/// ZLib DEFLATE compression from memory into a stream
// - by default, will use the deflate/.zip header-less format, but you may set
// ZlibFormat=true to add an header, as expected by zlib (and pdf)
function CompressStream(src: pointer; srcLen: integer;
aStream: TStream; CompressionLevel:integer=6; ZlibFormat: Boolean=false): cardinal;
/// ZLib INFLATE decompression from memory into a stream
// - return the number of bytes written into the stream
// - if checkCRC if not nil, it will contain thecrc32 (if aStream is nil, it will
// fast calculate the crc of the the uncompressed memory block)
// - by default, will use the deflate/.zip header-less format, but you may set
// ZlibFormat=true to add an header, as expected by zlib (and pdf)
function UnCompressStream(src: pointer; srcLen: integer; aStream: TStream;
checkCRC: PCardinal; ZlibFormat: Boolean=false): cardinal;
type
{$ifdef UNICODE}
/// define a raw storage string type, used for data buffer management
ZipString = type RawByteString;
{$else}
/// define a raw storage string type, used for data buffer management
ZipString = type AnsiString;
/// as available in newer Delphi versions
NativeUInt = cardinal;
{$endif}
/// compress some data, with a proprietary format (including CRC)
function CompressString(const data: ZipString; failIfGrow: boolean = false;
CompressionLevel: integer=6) : ZipString;
/// uncompress some data, with a proprietary format (including CRC)
// - return '' in case of a decompression failure
function UncompressString(const data: ZipString) : ZipString;
/// (un)compress a data content using the gzip algorithm
// - as expected by THttpSocket.RegisterCompress
// - will use internaly a level compression of 1, i.e. fastest available (content
// of 4803 bytes is compressed into 700, and time is 440 us instead of 220 us)
function CompressGZip(var DataRawByteString; Compress: boolean): AnsiString;
/// (un)compress a data content using the Deflate algorithm (i.e. "raw deflate")
// - as expected by THttpSocket.RegisterCompress
// - will use internaly a level compression of 1, i.e. fastest available (content
// of 4803 bytes is compressed into 700, and time is 440 us instead of 220 us)
// - deflate content encoding is pretty inconsistent in practice, so slightly
// slower CompressGZip() is preferred - http://stackoverflow.com/a/9186091/458259
function CompressDeflate(var DataRawByteString; Compress: boolean): AnsiString;
/// (un)compress a data content using the zlib algorithm
// - as expected by THttpSocket.RegisterCompress
// - will use internaly a level compression of 1, i.e. fastest available (content
// of 4803 bytes is compressed into 700, and time is 440 us instead of 220 us)
// - zlib content encoding is pretty inconsistent in practice, so slightly
// slower CompressGZip() is preferred - http://stackoverflow.com/a/9186091/458259
function CompressZLib(var DataRawByteString; Compress: boolean): AnsiString;
/// low-level check of the code returned by the ZLib library
function Check(const Code: Integer; const ValidCodes: array of Integer): integer;
type
PCardinalArray = ^TCardinalArray;
TCardinalArray = array[0..(MaxLongint div SizeOf(cardinal))-1] of cardinal;
/// just hash aString with CRC32 algorithm
// - crc32 is better than adler32 for short strings
function CRC32string(const aString: ZipString): cardinal;
// don't know why using objects below produce an Internal Error DT5830
// under Delphi 2009 Update 3 !!!!!
// -> see http://qc.embarcadero.com/wc/qcmain.aspx?d=79792
// it seems that this compiler doesn't like to compile packed objects,
// but all other versions (including Delphi 2009 Update 2) did
// -> do Codegear knows about regression tests?
type
/// exception raised internaly in case of Zip errors
ESynZipException = class(Exception);
/// the internal memory structure as expected by the ZLib library
{$ifdef USEPASZLIB}
TZStream = z_stream;
{$else}
{$ifdef USEZLIB}
{$ifdef KYLIX3}
TZStream = TZStreamRec;
{$else}
TZStream = z_stream;
{$endif}
{$else}
TZStream = record
next_in : PAnsiChar;
avail_in : cardinal;
total_in : cardinal;
next_out : PAnsiChar;
avail_out : cardinal;
total_out : cardinal;
msg : PAnsiChar;
state : pointer;
zalloc : pointer;
zfree : pointer;
opaque : pointer;
data_type: integer;
adler : cardinal;
reserved : cardinal;
end;
{$endif}
{$endif}
/// initialize the internal memory structure as expected by the ZLib library
procedure StreamInit(var Stream: TZStream);
/// prepare the internal memory structure as expected by the ZLib library for compression
function DeflateInit(var Stream: TZStream; CompressionLevel: integer; ZlibFormat: Boolean): Boolean;
type
{$A-} { force packed object (not allowed under Delphi 2009) }
PFileInfo = ^TFileInfo;
/// generic file information structure, as used in .zip file format
// - used in any header, contains info about following block
{$ifndef UNICODE}
TFileInfo = object
{$else}
TFileInfo = record
{$endif}
neededVersion : word; // $14
flags : word; // 0
zzipMethod : word; // 0=Z_STORED 8=Z_DEFLATED 12=BZ2 14=LZMA
zlastMod : integer; // time in dos format
zcrc32 : dword; // crc32 checksum of uncompressed data
zzipSize : dword; // size of compressed data
zfullSize : dword; // size of uncompressed data
nameLen : word; // length(name)
extraLen : word; // 0
function SameAs(aInfo: PFileInfo): boolean;
function AlgoID: integer; // 1..15 (1=SynLZ e.g.) from flags
procedure SetAlgoID(Algorithm: integer);
function GetUTF8FileName: boolean;
procedure SetUTF8FileName;
procedure UnSetUTF8FileName;
end;
/// directory file information structure, as used in .zip file format
// - used at the end of the zip file to recap all entries
TFileHeader = {$ifdef UNICODE}record{$else}object{$endif}
signature : dword; // $02014b50 PK#1#2
madeBy : word; // $14
fileInfo : TFileInfo;
commentLen : word; // 0
firstDiskNo : word; // 0
intFileAttr : word; // 0 = binary; 1 = text
extFileAttr : dword; // dos file attributes
localHeadOff : dword; // @TLocalFileHeader
function IsFolder: boolean; {$ifdef HASINLINE}inline;{$endif}
procedure Init;
end;
PFileHeader = ^TFileHeader;
/// internal file information structure, as used in .zip file format
// - used locally inside the file stream, followed by the name and then the data
TLocalFileHeader = {$ifdef UNICODE}record{$else}object{$endif}
signature : dword; // $04034b50 PK#3#4
fileInfo : TFileInfo;
function LocalData: PAnsiChar;
end;
PLocalFileHeader = ^TLocalFileHeader;
/// last header structure, as used in .zip file format
// - this header ends the file and is used to find the TFileHeader entries
TLastHeader = record
signature : dword; // $06054b50 PK#5#6
thisDisk : word; // 0
headerDisk : word; // 0
thisFiles : word; // 1
totalFiles : word; // 1
headerSize : dword; // sizeOf(TFileHeaders + names)
headerOffset : dword; // @TFileHeader
commentLen : word; // 0
end;
PLastHeader = ^TLastHeader;
{$A+}
{$ifdef linux}
const
ZLIB_VERSION = '1.2.5';
libz='libz.so';
type
gzFile = pointer;
const
Z_NO_FLUSH = 0;
Z_PARTIAL_FLUSH = 1;
Z_SYNC_FLUSH = 2;
Z_FULL_FLUSH = 3;
Z_FINISH = 4;
Z_OK = 0;
Z_STREAM_END = 1;
Z_NEED_DICT = 2;
Z_ERRNO = -(1);
Z_STREAM_ERROR = -(2);
Z_DATA_ERROR = -(3);
Z_MEM_ERROR = -(4);
Z_BUF_ERROR = -(5);
Z_VERSION_ERROR = -(6);
Z_NO_COMPRESSION = 0;
Z_BEST_SPEED = 1;
Z_BEST_COMPRESSION = 9;
Z_DEFAULT_COMPRESSION = -(1);
Z_FILTERED = 1;
Z_HUFFMAN_ONLY = 2;
Z_DEFAULT_STRATEGY = 0;
Z_BINARY = 0;
Z_ASCII = 1;
Z_UNKNOWN = 2;
Z_STORED = 0;
Z_DEFLATED = 8;
MAX_WBITS = 15; // 32K LZ77 window
DEF_MEM_LEVEL = 8;
Z_NULL = 0;
function zlibVersion: PAnsiChar; cdecl;
function deflate(var strm: TZStream; flush: integer): integer; cdecl;
function deflateEnd(var strm: TZStream): integer; cdecl;
function inflate(var strm: TZStream; flush: integer): integer; cdecl;
function inflateEnd(var strm: TZStream): integer; cdecl;
function deflateSetDictionary(var strm: TZStream;dictionary : PAnsiChar; dictLength: cardinal): integer; cdecl;
function deflateCopy(var dest,source: TZStream): integer; cdecl;
function deflateReset(var strm: TZStream): integer; cdecl;
function deflateParams(var strm: TZStream; level: integer; strategy: integer): integer; cdecl;
function inflateSetDictionary(var strm: TZStream;dictionary : PAnsiChar; dictLength: cardinal): integer; cdecl;
function inflateSync(var strm: TZStream): integer; cdecl;
function inflateReset(var strm: TZStream): integer; cdecl;
function compress(dest: PAnsiChar;var destLen:cardinal; source : PAnsiChar; sourceLen:cardinal):integer; cdecl;
function compress2(dest: PAnsiChar;destLen:pcardinal; source : PAnsiChar; sourceLen:cardinal; level:integer):integer; cdecl;
function uncompress(dest: PAnsiChar;destLen:pcardinal; source : PAnsiChar; sourceLen:cardinal):integer; cdecl;
{
function gzopen(path: PAnsiChar; mode: PAnsiChar): gzFile; cdecl;
function gzdopen(fd: integer; mode: PAnsiChar): gzFile; cdecl;
function gzsetparams(thefile: gzFile; level: integer; strategy: integer): integer; cdecl;
function gzread(thefile: gzFile; buf: pointer; len:cardinal): integer; cdecl;
function gzwrite(thefile: gzFile; buf: pointer; len:cardinal): integer; cdecl;
function gzprintf(thefile: gzFile; format: PAnsiChar; args:array of const): integer; cdecl;
function gzputs(thefile: gzFile; s: PAnsiChar): integer; cdecl;
function gzgets(thefile: gzFile; buf: PAnsiChar; len: integer): PAnsiChar; cdecl;
function gzputc(thefile: gzFile; c:AnsiChar):AnsiChar; cdecl;
function gzgetc(thefile: gzFile):AnsiChar; cdecl;
function gzflush(thefile: gzFile; flush: integer): integer; cdecl;
function gzseek(thefile: gzFile; offset:integer; whence: integer):integer; cdecl;
function gzrewind(thefile: gzFile): integer; cdecl;
function gztell(thefile: gzFile):integer; cdecl;
function gzeof(thefile: gzFile):longbool; cdecl;
function gzclose(thefile: gzFile): integer; cdecl;
function gzerror(thefile: gzFile; var errnum: integer): PAnsiChar; cdecl;
}
function adler32(adler:cardinal; buf: PAnsiChar; len: cardinal): cardinal; cdecl;
function crc32(crc:cardinal; buf: PAnsiChar; len: cardinal): cardinal; cdecl;
function deflateInit_(var strm: TZStream; level: integer; version: PAnsiChar; stream_size: integer): integer; cdecl;
function inflateInit_(var strm: TZStream; version: PAnsiChar; stream_size: integer): integer; cdecl;
function deflateInit2_(var strm: TZStream; level: integer; method: integer; windowBits: integer; memLevel: integer;strategy: integer; version: PAnsiChar; stream_size: integer): integer; cdecl;
function inflateInit2_(var strm: TZStream; windowBits: integer; version: PAnsiChar; stream_size: integer): integer; cdecl;
function get_crc_table: pointer; cdecl;
function zlibAllocMem(AppData: Pointer; Items, Size: cardinal): Pointer; cdecl;
procedure zlibFreeMem(AppData, Block: Pointer); cdecl;
{$else}
{ our very own short implementation of ZLibH }
{$ifdef USEZLIB}
function ZLIB_VERSION: PAnsiChar;
{$else}
const
ZLIB_VERSION = '1.2.5';
{$endif}
const
Z_NO_FLUSH = 0;
Z_PARTIAL_FLUSH = 1;
Z_SYNC_FLUSH = 2;
Z_FULL_FLUSH = 3;
Z_FINISH = 4;
Z_OK = 0;
Z_STREAM_END = 1;
Z_MEM_ERROR = -(4);
Z_BUF_ERROR = -(5);
Z_STORED = 0;
Z_DEFLATED = 8;
MAX_WBITS = 15; // 32K LZ77 window
DEF_MEM_LEVEL = 8;
Z_DEFAULT_STRATEGY = 0;
Z_HUFFMAN_ONLY = 2;
function deflateInit2_(var strm: TZStream; level: integer; method: integer; windowBits: integer; memLevel: integer;strategy: integer; version: PAnsiChar; stream_size: integer): integer;
function deflate(var strm: TZStream; flush: integer): integer;
function deflateEnd(var strm: TZStream): integer;
function inflateInit2_(var strm: TZStream; windowBits: integer; version: PAnsiChar; stream_size: integer): integer;
{$ifdef USEINLINEASM}stdcall;{$endif}
function inflate(var strm: TZStream; flush: integer): integer;
{$ifdef USEINLINEASM}stdcall;{$endif}
function inflateEnd(var strm: TZStream): integer;
{$ifdef USEINLINEASM}stdcall;{$endif}
function adler32(adler: cardinal; buf: PAnsiChar; len: cardinal): cardinal;
function crc32(crc: cardinal; buf: PAnsiChar; len: cardinal): cardinal;
function get_crc_table: pointer;
{$endif Linux}
/// uncompress a .gz file content
// - return '' if the .gz content is invalid (e.g. bad crc)
function GZRead(gz: PAnsiChar; gzLen: integer): ZipString;
type
/// a simple TStream descendant for compressing data into a stream
// - this simple version don't use any internal buffer, but rely
// on Zip library buffering system
// - the version in SynZipFiles is much more powerfull, but this one
// is sufficient for most common cases (e.g. for on the fly .gz backup)
TSynZipCompressor = class(TStream)
private
fInitialized: Boolean;
fDestStream: TStream;
fStrm: TZStream;
fCRC: Cardinal;
fGZFormat: boolean;
fBufferOut: array[word] of byte; // a 64 KB buffer
function FlushBufferOut: integer;
public
/// create a compression stream, writting the compressed data into
// the specified stream (e.g. a file stream)
constructor Create(outStream: TStream; CompressionLevel: Integer;
Format: TSynZipCompressorFormat = szcfRaw);
/// release memory
destructor Destroy; override;
/// this method will raise an error: it's a compression-only stream
function Read(var Buffer; Count: Longint): Longint; override;
/// add some data to be compressed
function Write(const Buffer; Count: Longint): Longint; override;
/// used to return the current position, i.e. the real byte written count
// - for real seek, this method will raise an error: it's a compression-only stream
function Seek(Offset: Longint; Origin: Word): Longint; override;
/// the number of byte written, i.e. the current uncompressed size
function SizeIn: cardinal;
/// the number of byte sent to the destination stream, i.e. the current
// compressed size
function SizeOut: cardinal;
/// write all pending compressed data into outStream
procedure Flush;
/// the current CRC of the written data, i.e. the uncompressed data CRC
property CRC: cardinal read fCRC;
end;
/// stores an entry of a file inside a .zip archive
TZipEntry = record
/// the information of this file, as stored locally in the .zip archive
// - note that infoLocal^.zzipSize/zfullSize/zcrc32 may be 0 if the info
// was stored in a "data descriptor" block after the data: in this case,
// you should use TZipRead.RetrieveFileInfo() instead of this structure
infoLocal: PFileInfo;
/// the information of this file, as stored at the end of the .zip archive
// - may differ from infoLocal^ content, depending of the zipper tool used
infoDirectory: PFileHeader;
/// points to the compressed data in the .zip archive, mapped in memory
data: PAnsiChar;
/// name of the file inside the .zip archive
// - not ASCIIZ: length = infoLocal.nameLen
storedName: PAnsiChar;
/// name of the file inside the .zip archive
// - converted from DOS/OEM or UTF-8 into generic (Unicode) string
zipName: TFileName;
end;
{$ifndef LINUX}
/// read-only access to a .zip archive file
// - can open directly a specified .zip file (will be memory mapped for fast access)
// - can open a .zip archive file content from a resource (embedded in the executable)
// - can open a .zip archive file content from memory
TZipRead = class
private
file_, map: NativeUInt; // we use a memory mapped file to access the zip content
buf: PByteArray;
FirstFileHeader: PFileHeader;
ReadOffset: cardinal;
procedure UnMap;
public
/// the number of files inside a .zip archive
Count: integer;
/// the files inside the .zip archive
Entry: array of TZipEntry;
/// open a .zip archive file as Read Only
constructor Create(const aFileName: TFileName; ZipStartOffset: cardinal=0;
Size: cardinal=0); overload;
/// open a .zip archive file directly from a resource
constructor Create(Instance: THandle; const ResName: string; ResType: PChar); overload;
/// open a .zip archive file from its File Handle
constructor Create(aFile: THandle; ZipStartOffset: cardinal=0;
Size: cardinal=0); overload;
/// open a .zip archive file directly from memory
constructor Create(BufZip: pByteArray; Size: cardinal); overload;
/// release associated memory
destructor Destroy; override;
/// get the index of a file inside the .zip archive
function NameToIndex(const aName: TFileName): integer;
/// uncompress a file stored inside the .zip archive into memory
function UnZip(aIndex: integer): ZipString; overload;
/// uncompress a file stored inside the .zip archive into a destination directory
function UnZip(aIndex: integer; const DestDir: TFileName;
DestDirIsFileName: boolean=false): boolean; overload;
/// uncompress a file stored inside the .zip archive into memory
function UnZip(const aName: TFileName): ZipString; overload;
/// uncompress a file stored inside the .zip archive into a destination directory
function UnZip(const aName, DestDir: TFileName;
DestDirIsFileName: boolean=false): boolean; overload;
/// uncompress all fields stored inside the .zip archive into the supplied
// destination directory
// - returns -1 on success, or the index in Entry[] of the failing file
function UnZipAll(DestDir: TFileName): integer;
/// retrieve information about a file
// - in some cases (e.g. for a .zip created by latest Java JRE),
// infoLocal^.zzipSize/zfullSize/zcrc32 may equal 0: this method is able
// to retrieve the information either from the ending "central directory",
// or by searching the "data descriptor" block
// - returns TRUE if the Index is correct and the info was retrieved
// - returns FALSE if the information was not successfully retrieved
function RetrieveFileInfo(Index: integer; var Info: TFileInfo): boolean;
end;
{$endif Linux}
/// abstract write-only access for creating a .zip archive
TZipWriteAbstract = class
protected
fAppendOffset: cardinal;
fMagic: cardinal;
function InternalAdd(const zipName: TFileName; Buf: pointer; Size: integer): cardinal;
function InternalWritePosition: cardinal; virtual; abstract;
procedure InternalWrite(const buf; len: cardinal); virtual; abstract;
public
/// the total number of entries
Count: integer;
/// the resulting file entries, ready to be written as a .zip catalog
// - those will be appended after the data blocks at the end of the .zip file
Entry: array of record
/// the file name, as stored in the .zip internal directory
intName: ZipString;
/// the corresponding file header
fhr: TFileHeader;
end;
/// initialize the .zip archive
// - a new .zip file content is prepared
constructor Create;
/// compress (using the deflate method) a memory buffer, and add it to the zip file
// - by default, the 1st of January, 2010 is used if not date is supplied
procedure AddDeflated(const aZipName: TFileName; Buf: pointer; Size: integer;
CompressLevel: integer=6; FileAge: integer=1+1 shl 5+30 shl 9); overload;
/// add a memory buffer to the zip file, without compression
// - content is stored, not deflated
// (in that case, no deflate code is added to the executable)
// - by default, the 1st of January, 2010 is used if not date is supplied
procedure AddStored(const aZipName: TFileName; Buf: pointer; Size: integer;
FileAge: integer=1+1 shl 5+30 shl 9);
/// append a file content into the destination file
// - useful to add the initial Setup.exe file, e.g.
procedure Append(const Content: ZipString);
/// release associated memory, and close destination archive
destructor Destroy; override;
end;
/// write-only access for creating a .zip archive file
// - not to be used to update a .zip file, but to create a new one
// - update can be done manualy by using a TZipRead instance and the
// AddFromZip() method
TZipWrite = class(TZipWriteAbstract)
protected
fFileName: TFileName;
function InternalWritePosition: cardinal; override;
procedure InternalWrite(const buf; len: cardinal); override;
public
/// the associated file handle
Handle: integer;
/// initialize the .zip file
// - a new .zip file content is created
constructor Create(const aFileName: TFileName); overload;
/// initialize an existing .zip file in order to add some content to it
// - warning: AddStored/AddDeflated() won't check for duplicate zip entries
// - this method is very fast, and will increase the .zip file in-place
// (the old content is not copied, new data is appended at the file end)
{$ifndef Linux}
constructor CreateFrom(const aFileName: TFileName);
{$endif}
/// compress (using the deflate method) a file, and add it to the zip file
procedure AddDeflated(const aFileName: TFileName; RemovePath: boolean=true;
CompressLevel: integer=6); overload;
/// add a file from an already compressed zip entry
procedure AddFromZip(const ZipEntry: TZipEntry);
/// release associated memory, and close destination file
destructor Destroy; override;
end;
/// write-only access for creating a .zip archive into a stream
TZipWriteToStream = class(TZipWriteAbstract)
protected
fDest: TStream;
function InternalWritePosition: cardinal; override;
procedure InternalWrite(const buf; len: cardinal); override;
public
/// initialize the .zip archive
// - a new .zip file content is prepared
constructor Create(aDest: TStream);
end;
/// a TSynLogArchiveEvent handler which will compress older .log files
// into .zip archive files
// - resulting file will be named YYYYMM.zip and will be located in the
// aDestinationPath directory, i.e. TSynLogFamily.ArchivePath+'\log\YYYYMM.zip'
{$ifndef Linux}
function EventArchiveZip(const aOldLogFileName, aDestinationPath: TFileName): boolean;
{$endif}
implementation
{$ifdef Linux}
uses
{$ifdef FPC}
SynFPCLinux,
BaseUnix;
{$else}
SynKylix;
{$endif}
{$endif Linux}
const
// those constants have +1 to avoid finding it in the exe
FIRSTHEADER_SIGNATURE_INC = $04034b50+1; // PK#3#4
LASTHEADER_SIGNATURE_INC = $06054b50+1; // PK#5#6
ENTRY_SIGNATURE_INC = $02014b50+1; // PK#1#2
{ TZipWrite }
var
EventArchiveZipWrite: TZipWrite = nil;
{$ifndef Linux}
function EventArchiveZip(const aOldLogFileName, aDestinationPath: TFileName): boolean;
var n: integer;
begin
result := false;
if aOldLogFileName='' then
FreeAndNil(EventArchiveZipWrite) else begin
if not FileExists(aOldLogFileName) then
exit;
if EventArchiveZipWrite=nil then
EventArchiveZipWrite := TZipWrite.CreateFrom(
system.copy(aDestinationPath,1,length(aDestinationPath)-1)+'.zip');
n := EventArchiveZipWrite.Count;
EventArchiveZipWrite.AddDeflated(aOldLogFileName,True);
if (EventArchiveZipWrite.Count=n+1) and DeleteFile({$ifndef Linux}pointer{$endif}(aOldLogFileName)) then
result := True;
end;
end;
{$endif}
function Is7BitAnsi(P: PChar): boolean;
begin
if P<>nil then
while true do
if ord(P^)=0 then
break else
if ord(P^)<=126 then
inc(P) else begin
result := false;
exit;
end;
result := true;
end;
{ TZipWriteAbstract }
constructor TZipWriteAbstract.Create;
begin
fMagic := FIRSTHEADER_SIGNATURE_INC; // +1 to avoid finding it in the exe generated code
dec(fMagic);
end;
function TZipWriteAbstract.InternalAdd(const zipName: TFileName; Buf: pointer; Size: integer): cardinal;
begin
with Entry[Count] do begin
fHr.signature := ENTRY_SIGNATURE_INC; // +1 to avoid finding it in the exe
dec(fHr.signature);
fHr.madeBy := $14;
fHr.fileInfo.neededVersion := $14;
result := InternalWritePosition;
fHr.localHeadOff := result-fAppendOffset;
{$ifndef DELPHI5OROLDER}
// Delphi 5 doesn't have UTF8Decode/UTF8Encode functions -> make 7 bit version
if Is7BitAnsi(pointer(zipName)) then begin
{$endif}
{$ifdef UNICODE}
intName := AnsiString(zipName);
{$else} // intName := zipName -> error reference count under Delphi 6
SetString(intName,PAnsiChar(pointer(zipName)),length(zipName));
{$endif}
fHr.fileInfo.UnSetUTF8FileName;
{$ifndef DELPHI5OROLDER}
end else begin
intName := UTF8Encode(WideString(zipName));
fHr.fileInfo.SetUTF8FileName;
end;
{$endif}
fHr.fileInfo.nameLen := length(intName);
InternalWrite(fMagic,sizeof(fMagic));
InternalWrite(fhr.fileInfo,sizeof(fhr.fileInfo));
InternalWrite(pointer(intName)^,fhr.fileInfo.nameLen);
end;
if Buf<>nil then begin
InternalWrite(Buf^,Size); // write stored data
inc(Count);
end;
end;
procedure TZipWriteAbstract.AddDeflated(const aZipName: TFileName; Buf: pointer;
Size, CompressLevel, FileAge: integer);
var tmp: pointer;
tmpsize: integer;
begin
if self=nil then
exit;
if Count>=length(Entry) then
SetLength(Entry,length(Entry)+20);
with Entry[Count] do begin
with fhr.fileInfo do begin
zcrc32 := SynZip.crc32(0,Buf,Size);
zfullSize := Size;
zzipMethod := Z_DEFLATED;
zlastMod := FileAge;
tmpsize := (Int64(Size)*11) div 10+12;
Getmem(tmp,tmpSize);
zzipSize := CompressMem(Buf,tmp,Size,tmpSize,CompressLevel);
InternalAdd(aZipName,tmp,zzipSize); // write stored data
Freemem(tmp);
end;
end;
end;
procedure TZipWriteAbstract.AddStored(const aZipName: TFileName; Buf: pointer;
Size, FileAge: integer);
begin
if self=nil then
exit;
if Count>=length(Entry) then
SetLength(Entry,length(Entry)+20);
with Entry[Count], fhr.fileInfo do begin
zcrc32 := SynZip.crc32(0,Buf,Size);
zfullSize := Size;
zzipSize := Size;
zlastMod := FileAge;
InternalAdd(aZipName,Buf,Size);
end;
end;
procedure TZipWriteAbstract.Append(const Content: ZipString);
begin
if (self=nil) or (fAppendOffset<>0) then
exit;
fAppendOffset := length(Content);
InternalWrite(pointer(Content)^,fAppendOffset);
end;
destructor TZipWriteAbstract.Destroy;
var lhr: TLastHeader;
i: integer;
begin
fillchar(lhr,sizeof(lhr),0);
lhr.signature := LASTHEADER_SIGNATURE_INC;
dec(lhr.signature); // +1 to avoid finding it in the exe
lhr.thisFiles := Count;
lhr.totalFiles := Count;
lhr.headerOffset := InternalWritePosition-fAppendOffset;
for i := 0 to Count-1 do
with Entry[i] do begin
assert(fhr.fileInfo.nameLen=length(intName));
inc(lhr.headerSize,sizeof(TFileHeader)+fhr.fileInfo.nameLen);
InternalWrite(fhr,sizeof(fhr));
InternalWrite(pointer(IntName)^,fhr.fileInfo.nameLen);
end;
InternalWrite(lhr,sizeof(lhr));
inherited;
end;
{ TZipWrite }
function TZipWrite.InternalWritePosition: cardinal;
begin
result := SetFilePointer(Handle,0,nil,{$ifdef Linux}SEEK_CUR{$else}FILE_CURRENT{$endif});
end;
procedure TZipWrite.InternalWrite(const buf; len: cardinal);
begin
FileWrite(Handle,buf,len);
end;
procedure TZipWrite.AddDeflated(const aFileName: TFileName; RemovePath: boolean=true;
CompressLevel: integer=6);
var {$ifndef Linux}
Time: TFileTime;
FileTime: LongRec;
{$endif}
ZipName: TFileName;
Size: Int64;
Size64: Int64Rec absolute Size;
OffsHead, OffsEnd: cardinal;
S: TFileStream;
D: THandleStream;
Z: TSynZipCompressor;
begin
S := TFileStream.Create(aFileName,fmOpenRead or fmShareDenyNone);
try
if RemovePath then
ZipName := ExtractFileName(aFileName) else
ZipName := aFileName;
{$ifndef Linux}
GetFileTime(S.Handle,nil,nil,@Time);
FileTimeToLocalFileTime(Time,Time);
FileTimeToDosDateTime(Time,FileTime.Hi,FileTime.Lo);
{$endif}
Size := S.Size;
if Size64.Hi<>0 then
raise ESynZipException.CreateFmt('%s file too big for .zip',[aFileName]);
if Count>=length(Entry) then
SetLength(Entry,length(Entry)+20);
OffsHead := InternalAdd(ZipName,nil,0);
D := THandleStream.Create(Handle);
Z := TSynZipCompressor.Create(D,CompressLevel);
try
Z.CopyFrom(S,Size64.Lo);
Z.Flush;
assert(Z.SizeIn=Size64.Lo);
with Entry[Count] do begin
with fhr.fileInfo do begin
zcrc32 := Z.CRC;
zfullSize := Z.SizeIn;
zzipSize := Z.SizeOut;
zzipMethod := Z_DEFLATED;
{$ifndef Linux}
zlastMod := integer(FileTime);
{$else}
zlastMod := FileAge(ZipName);
{$endif}
end;
OffsEnd := D.Position;
D.Position := OffsHead+sizeof(fMagic);