12 [global::__DynamicallyInvokable]
15 private class UrlDecoder
17 private int _bufferSize;
19 private int _numChars;
21 private char[] _charBuffer;
23 private int _numBytes;
25 private byte[] _byteBuffer;
29 private void FlushBytes()
33 _numChars += _encoding.
GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars);
38 internal UrlDecoder(
int bufferSize,
Encoding encoding)
40 _bufferSize = bufferSize;
42 _charBuffer =
new char[bufferSize];
45 internal void AddChar(
char ch)
51 _charBuffer[_numChars++] = ch;
54 internal void AddByte(
byte b)
56 if (_byteBuffer ==
null)
58 _byteBuffer =
new byte[_bufferSize];
60 _byteBuffer[_numBytes++] = b;
63 internal string GetString()
71 return new string(_charBuffer, 0, _numChars);
77 private static class HtmlEntities
79 private static string[] _entitiesList =
new string[253]
341 string[] entitiesList = _entitiesList;
342 foreach (
string text
in entitiesList)
344 dictionary.Add(text.Substring(2), text[0]);
349 public static char Lookup(
string entity)
356 private const char HIGH_SURROGATE_START =
'\ud800';
358 private const char LOW_SURROGATE_START =
'\udc00';
360 private const char LOW_SURROGATE_END =
'\udfff';
362 private const int UNICODE_PLANE00_END = 65535;
364 private const int UNICODE_PLANE01_START = 65536;
366 private const int UNICODE_PLANE16_END = 1114111;
368 private const int UnicodeReplacementChar = 65533;
370 private static readonly
char[] _htmlEntityEndingChars =
new char[2]
384 if (_htmlDecodeConformance != 0)
386 return _htmlDecodeConformance;
392 unicodeDecodingConformance2 = SettingsSectionInternal.Section.WebUtilityUnicodeDecodingConformance;
393 if (unicodeDecodingConformance2 <= UnicodeDecodingConformance.Auto || unicodeDecodingConformance2 >
UnicodeDecodingConformance.Loose)
395 unicodeDecodingConformance2 = unicodeDecodingConformance;
400 unicodeDecodingConformance2 = unicodeDecodingConformance;
404 return unicodeDecodingConformance;
406 _htmlDecodeConformance = unicodeDecodingConformance2;
407 return _htmlDecodeConformance;
415 if (_htmlEncodeConformance != 0)
417 return _htmlEncodeConformance;
423 unicodeEncodingConformance2 = SettingsSectionInternal.Section.WebUtilityUnicodeEncodingConformance;
424 if (unicodeEncodingConformance2 <= UnicodeEncodingConformance.Auto || unicodeEncodingConformance2 >
UnicodeEncodingConformance.Compat)
426 unicodeEncodingConformance2 = unicodeEncodingConformance;
431 unicodeEncodingConformance2 = unicodeEncodingConformance;
435 return unicodeEncodingConformance;
437 _htmlEncodeConformance = unicodeEncodingConformance2;
438 return _htmlEncodeConformance;
445 [global::__DynamicallyInvokable]
448 if (
string.IsNullOrEmpty(value))
452 int num = IndexOfHtmlEncodingChars(value, 0);
474 int num = IndexOfHtmlEncodingChars(value, 0);
481 int charsRemaining = value.Length - num;
482 fixed (
char* ptr = value)
489 output.
Write(*intPtr);
491 while (charsRemaining > 0)
499 output.Write("<"); 502 output.Write(">"); 505 output.Write(""
"); 508 output.Write("'
"); 511 output.Write("&
"); 521 if (c >= '\u00a0' && c < 'Ā') 525 else if (htmlEncodeConformance == UnicodeEncodingConformance.Strict && char.IsSurrogate(c)) 527 int nextUnicodeScalarValueFromUtf16Surrogate = GetNextUnicodeScalarValueFromUtf16Surrogate(ref pch, ref charsRemaining); 528 if (nextUnicodeScalarValueFromUtf16Surrogate >= 65536) 530 num3 = nextUnicodeScalarValueFromUtf16Surrogate; 534 c = (char)nextUnicodeScalarValueFromUtf16Surrogate; 540 output.Write(num3.ToString(NumberFormatInfo.InvariantInfo)); 558 [global::__DynamicallyInvokable] 559 public static string HtmlDecode(string value) 561 if (string.IsNullOrEmpty(value)) 565 if (!StringRequiresHtmlDecoding(value)) 569 StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); 570 HtmlDecode(value, stringWriter); 571 return stringWriter.ToString(); 578 public static void HtmlDecode(string value, TextWriter output) 586 throw new ArgumentNullException("output
"); 588 if (!StringRequiresHtmlDecoding(value)) 593 UnicodeDecodingConformance htmlDecodeConformance = HtmlDecodeConformance; 594 int length = value.Length; 595 for (int i = 0; i < length; i++) 600 int num = value.IndexOfAny(_htmlEntityEndingChars, i + 1); 601 if (num > 0 && value[num] == ';') 603 string text = value.Substring(i + 1, num - i - 1); 604 if (text.Length > 1 && text[0] == '#') 607 bool flag = (text[1] != 'x' && text[1] != 'X') ? uint.TryParse(text.Substring(1), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result) : uint.TryParse(text.Substring(2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out result); 610 switch (htmlDecodeConformance) 612 case UnicodeDecodingConformance.Strict: 613 flag = (result < 55296 || (57343 < result && result <= 1114111)); 615 case UnicodeDecodingConformance.Compat: 616 flag = (0 < result && result <= 65535); 618 case UnicodeDecodingConformance.Loose: 619 flag = (result <= 1114111); 630 output.Write((char)result); 634 ConvertSmpToUtf16(result, out char leadingSurrogate, out char trailingSurrogate); 635 output.Write(leadingSurrogate); 636 output.Write(trailingSurrogate); 645 char c2 = HtmlEntities.Lookup(text); 661 private unsafe static int IndexOfHtmlEncodingChars(string s, int startPos) 663 UnicodeEncodingConformance htmlEncodeConformance = HtmlEncodeConformance; 664 int num = s.Length - startPos; 665 fixed (char* ptr = s) 667 char* ptr2 = ptr + startPos; 680 return s.Length - num;
685 if (c >=
'\u00a0' && c <
'Ā')
687 return s.Length - num;
691 return s.Length - num;
701 private static byte[] UrlEncode(
byte[] bytes,
int offset,
int count,
bool alwaysCreateNewReturnValue)
703 byte[] array = UrlEncode(bytes, offset, count);
704 if (!alwaysCreateNewReturnValue || array ==
null || array != bytes)
708 return (
byte[])array.Clone();
711 private static byte[] UrlEncode(
byte[] bytes,
int offset,
int count)
713 if (!ValidateUrlEncodingParameters(bytes, offset, count))
719 for (
int i = 0; i < count; i++)
721 char c = (char)bytes[offset + i];
726 else if (!IsUrlSafeChar(c))
731 if (num == 0 && num2 == 0)
733 if (offset == 0 && bytes.Length == count)
737 byte[] array =
new byte[count];
738 Buffer.BlockCopy(bytes, offset, array, 0, count);
741 byte[] array2 =
new byte[count + num2 * 2];
743 for (
int j = 0; j < count; j++)
745 byte b = bytes[offset + j];
747 if (IsUrlSafeChar(c2))
758 array2[num3++] = (byte)IntToHex((b >> 4) & 0xF);
759 array2[num3++] = (byte)IntToHex(b & 0xF);
767 [global::__DynamicallyInvokable]
775 return Encoding.
UTF8.
GetString(UrlEncode(bytes, 0, bytes.Length, alwaysCreateNewReturnValue:
false));
783 [global::__DynamicallyInvokable]
786 return UrlEncode(value, offset, count, alwaysCreateNewReturnValue:
true);
789 private static string UrlDecodeInternal(
string value,
Encoding encoding)
795 int length = value.Length;
796 UrlDecoder urlDecoder =
new UrlDecoder(length, encoding);
797 for (
int i = 0; i < length; i++)
808 int num = HexToInt(value[i + 1]);
809 int num2 = HexToInt(value[i + 2]);
810 if (num >= 0 && num2 >= 0)
812 byte b = (byte)((num << 4) | num2);
814 urlDecoder.AddByte(b);
820 if ((c & 0xFF80) == 0)
822 urlDecoder.AddByte((
byte)c);
826 urlDecoder.AddChar(c);
829 return urlDecoder.GetString();
832 private static byte[] UrlDecodeInternal(
byte[] bytes,
int offset,
int count)
834 if (!ValidateUrlEncodingParameters(bytes, offset, count))
839 byte[] array =
new byte[count];
840 for (
int i = 0; i < count; i++)
842 int num2 = offset + i;
843 byte b = bytes[num2];
852 int num3 = HexToInt((
char)bytes[num2 + 1]);
853 int num4 = HexToInt((
char)bytes[num2 + 2]);
854 if (num3 >= 0 && num4 >= 0)
856 b = (byte)((num3 << 4) | num4);
864 if (num < array.Length)
866 byte[] array2 =
new byte[num];
867 Array.Copy(array, array2, num);
876 [global::__DynamicallyInvokable]
879 if (encodedValue ==
null)
891 [global::__DynamicallyInvokable]
894 return UrlDecodeInternal(encodedValue, offset, count);
897 private static void ConvertSmpToUtf16(uint smpChar, out
char leadingSurrogate, out
char trailingSurrogate)
899 int num = (int)(smpChar - 65536);
900 leadingSurrogate = (char)(num / 1024 + 55296);
901 trailingSurrogate = (char)(num % 1024 + 56320);
904 private unsafe
static int GetNextUnicodeScalarValueFromUtf16Surrogate(ref
char* pch, ref
int charsRemaining)
906 if (charsRemaining <= 1)
912 if (
char.IsSurrogatePair(c, c2))
916 return (c - 55296) * 1024 + (c2 - 56320) + 65536;
921 private static int HexToInt(
char h)
923 if (h < '0' || h >
'9')
925 if (h < 'a' || h >
'f')
927 if (h < 'A' || h >
'F')
938 private static char IntToHex(
int n)
942 return (
char)(n + 48);
944 return (
char)(n - 10 + 65);
947 private static bool IsUrlSafeChar(
char ch)
949 if ((ch >=
'a' && ch <=
'z') || (ch >=
'A' && ch <=
'Z') || (ch >=
'0' && ch <=
'9'))
968 private static bool ValidateUrlEncodingParameters(
byte[] bytes,
int offset,
int count)
970 if (bytes ==
null && count == 0)
976 throw new ArgumentNullException(
"bytes");
978 if (offset < 0 || offset > bytes.Length)
980 throw new ArgumentOutOfRangeException(
"offset");
982 if (count < 0 || offset + count > bytes.Length)
984 throw new ArgumentOutOfRangeException(
"count");
989 private static bool StringRequiresHtmlDecoding(
string s)
993 return s.IndexOf(
'&') >= 0;
995 foreach (
char c
in s)
997 if (c ==
'&' ||
char.IsSurrogate(c))
Represents a character encoding.To browse the .NET Framework source code for this type,...
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Provides methods for encoding and decoding URLs when processing Web requests.
unsafe string GetString(byte *bytes, int byteCount)
When overridden in a derived class, decodes a specified number of bytes starting at a specified addre...
virtual char [] GetChars(byte[] bytes)
When overridden in a derived class, decodes all the bytes in the specified byte array into a set of c...
virtual byte [] GetBytes(char[] chars)
When overridden in a derived class, encodes all the characters in the specified character array into ...
Implements a T:System.IO.TextWriter for writing information to a string. The information is stored in...
static unsafe void HtmlEncode(string value, TextWriter output)
Converts a string into an HTML-encoded string, and returns the output as a T:System....
static StringComparer Ordinal
Gets a T:System.StringComparer object that performs a case-sensitive ordinal string comparison.
UnicodeEncodingConformance
Controls how Unicode characters are output by the Overload:System.Net.WebUtility.HtmlEncode methods.
override string ToString()
Returns a string containing the characters written to the current StringWriter so far.
static byte [] UrlEncodeToBytes(byte[] value, int offset, int count)
Converts a byte array into a URL-encoded byte array.
Represents a writer that can write a sequential series of characters. This class is abstract.
static string UrlEncode(string value)
Converts a text string into a URL-encoded string.
static string UrlDecode(string encodedValue)
Converts a string that has been encoded for transmission in a URL into a decoded string.
The exception that is thrown when a configuration system error has occurred.
static string HtmlEncode(string value)
Converts a string to an HTML-encoded string.
static byte [] UrlDecodeToBytes(byte[] encodedValue, int offset, int count)
Converts an encoded byte array that has been encoded for transmission in a URL into a decoded byte ar...
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Provides information about a specific culture (called a locale for unmanaged code development)....
virtual void Write(char value)
Writes a character to the text string or stream.
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
UnicodeDecodingConformance
Controls how Unicode characters are interpreted by the Overload:System.Net.WebUtility....
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...