mscorlib(4.0.0.0) API with additions
WebUtility.cs
4 using System.IO;
7 using System.Text;
8 
9 namespace System.Net
10 {
12  [global::__DynamicallyInvokable]
13  public static class WebUtility
14  {
15  private class UrlDecoder
16  {
17  private int _bufferSize;
18 
19  private int _numChars;
20 
21  private char[] _charBuffer;
22 
23  private int _numBytes;
24 
25  private byte[] _byteBuffer;
26 
27  private Encoding _encoding;
28 
29  private void FlushBytes()
30  {
31  if (_numBytes > 0)
32  {
33  _numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars);
34  _numBytes = 0;
35  }
36  }
37 
38  internal UrlDecoder(int bufferSize, Encoding encoding)
39  {
40  _bufferSize = bufferSize;
41  _encoding = encoding;
42  _charBuffer = new char[bufferSize];
43  }
44 
45  internal void AddChar(char ch)
46  {
47  if (_numBytes > 0)
48  {
49  FlushBytes();
50  }
51  _charBuffer[_numChars++] = ch;
52  }
53 
54  internal void AddByte(byte b)
55  {
56  if (_byteBuffer == null)
57  {
58  _byteBuffer = new byte[_bufferSize];
59  }
60  _byteBuffer[_numBytes++] = b;
61  }
62 
63  internal string GetString()
64  {
65  if (_numBytes > 0)
66  {
67  FlushBytes();
68  }
69  if (_numChars > 0)
70  {
71  return new string(_charBuffer, 0, _numChars);
72  }
73  return string.Empty;
74  }
75  }
76 
77  private static class HtmlEntities
78  {
79  private static string[] _entitiesList = new string[253]
80  {
81  "\"-quot",
82  "&-amp",
83  "'-apos",
84  "<-lt",
85  ">-gt",
86  "\u00a0-nbsp",
87  "¡-iexcl",
88  "¢-cent",
89  "£-pound",
90  "¤-curren",
91  "¥-yen",
92  "¦-brvbar",
93  "§-sect",
94  "\u00a8-uml",
95  "©-copy",
96  "ª-ordf",
97  "«-laquo",
98  "¬-not",
99  "­-shy",
100  "®-reg",
101  "\u00af-macr",
102  "°-deg",
103  "±-plusmn",
104  "²-sup2",
105  "³-sup3",
106  "\u00b4-acute",
107  "µ-micro",
108  "¶-para",
109  "·-middot",
110  "\u00b8-cedil",
111  "¹-sup1",
112  "º-ordm",
113  "»-raquo",
114  "¼-frac14",
115  "½-frac12",
116  "¾-frac34",
117  "¿-iquest",
118  "À-Agrave",
119  "Á-Aacute",
120  "Â-Acirc",
121  "Ã-Atilde",
122  "Ä-Auml",
123  "Å-Aring",
124  "Æ-AElig",
125  "Ç-Ccedil",
126  "È-Egrave",
127  "É-Eacute",
128  "Ê-Ecirc",
129  "Ë-Euml",
130  "Ì-Igrave",
131  "Í-Iacute",
132  "Î-Icirc",
133  "Ï-Iuml",
134  "Ð-ETH",
135  "Ñ-Ntilde",
136  "Ò-Ograve",
137  "Ó-Oacute",
138  "Ô-Ocirc",
139  "Õ-Otilde",
140  "Ö-Ouml",
141  "×-times",
142  "Ø-Oslash",
143  "Ù-Ugrave",
144  "Ú-Uacute",
145  "Û-Ucirc",
146  "Ü-Uuml",
147  "Ý-Yacute",
148  "Þ-THORN",
149  "ß-szlig",
150  "à-agrave",
151  "á-aacute",
152  "â-acirc",
153  "ã-atilde",
154  "ä-auml",
155  "å-aring",
156  "æ-aelig",
157  "ç-ccedil",
158  "è-egrave",
159  "é-eacute",
160  "ê-ecirc",
161  "ë-euml",
162  "ì-igrave",
163  "í-iacute",
164  "î-icirc",
165  "ï-iuml",
166  "ð-eth",
167  "ñ-ntilde",
168  "ò-ograve",
169  "ó-oacute",
170  "ô-ocirc",
171  "õ-otilde",
172  "ö-ouml",
173  "÷-divide",
174  "ø-oslash",
175  "ù-ugrave",
176  "ú-uacute",
177  "û-ucirc",
178  "ü-uuml",
179  "ý-yacute",
180  "þ-thorn",
181  "ÿ-yuml",
182  "Œ-OElig",
183  "œ-oelig",
184  "Š-Scaron",
185  "š-scaron",
186  "Ÿ-Yuml",
187  "ƒ-fnof",
188  "\u02c6-circ",
189  "\u02dc-tilde",
190  "Α-Alpha",
191  "Β-Beta",
192  "Γ-Gamma",
193  "Δ-Delta",
194  "Ε-Epsilon",
195  "Ζ-Zeta",
196  "Η-Eta",
197  "Θ-Theta",
198  "Ι-Iota",
199  "Κ-Kappa",
200  "Λ-Lambda",
201  "Μ-Mu",
202  "Ν-Nu",
203  "Ξ-Xi",
204  "Ο-Omicron",
205  "Π-Pi",
206  "Ρ-Rho",
207  "Σ-Sigma",
208  "Τ-Tau",
209  "Υ-Upsilon",
210  "Φ-Phi",
211  "Χ-Chi",
212  "Ψ-Psi",
213  "Ω-Omega",
214  "α-alpha",
215  "β-beta",
216  "γ-gamma",
217  "δ-delta",
218  "ε-epsilon",
219  "ζ-zeta",
220  "η-eta",
221  "θ-theta",
222  "ι-iota",
223  "κ-kappa",
224  "λ-lambda",
225  "μ-mu",
226  "ν-nu",
227  "ξ-xi",
228  "ο-omicron",
229  "π-pi",
230  "ρ-rho",
231  "ς-sigmaf",
232  "σ-sigma",
233  "τ-tau",
234  "υ-upsilon",
235  "φ-phi",
236  "χ-chi",
237  "ψ-psi",
238  "ω-omega",
239  "ϑ-thetasym",
240  "ϒ-upsih",
241  "ϖ-piv",
242  "\u2002-ensp",
243  "\u2003-emsp",
244  "\u2009-thinsp",
245  "\u200c-zwnj",
246  "\u200d-zwj",
247  "\u200e-lrm",
248  "\u200f-rlm",
249  "–-ndash",
250  "—-mdash",
251  "‘-lsquo",
252  "’-rsquo",
253  "‚-sbquo",
254  "“-ldquo",
255  "”-rdquo",
256  "„-bdquo",
257  "†-dagger",
258  "‡-Dagger",
259  "•-bull",
260  "…-hellip",
261  "‰-permil",
262  "′-prime",
263  "″-Prime",
264  "‹-lsaquo",
265  "›-rsaquo",
266  "‾-oline",
267  "⁄-frasl",
268  "€-euro",
269  "ℑ-image",
270  "℘-weierp",
271  "ℜ-real",
272  "™-trade",
273  "ℵ-alefsym",
274  "←-larr",
275  "↑-uarr",
276  "→-rarr",
277  "↓-darr",
278  "↔-harr",
279  "↵-crarr",
280  "⇐-lArr",
281  "⇑-uArr",
282  "⇒-rArr",
283  "⇓-dArr",
284  "⇔-hArr",
285  "∀-forall",
286  "∂-part",
287  "∃-exist",
288  "∅-empty",
289  "∇-nabla",
290  "∈-isin",
291  "∉-notin",
292  "∋-ni",
293  "∏-prod",
294  "∑-sum",
295  "−-minus",
296  "∗-lowast",
297  "√-radic",
298  "∝-prop",
299  "∞-infin",
300  "∠-ang",
301  "∧-and",
302  "∨-or",
303  "∩-cap",
304  "∪-cup",
305  "∫-int",
306  "∴-there4",
307  "∼-sim",
308  "≅-cong",
309  "≈-asymp",
310  "≠-ne",
311  "≡-equiv",
312  "≤-le",
313  "≥-ge",
314  "⊂-sub",
315  "⊃-sup",
316  "⊄-nsub",
317  "⊆-sube",
318  "⊇-supe",
319  "⊕-oplus",
320  "⊗-otimes",
321  "⊥-perp",
322  "⋅-sdot",
323  "⌈-lceil",
324  "⌉-rceil",
325  "⌊-lfloor",
326  "⌋-rfloor",
327  "〈-lang",
328  "〉-rang",
329  "◊-loz",
330  "♠-spades",
331  "♣-clubs",
332  "♥-hearts",
333  "♦-diams"
334  };
335 
336  private static Dictionary<string, char> _lookupTable = GenerateLookupTable();
337 
338  private static Dictionary<string, char> GenerateLookupTable()
339  {
341  string[] entitiesList = _entitiesList;
342  foreach (string text in entitiesList)
343  {
344  dictionary.Add(text.Substring(2), text[0]);
345  }
346  return dictionary;
347  }
348 
349  public static char Lookup(string entity)
350  {
351  _lookupTable.TryGetValue(entity, out char value);
352  return value;
353  }
354  }
355 
356  private const char HIGH_SURROGATE_START = '\ud800';
357 
358  private const char LOW_SURROGATE_START = '\udc00';
359 
360  private const char LOW_SURROGATE_END = '\udfff';
361 
362  private const int UNICODE_PLANE00_END = 65535;
363 
364  private const int UNICODE_PLANE01_START = 65536;
365 
366  private const int UNICODE_PLANE16_END = 1114111;
367 
368  private const int UnicodeReplacementChar = 65533;
369 
370  private static readonly char[] _htmlEntityEndingChars = new char[2]
371  {
372  ';',
373  '&'
374  };
375 
376  private static volatile UnicodeDecodingConformance _htmlDecodeConformance = UnicodeDecodingConformance.Auto;
377 
378  private static volatile UnicodeEncodingConformance _htmlEncodeConformance = UnicodeEncodingConformance.Auto;
379 
380  private static UnicodeDecodingConformance HtmlDecodeConformance
381  {
382  get
383  {
384  if (_htmlDecodeConformance != 0)
385  {
386  return _htmlDecodeConformance;
387  }
388  UnicodeDecodingConformance unicodeDecodingConformance = BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? UnicodeDecodingConformance.Strict : UnicodeDecodingConformance.Compat;
389  UnicodeDecodingConformance unicodeDecodingConformance2 = unicodeDecodingConformance;
390  try
391  {
392  unicodeDecodingConformance2 = SettingsSectionInternal.Section.WebUtilityUnicodeDecodingConformance;
393  if (unicodeDecodingConformance2 <= UnicodeDecodingConformance.Auto || unicodeDecodingConformance2 > UnicodeDecodingConformance.Loose)
394  {
395  unicodeDecodingConformance2 = unicodeDecodingConformance;
396  }
397  }
398  catch (ConfigurationException)
399  {
400  unicodeDecodingConformance2 = unicodeDecodingConformance;
401  }
402  catch
403  {
404  return unicodeDecodingConformance;
405  }
406  _htmlDecodeConformance = unicodeDecodingConformance2;
407  return _htmlDecodeConformance;
408  }
409  }
410 
411  private static UnicodeEncodingConformance HtmlEncodeConformance
412  {
413  get
414  {
415  if (_htmlEncodeConformance != 0)
416  {
417  return _htmlEncodeConformance;
418  }
419  UnicodeEncodingConformance unicodeEncodingConformance = BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? UnicodeEncodingConformance.Strict : UnicodeEncodingConformance.Compat;
420  UnicodeEncodingConformance unicodeEncodingConformance2 = unicodeEncodingConformance;
421  try
422  {
423  unicodeEncodingConformance2 = SettingsSectionInternal.Section.WebUtilityUnicodeEncodingConformance;
424  if (unicodeEncodingConformance2 <= UnicodeEncodingConformance.Auto || unicodeEncodingConformance2 > UnicodeEncodingConformance.Compat)
425  {
426  unicodeEncodingConformance2 = unicodeEncodingConformance;
427  }
428  }
429  catch (ConfigurationException)
430  {
431  unicodeEncodingConformance2 = unicodeEncodingConformance;
432  }
433  catch
434  {
435  return unicodeEncodingConformance;
436  }
437  _htmlEncodeConformance = unicodeEncodingConformance2;
438  return _htmlEncodeConformance;
439  }
440  }
441 
445  [global::__DynamicallyInvokable]
446  public static string HtmlEncode(string value)
447  {
448  if (string.IsNullOrEmpty(value))
449  {
450  return value;
451  }
452  int num = IndexOfHtmlEncodingChars(value, 0);
453  if (num == -1)
454  {
455  return value;
456  }
458  HtmlEncode(value, stringWriter);
459  return stringWriter.ToString();
460  }
461 
466  public unsafe static void HtmlEncode(string value, TextWriter output)
467  {
468  if (value != null)
469  {
470  if (output == null)
471  {
472  throw new ArgumentNullException("output");
473  }
474  int num = IndexOfHtmlEncodingChars(value, 0);
475  if (num == -1)
476  {
477  output.Write(value);
478  return;
479  }
480  UnicodeEncodingConformance htmlEncodeConformance = HtmlEncodeConformance;
481  int charsRemaining = value.Length - num;
482  fixed (char* ptr = value)
483  {
484  char* pch = ptr;
485  while (num-- > 0)
486  {
487  char* intPtr = pch;
488  pch = intPtr + 1;
489  output.Write(*intPtr);
490  }
491  while (charsRemaining > 0)
492  {
493  char c = *pch;
494  if (c <= '>')
495  {
496  switch (c)
497  {
498  case '<':
499  output.Write("&lt;");
500  break;
501  case '>':
502  output.Write("&gt;");
503  break;
504  case '"':
505  output.Write("&quot;");
506  break;
507  case '\'':
508  output.Write("&#39;");
509  break;
510  case '&':
511  output.Write("&amp;");
512  break;
513  default:
514  output.Write(c);
515  break;
516  }
517  }
518  else
519  {
520  int num3 = -1;
521  if (c >= '\u00a0' && c < 'Ā')
522  {
523  num3 = c;
524  }
525  else if (htmlEncodeConformance == UnicodeEncodingConformance.Strict && char.IsSurrogate(c))
526  {
527  int nextUnicodeScalarValueFromUtf16Surrogate = GetNextUnicodeScalarValueFromUtf16Surrogate(ref pch, ref charsRemaining);
528  if (nextUnicodeScalarValueFromUtf16Surrogate >= 65536)
529  {
530  num3 = nextUnicodeScalarValueFromUtf16Surrogate;
531  }
532  else
533  {
534  c = (char)nextUnicodeScalarValueFromUtf16Surrogate;
535  }
536  }
537  if (num3 >= 0)
538  {
539  output.Write("&#");
540  output.Write(num3.ToString(NumberFormatInfo.InvariantInfo));
541  output.Write(';');
542  }
543  else
544  {
545  output.Write(c);
546  }
547  }
548  charsRemaining--;
549  pch++;
550  }
551  }
552  }
553  }
554 
558  [global::__DynamicallyInvokable]
559  public static string HtmlDecode(string value)
560  {
561  if (string.IsNullOrEmpty(value))
562  {
563  return value;
564  }
565  if (!StringRequiresHtmlDecoding(value))
566  {
567  return value;
568  }
569  StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
570  HtmlDecode(value, stringWriter);
571  return stringWriter.ToString();
572  }
573 
578  public static void HtmlDecode(string value, TextWriter output)
579  {
580  if (value == null)
581  {
582  return;
583  }
584  if (output == null)
585  {
586  throw new ArgumentNullException("output");
587  }
588  if (!StringRequiresHtmlDecoding(value))
589  {
590  output.Write(value);
591  return;
592  }
593  UnicodeDecodingConformance htmlDecodeConformance = HtmlDecodeConformance;
594  int length = value.Length;
595  for (int i = 0; i < length; i++)
596  {
597  char c = value[i];
598  if (c == '&')
599  {
600  int num = value.IndexOfAny(_htmlEntityEndingChars, i + 1);
601  if (num > 0 && value[num] == ';')
602  {
603  string text = value.Substring(i + 1, num - i - 1);
604  if (text.Length > 1 && text[0] == '#')
605  {
606  uint result;
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);
608  if (flag)
609  {
610  switch (htmlDecodeConformance)
611  {
612  case UnicodeDecodingConformance.Strict:
613  flag = (result < 55296 || (57343 < result && result <= 1114111));
614  break;
615  case UnicodeDecodingConformance.Compat:
616  flag = (0 < result && result <= 65535);
617  break;
618  case UnicodeDecodingConformance.Loose:
619  flag = (result <= 1114111);
620  break;
621  default:
622  flag = false;
623  break;
624  }
625  }
626  if (flag)
627  {
628  if (result <= 65535)
629  {
630  output.Write((char)result);
631  }
632  else
633  {
634  ConvertSmpToUtf16(result, out char leadingSurrogate, out char trailingSurrogate);
635  output.Write(leadingSurrogate);
636  output.Write(trailingSurrogate);
637  }
638  i = num;
639  continue;
640  }
641  }
642  else
643  {
644  i = num;
645  char c2 = HtmlEntities.Lookup(text);
646  if (c2 == '\0')
647  {
648  output.Write('&');
649  output.Write(text);
650  output.Write(';');
651  continue;
652  }
653  c = c2;
654  }
655  }
656  }
657  output.Write(c);
658  }
659  }
660 
661  private unsafe static int IndexOfHtmlEncodingChars(string s, int startPos)
662  {
663  UnicodeEncodingConformance htmlEncodeConformance = HtmlEncodeConformance;
664  int num = s.Length - startPos;
665  fixed (char* ptr = s)
666  {
667  char* ptr2 = ptr + startPos;
668  while (num > 0)
669  {
670  char c = *ptr2;
671  if (c <= '>')
672  {
673  switch (c)
674  {
675  case '"':
676  case '&':
677  case '\'':
678  case '<':
679  case '>':
680  return s.Length - num;
681  }
682  }
683  else
684  {
685  if (c >= '\u00a0' && c < 'Ā')
686  {
687  return s.Length - num;
688  }
689  if (htmlEncodeConformance == UnicodeEncodingConformance.Strict && char.IsSurrogate(c))
690  {
691  return s.Length - num;
692  }
693  }
694  ptr2++;
695  num--;
696  }
697  }
698  return -1;
699  }
700 
701  private static byte[] UrlEncode(byte[] bytes, int offset, int count, bool alwaysCreateNewReturnValue)
702  {
703  byte[] array = UrlEncode(bytes, offset, count);
704  if (!alwaysCreateNewReturnValue || array == null || array != bytes)
705  {
706  return array;
707  }
708  return (byte[])array.Clone();
709  }
710 
711  private static byte[] UrlEncode(byte[] bytes, int offset, int count)
712  {
713  if (!ValidateUrlEncodingParameters(bytes, offset, count))
714  {
715  return null;
716  }
717  int num = 0;
718  int num2 = 0;
719  for (int i = 0; i < count; i++)
720  {
721  char c = (char)bytes[offset + i];
722  if (c == ' ')
723  {
724  num++;
725  }
726  else if (!IsUrlSafeChar(c))
727  {
728  num2++;
729  }
730  }
731  if (num == 0 && num2 == 0)
732  {
733  if (offset == 0 && bytes.Length == count)
734  {
735  return bytes;
736  }
737  byte[] array = new byte[count];
738  Buffer.BlockCopy(bytes, offset, array, 0, count);
739  return array;
740  }
741  byte[] array2 = new byte[count + num2 * 2];
742  int num3 = 0;
743  for (int j = 0; j < count; j++)
744  {
745  byte b = bytes[offset + j];
746  char c2 = (char)b;
747  if (IsUrlSafeChar(c2))
748  {
749  array2[num3++] = b;
750  continue;
751  }
752  if (c2 == ' ')
753  {
754  array2[num3++] = 43;
755  continue;
756  }
757  array2[num3++] = 37;
758  array2[num3++] = (byte)IntToHex((b >> 4) & 0xF);
759  array2[num3++] = (byte)IntToHex(b & 0xF);
760  }
761  return array2;
762  }
763 
767  [global::__DynamicallyInvokable]
768  public static string UrlEncode(string value)
769  {
770  if (value == null)
771  {
772  return null;
773  }
774  byte[] bytes = Encoding.UTF8.GetBytes(value);
775  return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length, alwaysCreateNewReturnValue: false));
776  }
777 
783  [global::__DynamicallyInvokable]
784  public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count)
785  {
786  return UrlEncode(value, offset, count, alwaysCreateNewReturnValue: true);
787  }
788 
789  private static string UrlDecodeInternal(string value, Encoding encoding)
790  {
791  if (value == null)
792  {
793  return null;
794  }
795  int length = value.Length;
796  UrlDecoder urlDecoder = new UrlDecoder(length, encoding);
797  for (int i = 0; i < length; i++)
798  {
799  char c = value[i];
800  switch (c)
801  {
802  case '+':
803  c = ' ';
804  break;
805  case '%':
806  if (i < length - 2)
807  {
808  int num = HexToInt(value[i + 1]);
809  int num2 = HexToInt(value[i + 2]);
810  if (num >= 0 && num2 >= 0)
811  {
812  byte b = (byte)((num << 4) | num2);
813  i += 2;
814  urlDecoder.AddByte(b);
815  continue;
816  }
817  }
818  break;
819  }
820  if ((c & 0xFF80) == 0)
821  {
822  urlDecoder.AddByte((byte)c);
823  }
824  else
825  {
826  urlDecoder.AddChar(c);
827  }
828  }
829  return urlDecoder.GetString();
830  }
831 
832  private static byte[] UrlDecodeInternal(byte[] bytes, int offset, int count)
833  {
834  if (!ValidateUrlEncodingParameters(bytes, offset, count))
835  {
836  return null;
837  }
838  int num = 0;
839  byte[] array = new byte[count];
840  for (int i = 0; i < count; i++)
841  {
842  int num2 = offset + i;
843  byte b = bytes[num2];
844  switch (b)
845  {
846  case 43:
847  b = 32;
848  break;
849  case 37:
850  if (i < count - 2)
851  {
852  int num3 = HexToInt((char)bytes[num2 + 1]);
853  int num4 = HexToInt((char)bytes[num2 + 2]);
854  if (num3 >= 0 && num4 >= 0)
855  {
856  b = (byte)((num3 << 4) | num4);
857  i += 2;
858  }
859  }
860  break;
861  }
862  array[num++] = b;
863  }
864  if (num < array.Length)
865  {
866  byte[] array2 = new byte[num];
867  Array.Copy(array, array2, num);
868  array = array2;
869  }
870  return array;
871  }
872 
876  [global::__DynamicallyInvokable]
877  public static string UrlDecode(string encodedValue)
878  {
879  if (encodedValue == null)
880  {
881  return null;
882  }
883  return UrlDecodeInternal(encodedValue, Encoding.UTF8);
884  }
885 
891  [global::__DynamicallyInvokable]
892  public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count)
893  {
894  return UrlDecodeInternal(encodedValue, offset, count);
895  }
896 
897  private static void ConvertSmpToUtf16(uint smpChar, out char leadingSurrogate, out char trailingSurrogate)
898  {
899  int num = (int)(smpChar - 65536);
900  leadingSurrogate = (char)(num / 1024 + 55296);
901  trailingSurrogate = (char)(num % 1024 + 56320);
902  }
903 
904  private unsafe static int GetNextUnicodeScalarValueFromUtf16Surrogate(ref char* pch, ref int charsRemaining)
905  {
906  if (charsRemaining <= 1)
907  {
908  return 65533;
909  }
910  char c = *pch;
911  char c2 = pch[1];
912  if (char.IsSurrogatePair(c, c2))
913  {
914  pch++;
915  charsRemaining--;
916  return (c - 55296) * 1024 + (c2 - 56320) + 65536;
917  }
918  return 65533;
919  }
920 
921  private static int HexToInt(char h)
922  {
923  if (h < '0' || h > '9')
924  {
925  if (h < 'a' || h > 'f')
926  {
927  if (h < 'A' || h > 'F')
928  {
929  return -1;
930  }
931  return h - 65 + 10;
932  }
933  return h - 97 + 10;
934  }
935  return h - 48;
936  }
937 
938  private static char IntToHex(int n)
939  {
940  if (n <= 9)
941  {
942  return (char)(n + 48);
943  }
944  return (char)(n - 10 + 65);
945  }
946 
947  private static bool IsUrlSafeChar(char ch)
948  {
949  if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))
950  {
951  return true;
952  }
953  switch (ch)
954  {
955  case '!':
956  case '(':
957  case ')':
958  case '*':
959  case '-':
960  case '.':
961  case '_':
962  return true;
963  default:
964  return false;
965  }
966  }
967 
968  private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
969  {
970  if (bytes == null && count == 0)
971  {
972  return false;
973  }
974  if (bytes == null)
975  {
976  throw new ArgumentNullException("bytes");
977  }
978  if (offset < 0 || offset > bytes.Length)
979  {
980  throw new ArgumentOutOfRangeException("offset");
981  }
982  if (count < 0 || offset + count > bytes.Length)
983  {
984  throw new ArgumentOutOfRangeException("count");
985  }
986  return true;
987  }
988 
989  private static bool StringRequiresHtmlDecoding(string s)
990  {
991  if (HtmlDecodeConformance == UnicodeDecodingConformance.Compat)
992  {
993  return s.IndexOf('&') >= 0;
994  }
995  foreach (char c in s)
996  {
997  if (c == '&' || char.IsSurrogate(c))
998  {
999  return true;
1000  }
1001  }
1002  return false;
1003  }
1004  }
1005 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
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.
Definition: WebUtility.cs:13
unsafe string GetString(byte *bytes, int byteCount)
When overridden in a derived class, decodes a specified number of bytes starting at a specified addre...
Definition: Encoding.cs:1918
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...
Definition: Encoding.cs:1803
virtual byte [] GetBytes(char[] chars)
When overridden in a derived class, encodes all the characters in the specified character array into ...
Definition: Encoding.cs:1576
Implements a T:System.IO.TextWriter for writing information to a string. The information is stored in...
Definition: StringWriter.cs:13
Definition: __Canon.cs:3
static unsafe void HtmlEncode(string value, TextWriter output)
Converts a string into an HTML-encoded string, and returns the output as a T:System....
Definition: WebUtility.cs:466
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.
Definition: WebUtility.cs:784
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
static string UrlEncode(string value)
Converts a text string into a URL-encoded string.
Definition: WebUtility.cs:768
static string UrlDecode(string encodedValue)
Converts a string that has been encoded for transmission in a URL into a decoded string.
Definition: WebUtility.cs:877
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.
Definition: WebUtility.cs:446
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...
Definition: WebUtility.cs:892
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Definition: Encoding.cs:1023
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
virtual void Write(char value)
Writes a character to the text string or stream.
Definition: TextWriter.cs:543
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Definition: Dictionary.cs:1624
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...