1 {==============================================================================|
2 | Project : Delphree - Synapse | 001.003.004 |
3 |==============================================================================|
4 | Content: support for ASN.1 coding and decoding |
5 |==============================================================================|
6 | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
7 | (the "License"); you may not use this file except in compliance with the |
8 | License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
10 | Software distributed under the License is distributed on an "AS IS" basis, |
11 | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
12 | the specific language governing rights and limitations under the License. |
13 |==============================================================================|
14 | The Original Code is Synapse Delphi Library. |
15 |==============================================================================|
16 | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
17 | Portions created by Lukas Gebauer are Copyright (c) 1999,2000,2001. |
18 | Portions created by Hernan Sanchez are Copyright (c) 2000. |
19 | All Rights Reserved. |
20 |==============================================================================|
22 | Hernan Sanchez (hernan.sanchez@iname.com) |
23 |==============================================================================|
24 | History: see HISTORY.HTM from distribution package |
25 | (Found at URL: http://www.ararat.cz/synapse/) |
26 |==============================================================================}
50 function ASNEncOIDItem(Value
: Integer): string;
51 function ASNDecOIDItem(var Start
: Integer; const Buffer
: string): Integer;
52 function ASNEncLen(Len
: Integer): string;
53 function ASNDecLen(var Start
: Integer; const Buffer
: string): Integer;
54 function ASNEncInt(Value
: Integer): string;
55 function ASNEncUInt(Value
: Integer): string;
56 function ASNObject(const Data
: string; ASNType
: Integer): string;
57 function ASNItem(var Start
: Integer; const Buffer
: string;
58 var ValueType
: Integer): string;
59 function MibToId(Mib
: string): string;
60 function IdToMib(const Id
: string): string;
61 function IntMibToStr(const Value
: string): string;
65 {==============================================================================}
66 function ASNEncOIDItem(Value
: Integer): string;
81 Result
:= Char(xm
) + Result
;
85 {==============================================================================}
86 function ASNDecOIDItem(var Start
: Integer; const Buffer
: string): Integer;
93 Result
:= Result
* 128;
94 x
:= Ord(Buffer
[Start
]);
102 {==============================================================================}
103 function ASNEncLen(Len
: Integer): string;
116 Result
:= Char(y
) + Result
;
120 Result
:= Char(y
) + Result
;
124 {==============================================================================}
125 function ASNDecLen(var Start
: Integer; const Buffer
: string): Integer;
129 x
:= Ord(Buffer
[Start
]);
139 Result
:= Result
* 256;
140 x
:= Ord(Buffer
[Start
]);
142 Result
:= Result
+ x
;
147 {==============================================================================}
148 function ASNEncInt(Value
: Integer): string;
161 Result
:= Char(y
) + Result
;
163 if (not neg
) and (Result
[1] > #
$7F) then
164 Result
:= #0 + Result
;
167 {==============================================================================}
168 function ASNEncUInt(Value
: Integer): string;
176 x
:= x
and $7FFFFFFF;
181 Result
:= Char(y
) + Result
;
184 Result
[1] := Char(Ord(Result
[1]) or $80);
187 {==============================================================================}
188 function ASNObject(const Data
: string; ASNType
: Integer): string;
190 Result
:= Char(ASNType
) + ASNEncLen(Length(Data
)) + Data
;
193 {==============================================================================}
194 function ASNItem(var Start
: Integer; const Buffer
: string;
195 var ValueType
: Integer): string;
207 ValueType
:= ASN1_NULL
;
209 if l
< (Start
+ 1) then
211 ASNType
:= Ord(Buffer
[Start
]);
212 ValueType
:= ASNType
;
214 ASNSize
:= ASNDecLen(Start
, Buffer
);
215 if (Start
+ ASNSize
- 1) > l
then
217 if (ASNType
and $20) > 0 then
218 Result
:= '$' + Int2Hex(ASNType
, 2)
225 for n
:= 1 to ASNSize
do
227 x
:= Ord(Buffer
[Start
]);
228 if (n
= 1) and (x
> $7F) then
237 Result
:= Int2Str(y
);
239 ASN1_COUNTER
, ASN1_GAUGE
, ASN1_TIMETICKS
:
242 for n
:= 1 to ASNSize
do
244 y
:= y
* 256 + Ord(Buffer
[Start
]);
247 Result
:= Int2Str(y
);
249 ASN1_OCTSTR
, ASN1_OPAQUE
:
251 for n
:= 1 to ASNSize
do
253 c
:= Char(Buffer
[Start
]);
261 for n
:= 1 to ASNSize
do
263 c
:= Char(Buffer
[Start
]);
267 Result
:= IdToMib(s
);
272 for n
:= 1 to ASNSize
do
276 y
:= Ord(Buffer
[Start
]);
286 Start
:= Start
+ ASNSize
;
291 {==============================================================================}
292 function MibToId(Mib
: string): string;
296 function WalkInt(var s
: string): Integer;
309 t
:= Copy(s
, 1, x
- 1);
310 s
:= Copy(s
, x
+ 1, Length(s
) - x
);
312 Result
:= StrToIntDef(t
, 0);
318 x
:= x
* 40 + WalkInt(Mib
);
319 Result
:= ASNEncOIDItem(x
);
323 Result
:= Result
+ ASNEncOIDItem(x
);
327 {==============================================================================}
328 function IdToMib(const Id
: string): string;
334 while Length(Id
) + 1 > n
do
336 x
:= ASNDecOIDItem(n
, Id
);
341 Result
:= Int2Str(y
);
343 Result
:= Result
+ '.' + Int2Str(x
);
347 {==============================================================================}
348 function IntMibToStr(const Value
: string): string;
353 for n
:= 1 to Length(Value
) - 1 do
354 y
:= y
* 256 + Ord(Value
[n
]);
355 Result
:= Int2Str(y
);
358 {==============================================================================}