type TMemoryStreamEx=class(TMemoryStream)
public
function ReadByte:integer;
function ReadInt:integer;
function ReadLong:int64;
function ReadShort:integer;
function ReadString(Length:integer):
string;
end;
function ChangeEndian32(i:integer):integer;
register;
asm
bswap EAX
end;
procedure ChangeEndian64(P: Pointer);
register;
asm
mov EDX, [EAX]
bswap EDX
mov EBX, [EAX + $04]
bswap EBX
mov [EAX], EBX
mov [EAX + $04], EDX
end;
function TMemoryStreamEx.ReadByte:integer;
var b:byte;
begin
Read(b,SizeOf(byte));
Result:=b;
end;
function TMemoryStreamEx.ReadInt:integer;
begin
Read(Result,SizeOf(integer));
Result:=ChangeEndian32(Result);
end;
function TMemoryStreamEx.ReadLong:int64;
begin
Read(Result,SizeOf(int64));
ChangeEndian64(@Result);
end;
function TMemoryStreamEx.ReadShort:integer;
begin
Read(Result,2);
Result:=Swap(Result);
end;
function TMemoryStreamEx.ReadString(Length:integer):
string;
begin
SetLength(Result, Length);
ReadBuffer(Pointer(Result)^, Length);
end;