Release 23
git-svn-id: https://svn.code.sf.net/p/synalist/code/trunk@49 7c85be65-684b-0410-a082-b2ed4fbef004
This commit is contained in:
403
asn1util.pas
403
asn1util.pas
@ -1,9 +1,9 @@
|
||||
{==============================================================================|
|
||||
| Project : Delphree - Synapse | 001.003.002 |
|
||||
| Project : Delphree - Synapse | 001.003.003 |
|
||||
|==============================================================================|
|
||||
| Content: support for ASN.1 coding and decoding |
|
||||
|==============================================================================|
|
||||
| The contents of this file are subject to the Mozilla Public License Ver. 1.0 |
|
||||
| The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
|
||||
| (the "License"); you may not use this file except in compliance with the |
|
||||
| License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
|
||||
| |
|
||||
@ -26,6 +26,7 @@
|
||||
|==============================================================================}
|
||||
|
||||
{$Q-}
|
||||
{$WEAKPACKAGEUNIT ON}
|
||||
|
||||
unit ASN1Util;
|
||||
|
||||
@ -46,204 +47,202 @@ const
|
||||
ASN1_TIMETICKS = $43;
|
||||
ASN1_OPAQUE = $44;
|
||||
|
||||
function ASNEncOIDitem(Value: integer): string;
|
||||
function ASNDecOIDitem(var Start: integer; Buffer: string): integer;
|
||||
function ASNEncLen(Len: integer): string;
|
||||
function ASNDecLen(var Start: integer; Buffer: string): integer;
|
||||
function ASNEncInt(Value: integer): string;
|
||||
function ASNEncUInt(Value: integer): string;
|
||||
function ASNObject(Data: string; ASNType: integer): string;
|
||||
function ASNItem(var Start: integer; Buffer: string; var ValueType:integer): string;
|
||||
Function MibToId(mib:string):string;
|
||||
Function IdToMib(id:string):string;
|
||||
Function IntMibToStr(int:string):string;
|
||||
function ASNEncOIDItem(Value: Integer): string;
|
||||
function ASNDecOIDItem(var Start: Integer; const Buffer: string): Integer;
|
||||
function ASNEncLen(Len: Integer): string;
|
||||
function ASNDecLen(var Start: Integer; const Buffer: string): Integer;
|
||||
function ASNEncInt(Value: Integer): string;
|
||||
function ASNEncUInt(Value: Integer): string;
|
||||
function ASNObject(const Data: string; ASNType: Integer): string;
|
||||
function ASNItem(var Start: Integer; const Buffer: string;
|
||||
var ValueType: Integer): string;
|
||||
function MibToId(Mib: string): string;
|
||||
function IdToMib(const Id: string): string;
|
||||
function IntMibToStr(const Value: string): string;
|
||||
function IPToID(Host: string): string;
|
||||
|
||||
implementation
|
||||
|
||||
{==============================================================================}
|
||||
{ASNEncOIDitem}
|
||||
function ASNEncOIDitem(Value: integer): string;
|
||||
|
||||
function ASNEncOIDItem(Value: Integer): string;
|
||||
var
|
||||
x,xm:integer;
|
||||
b:boolean;
|
||||
x, xm: Integer;
|
||||
b: Boolean;
|
||||
begin
|
||||
x:=value;
|
||||
b:=false;
|
||||
result:='';
|
||||
x := Value;
|
||||
b := False;
|
||||
Result := '';
|
||||
repeat
|
||||
xm:=x mod 128;
|
||||
x:=x div 128;
|
||||
xm := x mod 128;
|
||||
x := x div 128;
|
||||
if b then
|
||||
xm:=xm or $80;
|
||||
if x>0
|
||||
then b:=true;
|
||||
result:=char(xm)+result;
|
||||
until x=0;
|
||||
xm := xm or $80;
|
||||
if x > 0 then
|
||||
b := True;
|
||||
Result := Char(xm) + Result;
|
||||
until x = 0;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNDecOIDitem}
|
||||
function ASNDecOIDitem(var Start: integer; Buffer: string): integer;
|
||||
|
||||
function ASNDecOIDItem(var Start: Integer; const Buffer: string): Integer;
|
||||
var
|
||||
x:integer;
|
||||
b:boolean;
|
||||
x: Integer;
|
||||
b: Boolean;
|
||||
begin
|
||||
result:=0;
|
||||
Result := 0;
|
||||
repeat
|
||||
result:=result*128;
|
||||
Result := Result * 128;
|
||||
x := Ord(Buffer[Start]);
|
||||
inc(start);
|
||||
b:=x>$7f;
|
||||
x:=x and $7f;
|
||||
result:=result+x;
|
||||
if not b
|
||||
then break;
|
||||
until false
|
||||
Inc(Start);
|
||||
b := x > $7F;
|
||||
x := x and $7F;
|
||||
Result := Result + x;
|
||||
until not b;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNEncLen}
|
||||
function ASNEncLen(Len: integer): string;
|
||||
|
||||
function ASNEncLen(Len: Integer): string;
|
||||
var
|
||||
x, y: integer;
|
||||
x, y: Integer;
|
||||
begin
|
||||
if (len<$80)
|
||||
then result:=char(len)
|
||||
else
|
||||
begin
|
||||
x:=len;
|
||||
result:='';
|
||||
repeat
|
||||
y:=x mod 256;
|
||||
x:=x div 256;
|
||||
result:=char(y)+result;
|
||||
until x=0;
|
||||
y:=length(result);
|
||||
y:=y or $80;
|
||||
result:=char(y)+result;
|
||||
end;
|
||||
if Len < $80 then
|
||||
Result := Char(Len)
|
||||
else
|
||||
begin
|
||||
x := Len;
|
||||
Result := '';
|
||||
repeat
|
||||
y := x mod 256;
|
||||
x := x div 256;
|
||||
Result := Char(y) + Result;
|
||||
until x = 0;
|
||||
y := Length(Result);
|
||||
y := y or $80;
|
||||
Result := Char(y) + Result;
|
||||
end;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNDecLen}
|
||||
function ASNDecLen(var Start: integer; Buffer: string): integer;
|
||||
|
||||
function ASNDecLen(var Start: Integer; const Buffer: string): Integer;
|
||||
var
|
||||
x,n: integer;
|
||||
x, n: Integer;
|
||||
begin
|
||||
x:=Ord(Buffer[Start]);
|
||||
x := Ord(Buffer[Start]);
|
||||
Inc(Start);
|
||||
if (x<$80)
|
||||
then Result:=x
|
||||
else
|
||||
begin
|
||||
result:=0;
|
||||
x:=x and $7f;
|
||||
for n:=1 to x do
|
||||
begin
|
||||
result:=result*256;
|
||||
x:=Ord(Buffer[Start]);
|
||||
Inc(Start);
|
||||
result:=result+x;
|
||||
end;
|
||||
end;
|
||||
if x < $80 then
|
||||
Result := x
|
||||
else
|
||||
begin
|
||||
Result := 0;
|
||||
x := x and $7F;
|
||||
for n := 1 to x do
|
||||
begin
|
||||
Result := Result * 256;
|
||||
x := Ord(Buffer[Start]);
|
||||
Inc(Start);
|
||||
Result := Result + x;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNEncInt}
|
||||
function ASNEncInt(Value: integer): string;
|
||||
|
||||
function ASNEncInt(Value: Integer): string;
|
||||
var
|
||||
x,y:cardinal;
|
||||
neg:boolean;
|
||||
x, y: Cardinal;
|
||||
neg: Boolean;
|
||||
begin
|
||||
neg:=value<0;
|
||||
x:=abs(Value);
|
||||
neg := Value < 0;
|
||||
x := Abs(Value);
|
||||
if neg then
|
||||
x:=not (x-1);
|
||||
result:='';
|
||||
x := not (x - 1);
|
||||
Result := '';
|
||||
repeat
|
||||
y:=x mod 256;
|
||||
x:=x div 256;
|
||||
result:=char(y)+result;
|
||||
until x=0;
|
||||
if (not neg) and (result[1]>#$7F)
|
||||
then result:=#0+result;
|
||||
y := x mod 256;
|
||||
x := x div 256;
|
||||
Result := Char(y) + Result;
|
||||
until x = 0;
|
||||
if (not neg) and (Result[1] > #$7F) then
|
||||
Result := #0 + Result;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNEncUInt}
|
||||
function ASNEncUInt(Value: integer): string;
|
||||
|
||||
function ASNEncUInt(Value: Integer): string;
|
||||
var
|
||||
x,y:integer;
|
||||
neg:boolean;
|
||||
x, y: Integer;
|
||||
neg: Boolean;
|
||||
begin
|
||||
neg:=value<0;
|
||||
x:=Value;
|
||||
if neg
|
||||
then x:=x and $7FFFFFFF;
|
||||
result:='';
|
||||
neg := Value < 0;
|
||||
x := Value;
|
||||
if neg then
|
||||
x := x and $7FFFFFFF;
|
||||
Result := '';
|
||||
repeat
|
||||
y:=x mod 256;
|
||||
x:=x div 256;
|
||||
result:=char(y)+result;
|
||||
until x=0;
|
||||
if neg
|
||||
then result[1]:=char(ord(result[1]) or $80);
|
||||
y := x mod 256;
|
||||
x := x div 256;
|
||||
Result := Char(y) + Result;
|
||||
until x = 0;
|
||||
if neg then
|
||||
Result[1] := Char(Ord(Result[1]) or $80);
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNObject}
|
||||
function ASNObject(Data: string; ASNType: integer): string;
|
||||
|
||||
function ASNObject(const Data: string; ASNType: Integer): string;
|
||||
begin
|
||||
Result := Char(ASNType) + ASNEncLen(Length(Data)) + Data;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{ASNItem}
|
||||
function ASNItem(var Start: integer; Buffer: string; var ValueType:integer): string;
|
||||
|
||||
function ASNItem(var Start: Integer; const Buffer: string;
|
||||
var ValueType: Integer): string;
|
||||
var
|
||||
ASNType: integer;
|
||||
ASNSize: integer;
|
||||
y,n: integer;
|
||||
ASNType: Integer;
|
||||
ASNSize: Integer;
|
||||
y, n: Integer;
|
||||
x: byte;
|
||||
s: string;
|
||||
c: char;
|
||||
neg: boolean;
|
||||
l:integer;
|
||||
neg: Boolean;
|
||||
l: Integer;
|
||||
begin
|
||||
Result:='';
|
||||
ValueType:=ASN1_NULL;
|
||||
l:=length(buffer);
|
||||
if l<(start+1)
|
||||
then exit;
|
||||
Result := '';
|
||||
ValueType := ASN1_NULL;
|
||||
l := Length(Buffer);
|
||||
if l < (Start + 1) then
|
||||
Exit;
|
||||
ASNType := Ord(Buffer[Start]);
|
||||
Valuetype:=ASNType;
|
||||
Inc(start);
|
||||
ValueType := ASNType;
|
||||
Inc(Start);
|
||||
ASNSize := ASNDecLen(Start, Buffer);
|
||||
if (Start+ASNSize-1)>l
|
||||
then exit;
|
||||
if ((ASNType and $20) > 0) then
|
||||
begin
|
||||
Result := '$' + IntToHex(ASNType, 2);
|
||||
end
|
||||
if (Start + ASNSize - 1) > l then
|
||||
Exit;
|
||||
if (ASNType and $20) > 0 then
|
||||
Result := '$' + IntToHex(ASNType, 2)
|
||||
else
|
||||
case ASNType of
|
||||
ASN1_INT:
|
||||
begin
|
||||
y := 0;
|
||||
neg:=false;
|
||||
neg := False;
|
||||
for n := 1 to ASNSize do
|
||||
begin
|
||||
x:=Ord(Buffer[Start]);
|
||||
if (n=1) and (x>$7F)
|
||||
then neg:=true;
|
||||
if neg
|
||||
then x:=not x;
|
||||
x := Ord(Buffer[Start]);
|
||||
if (n = 1) and (x > $7F) then
|
||||
neg := True;
|
||||
if neg then
|
||||
x := not x;
|
||||
y := y * 256 + x;
|
||||
Inc(Start);
|
||||
end;
|
||||
if neg
|
||||
then y:=-(y+1);
|
||||
if neg then
|
||||
y := -(y + 1);
|
||||
Result := IntToStr(y);
|
||||
end;
|
||||
ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
|
||||
@ -299,101 +298,95 @@ begin
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{MibToId}
|
||||
function MibToId(mib:string):string;
|
||||
var
|
||||
x:integer;
|
||||
|
||||
Function walkInt(var s:string):integer;
|
||||
function MibToId(Mib: string): string;
|
||||
var
|
||||
x: Integer;
|
||||
|
||||
function WalkInt(var s: string): Integer;
|
||||
var
|
||||
x:integer;
|
||||
t:string;
|
||||
x: Integer;
|
||||
t: string;
|
||||
begin
|
||||
x:=pos('.',s);
|
||||
if x<1 then
|
||||
begin
|
||||
t:=s;
|
||||
s:='';
|
||||
end
|
||||
else
|
||||
begin
|
||||
t:=copy(s,1,x-1);
|
||||
s:=copy(s,x+1,length(s)-x);
|
||||
end;
|
||||
result:=StrToIntDef(t,0);
|
||||
x := Pos('.', s);
|
||||
if x < 1 then
|
||||
begin
|
||||
t := s;
|
||||
s := '';
|
||||
end
|
||||
else
|
||||
begin
|
||||
t := Copy(s, 1, x - 1);
|
||||
s := Copy(s, x + 1, Length(s) - x);
|
||||
end;
|
||||
Result := StrToIntDef(t, 0);
|
||||
end;
|
||||
|
||||
begin
|
||||
result:='';
|
||||
x:=walkint(mib);
|
||||
x:=x*40+walkint(mib);
|
||||
result:=ASNEncOIDItem(x);
|
||||
while mib<>'' do
|
||||
begin
|
||||
x:=walkint(mib);
|
||||
result:=result+ASNEncOIDItem(x);
|
||||
end;
|
||||
Result := '';
|
||||
x := WalkInt(Mib);
|
||||
x := x * 40 + WalkInt(Mib);
|
||||
Result := ASNEncOIDItem(x);
|
||||
while Mib <> '' do
|
||||
begin
|
||||
x := WalkInt(Mib);
|
||||
Result := Result + ASNEncOIDItem(x);
|
||||
end;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{IdToMib}
|
||||
Function IdToMib(id:string):string;
|
||||
|
||||
function IdToMib(const Id: string): string;
|
||||
var
|
||||
x,y,n:integer;
|
||||
x, y, n: Integer;
|
||||
begin
|
||||
result:='';
|
||||
n:=1;
|
||||
while length(id)+1>n do
|
||||
Result := '';
|
||||
n := 1;
|
||||
while Length(Id) + 1 > n do
|
||||
begin
|
||||
x := ASNDecOIDItem(n, Id);
|
||||
if (n - 1) = 1 then
|
||||
begin
|
||||
x:=ASNDecOIDItem(n,id);
|
||||
if (n-1)=1 then
|
||||
begin
|
||||
y:=x div 40;
|
||||
x:=x mod 40;
|
||||
result:=IntTostr(y);
|
||||
end;
|
||||
result:=result+'.'+IntToStr(x);
|
||||
y := x div 40;
|
||||
x := x mod 40;
|
||||
Result := IntToStr(y);
|
||||
end;
|
||||
Result := Result + '.' + IntToStr(x);
|
||||
end;
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{IntMibToStr}
|
||||
Function IntMibToStr(int:string):string;
|
||||
Var
|
||||
n,y:integer;
|
||||
|
||||
function IntMibToStr(const Value: string): string;
|
||||
var
|
||||
n, y: Integer;
|
||||
begin
|
||||
y:=0;
|
||||
for n:=1 to length(int)-1 do
|
||||
y:=y*256+ord(int[n]);
|
||||
result:=IntToStr(y);
|
||||
y := 0;
|
||||
for n := 1 to Length(Value) - 1 do
|
||||
y := y * 256 + Ord(Value[n]);
|
||||
Result := IntToStr(y);
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
{IPToID} //Hernan Sanchez
|
||||
//Hernan Sanchez
|
||||
|
||||
function IPToID(Host: string): string;
|
||||
var
|
||||
s, t: string;
|
||||
i, x: integer;
|
||||
i, x: Integer;
|
||||
begin
|
||||
Result := '';
|
||||
for x:= 1 to 3 do
|
||||
begin
|
||||
t := '';
|
||||
s := StrScan(PChar(Host), '.');
|
||||
t := Copy(Host, 1, (Length(Host) - Length(s)));
|
||||
Delete(Host, 1, (Length(Host) - Length(s) + 1));
|
||||
i := StrTointDef(t, 0);
|
||||
Result := Result + Chr(i);
|
||||
end;
|
||||
i := StrTointDef(Host, 0);
|
||||
for x := 1 to 3 do
|
||||
begin
|
||||
t := '';
|
||||
s := StrScan(PChar(Host), '.');
|
||||
t := Copy(Host, 1, (Length(Host) - Length(s)));
|
||||
Delete(Host, 1, (Length(Host) - Length(s) + 1));
|
||||
i := StrToIntDef(t, 0);
|
||||
Result := Result + Chr(i);
|
||||
end;
|
||||
i := StrToIntDef(Host, 0);
|
||||
Result := Result + Chr(i);
|
||||
end;
|
||||
|
||||
{==============================================================================}
|
||||
|
||||
begin
|
||||
exit;
|
||||
asm
|
||||
db 'Synapse ASN.1 library by Lukas Gebauer',0
|
||||
end;
|
||||
end.
|
||||
|
||||
|
Reference in New Issue
Block a user