summary refs log tree commit diff
path: root/crypto/src/asn1/ConstructedDerEncoding.cs
blob: 3df7841e834c05cc91441565be60933f15266ed4 (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
using System;
using System.Diagnostics;

namespace Org.BouncyCastle.Asn1
{
    internal class ConstructedDerEncoding
        : DerEncoding
    {
        private readonly DerEncoding[] m_contentsElements;
        private readonly int m_contentsLength;

        internal ConstructedDerEncoding(int tagClass, int tagNo, DerEncoding[] contentsElements)
            : base(tagClass, tagNo)
        {
            Debug.Assert(contentsElements != null);
            m_contentsElements = contentsElements;
            m_contentsLength = Asn1OutputStream.GetLengthOfContents(contentsElements);
        }

        protected internal override int CompareLengthAndContents(DerEncoding other)
        {
            if (!(other is ConstructedDerEncoding that))
                throw new InvalidOperationException();

            if (this.m_contentsLength != that.m_contentsLength)
                return this.m_contentsLength - that.m_contentsLength;

            int length = System.Math.Min(this.m_contentsElements.Length, that.m_contentsElements.Length);
            for (int i = 0; i < length; i++)
            {
                int c = this.m_contentsElements[i].CompareTo(that.m_contentsElements[i]);
                if (c != 0)
                    return c;
            }

            Debug.Assert(this.m_contentsElements.Length == that.m_contentsElements.Length);
            return this.m_contentsElements.Length - that.m_contentsElements.Length;
        }

        public override void Encode(Asn1OutputStream asn1Out)
        {
            asn1Out.WriteIdentifier(Asn1Tags.Constructed | m_tagClass, m_tagNo);
            asn1Out.WriteDL(m_contentsLength);
            asn1Out.EncodeContents(m_contentsElements);
        }

        public override int GetLength()
        {
            return Asn1OutputStream.GetLengthOfEncodingDL(m_tagNo, m_contentsLength);
        }
    }
}