2005-09-19 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
1 // 
2 // System.Web.HttpUtility
3 //
4 // Authors:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Wictor WilĂ©n (decode/encode functions) (wictor@ibizkit.se)
7 //   Tim Coleman (tim@timcoleman.com)
8 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Globalization;
35 using System.IO;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.Web.Util;
39
40 namespace System.Web {
41
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class HttpUtility {
45
46                 #region Fields
47         
48                 static Hashtable entities;
49                 static object lock_ = new object ();
50         
51                 #endregion // Fields
52         
53                 static Hashtable Entities {
54                         get {
55                                 lock (lock_) {
56                                         if (entities == null)
57                                                 InitEntities ();
58
59                                         return entities;
60                                 }
61                         }
62                 }
63                 
64                 #region Constructors
65
66                 static void InitEntities ()
67                 {
68                         // Build the hash table of HTML entity references.  This list comes
69                         // from the HTML 4.01 W3C recommendation.
70                         entities = new Hashtable ();
71                         entities.Add ("nbsp", '\u00A0');
72                         entities.Add ("iexcl", '\u00A1');
73                         entities.Add ("cent", '\u00A2');
74                         entities.Add ("pound", '\u00A3');
75                         entities.Add ("curren", '\u00A4');
76                         entities.Add ("yen", '\u00A5');
77                         entities.Add ("brvbar", '\u00A6');
78                         entities.Add ("sect", '\u00A7');
79                         entities.Add ("uml", '\u00A8');
80                         entities.Add ("copy", '\u00A9');
81                         entities.Add ("ordf", '\u00AA');
82                         entities.Add ("laquo", '\u00AB');
83                         entities.Add ("not", '\u00AC');
84                         entities.Add ("shy", '\u00AD');
85                         entities.Add ("reg", '\u00AE');
86                         entities.Add ("macr", '\u00AF');
87                         entities.Add ("deg", '\u00B0');
88                         entities.Add ("plusmn", '\u00B1');
89                         entities.Add ("sup2", '\u00B2');
90                         entities.Add ("sup3", '\u00B3');
91                         entities.Add ("acute", '\u00B4');
92                         entities.Add ("micro", '\u00B5');
93                         entities.Add ("para", '\u00B6');
94                         entities.Add ("middot", '\u00B7');
95                         entities.Add ("cedil", '\u00B8');
96                         entities.Add ("sup1", '\u00B9');
97                         entities.Add ("ordm", '\u00BA');
98                         entities.Add ("raquo", '\u00BB');
99                         entities.Add ("frac14", '\u00BC');
100                         entities.Add ("frac12", '\u00BD');
101                         entities.Add ("frac34", '\u00BE');
102                         entities.Add ("iquest", '\u00BF');
103                         entities.Add ("Agrave", '\u00C0');
104                         entities.Add ("Aacute", '\u00C1');
105                         entities.Add ("Acirc", '\u00C2');
106                         entities.Add ("Atilde", '\u00C3');
107                         entities.Add ("Auml", '\u00C4');
108                         entities.Add ("Aring", '\u00C5');
109                         entities.Add ("AElig", '\u00C6');
110                         entities.Add ("Ccedil", '\u00C7');
111                         entities.Add ("Egrave", '\u00C8');
112                         entities.Add ("Eacute", '\u00C9');
113                         entities.Add ("Ecirc", '\u00CA');
114                         entities.Add ("Euml", '\u00CB');
115                         entities.Add ("Igrave", '\u00CC');
116                         entities.Add ("Iacute", '\u00CD');
117                         entities.Add ("Icirc", '\u00CE');
118                         entities.Add ("Iuml", '\u00CF');
119                         entities.Add ("ETH", '\u00D0');
120                         entities.Add ("Ntilde", '\u00D1');
121                         entities.Add ("Ograve", '\u00D2');
122                         entities.Add ("Oacute", '\u00D3');
123                         entities.Add ("Ocirc", '\u00D4');
124                         entities.Add ("Otilde", '\u00D5');
125                         entities.Add ("Ouml", '\u00D6');
126                         entities.Add ("times", '\u00D7');
127                         entities.Add ("Oslash", '\u00D8');
128                         entities.Add ("Ugrave", '\u00D9');
129                         entities.Add ("Uacute", '\u00DA');
130                         entities.Add ("Ucirc", '\u00DB');
131                         entities.Add ("Uuml", '\u00DC');
132                         entities.Add ("Yacute", '\u00DD');
133                         entities.Add ("THORN", '\u00DE');
134                         entities.Add ("szlig", '\u00DF');
135                         entities.Add ("agrave", '\u00E0');
136                         entities.Add ("aacute", '\u00E1');
137                         entities.Add ("acirc", '\u00E2');
138                         entities.Add ("atilde", '\u00E3');
139                         entities.Add ("auml", '\u00E4');
140                         entities.Add ("aring", '\u00E5');
141                         entities.Add ("aelig", '\u00E6');
142                         entities.Add ("ccedil", '\u00E7');
143                         entities.Add ("egrave", '\u00E8');
144                         entities.Add ("eacute", '\u00E9');
145                         entities.Add ("ecirc", '\u00EA');
146                         entities.Add ("euml", '\u00EB');
147                         entities.Add ("igrave", '\u00EC');
148                         entities.Add ("iacute", '\u00ED');
149                         entities.Add ("icirc", '\u00EE');
150                         entities.Add ("iuml", '\u00EF');
151                         entities.Add ("eth", '\u00F0');
152                         entities.Add ("ntilde", '\u00F1');
153                         entities.Add ("ograve", '\u00F2');
154                         entities.Add ("oacute", '\u00F3');
155                         entities.Add ("ocirc", '\u00F4');
156                         entities.Add ("otilde", '\u00F5');
157                         entities.Add ("ouml", '\u00F6');
158                         entities.Add ("divide", '\u00F7');
159                         entities.Add ("oslash", '\u00F8');
160                         entities.Add ("ugrave", '\u00F9');
161                         entities.Add ("uacute", '\u00FA');
162                         entities.Add ("ucirc", '\u00FB');
163                         entities.Add ("uuml", '\u00FC');
164                         entities.Add ("yacute", '\u00FD');
165                         entities.Add ("thorn", '\u00FE');
166                         entities.Add ("yuml", '\u00FF');
167                         entities.Add ("fnof", '\u0192');
168                         entities.Add ("Alpha", '\u0391');
169                         entities.Add ("Beta", '\u0392');
170                         entities.Add ("Gamma", '\u0393');
171                         entities.Add ("Delta", '\u0394');
172                         entities.Add ("Epsilon", '\u0395');
173                         entities.Add ("Zeta", '\u0396');
174                         entities.Add ("Eta", '\u0397');
175                         entities.Add ("Theta", '\u0398');
176                         entities.Add ("Iota", '\u0399');
177                         entities.Add ("Kappa", '\u039A');
178                         entities.Add ("Lambda", '\u039B');
179                         entities.Add ("Mu", '\u039C');
180                         entities.Add ("Nu", '\u039D');
181                         entities.Add ("Xi", '\u039E');
182                         entities.Add ("Omicron", '\u039F');
183                         entities.Add ("Pi", '\u03A0');
184                         entities.Add ("Rho", '\u03A1');
185                         entities.Add ("Sigma", '\u03A3');
186                         entities.Add ("Tau", '\u03A4');
187                         entities.Add ("Upsilon", '\u03A5');
188                         entities.Add ("Phi", '\u03A6');
189                         entities.Add ("Chi", '\u03A7');
190                         entities.Add ("Psi", '\u03A8');
191                         entities.Add ("Omega", '\u03A9');
192                         entities.Add ("alpha", '\u03B1');
193                         entities.Add ("beta", '\u03B2');
194                         entities.Add ("gamma", '\u03B3');
195                         entities.Add ("delta", '\u03B4');
196                         entities.Add ("epsilon", '\u03B5');
197                         entities.Add ("zeta", '\u03B6');
198                         entities.Add ("eta", '\u03B7');
199                         entities.Add ("theta", '\u03B8');
200                         entities.Add ("iota", '\u03B9');
201                         entities.Add ("kappa", '\u03BA');
202                         entities.Add ("lambda", '\u03BB');
203                         entities.Add ("mu", '\u03BC');
204                         entities.Add ("nu", '\u03BD');
205                         entities.Add ("xi", '\u03BE');
206                         entities.Add ("omicron", '\u03BF');
207                         entities.Add ("pi", '\u03C0');
208                         entities.Add ("rho", '\u03C1');
209                         entities.Add ("sigmaf", '\u03C2');
210                         entities.Add ("sigma", '\u03C3');
211                         entities.Add ("tau", '\u03C4');
212                         entities.Add ("upsilon", '\u03C5');
213                         entities.Add ("phi", '\u03C6');
214                         entities.Add ("chi", '\u03C7');
215                         entities.Add ("psi", '\u03C8');
216                         entities.Add ("omega", '\u03C9');
217                         entities.Add ("thetasym", '\u03D1');
218                         entities.Add ("upsih", '\u03D2');
219                         entities.Add ("piv", '\u03D6');
220                         entities.Add ("bull", '\u2022');
221                         entities.Add ("hellip", '\u2026');
222                         entities.Add ("prime", '\u2032');
223                         entities.Add ("Prime", '\u2033');
224                         entities.Add ("oline", '\u203E');
225                         entities.Add ("frasl", '\u2044');
226                         entities.Add ("weierp", '\u2118');
227                         entities.Add ("image", '\u2111');
228                         entities.Add ("real", '\u211C');
229                         entities.Add ("trade", '\u2122');
230                         entities.Add ("alefsym", '\u2135');
231                         entities.Add ("larr", '\u2190');
232                         entities.Add ("uarr", '\u2191');
233                         entities.Add ("rarr", '\u2192');
234                         entities.Add ("darr", '\u2193');
235                         entities.Add ("harr", '\u2194');
236                         entities.Add ("crarr", '\u21B5');
237                         entities.Add ("lArr", '\u21D0');
238                         entities.Add ("uArr", '\u21D1');
239                         entities.Add ("rArr", '\u21D2');
240                         entities.Add ("dArr", '\u21D3');
241                         entities.Add ("hArr", '\u21D4');
242                         entities.Add ("forall", '\u2200');
243                         entities.Add ("part", '\u2202');
244                         entities.Add ("exist", '\u2203');
245                         entities.Add ("empty", '\u2205');
246                         entities.Add ("nabla", '\u2207');
247                         entities.Add ("isin", '\u2208');
248                         entities.Add ("notin", '\u2209');
249                         entities.Add ("ni", '\u220B');
250                         entities.Add ("prod", '\u220F');
251                         entities.Add ("sum", '\u2211');
252                         entities.Add ("minus", '\u2212');
253                         entities.Add ("lowast", '\u2217');
254                         entities.Add ("radic", '\u221A');
255                         entities.Add ("prop", '\u221D');
256                         entities.Add ("infin", '\u221E');
257                         entities.Add ("ang", '\u2220');
258                         entities.Add ("and", '\u2227');
259                         entities.Add ("or", '\u2228');
260                         entities.Add ("cap", '\u2229');
261                         entities.Add ("cup", '\u222A');
262                         entities.Add ("int", '\u222B');
263                         entities.Add ("there4", '\u2234');
264                         entities.Add ("sim", '\u223C');
265                         entities.Add ("cong", '\u2245');
266                         entities.Add ("asymp", '\u2248');
267                         entities.Add ("ne", '\u2260');
268                         entities.Add ("equiv", '\u2261');
269                         entities.Add ("le", '\u2264');
270                         entities.Add ("ge", '\u2265');
271                         entities.Add ("sub", '\u2282');
272                         entities.Add ("sup", '\u2283');
273                         entities.Add ("nsub", '\u2284');
274                         entities.Add ("sube", '\u2286');
275                         entities.Add ("supe", '\u2287');
276                         entities.Add ("oplus", '\u2295');
277                         entities.Add ("otimes", '\u2297');
278                         entities.Add ("perp", '\u22A5');
279                         entities.Add ("sdot", '\u22C5');
280                         entities.Add ("lceil", '\u2308');
281                         entities.Add ("rceil", '\u2309');
282                         entities.Add ("lfloor", '\u230A');
283                         entities.Add ("rfloor", '\u230B');
284                         entities.Add ("lang", '\u2329');
285                         entities.Add ("rang", '\u232A');
286                         entities.Add ("loz", '\u25CA');
287                         entities.Add ("spades", '\u2660');
288                         entities.Add ("clubs", '\u2663');
289                         entities.Add ("hearts", '\u2665');
290                         entities.Add ("diams", '\u2666');
291                         entities.Add ("quot", '\u0022');
292                         entities.Add ("amp", '\u0026');
293                         entities.Add ("lt", '\u003C');
294                         entities.Add ("gt", '\u003E');
295                         entities.Add ("OElig", '\u0152');
296                         entities.Add ("oelig", '\u0153');
297                         entities.Add ("Scaron", '\u0160');
298                         entities.Add ("scaron", '\u0161');
299                         entities.Add ("Yuml", '\u0178');
300                         entities.Add ("circ", '\u02C6');
301                         entities.Add ("tilde", '\u02DC');
302                         entities.Add ("ensp", '\u2002');
303                         entities.Add ("emsp", '\u2003');
304                         entities.Add ("thinsp", '\u2009');
305                         entities.Add ("zwnj", '\u200C');
306                         entities.Add ("zwj", '\u200D');
307                         entities.Add ("lrm", '\u200E');
308                         entities.Add ("rlm", '\u200F');
309                         entities.Add ("ndash", '\u2013');
310                         entities.Add ("mdash", '\u2014');
311                         entities.Add ("lsquo", '\u2018');
312                         entities.Add ("rsquo", '\u2019');
313                         entities.Add ("sbquo", '\u201A');
314                         entities.Add ("ldquo", '\u201C');
315                         entities.Add ("rdquo", '\u201D');
316                         entities.Add ("bdquo", '\u201E');
317                         entities.Add ("dagger", '\u2020');
318                         entities.Add ("Dagger", '\u2021');
319                         entities.Add ("permil", '\u2030');
320                         entities.Add ("lsaquo", '\u2039');
321                         entities.Add ("rsaquo", '\u203A');
322                         entities.Add ("euro", '\u20AC');
323                 }
324
325 #if NET_2_0
326                 [Obsolete]
327 #endif
328                 public HttpUtility () 
329                 {
330                 }
331         
332                 #endregion // Constructors
333         
334                 #region Methods
335         
336                 public static void HtmlAttributeEncode (string s, TextWriter output) 
337                 {
338                         output.Write(HtmlAttributeEncode(s));
339                 }
340         
341                 public static string HtmlAttributeEncode (string s) 
342                 {
343                         if (null == s) 
344                                 return null;
345         
346                         if (s.IndexOf ('&') == -1 && s.IndexOf ('"') == -1)
347                                 return s;
348
349                         StringBuilder output = new StringBuilder ();
350                         foreach (char c in s) 
351                                 switch (c) {
352                                 case '&' : 
353                                         output.Append ("&amp;");
354                                         break;
355                                 case '"' :
356                                         output.Append ("&quot;");
357                                         break;
358                                 default:
359                                         output.Append (c);
360                                         break;
361                                 }
362         
363                         return output.ToString();
364                 }
365         
366                 public static string UrlDecode (string str) 
367                 {
368                         return UrlDecode(str, Encoding.UTF8);
369                 }
370         
371                 private static char [] GetChars (MemoryStream b, Encoding e)
372                 {
373                         return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
374                 }
375                 
376                 public static string UrlDecode (string s, Encoding e)
377                 {
378                         if (null == s) 
379                                 return null;
380
381                         if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
382                                 return s;
383
384                         if (e == null)
385                                 e = Encoding.UTF8;
386         
387                         StringBuilder output = new StringBuilder ();
388                         long len = s.Length;
389                         NumberStyles hexa = NumberStyles.HexNumber;
390                         MemoryStream bytes = new MemoryStream ();
391         
392                         for (int i = 0; i < len; i++) {
393                                 if (s [i] == '%' && i + 2 < len) {
394                                         if (s [i + 1] == 'u' && i + 5 < len) {
395                                                 if (bytes.Length > 0) {
396                                                         output.Append (GetChars (bytes, e));
397                                                         bytes.SetLength (0);
398                                                 }
399                                                 output.Append ((char) Int32.Parse (s.Substring (i + 2, 4), hexa));
400                                                 i += 5;
401                                         } else {
402                                                 bytes.WriteByte ((byte) Int32.Parse (s.Substring (i + 1, 2), hexa));
403                                                 i += 2;
404                                         }
405                                         continue;
406                                 }
407
408                                 if (bytes.Length > 0) {
409                                         output.Append (GetChars (bytes, e));
410                                         bytes.SetLength (0);
411                                 }
412
413                                 if (s [i] == '+') {
414                                         output.Append (' ');
415                                 } else {
416                                         output.Append (s [i]);
417                                 }
418                         }
419         
420                         if (bytes.Length > 0) {
421                                 output.Append (GetChars (bytes, e));
422                         }
423
424                         bytes = null;
425                         return output.ToString ();
426                 }
427         
428                 public static string UrlDecode (byte [] bytes, Encoding e)
429                 {
430                         if (bytes == null)
431                                 return null;
432
433                         return UrlDecode (bytes, 0, bytes.Length, e);
434                 }
435
436                 private static int GetInt (byte b)
437                 {
438                         char c = Char.ToUpper ((char) b);
439                         if (c >= '0' && c <= '9')
440                                 return c - '0';
441
442                         if (c < 'A' || c > 'F')
443                                 return 0;
444
445                         return (c - 'A' + 10);
446                 }
447
448                 private static char GetChar (byte [] bytes, int offset, int length)
449                 {
450                         int value = 0;
451                         int end = length + offset;
452                         for (int i = offset; i < end; i++)
453                                 value = (value << 4) + GetInt (bytes [i]);
454
455                         return (char) value;
456                 }
457                 
458                 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
459                 {
460                         if (bytes == null)
461                                 return null;
462                         if (count == 0)
463                                 return String.Empty;
464
465                         if (bytes == null)
466                                 throw new ArgumentNullException ("bytes");
467
468                         if (offset < 0 || offset > bytes.Length)
469                                 throw new ArgumentOutOfRangeException ("offset");
470
471                         if (count < 0 || offset + count > bytes.Length)
472                                 throw new ArgumentOutOfRangeException ("count");
473
474                         StringBuilder output = new StringBuilder ();
475                         MemoryStream acc = new MemoryStream ();
476
477                         int end = count + offset;
478                         for (int i = offset; i < end; i++) {
479                                 if (bytes [i] == '%' && i + 2 < count) {
480                                         if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
481                                                 if (acc.Length > 0) {
482                                                         output.Append (GetChars (acc, e));
483                                                         acc.SetLength (0);
484                                                 }
485                                                 output.Append (GetChar (bytes, i + 2, 4));
486                                                 i += 5;
487                                         } else {
488                                                 acc.WriteByte ((byte) GetChar (bytes, i + 1, 2));
489                                                 i += 2;
490                                         }
491                                         continue;
492                                 }
493
494                                 if (acc.Length > 0) {
495                                         output.Append (GetChars (acc, e));
496                                         acc.SetLength (0);
497                                 }
498
499                                 if (bytes [i] == '+') {
500                                         output.Append (' ');
501                                 } else {
502                                         output.Append ((char) bytes [i]);
503                                 }
504                         }
505
506                         if (acc.Length > 0) {
507                                 output.Append (GetChars (acc, e));
508                         }
509                         
510                         acc = null;
511                         return output.ToString ();
512                 }
513         
514                 public static byte [] UrlDecodeToBytes (byte [] bytes)
515                 {
516                         if (bytes == null)
517                                 return null;
518
519                         return UrlDecodeToBytes (bytes, 0, bytes.Length);
520                 }
521
522                 public static byte [] UrlDecodeToBytes (string str)
523                 {
524                         return UrlDecodeToBytes (str, Encoding.UTF8);
525                 }
526
527                 public static byte [] UrlDecodeToBytes (string str, Encoding e)
528                 {
529                         if (str == null)
530                                 return null;
531
532                         if (e == null)
533                                 throw new ArgumentNullException ("e");
534
535                         return UrlDecodeToBytes (e.GetBytes (str));
536                 }
537
538                 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
539                 {
540                         if (bytes == null)
541                                 return null;
542                         if (count == 0)
543                                 return new byte [0];
544
545                         int len = bytes.Length;
546                         if (offset < 0 || offset >= len)
547                                 throw new ArgumentOutOfRangeException("offset");
548
549                         if (count < 0 || offset > len - count)
550                                 throw new ArgumentOutOfRangeException("count");
551
552                         MemoryStream result = new MemoryStream ();
553                         int end = offset + count;
554                         for (int i = offset; i < end; i++){
555                                 char c = (char) bytes [i];
556                                 if (c == '+')
557                                         c = ' ';
558                                 else if (c == '%' && i < end - 2) {
559                                         c = GetChar (bytes, i + 1, 2);
560                                         i += 2;
561                                 }
562                                 result.WriteByte ((byte) c);
563                         }
564
565                         return result.ToArray ();
566                 }
567
568                 public static string UrlEncode(string str) 
569                 {
570                         return UrlEncode(str, Encoding.UTF8);
571                 }
572         
573                 public static string UrlEncode (string s, Encoding Enc) 
574                 {
575                         if (s == null)
576                                 return null;
577
578                         if (s == "")
579                                 return "";
580
581                         byte [] bytes = Enc.GetBytes (s);
582                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
583                 }
584           
585                 public static string UrlEncode (byte [] bytes)
586                 {
587                         if (bytes == null)
588                                 return null;
589
590                         if (bytes.Length == 0)
591                                 return "";
592
593                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
594                 }
595
596                 public static string UrlEncode (byte [] bytes, int offset, int count)
597                 {
598                         if (bytes == null)
599                                 return null;
600
601                         if (bytes.Length == 0)
602                                 return "";
603
604                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
605                 }
606
607                 public static byte [] UrlEncodeToBytes (string str)
608                 {
609                         return UrlEncodeToBytes (str, Encoding.UTF8);
610                 }
611
612                 public static byte [] UrlEncodeToBytes (string str, Encoding e)
613                 {
614                         if (str == null)
615                                 return null;
616
617                         if (str == "")
618                                 return new byte [0];
619
620                         byte [] bytes = e.GetBytes (str);
621                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
622                 }
623
624                 public static byte [] UrlEncodeToBytes (byte [] bytes)
625                 {
626                         if (bytes == null)
627                                 return null;
628
629                         if (bytes.Length == 0)
630                                 return new byte [0];
631
632                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
633                 }
634
635                 static char [] hexChars = "0123456789abcdef".ToCharArray ();
636
637                 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
638                 {
639                         if (bytes == null)
640                                 return null;
641
642                         int len = bytes.Length;
643                         if (len == 0)
644                                 return new byte [0];
645
646                         if (offset < 0 || offset >= len)
647                                 throw new ArgumentOutOfRangeException("offset");
648
649                         if (count < 0 || count > len - offset)
650                                 throw new ArgumentOutOfRangeException("count");
651
652                         MemoryStream result = new MemoryStream ();
653                         int end = offset + count;
654                         for (int i = offset; i < end; i++) {
655                                 char c = (char) bytes [i];
656                                 if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||
657                                     (c < 'A' && c > '9') ||
658                                     (c > 'Z' && c < 'a' && c != '_') ||
659                                     (c > 'z')) {
660                                         result.WriteByte ((byte) '%');
661                                         int idx = ((int) c) >> 4;
662                                         result.WriteByte ((byte) hexChars [idx]);
663                                         idx = ((int) c) & 0x0F;
664                                         result.WriteByte ((byte) hexChars [idx]);
665                                 } else {
666                                         result.WriteByte ((byte) c);
667                                 }
668                         }
669
670                         return result.ToArray ();
671                 }
672
673                 public static string UrlEncodeUnicode (string str)
674                 {
675                         if (str == null)
676                                 return null;
677
678                         StringBuilder result = new StringBuilder ();
679                         int end = str.Length;
680                         for (int i = 0; i < end; i++) {
681                                 int idx;
682                                 char c = str [i];
683                                 if (c > 255) {
684                                         result.Append ("%u");
685                                         idx = ((int) c) >> 24;
686                                         result.Append (hexChars [idx]);
687                                         idx = (((int) c) >> 16) & 0x0F;
688                                         result.Append (hexChars [idx]);
689                                         idx = (((int) c) >> 8) & 0x0F;
690                                         result.Append (hexChars [idx]);
691                                         idx = ((int) c) & 0x0F;
692                                         result.Append (hexChars [idx]);
693                                         continue;
694                                 }
695                                 
696                                 if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||
697                                     (c < 'A' && c > '9') ||
698                                     (c > 'Z' && c < 'a' && c != '_') ||
699                                     (c > 'z')) {
700                                         result.Append ('%');
701                                         idx = ((int) c) >> 4;
702                                         result.Append (hexChars [idx]);
703                                         idx = ((int) c) & 0x0F;
704                                         result.Append (hexChars [idx]);
705                                         continue;
706                                 }
707
708                                 result.Append (c);
709                         }
710
711                         return result.ToString ();
712                 }
713
714                 public static byte [] UrlEncodeUnicodeToBytes (string str)
715                 {
716                         if (str == null)
717                                 return null;
718
719                         if (str == "")
720                                 return new byte [0];
721
722                         return Encoding.ASCII.GetBytes (UrlEncodeUnicode (str));
723                 }
724
725                 /// <summary>
726                 /// Decodes an HTML-encoded string and returns the decoded string.
727                 /// </summary>
728                 /// <param name="s">The HTML string to decode. </param>
729                 /// <returns>The decoded text.</returns>
730                 public static string HtmlDecode (string s) 
731                 {
732                         if (s == null)
733                                 throw new ArgumentNullException ("s");
734
735                         if (s.IndexOf ('&') == -1)
736                                 return s;
737
738                         StringBuilder entity = new StringBuilder ();
739                         StringBuilder output = new StringBuilder ();
740                         int len = s.Length;
741                         // 0 -> nothing,
742                         // 1 -> right after '&'
743                         // 2 -> between '&' and ';' but no '#'
744                         // 3 -> '#' found after '&' and getting numbers
745                         int state = 0;
746                         int number = 0;
747                         bool have_trailing_digits = false;
748         
749                         for (int i = 0; i < len; i++) {
750                                 char c = s [i];
751                                 if (state == 0) {
752                                         if (c == '&') {
753                                                 entity.Append (c);
754                                                 state = 1;
755                                         } else {
756                                                 output.Append (c);
757                                         }
758                                         continue;
759                                 }
760
761                                 if (c == '&') {
762                                         state = 1;
763                                         if (have_trailing_digits) {
764                                                 entity.Append (number.ToString (CultureInfo.InvariantCulture));
765                                                 have_trailing_digits = false;
766                                         }
767
768                                         output.Append (entity.ToString ());
769                                         entity.Length = 0;
770                                         entity.Append ('&');
771                                         continue;
772                                 }
773
774                                 if (state == 1) {
775                                         if (c == ';') {
776                                                 state = 0;
777                                                 output.Append (entity.ToString ());
778                                                 output.Append (c);
779                                                 entity.Length = 0;
780                                         } else {
781                                                 number = 0;
782                                                 if (c != '#') {
783                                                         state = 2;
784                                                 } else {
785                                                         state = 3;
786                                                 }
787                                                 entity.Append (c);
788                                         }
789                                 } else if (state == 2) {
790                                         entity.Append (c);
791                                         if (c == ';') {
792                                                 string key = entity.ToString ();
793                                                 if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
794                                                         key = Entities [key.Substring (1, key.Length - 2)].ToString ();
795
796                                                 output.Append (key);
797                                                 state = 0;
798                                                 entity.Length = 0;
799                                         }
800                                 } else if (state == 3) {
801                                         if (c == ';') {
802                                                 if (number > 65535) {
803                                                         output.Append ("&#");
804                                                         output.Append (number.ToString (CultureInfo.InvariantCulture));
805                                                         output.Append (";");
806                                                 } else {
807                                                         output.Append ((char) number);
808                                                 }
809                                                 state = 0;
810                                                 entity.Length = 0;
811                                                 have_trailing_digits = false;
812                                         } else if (Char.IsDigit (c)) {
813                                                 number = number * 10 + ((int) c - '0');
814                                                 have_trailing_digits = true;
815                                         } else {
816                                                 state = 2;
817                                                 if (have_trailing_digits) {
818                                                         entity.Append (number.ToString (CultureInfo.InvariantCulture));
819                                                         have_trailing_digits = false;
820                                                 }
821                                                 entity.Append (c);
822                                         }
823                                 }
824                         }
825
826                         if (entity.Length > 0) {
827                                 output.Append (entity.ToString ());
828                         } else if (have_trailing_digits) {
829                                 output.Append (number.ToString (CultureInfo.InvariantCulture));
830                         }
831                         return output.ToString ();
832                 }
833         
834                 /// <summary>
835                 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
836                 /// </summary>
837                 /// <param name="s">The HTML string to decode</param>
838                 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
839                 public static void HtmlDecode(string s, TextWriter output) 
840                 {
841                         if (s != null)
842                                 output.Write (HtmlDecode (s));
843                 }
844         
845                 /// <summary>
846                 /// HTML-encodes a string and returns the encoded string.
847                 /// </summary>
848                 /// <param name="s">The text string to encode. </param>
849                 /// <returns>The HTML-encoded text.</returns>
850                 public static string HtmlEncode (string s) 
851                 {
852                         if (s == null)
853                                 return null;
854
855                         StringBuilder output = new StringBuilder ();
856                         
857                         foreach (char c in s) 
858                                 switch (c) {
859                                 case '&' :
860                                         output.Append ("&amp;");
861                                         break;
862                                 case '>' : 
863                                         output.Append ("&gt;");
864                                         break;
865                                 case '<' :
866                                         output.Append ("&lt;");
867                                         break;
868                                 case '"' :
869                                         output.Append ("&quot;");
870                                         break;
871                                 default:
872                                         // MS starts encoding with &# from 160 and stops at 255.
873                                         // We don't do that. One reason is the 65308/65310 unicode
874                                         // characters that look like '<' and '>'.
875                                         if (c > 159) {
876                                                 output.Append ("&#");
877                                                 output.Append (((int) c).ToString (CultureInfo.InvariantCulture));
878                                                 output.Append (";");
879                                         } else {
880                                                 output.Append (c);
881                                         }
882                                         break;
883                                 }
884                         return output.ToString ();
885                 }
886         
887                 /// <summary>
888                 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
889                 /// </summary>
890                 /// <param name="s">The string to encode. </param>
891                 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
892                 public static void HtmlEncode(string s, TextWriter output) 
893                 {
894                         if (s != null)
895                                 output.Write (HtmlEncode (s));
896                 }
897
898 #if NET_1_1
899                 public static string UrlPathEncode (string s)
900                 {
901                         if (s == null)
902                                 return null;
903
904                         int idx = s.IndexOf ("?");
905                         string s2 = null;
906                         if (idx != -1) {
907                                 s2 = s.Substring (0, idx-1);
908                                 s2 = UrlEncode (s2) + s.Substring (idx);
909                         } else {
910                                 s2 = UrlEncode (s);
911                         }
912
913                         return s2;
914                 }
915 #endif
916
917 #if NET_2_0
918                 [MonoTODO]
919                 public static NameValueCollection ParseQueryString (string query)
920                 {
921                         // LAMESPEC: default encoding not specified
922                         throw new NotImplementedException ();
923                 }
924
925                 [MonoTODO]
926                 public static NameValueCollection ParseQueryString (string query, Encoding encoding)
927                 {
928                         throw new NotImplementedException ();
929                 }
930 #endif
931                 #endregion // Methods
932         }
933 }
934