summary refs log tree commit diff
path: root/crypto/src/bcpg/attr/ImageAttrib.cs
blob: 2d0fef8b832306b3088199a7b805641e457e0cbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)
            : this(false, data)
        {
        }

        public ImageAttrib(bool forceLongLength, byte[] data)
            : base(UserAttributeSubpacketTag.ImageAttribute, forceLongLength, 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 virtual int Version
        {
            get { return _version; }
        }

        public virtual int Encoding
        {
            get { return _encoding; }
        }

        public virtual byte[] GetImageData()
        {
            return imageData;
        }
    }
}