2008-04-23 23:32:54 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 10:09:13 +03:00
|
|
|
| Project : Delphree - Synapse | 001.003.004 |
|
2008-04-23 23:32:54 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: support for ASN.1 coding and decoding |
|
|
|
|
|==============================================================================|
|
2008-04-24 10:05:26 +03:00
|
|
|
| The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
|
2008-04-23 23:32:54 +03:00
|
|
|
| (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/ |
|
|
|
|
| |
|
|
|
|
| Software distributed under the License is distributed on an "AS IS" basis, |
|
|
|
|
| WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
|
|
|
|
| the specific language governing rights and limitations under the License. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Original Code is Synapse Delphi Library. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2008-04-24 09:44:13 +03:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c) 1999,2000,2001. |
|
2008-04-23 23:32:54 +03:00
|
|
|
| Portions created by Hernan Sanchez are Copyright (c) 2000. |
|
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
|
|
|
| Hernan Sanchez (hernan.sanchez@iname.com) |
|
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
2008-04-23 23:48:39 +03:00
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
2008-04-23 23:32:54 +03:00
|
|
|
|==============================================================================}
|
|
|
|
|
2008-04-24 10:00:43 +03:00
|
|
|
{$Q-}
|
2008-04-24 10:05:26 +03:00
|
|
|
{$WEAKPACKAGEUNIT ON}
|
2008-04-24 10:00:43 +03:00
|
|
|
|
2008-04-23 23:32:54 +03:00
|
|
|
unit ASN1Util;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-04-23 23:51:17 +03:00
|
|
|
SysUtils;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
|
|
|
const
|
2008-04-23 23:39:31 +03:00
|
|
|
ASN1_INT = $02;
|
|
|
|
ASN1_OCTSTR = $04;
|
|
|
|
ASN1_NULL = $05;
|
|
|
|
ASN1_OBJID = $06;
|
|
|
|
ASN1_SEQ = $30;
|
|
|
|
ASN1_IPADDR = $40;
|
|
|
|
ASN1_COUNTER = $41;
|
|
|
|
ASN1_GAUGE = $42;
|
2008-04-23 23:32:54 +03:00
|
|
|
ASN1_TIMETICKS = $43;
|
2008-04-23 23:49:43 +03:00
|
|
|
ASN1_OPAQUE = $44;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNEncOIDItem(Value: Integer): string;
|
2008-04-23 23:46:58 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x, xm: Integer;
|
|
|
|
b: Boolean;
|
2008-04-23 23:46:58 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
x := Value;
|
|
|
|
b := False;
|
|
|
|
Result := '';
|
2008-04-23 23:46:58 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
xm := x mod 128;
|
|
|
|
x := x div 128;
|
2008-04-23 23:46:58 +03:00
|
|
|
if b then
|
2008-04-24 10:05:26 +03:00
|
|
|
xm := xm or $80;
|
|
|
|
if x > 0 then
|
|
|
|
b := True;
|
|
|
|
Result := Char(xm) + Result;
|
|
|
|
until x = 0;
|
2008-04-23 23:46:58 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNDecOIDItem(var Start: Integer; const Buffer: string): Integer;
|
2008-04-23 23:46:58 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x: Integer;
|
|
|
|
b: Boolean;
|
2008-04-23 23:46:58 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := 0;
|
2008-04-23 23:46:58 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := Result * 128;
|
2008-04-23 23:46:58 +03:00
|
|
|
x := Ord(Buffer[Start]);
|
2008-04-24 10:05:26 +03:00
|
|
|
Inc(Start);
|
|
|
|
b := x > $7F;
|
|
|
|
x := x and $7F;
|
|
|
|
Result := Result + x;
|
|
|
|
until not b;
|
2008-04-23 23:46:58 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNEncLen(Len: Integer): string;
|
2008-04-23 23:32:54 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x, y: Integer;
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNDecLen(var Start: Integer; const Buffer: string): Integer;
|
2008-04-23 23:32:54 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x, n: Integer;
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
x := Ord(Buffer[Start]);
|
2008-04-23 23:46:58 +03:00
|
|
|
Inc(Start);
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNEncInt(Value: Integer): string;
|
2008-04-23 23:49:43 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x, y: Cardinal;
|
|
|
|
neg: Boolean;
|
2008-04-23 23:49:43 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
neg := Value < 0;
|
|
|
|
x := Abs(Value);
|
2008-04-23 23:49:43 +03:00
|
|
|
if neg then
|
2008-04-24 10:05:26 +03:00
|
|
|
x := not (x - 1);
|
|
|
|
Result := '';
|
2008-04-23 23:49:43 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2008-04-23 23:49:43 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNEncUInt(Value: Integer): string;
|
2008-04-23 23:32:54 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x, y: Integer;
|
|
|
|
neg: Boolean;
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
neg := Value < 0;
|
|
|
|
x := Value;
|
|
|
|
if neg then
|
|
|
|
x := x and $7FFFFFFF;
|
|
|
|
Result := '';
|
2008-04-23 23:46:58 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
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);
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNObject(const Data: string; ASNType: Integer): string;
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
|
|
|
Result := Char(ASNType) + ASNEncLen(Length(Data)) + Data;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function ASNItem(var Start: Integer; const Buffer: string;
|
|
|
|
var ValueType: Integer): string;
|
2008-04-23 23:32:54 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
ASNType: Integer;
|
|
|
|
ASNSize: Integer;
|
|
|
|
y, n: Integer;
|
2008-04-23 23:49:43 +03:00
|
|
|
x: byte;
|
2008-04-23 23:32:54 +03:00
|
|
|
s: string;
|
|
|
|
c: char;
|
2008-04-24 10:05:26 +03:00
|
|
|
neg: Boolean;
|
|
|
|
l: Integer;
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := '';
|
|
|
|
ValueType := ASN1_NULL;
|
|
|
|
l := Length(Buffer);
|
|
|
|
if l < (Start + 1) then
|
|
|
|
Exit;
|
2008-04-23 23:32:54 +03:00
|
|
|
ASNType := Ord(Buffer[Start]);
|
2008-04-24 10:05:26 +03:00
|
|
|
ValueType := ASNType;
|
|
|
|
Inc(Start);
|
2008-04-23 23:32:54 +03:00
|
|
|
ASNSize := ASNDecLen(Start, Buffer);
|
2008-04-24 10:05:26 +03:00
|
|
|
if (Start + ASNSize - 1) > l then
|
|
|
|
Exit;
|
|
|
|
if (ASNType and $20) > 0 then
|
|
|
|
Result := '$' + IntToHex(ASNType, 2)
|
2008-04-23 23:32:54 +03:00
|
|
|
else
|
|
|
|
case ASNType of
|
2008-04-23 23:49:43 +03:00
|
|
|
ASN1_INT:
|
|
|
|
begin
|
|
|
|
y := 0;
|
2008-04-24 10:05:26 +03:00
|
|
|
neg := False;
|
2008-04-23 23:49:43 +03:00
|
|
|
for n := 1 to ASNSize do
|
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
x := Ord(Buffer[Start]);
|
|
|
|
if (n = 1) and (x > $7F) then
|
|
|
|
neg := True;
|
|
|
|
if neg then
|
|
|
|
x := not x;
|
2008-04-23 23:49:43 +03:00
|
|
|
y := y * 256 + x;
|
|
|
|
Inc(Start);
|
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
if neg then
|
|
|
|
y := -(y + 1);
|
2008-04-23 23:49:43 +03:00
|
|
|
Result := IntToStr(y);
|
|
|
|
end;
|
|
|
|
ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
|
|
|
y := 0;
|
2008-04-23 23:39:31 +03:00
|
|
|
for n := 1 to ASNSize do
|
|
|
|
begin
|
|
|
|
y := y * 256 + Ord(Buffer[Start]);
|
|
|
|
Inc(Start);
|
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
Result := IntToStr(y);
|
|
|
|
end;
|
2008-04-23 23:49:43 +03:00
|
|
|
ASN1_OCTSTR, ASN1_OPAQUE:
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
for n := 1 to ASNSize do
|
|
|
|
begin
|
|
|
|
c := Char(Buffer[Start]);
|
|
|
|
Inc(Start);
|
|
|
|
s := s + c;
|
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
Result := s;
|
|
|
|
end;
|
|
|
|
ASN1_OBJID:
|
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
for n := 1 to ASNSize do
|
|
|
|
begin
|
|
|
|
c := Char(Buffer[Start]);
|
|
|
|
Inc(Start);
|
|
|
|
s := s + c;
|
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
Result := IdToMib(s);
|
|
|
|
end;
|
|
|
|
ASN1_IPADDR:
|
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
s := '';
|
|
|
|
for n := 1 to ASNSize do
|
|
|
|
begin
|
|
|
|
if (n <> 1) then
|
|
|
|
s := s + '.';
|
|
|
|
y := Ord(Buffer[Start]);
|
|
|
|
Inc(Start);
|
|
|
|
s := s + IntToStr(y);
|
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
Result := s;
|
|
|
|
end;
|
2008-04-23 23:39:31 +03:00
|
|
|
else // NULL
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
Inc(Start);
|
|
|
|
Start := Start + ASNSize;
|
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function MibToId(Mib: string): string;
|
2008-04-23 23:51:17 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x: Integer;
|
2008-04-23 23:51:17 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function WalkInt(var s: string): Integer;
|
2008-04-23 23:51:17 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x: Integer;
|
|
|
|
t: string;
|
2008-04-23 23:51:17 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
x := Pos('.', s);
|
|
|
|
if x < 1 then
|
|
|
|
begin
|
|
|
|
t := s;
|
|
|
|
s := '';
|
|
|
|
end
|
|
|
|
else
|
2008-04-23 23:51:17 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
t := Copy(s, 1, x - 1);
|
|
|
|
s := Copy(s, x + 1, Length(s) - x);
|
2008-04-23 23:51:17 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2008-04-23 23:51:17 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function IdToMib(const Id: string): string;
|
2008-04-23 23:51:17 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x, y, n: Integer;
|
2008-04-23 23:51:17 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := '';
|
|
|
|
n := 1;
|
|
|
|
while Length(Id) + 1 > n do
|
|
|
|
begin
|
|
|
|
x := ASNDecOIDItem(n, Id);
|
|
|
|
if (n - 1) = 1 then
|
2008-04-23 23:51:17 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
y := x div 40;
|
|
|
|
x := x mod 40;
|
|
|
|
Result := IntToStr(y);
|
2008-04-23 23:51:17 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := Result + '.' + IntToStr(x);
|
|
|
|
end;
|
2008-04-23 23:51:17 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
2008-04-24 10:05:26 +03:00
|
|
|
function IntMibToStr(const Value: string): string;
|
|
|
|
var
|
|
|
|
n, y: Integer;
|
2008-04-23 23:51:17 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
y := 0;
|
|
|
|
for n := 1 to Length(Value) - 1 do
|
|
|
|
y := y * 256 + Ord(Value[n]);
|
|
|
|
Result := IntToStr(y);
|
2008-04-23 23:51:17 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:32:54 +03:00
|
|
|
end.
|