X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2FBinEditor.pas;h=25526e14c0374b5ac3de55ff639872625d34d5a4;hb=2fa77a7c9667395ef6d4141cde69ff6349bf301e;hp=50867f41ff60e4664e4c664a326b65977b3b9c99;hpb=ac201b02f10ef558087d50f6b03b4519ab567558;p=d2df-sdl.git diff --git a/src/shared/BinEditor.pas b/src/shared/BinEditor.pas index 50867f4..25526e1 100644 --- a/src/shared/BinEditor.pas +++ b/src/shared/BinEditor.pas @@ -1,4 +1,19 @@ -{$MODE DELPHI} +(* Copyright (C) DooM 2D:Forever Developers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *) +{$INCLUDE a_modes.inc} Unit BinEditor; Interface @@ -29,7 +44,7 @@ Type Procedure WriteInt(Var x: Integer); Procedure WriteSingle(Var x: Single); Procedure WriteBoolean(Var x: Boolean); - Procedure WriteString(Var x: String; aMaxLen: Byte = 255); + Procedure WriteString(const x: AnsiString; aMaxLen: Word=65535); Procedure WriteMemory(Var x: Pointer; memSize: Cardinal); Procedure Fill(aLen: Cardinal; aFillSym: Byte); Procedure SaveToFile(Var aFile: File); @@ -55,7 +70,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); @@ -151,7 +166,7 @@ begin if (FPosition + varSize) > FSize then ExtendMemory(varSize); - CopyMemory(Pointer(Cardinal(FData) + FPosition), + CopyMemory(Pointer(NativeUInt(FData) + FPosition), @x, varSize); FPosition := FPosition + varSize; end; @@ -230,26 +245,22 @@ begin 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(Cardinal(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(Cardinal(FData) + FPosition), - @x[1], len); - FPosition := FPosition + len; + CopyMemory(Pointer(NativeUInt(FData) + FPosition), @x[1], len); + FPosition := FPosition+len; end; end; @@ -259,13 +270,13 @@ begin ExtendMemory(SizeOf(Cardinal) + memSize); // Äëèíà áëîêà ïàìÿòè: - CopyMemory(Pointer(Cardinal(FData) + FPosition), + CopyMemory(Pointer(NativeUInt(FData) + FPosition), @memSize, SizeOf(Cardinal)); FPosition := FPosition + SizeOf(Cardinal); // Áëîê ïàìÿòè: if memSize > 0 then begin - CopyMemory(Pointer(Cardinal(FData) + FPosition), + CopyMemory(Pointer(NativeUInt(FData) + FPosition), x, memSize); FPosition := FPosition + memSize; end; @@ -278,7 +289,7 @@ begin if aLen > 0 then begin - FillMemory(Pointer(Cardinal(FData) + FPosition), + FillMemory(Pointer(NativeUInt(FData) + FPosition), aLen, aFillSym); FPosition := FPosition + aLen; end; @@ -339,7 +350,7 @@ begin if (FPosition + varSize) <= FSize then begin CopyMemory(@x, - Pointer(Cardinal(FData) + FPosition), + Pointer(NativeUInt(FData) + FPosition), varSize); FPosition := FPosition + varSize; end @@ -395,38 +406,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(Cardinal(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(Cardinal(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); @@ -435,7 +447,7 @@ begin begin // Äëèíà áëîêà ïàìÿòè: CopyMemory(@memSize, - Pointer(Cardinal(FData) + FPosition), + Pointer(NativeUInt(FData) + FPosition), SizeOf(Cardinal)); if (FPosition + SizeOf(Cardinal) + memSize) <= FSize then @@ -446,7 +458,7 @@ begin begin GetMem(x, memSize); CopyMemory(x, - Pointer(Cardinal(FData) + FPosition), + Pointer(NativeUInt(FData) + FPosition), memSize); FPosition := FPosition + memSize; end