summary refs log tree commit diff
path: root/crypto/src/asn1/DerGraphicString.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-06 16:34:04 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-06 16:34:04 +0700
commita6c8ed8e8916eade1739fe053b82078a49a97ad0 (patch)
treec8beec9143d7f08afa628773ce3bf8673f24009b /crypto/src/asn1/DerGraphicString.cs
parentASN.1 tagged objects (diff)
downloadBouncyCastle.NET-ed25519-a6c8ed8e8916eade1739fe053b82078a49a97ad0.tar.xz
DerGraphicString refactoring
- avoid extra copy of parsed contents
Diffstat (limited to '')
-rw-r--r--crypto/src/asn1/DerGraphicString.cs45
1 files changed, 25 insertions, 20 deletions
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);
         }
     }
 }