Skip to content

Commit 568f0a5

Browse files
committed
added Asn1Utils.Encode overload and enabled its usage
1 parent f0e143e commit 568f0a5

18 files changed

+38
-29
lines changed

Asn1Parser/Asn1Builder.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ namespace SysadminsLV.Asn1Parser {
1414
public class Asn1Builder {
1515
readonly List<Byte> _rawData;
1616

17-
/// <summary>
18-
/// Initializes a new instance of <strong>Asn1Builder</strong> class.
19-
/// </summary>
20-
public Asn1Builder() {
17+
Asn1Builder() {
2118
_rawData = new List<Byte>();
2219
}
2320

@@ -264,7 +261,7 @@ public Asn1Builder AddVideotexString(String value) {
264261
if (value == null) {
265262
throw new ArgumentNullException(nameof(value));
266263
}
267-
_rawData.AddRange(Asn1Utils.Encode(Encoding.ASCII.GetBytes(value), (Byte)Asn1Type.VideotexString));
264+
_rawData.AddRange(Asn1Utils.Encode(Encoding.ASCII.GetBytes(value), Asn1Type.VideotexString));
268265
return this;
269266
}
270267
/// <summary>
@@ -434,11 +431,12 @@ public Asn1Builder AddDerData(Byte[] value, Byte outerTag) {
434431
/// </exception>
435432
/// <returns>Current instance with added value.</returns>
436433
/// <remarks>
437-
/// If <strong>mustEncode</strong> parameter is set to <strong>true</strong>, then data in <strong>value</strong> parameter
438-
/// is untagged. If <strong>mustEncode</strong> parameter is set to <strong>false</strong>, then data in <strong>value</strong>
439-
/// parameter is explicitly tagged and only tag name change is necessary. Caller must have knowledge in advance if value is tagged or not.
440-
/// If <strong>mustEncode</strong> parameter is set to <strong>false</strong> and value passed in <strong>value</strong> parameter
441-
/// is untagged, an exception will be thrown.
434+
/// If <strong>mustEncode</strong> parameter is set to <strong>true</strong>, then data in <strong>value</strong>
435+
/// parameter is untagged and this method will encode value with specified tag. If <strong>mustEncode</strong>
436+
/// parameter is set to <strong>false</strong>, then data in <strong>value</strong> parameter is considered
437+
/// encoded method will perform tag replacement. Caller must have knowledge in advance if value is tagged or
438+
/// not. If <strong>mustEncode</strong> parameter is set to <strong>false</strong> and value passed in
439+
/// <strong>value</strong> parameter is untagged, an exception will be thrown.
442440
/// </remarks>
443441
public Asn1Builder AddImplicit(Byte implicitTag, Byte[] value, Boolean mustEncode) {
444442
if (value == null) {
@@ -534,7 +532,7 @@ public Asn1Builder AddOctetString(Func<Asn1Builder, Asn1Builder> selector) {
534532
throw new ArgumentNullException(nameof(selector));
535533
}
536534
Asn1Builder b = selector(new Asn1Builder());
537-
_rawData.AddRange(Asn1Utils.Encode(b._rawData.ToArray(), (Byte)Asn1Type.OCTET_STRING));
535+
_rawData.AddRange(Asn1Utils.Encode(b._rawData.ToArray(), Asn1Type.OCTET_STRING));
538536
return this;
539537
}
540538
/// <summary>
@@ -601,7 +599,7 @@ public Asn1Builder AddExplicit(Byte explicitTag, Func<Asn1Builder, Asn1Builder>
601599
/// A new instance of ASN.1 DER builder that contains the state of the current instance.
602600
/// </returns>
603601
public Asn1Builder Encode(Byte outerType = 0x30) {
604-
IEnumerable<Byte> encoded = GetEncoded(outerType);
602+
IEnumerable<Byte> encoded = Asn1Utils.Encode(_rawData.ToArray(), outerType);
605603
_rawData.Clear();
606604
_rawData.AddRange(encoded);
607605
return new Asn1Builder(this);

Asn1Parser/Asn1Utils.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static Int64 CalculatePayloadLength(Byte[] asnHeader) {
6767
/// </summary>
6868
/// <remarks>This method do not check whether the data in <strong>rawData</strong> is valid data for specified enclosing type.</remarks>
6969
/// <param name="rawData">A byte array to wrap.</param>
70-
/// <param name="enclosingTag">An enumeration of <see cref="Asn1Type"/>.</param>
70+
/// <param name="enclosingTag">An enumeration of <see cref="Asn1Type"/> type represented as byte.</param>
7171
/// <returns>Wrapped encoded byte array.</returns>
7272
/// <remarks>If <strong>rawData</strong> is null, an empty tag is encoded.</remarks>
7373
public static Byte[] Encode(Byte[] rawData, Byte enclosingTag) {
@@ -103,6 +103,17 @@ public static Byte[] Encode(Byte[] rawData, Byte enclosingTag) {
103103
}
104104
return retValue;
105105
}
106+
/// <summary>
107+
/// Wraps encoded data to an ASN.1 type/structure.
108+
/// </summary>
109+
/// <remarks>This method do not check whether the data in <strong>rawData</strong> is valid data for specified enclosing type.</remarks>
110+
/// <param name="rawData">A byte array to wrap.</param>
111+
/// <param name="type">An enumeration of <see cref="Asn1Type"/>.</param>
112+
/// <returns>Wrapped encoded byte array.</returns>
113+
/// <remarks>If <strong>rawData</strong> is null, an empty tag is encoded.</remarks>
114+
public static Byte[] Encode(Byte[] rawData, Asn1Type type) {
115+
return Encode(rawData, (Byte)type);
116+
}
106117
#endregion
107118

108119
#region internal

Asn1Parser/Universal/Asn1BitString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void m_encode(Byte[] value, Boolean calc, Byte unusedBits) {
7171
Byte[] v = new Byte[value.Length + 1];
7272
v[0] = UnusedBits;
7373
value.CopyTo(v, 1);
74-
Initialize(new Asn1Reader(Asn1Utils.Encode(v, Tag)));
74+
Initialize(new Asn1Reader(Asn1Utils.Encode(v, TYPE)));
7575
}
7676

7777
/// <summary>

Asn1Parser/Universal/Asn1BmpString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Asn1BMPString(String inputString) : base(TYPE) {
3838

3939
void m_encode(String inputString) {
4040
Value = inputString;
41-
Initialize(new Asn1Reader(Asn1Utils.Encode(Encoding.BigEndianUnicode.GetBytes(inputString), Tag)));
41+
Initialize(new Asn1Reader(Asn1Utils.Encode(Encoding.BigEndianUnicode.GetBytes(inputString), TYPE)));
4242
}
4343
void m_decode(Asn1Reader asn) {
4444
Value = Encoding.BigEndianUnicode.GetString(asn.GetPayload());

Asn1Parser/Universal/Asn1Boolean.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Asn1Boolean(Boolean fValue) : base(TYPE) {
4141
void m_encode(Boolean fValue) {
4242
Value = fValue;
4343
Byte value = (Byte)(fValue ? 255 : 0);
44-
Initialize(new Asn1Reader(Asn1Utils.Encode(new[] {value}, Tag)));
44+
Initialize(new Asn1Reader(Asn1Utils.Encode(new[] {value}, TYPE)));
4545
}
4646
void m_decode(Asn1Reader asn) {
4747
Value = asn.GetPayload()[0] > 0;

Asn1Parser/Universal/Asn1Enumerated.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Asn1Enumerated(UInt64 inputInteger) : base(TYPE) {
4545

4646
void m_encode(BigInteger inputInteger) {
4747
Value = (UInt64)inputInteger;
48-
Initialize(new Asn1Reader(Asn1Utils.Encode(inputInteger.GetAsnBytes(), Tag)));
48+
Initialize(new Asn1Reader(Asn1Utils.Encode(inputInteger.GetAsnBytes(), TYPE)));
4949
}
5050
void m_decode(Asn1Reader asn) {
5151
var value = new BigInteger(asn.GetPayload().Reverse().ToArray());

Asn1Parser/Universal/Asn1GeneralizedTime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Asn1GeneralizedTime(Byte[] rawData) : this(new Asn1Reader(rawData)) { }
5454
void m_encode(DateTime time, TimeZoneInfo zone, Boolean preciseTime) {
5555
Value = time;
5656
ZoneInfo = zone;
57-
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, zone, false, preciseTime), Tag)));
57+
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, zone, false, preciseTime), TYPE)));
5858
}
5959
void m_decode(Byte[] rawData) {
6060
var asn = new Asn1Reader(rawData);

Asn1Parser/Universal/Asn1IA5String.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void m_encode(String inputString) {
5252
throw new InvalidDataException(String.Format(InvalidType, TYPE.ToString()));
5353
}
5454
Value = inputString;
55-
Initialize(new Asn1Reader(Asn1Utils.Encode(Encoding.ASCII.GetBytes(inputString), Tag)));
55+
Initialize(new Asn1Reader(Asn1Utils.Encode(Encoding.ASCII.GetBytes(inputString), TYPE)));
5656
}
5757
void m_decode(Asn1Reader asn) {
5858
if (asn.GetPayload().Any(b => b > 127)) {

Asn1Parser/Universal/Asn1Integer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Asn1Integer(BigInteger inputInteger) : base(TYPE) {
4444

4545
void m_encode(BigInteger inputInteger) {
4646
Value = inputInteger;
47-
Initialize(new Asn1Reader(Asn1Utils.Encode(inputInteger.GetAsnBytes(), Tag)));
47+
Initialize(new Asn1Reader(Asn1Utils.Encode(inputInteger.GetAsnBytes(), TYPE)));
4848
}
4949
void m_decode(Asn1Reader asn) {
5050
Value = new BigInteger(asn.GetPayload().Reverse().ToArray());

Asn1Parser/Universal/Asn1NumericString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void m_encode(String inputString) {
5252
throw new InvalidDataException(String.Format(InvalidType, TYPE.ToString()));
5353
}
5454
Value = inputString;
55-
Initialize(new Asn1Reader(Asn1Utils.Encode(Encoding.ASCII.GetBytes(inputString), Tag)));
55+
Initialize(new Asn1Reader(Asn1Utils.Encode(Encoding.ASCII.GetBytes(inputString), TYPE)));
5656
}
5757
void m_decode(Asn1Reader asn) {
5858
if (asn.GetPayload().Any(b => (b < 48 || b > 57) && b != 32)) {

0 commit comments

Comments
 (0)