summary refs log tree commit diff
path: root/crypto/src/asn1/LazyDLSet.cs
blob: 87354d394cca721a1382bd4753a01b0d8f22afbd (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.IO;

namespace Org.BouncyCastle.Asn1
{
    internal class LazyDLSet
        : DLSet
    {
        private byte[] m_encoded;

        internal LazyDLSet(byte[] encoded)
            : base()
        {
            if (null == encoded)
                throw new ArgumentNullException(nameof(encoded));

            m_encoded = encoded;
        }

        public override Asn1Encodable this[int index]
        {
            get
            {
                Force();

                return base[index];
            }
        }

        public override IEnumerator<Asn1Encodable> GetEnumerator()
        {
            byte[] encoded = GetContents();
            if (null != encoded)
                return new LazyDLEnumerator(encoded);

            return base.GetEnumerator();
        }

        public override int Count
        {
            get
            {
                Force();

                return base.Count;
            }
        }

        public override Asn1Encodable[] ToArray()
        {
            Force();

            return base.ToArray();
        }

        public override string ToString()
        {
            Force();

            return base.ToString();
        }

        internal override IAsn1Encoding GetEncoding(int encoding)
        {
            if (Asn1OutputStream.EncodingBer == encoding)
            {
                byte[] encoded = GetContents();
                if (null != encoded)
                    return new ConstructedLazyDLEncoding(Asn1Tags.Universal, Asn1Tags.Set, encoded);
            }
            else
            {
                Force();
            }

            return base.GetEncoding(encoding);
        }

        internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
        {
            if (Asn1OutputStream.EncodingBer == encoding)
            {
                byte[] encoded = GetContents();
                if (null != encoded)
                    return new ConstructedLazyDLEncoding(tagClass, tagNo, encoded);
            }
            else
            {
                Force();
            }

            return base.GetEncodingImplicit(encoding, tagClass, tagNo);
        }

        private void Force()
        {
            lock (this)
            {
                if (null != m_encoded)
                {
                    try
                    {
                        Asn1EncodableVector v;
                        using (var input = new LazyAsn1InputStream(m_encoded))
                        {
                            v = input.ReadVector();
                        }

                        m_elements = v.TakeElements();
                        m_sortedElements = m_elements.Length <= 1 ? m_elements : null;
                        m_encoded = null;
                    }
                    catch (IOException e)
                    {
                        throw new Asn1ParsingException("malformed ASN.1: " + e.Message, e);
                    }
                }
            }
        }

        private byte[] GetContents()
        {
            lock (this) return m_encoded;
        }
    }
}