diff options
Diffstat (limited to 'crypto/src/asn1/DerGeneralString.cs')
-rw-r--r-- | crypto/src/asn1/DerGeneralString.cs | 96 |
1 files changed, 63 insertions, 33 deletions
diff --git a/crypto/src/asn1/DerGeneralString.cs b/crypto/src/asn1/DerGeneralString.cs index f2a47c370..e6637732a 100644 --- a/crypto/src/asn1/DerGeneralString.cs +++ b/crypto/src/asn1/DerGeneralString.cs @@ -1,5 +1,5 @@ using System; -using System.Text; +using System.IO; using Org.BouncyCastle.Utilities; @@ -8,78 +8,108 @@ namespace Org.BouncyCastle.Asn1 public class DerGeneralString : DerStringBase { - private readonly string str; + internal class Meta : Asn1UniversalType + { + internal static readonly Asn1UniversalType Instance = new Meta(); + + private Meta() : base(typeof(DerGeneralString), Asn1Tags.GeneralString) {} + + internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString) + { + return CreatePrimitive(octetString.GetOctets()); + } + } - public static DerGeneralString GetInstance( - object obj) + public static DerGeneralString GetInstance(object obj) { if (obj == null || obj is DerGeneralString) { return (DerGeneralString) obj; } + else if (obj is IAsn1Convertible) + { + Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); + if (asn1Object is DerGeneralString) + return (DerGeneralString)asn1Object; + } + else if (obj is byte[]) + { + try + { + return (DerGeneralString)Meta.Instance.FromByteArray((byte[])obj); + } + catch (IOException e) + { + throw new ArgumentException("failed to construct general string from byte[]: " + e.Message); + } + } - throw new ArgumentException("illegal object in GetInstance: " - + Platform.GetTypeName(obj)); + throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj)); } - public static DerGeneralString GetInstance( - Asn1TaggedObject obj, - bool isExplicit) + public static DerGeneralString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - Asn1Object o = obj.GetObject(); + return (DerGeneralString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); + } - if (isExplicit || o is DerGeneralString) - { - return GetInstance(o); - } + private readonly byte[] m_contents; - return new DerGeneralString(((Asn1OctetString)o).GetOctets()); + public DerGeneralString(string str) + { + if (str == null) + throw new ArgumentNullException("str"); + + m_contents = Strings.ToAsciiByteArray(str); } - public DerGeneralString( - byte[] str) - : this(Strings.FromAsciiByteArray(str)) + public DerGeneralString(byte[] contents) + : this(contents, true) { } - public DerGeneralString( - string str) + internal DerGeneralString(byte[] contents, bool clone) { - if (str == null) - throw new ArgumentNullException("str"); + if (null == contents) + throw new ArgumentNullException("contents"); - this.str = str; + m_contents = clone ? Arrays.Clone(contents) : contents; } public override string GetString() { - return str; + return Strings.FromAsciiByteArray(m_contents); } public byte[] GetOctets() { - return Strings.ToAsciiByteArray(str); + return Arrays.Clone(m_contents); } internal override IAsn1Encoding GetEncoding(int encoding) { - return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GeneralString, GetOctets()); + return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GeneralString, m_contents); } internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) { - return new PrimitiveEncoding(tagClass, tagNo, GetOctets()); + return new PrimitiveEncoding(tagClass, tagNo, m_contents); } - protected override bool Asn1Equals( - Asn1Object asn1Object) + protected override bool Asn1Equals(Asn1Object asn1Object) { - DerGeneralString other = asn1Object as DerGeneralString; + DerGeneralString that = asn1Object as DerGeneralString; + return null != that + && Arrays.AreEqual(this.m_contents, that.m_contents); + } - if (other == null) - return false; + protected override int Asn1GetHashCode() + { + return Arrays.GetHashCode(m_contents); + } - return this.str.Equals(other.str); + internal static DerGeneralString CreatePrimitive(byte[] contents) + { + return new DerGeneralString(contents, false); } } } |