1

进一步到ruakh的帖子我想确定以下代码段是否属于TLVKLV编码类型:

interface

const
  Lex_0: string[length('(EOF)')] ='(EOF)';
  Lex_1: string[length('(Error)')] ='(Error)';

  LexTable : array[0..114] of ^String = (
  @Lex_0,
  @Lex_1
  )
4

2 回答 2

2

您的示例代码将不起作用,因为您正在混合shortstring const-正如 Rob 在他的评论中所说的那样pointerstring

而且您的代码与 KLV 或 TLV 编码无关。

这里可能是一个 TLV 编码示例(我只展示了字符串编码,但您可以添加其他类型):

type
  TTLVType = (tlvUnknown, tlvString, tlvInteger);

function EncodeToTLV(const aString: WideString): TBytes;
var Len: integer;
begin
  Len := length(aString)*2;
  SetLength(result,Len+sizeof(TTLVType)+sizeof(integer));
  Result[0] := ord(tlvString); // Type
  PInteger(@Result[sizeof(TTLVType)])^ := Len; // Length
  move(pointer(aString)^,Result[sizeof(TTLVType)+sizeof(integer)],Len); // Value
end;

function DecodeStringFromTLV(const aValue: TBytes): WideString;
begin
  if (length(aValue)<3) or (aValue[0]<>ord(tlvString)) or
     (PInteger(@aValue[sizeof(TTLVType)])^<>length(aValue)-sizeof(TTLVType)-sizeof(integer)) then
    raise EXception.Create('Invalid input format');
  SetString(result,PWideChar(@Result[sizeof(TTLVType)+sizeof(integer)]),PInteger(@aValue[sizeof(TTLVType)])^ div 2);
end;

WideString在这里使用它是因为它可以安全地存储任何 Unicode 内容,即使在 Delphi 2009 之前的编译器版本上也是如此。

您可以使用记录而不是我的指针算术:

type
  TTLVHeader = packed record
    ContentType: TTLVType;
    ContentLength: integer;
    Content: array[0..1000] of byte; // but real length will vary
  end;
  PTLVHeader = ^TTLVHeader;

function EncodeToTLV(const aString: WideString): TBytes;
var Len: integer;
begin
  Len := length(aString)*2;
  SetLength(result,Len+sizeof(TTLVType)+sizeof(integer));
  with PTLVHeader(result)^ do 
  begin
    ContentType := tlvString;
    ContentLength := Len;
    move(pointer(aString)^,Content,Len); 
  end;  
end;

KLV 可以使用类似的编码,但在标头中添加一个整数键。

于 2011-12-05T17:39:19.910 回答
0

据我所知,这只定义了两个常量(在这种情况下)Lex_0并将Lex_1它们放入一个名为 的数组LexTable中,这根本不是编码。

Lex_0: string // declares a string constant by the name of Lex_0
[length('(EOF)')] // specifies the length of the string, this is equivalent to [5] in this case
='(EOF)' // sets the constant to '(EOF)'

然后LexTable创建指向字符串的指针数组,并将两个常量的地址放入数组中。

于 2011-12-05T17:08:18.683 回答