408 lines
19 KiB
C#
408 lines
19 KiB
C#
using System;
|
||
using System.Text;
|
||
|
||
namespace System.Globalization
|
||
{
|
||
/// <summary>Supports the use of non-ASCII characters for Internet domain names. This class cannot be inherited.</summary>
|
||
// Token: 0x0200018A RID: 394
|
||
public sealed class IdnMapping
|
||
{
|
||
/// <summary>Gets or sets a value indicating whether unassigned Unicode code points are used in operations performed by members of the current <see cref="T:System.Globalization.IdnMapping" /> object.</summary>
|
||
/// <returns>true if unassigned code points are used in operations; otherwise, false.</returns>
|
||
// Token: 0x1700035E RID: 862
|
||
// (get) Token: 0x0600139F RID: 5023 RVA: 0x0004C0FC File Offset: 0x0004A2FC
|
||
// (set) Token: 0x060013A0 RID: 5024 RVA: 0x0004C104 File Offset: 0x0004A304
|
||
public bool AllowUnassigned
|
||
{
|
||
get
|
||
{
|
||
return this.allow_unassigned;
|
||
}
|
||
set
|
||
{
|
||
this.allow_unassigned = value;
|
||
}
|
||
}
|
||
|
||
/// <summary>Gets or sets a value indicating whether standard or nonstandard naming conventions are used in operations performed by members of the current <see cref="T:System.Globalization.IdnMapping" /> object.</summary>
|
||
/// <returns>true if nonstandard naming conventions are used in operations; otherwise, false.</returns>
|
||
// Token: 0x1700035F RID: 863
|
||
// (get) Token: 0x060013A1 RID: 5025 RVA: 0x0004C110 File Offset: 0x0004A310
|
||
// (set) Token: 0x060013A2 RID: 5026 RVA: 0x0004C118 File Offset: 0x0004A318
|
||
public bool UseStd3AsciiRules
|
||
{
|
||
get
|
||
{
|
||
return this.use_std3;
|
||
}
|
||
set
|
||
{
|
||
this.use_std3 = value;
|
||
}
|
||
}
|
||
|
||
/// <summary>Indicates whether a specified object and this <see cref="T:System.Globalization.IdnMapping" /> object are equal.</summary>
|
||
/// <returns>true if the <paramref name="obj" /> parameter is derived from <see cref="T:System.Globalization.IdnMapping" /> and its <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties are equal; otherwise, false. </returns>
|
||
/// <param name="obj">An object.</param>
|
||
// Token: 0x060013A3 RID: 5027 RVA: 0x0004C124 File Offset: 0x0004A324
|
||
public override bool Equals(object obj)
|
||
{
|
||
IdnMapping idnMapping = obj as IdnMapping;
|
||
return idnMapping != null && this.allow_unassigned == idnMapping.allow_unassigned && this.use_std3 == idnMapping.use_std3;
|
||
}
|
||
|
||
/// <summary>Returns a hash code for this <see cref="T:System.Globalization.IdnMapping" /> object.</summary>
|
||
/// <returns>One of four 32-bit signed constants derived from the properties of a <see cref="T:System.Globalization.IdnMapping" /> object. The return value has no special meaning and is not suitable for use in a hash code algorithm.</returns>
|
||
// Token: 0x060013A4 RID: 5028 RVA: 0x0004C160 File Offset: 0x0004A360
|
||
public override int GetHashCode()
|
||
{
|
||
return ((!this.allow_unassigned) ? 0 : 2) + ((!this.use_std3) ? 0 : 1);
|
||
}
|
||
|
||
/// <summary>Encodes a string of one or more domain name labels that consist of Unicode characters to a string of Unicode characters in the US-ASCII character range.</summary>
|
||
/// <returns>The equivalent of the string specified by the <paramref name="unicode" /> parameter, consisting of displayable Unicode characters in the US-ASCII character range (U+0020 to U+007E) and formatted according to the Internationalizing Domain Names in Applications (IDNA) standard.</returns>
|
||
/// <param name="unicode">An input string to convert, which consists of one or more domain name labels delimited with label separators.</param>
|
||
/// <exception cref="T:System.ArgumentNullException">
|
||
/// <paramref name="unicode" /> is null.</exception>
|
||
/// <exception cref="T:System.ArgumentException">
|
||
/// <paramref name="unicode" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.-or-A label contains one or more of the Unicode control characters from U+0001 through U+001F, or U+007F.</exception>
|
||
// Token: 0x060013A5 RID: 5029 RVA: 0x0004C188 File Offset: 0x0004A388
|
||
public string GetAscii(string unicode)
|
||
{
|
||
if (unicode == null)
|
||
{
|
||
throw new ArgumentNullException("unicode");
|
||
}
|
||
return this.GetAscii(unicode, 0, unicode.Length);
|
||
}
|
||
|
||
/// <summary>Encodes a substring of one or more domain name labels that consist of Unicode characters to a string of Unicode characters in the US-ASCII character range. </summary>
|
||
/// <returns>The equivalent of the substring specified by the <paramref name="unicode" /> and <paramref name="index" /> parameters, consisting of displayable Unicode characters in the US-ASCII character range (U+0020 to U+007E) and formatted according to the Internationalizing Domain Names in Applications (IDNA) standard.</returns>
|
||
/// <param name="unicode">An input string to convert, which consists of one or more domain name labels delimited with label separators.</param>
|
||
/// <param name="index">A zero-based offset into <paramref name="unicode" /> that specifies the start of the substring. The conversion operation continues to the end of <paramref name="unicode" />.</param>
|
||
/// <exception cref="T:System.ArgumentNullException">
|
||
/// <paramref name="unicode" /> is null.</exception>
|
||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||
/// <paramref name="index" /> or <paramref name="count" /> is less than zero.-or-<paramref name="index" /> is greater than the length of <paramref name="unicode" />.</exception>
|
||
/// <exception cref="T:System.ArgumentException">
|
||
/// <paramref name="unicode" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.-or-A label contains one or more of the Unicode control characters from U+0001 through U+001F, or U+007F.</exception>
|
||
// Token: 0x060013A6 RID: 5030 RVA: 0x0004C1AC File Offset: 0x0004A3AC
|
||
public string GetAscii(string unicode, int index)
|
||
{
|
||
if (unicode == null)
|
||
{
|
||
throw new ArgumentNullException("unicode");
|
||
}
|
||
return this.GetAscii(unicode, index, unicode.Length - index);
|
||
}
|
||
|
||
/// <summary>Encodes a substring of one or more domain name labels that consist of Unicode characters to a string of Unicode characters in the US-ASCII character range. The string is formatted according to the Internationalizing Domain Names in Applications (IDNA) standard. </summary>
|
||
/// <returns>The equivalent of the substring specified by the <paramref name="unicode" />, <paramref name="index" />, and <paramref name="count" /> parameters, consisting of displayable Unicode characters in the US-ASCII character range (U+0020 to U+007E) and formatted according to the IDNA standard.</returns>
|
||
/// <param name="unicode">An input string to convert, which consists of one or more domain name labels delimited with label separators.</param>
|
||
/// <param name="index">A zero-based offset into <paramref name="unicode" /> that specifies the start of the substring.</param>
|
||
/// <param name="count">The number of characters to convert in the substring that starts at the position specified by <paramref name="unicode" /> and <paramref name="index" />. </param>
|
||
/// <exception cref="T:System.ArgumentNullException">
|
||
/// <paramref name="unicode" /> is null.</exception>
|
||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||
/// <paramref name="index" /> or <paramref name="count" /> is less than zero.-or-<paramref name="index" /> is greater than the length of <paramref name="unicode" />.-or-<paramref name="index" /> is greater than the length of <paramref name="unicode" /> minus <paramref name="count" />.</exception>
|
||
/// <exception cref="T:System.ArgumentException">
|
||
/// <paramref name="unicode" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.-or-A label contains one or more of the Unicode control characters from U+0001 through U+001F, or U+007F.</exception>
|
||
// Token: 0x060013A7 RID: 5031 RVA: 0x0004C1D0 File Offset: 0x0004A3D0
|
||
public string GetAscii(string unicode, int index, int count)
|
||
{
|
||
if (unicode == null)
|
||
{
|
||
throw new ArgumentNullException("unicode");
|
||
}
|
||
if (index < 0)
|
||
{
|
||
throw new ArgumentOutOfRangeException("index must be non-negative value");
|
||
}
|
||
if (count < 0 || index + count > unicode.Length)
|
||
{
|
||
throw new ArgumentOutOfRangeException("index + count must point inside the argument unicode string");
|
||
}
|
||
return this.Convert(unicode, index, count, true);
|
||
}
|
||
|
||
// Token: 0x060013A8 RID: 5032 RVA: 0x0004C22C File Offset: 0x0004A42C
|
||
private string Convert(string input, int index, int count, bool toAscii)
|
||
{
|
||
string text = input.Substring(index, count);
|
||
for (int i = 0; i < text.Length; i++)
|
||
{
|
||
if (text[i] >= '\u0080')
|
||
{
|
||
text = text.ToLower(CultureInfo.InvariantCulture);
|
||
break;
|
||
}
|
||
}
|
||
string[] array = text.Split(new char[] { '.', '。', '.', '。' });
|
||
int num = 0;
|
||
for (int j = 0; j < array.Length; j++)
|
||
{
|
||
if (array[j].Length != 0 || j + 1 != array.Length)
|
||
{
|
||
if (toAscii)
|
||
{
|
||
array[j] = this.ToAscii(array[j], num);
|
||
}
|
||
else
|
||
{
|
||
array[j] = this.ToUnicode(array[j], num);
|
||
}
|
||
}
|
||
num += array[j].Length;
|
||
}
|
||
return string.Join(".", array);
|
||
}
|
||
|
||
// Token: 0x060013A9 RID: 5033 RVA: 0x0004C310 File Offset: 0x0004A510
|
||
private string ToAscii(string s, int offset)
|
||
{
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
if (s[i] < ' ' || s[i] == '\u007f')
|
||
{
|
||
throw new ArgumentException(string.Format("Not allowed character was found, at {0}", offset + i));
|
||
}
|
||
if (s[i] >= '\u0080')
|
||
{
|
||
s = this.NamePrep(s, offset);
|
||
break;
|
||
}
|
||
}
|
||
if (this.use_std3)
|
||
{
|
||
this.VerifyStd3AsciiRules(s, offset);
|
||
}
|
||
int j = 0;
|
||
while (j < s.Length)
|
||
{
|
||
if (s[j] >= '\u0080')
|
||
{
|
||
if (s.StartsWith("xn--", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
throw new ArgumentException(string.Format("The input string must not start with ACE (xn--), at {0}", offset + j));
|
||
}
|
||
s = this.puny.Encode(s, offset);
|
||
s = "xn--" + s;
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
j++;
|
||
}
|
||
}
|
||
this.VerifyLength(s, offset);
|
||
return s;
|
||
}
|
||
|
||
// Token: 0x060013AA RID: 5034 RVA: 0x0004C418 File Offset: 0x0004A618
|
||
private void VerifyLength(string s, int offset)
|
||
{
|
||
if (s.Length == 0)
|
||
{
|
||
throw new ArgumentException(string.Format("A label in the input string resulted in an invalid zero-length string, at {0}", offset));
|
||
}
|
||
if (s.Length > 63)
|
||
{
|
||
throw new ArgumentException(string.Format("A label in the input string exceeded the length in ASCII representation, at {0}", offset));
|
||
}
|
||
}
|
||
|
||
// Token: 0x060013AB RID: 5035 RVA: 0x0004C46C File Offset: 0x0004A66C
|
||
private string NamePrep(string s, int offset)
|
||
{
|
||
s = s.Normalize(NormalizationForm.FormKC);
|
||
this.VerifyProhibitedCharacters(s, offset);
|
||
if (!this.allow_unassigned)
|
||
{
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
if (char.GetUnicodeCategory(s, i) == UnicodeCategory.OtherNotAssigned)
|
||
{
|
||
throw new ArgumentException(string.Format("Use of unassigned Unicode characer is prohibited in this IdnMapping, at {0}", offset + i));
|
||
}
|
||
}
|
||
}
|
||
return s;
|
||
}
|
||
|
||
// Token: 0x060013AC RID: 5036 RVA: 0x0004C4D4 File Offset: 0x0004A6D4
|
||
private void VerifyProhibitedCharacters(string s, int offset)
|
||
{
|
||
int i = 0;
|
||
while (i < s.Length)
|
||
{
|
||
switch (char.GetUnicodeCategory(s, i))
|
||
{
|
||
case UnicodeCategory.SpaceSeparator:
|
||
if (s[i] >= '\u0080')
|
||
{
|
||
goto IL_164;
|
||
}
|
||
break;
|
||
case UnicodeCategory.LineSeparator:
|
||
case UnicodeCategory.ParagraphSeparator:
|
||
case UnicodeCategory.Format:
|
||
goto IL_80;
|
||
case UnicodeCategory.Control:
|
||
if (s[i] == '\0' || s[i] >= '\u0080')
|
||
{
|
||
goto IL_164;
|
||
}
|
||
break;
|
||
case UnicodeCategory.Surrogate:
|
||
case UnicodeCategory.PrivateUse:
|
||
goto IL_164;
|
||
default:
|
||
goto IL_80;
|
||
}
|
||
IL_17C:
|
||
i++;
|
||
continue;
|
||
IL_80:
|
||
char c = s[i];
|
||
if (('\ufddf' > c || c > '\ufdef') && ((c & '\uffff') != '\ufffe' && ('\ufff9' > c || c > '\ufffd')) && ('⿰' > c || c > '⿻') && ('\u202a' > c || c > '\u202e') && ('\u206a' > c || c > '\u206f'))
|
||
{
|
||
char c2 = c;
|
||
if (c2 != '\u0340' && c2 != '\u0341' && c2 != '\u200e' && c2 != '\u200f' && c2 != '\u2028' && c2 != '\u2029')
|
||
{
|
||
goto IL_17C;
|
||
}
|
||
}
|
||
IL_164:
|
||
throw new ArgumentException(string.Format("Not allowed character was in the input string, at {0}", offset + i));
|
||
}
|
||
}
|
||
|
||
// Token: 0x060013AD RID: 5037 RVA: 0x0004C670 File Offset: 0x0004A870
|
||
private void VerifyStd3AsciiRules(string s, int offset)
|
||
{
|
||
if (s.Length > 0 && s[0] == '-')
|
||
{
|
||
throw new ArgumentException(string.Format("'-' is not allowed at head of a sequence in STD3 mode, found at {0}", offset));
|
||
}
|
||
if (s.Length > 0 && s[s.Length - 1] == '-')
|
||
{
|
||
throw new ArgumentException(string.Format("'-' is not allowed at tail of a sequence in STD3 mode, found at {0}", offset + s.Length - 1));
|
||
}
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
char c = s[i];
|
||
if (c != '-')
|
||
{
|
||
if (c <= '/' || (':' <= c && c <= '@') || ('[' <= c && c <= '`') || ('{' <= c && c <= '\u007f'))
|
||
{
|
||
throw new ArgumentException(string.Format("Not allowed character in STD3 mode, found at {0}", offset + i));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>Decodes a string of one or more domain name labels encoded according to the Internationalizing Domain Names in Applications (IDNA) standard to a string of Unicode characters. </summary>
|
||
/// <returns>The Unicode equivalent of the IDNA substring specified by the <paramref name="ascii" /> parameter.</returns>
|
||
/// <param name="ascii">One or more labels in the US-ASCII character range (U+0020 to U+007E) encoded according to the IDNA standard. </param>
|
||
/// <exception cref="T:System.ArgumentNullException">
|
||
/// <paramref name="ascii" /> is null.</exception>
|
||
/// <exception cref="T:System.ArgumentException">
|
||
/// <paramref name="ascii" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.</exception>
|
||
// Token: 0x060013AE RID: 5038 RVA: 0x0004C76C File Offset: 0x0004A96C
|
||
public string GetUnicode(string ascii)
|
||
{
|
||
if (ascii == null)
|
||
{
|
||
throw new ArgumentNullException("ascii");
|
||
}
|
||
return this.GetUnicode(ascii, 0, ascii.Length);
|
||
}
|
||
|
||
/// <summary>Decodes a substring of one or more domain name labels encoded according to the Internationalizing Domain Names in Applications (IDNA) standard to a string of Unicode characters. </summary>
|
||
/// <returns>The Unicode equivalent of the IDNA substring specified by the <paramref name="ascii" /> and <paramref name="index" /> parameters.</returns>
|
||
/// <param name="ascii">One or more labels in the US-ASCII character range (U+0020 to U+007E) encoded according to the IDNA standard. </param>
|
||
/// <param name="index">A zero-based offset into <paramref name="ascii" /> that specifies the start of the substring. </param>
|
||
/// <exception cref="T:System.ArgumentNullException">
|
||
/// <paramref name="ascii" /> is null.</exception>
|
||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||
/// <paramref name="index" /> is less than zero.-or-<paramref name="index" /> is greater than the length of <paramref name="ascii" />.</exception>
|
||
/// <exception cref="T:System.ArgumentException">
|
||
/// <paramref name="ascii" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.</exception>
|
||
// Token: 0x060013AF RID: 5039 RVA: 0x0004C790 File Offset: 0x0004A990
|
||
public string GetUnicode(string ascii, int index)
|
||
{
|
||
if (ascii == null)
|
||
{
|
||
throw new ArgumentNullException("ascii");
|
||
}
|
||
return this.GetUnicode(ascii, index, ascii.Length - index);
|
||
}
|
||
|
||
/// <summary>Decodes a substring of one or more domain name labels encoded according to the Internationalizing Domain Names in Applications (IDNA) standard to a string of Unicode characters. </summary>
|
||
/// <returns>The Unicode equivalent of the IDNA substring specified by the <paramref name="ascii" />, <paramref name="index" />, and <paramref name="count" /> parameters.</returns>
|
||
/// <param name="ascii">One or more labels in the US-ASCII character range (U+0020 to U+007E) encoded according to the IDNA standard. </param>
|
||
/// <param name="index">A zero-based offset into <paramref name="ascii" /> that specifies the start of the substring. </param>
|
||
/// <param name="count">The number of characters to convert in the substring that starts at the position specified by <paramref name="ascii" /> and <paramref name="index" />. </param>
|
||
/// <exception cref="T:System.ArgumentNullException">
|
||
/// <paramref name="ascii" /> is null.</exception>
|
||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||
/// <paramref name="index" /> or <paramref name="count" /> is less than zero.-or-<paramref name="index" /> is greater than the length of <paramref name="ascii" />.-or-<paramref name="index" /> is greater than the length of <paramref name="ascii" /> minus <paramref name="count" />.</exception>
|
||
/// <exception cref="T:System.ArgumentException">
|
||
/// <paramref name="ascii" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.</exception>
|
||
// Token: 0x060013B0 RID: 5040 RVA: 0x0004C7B4 File Offset: 0x0004A9B4
|
||
public string GetUnicode(string ascii, int index, int count)
|
||
{
|
||
if (ascii == null)
|
||
{
|
||
throw new ArgumentNullException("ascii");
|
||
}
|
||
if (index < 0)
|
||
{
|
||
throw new ArgumentOutOfRangeException("index must be non-negative value");
|
||
}
|
||
if (count < 0 || index + count > ascii.Length)
|
||
{
|
||
throw new ArgumentOutOfRangeException("index + count must point inside the argument ascii string");
|
||
}
|
||
return this.Convert(ascii, index, count, false);
|
||
}
|
||
|
||
// Token: 0x060013B1 RID: 5041 RVA: 0x0004C810 File Offset: 0x0004AA10
|
||
private string ToUnicode(string s, int offset)
|
||
{
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
if (s[i] >= '\u0080')
|
||
{
|
||
s = this.NamePrep(s, offset);
|
||
break;
|
||
}
|
||
}
|
||
if (!s.StartsWith("xn--", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return s;
|
||
}
|
||
s = s.ToLower(CultureInfo.InvariantCulture);
|
||
string text = s;
|
||
s = s.Substring(4);
|
||
s = this.puny.Decode(s, offset);
|
||
string text2 = s;
|
||
s = this.ToAscii(s, offset);
|
||
if (string.Compare(text, s, StringComparison.OrdinalIgnoreCase) != 0)
|
||
{
|
||
throw new ArgumentException(string.Format("ToUnicode() failed at verifying the result, at label part from {0}", offset));
|
||
}
|
||
return text2;
|
||
}
|
||
|
||
// Token: 0x04000571 RID: 1393
|
||
private bool allow_unassigned;
|
||
|
||
// Token: 0x04000572 RID: 1394
|
||
private bool use_std3;
|
||
|
||
// Token: 0x04000573 RID: 1395
|
||
private Punycode puny = new Punycode();
|
||
}
|
||
}
|