diff --git a/crypto/src/asn1/DerBMPString.cs b/crypto/src/asn1/DerBMPString.cs
index 284a4b830..bf21fa424 100644
--- a/crypto/src/asn1/DerBMPString.cs
+++ b/crypto/src/asn1/DerBMPString.cs
@@ -1,4 +1,7 @@
using System;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+using System.Buffers;
+#endif
using System.IO;
using Org.BouncyCastle.Utilities;
@@ -82,6 +85,16 @@ namespace Org.BouncyCastle.Asn1
throw new ArgumentException("malformed BMPString encoding encountered", "contents");
int charLen = byteLen / 2;
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+ m_str = string.Create(charLen, contents, (chars, bytes) =>
+ {
+ for (int i = 0; i < chars.Length; ++i)
+ {
+ chars[i] = (char)((bytes[2 * i] << 8) | (bytes[2 * i + 1] & 0xff));
+ }
+ });
+#else
char[] cs = new char[charLen];
for (int i = 0; i != charLen; i++)
@@ -90,8 +103,10 @@ namespace Org.BouncyCastle.Asn1
}
m_str = new string(cs);
+#endif
}
+#if !(NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
internal DerBmpString(char[] str)
{
if (str == null)
@@ -99,6 +114,7 @@ namespace Org.BouncyCastle.Asn1
m_str = new string(str);
}
+#endif
/**
* basic constructor
@@ -157,10 +173,17 @@ namespace Org.BouncyCastle.Asn1
return new DerBmpString(contents);
}
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+ internal static DerBmpString CreatePrimitive<TState>(int length, TState state, SpanAction<char, TState> action)
+ {
+ return new DerBmpString(string.Create(length, state, action));
+ }
+#else
internal static DerBmpString CreatePrimitive(char[] str)
{
// TODO[asn1] Asn1InputStream has a validator/converter that should be unified in this class somehow
return new DerBmpString(str);
}
+#endif
}
}
|