You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RnQ/for.RnQ/RTL/RnQBinUtils.pas

136 lines
3.0 KiB
Plaintext

{
This file is part of R&Q.
Under same license
}
unit RnQBinUtils;
{$I ForRnQConfig.inc}
interface
uses
System.SysUtils;
{$I NoRTTI.inc}
function TLV2(code: integer; const data: RawByteString): RawByteString; overload;
function TLV2(code: integer; const data: TDateTime): RawByteString; overload;
function TLV2(code: integer; const data: integer): RawByteString; overload;
function TLV2(code: integer; const data: boolean): RawByteString; overload;
function dword_LEat(p: Pointer): LongWord; overload; {$IFDEF HAS_INLINE}inline; {$ENDIF HAS_INLINE}
function dword_LEat(const s: RawByteString; ofs: Integer): Integer; overload; {$IFDEF HAS_INLINE}inline; {$ENDIF HAS_INLINE}
function int2str(i: integer): RawByteString;
function dt2str(dt: TDateTime): RawByteString;
function str2int(const s: RawByteString): integer; overload;
function str2int(p: pointer): integer; overload; inline;
implementation
uses
Winapi.Windows;
function Int2Str(I: Integer): RawByteString;
var
V: RawByteString;
begin
SetLength(V, 4);
Move(I, pointer(V)^, 4);
Result := V;
end;
function dword_LEat(P: Pointer): LongWord; overLoad; {$IFDEF HAS_INLINE}inline;{$ENDIF HAS_INLINE}
begin
Result := LongWord(P^)
end;
function dword_LEat(const S: RawByteString; Ofs: Integer): Integer; {$IFDEF HAS_INLINE}inline;{$ENDIF HAS_INLINE}
begin
Result := Int32((@S[Ofs])^)
end;
function TLV2(Code: Integer; const Data: RawByteString): RawByteString;
var
S: RawByteString;
Ps: Pointer;
I: Integer;
begin
I := Length(Data);
SetLength(S, 4 + 4 + I);
Ps := Pointer(S);
PInteger(Ps)^ := Code;
Inc(PByte(Ps), 4);
PInteger(Ps)^ := I;
Inc(PByte(Ps), 4);
if I > 0 then
Move(Pointer(Data)^, Ps^, I);
Result := S;
end;
function TLV2(Code: integer; const Data: TDateTime): RawByteString;
var
S: RawByteString;
Ps: Pointer;
begin
SetLength(S, 4 + 4 + 8);
Ps := Pointer(S);
PInteger(Ps)^ := Code;
Inc(PByte(Ps), 4);
PInteger(ps)^ := 8;
Inc(PByte(Ps), 4);
PDateTime(ps)^ := Data;
Result := S;
end;
function TLV2(Code: Integer; const Data: Integer): RawByteString;
var
S: RawByteString;
Ps: Pointer;
begin
SetLength(S, 4 + 4 + 4);
Ps := Pointer(S);
PInteger(Ps)^ := Code;
Inc(PByte(Ps), 4);
PInteger(Ps)^ := 4;
Inc(PByte(Ps), 4);
PInteger(Ps)^ := Data;
Result := S;
end;
function TLV2(Code: Integer; const Data: Boolean): RawByteString;
var
S: RawByteString;
Ps: Pointer;
begin
SetLength(S, 4 + 4 + 1);
Ps := Pointer(S);
PInteger(Ps)^ := Code;
Inc(PByte(Ps), 4);
PInteger(Ps)^ := 1;
Inc(PByte(Ps), 4);
PByte(Ps)^ := Byte(Data);
Result := S;
end;
function dt2str(dt: TDateTime): RawByteString;
var
V: RawByteString;
begin
SetLength(V, 8);
Move(dt, Pointer(V)^, 8);
Result := V;
end;
function str2int(const S: RawByteString): Integer;
begin
Result := dword_LEat(Pointer(S))
end;
function str2int(P: Pointer): Integer;
begin
Result := dword_LEat(P)
end;
end.