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

namespace Org.BouncyCastle.Asn1
{
    internal class TaggedDerEncoding
        : DerEncoding
    {
        private readonly DerEncoding m_contentsElement;
        private readonly int m_contentsLength;

        internal TaggedDerEncoding(int tagClass, int tagNo, DerEncoding contentsElement)
            : base(tagClass, tagNo)
        {
            Debug.Assert(contentsElement != null);
            m_contentsElement = contentsElement;
            m_contentsLength = contentsElement.GetLength();
        }

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

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

            return this.m_contentsElement.CompareTo(that.m_contentsElement);
        }

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

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