Welcome to mirror list, hosted at ThFree Co, Russian Federation.

securitydocument.cs « security « system « mscorlib « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fffcf0cbfdbc27e383c4c6a009f3e7761f9a45cf (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// ==++==
// 
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// ==--==
//---------------------------------------------------------------------------
//
// CLASS:    SecurityDocument.cs
// 
// <OWNER>[....]</OWNER>
//
// PURPOSE:  Represent an XML document
// 
// 
//---------------------------------------------------------------------------

namespace System.Security
{
    using System;
    using System.Collections;
    using System.Security.Util;
    using System.Text;
    using System.Globalization;
    using System.IO;
    using System.Diagnostics.Contracts;
    using StringMaker = System.Security.Util.Tokenizer.StringMaker;
#if !MONO
    [Serializable]
    sealed internal class SecurityDocumentElement : ISecurityElementFactory
    {
        private int m_position;
        private SecurityDocument m_document;

        internal SecurityDocumentElement( SecurityDocument document, int position )
        {
            m_document = document;
            m_position = position;
        }

        SecurityElement ISecurityElementFactory.CreateSecurityElement()
        {
            return m_document.GetElement( m_position, true );
        }

        Object ISecurityElementFactory.Copy()
        {
            return new SecurityDocumentElement( m_document, m_position );
        }

        String ISecurityElementFactory.GetTag()
        {
            return m_document.GetTagForElement( m_position );
        }

        String ISecurityElementFactory.Attribute( String attributeName )
        {
            return m_document.GetAttributeForElement( m_position, attributeName );
        }

    }
#endif

    [Serializable]
    sealed internal class SecurityDocument
    {
        internal byte[] m_data;

        internal const byte c_element = 1;
        internal const byte c_attribute = 2;
        internal const byte c_text = 3;
        internal const byte c_children = 4;
        internal const int c_growthSize = 32;

        public SecurityDocument( int numData )
        {
            m_data = new byte[numData];
        }

        public SecurityDocument( byte[] data )
        {
            this.m_data = data;
        }

        public SecurityDocument( SecurityElement elRoot )
        {
            m_data = new byte[c_growthSize];

            int position = 0;
            ConvertElement( elRoot, ref position );
        }

        public void GuaranteeSize( int size )
        {
            if (m_data.Length < size)
            {
                byte[] m_newData = new byte[((size / c_growthSize) + 1) * c_growthSize];
                Array.Copy( m_data, 0, m_newData, 0, m_data.Length );
                m_data = m_newData;
            }
        }

        public void AddString( String str, ref int position )
        {
            // @

            GuaranteeSize( position + str.Length * 2 + 2 );

            for (int i = 0; i < str.Length; ++i)
            {
                m_data[position+(2*i)] = (byte)(str[i] >> 8);
                m_data[position+(2*i)+1] = (byte)(str[i] & 0x00FF);
            }
            m_data[position + str.Length * 2] = 0;
            m_data[position + str.Length * 2 + 1] = 0;

            position += str.Length * 2 + 2;
        }

        public void AppendString( String str, ref int position )
        {
            if (position <= 1 ||
                m_data[position - 1] != 0 ||
                m_data[position - 2] != 0 )
                throw new XmlSyntaxException();

            position -= 2;

            AddString( str, ref position );
        }

        public static int EncodedStringSize( String str )
        {
            return str.Length * 2 + 2;
        }

        public String GetString( ref int position )
        {
            return GetString( ref position, true );
        }

        public String GetString( ref int position, bool bCreate )
        {
            int stringEnd;
            bool bFoundEnd = false;
            for (stringEnd = position; stringEnd < m_data.Length-1; stringEnd += 2)
            {
                if (m_data[stringEnd] == 0 && m_data[stringEnd + 1] == 0)
                {
                    bFoundEnd = true;
                    break;
                }
            }

            Contract.Assert(bFoundEnd, "Malformed string in parse data");

            StringMaker m = System.SharedStatics.GetSharedStringMaker();

            try 
            {

                if (bCreate)
                {
                    m._outStringBuilder = null;
                    m._outIndex = 0;

                    for (int i = position; i < stringEnd; i += 2)
                    {
                        char c = (char)(m_data[i] << 8 | m_data[i+1]);

                        // add character  to the string
                        if (m._outIndex < StringMaker.outMaxSize) 
                        {
                            // easy case
                            m._outChars[m._outIndex++] = c;
                        } 
                        else
                        {
                            if (m._outStringBuilder == null) 
                            {
                                // OK, first check if we have to init the StringBuilder
                                m._outStringBuilder = new StringBuilder();
                            }
                    
                            // OK, copy from _outChars to _outStringBuilder
                            m._outStringBuilder.Append(m._outChars, 0, StringMaker.outMaxSize);
                     
                            // reset _outChars pointer
                            m._outChars[0] = c;
                            m._outIndex = 1;
                        }
                    }
                }

                position = stringEnd + 2;

                if (bCreate)
                    return m.MakeString();
                else
                    return null;
            }
            finally
            {
                System.SharedStatics.ReleaseSharedStringMaker(ref m);
            }
        }
                    

        public void AddToken( byte b, ref int position )
        {
            GuaranteeSize( position + 1 );
            m_data[position++] = b;
        }

        public void ConvertElement( SecurityElement elCurrent, ref int position )
        {
            AddToken( c_element, ref position );
            AddString( elCurrent.m_strTag, ref position );

            if (elCurrent.m_lAttributes != null)
            {
                for (int i = 0; i < elCurrent.m_lAttributes.Count; i+=2)
                {
                    AddToken( c_attribute, ref position );
                    AddString( (String)elCurrent.m_lAttributes[i], ref position );
                    AddString( (String)elCurrent.m_lAttributes[i+1], ref position );
                }
            }

            if (elCurrent.m_strText != null)
            {
                AddToken( c_text, ref position );
                AddString( elCurrent.m_strText, ref position );
            }

            if (elCurrent.InternalChildren != null)
            {
                for (int i = 0; i < elCurrent.InternalChildren.Count; ++i)
                {
                    ConvertElement( (SecurityElement)elCurrent.Children[i], ref position );
                }
            }
            AddToken( c_children, ref position );
        }

        public SecurityElement GetRootElement()
        {
            return GetElement( 0, true );
        }

        public SecurityElement GetElement( int position, bool bCreate )
        {
            SecurityElement elRoot = InternalGetElement( ref position, bCreate );
            return elRoot;
        }

        internal SecurityElement InternalGetElement( ref int position, bool bCreate )
        {
            if (m_data.Length <= position)
                throw new XmlSyntaxException();

            if (m_data[position++] != c_element)
                throw new XmlSyntaxException();

            SecurityElement elCurrent = null;
            String strTag = GetString( ref position, bCreate );
            if (bCreate)
                elCurrent = new SecurityElement( strTag );

            while (m_data[position] == c_attribute)
            {
                position++;
                String strName = GetString( ref position, bCreate );
                String strValue = GetString( ref position, bCreate );
                if (bCreate)
                    elCurrent.AddAttribute( strName, strValue );
            }

            if (m_data[position] == c_text)
            {
                position++;
                String strText = GetString( ref position, bCreate );
                if (bCreate)
                    elCurrent.m_strText = strText;
            }

            while (m_data[position] != c_children)
            {
                SecurityElement elChild = InternalGetElement( ref position, bCreate );
                if (bCreate)
                    elCurrent.AddChild( elChild );
            }
            position++;

            return elCurrent;
        }

        public String GetTagForElement( int position )
        {
            if (m_data.Length <= position)
                throw new XmlSyntaxException();

            if (m_data[position++] != c_element)
                throw new XmlSyntaxException();

            String strTag = GetString( ref position );
            return strTag;
        }

        public ArrayList GetChildrenPositionForElement( int position )
        {
            if (m_data.Length <= position)
                throw new XmlSyntaxException();

            if (m_data[position++] != c_element)
                throw new XmlSyntaxException();

            ArrayList children = new ArrayList();

            // This is to move past the tag string
            GetString( ref position );

            while (m_data[position] == c_attribute)
            {
                position++;
                // Read name and value, then throw them away
                GetString( ref position, false );
                GetString( ref position, false );
            }

            if (m_data[position] == c_text)
            {
                position++;
                // Read text, then throw it away.
                GetString( ref position, false );
            }

            while (m_data[position] != c_children)
            {
                children.Add( position );
                InternalGetElement( ref position, false );
            }
            position++;

            return children;
        }

        public String GetAttributeForElement( int position, String attributeName )
        {
            if (m_data.Length <= position)
                throw new XmlSyntaxException();

            if (m_data[position++] != c_element)
                throw new XmlSyntaxException();

            String strRetValue = null;
            // This is to move past the tag string.
            GetString( ref position, false );
            

            while (m_data[position] == c_attribute)
            {
                position++;
                String strName = GetString( ref position );
                String strValue = GetString( ref position );

                if (String.Equals( strName, attributeName ))
                {
                    strRetValue = strValue;
                    break;
                }
            }


            return strRetValue;
        }
    }
}