X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2FWADEDITOR_dfzip.pas;h=f250e2c0a05c8fc3fe1a2ce98b9debc9092c0392;hb=fbaef03a576a16fa918090c5a7556132ee2635c8;hp=f9e54ad57f88f597a6a56fdf8e0fac7b8b1f4136;hpb=107de9653cd3649b79ee0a1bda085c37e3154a96;p=d2df-editor.git diff --git a/src/shared/WADEDITOR_dfzip.pas b/src/shared/WADEDITOR_dfzip.pas index f9e54ad..f250e2c 100644 --- a/src/shared/WADEDITOR_dfzip.pas +++ b/src/shared/WADEDITOR_dfzip.pas @@ -26,11 +26,14 @@ interface usize: UInt32; comp: UInt32; chksum: UInt32; + mtime: UInt32; + flags: UInt32; stream: TMemoryStream; end; TSection = record name: AnsiString; + mtime: UInt32; list: array of TResource; end; @@ -46,23 +49,23 @@ interface function FindSectionIDRAW(name: AnsiString; caseSensitive: Boolean): Integer; function FindSectionRAW(name: AnsiString; caseSensitive: Boolean): PSection; - function InsertSectionRAW(name: AnsiString): PSection; + function InsertSectionRAW(name: AnsiString; mtime: UInt32): PSection; function FindSectionID(name: AnsiString): Integer; function FindSection(name: AnsiString): PSection; - function InsertSection(name: AnsiString): PSection; + function InsertSection(name: AnsiString; mtime: UInt32): PSection; - function InsertFileInfo(const section, name: AnsiString; pos, csize, usize, comp, crc: UInt32): PResource; + function InsertFileInfo(const section, name: AnsiString; pos, csize, usize, comp, crc, mtime, flags: UInt32): PResource; function Preload(p: PResource): Boolean; function GetSourceStream(p: PResource): TStream; - procedure ReadLFH(s: TStream; fname: AnsiString; xcsize, xusize, xcomp, xcrc: UInt32); + procedure ReadLFH(s: TStream; fname: AnsiString; xcsize, xusize, xcomp, xcrc, xtime, xflags: UInt32); procedure ReadCDR(s: TStream; cdrid: Integer); function FindEOCD(s: TStream): Boolean; procedure ReadEOCD(s: TStream); - procedure WriteLFH(s: TStream; comp, crc, csize, usize: UInt32; const afname: AnsiString); - procedure WriteCDR(s: TStream; comp, crc, csize, usize, eattr, offset: UInt32; const afname: AnsiString; cdrid: Integer); + procedure WriteLFH(s: TStream; flags, comp, mtime, crc, csize, usize: UInt32; const afname: AnsiString); + procedure WriteCDR(s: TStream; flags, comp, mtime, crc, csize, usize, eattr, offset: UInt32; const afname: AnsiString; cdrid: Integer); procedure SaveToStream(s: TStream); public @@ -92,7 +95,7 @@ interface implementation - uses SysUtils, StrUtils, Math, utils, zstream, crc, e_log; + uses SysUtils, StrUtils, DateUtils, Math, utils, zstream, crc, e_log; const ZIP_SIGN_CDR = 'PK'#1#2; @@ -131,7 +134,11 @@ implementation const ZIP_ENCRYPTION_MASK = (1 << 0) or (1 << 6) or (1 << 13); + ZIP_COMP_MASK = (1 << 1) or (1 << 2) or (1 << 4) or (1 << 12); + ZIP_DATA_MASK = (1 << 3); + ZIP_PATCH_MASK = (1 << 5); ZIP_UTF8_MASK = (1 << 11); + ZIP_STREAM_MASK = (1 << 14); function IsASCII(const s: AnsiString): Boolean; var i: Integer; @@ -177,6 +184,15 @@ implementation Result := True; end; + function DosToStr(dostime: UInt32): AnsiString; + begin + try + DateTimeToString(Result, 'yyyy/mm/dd hh:nn:ss', DosDateTimeToDateTime(dostime)); + except on e: EConvertError do + Result := 'INVALID ($' + IntToHex(dostime, 8) + ')'; + end; + end; + procedure ToSectionFile(fname: AnsiString; out section, name: AnsiString); inline; var i: SizeInt; begin @@ -286,13 +302,14 @@ implementation Result := nil; end; - function TZIPEditor.InsertSectionRAW(name: AnsiString): PSection; + function TZIPEditor.InsertSectionRAW(name: AnsiString; mtime: UInt32): PSection; var i: Integer; begin if FSection = nil then i := 0 else i := Length(FSection); SetLength(FSection, i + 1); FSection[i] := Default(TSection); FSection[i].name := name; + FSection[i].mtime := mtime; Result := @FSection[i]; end; @@ -316,21 +333,21 @@ implementation Result := FindSectionRAW(fixName, False); // CASENAME end; - function TZIPEditor.InsertSection(name: AnsiString): PSection; + function TZIPEditor.InsertSection(name: AnsiString; mtime: UInt32): PSection; begin Result := FindSection(name); if Result = nil then - Result := InsertSectionRAW(name); + Result := InsertSectionRAW(name, mtime); end; - function TZIPEditor.InsertFileInfo(const section, name: AnsiString; pos, csize, usize, comp, crc: UInt32): PResource; + function TZIPEditor.InsertFileInfo(const section, name: AnsiString; pos, csize, usize, comp, crc, mtime, flags: UInt32): PResource; var p: PSection; i: Integer; begin p := FindSectionRAW(section, True); if p = nil then - p := InsertSectionRAW(section); + p := InsertSectionRAW(section, mtime); if p.list = nil then i := 0 else i := Length(p.list); SetLength(p.list, i + 1); p.list[i] := Default(TResource); @@ -340,6 +357,8 @@ implementation p.list[i].usize := usize; p.list[i].comp := comp; p.list[i].chksum := crc; + p.list[i].mtime := mtime; + p.list[i].flags := flags; p.list[i].stream := nil; Result := @p.list[i]; end; @@ -359,6 +378,8 @@ implementation var s: TMemoryStream; cs: TCompressionStream; p: PResource; var comp, crc: UInt32; begin + Name := win2utf(Name); + Section := win2utf(Section); Result := False; if Name <> '' then begin @@ -385,7 +406,7 @@ implementation end; crc := crc32(0, nil, 0); crc := crc32(crc, data, len); - p := InsertFileInfo(Section, Name, $ffffffff, s.Size, Len, comp, crc); + p := InsertFileInfo(Section, Name, $ffffffff, s.Size, Len, comp, crc, DateTimeToDosDateTime(Now()), 0); p.stream := s; Result := True; except @@ -537,17 +558,21 @@ implementation procedure TZIPEditor.AddSection(Name: String); begin - if InsertSection(Name) = nil then + Name := win2utf(Name); + if InsertSection(Name, DateTimeToDosDateTime(Now())) = nil then raise Exception.Create('DFZIP: AddSection[' + Name + ']: failed to insert'); end; function TZIPEditor.HaveResource(Section, Resource: String): Boolean; begin + Section := win2utf(Section); + Resource := win2utf(Resource); Result := FindResource(FindSection(Section), Resource) <> nil; end; function TZIPEditor.HaveSection(Section: String): Boolean; begin + Section := win2utf(Section); Result := FindSection(Section) <> nil; end; @@ -571,6 +596,8 @@ implementation function TZIPEditor.GetResource(Section, Resource: String; var pData: Pointer; var Len: Integer): Boolean; var p: PResource; ptr: PByte; src: TStream; tmp: TDecompressionStream; crc: UInt32; begin + Section := win2utf(Section); + Resource := win2utf(Resource); FLastError := DFWAD_ERROR_CANTOPENWAD; Result := False; pData := nil; @@ -663,6 +690,7 @@ implementation function TZIPEditor.GetResourcesList(Section: String): SArray; var p: PSection; i: Integer; begin + Section := win2utf(Section); Result := nil; p := FindSection(Section); if (p <> nil) and (p.list <> nil) then @@ -670,7 +698,7 @@ implementation SetLength(Result, Length(p.list)); for i := 0 to High(p.list) do begin - Result[i] := p.list[i].name; + Result[i] := utf2win(p.list[i].name); end; end; end; @@ -684,12 +712,12 @@ implementation SetLength(Result, Length(FSection)); for i := 0 to High(FSection) do begin - Result[i] := FSection[i].name; + Result[i] := utf2win(FSection[i].name); end; end; end; - procedure TZIPEditor.ReadLFH(s: TStream; fname: AnsiString; xcsize, xusize, xcomp, xcrc: UInt32); + procedure TZIPEditor.ReadLFH(s: TStream; fname: AnsiString; xcsize, xusize, xcomp, xcrc, xtime, xflags: UInt32); var sig: packed array [0..3] of Char; var va, vb, flags, comp: UInt16; var mtime, crc, csize, usize: UInt32; @@ -721,7 +749,7 @@ implementation e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Min System : ' + IntToStr(vb), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Flags : $' + IntToHex(flags, 4), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Compression : ' + IntToStr(comp), MSG_NOTIFY); - e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Modification Time : $' + IntToHex(mtime, 8), MSG_NOTIFY); + e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Modification Time : ' + DosToStr(mtime), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': CRC-32 : $' + IntToHex(crc, 8), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Compressed size : ' + IntToStr(csize), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Decompressed size : ' + IntToStr(usize), MSG_NOTIFY); @@ -738,11 +766,11 @@ implementation begin p := FindSectionRAW(section, True); if p = nil then - p := InsertSectionRAW(section) + p := InsertSectionRAW(section, xtime); end else begin - p := InsertFileInfo(section, name, datapos, xcsize, xusize, xcomp, xcrc); + p := InsertFileInfo(section, name, datapos, xcsize, xusize, xcomp, xcrc, xtime, xflags and ZIP_COMP_MASK); end; if p = nil then raise Exception.Create('Failed to register resource [' + fname + ']'); @@ -806,7 +834,7 @@ implementation e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Min System : ' + IntToStr(vb), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Flags : $' + IntToHex(flags, 4), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Compression : ' + IntToStr(comp), MSG_NOTIFY); - e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Modification Time : $' + IntToHex(mtime, 8), MSG_NOTIFY); + e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Modification Time : ' + DosToStr(mtime), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': CRC-32 : $' + IntToHex(crc, 8), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Compressed size : ' + IntToStr(csize), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Decompressed size : ' + IntToStr(usize), MSG_NOTIFY); @@ -885,7 +913,7 @@ implementation e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Name : "' + aname + '"', MSG_NOTIFY); end; s.Seek(offset, TSeekOrigin.soBeginning); - ReadLFH(s, aname, csize, usize, comp, crc); + ReadLFH(s, aname, csize, usize, comp, crc, mtime, flags); finally s.Seek(next, TSeekOrigin.soBeginning); FreeMem(name); @@ -1086,6 +1114,8 @@ implementation procedure TZIPEditor.RemoveResource(Section, Resource: String); var p: PSection; i: Integer; begin + Section := win2utf(Section); + Resource := win2utf(Resource); p := FindSection(Section); i := FindResourceID(p, Resource); if i >= 0 then @@ -1137,13 +1167,12 @@ implementation Result := version; end; - procedure TZIPEditor.WriteLFH(s: TStream; comp, crc, csize, usize: UInt32; const afname: AnsiString); - var fname: PChar; version: UInt8; fnlen, flags: UInt16; mypos: UInt64; + procedure TZIPEditor.WriteLFH(s: TStream; flags, comp, mtime, crc, csize, usize: UInt32; const afname: AnsiString); + var fname: PChar; version: UInt8; fnlen: UInt16; mypos: UInt64; begin mypos := s.Position; fname := PChar(afname); fnlen := Length(fname); - flags := 0; if IsASCII(afname) = False then flags := flags or ZIP_UTF8_MASK; version := GetZIPVersion(afname, flags, comp); @@ -1154,7 +1183,7 @@ implementation e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Flags : $' + IntToHex(flags, 4), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Compression : ' + IntToStr(comp), MSG_NOTIFY); - e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Modification Time : $' + IntToHex(0, 8), MSG_NOTIFY); + e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Modification Time : ' + DosToStr(mtime), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': CRC-32 : $' + IntToHex(crc, 8), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Compressed size : ' + IntToStr(csize), MSG_NOTIFY); e_WriteLog('LFH @' + IntToHex(mypos, 8) + ': Decompressed size : ' + IntToStr(usize), MSG_NOTIFY); @@ -1167,7 +1196,7 @@ implementation s.WriteByte(ZIP_SYSTEM); // System WriteInt(s, UInt16(flags)); // Flags WriteInt(s, UInt16(comp)); // Compression method - WriteInt(s, UInt32(0)); // Modification time/date + WriteInt(s, UInt32(mtime)); // Modification time/date WriteInt(s, UInt32(crc)); // CRC-32 WriteInt(s, UInt32(csize)); // Compressed size WriteInt(s, UInt32(usize)); // Decompressed size @@ -1176,13 +1205,12 @@ implementation s.WriteBuffer(fname[0], fnlen); // File Name end; - procedure TZIPEditor.WriteCDR(s: TStream; comp, crc, csize, usize, eattr, offset: UInt32; const afname: AnsiString; cdrid: Integer); - var fname: PChar; version: UInt8; fnlen, flags: UInt16; mypos: UInt64; + procedure TZIPEditor.WriteCDR(s: TStream; flags, comp, mtime, crc, csize, usize, eattr, offset: UInt32; const afname: AnsiString; cdrid: Integer); + var fname: PChar; version: UInt8; fnlen: UInt16; mypos: UInt64; begin mypos := s.Position; fname := PChar(afname); fnlen := Length(fname); - flags := 0; if IsASCII(afname) = False then flags := flags or ZIP_UTF8_MASK; version := GetZIPVersion(afname, flags, comp); @@ -1195,7 +1223,7 @@ implementation e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Flags : $' + IntToHex(flags, 4), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Compression : ' + IntToStr(comp), MSG_NOTIFY); - e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Modification Time : $' + IntToHex(0, 8), MSG_NOTIFY); + e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Modification Time : ' + DosToStr(mtime), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': CRC-32 : $' + IntToHex(crc, 8), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Compressed size : ' + IntToStr(csize), MSG_NOTIFY); e_WriteLog('CDR#' + IntToStr(cdrid) + ' @' + IntToHex(mypos, 8) + ': Decompressed size : ' + IntToStr(usize), MSG_NOTIFY); @@ -1215,7 +1243,7 @@ implementation s.WriteByte(ZIP_SYSTEM); // Min system WriteInt(s, UInt16(flags)); // Flags WriteInt(s, UInt16(comp)); // Compression method - WriteInt(s, UInt32(0)); // Modification time/date + WriteInt(s, UInt32(mtime)); // Modification time/date WriteInt(s, UInt32(crc)); // CRC-32 WriteInt(s, UInt32(csize)); // Compressed size WriteInt(s, UInt32(usize)); // Decompressed size @@ -1249,7 +1277,7 @@ implementation begin p := @FSection[i].list[j]; afname := GetFileName(FSection[i].name, p.name); - WriteLFH(s, p.comp, p.chksum, p.csize, p.usize, afname); + WriteLFH(s, p.flags, p.comp, p.mtime, p.chksum, p.csize, p.usize, afname); if p.stream <> nil then begin Assert(p.stream.Size = p.csize); @@ -1269,7 +1297,7 @@ implementation else begin afname := GetFileName(FSection[i].name, ''); - WriteLFH(s, ZIP_COMP_STORE, zcrc, 0, 0, afname); + WriteLFH(s, 0, ZIP_COMP_STORE, FSection[i].mtime, zcrc, 0, 0, afname); end; end; end; @@ -1287,7 +1315,7 @@ implementation begin p := @FSection[i].list[j]; afname := GetFileName(FSection[i].name, p.name); - WriteCDR(s, p.comp, p.chksum, p.csize, p.usize, $00, loffset, afname, i); + WriteCDR(s, p.flags, p.comp, p.mtime, p.chksum, p.csize, p.usize, $00, loffset, afname, i); loffset := loffset + 30 + Length(afname) + p.csize; Inc(count); end; @@ -1295,7 +1323,7 @@ implementation else begin afname := GetFileName(FSection[i].name, ''); - WriteCDR(s, ZIP_COMP_STORE, zcrc, 0, 0, $10, loffset, afname, i); + WriteCDR(s, 0, ZIP_COMP_STORE, FSection[i].mtime, zcrc, 0, 0, $10, loffset, afname, i); loffset := loffset + 30 + Length(afname) + 0; Inc(count); end;