Files
2026-06-04 11:42:34 +02:00

663 lines
22 KiB
C#

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Xml;
namespace System.Security
{
/// <summary>Represents the XML object model for encoding security objects. This class cannot be inherited.</summary>
// Token: 0x020005CD RID: 1485
[ComVisible(true)]
[Serializable]
public sealed class SecurityElement
{
/// <summary>Initializes a new instance of the <see cref="T:System.Security.SecurityElement" /> class with the specified tag.</summary>
/// <param name="tag">The tag name of an XML element. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="tag" /> parameter is null. </exception>
/// <exception cref="T:System.ArgumentException">The <paramref name="tag" /> parameter is invalid in XML. </exception>
// Token: 0x06003625 RID: 13861 RVA: 0x000B91E8 File Offset: 0x000B73E8
public SecurityElement(string tag)
: this(tag, null)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.Security.SecurityElement" /> class with the specified tag and text.</summary>
/// <param name="tag">The tag name of the XML element. </param>
/// <param name="text">The text content within the element. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="tag" /> parameter is null. </exception>
/// <exception cref="T:System.ArgumentException">The <paramref name="tag" /> parameter or <paramref name="text" /> parameter is invalid in XML. </exception>
// Token: 0x06003626 RID: 13862 RVA: 0x000B91F4 File Offset: 0x000B73F4
public SecurityElement(string tag, string text)
{
if (tag == null)
{
throw new ArgumentNullException("tag");
}
if (!SecurityElement.IsValidTag(tag))
{
throw new ArgumentException(Locale.GetText("Invalid XML string") + ": " + tag);
}
this.tag = tag;
this.Text = text;
}
// Token: 0x06003627 RID: 13863 RVA: 0x000B924C File Offset: 0x000B744C
internal SecurityElement(SecurityElement se)
{
this.Tag = se.Tag;
this.Text = se.Text;
if (se.attributes != null)
{
foreach (object obj in se.attributes)
{
SecurityElement.SecurityAttribute securityAttribute = (SecurityElement.SecurityAttribute)obj;
this.AddAttribute(securityAttribute.Name, securityAttribute.Value);
}
}
if (se.children != null)
{
foreach (object obj2 in se.children)
{
SecurityElement securityElement = (SecurityElement)obj2;
this.AddChild(securityElement);
}
}
}
/// <summary>Gets or sets the attributes of an XML element as name/value pairs.</summary>
/// <returns>The <see cref="T:System.Collections.Hashtable" /> object for the attribute values of the XML element.</returns>
/// <exception cref="T:System.InvalidCastException">The name or value of the <see cref="T:System.Collections.Hashtable" /> object is invalid. </exception>
// Token: 0x17000A95 RID: 2709
// (get) Token: 0x06003629 RID: 13865 RVA: 0x000B93E8 File Offset: 0x000B75E8
// (set) Token: 0x0600362A RID: 13866 RVA: 0x000B9484 File Offset: 0x000B7684
public Hashtable Attributes
{
get
{
if (this.attributes == null)
{
return null;
}
Hashtable hashtable = new Hashtable(this.attributes.Count);
foreach (object obj in this.attributes)
{
SecurityElement.SecurityAttribute securityAttribute = (SecurityElement.SecurityAttribute)obj;
hashtable.Add(securityAttribute.Name, securityAttribute.Value);
}
return hashtable;
}
set
{
if (value == null || value.Count == 0)
{
this.attributes.Clear();
return;
}
if (this.attributes == null)
{
this.attributes = new ArrayList();
}
else
{
this.attributes.Clear();
}
IDictionaryEnumerator enumerator = value.GetEnumerator();
while (enumerator.MoveNext())
{
this.attributes.Add(new SecurityElement.SecurityAttribute((string)enumerator.Key, (string)enumerator.Value));
}
}
}
/// <summary>Gets or sets the array of child elements of the XML element.</summary>
/// <returns>The ordered child elements of the XML element as security elements.</returns>
/// <exception cref="T:System.ArgumentException">A child of the XML parent node is null. </exception>
// Token: 0x17000A96 RID: 2710
// (get) Token: 0x0600362B RID: 13867 RVA: 0x000B9514 File Offset: 0x000B7714
// (set) Token: 0x0600362C RID: 13868 RVA: 0x000B951C File Offset: 0x000B771C
public ArrayList Children
{
get
{
return this.children;
}
set
{
if (value != null)
{
using (IEnumerator enumerator = value.GetEnumerator())
{
while (enumerator.MoveNext())
{
if (enumerator.Current == null)
{
throw new ArgumentNullException();
}
}
}
}
this.children = value;
}
}
/// <summary>Gets or sets the tag name of an XML element.</summary>
/// <returns>The tag name of an XML element.</returns>
/// <exception cref="T:System.ArgumentNullException">The tag is null. </exception>
/// <exception cref="T:System.ArgumentException">The tag is not valid in XML. </exception>
// Token: 0x17000A97 RID: 2711
// (get) Token: 0x0600362D RID: 13869 RVA: 0x000B9594 File Offset: 0x000B7794
// (set) Token: 0x0600362E RID: 13870 RVA: 0x000B959C File Offset: 0x000B779C
public string Tag
{
get
{
return this.tag;
}
set
{
if (value == null)
{
throw new ArgumentNullException("Tag");
}
if (!SecurityElement.IsValidTag(value))
{
throw new ArgumentException(Locale.GetText("Invalid XML string") + ": " + value);
}
this.tag = value;
}
}
/// <summary>Gets or sets the text within an XML element.</summary>
/// <returns>The value of the text within an XML element.</returns>
/// <exception cref="T:System.ArgumentException">The text is not valid in XML. </exception>
// Token: 0x17000A98 RID: 2712
// (get) Token: 0x0600362F RID: 13871 RVA: 0x000B95E8 File Offset: 0x000B77E8
// (set) Token: 0x06003630 RID: 13872 RVA: 0x000B95F0 File Offset: 0x000B77F0
public string Text
{
get
{
return this.text;
}
set
{
if (value != null && !SecurityElement.IsValidText(value))
{
throw new ArgumentException(Locale.GetText("Invalid XML string") + ": " + value);
}
this.text = SecurityElement.Unescape(value);
}
}
/// <summary>Adds a name/value attribute to an XML element.</summary>
/// <param name="name">The name of the attribute. </param>
/// <param name="value">The value of the attribute. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="name" /> parameter or <paramref name="value" /> parameter is null. </exception>
/// <exception cref="T:System.ArgumentException">The <paramref name="name" /> parameter or <paramref name="value" /> parameter is invalid in XML.-or- An attribute with the name specified by the <paramref name="name" /> parameter already exists. </exception>
// Token: 0x06003631 RID: 13873 RVA: 0x000B9638 File Offset: 0x000B7838
public void AddAttribute(string name, string value)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
if (value == null)
{
throw new ArgumentNullException("value");
}
if (this.GetAttribute(name) != null)
{
throw new ArgumentException(Locale.GetText("Duplicate attribute : " + name));
}
if (this.attributes == null)
{
this.attributes = new ArrayList();
}
this.attributes.Add(new SecurityElement.SecurityAttribute(name, value));
}
/// <summary>Adds a child element to the XML element.</summary>
/// <param name="child">The child element to add. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="child" /> parameter is null. </exception>
// Token: 0x06003632 RID: 13874 RVA: 0x000B96B4 File Offset: 0x000B78B4
public void AddChild(SecurityElement child)
{
if (child == null)
{
throw new ArgumentNullException("child");
}
if (this.children == null)
{
this.children = new ArrayList();
}
this.children.Add(child);
}
/// <summary>Finds an attribute by name in an XML element.</summary>
/// <returns>The value associated with the named attribute, or null if no attribute with <paramref name="name" /> exists.</returns>
/// <param name="name">The name of the attribute for which to search. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="name" /> parameter is null. </exception>
// Token: 0x06003633 RID: 13875 RVA: 0x000B96F8 File Offset: 0x000B78F8
public string Attribute(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
SecurityElement.SecurityAttribute attribute = this.GetAttribute(name);
return (attribute != null) ? attribute.Value : null;
}
/// <summary>Creates and returns an identical copy of the current <see cref="T:System.Security.SecurityElement" /> object.</summary>
/// <returns>A copy of the current <see cref="T:System.Security.SecurityElement" /> object.</returns>
// Token: 0x06003634 RID: 13876 RVA: 0x000B9730 File Offset: 0x000B7930
[ComVisible(false)]
public SecurityElement Copy()
{
return new SecurityElement(this);
}
/// <summary>Compares two XML element objects for equality.</summary>
/// <returns>true if the tag, attribute names and values, child elements, and text fields in the current XML element are identical to their counterparts in the <paramref name="other" /> parameter; otherwise, false.</returns>
/// <param name="other">An XML element object to which to compare the current XML element object. </param>
// Token: 0x06003635 RID: 13877 RVA: 0x000B9738 File Offset: 0x000B7938
public bool Equal(SecurityElement other)
{
if (other == null)
{
return false;
}
if (this == other)
{
return true;
}
if (this.text != other.text)
{
return false;
}
if (this.tag != other.tag)
{
return false;
}
if (this.attributes == null && other.attributes != null && other.attributes.Count != 0)
{
return false;
}
if (other.attributes == null && this.attributes != null && this.attributes.Count != 0)
{
return false;
}
if (this.attributes != null && other.attributes != null)
{
if (this.attributes.Count != other.attributes.Count)
{
return false;
}
foreach (object obj in this.attributes)
{
SecurityElement.SecurityAttribute securityAttribute = (SecurityElement.SecurityAttribute)obj;
SecurityElement.SecurityAttribute attribute = other.GetAttribute(securityAttribute.Name);
if (attribute == null || securityAttribute.Value != attribute.Value)
{
return false;
}
}
}
if (this.children == null && other.children != null && other.children.Count != 0)
{
return false;
}
if (other.children == null && this.children != null && this.children.Count != 0)
{
return false;
}
if (this.children != null && other.children != null)
{
if (this.children.Count != other.children.Count)
{
return false;
}
for (int i = 0; i < this.children.Count; i++)
{
if (!((SecurityElement)this.children[i]).Equal((SecurityElement)other.children[i]))
{
return false;
}
}
}
return true;
}
/// <summary>Replaces invalid XML characters in a string with their valid XML equivalent.</summary>
/// <returns>The input string with invalid characters replaced.</returns>
/// <param name="str">The string within which to escape invalid characters. </param>
// Token: 0x06003636 RID: 13878 RVA: 0x000B996C File Offset: 0x000B7B6C
public static string Escape(string str)
{
if (str == null)
{
return null;
}
if (str.IndexOfAny(SecurityElement.invalid_chars) == -1)
{
return str;
}
StringBuilder stringBuilder = new StringBuilder();
int length = str.Length;
for (int i = 0; i < length; i++)
{
char c = str[i];
char c2 = c;
switch (c2)
{
case '"':
stringBuilder.Append("&quot;");
break;
default:
switch (c2)
{
case '<':
stringBuilder.Append("&lt;");
goto IL_D9;
case '>':
stringBuilder.Append("&gt;");
goto IL_D9;
}
stringBuilder.Append(c);
break;
case '&':
stringBuilder.Append("&amp;");
break;
case '\'':
stringBuilder.Append("&apos;");
break;
}
IL_D9:;
}
return stringBuilder.ToString();
}
// Token: 0x06003637 RID: 13879 RVA: 0x000B9A64 File Offset: 0x000B7C64
private static string Unescape(string str)
{
if (str == null)
{
return null;
}
StringBuilder stringBuilder = new StringBuilder(str);
stringBuilder.Replace("&lt;", "<");
stringBuilder.Replace("&gt;", ">");
stringBuilder.Replace("&amp;", "&");
stringBuilder.Replace("&quot;", "\"");
stringBuilder.Replace("&apos;", "'");
return stringBuilder.ToString();
}
/// <summary>Creates a security element from an XML-encoded string.</summary>
/// <returns>A <see cref="T:System.Security.SecurityElement" /> created from the XML.</returns>
/// <param name="xml">The XML-encoded string from which to create the security element.</param>
/// <exception cref="T:System.Security.XmlSyntaxException">
/// <paramref name="xml" /> contains one or more single quotation mark characters.</exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="xml" /> is null.</exception>
// Token: 0x06003638 RID: 13880 RVA: 0x000B9ADC File Offset: 0x000B7CDC
public static SecurityElement FromString(string xml)
{
if (xml == null)
{
throw new ArgumentNullException("xml");
}
if (xml.Length == 0)
{
throw new XmlSyntaxException(Locale.GetText("Empty string."));
}
SecurityElement securityElement;
try
{
SecurityParser securityParser = new SecurityParser();
securityParser.LoadXml(xml);
securityElement = securityParser.ToXml();
}
catch (Exception ex)
{
string text = Locale.GetText("Invalid XML.");
throw new XmlSyntaxException(text, ex);
}
return securityElement;
}
/// <summary>Determines whether a string is a valid attribute name.</summary>
/// <returns>true if the <paramref name="name" /> parameter is a valid XML attribute name; otherwise, false.</returns>
/// <param name="name">The attribute name to test for validity. </param>
// Token: 0x06003639 RID: 13881 RVA: 0x000B9B6C File Offset: 0x000B7D6C
public static bool IsValidAttributeName(string name)
{
return name != null && name.IndexOfAny(SecurityElement.invalid_attr_name_chars) == -1;
}
/// <summary>Determines whether a string is a valid attribute value.</summary>
/// <returns>true if the <paramref name="value" /> parameter is a valid XML attribute value; otherwise, false.</returns>
/// <param name="value">The attribute value to test for validity. </param>
// Token: 0x0600363A RID: 13882 RVA: 0x000B9B88 File Offset: 0x000B7D88
public static bool IsValidAttributeValue(string value)
{
return value != null && value.IndexOfAny(SecurityElement.invalid_attr_value_chars) == -1;
}
/// <summary>Determines whether a string is a valid tag.</summary>
/// <returns>true if the <paramref name="tag" /> parameter is a valid XML tag; otherwise, false.</returns>
/// <param name="tag">The tag to test for validity. </param>
// Token: 0x0600363B RID: 13883 RVA: 0x000B9BA4 File Offset: 0x000B7DA4
public static bool IsValidTag(string tag)
{
return tag != null && tag.IndexOfAny(SecurityElement.invalid_tag_chars) == -1;
}
/// <summary>Determines whether a string is valid as text within an XML element.</summary>
/// <returns>true if the <paramref name="text" /> parameter is a valid XML text element; otherwise, false.</returns>
/// <param name="text">The text to test for validity. </param>
// Token: 0x0600363C RID: 13884 RVA: 0x000B9BC0 File Offset: 0x000B7DC0
public static bool IsValidText(string text)
{
return text != null && text.IndexOfAny(SecurityElement.invalid_text_chars) == -1;
}
/// <summary>Finds a child by its tag name.</summary>
/// <returns>The first child XML element with the specified tag value, or null if no child element with <paramref name="tag" /> exists.</returns>
/// <param name="tag">The tag for which to search in child elements. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="tag" /> parameter is null. </exception>
// Token: 0x0600363D RID: 13885 RVA: 0x000B9BDC File Offset: 0x000B7DDC
public SecurityElement SearchForChildByTag(string tag)
{
if (tag == null)
{
throw new ArgumentNullException("tag");
}
if (this.children == null)
{
return null;
}
for (int i = 0; i < this.children.Count; i++)
{
SecurityElement securityElement = (SecurityElement)this.children[i];
if (securityElement.tag == tag)
{
return securityElement;
}
}
return null;
}
/// <summary>Finds a child by its tag name and returns the contained text.</summary>
/// <returns>The text contents of the first child element with the specified tag value.</returns>
/// <param name="tag">The tag for which to search in child elements. </param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="tag" /> is null. </exception>
// Token: 0x0600363E RID: 13886 RVA: 0x000B9C4C File Offset: 0x000B7E4C
public string SearchForTextOfTag(string tag)
{
if (tag == null)
{
throw new ArgumentNullException("tag");
}
if (this.tag == tag)
{
return this.text;
}
if (this.children == null)
{
return null;
}
for (int i = 0; i < this.children.Count; i++)
{
string text = ((SecurityElement)this.children[i]).SearchForTextOfTag(tag);
if (text != null)
{
return text;
}
}
return null;
}
/// <summary>Produces a string representation of an XML element and its constituent attributes, child elements, and text.</summary>
/// <returns>The XML element and its contents.</returns>
// Token: 0x0600363F RID: 13887 RVA: 0x000B9CCC File Offset: 0x000B7ECC
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
this.ToXml(ref stringBuilder, 0);
return stringBuilder.ToString();
}
// Token: 0x06003640 RID: 13888 RVA: 0x000B9CF0 File Offset: 0x000B7EF0
private void ToXml(ref StringBuilder s, int level)
{
s.Append("<");
s.Append(this.tag);
if (this.attributes != null)
{
s.Append(" ");
for (int i = 0; i < this.attributes.Count; i++)
{
SecurityElement.SecurityAttribute securityAttribute = (SecurityElement.SecurityAttribute)this.attributes[i];
s.Append(securityAttribute.Name).Append("=\"").Append(SecurityElement.Escape(securityAttribute.Value))
.Append("\"");
if (i != this.attributes.Count - 1)
{
s.Append(Environment.NewLine);
}
}
}
if ((this.text == null || this.text == string.Empty) && (this.children == null || this.children.Count == 0))
{
s.Append("/>").Append(Environment.NewLine);
}
else
{
s.Append(">").Append(SecurityElement.Escape(this.text));
if (this.children != null)
{
s.Append(Environment.NewLine);
foreach (object obj in this.children)
{
SecurityElement securityElement = (SecurityElement)obj;
securityElement.ToXml(ref s, level + 1);
}
}
s.Append("</").Append(this.tag).Append(">")
.Append(Environment.NewLine);
}
}
// Token: 0x06003641 RID: 13889 RVA: 0x000B9ED0 File Offset: 0x000B80D0
internal SecurityElement.SecurityAttribute GetAttribute(string name)
{
if (this.attributes != null)
{
foreach (object obj in this.attributes)
{
SecurityElement.SecurityAttribute securityAttribute = (SecurityElement.SecurityAttribute)obj;
if (securityAttribute.Name == name)
{
return securityAttribute;
}
}
}
return null;
}
// Token: 0x0400162C RID: 5676
private string text;
// Token: 0x0400162D RID: 5677
private string tag;
// Token: 0x0400162E RID: 5678
private ArrayList attributes;
// Token: 0x0400162F RID: 5679
private ArrayList children;
// Token: 0x04001630 RID: 5680
private static readonly char[] invalid_tag_chars = new char[] { ' ', '<', '>' };
// Token: 0x04001631 RID: 5681
private static readonly char[] invalid_text_chars = new char[] { '<', '>' };
// Token: 0x04001632 RID: 5682
private static readonly char[] invalid_attr_name_chars = new char[] { ' ', '<', '>' };
// Token: 0x04001633 RID: 5683
private static readonly char[] invalid_attr_value_chars = new char[] { '"', '<', '>' };
// Token: 0x04001634 RID: 5684
private static readonly char[] invalid_chars = new char[] { '<', '>', '"', '\'', '&' };
// Token: 0x020005CE RID: 1486
internal class SecurityAttribute
{
// Token: 0x06003642 RID: 13890 RVA: 0x000B9F60 File Offset: 0x000B8160
public SecurityAttribute(string name, string value)
{
if (!SecurityElement.IsValidAttributeName(name))
{
throw new ArgumentException(Locale.GetText("Invalid XML attribute name") + ": " + name);
}
if (!SecurityElement.IsValidAttributeValue(value))
{
throw new ArgumentException(Locale.GetText("Invalid XML attribute value") + ": " + value);
}
this._name = name;
this._value = SecurityElement.Unescape(value);
}
// Token: 0x17000A99 RID: 2713
// (get) Token: 0x06003643 RID: 13891 RVA: 0x000B9FD4 File Offset: 0x000B81D4
public string Name
{
get
{
return this._name;
}
}
// Token: 0x17000A9A RID: 2714
// (get) Token: 0x06003644 RID: 13892 RVA: 0x000B9FDC File Offset: 0x000B81DC
public string Value
{
get
{
return this._value;
}
}
// Token: 0x04001635 RID: 5685
private string _name;
// Token: 0x04001636 RID: 5686
private string _value;
}
}
}