index f59d4f3a5f2fb3c64fe748aecb963e133bf64441..25526e14c0374b5ac3de55ff639872625d34d5a4 100644 (file)
--- a/src/shared/BinEditor.pas
+++ b/src/shared/BinEditor.pas
+(* 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 <http://www.gnu.org/licenses/>.
+ *)
+{$INCLUDE a_modes.inc}
Unit BinEditor;
Interface
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);
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);
begin
FillChar(Dest^, Len, 0);
end;
-
+
{ T B i n M e m o r y W r i t e r : }
Constructor TBinMemoryWriter.Create(aSize: Cardinal);
FreeMem(FData);
FData := nil;
end;
-
+
Inherited;
end;
if (FPosition + varSize) > FSize then
ExtendMemory(varSize);
- CopyMemory(Pointer(Cardinal(FData) + FPosition),
+ CopyMemory(Pointer(NativeUInt(FData) + FPosition),
@x, varSize);
FPosition := FPosition + varSize;
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
- 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;
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;
if aLen > 0 then
begin
- FillMemory(Pointer(Cardinal(FData) + FPosition),
+ FillMemory(Pointer(NativeUInt(FData) + FPosition),
aLen, aFillSym);
FPosition := FPosition + aLen;
end;
if (FPosition + varSize) <= FSize then
begin
CopyMemory(@x,
- Pointer(Cardinal(FData) + FPosition),
+ Pointer(NativeUInt(FData) + FPosition),
varSize);
FPosition := FPosition + varSize;
end
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);
begin
// Äëèíà áëîêà ïàìÿòè:
CopyMemory(@memSize,
- Pointer(Cardinal(FData) + FPosition),
+ Pointer(NativeUInt(FData) + FPosition),
SizeOf(Cardinal));
if (FPosition + SizeOf(Cardinal) + memSize) <= FSize then
begin
GetMem(x, memSize);
CopyMemory(x,
- Pointer(Cardinal(FData) + FPosition),
+ Pointer(NativeUInt(FData) + FPosition),
memSize);
FPosition := FPosition + memSize;
end