summary refs log tree commit diff
path: root/crypto/src/bcpg/attr/ImageAttrib.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/bcpg/attr/ImageAttrib.cs')
-rw-r--r--crypto/src/bcpg/attr/ImageAttrib.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/crypto/src/bcpg/attr/ImageAttrib.cs b/crypto/src/bcpg/attr/ImageAttrib.cs
new file mode 100644
index 000000000..73490791c
--- /dev/null
+++ b/crypto/src/bcpg/attr/ImageAttrib.cs
@@ -0,0 +1,68 @@
+using System;
+using System.IO;
+
+namespace Org.BouncyCastle.Bcpg.Attr
+{
+	/// <remarks>Basic type for a image attribute packet.</remarks>
+    public class ImageAttrib
+		: UserAttributeSubpacket
+    {
+		public enum Format : byte
+		{
+			Jpeg = 1
+		}
+
+		private static readonly byte[] Zeroes = new byte[12];
+
+		private int     hdrLength;
+        private int     _version;
+        private int     _encoding;
+        private byte[]  imageData;
+
+        public ImageAttrib(
+            byte[] data)
+            : base(UserAttributeSubpacketTag.ImageAttribute, data)
+        {
+            hdrLength = ((data[1] & 0xff) << 8) | (data[0] & 0xff);
+            _version = data[2] & 0xff;
+            _encoding = data[3] & 0xff;
+
+            imageData = new byte[data.Length - hdrLength];
+            Array.Copy(data, hdrLength, imageData, 0, imageData.Length);
+        }
+
+		public ImageAttrib(
+			Format	imageType,
+			byte[]	imageData)
+			: this(ToByteArray(imageType, imageData))
+		{
+		}
+
+		private static byte[] ToByteArray(
+			Format	imageType,
+			byte[]	imageData)
+		{
+			MemoryStream bOut = new MemoryStream();
+			bOut.WriteByte(0x10); bOut.WriteByte(0x00); bOut.WriteByte(0x01);
+			bOut.WriteByte((byte) imageType);
+			bOut.Write(Zeroes, 0, Zeroes.Length);
+			bOut.Write(imageData, 0, imageData.Length);
+			return bOut.ToArray();
+		}
+
+		public int Version
+        {
+			get { return _version; }
+        }
+
+        public int Encoding
+        {
+			get { return _encoding; }
+        }
+
+		public byte[] GetImageData()
+        {
+            return imageData;
+        }
+    }
+}