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

namespace Org.BouncyCastle.Asn1
{
    internal class PrimitiveDerEncoding
        : DerEncoding
    {
        internal readonly byte[] m_contentsOctets;

        internal PrimitiveDerEncoding(int tagClass, int tagNo, byte[] contentsOctets)
            : base (tagClass, tagNo)
        {
            Debug.Assert(contentsOctets != null);
            m_contentsOctets = contentsOctets;
        }

        protected internal override int CompareLengthAndContents(DerEncoding other)
        {
            if (other is PrimitiveDerEncodingSuffixed suffixed)
                return -suffixed.CompareLengthAndContents(this);

            if (!(other is PrimitiveDerEncoding that))
                throw new InvalidOperationException();

            int length = this.m_contentsOctets.Length;
            if (length != that.m_contentsOctets.Length)
                return length - that.m_contentsOctets.Length;

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            return this.m_contentsOctets.AsSpan(0, length).SequenceCompareTo(
                   that.m_contentsOctets.AsSpan(0, length));
#else
            for (int i = 0; i < length; i++)
            {
                byte ai = this.m_contentsOctets[i], bi = that.m_contentsOctets[i];
                if (ai != bi)
                    return ai - bi;
            }
            return 0;
#endif
        }

        public override void Encode(Asn1OutputStream asn1Out)
        {
            asn1Out.WriteIdentifier(m_tagClass, m_tagNo);
            asn1Out.WriteDL(m_contentsOctets.Length);
            asn1Out.Write(m_contentsOctets, 0, m_contentsOctets.Length);
        }

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