diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-06 16:34:04 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-06 16:34:04 +0700 |
commit | a6c8ed8e8916eade1739fe053b82078a49a97ad0 (patch) | |
tree | c8beec9143d7f08afa628773ce3bf8673f24009b /crypto/src/asn1 | |
parent | ASN.1 tagged objects (diff) | |
download | BouncyCastle.NET-ed25519-a6c8ed8e8916eade1739fe053b82078a49a97ad0.tar.xz |
DerGraphicString refactoring
- avoid extra copy of parsed contents
Diffstat (limited to 'crypto/src/asn1')
-rw-r--r-- | crypto/src/asn1/Asn1InputStream.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/DerGraphicString.cs | 45 |
2 files changed, 26 insertions, 21 deletions
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs index 20734fd59..f00bc6ef6 100644 --- a/crypto/src/asn1/Asn1InputStream.cs +++ b/crypto/src/asn1/Asn1InputStream.cs @@ -434,7 +434,7 @@ namespace Org.BouncyCastle.Asn1 case Asn1Tags.GeneralString: return new DerGeneralString(bytes); case Asn1Tags.GraphicString: - return new DerGraphicString(bytes); + return DerGraphicString.CreatePrimitive(bytes); case Asn1Tags.IA5String: return new DerIA5String(bytes); case Asn1Tags.Integer: diff --git a/crypto/src/asn1/DerGraphicString.cs b/crypto/src/asn1/DerGraphicString.cs index 598b8bc36..7d7a15f59 100644 --- a/crypto/src/asn1/DerGraphicString.cs +++ b/crypto/src/asn1/DerGraphicString.cs @@ -7,8 +7,6 @@ namespace Org.BouncyCastle.Asn1 public class DerGraphicString : DerStringBase { - private readonly byte[] mString; - /** * return a Graphic String from the passed in object * @@ -60,49 +58,56 @@ namespace Org.BouncyCastle.Asn1 return new DerGraphicString(((Asn1OctetString)o).GetOctets()); } - /** - * basic constructor - with bytes. - * @param string the byte encoding of the characters making up the string. - */ - public DerGraphicString(byte[] encoding) + private readonly byte[] m_contents; + + public DerGraphicString(byte[] contents) + : this(contents, true) { - this.mString = Arrays.Clone(encoding); + } + + internal DerGraphicString(byte[] contents, bool clone) + { + if (null == contents) + throw new ArgumentNullException("contents"); + + this.m_contents = clone ? Arrays.Clone(contents) : contents; } public override string GetString() { - return Strings.FromByteArray(mString); + return Strings.FromByteArray(m_contents); } public byte[] GetOctets() { - return Arrays.Clone(mString); + return Arrays.Clone(m_contents); } internal override int EncodedLength(bool withID) { - return Asn1OutputStream.GetLengthOfEncodingDL(withID, mString.Length); + return Asn1OutputStream.GetLengthOfEncodingDL(withID, m_contents.Length); } internal override void Encode(Asn1OutputStream asn1Out, bool withID) { - asn1Out.WriteEncodingDL(withID, Asn1Tags.GraphicString, mString); + asn1Out.WriteEncodingDL(withID, Asn1Tags.GraphicString, m_contents); } protected override int Asn1GetHashCode() { - return Arrays.GetHashCode(mString); + return Arrays.GetHashCode(m_contents); } - protected override bool Asn1Equals( - Asn1Object asn1Object) + protected override bool Asn1Equals(Asn1Object asn1Object) { - DerGraphicString other = asn1Object as DerGraphicString; - - if (other == null) - return false; + DerGraphicString that = asn1Object as DerGraphicString; + return null != that + && Arrays.AreEqual(this.m_contents, that.m_contents); + } - return Arrays.AreEqual(mString, other.mString); + internal static DerGraphicString CreatePrimitive(byte[] contents) + { + return new DerGraphicString(contents, false); } } } |