1 {$INCLUDE ../shared/a_modes.inc}
5 // Implementation restrictions:
6 // - File must start with LFH or EOCD signature
7 // - EOCD must be located strictly at the end of file
8 // - Multi-disk ZIP files are not supported
9 // - Expect UTF-8 or CP1251 encoded names
10 // - ZIP64 not supported
11 // - Encryption not supported
12 // - Zero-length file names not supported
13 // - CDR holds most actual data about file, LFH mostly ignored
14 // - Attributes and extra data are ignored and not preserved
15 // - Store and Deflate compression supported
19 uses Classes
, WADEDITOR
;
32 stream
: TMemoryStream
;
39 list
: array of TResource
;
42 PResource
= ^TResource
;
45 TZIPEditor
= class sealed(WADEDITOR
.TWADEditor
)
47 FSection
: array of TSection
;
53 function FindSectionIDRAW(name
: AnsiString; caseSensitive
: Boolean): Integer;
54 function FindSectionRAW(name
: AnsiString; caseSensitive
: Boolean): PSection
;
55 function InsertSectionRAW(name
: AnsiString; mtime
: UInt32
; comment
: AnsiString): PSection
;
57 function FindSectionID(name
: AnsiString): Integer;
58 function FindSection(name
: AnsiString): PSection
;
59 function InsertSection(name
: AnsiString; mtime
: UInt32
; comment
: AnsiString): PSection
;
61 function InsertFileInfo(const section
, name
: AnsiString; pos
, csize
, usize
, comp
, crc
, mtime
, flags
: UInt32
; comment
: AnsiString): PResource
;
62 function Preload(p
: PResource
): Boolean;
63 function GetSourceStream(p
: PResource
): TStream
;
65 procedure ReadLFH(s
: TStream
; fname
, xcomment
: AnsiString; xcsize
, xusize
, xcomp
, xcrc
, xtime
, xflags
: UInt32
);
66 procedure ReadCDR(s
: TStream
; cdrid
: Integer);
67 function FindEOCD(s
: TStream
): Boolean;
68 procedure ReadEOCD(s
: TStream
);
70 procedure WriteLFH(s
: TStream
; flags
, comp
, mtime
, crc
, csize
, usize
: UInt32
; const name
: AnsiString);
71 procedure WriteCDR(s
: TStream
; flags
, comp
, mtime
, crc
, csize
, usize
, eattr
, offset
: UInt32
; const name
, com
: AnsiString; cdrid
: Integer);
72 procedure SaveToStream(s
: TStream
);
76 destructor Destroy(); override;
77 procedure FreeWAD(); override;
78 function ReadFile2(FileName
: string): Boolean; override;
79 function ReadMemory(Data
: Pointer; Len
: LongWord): Boolean; override;
80 procedure CreateImage(); override;
81 function AddResource(Data
: Pointer; Len
: LongWord; Name
, Section
: String): Boolean; override; overload
;
82 function AddResource(FileName
, Name
, Section
: String): Boolean; override; overload
;
83 function AddAlias(Res
, Alias
: String): Boolean; override;
84 procedure AddSection(Name
: String); override;
85 procedure RemoveResource(Section
, Resource
: String); override;
86 procedure SaveTo(FileName
: String); override;
87 function HaveResource(Section
, Resource
: String): Boolean; override;
88 function HaveSection(Section
: string): Boolean; override;
89 function GetResource(Section
, Resource
: String; var pData
: Pointer; var Len
: Integer): Boolean; override;
90 function GetSectionList(): SArray
; override;
91 function GetResourcesList(Section
: String): SArray
; override;
93 function GetLastError
: Integer; override;
94 function GetLastErrorStr
: String; override;
95 function GetResourcesCount
: Word; override;
96 function GetVersion
: Byte; override;
101 uses SysUtils
, StrUtils
, DateUtils
, Math
, utils
, zstream
, crc
, e_log
;
104 ZIP_SIGN_CDR
= 'PK'#1#2;
105 ZIP_SIGN_LFH
= 'PK'#3#4;
106 ZIP_SIGN_EOCD
= 'PK'#5#6;
111 ZIP_COMP_REDUCE1
= 2;
112 ZIP_COMP_REDUCE2
= 3;
113 ZIP_COMP_REDUCE3
= 4;
114 ZIP_COMP_REDUCE4
= 5;
115 ZIP_COMP_IMPLODE
= 6;
116 ZIP_COMP_TOKENIZED
= 7;
117 ZIP_COMP_DEFLATE
= 8;
118 ZIP_COMP_DEFLATE64
= 9;
119 ZIP_COMP_TERSE1
= 10;
123 ZIP_COMP_TERSE2
= 18;
130 ZIP_COMP_WAVPACK
= 97;
135 ZIP_SYSTEM
= 0; // DOS / FAT
136 ZIP_MAXVERSION
= 63; // Max supported version
139 ZIP_ENCRYPTION_MASK
= (1 << 0) or (1 << 6) or (1 << 13);
140 ZIP_COMP_MASK
= (1 << 1) or (1 << 2) or (1 << 4) or (1 << 12);
141 ZIP_DATA_MASK
= (1 << 3);
142 ZIP_PATCH_MASK
= (1 << 5);
143 ZIP_UTF8_MASK
= (1 << 11);
144 ZIP_STREAM_MASK
= (1 << 14);
146 function IsASCII(const s
: AnsiString): Boolean;
149 for i
:= 1 to Length(s
) do
160 function IsUTF8(const s
: AnsiString): Boolean;
161 var i
, j
, len
: Integer;
164 i
:= 1; len
:= Length(s
);
169 $80..$BF: exit
; // invalid encoding
173 otherwise exit
; // invalid encoding
178 if i
> len
then exit
; // invlid length
181 else exit
; // invalid encoding
190 function DosToStr(dostime
: UInt32
): AnsiString;
193 DateTimeToString(Result
, 'yyyy/mm/dd hh:nn:ss', DosDateTimeToDateTime(dostime
));
194 except on e
: EConvertError
do
195 Result
:= 'INVALID ($' + IntToHex(dostime
, 8) + ')';
199 procedure ToSectionFile(fname
: AnsiString; out section
, name
: AnsiString); inline;
202 i
:= LastDelimiter('/', fname
);
203 section
:= Copy(fname
, 1, i
- 1);
204 name
:= Copy(fname
, i
+ 1)
207 function GetFileName(const Section
, Name
: AnsiString): AnsiString; inline;
212 Result
:= Section
+ '/' + Name
;
215 function PrepString(const s
: AnsiString; caseSensitive
, extSensitive
: Boolean): AnsiString; inline;
219 if caseSensitive
= False then
221 Result
:= UpperCase(Result
);
223 if extSensitive
= False then
225 i
:= Pos('.', Result
); // fix dotfiles
227 SetLength(Result
, i
- 1);
231 function FindResourceIDRAW(p
: PSection
; name
: AnsiString; caseSensitive
, extSensitive
: Boolean): Integer;
232 var i
: Integer; pname
: AnsiString;
236 pname
:= PrepString(name
, caseSensitive
, extSensitive
);
237 for i
:= 0 to High(p
.list
) do
239 if PrepString(p
.list
[i
].name
, caseSensitive
, extSensitive
) = pname
then
249 function FindResourceID(p
: PSection
; name
: AnsiString): Integer;
252 i
:= FindResourceIDRAW(p
, name
, True, True); // CaSeNaMe.Ext
255 i
:= FindResourceIDRAW(p
, name
, False, True); // CASENAME.EXT
258 i
:= FindResourceIDRAW(p
, name
, True, False); // CaSeNaMe
261 i
:= FindResourceIDRAW(p
, name
, False, False); // CASENAME
268 function FindResource(p
: PSection
; name
: AnsiString): PResource
;
271 i
:= FindResourceID(p
, name
);
280 function TZIPEditor
.FindSectionIDRAW(name
: AnsiString; caseSensitive
: Boolean): Integer;
281 var i
: Integer; pname
: AnsiString;
283 if FSection
<> nil then
285 pname
:= PrepString(name
, caseSensitive
, True);
286 for i
:= 0 to High(FSection
) do
288 if PrepString(FSection
[i
].name
, caseSensitive
, True) = pname
then
298 function TZIPEditor
.FindSectionRAW(name
: AnsiString; caseSensitive
: Boolean): PSection
;
301 i
:= FindSectionIDRAW(name
, caseSensitive
);
303 Result
:= @FSection
[i
]
308 function TZIPEditor
.InsertSectionRAW(name
: AnsiString; mtime
: UInt32
; comment
: AnsiString): PSection
;
311 if FSection
= nil then i
:= 0 else i
:= Length(FSection
);
312 SetLength(FSection
, i
+ 1);
313 FSection
[i
] := Default(TSection
);
314 FSection
[i
].name
:= name
;
315 FSection
[i
].mtime
:= mtime
;
316 FSection
[i
].comment
:= comment
;
317 Result
:= @FSection
[i
];
322 function TZIPEditor
.FindSectionID(name
: AnsiString): Integer;
323 var fixName
: AnsiString;
325 fixName
:= StringReplace(name
, '\', '/', [rfReplaceAll
], TStringReplaceAlgorithm
.sraManySmall
);
326 Result
:= FindSectionIDRAW(fixName
, True); // CaSeNaMe
328 Result
:= FindSectionIDRAW(fixName
, False); // CASENAME
331 function TZIPEditor
.FindSection(name
: AnsiString): PSection
;
332 var fixName
: AnsiString;
334 fixName
:= StringReplace(name
, '\', '/', [rfReplaceAll
], TStringReplaceAlgorithm
.sraManySmall
);
335 Result
:= FindSectionRAW(fixName
, True); // CaSeNaMe
337 Result
:= FindSectionRAW(fixName
, False); // CASENAME
340 function TZIPEditor
.InsertSection(name
: AnsiString; mtime
: UInt32
; comment
: AnsiString): PSection
;
342 Result
:= FindSection(name
);
344 Result
:= InsertSectionRAW(name
, mtime
, comment
);
349 function TZIPEditor
.InsertFileInfo(const section
, name
: AnsiString; pos
, csize
, usize
, comp
, crc
, mtime
, flags
: UInt32
; comment
: AnsiString): PResource
;
350 var p
: PSection
; i
: Integer;
352 p
:= FindSectionRAW(section
, True);
354 p
:= InsertSectionRAW(section
, mtime
, '');
355 if p
.list
= nil then i
:= 0 else i
:= Length(p
.list
);
356 SetLength(p
.list
, i
+ 1);
357 p
.list
[i
] := Default(TResource
);
358 p
.list
[i
].name
:= name
;
359 p
.list
[i
].pos
:= pos
;
360 p
.list
[i
].csize
:= csize
;
361 p
.list
[i
].usize
:= usize
;
362 p
.list
[i
].comp
:= comp
;
363 p
.list
[i
].chksum
:= crc
;
364 p
.list
[i
].mtime
:= mtime
;
365 p
.list
[i
].flags
:= flags
;
366 p
.list
[i
].comment
:= comment
;
367 p
.list
[i
].stream
:= nil;
368 Result
:= @p
.list
[i
];
373 function TZIPEditor
.AddAlias(Res
, Alias
: String): Boolean;
375 // Hard-links not supported in ZIP
376 // However, they never created by editor
380 function TZIPEditor
.AddResource(Data
: Pointer; Len
: LongWord; Name
, Section
: String): Boolean;
381 const compress
: Boolean = True;
382 const level
: TCompressionLevel
= TCompressionLevel
.clMax
;
383 var s
: TMemoryStream
; cs
: TCompressionStream
; p
: PResource
;
384 var comp
, crc
: UInt32
;
386 Name
:= win2utf(Name
);
387 Section
:= win2utf(Section
);
391 s
:= TMemoryStream
.Create();
393 if compress
and (Len
> 0) then
395 cs
:= TCompressionStream
.Create(level
, s
, True);
397 cs
.WriteBuffer(PByte(Data
)[0], Len
);
399 comp
:= ZIP_COMP_DEFLATE
;
404 if (Len
= 0) or (compress
= False) or (s
.Size
>= Len
) then
406 s
.Seek(0, TSeekOrigin
.soBeginning
);
408 s
.WriteBuffer(PByte(Data
)[0], Len
);
409 comp
:= ZIP_COMP_STORE
;
410 Assert(s
.Size
= Len
);
412 crc
:= crc32(0, nil, 0);
413 crc
:= crc32(crc
, data
, len
);
414 p
:= InsertFileInfo(Section
, Name
, $ffffffff, s
.Size
, Len
, comp
, crc
, DateTimeToDosDateTime(Now()), 0, '');
424 function TZIPEditor
.AddResource(FileName
, Name
, Section
: String): Boolean;
425 var s
: TFileStream
; ptr
: PByte;
428 FLastError
:= DFWAD_ERROR_READWAD
;
430 s
:= TFileStream
.Create(FileName
, fmOpenRead
or fmShareDenyWrite
);
434 s
.ReadBuffer(ptr
[0], s
.Size
);
435 Result
:= AddResource(ptr
, s
.Size
, Name
, Section
);
436 if Result
= True then FLastError
:= DFWAD_NOERROR
;
446 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
447 e_WriteLog('DFZIP: AddResource: failed to open file ' + FileName
, MSG_NOTIFY
);
448 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
453 constructor TZIPEditor
.Create();
458 FLastError
:= DFWAD_NOERROR
;
463 destructor TZIPEditor
.Destroy();
469 procedure TZIPEditor
.FreeWAD();
472 if FSection
<> nil then
474 for i
:= 0 to High(FSection
) do
476 if FSection
[i
].list
<> nil then
478 for j
:= 0 to High(FSection
[i
].list
) do
480 if FSection
[i
].list
[j
].stream
<> nil then
482 FreeAndNil(FSection
[i
].list
[j
].stream
);
485 SetLength(FSection
[i
].list
, 0);
488 SetLength(FSection
, 0);
490 if FStream
<> nil then
495 FLastError
:= DFWAD_NOERROR
;
499 function TZIPEditor
.Preload(p
: PResource
): Boolean;
500 var s
: TMemoryStream
;
505 Result
:= p
.stream
<> nil;
506 if (p
.stream
= nil) and (FStream
<> nil) then
508 s
:= TMemoryStream
.Create();
512 FStream
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
513 s
.CopyFrom(FStream
, p
.csize
);
515 Assert(s
.Size
= p
.csize
); // wtf, random size if copied zero bytes!
525 procedure TZIPEditor
.CreateImage();
528 if FStream
= nil then
530 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
531 e_WriteLog('DFZIP: CreateImage: File not assigned', MSG_NOTIFY
);
532 FLastError
:= DFWAD_ERROR_WADNOTLOADED
;
534 else if FStream
is TMemoryStream
then
536 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
537 e_WriteLog('DFZIP: CreateImage: Memory stream', MSG_NOTIFY
);
538 FLastError
:= DFWAD_NOERROR
;
542 if FSection
<> nil then
544 for i
:= 0 to High(FSection
) do
546 if FSection
[i
].list
<> nil then
548 for j
:= 0 to High(FSection
[i
].list
) do
550 if Preload(@FSection
[i
].list
[j
]) = False then
552 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
553 e_WriteLog('DFZIP: CreateImage: failed to preload resource [' + FSection
[i
].name
+ '][' + FSection
[i
].list
[j
].name
+ ']', MSG_WARNING
);
554 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
562 FLastError
:= DFWAD_NOERROR
;
566 procedure TZIPEditor
.AddSection(Name
: String);
568 Name
:= win2utf(Name
);
569 if InsertSection(Name
, DateTimeToDosDateTime(Now()), '') = nil then
570 raise Exception
.Create('DFZIP: AddSection[' + Name
+ ']: failed to insert');
573 function TZIPEditor
.HaveResource(Section
, Resource
: String): Boolean;
575 Section
:= win2utf(Section
);
576 Resource
:= win2utf(Resource
);
577 Result
:= FindResource(FindSection(Section
), Resource
) <> nil;
580 function TZIPEditor
.HaveSection(Section
: String): Boolean;
582 Section
:= win2utf(Section
);
583 Result
:= FindSection(Section
) <> nil;
586 function TZIPEditor
.GetSourceStream(p
: PResource
): TStream
;
590 if p
.stream
<> nil then
593 src
.Seek(0, TSeekOrigin
.soBeginning
);
595 else if FStream
<> nil then
598 src
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
603 function TZIPEditor
.GetResource(Section
, Resource
: String; var pData
: Pointer; var Len
: Integer): Boolean;
604 var p
: PResource
; ptr
: PByte; src
: TStream
; tmp
: TDecompressionStream
; crc
: UInt32
;
606 Section
:= win2utf(Section
);
607 Resource
:= win2utf(Resource
);
608 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
612 p
:= FindResource(FindSection(Section
), Resource
);
615 src
:= GetSourceStream(p
);
621 Assert(p
.csize
= p
.usize
);
622 GetMem(ptr
, p
.usize
);
625 src
.ReadBuffer(ptr
[0], p
.usize
);
631 except on e
: EReadError
do
632 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
633 e_WriteLog('DFZIP: Failed to read STOREd data, reason: ' + e
.Message, MSG_WARNING
);
638 tmp
:= TDecompressionStream
.Create(src
, True);
640 GetMem(ptr
, p
.usize
);
642 tmp
.ReadBuffer(ptr
[0], p
.usize
);
652 on e
: EStreamError
do
654 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
655 e_WriteLog('DFZIP: Failed to decompress DEFLATEd data, reason: ' + e
.Message, MSG_WARNING
);
660 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
661 e_WriteLog('DFZIP: Unsupported compression method: ' + IntToStr(p
.comp
), MSG_WARNING
);
666 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
667 e_WriteLog('DFZIP: No available source for file data', MSG_WARNING
);
668 FLastError
:= DFWAD_ERROR_WADNOTLOADED
;
670 if Result
= True then
672 crc
:= crc32(0, nil, 0);
673 crc
:= crc32(crc
, ptr
, p
.usize
);
674 Result
:= crc
= p
.chksum
;
675 if Result
= True then
679 FLastError
:= DFWAD_NOERROR
;
683 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
684 e_WriteLog('DFZIP: File integrity check failed: expected CRC32 $' + IntToHex(p
.chksum
, 8) + ', calculated CRC32 $' + IntToHex(crc
, 8), MSG_WARNING
);
691 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
692 e_WriteLog('DFZIP: Resource not found', MSG_NOTIFY
);
693 FLastError
:= DFWAD_ERROR_RESOURCENOTFOUND
;
697 function TZIPEditor
.GetResourcesList(Section
: String): SArray
;
698 var p
: PSection
; i
: Integer;
700 Section
:= win2utf(Section
);
702 p
:= FindSection(Section
);
703 if (p
<> nil) and (p
.list
<> nil) then
705 SetLength(Result
, Length(p
.list
));
706 for i
:= 0 to High(p
.list
) do
708 Result
[i
] := utf2win(p
.list
[i
].name
);
713 function TZIPEditor
.GetSectionList(): SArray
;
717 if FSection
<> nil then
719 SetLength(Result
, Length(FSection
));
720 for i
:= 0 to High(FSection
) do
722 Result
[i
] := utf2win(FSection
[i
].name
);
727 procedure TZIPEditor
.ReadLFH(s
: TStream
; fname
, xcomment
: AnsiString; xcsize
, xusize
, xcomp
, xcrc
, xtime
, xflags
: UInt32
);
728 var sig
: packed array [0..3] of Char;
729 var va
, vb
, flags
, comp
: UInt16
;
730 var mtime
, crc
, csize
, usize
: UInt32
;
731 var fnlen
, extlen
: UInt16
;
732 var mypos
, datapos
: UInt64;
733 var section
, name
: AnsiString;
737 if mypos
+ 30 <= s
.Size
then
739 s
.ReadBuffer(sig
[0], 4);
740 if sig
= ZIP_SIGN_LFH
then
742 va
:= s
.ReadByte(); // Min Version
743 vb
:= s
.ReadByte(); // Min System
744 flags
:= LEtoN(s
.ReadWord());
745 comp
:= LEtoN(s
.ReadWord());
746 mtime
:= LEtoN(s
.ReadDWord());
747 crc
:= LEtoN(s
.ReadDWord());
748 csize
:= LEtoN(s
.ReadDWord());
749 usize
:= LEtoN(s
.ReadDWord());
750 fnlen
:= LEtoN(s
.ReadWord());
751 extlen
:= LEtoN(s
.ReadWord());
752 datapos
:= s
.Position
+ fnlen
+ extlen
;
753 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
755 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(va
), MSG_NOTIFY
);
756 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(vb
), MSG_NOTIFY
);
757 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
758 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
759 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
760 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
761 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
762 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
763 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
764 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(extlen
), MSG_NOTIFY
);
765 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': <DATA OFFSET> : $' + IntToHex(datapos
, 8), MSG_NOTIFY
);
767 if (va
>= 10) and (va
<= ZIP_MAXVERSION
) then
769 if datapos
+ xcsize
<= s
.Size
then
771 ToSectionFile(fname
, section
, name
);
774 p
:= FindSectionRAW(section
, True);
776 p
:= InsertSectionRAW(section
, xtime
, xcomment
);
780 p
:= InsertFileInfo(section
, name
, datapos
, xcsize
, xusize
, xcomp
, xcrc
, xtime
, xflags
and ZIP_COMP_MASK
, xcomment
);
783 raise Exception
.Create('Failed to register resource [' + fname
+ ']');
786 raise Exception
.Create('Invalid LFH size (corrupted file?)');
790 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
791 raise Exception
.Create('Unsupported CDR version ' + IntToStr(va
) + ', not in range [10..' + IntToStr(ZIP_MAXVERSION
) + ']');
795 raise Exception
.Create('Invalid LFH signature $' +IntToHex(Ord(sig
[0]), 2) + ' $' +IntToHex(Ord(sig
[1]), 2) + ' $' +IntToHex(Ord(sig
[2]), 2) + ' $' +IntToHex(Ord(sig
[3]), 2) + ' (corrupted file?)');
798 raise Exception
.Create('Invalid LFH size (corrupted file?)');
801 procedure TZIPEditor
.ReadCDR(s
: TStream
; cdrid
: Integer);
802 var sig
: packed array [0..3] of Char;
803 var vva
, vvb
, va
, vb
, flags
, comp
: UInt16
;
804 var mtime
, crc
, csize
, usize
: UInt32
;
805 var fnlen
, extlen
, comlen
, disk
, iattr
: UInt16
;
806 var eattr
, offset
: UInt32
;
807 var mypos
, next
: UInt64;
809 var name
, comment
: AnsiString;
810 var cvtbug
, utf8
: Boolean;
813 s
.ReadBuffer(sig
[0], 4);
814 if sig
= ZIP_SIGN_CDR
then
816 // Valid Central Directory Signature
817 vva
:= s
.ReadByte(); // Writer Version
818 vvb
:= s
.ReadByte(); // Writer System
819 va
:= s
.ReadByte(); // Min Version
820 vb
:= s
.ReadByte(); // Min System
821 flags
:= LEtoN(s
.ReadWord());
822 comp
:= LEtoN(s
.ReadWord());
823 mtime
:= LEtoN(s
.ReadDWord());
824 crc
:= LEtoN(s
.ReadDWord());
825 csize
:= LEtoN(s
.ReadDWord());
826 usize
:= LEtoN(s
.ReadDWord());
827 fnlen
:= LEtoN(s
.ReadWord());
828 extlen
:= LEtoN(s
.ReadWord());
829 comlen
:= LEtoN(s
.ReadWord());
830 disk
:= LEtoN(s
.ReadWord());
831 iattr
:= LEtoN(s
.ReadWord());
832 eattr
:= LEtoN(s
.ReadDWord());
833 offset
:= LEtoN(s
.ReadDWord());
834 next
:= s
.Position
+ fnlen
+ extlen
+ comlen
;
836 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
838 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer Version : ' + IntToStr(vva
), MSG_NOTIFY
);
839 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer System : ' + IntToStr(vvb
), MSG_NOTIFY
);
840 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(va
), MSG_NOTIFY
);
841 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(vb
), MSG_NOTIFY
);
842 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
843 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
844 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
845 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
846 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
847 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
848 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
849 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(extlen
), MSG_NOTIFY
);
850 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
851 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Disk : ' + IntToStr(disk
), MSG_NOTIFY
);
852 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Internal Attrib : $' + IntToHex(iattr
, 4), MSG_NOTIFY
);
853 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': External Attrib : $' + IntToHex(eattr
, 8), MSG_NOTIFY
);
854 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': LFH Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
857 if (vva
= $10) and (vvb
= $0A) and (va
= $10) and (vb
= $00) and (flags
= (1 << 10)) and (mtime
= 0) and (iattr
= 0) and (eattr
= 0) then
859 // HACK: Editor and wadcvt for long time sets incorrent flag for UTF-8
860 flags
:= ZIP_UTF8_MASK
;
863 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
864 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': WADCVT BUG : ' + BoolToStr(cvtbug
, True), MSG_NOTIFY
);
865 if (va
>= 10) and (va
<= ZIP_MAXVERSION
) then
867 if (flags
and ZIP_ENCRYPTION_MASK
) = 0 then
869 if (flags
and ZIP_PATCH_MASK
) = 0 then
871 if (csize
<> $ffffffff) and (usize
<> $ffffffff) and (disk
<> $ffff) and (offset
<> $ffffffff) then
875 if (next
<= s
.Size
) and (fnlen
> 0) then
879 if csize
<> usize
then
880 raise Exception
.Create('Compressed size ' + IntToStr(csize
) + ' != Descompressed size ' + IntToStr(usize
) + 'for STORE method (corrupted file?)');
904 raise Exception
.Create('Encrypted archives not supported');
906 raise Exception
.Create('Unknown compression method ' + IntToStr(comp
));
910 GetMem(tmp
, UInt32(fnlen
) + 1);
912 s
.ReadBuffer(tmp
[0], fnlen
);
918 // Skip ZIP extensions
919 s
.Seek(extlen
, TSeekOrigin
.soCurrent
);
924 GetMem(tmp
, UInt32(comlen
) + 1);
926 s
.ReadBuffer(tmp
[0], comlen
);
935 if (utf8
= False) or (flags
and ZIP_UTF8_MASK
= 0) and (IsUTF8(name
) = False) then
937 name
:= win2utf(name
);
940 if (utf8
= False) or (flags
and ZIP_UTF8_MASK
= 0) and (IsUTF8(comment
) = False) then
942 comment
:= win2utf(comment
);
945 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
947 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': UTF-8 Comatible : ' + BoolToStr(utf8
, True), MSG_NOTIFY
);
948 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name : "' + name
+ '"', MSG_NOTIFY
);
949 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment : "' + comment
+ '"', MSG_NOTIFY
);
951 s
.Seek(offset
, TSeekOrigin
.soBeginning
);
952 ReadLFH(s
, name
, comment
, csize
, usize
, comp
, crc
, mtime
, flags
);
953 s
.Seek(next
, TSeekOrigin
.soBeginning
);
956 raise Exception
.Create('Empty files names not supported');
959 raise Exception
.Create('Splitted archives not supported');
963 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
964 raise Exception
.Create('ZIP64 archives not supported');
969 FLastError
:= DFWAD_ERROR_READWAD
;
970 raise Exception
.Create('Patch archives not supported');
975 FLastError
:= DFWAD_ERROR_READWAD
;
976 raise Exception
.Create('Encrypted archives not supported');
981 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
982 raise Exception
.Create('Unsupported CDR version ' + IntToStr(va
) + ', not in range [10..' + IntToStr(ZIP_MAXVERSION
) + ']');
986 raise Exception
.Create('Invalid CDR signature $' + IntToHex(Ord(sig
[0]), 2) + ' $' +IntToHex(Ord(sig
[1]), 2) + ' $' +IntToHex(Ord(sig
[2]), 2) + ' $' +IntToHex(Ord(sig
[3]), 2) + ' (corrupted file?)');
989 function TZIPEditor
.FindEOCD(s
: TStream
): Boolean;
990 const maxedir
= 20; // end of central directory entry
991 const maxecdir
= maxedir
+ 65536; // + comment
992 var sig
: packed array [0..3] of Char; off
, lim
: Int64;
995 if s
.Size
>= maxedir
then
997 if s
.Size
< maxecdir
then lim
:= s
.Size
else lim
:= maxecdir
;
998 lim
:= lim
- maxedir
;
1000 while (off
<= lim
) and (Result
= False) do
1002 s
.Seek(s
.Size
- off
, TSeekOrigin
.soBeginning
);
1003 s
.ReadBuffer(sig
[0], 4);
1004 Result
:= sig
= ZIP_SIGN_EOCD
;
1010 procedure TZIPEditor
.ReadEOCD(s
: TStream
);
1011 var sig
: packed array [0..3] of Char;
1012 var idisk
, ndisk
, nrec
, total
, comlen
: UInt16
;
1013 var csize
, cpos
, i
: UInt32
;
1018 FLastError
:= DFWAD_ERROR_FILENOTWAD
;
1020 s
.ReadBuffer(sig
[0], 4);
1021 if (sig
= ZIP_SIGN_LFH
) or (sig
= ZIP_SIGN_EOCD
) then
1025 // End of Central Directory found
1026 FLastError
:= DFWAD_ERROR_READWAD
;
1027 mypos
:= s
.Position
- 4;
1028 idisk
:= LEtoN(s
.ReadWord());
1029 ndisk
:= LEtoN(s
.ReadWord());
1030 nrec
:= LEtoN(s
.ReadWord());
1031 total
:= LEtoN(s
.ReadWord());
1032 csize
:= LEtoN(s
.ReadDWord());
1033 cpos
:= LEtoN(s
.ReadDWord());
1034 comlen
:= LEtoN(s
.ReadWord());
1035 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1037 e_WriteLog('==============================================', MSG_NOTIFY
);
1038 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID : ' + IntToStr(idisk
), MSG_NOTIFY
);
1039 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID with CD : ' + IntToStr(ndisk
), MSG_NOTIFY
);
1040 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Available CDR''s : ' + IntToStr(nrec
), MSG_NOTIFY
);
1041 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Total CDR''s : ' + IntToStr(total
), MSG_NOTIFY
);
1042 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Length : ' + IntToStr(csize
), MSG_NOTIFY
);
1043 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Offset : $' + IntToHex(cpos
, 8), MSG_NOTIFY
);
1044 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
1046 if (idisk
<> $ffff) and (ndisk
<> $ffff) and (nrec
<> $ffff) and (total
<> $ffff) and (csize
<> $ffffffff) and (cpos
<> $ffffffff) then
1048 if s
.Position
+ comlen
= s
.Size
then
1050 if (idisk
= 0) and (ndisk
= 0) and (nrec
= total
) then
1052 if (nrec
* 46 <= csize
) and (UInt64(cpos
) + csize
<= s
.Size
) then
1059 GetMem(tmp
, UInt32(comlen
) + 1);
1061 s
.ReadBuffer(tmp
[0], comlen
);
1064 if IsUTF8(FComment
) = False then
1066 FComment
:= win2utf(FComment
);
1073 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1075 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': UTF8 Comaptible : ' + BoolToStr(utf8
, True), MSG_NOTIFY
);
1076 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment : "' + FComment
+ '"', MSG_NOTIFY
);
1079 s
.Seek(cpos
, TSeekOrigin
.soBeginning
);
1082 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1083 e_WriteLog('==============================================', MSG_NOTIFY
);
1087 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1088 e_WriteLog('==============================================', MSG_NOTIFY
);
1092 raise Exception
.Create('Central Directory too big (corrupted file?)');
1095 raise Exception
.Create('Splitted archives not supported');
1098 raise Exception
.Create('EOCD too big (corrupted file?)');
1101 raise Exception
.Create('ZIP64 not supported');
1104 raise Exception
.Create('EOCD not found (corrupted file?)');
1107 raise Exception
.Create('Not DFZIP formated file');
1110 function TZIPEditor
.ReadFile2(FileName
: String): Boolean;
1117 s
:= TFileStream
.Create(FileName
, fmOpenRead
or fmShareDenyWrite
);
1121 FLastError
:= DFWAD_NOERROR
;
1130 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1131 e_WriteLog('ZIP: Failed to read ZIP from file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1136 on e
: EFOpenError
do
1138 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1139 e_WriteLog('DFZIP: Failed to open file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1140 if FileExists(FileName
) then
1141 FLastError
:= DFWAD_ERROR_CANTOPENWAD
1143 FLastError
:= DFWAD_ERROR_WADNOTFOUND
;
1148 function TZIPEditor
.ReadMemory(Data
: Pointer; Len
: LongWord): Boolean;
1149 var s
: TMemoryStream
;
1154 s
:= TMemoryStream
.Create
;
1157 s
.WriteBuffer(PByte(Data
)[0], Len
);
1158 s
.Seek(0, soBeginning
);
1161 FLastError
:= DFWAD_NOERROR
;
1170 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1171 e_WriteLog('DFZIP: Failed to read ZIP from memory, reason: ' + e
.Message, MSG_WARNING
);
1177 procedure TZIPEditor
.RemoveResource(Section
, Resource
: String);
1178 var p
: PSection
; i
: Integer;
1180 Section
:= win2utf(Section
);
1181 Resource
:= win2utf(Resource
);
1182 p
:= FindSection(Section
);
1183 i
:= FindResourceID(p
, Resource
);
1186 if p
.list
[i
].stream
<> nil then
1187 FreeAndNil(p
.list
[i
].stream
);
1188 for i
:= i
+ 1 to High(p
.list
) do
1190 p
.list
[i
- 1] := p
.list
[i
];
1192 SetLength(p
.list
, High(p
.list
));
1196 function GetZIPVersion(const afname
: AnsiString; flags
, comp
: UInt16
): UInt8
;
1199 version
:= 10; // Base version
1201 ZIP_COMP_STORE
: version
:= 10;
1202 ZIP_COMP_SHRUNK
: version
:= 10;
1203 ZIP_COMP_REDUCE1
: version
:= 10;
1204 ZIP_COMP_REDUCE2
: version
:= 10;
1205 ZIP_COMP_REDUCE3
: version
:= 10;
1206 ZIP_COMP_REDUCE4
: version
:= 10;
1207 ZIP_COMP_IMPLODE
: version
:= 10;
1208 ZIP_COMP_TOKENIZED
: version
:= 20;
1209 ZIP_COMP_DEFLATE
: version
:= 20;
1210 ZIP_COMP_DEFLATE64
: version
:= 21;
1211 ZIP_COMP_TERSE1
: version
:= 25; // PKWARE DCL Implode
1212 ZIP_COMP_BZIP2
: version
:= 46;
1213 ZIP_COMP_LZMA
: version
:= 63;
1214 ZIP_COMP_CMPSC
: version
:= 63;
1215 ZIP_COMP_TERSE2
: version
:= 63;
1216 ZIP_COMP_LZ77
: version
:= 63;
1217 ZIP_COMP_ZSTD1
: version
:= 63;
1218 ZIP_COMP_ZSTD2
: version
:= 63;
1219 ZIP_COMP_MP3
: version
:= 63;
1220 ZIP_COMP_XZ
: version
:= 63;
1221 ZIP_COMP_JPEG
: version
:= 63;
1222 ZIP_COMP_WAVPACK
: version
:= 63;
1223 ZIP_COMP_PPMD
: version
:= 63;
1224 ZIP_COMP_AE
: version
:= 63;
1226 if afname
[Length(afname
)] = '/' then
1227 version
:= Max(20, version
); // Folder
1228 if flags
and ZIP_UTF8_MASK
<> 0 then
1229 version
:= Max(63, version
); // UTF-8 name
1233 procedure TZIPEditor
.WriteLFH(s
: TStream
; flags
, comp
, mtime
, crc
, csize
, usize
: UInt32
; const name
: AnsiString);
1234 var version
: UInt8
; fnlen
: UInt16
; mypos
: UInt64;
1236 mypos
:= s
.Position
;
1237 fnlen
:= Length(name
);
1238 if IsASCII(name
) = False then
1239 flags
:= flags
or ZIP_UTF8_MASK
;
1240 version
:= GetZIPVersion(name
, flags
, comp
);
1241 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1243 e_WriteLog('==============================================', MSG_NOTIFY
);
1244 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(version
), MSG_NOTIFY
);
1245 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1246 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
1247 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
1248 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
1249 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
1250 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
1251 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
1252 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
1253 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(0), MSG_NOTIFY
);
1254 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name : "' + name
+ '"', MSG_NOTIFY
);
1256 s
.WriteBuffer(ZIP_SIGN_LFH
, 4); // LFH Signature
1257 s
.WriteByte(version
); // Min version
1258 s
.WriteByte(ZIP_SYSTEM
); // System
1259 WriteInt(s
, UInt16(flags
)); // Flags
1260 WriteInt(s
, UInt16(comp
)); // Compression method
1261 WriteInt(s
, UInt32(mtime
)); // Modification time/date
1262 WriteInt(s
, UInt32(crc
)); // CRC-32
1263 WriteInt(s
, UInt32(csize
)); // Compressed size
1264 WriteInt(s
, UInt32(usize
)); // Decompressed size
1265 WriteInt(s
, UInt16(fnlen
)); // Name field length
1266 WriteInt(s
, UInt16(0)); // Extra field length
1267 s
.WriteBuffer(name
[1], fnlen
); // File Name
1270 procedure TZIPEditor
.WriteCDR(s
: TStream
; flags
, comp
, mtime
, crc
, csize
, usize
, eattr
, offset
: UInt32
; const name
, com
: AnsiString; cdrid
: Integer);
1271 var version
: UInt8
; fnlen
, fclen
: UInt16
; mypos
: UInt64;
1273 mypos
:= s
.Position
;
1274 fnlen
:= Length(name
);
1275 fclen
:= Length(com
);
1276 if (IsASCII(name
) = False) or (IsASCII(com
) = False) then
1277 flags
:= flags
or ZIP_UTF8_MASK
;
1278 version
:= GetZIPVersion(name
, flags
, comp
);
1279 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1281 e_WriteLog('==============================================', MSG_NOTIFY
);
1282 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer Version : ' + IntToStr(ZIP_MAXVERSION
), MSG_NOTIFY
);
1283 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1284 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(version
), MSG_NOTIFY
);
1285 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1286 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
1287 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
1288 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
1289 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
1290 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
1291 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
1292 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
1293 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(0), MSG_NOTIFY
);
1294 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(fclen
), MSG_NOTIFY
);
1295 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Disk : ' + IntToStr(0), MSG_NOTIFY
);
1296 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Internal Attrib : $' + IntToHex(0, 4), MSG_NOTIFY
);
1297 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': External Attrib : $' + IntToHex(eattr
, 8), MSG_NOTIFY
);
1298 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': LFH Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
1299 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name : "' + name
+ '"', MSG_NOTIFY
);
1300 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment : "' + com
+ '"', MSG_NOTIFY
);
1302 s
.WriteBuffer(ZIP_SIGN_CDR
, 4); // CDR Signature
1303 s
.WriteByte(ZIP_MAXVERSION
); // Used version
1304 s
.WriteByte(ZIP_SYSTEM
); // Used system
1305 s
.WriteByte(version
); // Min version
1306 s
.WriteByte(ZIP_SYSTEM
); // Min system
1307 WriteInt(s
, UInt16(flags
)); // Flags
1308 WriteInt(s
, UInt16(comp
)); // Compression method
1309 WriteInt(s
, UInt32(mtime
)); // Modification time/date
1310 WriteInt(s
, UInt32(crc
)); // CRC-32
1311 WriteInt(s
, UInt32(csize
)); // Compressed size
1312 WriteInt(s
, UInt32(usize
)); // Decompressed size
1313 WriteInt(s
, UInt16(fnlen
)); // Name field length
1314 WriteInt(s
, UInt16(0)); // Extra field length
1315 WriteInt(s
, UInt16(fclen
)); // Comment field length
1316 WriteInt(s
, UInt16(0)); // Disk
1317 WriteInt(s
, UInt16(0)); // Internal attributes
1318 WriteInt(s
, UInt32(eattr
)); // External attributes
1319 WriteInt(s
, UInt32(offset
)); // LFH offset
1320 s
.WriteBuffer(name
[1], fnlen
); // File Name
1321 s
.WriteBuffer(com
[1], fclen
); // Comment
1324 procedure TZIPEditor
.SaveToStream(s
: TStream
);
1326 var start
, offset
, loffset
, size
, zcrc
, count
, comlen
: UInt32
;
1328 var afname
: AnsiString;
1331 // Write LFH headers and data
1332 start
:= s
.Position
;
1333 zcrc
:= crc32(0, nil, 0);
1334 if FSection
<> nil then
1336 for i
:= 0 to High(FSection
) do
1338 if FSection
[i
].list
<> nil then
1340 for j
:= 0 to High(FSection
[i
].list
) do
1342 p
:= @FSection
[i
].list
[j
];
1343 afname
:= GetFileName(FSection
[i
].name
, p
.name
);
1344 WriteLFH(s
, p
.flags
, p
.comp
, p
.mtime
, p
.chksum
, p
.csize
, p
.usize
, afname
);
1345 if p
.stream
<> nil then
1347 Assert(p
.stream
.Size
= p
.csize
);
1348 p
.stream
.SaveToStream(s
);
1350 else if FStream
<> nil then
1352 FStream
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
1353 s
.CopyFrom(FStream
, p
.csize
);
1357 raise Exception
.Create('No data source available (somethig very wrong)');
1363 afname
:= GetFileName(FSection
[i
].name
, '');
1364 WriteLFH(s
, 0, ZIP_COMP_STORE
, FSection
[i
].mtime
, zcrc
, 0, 0, afname
);
1368 // Write CDR headers
1371 offset
:= s
.Position
- start
;
1372 if FSection
<> nil then
1374 for i
:= 0 to High(FSection
) do
1376 if FSection
[i
].list
<> nil then
1378 for j
:= 0 to High(FSection
[i
].list
) do
1380 p
:= @FSection
[i
].list
[j
];
1381 afname
:= GetFileName(FSection
[i
].name
, p
.name
);
1382 WriteCDR(s
, p
.flags
, p
.comp
, p
.mtime
, p
.chksum
, p
.csize
, p
.usize
, $00, loffset
, afname
, p
.comment
, i
);
1383 loffset
:= loffset
+ 30 + Length(afname
) + p
.csize
;
1389 afname
:= GetFileName(FSection
[i
].name
, '');
1390 WriteCDR(s
, 0, ZIP_COMP_STORE
, FSection
[i
].mtime
, zcrc
, 0, 0, $10, loffset
, afname
, FSection
[i
].comment
, i
);
1391 loffset
:= loffset
+ 30 + Length(afname
) + 0;
1396 Assert(loffset
= offset
);
1397 Assert(count
< $ffff);
1398 size
:= s
.Position
- start
- offset
;
1399 // Write EOCD header
1400 mypos
:= s
.Position
;
1401 comlen
:= Length(FComment
);
1402 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1404 e_WriteLog('==============================================', MSG_NOTIFY
);
1405 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID : ' + IntToStr(0), MSG_NOTIFY
);
1406 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID with CD : ' + IntToStr(0), MSG_NOTIFY
);
1407 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Available CDR''s : ' + IntToStr(count
), MSG_NOTIFY
);
1408 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Total CDR''s : ' + IntToStr(count
), MSG_NOTIFY
);
1409 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Length : ' + IntToStr(size
), MSG_NOTIFY
);
1410 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
1411 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
1412 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment : "' + FComment
+ '"', MSG_NOTIFY
);
1413 e_WriteLog('==============================================', MSG_NOTIFY
);
1415 s
.WriteBuffer(ZIP_SIGN_EOCD
, 4); // EOCD Signature
1416 WriteInt(s
, UInt16(0)); // Disk
1417 WriteInt(s
, UInt16(0)); // Num of Disks
1418 WriteInt(s
, UInt16(count
)); // Num of CDRs
1419 WriteInt(s
, UInt16(count
)); // Total CDR entries
1420 WriteInt(s
, UInt32(size
)); // Central Directory size
1421 WriteInt(s
, UInt32(offset
)); // Central Directory offset
1422 WriteInt(s
, UInt16(comlen
)); // Comment field length
1423 s
.WriteBuffer(FComment
[1], comlen
); // Comment
1426 procedure TZIPEditor
.SaveTo(FileName
: String);
1430 s
:= TFileStream
.Create(FileName
, fmCreate
);
1439 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1440 e_WriteLog('ZIP: Failed to create file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1446 function TZIPEditor
.GetLastError
: Integer;
1448 Result
:= FLastError
;
1451 function TZIPEditor
.GetLastErrorStr
: String;
1454 DFWAD_NOERROR
: Result
:= '';
1455 DFWAD_ERROR_WADNOTFOUND
: Result
:= 'DFZIP file not found';
1456 DFWAD_ERROR_CANTOPENWAD
: Result
:= 'Can''t open DFZIP file';
1457 DFWAD_ERROR_RESOURCENOTFOUND
: Result
:= 'Resource not found';
1458 DFWAD_ERROR_FILENOTWAD
: Result
:= 'File is not DFZIP';
1459 DFWAD_ERROR_WADNOTLOADED
: Result
:= 'DFZIP file is not loaded';
1460 DFWAD_ERROR_READRESOURCE
: Result
:= 'Read resource error';
1461 DFWAD_ERROR_READWAD
: Result
:= 'Read DFZIP error';
1462 otherwise Result
:= IntToStr(FLastError
);
1466 function TZIPEditor
.GetResourcesCount
: Word;
1470 if FSection
<> nil then
1472 Result
:= Result
+ Length(FSection
);
1473 for i
:= 0 to High(FSection
) do
1474 if FSection
[i
].list
<> nil then
1475 Result
:= Result
+ Length(FSection
[i
].list
);
1479 function TZIPEditor
.GetVersion
: Byte;
1485 gWADEditorFactory
.RegisterEditor('DFZIP', TZIPEditor
);