Skip to content

Commit ccc7d57

Browse files
committed
extracted OID encoder into static method because it is reused by Asn1RelativeOid
1 parent 2e4b7ab commit ccc7d57

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Asn1Parser/Utils/OidUtils.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
5+
namespace SysadminsLV.Asn1Parser.Utils;
6+
static class OidUtils {
7+
public static IEnumerable<Byte> EncodeOidArc(BigInteger arcValue) {
8+
List<Byte> rawOid = [];
9+
BigInteger temp = arcValue;
10+
// calculate how many bits are occupied by the current integer value
11+
Int16 bitLength = 0;
12+
do {
13+
temp = (BigInteger)Math.Floor((Double)temp / 2);
14+
bitLength++;
15+
} while (temp > 0);
16+
// calculate how many additional bytes are required and encode each integer in a 7 bit.
17+
// 8th bit of the integer is shifted to the left and 8th bit is set to 1 to indicate that
18+
// additional bytes are related to the current OID arc. Details:
19+
// http://msdn.microsoft.com/en-us/library/bb540809(v=vs.85).aspx
20+
// loop may not execute if arc value is less than 128.
21+
for (Int32 index = (bitLength - 1) / 7; index > 0; index--) {
22+
rawOid.Add((Byte)(0x80 | ((arcValue >> (index * 7)) & 0x7f)));
23+
}
24+
rawOid.Add((Byte)(arcValue & 0x7f));
25+
26+
return rawOid;
27+
}
28+
public static String DecodeOidArc(IEnumerable<Byte> rawData) {
29+
throw new NotImplementedException();
30+
}
31+
}

0 commit comments

Comments
 (0)