¿Es esta una técnica de codificación?
Solución
Su código de muestra no funcionará, ya que está mezclando shortstring const
y pointer
de string
- Como dijo Rob en su comentario.
Y su código no tiene nada que ver con la codificación de KLV ni TLV.
Aquí puede ser una muestra de codificación TLV (muestro solo la codificación de cadenas, pero puede agregar otros tipos):
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;
solía WideString
Aquí porque puede almacenar de forma segura cualquier contenido de Unicode, incluso en la versión pre-Delphi 2009 del compilador.
Puede usar un registro en lugar de mi puntero aritmético:
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;
Se puede usar una codificación similar para KLV, pero agregar una tecla entera en el encabezado.
Otros consejos
Por lo que puedo decir, esto solo define dos constantes (Lex_0
y Lex_1
en este caso) y los pone en una matriz llamada LexTable
, esto no es una codificación en absoluto.
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)'
entonces el LexTable
Se crea la matriz de punteros a la cadena y las direcciones de las dos constantes se colocan en la matriz.