-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpUtility.cs
1504 lines (1423 loc) · 47.8 KB
/
HttpUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace WinCachebox
{
/// <summary>Provides methods for encoding and decoding URLs when processing Web requests. This class cannot be inherited.</summary>
public sealed class HttpUtility
{
/// <summary>
/// Decodes all the bytes in the specified byte array into a string.
/// </summary>
/// <remarks>
/// Replace the method "System.Text.Encoding.ASCII.GetString(byte[] bytes);" in .Net Framework.
/// </remarks>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <returns>A String containing the results of decoding the specified sequence of bytes.</returns>
private static string ASCIIGetString(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
private static char[] s_entityEndingChars = new char[] { ';', '&' };
//internal static string AspCompatUrlEncode(string s)
//{
// s = UrlEncode(s);
// s = s.Replace("!", "%21");
// s = s.Replace("*", "%2A");
// s = s.Replace("(", "%28");
// s = s.Replace(")", "%29");
// s = s.Replace("-", "%2D");
// s = s.Replace(".", "%2E");
// s = s.Replace("_", "%5F");
// s = s.Replace(@"\", "%5C");
// return s;
//}
//internal static string CollapsePercentUFromStringInternal(string s, Encoding e)
//{
// int length = s.Length;
// UrlDecoder decoder = new UrlDecoder(length, e);
// if (s.IndexOf("%u", StringComparison.Ordinal) == -1)
// {
// return s;
// }
// for (int i = 0; i < length; i++)
// {
// char ch = s[i];
// if (((ch == '%') && (i < (length - 5))) && (s[i + 1] == 'u'))
// {
// int num4 = HexToInt(s[i + 2]);
// int num5 = HexToInt(s[i + 3]);
// int num6 = HexToInt(s[i + 4]);
// int num7 = HexToInt(s[i + 5]);
// if (((num4 >= 0) && (num5 >= 0)) && ((num6 >= 0) && (num7 >= 0)))
// {
// ch = (char) ((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
// i += 5;
// decoder.AddChar(ch);
// continue;
// }
// }
// if ((ch & 0xff80) == 0)
// {
// decoder.AddByte((byte) ch);
// }
// else
// {
// decoder.AddChar(ch);
// }
// }
// return decoder.GetString();
//}
//internal static string FormatHttpCookieDateTime(DateTime dt)
//{
// if ((dt < DateTime.MaxValue.AddDays(-1.0)) && (dt > DateTime.MinValue.AddDays(1.0)))
// {
// dt = dt.ToUniversalTime();
// }
// return dt.ToString("ddd, dd-MMM-yyyy HH':'mm':'ss 'GMT'", DateTimeFormatInfo.InvariantInfo);
//}
//internal static string FormatHttpDateTime(DateTime dt)
//{
// if ((dt < DateTime.MaxValue.AddDays(-1.0)) && (dt > DateTime.MinValue.AddDays(1.0)))
// {
// dt = dt.ToUniversalTime();
// }
// return dt.ToString("R", DateTimeFormatInfo.InvariantInfo);
//}
//internal static string FormatHttpDateTimeUtc(DateTime dt)
//{
// return dt.ToString("R", DateTimeFormatInfo.InvariantInfo);
//}
//internal static string FormatPlainTextAsHtml(string s)
//{
// if (s == null)
// {
// return null;
// }
// StringBuilder sb = new StringBuilder();
// StringWriter output = new StringWriter(sb);
// FormatPlainTextAsHtml(s, output);
// return sb.ToString();
//}
//internal static void FormatPlainTextAsHtml(string s, TextWriter output)
//{
// if (s != null)
// {
// int length = s.Length;
// char ch = '';
// for (int i = 0; i < length; i++)
// {
// char ch2 = s[i];
// switch (ch2)
// {
// case '\n':
// output.Write("<br>");
// goto Label_0113;
// case '\r':
// goto Label_0113;
// case ' ':
// if (ch != ' ')
// {
// break;
// }
// output.Write(" ");
// goto Label_0113;
// case '"':
// output.Write(""");
// goto Label_0113;
// case '&':
// output.Write("&");
// goto Label_0113;
// case '<':
// output.Write("<");
// goto Label_0113;
// case '>':
// output.Write(">");
// goto Label_0113;
// default:
// if ((ch2 >= '\x00a0') && (ch2 < 'A'))
// {
// output.Write("&#");
// output.Write(((int) ch2).ToString(NumberFormatInfo.InvariantInfo));
// output.Write(';');
// }
// else
// {
// output.Write(ch2);
// }
// goto Label_0113;
// }
// output.Write(ch2);
// Label_0113:
// ch = ch2;
// }
// }
//}
//internal static string FormatPlainTextSpacesAsHtml(string s)
//{
// if (s == null)
// {
// return null;
// }
// StringBuilder sb = new StringBuilder();
// StringWriter writer = new StringWriter(sb);
// int length = s.Length;
// for (int i = 0; i < length; i++)
// {
// char ch = s[i];
// if (ch == ' ')
// {
// writer.Write(" ");
// }
// else
// {
// writer.Write(ch);
// }
// }
// return sb.ToString();
//}
private static int HexToInt(char h)
{
if ((h >= '0') && (h <= '9'))
{
return (h - '0');
}
if ((h >= 'a') && (h <= 'f'))
{
return ((h - 'a') + 10);
}
if ((h >= 'A') && (h <= 'F'))
{
return ((h - 'A') + 10);
}
return -1;
}
///// <summary>Minimally converts a string to an HTML-encoded string.</summary>
///// <returns>An encoded string.</returns>
///// <param name="s">The string to encode. </param>
//public static string HtmlAttributeEncode(string s)
//{
// if (s == null)
// {
// return null;
// }
// int num = IndexOfHtmlAttributeEncodingChars(s, 0);
// if (num == -1)
// {
// return s;
// }
// StringBuilder builder = new StringBuilder(s.Length + 5);
// int length = s.Length;
// int startIndex = 0;
//Label_002A:
// if (num > startIndex)
// {
// builder.Append(s, startIndex, num - startIndex);
// }
// switch (s[num])
// {
// case '"':
// builder.Append(""");
// break;
// case '&':
// builder.Append("&");
// break;
// case '<':
// builder.Append("<");
// break;
// }
// startIndex = num + 1;
// if (startIndex < length)
// {
// num = IndexOfHtmlAttributeEncodingChars(s, startIndex);
// if (num != -1)
// {
// goto Label_002A;
// }
// builder.Append(s, startIndex, length - startIndex);
// }
// return builder.ToString();
//}
///// <summary>Minimally converts a string into an HTML-encoded string and sends the encoded string to a <see cref="T:System.IO.TextWriter"></see> output stream.</summary>
///// <param name="s">The string to encode </param>
///// <param name="output">A <see cref="T:System.IO.TextWriter"></see> output stream. </param>
//public static unsafe void HtmlAttributeEncode(string s, TextWriter output)
//{
// if (s != null)
// {
// int num = IndexOfHtmlAttributeEncodingChars(s, 0);
// if (num == -1)
// {
// output.Write(s);
// }
// else
// {
// int num2 = s.Length - num;
// fixed (char* str = ((char*) s))
// {
// char* chPtr = str;
// char* chPtr2 = chPtr;
// while (num-- > 0)
// {
// chPtr2++;
// output.Write(chPtr2[0]);
// }
// while (num2-- > 0)
// {
// chPtr2++;
// char ch = chPtr2[0];
// if (ch > '<')
// {
// goto Label_00A2;
// }
// char ch2 = ch;
// if (ch2 != '"')
// {
// if (ch2 == '&')
// {
// goto Label_008B;
// }
// if (ch2 != '<')
// {
// goto Label_0098;
// }
// output.Write("<");
// }
// else
// {
// output.Write(""");
// }
// continue;
// Label_008B:
// output.Write("&");
// continue;
// Label_0098:
// output.Write(ch);
// continue;
// Label_00A2:
// output.Write(ch);
// }
// }
// }
// }
//}
//internal static void HtmlAttributeEncodeInternal(string s, HttpWriter writer)
//{
// int num = IndexOfHtmlAttributeEncodingChars(s, 0);
// if (num == -1)
// {
// writer.Write(s);
// return;
// }
// int length = s.Length;
// int index = 0;
//Label_001D:
// if (num > index)
// {
// writer.WriteString(s, index, num - index);
// }
// switch (s[num])
// {
// case '"':
// writer.Write(""");
// break;
// case '&':
// writer.Write("&");
// break;
// case '<':
// writer.Write("<");
// break;
// }
// index = num + 1;
// if (index < length)
// {
// num = IndexOfHtmlAttributeEncodingChars(s, index);
// if (num != -1)
// {
// goto Label_001D;
// }
// writer.WriteString(s, index, length - index);
// }
//}
/// <summary>Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.</summary>
/// <returns>A decoded string.</returns>
/// <param name="s">The string to decode. </param>
public static string HtmlDecode(string s)
{
if (s == null)
{
return null;
}
if (s.IndexOf('&') < 0)
{
return s;
}
StringBuilder sb = new StringBuilder();
StringWriter output = new StringWriter(sb);
HtmlDecode(s, output);
return sb.ToString();
}
/// <summary>Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a <see cref="T:System.IO.TextWriter"></see> output stream.</summary>
/// <param name="s">The string to decode. </param>
/// <param name="output">A <see cref="T:System.IO.TextWriter"></see> stream of output. </param>
public static void HtmlDecode(string s, TextWriter output)
{
if (s != null)
{
if (s.IndexOf('&') < 0)
{
output.Write(s);
}
else
{
int length = s.Length;
for (int i = 0; i < length; i++)
{
char ch = s[i];
if (ch == '&')
{
int num3 = s.IndexOfAny(s_entityEndingChars, i + 1);
if ((num3 > 0) && (s[num3] == ';'))
{
string entity = s.Substring(i + 1, (num3 - i) - 1);
if ((entity.Length > 1) && (entity[0] == '#'))
{
try
{
if ((entity[1] == 'x') || (entity[1] == 'X'))
{
ch = (char)int.Parse(entity.Substring(2), NumberStyles.AllowHexSpecifier);
}
else
{
ch = (char)int.Parse(entity.Substring(1));
}
i = num3;
}
catch (FormatException)
{
i++;
}
catch (ArgumentException)
{
i++;
}
}
else
{
i = num3;
char ch2 = HtmlEntities.Lookup(entity);
if (ch2 != '\0')
{
ch = ch2;
}
else
{
output.Write('&');
output.Write(entity);
output.Write(';');
return;
}
}
}
}
output.Write(ch);
}
}
}
}
///// <summary>Converts a string to an HTML-encoded string.</summary>
///// <returns>An encoded string.</returns>
///// <param name="s">The string to encode. </param>
//public static string HtmlEncode(string s)
//{
// if (s == null)
// {
// return null;
// }
// int num = IndexOfHtmlEncodingChars(s, 0);
// if (num == -1)
// {
// return s;
// }
// StringBuilder builder = new StringBuilder(s.Length + 5);
// int length = s.Length;
// int startIndex = 0;
//Label_002A:
// if (num > startIndex)
// {
// builder.Append(s, startIndex, num - startIndex);
// }
// char ch = s[num];
// if (ch > '>')
// {
// builder.Append("&#");
// builder.Append(((int) ch).ToString(NumberFormatInfo.InvariantInfo));
// builder.Append(';');
// }
// else
// {
// char ch2 = ch;
// if (ch2 != '"')
// {
// switch (ch2)
// {
// case '<':
// builder.Append("<");
// goto Label_00D5;
// case '=':
// goto Label_00D5;
// case '>':
// builder.Append(">");
// goto Label_00D5;
// case '&':
// builder.Append("&");
// goto Label_00D5;
// }
// }
// else
// {
// builder.Append(""");
// }
// }
//Label_00D5:
// startIndex = num + 1;
// if (startIndex < length)
// {
// num = IndexOfHtmlEncodingChars(s, startIndex);
// if (num != -1)
// {
// goto Label_002A;
// }
// builder.Append(s, startIndex, length - startIndex);
// }
// return builder.ToString();
//}
///// <summary>Converts a string into an HTML-encoded string, and returns the output as a <see cref="T:System.IO.TextWriter"></see> stream of output.</summary>
///// <param name="s">The string to encode </param>
///// <param name="output">A <see cref="T:System.IO.TextWriter"></see> output stream. </param>
//public static unsafe void HtmlEncode(string s, TextWriter output)
//{
// if (s != null)
// {
// int num = IndexOfHtmlEncodingChars(s, 0);
// if (num == -1)
// {
// output.Write(s);
// }
// else
// {
// int num2 = s.Length - num;
// fixed (char* str = ((char*) s))
// {
// char* chPtr = str;
// char* chPtr2 = chPtr;
// while (num-- > 0)
// {
// chPtr2++;
// output.Write(chPtr2[0]);
// }
// while (num2-- > 0)
// {
// chPtr2++;
// char ch = chPtr2[0];
// if (ch > '>')
// {
// goto Label_00C4;
// }
// char ch2 = ch;
// if (ch2 != '"')
// {
// switch (ch2)
// {
// case '<':
// {
// output.Write("<");
// continue;
// }
// case '=':
// goto Label_00BA;
// case '>':
// {
// output.Write(">");
// continue;
// }
// case '&':
// goto Label_00AD;
// }
// goto Label_00BA;
// }
// output.Write(""");
// continue;
// Label_00AD:
// output.Write("&");
// continue;
// Label_00BA:
// output.Write(ch);
// continue;
// Label_00C4:
// if ((ch >= '\x00a0') && (ch < 'A'))
// {
// output.Write("&#");
// output.Write(((int) ch).ToString(NumberFormatInfo.InvariantInfo));
// output.Write(';');
// }
// else
// {
// output.Write(ch);
// }
// }
// }
// }
// }
//}
//private static unsafe int IndexOfHtmlAttributeEncodingChars(string s, int startPos)
//{
// int num = s.Length - startPos;
// fixed (char* str = ((char*) s))
// {
// char* chPtr = str;
// char* chPtr2 = chPtr + startPos;
// while (num > 0)
// {
// char ch = chPtr2[0];
// if (ch <= '<')
// {
// switch (ch)
// {
// case '"':
// case '&':
// case '<':
// return (s.Length - num);
// }
// }
// chPtr2++;
// num--;
// }
// }
// return -1;
//}
//private static unsafe int IndexOfHtmlEncodingChars(string s, int startPos)
//{
// int num = s.Length - startPos;
// fixed (char* str = ((char*) s))
// {
// char* chPtr = str;
// char* chPtr2 = chPtr + startPos;
// while (num > 0)
// {
// char ch = chPtr2[0];
// if (ch <= '>')
// {
// switch (ch)
// {
// case '<':
// case '>':
// case '"':
// case '&':
// return (s.Length - num);
// case '=':
// goto Label_007A;
// }
// }
// else if ((ch >= '\x00a0') && (ch < 'A'))
// {
// return (s.Length - num);
// }
// Label_007A:
// chPtr2++;
// num--;
// }
// }
// return -1;
//}
internal static char IntToHex(int n)
{
if (n <= 9)
{
return (char)(n + 0x30);
}
return (char)((n - 10) + 0x61);
}
private static bool IsNonAsciiByte(byte b)
{
if (b < 0x7f)
{
return (b < 0x20);
}
return true;
}
internal static bool IsSafe(char ch)
{
if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
{
return true;
}
switch (ch)
{
case '\'':
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
case '!':
return true;
}
return false;
}
///// <summary>Parses a query string into a <see cref="T:System.Collections.Specialized.NameValueCollection"></see> using <see cref="P:System.Text.Encoding.UTF8"></see> encoding.</summary>
///// <returns>A <see cref="T:System.Collections.Specialized.NameValueCollection"></see> of query parameters and values.</returns>
///// <param name="query">The query string to parse.</param>
///// <exception cref="T:System.ArgumentNullException">query is null. </exception>
//public static NameValueCollection ParseQueryString(string query)
//{
// return ParseQueryString(query, Encoding.UTF8);
//}
///// <summary>Parses a query string into a <see cref="T:System.Collections.Specialized.NameValueCollection"></see> using the specified <see cref="T:System.Text.Encoding"></see>. </summary>
///// <returns>A <see cref="T:System.Collections.Specialized.NameValueCollection"></see> of query parameters and values.</returns>
///// <param name="encoding">The <see cref="T:System.Text.Encoding"></see> to use.</param>
///// <param name="query">The query string to parse.</param>
///// <exception cref="T:System.ArgumentNullException">query is null.- or -encoding is null.</exception>
//public static NameValueCollection ParseQueryString(string query, Encoding encoding)
//{
// if (query == null)
// {
// throw new ArgumentNullException("query");
// }
// if (encoding == null)
// {
// throw new ArgumentNullException("encoding");
// }
// if ((query.Length > 0) && (query[0] == '?'))
// {
// query = query.Substring(1);
// }
// return new HttpValueCollection(query, false, true, encoding);
//}
/// <summary>Converts a string that has been encoded for transmission in a URL into a decoded string.</summary>
/// <returns>A decoded string.</returns>
/// <param name="str">The string to decode. </param>
public static string UrlDecode(string str)
{
if (str == null)
{
return null;
}
return UrlDecode(str, Encoding.UTF8);
}
/// <summary>Converts a URL-encoded byte array into a decoded string using the specified decoding object.</summary>
/// <returns>A decoded string.</returns>
/// <param name="e">The <see cref="T:System.Text.Encoding"></see> that specifies the decoding scheme. </param>
/// <param name="bytes">The array of bytes to decode. </param>
public static string UrlDecode(byte[] bytes, Encoding e)
{
if (bytes == null)
{
return null;
}
return UrlDecode(bytes, 0, bytes.Length, e);
}
/// <summary>Converts a URL-encoded string into a decoded string, using the specified encoding object.</summary>
/// <returns>A decoded string.</returns>
/// <param name="e">The <see cref="T:System.Text.Encoding"></see> that specifies the decoding scheme. </param>
/// <param name="str">The string to decode. </param>
public static string UrlDecode(string str, Encoding e)
{
if (str == null)
{
return null;
}
return UrlDecodeStringFromStringInternal(str, e);
}
/// <summary>Converts a URL-encoded byte array into a decoded string using the specified encoding object, starting at the specified position in the array, and continuing for the specified number of bytes.</summary>
/// <returns>A decoded string.</returns>
/// <param name="offset">The position in the byte to begin decoding. </param>
/// <param name="count">The number of bytes to decode. </param>
/// <param name="e">The <see cref="T:System.Text.Encoding"></see> object that specifies the decoding scheme. </param>
/// <param name="bytes">The array of bytes to decode. </param>
public static string UrlDecode(byte[] bytes, int offset, int count, Encoding e)
{
if ((bytes == null) && (count == 0))
{
return null;
}
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
if ((offset < 0) || (offset > bytes.Length))
{
throw new ArgumentOutOfRangeException("offset");
}
if ((count < 0) || ((offset + count) > bytes.Length))
{
throw new ArgumentOutOfRangeException("count");
}
return UrlDecodeStringFromBytesInternal(bytes, offset, count, e);
}
private static byte[] UrlDecodeBytesFromBytesInternal(byte[] buf, int offset, int count)
{
int length = 0;
byte[] sourceArray = new byte[count];
for (int i = 0; i < count; i++)
{
int index = offset + i;
byte num4 = buf[index];
if (num4 == 0x2b)
{
num4 = 0x20;
}
else if ((num4 == 0x25) && (i < (count - 2)))
{
int num5 = HexToInt((char)buf[index + 1]);
int num6 = HexToInt((char)buf[index + 2]);
if ((num5 >= 0) && (num6 >= 0))
{
num4 = (byte)((num5 << 4) | num6);
i += 2;
}
}
sourceArray[length++] = num4;
}
if (length < sourceArray.Length)
{
byte[] destinationArray = new byte[length];
Array.Copy(sourceArray, destinationArray, length);
sourceArray = destinationArray;
}
return sourceArray;
}
private static string UrlDecodeStringFromBytesInternal(byte[] buf, int offset, int count, Encoding e)
{
UrlDecoder decoder = new UrlDecoder(count, e);
for (int i = 0; i < count; i++)
{
int index = offset + i;
byte b = buf[index];
if (b == 0x2b)
{
b = 0x20;
}
else if ((b == 0x25) && (i < (count - 2)))
{
if ((buf[index + 1] == 0x75) && (i < (count - 5)))
{
int num4 = HexToInt((char)buf[index + 2]);
int num5 = HexToInt((char)buf[index + 3]);
int num6 = HexToInt((char)buf[index + 4]);
int num7 = HexToInt((char)buf[index + 5]);
if (((num4 < 0) || (num5 < 0)) || ((num6 < 0) || (num7 < 0)))
{
break;
}
char ch = (char)((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
i += 5;
decoder.AddChar(ch);
continue;
}
int num8 = HexToInt((char)buf[index + 1]);
int num9 = HexToInt((char)buf[index + 2]);
if ((num8 >= 0) && (num9 >= 0))
{
b = (byte)((num8 << 4) | num9);
i += 2;
}
}
decoder.AddByte(b);
}
return decoder.GetString();
}
private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
{
int length = s.Length;
UrlDecoder decoder = new UrlDecoder(length, e);
for (int i = 0; i < length; i++)
{
char ch = s[i];
if (ch == '+')
{
ch = ' ';
}
else if ((ch == '%') && (i < (length - 2)))
{
if ((s[i + 1] == 'u') && (i < (length - 5)))
{
int num3 = HexToInt(s[i + 2]);
int num4 = HexToInt(s[i + 3]);
int num5 = HexToInt(s[i + 4]);
int num6 = HexToInt(s[i + 5]);
if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
{
break;
}
ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
i += 5;
decoder.AddChar(ch);
continue;
}
int num7 = HexToInt(s[i + 1]);
int num8 = HexToInt(s[i + 2]);
if ((num7 >= 0) && (num8 >= 0))
{
byte b = (byte)((num7 << 4) | num8);
i += 2;
decoder.AddByte(b);
continue;
}
}
if ((ch & 0xff80) == 0)
{
decoder.AddByte((byte)ch);
}
else
{
decoder.AddChar(ch);
}
}
return decoder.GetString();
}
/// <summary>Converts a URL-encoded array of bytes into a decoded array of bytes.</summary>
/// <returns>A decoded array of bytes.</returns>
/// <param name="bytes">The array of bytes to decode. </param>
public static byte[] UrlDecodeToBytes(byte[] bytes)
{
if (bytes == null)
{
return null;
}
return UrlDecodeToBytes(bytes, 0, (bytes != null) ? bytes.Length : 0);
}
/// <summary>Converts a URL-encoded string into a decoded array of bytes.</summary>
/// <returns>A decoded array of bytes.</returns>
/// <param name="str">The string to decode. </param>
public static byte[] UrlDecodeToBytes(string str)
{
if (str == null)
{
return null;
}
return UrlDecodeToBytes(str, Encoding.UTF8);
}
/// <summary>Converts a URL-encoded string into a decoded array of bytes using the specified decoding object.</summary>
/// <returns>A decoded array of bytes.</returns>
/// <param name="e">The <see cref="T:System.Text.Encoding"></see> object that specifies the decoding scheme. </param>
/// <param name="str">The string to decode. </param>
public static byte[] UrlDecodeToBytes(string str, Encoding e)
{
if (str == null)
{
return null;
}
return UrlDecodeToBytes(e.GetBytes(str));
}
/// <summary>Converts a URL-encoded array of bytes into a decoded array of bytes, starting at the specified position in the array and continuing for the specified number of bytes.</summary>
/// <returns>A decoded array of bytes.</returns>
/// <param name="offset">The position in the byte array at which to begin decoding. </param>
/// <param name="count">The number of bytes to decode. </param>
/// <param name="bytes">The array of bytes to decode. </param>
public static byte[] UrlDecodeToBytes(byte[] bytes, int offset, int count)
{
if ((bytes == null) && (count == 0))
{
return null;
}
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
if ((offset < 0) || (offset > bytes.Length))
{
throw new ArgumentOutOfRangeException("offset");
}
if ((count < 0) || ((offset + count) > bytes.Length))
{
throw new ArgumentOutOfRangeException("count");
}
return UrlDecodeBytesFromBytesInternal(bytes, offset, count);
}
/// <summary>Converts a byte array into an encoded URL string.</summary>