summary refs log tree commit diff
path: root/crypto/src/openpgp/PgpObjectFactory.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/openpgp/PgpObjectFactory.cs')
-rw-r--r--crypto/src/openpgp/PgpObjectFactory.cs151
1 files changed, 69 insertions, 82 deletions
diff --git a/crypto/src/openpgp/PgpObjectFactory.cs b/crypto/src/openpgp/PgpObjectFactory.cs
index e2c4ac527..f7bf89507 100644
--- a/crypto/src/openpgp/PgpObjectFactory.cs
+++ b/crypto/src/openpgp/PgpObjectFactory.cs
@@ -1,9 +1,7 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Bcpg.OpenPgp
 {
 	/// <remarks>
@@ -18,14 +16,12 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
     {
         private readonly BcpgInputStream bcpgIn;
 
-		public PgpObjectFactory(
-            Stream inputStream)
+		public PgpObjectFactory(Stream inputStream)
         {
             this.bcpgIn = BcpgInputStream.Wrap(inputStream);
         }
 
-        public PgpObjectFactory(
-            byte[] bytes)
+        public PgpObjectFactory(byte[] bytes)
             : this(new MemoryStream(bytes, false))
         {
         }
@@ -36,90 +32,81 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
         {
             PacketTag tag = bcpgIn.NextPacketTag();
 
-            if ((int) tag == -1) return null;
+            if ((int) tag == -1)
+                return null;
 
             switch (tag)
             {
-                case PacketTag.Signature:
-                {
-                    IList l = Platform.CreateArrayList();
-
-                    while (bcpgIn.NextPacketTag() == PacketTag.Signature)
-                    {
-                        try
-                        {
-                            l.Add(new PgpSignature(bcpgIn));
-                        }
-                        catch (UnsupportedPacketVersionException)
-                        {
-                            // Signatures of unsupported version MUST BE ignored
-                            // see: https://tests.sequoia-pgp.org/#Detached_signatures_with_unknown_packets
-                            continue;
-                        }
-                        catch (PgpException e)
-                        {
-                            throw new IOException("can't create signature object: " + e);
-                        }
-                    }
+            case PacketTag.Signature:
+            {
+                var l = new List<PgpSignature>();
 
-                    PgpSignature[] sigs = new PgpSignature[l.Count];
-                    for (int i = 0; i < l.Count; ++i)
+                while (bcpgIn.NextPacketTag() == PacketTag.Signature)
+                {
+                    try
                     {
-                        sigs[i] = (PgpSignature)l[i];
+                        l.Add(new PgpSignature(bcpgIn));
                     }
-                    return new PgpSignatureList(sigs);
-                }
-                case PacketTag.SecretKey:
-                    try
+                    catch (UnsupportedPacketVersionException)
                     {
-                        return new PgpSecretKeyRing(bcpgIn);
+                        // Signatures of unsupported version MUST BE ignored
+                        // see: https://tests.sequoia-pgp.org/#Detached_signatures_with_unknown_packets
+                        continue;
                     }
                     catch (PgpException e)
                     {
-                        throw new IOException("can't create secret key object: " + e);
+                        throw new IOException("can't create signature object: " + e);
                     }
-                case PacketTag.PublicKey:
-                    return new PgpPublicKeyRing(bcpgIn);
-				// TODO Make PgpPublicKey a PgpObject or return a PgpPublicKeyRing
-//				case PacketTag.PublicSubkey:
-//					return PgpPublicKeyRing.ReadSubkey(bcpgIn);
-                case PacketTag.CompressedData:
-                    return new PgpCompressedData(bcpgIn);
-                case PacketTag.LiteralData:
-                    return new PgpLiteralData(bcpgIn);
-                case PacketTag.PublicKeyEncryptedSession:
-                case PacketTag.SymmetricKeyEncryptedSessionKey:
-                    return new PgpEncryptedDataList(bcpgIn);
-                case PacketTag.OnePassSignature:
+                }
+
+                return new PgpSignatureList(l.ToArray());
+            }
+            case PacketTag.SecretKey:
+                try
+                {
+                    return new PgpSecretKeyRing(bcpgIn);
+                }
+                catch (PgpException e)
                 {
-                    IList l = Platform.CreateArrayList();
+                    throw new IOException("can't create secret key object: " + e);
+                }
+            case PacketTag.PublicKey:
+                return new PgpPublicKeyRing(bcpgIn);
+			// TODO Make PgpPublicKey a PgpObject or return a PgpPublicKeyRing
+			//case PacketTag.PublicSubkey:
+			//	return PgpPublicKeyRing.ReadSubkey(bcpgIn);
+            case PacketTag.CompressedData:
+                return new PgpCompressedData(bcpgIn);
+            case PacketTag.LiteralData:
+                return new PgpLiteralData(bcpgIn);
+            case PacketTag.PublicKeyEncryptedSession:
+            case PacketTag.SymmetricKeyEncryptedSessionKey:
+                return new PgpEncryptedDataList(bcpgIn);
+            case PacketTag.OnePassSignature:
+            {
+                var l = new List<PgpOnePassSignature>();
 
-                    while (bcpgIn.NextPacketTag() == PacketTag.OnePassSignature)
+                while (bcpgIn.NextPacketTag() == PacketTag.OnePassSignature)
+                {
+                    try
                     {
-                        try
-                        {
-                            l.Add(new PgpOnePassSignature(bcpgIn));
-                        }
-                        catch (PgpException e)
-                        {
-							throw new IOException("can't create one pass signature object: " + e);
-						}
+                        l.Add(new PgpOnePassSignature(bcpgIn));
                     }
-
-                    PgpOnePassSignature[] sigs = new PgpOnePassSignature[l.Count];
-                    for (int i = 0; i < l.Count; ++i)
+                    catch (PgpException e)
                     {
-                        sigs[i] = (PgpOnePassSignature)l[i];
-                    }
-					return new PgpOnePassSignatureList(sigs);
+						throw new IOException("can't create one pass signature object: " + e);
+					}
                 }
-                case PacketTag.Marker:
-                    return new PgpMarker(bcpgIn);
-                case PacketTag.Experimental1:
-                case PacketTag.Experimental2:
-                case PacketTag.Experimental3:
-                case PacketTag.Experimental4:
-					return new PgpExperimental(bcpgIn);
+
+				return new PgpOnePassSignatureList(l.ToArray());
+            }
+            case PacketTag.Marker:
+                return new PgpMarker(bcpgIn);
+            case PacketTag.Experimental1:
+            case PacketTag.Experimental2:
+            case PacketTag.Experimental3:
+            case PacketTag.Experimental4:
+				return new PgpExperimental(bcpgIn);
             }
 
             throw new IOException("unknown object in stream " + bcpgIn.NextPacketTag());
@@ -129,9 +116,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// Return all available objects in a list.
 		/// </summary>
 		/// <returns>An <c>IList</c> containing all objects from this factory, in order.</returns>
-		public IList AllPgpObjects()
+		public IList<PgpObject> AllPgpObjects()
 		{
-            IList result = Platform.CreateArrayList();
+            var result = new List<PgpObject>();
 			PgpObject pgpObject;
 			while ((pgpObject = NextPgpObject()) != null)
 			{
@@ -143,17 +130,17 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
         /// <summary>
         /// Read all available objects, returning only those that are assignable to the specified type.
         /// </summary>
-        /// <param name="type">The type of objects to return. All other objects are ignored.</param>
-        /// <returns>An <c>IList</c> containing the filtered objects from this factory, in order.</returns>
-        public IList FilterPgpObjects(Type type)
+        /// <returns>An <see cref="IList{T}"/> containing the filtered objects from this factory, in order.</returns>
+        public IList<T> FilterPgpObjects<T>()
+            where T : PgpObject
         {
-            IList result = Platform.CreateArrayList();
+            var result = new List<T>();
             PgpObject pgpObject;
             while ((pgpObject = NextPgpObject()) != null)
             {
-                if (type.IsInstanceOfType(pgpObject))
+                if (pgpObject is T t)
                 {
-                    result.Add(pgpObject);
+                    result.Add(t);
                 }
             }
             return result;