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