Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support ArraySegment<byte>. #472

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1GeneralizedTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public static Asn1GeneralizedTime GetInstance(object obj)
throw new ArgumentException("failed to construct generalized time from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1GeneralizedTime)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct generalized time from byte[]: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public static Asn1Null GetInstance(object obj)
throw new ArgumentException("failed to construct NULL from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1Null)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct NULL from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
41 changes: 26 additions & 15 deletions crypto/src/asn1/Asn1Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,39 @@ public bool Equals(Asn1Object other)
/// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
/// </exception>
public static Asn1Object FromByteArray(byte[] data)
{
{
return FromByteArray(new ArraySegment<byte>(data));
}

/// <summary>Create a base ASN.1 object from a byte array.</summary>
/// <param name="data">The byte array to parse.</param>
/// <returns>The base ASN.1 object represented by the byte array.</returns>
/// <exception cref="IOException">
/// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
/// </exception>
public static Asn1Object FromByteArray(ArraySegment<byte> data)
{
try
{
using (var asn1In = new Asn1InputStream(new MemoryStream(data, false), data.Length))
{
using (var asn1In = new Asn1InputStream(new MemoryStream(data.Array ?? Array.Empty<byte>(), data.Offset, data.Count, false), data.Count))
{
Asn1Object result = asn1In.ReadObject();
if (data.Length != asn1In.Position)
if (data.Count != asn1In.Position)
throw new IOException("extra data found after object");
return result;
}
}
catch (InvalidCastException)
{
throw new IOException("cannot recognise object in byte array");
}
}
}
catch (InvalidCastException)
{
throw new IOException("cannot recognise object in byte array");
}
}

/// <summary>Read a base ASN.1 object from a stream.</summary>
/// <param name="inStr">The stream to parse.</param>
/// <returns>The base ASN.1 object represented by the byte array.</returns>
/// <exception cref="IOException">If there is a problem parsing the data.</exception>
public static Asn1Object FromStream(Stream inStr)
/// <summary>Read a base ASN.1 object from a stream.</summary>
/// <param name="inStr">The stream to parse.</param>
/// <returns>The base ASN.1 object represented by the byte array.</returns>
/// <exception cref="IOException">If there is a problem parsing the data.</exception>
public static Asn1Object FromStream(Stream inStr)
{
try
{
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1ObjectDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public static Asn1ObjectDescriptor GetInstance(object obj)
throw new ArgumentException("failed to construct object descriptor from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1ObjectDescriptor)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct object descriptor from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1OctetString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public static Asn1OctetString GetInstance(object obj)
throw new ArgumentException("failed to construct OCTET STRING from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1OctetString)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct OCTET STRING from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1RelativeOid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public static Asn1RelativeOid GetInstance(object obj)
throw new ArgumentException("failed to construct relative OID from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1RelativeOid)FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct relative OID from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public static Asn1Sequence GetInstance(object obj)
throw new ArgumentException("failed to construct sequence from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1Sequence)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct sequence from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public static Asn1Set GetInstance(object obj)
throw new ArgumentException("failed to construct set from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1Set)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct set from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1TaggedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public static Asn1TaggedObject GetInstance(object obj)
throw new ArgumentException("failed to construct tagged object from byte[]", nameof(obj), e);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return CheckedCast(FromByteArray(arraySegment));
}
catch (IOException e)
{
throw new ArgumentException("failed to construct tagged object from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
}
Expand Down
6 changes: 6 additions & 0 deletions crypto/src/asn1/Asn1UniversalType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ internal Asn1Object FromByteArray(byte[] bytes)
return CheckedCast(Asn1Object.FromByteArray(bytes));
}

/// <exception cref="IOException"/>
internal Asn1Object FromByteArray(ArraySegment<byte> bytes)
{
return CheckedCast(Asn1Object.FromByteArray(bytes));
}

internal Asn1Object GetContextInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
{
if (Asn1Tags.ContextSpecific != taggedObject.TagClass)
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/Asn1UtcTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public static Asn1UtcTime GetInstance(object obj)
throw new ArgumentException("failed to construct UTC time from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (Asn1UtcTime)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct UTC time from byte[]: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DERExternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public static DerExternal GetInstance(object obj)
throw new ArgumentException("failed to construct external from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerExternal)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct external from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerBMPString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public static DerBmpString GetInstance(object obj)
throw new ArgumentException("failed to construct BMP string from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerBmpString)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct BMP string from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerBitString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public static DerBitString GetInstance(object obj)
throw new ArgumentException("failed to construct BIT STRING from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return GetInstance(FromByteArray(arraySegment));
}
catch (IOException e)
{
throw new ArgumentException("failed to construct BIT STRING from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public static DerBoolean GetInstance(object obj)
throw new ArgumentException("failed to construct boolean from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerBoolean)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct boolean from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerEnumerated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public static DerEnumerated GetInstance(object obj)
throw new ArgumentException("failed to construct enumerated from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerEnumerated)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct enumerated from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerGeneralString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public static DerGeneralString GetInstance(object obj)
throw new ArgumentException("failed to construct general string from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerGeneralString)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct general string from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerGraphicString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public static DerGraphicString GetInstance(object obj)
throw new ArgumentException("failed to construct graphic string from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerGraphicString)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct graphic string from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerIA5String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public static DerIA5String GetInstance(object obj)
throw new ArgumentException("failed to construct IA5 string from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerIA5String)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct IA5 string from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
11 changes: 11 additions & 0 deletions crypto/src/asn1/DerInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public static DerInteger GetInstance(object obj)
throw new ArgumentException("failed to construct integer from byte[]: " + e.Message);
}
}
else if (obj is ArraySegment<byte> arraySegment)
{
try
{
return (DerInteger)Meta.Instance.FromByteArray(arraySegment);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct integer from ArraySegment<byte>: " + e.Message);
}
}

throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
}
Expand Down
Loading