index 9e77d0e761a80deb27de588e6886cd591710c330..e1e90229576fdcf6eef72af0257f189f15d36ed7 100644 (file)
--- a/src/shared/BinEditor.pas
+++ b/src/shared/BinEditor.pas
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
-{$MODE DELPHI}
+{$INCLUDE a_modes.inc}
Unit BinEditor;
Interface
Unit BinEditor;
Interface
FData: Pointer;
FPosition: Cardinal;
FData: Pointer;
FPosition: Cardinal;
- Procedure WriteVar(Var x; varSize: Cardinal);
+ Procedure WriteVar (var x; varSize: Cardinal);
Procedure ExtendMemory(addLen: Cardinal);
Public
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)
End;
TBinMemoryReader = Class (TObject)
Procedure ReadInt(Var x: Integer);
Procedure ReadSingle(Var x: Single);
Procedure ReadBoolean(Var x: Boolean);
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);
Procedure ReadMemory(Var x: Pointer; Var memSize: Cardinal);
Procedure Skip(aLen: Cardinal);
Procedure LoadFromFile(Var aFile: File);
Inherited;
end;
Inherited;
end;
-Procedure TBinMemoryWriter.WriteVar(Var x; varSize: Cardinal);
+Procedure TBinMemoryWriter.WriteVar (var x; varSize: Cardinal);
begin
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);
end;
Procedure TBinMemoryWriter.ExtendMemory(addLen: Cardinal);
e_WriteLog('Save Memory Extended: '+IntToStr(FSize), MSG_NOTIFY);
end;
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;
begin
WriteVar(x, SizeOf(Byte));
end;
-Procedure TBinMemoryWriter.WriteWord(Var x: Word);
+Procedure TBinMemoryWriter.WriteWord (x: Word);
begin
WriteVar(x, SizeOf(Word));
end;
begin
WriteVar(x, SizeOf(Word));
end;
-Procedure TBinMemoryWriter.WriteDWORD(Var x: DWORD);
+Procedure TBinMemoryWriter.WriteDWORD (x: DWORD);
begin
WriteVar(x, SizeOf(DWORD));
end;
begin
WriteVar(x, SizeOf(DWORD));
end;
-Procedure TBinMemoryWriter.WriteShortInt(Var x: ShortInt);
+Procedure TBinMemoryWriter.WriteShortInt (x: ShortInt);
begin
WriteVar(x, SizeOf(ShortInt));
end;
begin
WriteVar(x, SizeOf(ShortInt));
end;
-Procedure TBinMemoryWriter.WriteSmallInt(Var x: SmallInt);
+Procedure TBinMemoryWriter.WriteSmallInt (x: SmallInt);
begin
WriteVar(x, SizeOf(SmallInt));
end;
begin
WriteVar(x, SizeOf(SmallInt));
end;
-Procedure TBinMemoryWriter.WriteInt(Var x: Integer);
+Procedure TBinMemoryWriter.WriteInt (x: Integer);
begin
WriteVar(x, SizeOf(Integer));
end;
begin
WriteVar(x, SizeOf(Integer));
end;
-Procedure TBinMemoryWriter.WriteSingle(Var x: Single);
+Procedure TBinMemoryWriter.WriteSingle (x: Single);
begin
WriteVar(x, SizeOf(Single));
end;
begin
WriteVar(x, SizeOf(Single));
end;
-Procedure TBinMemoryWriter.WriteBoolean(Var x: Boolean);
+Procedure TBinMemoryWriter.WriteBoolean (x: Boolean);
var
y: Byte;
var
y: Byte;
-
begin
begin
- if x then
- y := 1
- else
- y := 0;
-
+ if x then y := 1 else y := 0;
WriteVar(y, SizeOf(Byte));
end;
WriteVar(y, SizeOf(Byte));
end;
-Procedure TBinMemoryWriter.WriteString(Var x: String; aMaxLen: Byte = 255);
+Procedure TBinMemoryWriter.WriteString (const x: AnsiString; aMaxLen: Word=65535);
var
var
- len: Byte;
-
+ len: Word;
begin
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
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;
end;
end;
-Procedure TBinMemoryWriter.WriteMemory(Var x: Pointer; memSize: Cardinal);
+Procedure TBinMemoryWriter.WriteMemory (x: Pointer; memSize: Cardinal);
begin
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
begin
- CopyMemory(Pointer(NativeUInt(FData) + FPosition),
- x, memSize);
- FPosition := FPosition + memSize;
+ CopyMemory(Pointer(PtrUInt(FData)+FPosition), x, memSize);
+ FPosition := FPosition+memSize;
end;
end;
end;
end;
-Procedure TBinMemoryWriter.Fill(aLen: Cardinal; aFillSym: Byte);
+Procedure TBinMemoryWriter.Fill (aLen: Cardinal; aFillSym: Byte);
begin
begin
- if (FPosition + aLen) > FSize then
- ExtendMemory(aLen);
-
- if aLen > 0 then
+ if (FPosition+aLen > FSize) then ExtendMemory(aLen);
+ if (aLen > 0) then
begin
begin
- FillMemory(Pointer(NativeUInt(FData) + FPosition),
- aLen, aFillSym);
- FPosition := FPosition + aLen;
+ FillMemory(Pointer(PtrUInt(FData) + FPosition), aLen, aFillSym);
+ FPosition := FPosition+aLen;
end;
end;
end;
end;
x := False;
end;
x := False;
end;
-Procedure TBinMemoryReader.ReadString(Var x: String);
+Procedure TBinMemoryReader.ReadString (Var x: AnsiString);
var
var
- len: Byte;
-
+ len: Word;
begin
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
else
- raise EBinSizeError.Create('TBinMemoryReader.ReadString: Too Long String');
+ begin
+ x := '';
+ end;
end
end
+ else
+ begin
+ raise EBinSizeError.Create('TBinMemoryReader.ReadString: Too Long String');
+ end;
+ end
else
else
+ begin
raise EBinSizeError.Create('TBinMemoryReader.ReadString: End of Memory');
raise EBinSizeError.Create('TBinMemoryReader.ReadString: End of Memory');
+ end;
end;
Procedure TBinMemoryReader.ReadMemory(Var x: Pointer; Var memSize: Cardinal);
end;
Procedure TBinMemoryReader.ReadMemory(Var x: Pointer; Var memSize: Cardinal);
begin
// Äëèíà áëîêà ïàìÿòè:
CopyMemory(@memSize,
begin
// Äëèíà áëîêà ïàìÿòè:
CopyMemory(@memSize,
- Pointer((FData) + FPosition),
+ Pointer(NativeUInt(FData) + FPosition),
SizeOf(Cardinal));
if (FPosition + SizeOf(Cardinal) + memSize) <= FSize then
SizeOf(Cardinal));
if (FPosition + SizeOf(Cardinal) + memSize) <= FSize then