X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2FBinEditor.pas;h=e1e90229576fdcf6eef72af0257f189f15d36ed7;hb=939cb39acd39a1ca2bf11a0e4a8488c05baf7358;hp=9e77d0e761a80deb27de588e6886cd591710c330;hpb=2a785940b8e595cffcc9e9d652c2ae9248c989ee;p=d2df-sdl.git diff --git a/src/shared/BinEditor.pas b/src/shared/BinEditor.pas index 9e77d0e..e1e9022 100644 --- a/src/shared/BinEditor.pas +++ b/src/shared/BinEditor.pas @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . *) -{$MODE DELPHI} +{$INCLUDE a_modes.inc} Unit BinEditor; Interface @@ -30,25 +30,26 @@ Type FData: Pointer; FPosition: Cardinal; - Procedure WriteVar(Var x; varSize: Cardinal); + Procedure WriteVar (var x; varSize: Cardinal); Procedure ExtendMemory(addLen: Cardinal); Public - Constructor Create(aSize: Cardinal); - Destructor Destroy(); Override; - Procedure WriteByte(Var x: Byte); - Procedure WriteWord(Var x: Word); - Procedure WriteDWORD(Var x: DWORD); - Procedure WriteShortInt(Var x: ShortInt); - Procedure WriteSmallInt(Var x: SmallInt); - Procedure WriteInt(Var x: Integer); - Procedure WriteSingle(Var x: Single); - Procedure WriteBoolean(Var x: Boolean); - Procedure WriteString(Var x: String; aMaxLen: Byte = 255); - Procedure WriteMemory(Var x: Pointer; memSize: Cardinal); - Procedure Fill(aLen: Cardinal; aFillSym: Byte); - Procedure SaveToFile(Var aFile: File); - Procedure SaveToMemory(Var aMem: TBinMemoryWriter); + Constructor Create (aSize: Cardinal); + Destructor Destroy (); Override; + + Procedure WriteByte (x: Byte); + Procedure WriteWord (x: Word); + Procedure WriteDWORD (x: DWORD); + Procedure WriteShortInt (x: ShortInt); + Procedure WriteSmallInt (x: SmallInt); + Procedure WriteInt (x: Integer); + Procedure WriteSingle (x: Single); + Procedure WriteBoolean (x: Boolean); + Procedure WriteString (const x: AnsiString; aMaxLen: Word=65535); + Procedure WriteMemory (x: Pointer; memSize: Cardinal); + Procedure Fill (aLen: Cardinal; aFillSym: Byte); + Procedure SaveToFile (Var aFile: File); + Procedure SaveToMemory (Var aMem: TBinMemoryWriter); End; TBinMemoryReader = Class (TObject) @@ -70,7 +71,7 @@ Type Procedure ReadInt(Var x: Integer); Procedure ReadSingle(Var x: Single); Procedure ReadBoolean(Var x: Boolean); - Procedure ReadString(Var x: String); + Procedure ReadString(Var x: AnsiString); Procedure ReadMemory(Var x: Pointer; Var memSize: Cardinal); Procedure Skip(aLen: Cardinal); Procedure LoadFromFile(Var aFile: File); @@ -161,14 +162,11 @@ begin Inherited; end; -Procedure TBinMemoryWriter.WriteVar(Var x; varSize: Cardinal); +Procedure TBinMemoryWriter.WriteVar (var x; varSize: Cardinal); begin - if (FPosition + varSize) > FSize then - ExtendMemory(varSize); - - CopyMemory(Pointer(NativeUInt(FData) + FPosition), - @x, varSize); - FPosition := FPosition + varSize; + if (FPosition+varSize > FSize) then ExtendMemory(varSize); + CopyMemory(Pointer(PtrUInt(FData)+FPosition), @x, varSize); + FPosition := FPosition+varSize; end; Procedure TBinMemoryWriter.ExtendMemory(addLen: Cardinal); @@ -197,105 +195,89 @@ begin e_WriteLog('Save Memory Extended: '+IntToStr(FSize), MSG_NOTIFY); end; -Procedure TBinMemoryWriter.WriteByte(Var x: Byte); +Procedure TBinMemoryWriter.WriteByte (x: Byte); begin WriteVar(x, SizeOf(Byte)); end; -Procedure TBinMemoryWriter.WriteWord(Var x: Word); +Procedure TBinMemoryWriter.WriteWord (x: Word); begin WriteVar(x, SizeOf(Word)); end; -Procedure TBinMemoryWriter.WriteDWORD(Var x: DWORD); +Procedure TBinMemoryWriter.WriteDWORD (x: DWORD); begin WriteVar(x, SizeOf(DWORD)); end; -Procedure TBinMemoryWriter.WriteShortInt(Var x: ShortInt); +Procedure TBinMemoryWriter.WriteShortInt (x: ShortInt); begin WriteVar(x, SizeOf(ShortInt)); end; -Procedure TBinMemoryWriter.WriteSmallInt(Var x: SmallInt); +Procedure TBinMemoryWriter.WriteSmallInt (x: SmallInt); begin WriteVar(x, SizeOf(SmallInt)); end; -Procedure TBinMemoryWriter.WriteInt(Var x: Integer); +Procedure TBinMemoryWriter.WriteInt (x: Integer); begin WriteVar(x, SizeOf(Integer)); end; -Procedure TBinMemoryWriter.WriteSingle(Var x: Single); +Procedure TBinMemoryWriter.WriteSingle (x: Single); begin WriteVar(x, SizeOf(Single)); end; -Procedure TBinMemoryWriter.WriteBoolean(Var x: Boolean); +Procedure TBinMemoryWriter.WriteBoolean (x: Boolean); var y: Byte; - begin - if x then - y := 1 - else - y := 0; - + if x then y := 1 else y := 0; WriteVar(y, SizeOf(Byte)); end; -Procedure TBinMemoryWriter.WriteString(Var x: String; aMaxLen: Byte = 255); +Procedure TBinMemoryWriter.WriteString (const x: AnsiString; aMaxLen: Word=65535); var - len: Byte; - + len: Word; begin - len := Min(Length(x), aMaxLen); + if (Length(x) > aMaxLen) then len := aMaxLen else len := Word(Length(x)); - if (FPosition + SizeOf(Byte) + len) > FSize then - ExtendMemory(SizeOf(Byte) + len); + if (FPosition+SizeOf(Byte)+len) > FSize then ExtendMemory(SizeOf(Byte)+len); -// Äëèíà ñòðîêè: - CopyMemory(Pointer(NativeUInt(FData) + FPosition), - @len, SizeOf(Byte)); - FPosition := FPosition + SizeOf(Byte); -// Ñòðîêà: - if len > 0 then + // Äëèíà ñòðîêè: + CopyMemory(Pointer(NativeUInt(FData)+FPosition), @len, SizeOf(len)); + FPosition := FPosition+SizeOf(len); + // Ñòðîêà: + if (len > 0) then begin - CopyMemory(Pointer(NativeUInt(FData) + FPosition), - @x[1], len); - FPosition := FPosition + len; + CopyMemory(Pointer(NativeUInt(FData) + FPosition), @x[1], len); + FPosition := FPosition+len; end; end; -Procedure TBinMemoryWriter.WriteMemory(Var x: Pointer; memSize: Cardinal); +Procedure TBinMemoryWriter.WriteMemory (x: Pointer; memSize: Cardinal); begin - if (FPosition + SizeOf(Cardinal) + memSize) > FSize then - ExtendMemory(SizeOf(Cardinal) + memSize); - -// Äëèíà áëîêà ïàìÿòè: - CopyMemory(Pointer(NativeUInt(FData) + FPosition), - @memSize, SizeOf(Cardinal)); - FPosition := FPosition + SizeOf(Cardinal); -// Áëîê ïàìÿòè: - if memSize > 0 then + if (FPosition+SizeOf(Cardinal)+memSize) > FSize then ExtendMemory(SizeOf(Cardinal)+memSize); + // Äëèíà áëîêà ïàìÿòè + CopyMemory(Pointer(PtrUInt(FData)+FPosition), @memSize, SizeOf(Cardinal)); + FPosition := FPosition+SizeOf(Cardinal); + // Áëîê ïàìÿòè + if (memSize > 0) then begin - CopyMemory(Pointer(NativeUInt(FData) + FPosition), - x, memSize); - FPosition := FPosition + memSize; + CopyMemory(Pointer(PtrUInt(FData)+FPosition), x, memSize); + FPosition := FPosition+memSize; end; end; -Procedure TBinMemoryWriter.Fill(aLen: Cardinal; aFillSym: Byte); +Procedure TBinMemoryWriter.Fill (aLen: Cardinal; aFillSym: Byte); begin - if (FPosition + aLen) > FSize then - ExtendMemory(aLen); - - if aLen > 0 then + if (FPosition+aLen > FSize) then ExtendMemory(aLen); + if (aLen > 0) then begin - FillMemory(Pointer(NativeUInt(FData) + FPosition), - aLen, aFillSym); - FPosition := FPosition + aLen; + FillMemory(Pointer(PtrUInt(FData) + FPosition), aLen, aFillSym); + FPosition := FPosition+aLen; end; end; @@ -410,38 +392,39 @@ begin x := False; end; -Procedure TBinMemoryReader.ReadString(Var x: String); +Procedure TBinMemoryReader.ReadString (Var x: AnsiString); var - len: Byte; - + len: Word; begin - if (FPosition + SizeOf(Byte)) <= FSize then - begin + if (FPosition+SizeOf(len)) <= FSize then + begin // Äëèíà ñòðîêè: - CopyMemory(@len, - Pointer(NativeUInt(FData) + FPosition), - SizeOf(Byte)); - - if (FPosition + SizeOf(Byte) + len) <= FSize then - begin - FPosition := FPosition + SizeOf(Byte); - // Ñòðîêà: - SetLength(x, len); - if len > 0 then - begin - CopyMemory(@x[1], - Pointer(NativeUInt(FData) + FPosition), - len); - FPosition := FPosition + len; - end - else - x := ''; - end + CopyMemory(@len, Pointer(NativeUInt(FData)+FPosition), SizeOf(len)); + if (FPosition+SizeOf(len)+len <= FSize) then + begin + FPosition := FPosition+SizeOf(len); + // Ñòðîêà: + UniqueString(x); + SetLength(x, len); + if (len > 0) then + begin + CopyMemory(@x[1], Pointer(NativeUInt(FData) + FPosition), len); + FPosition := FPosition+len; + end else - raise EBinSizeError.Create('TBinMemoryReader.ReadString: Too Long String'); + begin + x := ''; + end; end + else + begin + raise EBinSizeError.Create('TBinMemoryReader.ReadString: Too Long String'); + end; + end else + begin raise EBinSizeError.Create('TBinMemoryReader.ReadString: End of Memory'); + end; end; Procedure TBinMemoryReader.ReadMemory(Var x: Pointer; Var memSize: Cardinal); @@ -450,7 +433,7 @@ begin begin // Äëèíà áëîêà ïàìÿòè: CopyMemory(@memSize, - Pointer((FData) + FPosition), + Pointer(NativeUInt(FData) + FPosition), SizeOf(Cardinal)); if (FPosition + SizeOf(Cardinal) + memSize) <= FSize then