2008-04-23 23:32:54 +03:00
|
|
|
{==============================================================================|
|
2021-06-18 15:16:38 +02:00
|
|
|
| Project : Ararat Synapse | 002.001.001 |
|
2008-04-23 23:32:54 +03:00
|
|
|
|==============================================================================|
|
2008-04-24 10:12:01 +03:00
|
|
|
| Content: support for ASN.1 BER coding and decoding |
|
2008-04-23 23:32:54 +03:00
|
|
|
|==============================================================================|
|
2021-06-18 15:16:38 +02:00
|
|
|
| Copyright (c)1999-2021, Lukas Gebauer |
|
2008-04-24 10:20:39 +03:00
|
|
|
| All rights reserved. |
|
2008-04-23 23:32:54 +03:00
|
|
|
| |
|
2008-04-24 10:20:39 +03:00
|
|
|
| Redistribution and use in source and binary forms, with or without |
|
|
|
|
| modification, are permitted provided that the following conditions are met: |
|
|
|
|
| |
|
|
|
|
| Redistributions of source code must retain the above copyright notice, this |
|
|
|
|
| list of conditions and the following disclaimer. |
|
|
|
|
| |
|
|
|
|
| Redistributions in binary form must reproduce the above copyright notice, |
|
|
|
|
| this list of conditions and the following disclaimer in the documentation |
|
|
|
|
| and/or other materials provided with the distribution. |
|
|
|
|
| |
|
|
|
|
| Neither the name of Lukas Gebauer nor the names of its contributors may |
|
|
|
|
| be used to endorse or promote products derived from this software without |
|
|
|
|
| specific prior written permission. |
|
|
|
|
| |
|
|
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
|
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
|
|
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
|
|
|
|
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
|
|
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
|
|
|
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
|
|
|
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
|
|
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
|
|
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
|
|
|
| DAMAGE. |
|
2008-04-23 23:32:54 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2021-06-18 15:16:38 +02:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c) 1999-2021 |
|
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:29:09 +03:00
|
|
|
{: @abstract(Utilities for handling ASN.1 BER encoding)
|
|
|
|
By this unit you can parse ASN.1 BER encoded data to elements or build back any
|
|
|
|
elements to ASN.1 BER encoded buffer. You can dump ASN.1 BER encoded data to
|
|
|
|
human readable form for easy debugging, too.
|
|
|
|
|
|
|
|
Supported element types are: ASN1_BOOL, ASN1_INT, ASN1_OCTSTR, ASN1_NULL,
|
|
|
|
ASN1_OBJID, ASN1_ENUM, ASN1_SEQ, ASN1_SETOF, ASN1_IPADDR, ASN1_COUNTER,
|
|
|
|
ASN1_GAUGE, ASN1_TIMETICKS, ASN1_OPAQUE
|
|
|
|
|
2008-04-24 10:39:12 +03:00
|
|
|
For sample of using, look to @link(TSnmpSend) or @link(TLdapSend)class.
|
2008-04-24 10:29:09 +03:00
|
|
|
}
|
|
|
|
|
2008-04-24 10:00:43 +03:00
|
|
|
{$Q-}
|
2008-04-24 10:25:18 +03:00
|
|
|
{$H+}
|
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE DELPHI}
|
|
|
|
{$ENDIF}
|
2008-04-24 10:00:43 +03:00
|
|
|
|
2010-02-03 16:53:03 +02:00
|
|
|
{$IFDEF UNICODE}
|
|
|
|
{$WARN IMPLICIT_STRING_CAST OFF}
|
|
|
|
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2008-04-24 10:25:18 +03:00
|
|
|
unit asn1util;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-04-24 10:39:12 +03:00
|
|
|
SysUtils, Classes, synautil;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
|
|
|
const
|
2008-04-24 10:25:18 +03:00
|
|
|
ASN1_BOOL = $01;
|
2008-04-23 23:39:31 +03:00
|
|
|
ASN1_INT = $02;
|
|
|
|
ASN1_OCTSTR = $04;
|
|
|
|
ASN1_NULL = $05;
|
|
|
|
ASN1_OBJID = $06;
|
2008-04-24 10:25:18 +03:00
|
|
|
ASN1_ENUM = $0a;
|
2008-04-23 23:39:31 +03:00
|
|
|
ASN1_SEQ = $30;
|
2008-04-24 10:25:18 +03:00
|
|
|
ASN1_SETOF = $31;
|
2008-04-23 23:39:31 +03:00
|
|
|
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;
|
2014-03-03 17:34:46 +03:00
|
|
|
ASN1_COUNTER64 = $46;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
2008-04-24 10:29:09 +03:00
|
|
|
{:Encodes OID item to binary form.}
|
2013-02-08 12:21:03 +03:00
|
|
|
function ASNEncOIDItem(Value: Int64): AnsiString;
|
2008-04-24 10:29:09 +03:00
|
|
|
|
|
|
|
{:Decodes an OID item of the next element in the "Buffer" from the "Start"
|
|
|
|
position.}
|
2013-02-08 12:21:03 +03:00
|
|
|
function ASNDecOIDItem(var Start: Integer; const Buffer: AnsiString): Int64;
|
2008-04-24 10:29:09 +03:00
|
|
|
|
|
|
|
{:Encodes the length of ASN.1 element to binary.}
|
|
|
|
function ASNEncLen(Len: Integer): AnsiString;
|
|
|
|
|
|
|
|
{:Decodes length of next element in "Buffer" from the "Start" position.}
|
|
|
|
function ASNDecLen(var Start: Integer; const Buffer: AnsiString): Integer;
|
|
|
|
|
|
|
|
{:Encodes a signed integer to ASN.1 binary}
|
2013-02-05 19:16:35 +03:00
|
|
|
function ASNEncInt(Value: Int64): AnsiString;
|
2008-04-24 10:29:09 +03:00
|
|
|
|
|
|
|
{:Encodes unsigned integer into ASN.1 binary}
|
|
|
|
function ASNEncUInt(Value: Integer): AnsiString;
|
|
|
|
|
|
|
|
{:Encodes ASN.1 object to binary form.}
|
|
|
|
function ASNObject(const Data: AnsiString; ASNType: Integer): AnsiString;
|
|
|
|
|
|
|
|
{:Beginning with the "Start" position, decode the ASN.1 item of the next element
|
|
|
|
in "Buffer". Type of item is stored in "ValueType."}
|
|
|
|
function ASNItem(var Start: Integer; const Buffer: AnsiString;
|
|
|
|
var ValueType: Integer): AnsiString;
|
|
|
|
|
|
|
|
{:Encodes an MIB OID string to binary form.}
|
|
|
|
function MibToId(Mib: String): AnsiString;
|
|
|
|
|
|
|
|
{:Decodes MIB OID from binary form to string form.}
|
|
|
|
function IdToMib(const Id: AnsiString): String;
|
|
|
|
|
|
|
|
{:Encodes an one number from MIB OID to binary form. (used internally from
|
|
|
|
@link(MibToId))}
|
|
|
|
function IntMibToStr(const Value: AnsiString): AnsiString;
|
|
|
|
|
|
|
|
{:Convert ASN.1 BER encoded buffer to human readable form for debugging.}
|
|
|
|
function ASNdump(const Value: AnsiString): AnsiString;
|
2008-04-23 23:32:54 +03:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2013-02-08 12:21:03 +03:00
|
|
|
function ASNEncOIDItem(Value: Int64): AnsiString;
|
2008-04-23 23:46:58 +03:00
|
|
|
var
|
2013-02-08 12:21:03 +03:00
|
|
|
x: Int64;
|
|
|
|
xm: Byte;
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(xm) + Result;
|
2008-04-24 10:05:26 +03:00
|
|
|
until x = 0;
|
2008-04-23 23:46:58 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2013-02-08 12:21:03 +03:00
|
|
|
function ASNDecOIDItem(var Start: Integer; const Buffer: AnsiString): Int64;
|
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:29:09 +03:00
|
|
|
function ASNEncLen(Len: Integer): AnsiString;
|
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
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(Len)
|
2008-04-24 10:05:26 +03:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
x := Len;
|
|
|
|
Result := '';
|
|
|
|
repeat
|
|
|
|
y := x mod 256;
|
|
|
|
x := x div 256;
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(y) + Result;
|
2008-04-24 10:05:26 +03:00
|
|
|
until x = 0;
|
|
|
|
y := Length(Result);
|
|
|
|
y := y or $80;
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(y) + Result;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:29:09 +03:00
|
|
|
function ASNDecLen(var Start: Integer; const Buffer: AnsiString): 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
|
|
|
{==============================================================================}
|
2013-02-05 19:16:35 +03:00
|
|
|
function ASNEncInt(Value: Int64): AnsiString;
|
2008-04-23 23:49:43 +03:00
|
|
|
var
|
2013-02-05 19:16:35 +03:00
|
|
|
x: Int64;
|
|
|
|
y: byte;
|
2008-04-24 10:05:26 +03:00
|
|
|
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
|
2013-02-05 19:16:35 +03:00
|
|
|
x := x - 1;
|
2008-04-24 10:05:26 +03:00
|
|
|
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;
|
2013-02-05 19:16:35 +03:00
|
|
|
if neg then
|
|
|
|
y := not y;
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(y) + Result;
|
2008-04-24 10:05:26 +03:00
|
|
|
until x = 0;
|
|
|
|
if (not neg) and (Result[1] > #$7F) then
|
|
|
|
Result := #0 + Result;
|
2013-02-05 19:16:35 +03:00
|
|
|
if (neg) and (Result[1] < #$80) then
|
|
|
|
Result := #$FF + Result;
|
2008-04-23 23:49:43 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:29:09 +03:00
|
|
|
function ASNEncUInt(Value: Integer): AnsiString;
|
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;
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(y) + Result;
|
2008-04-24 10:05:26 +03:00
|
|
|
until x = 0;
|
|
|
|
if neg then
|
2008-04-24 10:29:09 +03:00
|
|
|
Result[1] := AnsiChar(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:29:09 +03:00
|
|
|
function ASNObject(const Data: AnsiString; ASNType: Integer): AnsiString;
|
2008-04-23 23:32:54 +03:00
|
|
|
begin
|
2008-04-24 10:29:09 +03:00
|
|
|
Result := AnsiChar(ASNType) + ASNEncLen(Length(Data)) + Data;
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:29:09 +03:00
|
|
|
function ASNItem(var Start: Integer; const Buffer: AnsiString;
|
|
|
|
var ValueType: Integer): AnsiString;
|
2008-04-23 23:32:54 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
ASNType: Integer;
|
|
|
|
ASNSize: Integer;
|
2013-02-05 19:16:35 +03:00
|
|
|
y: int64;
|
|
|
|
n: Integer;
|
2008-04-23 23:49:43 +03:00
|
|
|
x: byte;
|
2008-04-24 10:29:09 +03:00
|
|
|
s: AnsiString;
|
|
|
|
c: AnsiChar;
|
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
|
2008-04-24 10:25:18 +03:00
|
|
|
// Result := '$' + IntToHex(ASNType, 2)
|
|
|
|
Result := Copy(Buffer, Start, ASNSize)
|
2008-04-23 23:32:54 +03:00
|
|
|
else
|
|
|
|
case ASNType of
|
2008-04-24 10:25:18 +03:00
|
|
|
ASN1_INT, ASN1_ENUM, ASN1_BOOL:
|
2008-04-23 23:49:43 +03:00
|
|
|
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;
|
2014-03-03 17:34:46 +03:00
|
|
|
ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS, ASN1_COUNTER64:
|
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
|
2008-04-24 10:29:09 +03:00
|
|
|
c := AnsiChar(Buffer[Start]);
|
2008-04-23 23:39:31 +03:00
|
|
|
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
|
2008-04-24 10:29:09 +03:00
|
|
|
c := AnsiChar(Buffer[Start]);
|
2008-04-23 23:39:31 +03:00
|
|
|
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-24 10:25:18 +03:00
|
|
|
ASN1_NULL:
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
Start := Start + ASNSize;
|
|
|
|
end;
|
|
|
|
else // unknown
|
2008-04-23 23:39:31 +03:00
|
|
|
begin
|
2008-04-24 10:25:18 +03:00
|
|
|
for n := 1 to ASNSize do
|
|
|
|
begin
|
2008-04-24 10:29:09 +03:00
|
|
|
c := AnsiChar(Buffer[Start]);
|
2008-04-24 10:25:18 +03:00
|
|
|
Inc(Start);
|
|
|
|
s := s + c;
|
|
|
|
end;
|
|
|
|
Result := s;
|
2008-04-23 23:39:31 +03:00
|
|
|
end;
|
2008-04-23 23:32:54 +03:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:29:09 +03:00
|
|
|
function MibToId(Mib: String): AnsiString;
|
2008-04-23 23:51:17 +03:00
|
|
|
var
|
2021-06-18 15:16:38 +02:00
|
|
|
x: int64;
|
2008-04-23 23:51:17 +03:00
|
|
|
|
2021-06-18 15:16:38 +02:00
|
|
|
function WalkInt(var s: String): int64;
|
2008-04-23 23:51:17 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x: Integer;
|
2008-04-24 10:29:09 +03:00
|
|
|
t: AnsiString;
|
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;
|
2021-06-18 15:16:38 +02:00
|
|
|
Result := StrToInt64Def(t, 0);
|
2008-04-24 10:05:26 +03:00
|
|
|
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:29:09 +03:00
|
|
|
function IdToMib(const Id: AnsiString): String;
|
2008-04-23 23:51:17 +03:00
|
|
|
var
|
2021-06-18 15:16:38 +02:00
|
|
|
x, y: int64;
|
|
|
|
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:29:09 +03:00
|
|
|
function IntMibToStr(const Value: AnsiString): AnsiString;
|
2008-04-24 10:05:26 +03:00
|
|
|
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-24 10:25:18 +03:00
|
|
|
{==============================================================================}
|
2008-04-24 10:29:09 +03:00
|
|
|
function ASNdump(const Value: AnsiString): AnsiString;
|
2008-04-24 10:25:18 +03:00
|
|
|
var
|
|
|
|
i, at, x, n: integer;
|
2008-04-24 10:29:09 +03:00
|
|
|
s, indent: AnsiString;
|
2008-04-24 10:25:18 +03:00
|
|
|
il: TStringList;
|
|
|
|
begin
|
|
|
|
il := TStringList.Create;
|
|
|
|
try
|
|
|
|
Result := '';
|
|
|
|
i := 1;
|
|
|
|
indent := '';
|
|
|
|
while i < Length(Value) do
|
|
|
|
begin
|
|
|
|
for n := il.Count - 1 downto 0 do
|
|
|
|
begin
|
|
|
|
x := StrToIntDef(il[n], 0);
|
|
|
|
if x <= i then
|
|
|
|
begin
|
|
|
|
il.Delete(n);
|
|
|
|
Delete(indent, 1, 2);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
s := ASNItem(i, Value, at);
|
|
|
|
Result := Result + indent + '$' + IntToHex(at, 2);
|
|
|
|
if (at and $20) > 0 then
|
|
|
|
begin
|
|
|
|
x := Length(s);
|
|
|
|
Result := Result + ' constructed: length ' + IntToStr(x);
|
|
|
|
indent := indent + ' ';
|
|
|
|
il.Add(IntToStr(x + i - 1));
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
case at of
|
|
|
|
ASN1_BOOL:
|
|
|
|
Result := Result + ' BOOL: ';
|
|
|
|
ASN1_INT:
|
|
|
|
Result := Result + ' INT: ';
|
|
|
|
ASN1_ENUM:
|
|
|
|
Result := Result + ' ENUM: ';
|
|
|
|
ASN1_COUNTER:
|
|
|
|
Result := Result + ' COUNTER: ';
|
|
|
|
ASN1_GAUGE:
|
|
|
|
Result := Result + ' GAUGE: ';
|
|
|
|
ASN1_TIMETICKS:
|
|
|
|
Result := Result + ' TIMETICKS: ';
|
|
|
|
ASN1_OCTSTR:
|
|
|
|
Result := Result + ' OCTSTR: ';
|
|
|
|
ASN1_OPAQUE:
|
|
|
|
Result := Result + ' OPAQUE: ';
|
|
|
|
ASN1_OBJID:
|
|
|
|
Result := Result + ' OBJID: ';
|
|
|
|
ASN1_IPADDR:
|
|
|
|
Result := Result + ' IPADDR: ';
|
|
|
|
ASN1_NULL:
|
|
|
|
Result := Result + ' NULL: ';
|
2014-03-03 17:34:46 +03:00
|
|
|
ASN1_COUNTER64:
|
|
|
|
Result := Result + ' COUNTER64: ';
|
2008-04-24 10:25:18 +03:00
|
|
|
else // other
|
|
|
|
Result := Result + ' unknown: ';
|
|
|
|
end;
|
2008-04-24 10:29:09 +03:00
|
|
|
if IsBinaryString(s) then
|
|
|
|
s := DumpExStr(s);
|
2008-04-24 10:25:18 +03:00
|
|
|
Result := Result + s;
|
|
|
|
end;
|
|
|
|
Result := Result + #$0d + #$0a;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
il.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:51:17 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:32:54 +03:00
|
|
|
end.
|