mscorlib(4.0.0.0) API with additions
SecurityElement.cs
1 using System.Collections;
3 using System.IO;
6 using System.Security.Util;
7 using System.Text;
8 
9 namespace System.Security
10 {
12  [Serializable]
13  [ComVisible(true)]
14  public sealed class SecurityElement : ISecurityElementFactory
15  {
16  private delegate void ToStringHelperFunc(object obj, string str);
17 
18  internal string m_strTag;
19 
20  internal string m_strText;
21 
22  private ArrayList m_lChildren;
23 
24  internal ArrayList m_lAttributes;
25 
26  internal SecurityElementType m_type;
27 
28  private static readonly char[] s_tagIllegalCharacters = new char[3]
29  {
30  ' ',
31  '<',
32  '>'
33  };
34 
35  private static readonly char[] s_textIllegalCharacters = new char[2]
36  {
37  '<',
38  '>'
39  };
40 
41  private static readonly char[] s_valueIllegalCharacters = new char[3]
42  {
43  '<',
44  '>',
45  '"'
46  };
47 
48  private const string s_strIndent = " ";
49 
50  private const int c_AttributesTypical = 8;
51 
52  private const int c_ChildrenTypical = 1;
53 
54  private static readonly string[] s_escapeStringPairs = new string[10]
55  {
56  "<",
57  "&lt;",
58  ">",
59  "&gt;",
60  "\"",
61  "&quot;",
62  "'",
63  "&apos;",
64  "&",
65  "&amp;"
66  };
67 
68  private static readonly char[] s_escapeChars = new char[5]
69  {
70  '<',
71  '>',
72  '"',
73  '\'',
74  '&'
75  };
76 
81  public string Tag
82  {
83  get
84  {
85  return m_strTag;
86  }
87  set
88  {
89  if (value == null)
90  {
91  throw new ArgumentNullException("Tag");
92  }
93  if (!IsValidTag(value))
94  {
95  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementTag"), value));
96  }
97  m_strTag = value;
98  }
99  }
100 
105  public Hashtable Attributes
106  {
107  get
108  {
109  if (m_lAttributes == null || m_lAttributes.Count == 0)
110  {
111  return null;
112  }
113  Hashtable hashtable = new Hashtable(m_lAttributes.Count / 2);
114  int count = m_lAttributes.Count;
115  for (int i = 0; i < count; i += 2)
116  {
117  hashtable.Add(m_lAttributes[i], m_lAttributes[i + 1]);
118  }
119  return hashtable;
120  }
121  set
122  {
123  if (value == null || value.Count == 0)
124  {
125  m_lAttributes = null;
126  return;
127  }
128  ArrayList arrayList = new ArrayList(value.Count);
129  IDictionaryEnumerator enumerator = value.GetEnumerator();
130  while (enumerator.MoveNext())
131  {
132  string text = (string)enumerator.Key;
133  string value2 = (string)enumerator.Value;
134  if (!IsValidAttributeName(text))
135  {
136  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementName"), (string)enumerator.Current));
137  }
138  if (!IsValidAttributeValue(value2))
139  {
140  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementValue"), (string)enumerator.Value));
141  }
142  arrayList.Add(text);
143  arrayList.Add(value2);
144  }
145  m_lAttributes = arrayList;
146  }
147  }
148 
152  public string Text
153  {
154  get
155  {
156  return Unescape(m_strText);
157  }
158  set
159  {
160  if (value == null)
161  {
162  m_strText = null;
163  return;
164  }
165  if (!IsValidText(value))
166  {
167  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementTag"), value));
168  }
169  m_strText = value;
170  }
171  }
172 
176  public ArrayList Children
177  {
178  get
179  {
180  ConvertSecurityElementFactories();
181  return m_lChildren;
182  }
183  set
184  {
185  if (value != null)
186  {
187  IEnumerator enumerator = value.GetEnumerator();
188  while (enumerator.MoveNext())
189  {
190  if (enumerator.Current == null)
191  {
192  throw new ArgumentException(Environment.GetResourceString("ArgumentNull_Child"));
193  }
194  }
195  }
196  m_lChildren = value;
197  }
198  }
199 
200  internal ArrayList InternalChildren => m_lChildren;
201 
202  internal SecurityElement()
203  {
204  }
205 
206  SecurityElement ISecurityElementFactory.CreateSecurityElement()
207  {
208  return this;
209  }
210 
211  string ISecurityElementFactory.GetTag()
212  {
213  return Tag;
214  }
215 
216  object ISecurityElementFactory.Copy()
217  {
218  return Copy();
219  }
220 
221  string ISecurityElementFactory.Attribute(string attributeName)
222  {
223  return Attribute(attributeName);
224  }
225 
233  public static SecurityElement FromString(string xml)
234  {
235  if (xml == null)
236  {
237  throw new ArgumentNullException("xml");
238  }
239  return new Parser(xml).GetTopElement();
240  }
241 
246  public SecurityElement(string tag)
247  {
248  if (tag == null)
249  {
250  throw new ArgumentNullException("tag");
251  }
252  if (!IsValidTag(tag))
253  {
254  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementTag"), tag));
255  }
256  m_strTag = tag;
257  m_strText = null;
258  }
259 
265  public SecurityElement(string tag, string text)
266  {
267  if (tag == null)
268  {
269  throw new ArgumentNullException("tag");
270  }
271  if (!IsValidTag(tag))
272  {
273  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementTag"), tag));
274  }
275  if (text != null && !IsValidText(text))
276  {
277  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementText"), text));
278  }
279  m_strTag = tag;
280  m_strText = text;
281  }
282 
283  internal void ConvertSecurityElementFactories()
284  {
285  if (m_lChildren == null)
286  {
287  return;
288  }
289  for (int i = 0; i < m_lChildren.Count; i++)
290  {
291  ISecurityElementFactory securityElementFactory = m_lChildren[i] as ISecurityElementFactory;
292  if (securityElementFactory != null && !(m_lChildren[i] is SecurityElement))
293  {
294  m_lChildren[i] = securityElementFactory.CreateSecurityElement();
295  }
296  }
297  }
298 
299  internal void AddAttributeSafe(string name, string value)
300  {
301  if (m_lAttributes == null)
302  {
303  m_lAttributes = new ArrayList(8);
304  }
305  else
306  {
307  int count = m_lAttributes.Count;
308  for (int i = 0; i < count; i += 2)
309  {
310  string a = (string)m_lAttributes[i];
311  if (string.Equals(a, name))
312  {
313  throw new ArgumentException(Environment.GetResourceString("Argument_AttributeNamesMustBeUnique"));
314  }
315  }
316  }
317  m_lAttributes.Add(name);
318  m_lAttributes.Add(value);
319  }
320 
326  public void AddAttribute(string name, string value)
327  {
328  if (name == null)
329  {
330  throw new ArgumentNullException("name");
331  }
332  if (value == null)
333  {
334  throw new ArgumentNullException("value");
335  }
336  if (!IsValidAttributeName(name))
337  {
338  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementName"), name));
339  }
340  if (!IsValidAttributeValue(value))
341  {
342  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidElementValue"), value));
343  }
344  AddAttributeSafe(name, value);
345  }
346 
350  public void AddChild(SecurityElement child)
351  {
352  if (child == null)
353  {
354  throw new ArgumentNullException("child");
355  }
356  if (m_lChildren == null)
357  {
358  m_lChildren = new ArrayList(1);
359  }
360  m_lChildren.Add(child);
361  }
362 
363  internal void AddChild(ISecurityElementFactory child)
364  {
365  if (child == null)
366  {
367  throw new ArgumentNullException("child");
368  }
369  if (m_lChildren == null)
370  {
371  m_lChildren = new ArrayList(1);
372  }
373  m_lChildren.Add(child);
374  }
375 
376  internal void AddChildNoDuplicates(ISecurityElementFactory child)
377  {
378  if (child == null)
379  {
380  throw new ArgumentNullException("child");
381  }
382  if (m_lChildren == null)
383  {
384  m_lChildren = new ArrayList(1);
385  m_lChildren.Add(child);
386  return;
387  }
388  for (int i = 0; i < m_lChildren.Count; i++)
389  {
390  if (m_lChildren[i] == child)
391  {
392  return;
393  }
394  }
395  m_lChildren.Add(child);
396  }
397 
402  public bool Equal(SecurityElement other)
403  {
404  if (other == null)
405  {
406  return false;
407  }
408  if (!string.Equals(m_strTag, other.m_strTag))
409  {
410  return false;
411  }
412  if (!string.Equals(m_strText, other.m_strText))
413  {
414  return false;
415  }
416  if (m_lAttributes == null || other.m_lAttributes == null)
417  {
418  if (m_lAttributes != other.m_lAttributes)
419  {
420  return false;
421  }
422  }
423  else
424  {
425  int count = m_lAttributes.Count;
426  if (count != other.m_lAttributes.Count)
427  {
428  return false;
429  }
430  for (int i = 0; i < count; i++)
431  {
432  string a = (string)m_lAttributes[i];
433  string b = (string)other.m_lAttributes[i];
434  if (!string.Equals(a, b))
435  {
436  return false;
437  }
438  }
439  }
440  if (m_lChildren == null || other.m_lChildren == null)
441  {
442  if (m_lChildren != other.m_lChildren)
443  {
444  return false;
445  }
446  }
447  else
448  {
449  if (m_lChildren.Count != other.m_lChildren.Count)
450  {
451  return false;
452  }
453  ConvertSecurityElementFactories();
454  other.ConvertSecurityElementFactories();
455  IEnumerator enumerator = m_lChildren.GetEnumerator();
456  IEnumerator enumerator2 = other.m_lChildren.GetEnumerator();
457  while (enumerator.MoveNext())
458  {
459  enumerator2.MoveNext();
460  SecurityElement securityElement = (SecurityElement)enumerator.Current;
461  SecurityElement other2 = (SecurityElement)enumerator2.Current;
462  if (securityElement == null || !securityElement.Equal(other2))
463  {
464  return false;
465  }
466  }
467  }
468  return true;
469  }
470 
473  [ComVisible(false)]
475  {
476  SecurityElement securityElement = new SecurityElement(m_strTag, m_strText);
477  securityElement.m_lChildren = ((m_lChildren == null) ? null : new ArrayList(m_lChildren));
478  securityElement.m_lAttributes = ((m_lAttributes == null) ? null : new ArrayList(m_lAttributes));
479  return securityElement;
480  }
481 
486  public static bool IsValidTag(string tag)
487  {
488  if (tag == null)
489  {
490  return false;
491  }
492  return tag.IndexOfAny(s_tagIllegalCharacters) == -1;
493  }
494 
499  public static bool IsValidText(string text)
500  {
501  if (text == null)
502  {
503  return false;
504  }
505  return text.IndexOfAny(s_textIllegalCharacters) == -1;
506  }
507 
512  public static bool IsValidAttributeName(string name)
513  {
514  return IsValidTag(name);
515  }
516 
521  public static bool IsValidAttributeValue(string value)
522  {
523  if (value == null)
524  {
525  return false;
526  }
527  return value.IndexOfAny(s_valueIllegalCharacters) == -1;
528  }
529 
530  private static string GetEscapeSequence(char c)
531  {
532  int num = s_escapeStringPairs.Length;
533  for (int i = 0; i < num; i += 2)
534  {
535  string text = s_escapeStringPairs[i];
536  string result = s_escapeStringPairs[i + 1];
537  if (text[0] == c)
538  {
539  return result;
540  }
541  }
542  return c.ToString();
543  }
544 
548  public static string Escape(string str)
549  {
550  if (str == null)
551  {
552  return null;
553  }
554  StringBuilder stringBuilder = null;
555  int length = str.Length;
556  int num = 0;
557  while (true)
558  {
559  int num2 = str.IndexOfAny(s_escapeChars, num);
560  if (num2 == -1)
561  {
562  break;
563  }
564  if (stringBuilder == null)
565  {
566  stringBuilder = new StringBuilder();
567  }
568  stringBuilder.Append(str, num, num2 - num);
569  stringBuilder.Append(GetEscapeSequence(str[num2]));
570  num = num2 + 1;
571  }
572  if (stringBuilder == null)
573  {
574  return str;
575  }
576  stringBuilder.Append(str, num, length - num);
577  return stringBuilder.ToString();
578  }
579 
580  private static string GetUnescapeSequence(string str, int index, out int newIndex)
581  {
582  int num = str.Length - index;
583  int num2 = s_escapeStringPairs.Length;
584  for (int i = 0; i < num2; i += 2)
585  {
586  string result = s_escapeStringPairs[i];
587  string text = s_escapeStringPairs[i + 1];
588  int length = text.Length;
589  if (length <= num && string.Compare(text, 0, str, index, length, StringComparison.Ordinal) == 0)
590  {
591  newIndex = index + text.Length;
592  return result;
593  }
594  }
595  newIndex = index + 1;
596  return str[index].ToString();
597  }
598 
599  private static string Unescape(string str)
600  {
601  if (str == null)
602  {
603  return null;
604  }
605  StringBuilder stringBuilder = null;
606  int length = str.Length;
607  int newIndex = 0;
608  while (true)
609  {
610  int num = str.IndexOf('&', newIndex);
611  if (num == -1)
612  {
613  break;
614  }
615  if (stringBuilder == null)
616  {
617  stringBuilder = new StringBuilder();
618  }
619  stringBuilder.Append(str, newIndex, num - newIndex);
620  stringBuilder.Append(GetUnescapeSequence(str, num, out newIndex));
621  }
622  if (stringBuilder == null)
623  {
624  return str;
625  }
626  stringBuilder.Append(str, newIndex, length - newIndex);
627  return stringBuilder.ToString();
628  }
629 
630  private static void ToStringHelperStringBuilder(object obj, string str)
631  {
632  ((StringBuilder)obj).Append(str);
633  }
634 
635  private static void ToStringHelperStreamWriter(object obj, string str)
636  {
637  ((StreamWriter)obj).Write(str);
638  }
639 
642  public override string ToString()
643  {
644  StringBuilder stringBuilder = new StringBuilder();
645  ToString("", stringBuilder, ToStringHelperStringBuilder);
646  return stringBuilder.ToString();
647  }
648 
649  internal void ToWriter(StreamWriter writer)
650  {
651  ToString("", writer, ToStringHelperStreamWriter);
652  }
653 
654  private void ToString(string indent, object obj, ToStringHelperFunc func)
655  {
656  func(obj, "<");
657  switch (m_type)
658  {
659  case SecurityElementType.Format:
660  func(obj, "?");
661  break;
662  case SecurityElementType.Comment:
663  func(obj, "!");
664  break;
665  }
666  func(obj, m_strTag);
667  if (m_lAttributes != null && m_lAttributes.Count > 0)
668  {
669  func(obj, " ");
670  int count = m_lAttributes.Count;
671  for (int i = 0; i < count; i += 2)
672  {
673  string str = (string)m_lAttributes[i];
674  string str2 = (string)m_lAttributes[i + 1];
675  func(obj, str);
676  func(obj, "=\"");
677  func(obj, str2);
678  func(obj, "\"");
679  if (i != m_lAttributes.Count - 2)
680  {
681  if (m_type == SecurityElementType.Regular)
682  {
683  func(obj, Environment.NewLine);
684  }
685  else
686  {
687  func(obj, " ");
688  }
689  }
690  }
691  }
692  if (m_strText == null && (m_lChildren == null || m_lChildren.Count == 0))
693  {
694  switch (m_type)
695  {
696  case SecurityElementType.Comment:
697  func(obj, ">");
698  break;
699  case SecurityElementType.Format:
700  func(obj, " ?>");
701  break;
702  default:
703  func(obj, "/>");
704  break;
705  }
706  func(obj, Environment.NewLine);
707  return;
708  }
709  func(obj, ">");
710  func(obj, m_strText);
711  if (m_lChildren != null)
712  {
713  ConvertSecurityElementFactories();
714  func(obj, Environment.NewLine);
715  for (int j = 0; j < m_lChildren.Count; j++)
716  {
717  ((SecurityElement)m_lChildren[j]).ToString("", obj, func);
718  }
719  }
720  func(obj, "</");
721  func(obj, m_strTag);
722  func(obj, ">");
723  func(obj, Environment.NewLine);
724  }
725 
730  public string Attribute(string name)
731  {
732  if (name == null)
733  {
734  throw new ArgumentNullException("name");
735  }
736  if (m_lAttributes == null)
737  {
738  return null;
739  }
740  int count = m_lAttributes.Count;
741  for (int i = 0; i < count; i += 2)
742  {
743  string a = (string)m_lAttributes[i];
744  if (string.Equals(a, name))
745  {
746  string str = (string)m_lAttributes[i + 1];
747  return Unescape(str);
748  }
749  }
750  return null;
751  }
752 
758  {
759  if (tag == null)
760  {
761  throw new ArgumentNullException("tag");
762  }
763  if (m_lChildren == null)
764  {
765  return null;
766  }
767  IEnumerator enumerator = m_lChildren.GetEnumerator();
768  while (enumerator.MoveNext())
769  {
770  SecurityElement securityElement = (SecurityElement)enumerator.Current;
771  if (securityElement != null && string.Equals(securityElement.Tag, tag))
772  {
773  return securityElement;
774  }
775  }
776  return null;
777  }
778 
779  internal IPermission ToPermission(bool ignoreTypeLoadFailures)
780  {
781  IPermission permission = XMLUtil.CreatePermission(this, PermissionState.None, ignoreTypeLoadFailures);
782  if (permission == null)
783  {
784  return null;
785  }
786  permission.FromXml(this);
787  PermissionToken token = PermissionToken.GetToken(permission);
788  return permission;
789  }
790 
791  [SecurityCritical]
792  internal object ToSecurityObject()
793  {
794  string strTag = m_strTag;
795  if (strTag == "PermissionSet")
796  {
797  PermissionSet permissionSet = new PermissionSet(PermissionState.None);
798  permissionSet.FromXml(this);
799  return permissionSet;
800  }
801  return ToPermission(ignoreTypeLoadFailures: false);
802  }
803 
804  internal string SearchForTextOfLocalName(string strLocalName)
805  {
806  if (strLocalName == null)
807  {
808  throw new ArgumentNullException("strLocalName");
809  }
810  if (m_strTag == null)
811  {
812  return null;
813  }
814  if (m_strTag.Equals(strLocalName) || m_strTag.EndsWith(":" + strLocalName, StringComparison.Ordinal))
815  {
816  return Unescape(m_strText);
817  }
818  if (m_lChildren == null)
819  {
820  return null;
821  }
822  IEnumerator enumerator = m_lChildren.GetEnumerator();
823  while (enumerator.MoveNext())
824  {
825  string text = ((SecurityElement)enumerator.Current).SearchForTextOfLocalName(strLocalName);
826  if (text != null)
827  {
828  return text;
829  }
830  }
831  return null;
832  }
833 
839  public string SearchForTextOfTag(string tag)
840  {
841  if (tag == null)
842  {
843  throw new ArgumentNullException("tag");
844  }
845  if (string.Equals(m_strTag, tag))
846  {
847  return Unescape(m_strText);
848  }
849  if (m_lChildren == null)
850  {
851  return null;
852  }
853  IEnumerator enumerator = m_lChildren.GetEnumerator();
854  ConvertSecurityElementFactories();
855  while (enumerator.MoveNext())
856  {
857  string text = ((SecurityElement)enumerator.Current).SearchForTextOfTag(tag);
858  if (text != null)
859  {
860  return text;
861  }
862  }
863  return null;
864  }
865  }
866 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
bool MoveNext()
Advances the enumerator to the next element of the collection.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
SecurityElement(string tag, string text)
Initializes a new instance of the T:System.Security.SecurityElement class with the specified tag and ...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
object Key
Gets the key of the current dictionary entry.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
SecurityElement SearchForChildByTag(string tag)
Finds a child by its tag name.
static bool IsValidAttributeValue(string value)
Determines whether a string is a valid attribute value.
string Tag
Gets or sets the tag name of an XML element.
void FromXml(SecurityElement e)
Reconstructs a security object with a specified state from an XML encoding.
string SearchForTextOfTag(string tag)
Finds a child by its tag name and returns the contained text.
static string Escape(string str)
Replaces invalid XML characters in a string with their valid XML equivalent.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
Implements a T:System.IO.TextWriter for writing characters to a stream in a particular encoding....
Definition: StreamWriter.cs:15
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
Represents the XML object model for encoding security objects. This class cannot be inherited.
Hashtable Attributes
Gets or sets the attributes of an XML element as name/value pairs.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
string Text
Gets or sets the text within an XML element.
SecurityElement Copy()
Creates and returns an identical copy of the current T:System.Security.SecurityElement object.
static bool IsValidText(string text)
Determines whether a string is valid as text within an XML element.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
SecurityElement(string tag)
Initializes a new instance of the T:System.Security.SecurityElement class with the specified tag.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
static SecurityElement FromString(string xml)
Creates a security element from an XML-encoded string.
string Attribute(string name)
Finds an attribute by name in an XML element.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
bool Equal(SecurityElement other)
Compares two XML element objects for equality.
object Value
Gets the value of the current dictionary entry.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
Enumerates the elements of a nongeneric dictionary.
ArrayList Children
Gets or sets the array of child elements of the XML element.
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
static bool IsValidAttributeName(string name)
Determines whether a string is a valid attribute name.
static bool IsValidTag(string tag)
Determines whether a string is a valid tag.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14