forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebUtility.cs
174 lines (162 loc) · 4.78 KB
/
WebUtility.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
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
//
// (c) Microsoft Corporation. See LICENSE.TXT file for licensing details
//
using System.Text;
namespace System.Net
{
public static class WebUtility
{
public static string UrlEncode(string str)
{
if (str == null)
{
return null;
}
return new string(Encoding.UTF8.GetChars(UrlEncodeToBytes(str, Encoding.UTF8)));
}
static byte[] UrlEncodeToBytes(string str, Encoding e)
{
if (str == null)
{
return null;
}
var bytes = e.GetBytes(str);
return UrlEncodeBytesToBytesInternal(bytes, 0, bytes.Length, false);
}
static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
{
var num = 0;
var num2 = 0;
for (var i = 0; i < count; i++)
{
var ch = (char)bytes[offset + i];
if (ch == ' ')
{
num++;
}
else if (!IsSafe(ch))
{
num2++;
}
}
if ((!alwaysCreateReturnValue && (num == 0)) &&
(num2 == 0))
{
return bytes;
}
var buffer = new byte[count + (num2 * 2)];
var num4 = 0;
for (var j = 0; j < count; j++)
{
var num6 = bytes[offset + j];
var ch2 = (char)num6;
if (IsSafe(ch2))
{
buffer[num4++] = num6;
}
else if (ch2 == ' ')
{
buffer[num4++] = 0x2b;
}
else
{
buffer[num4++] = 0x25;
buffer[num4++] = (byte)IntToHex((num6 >> 4) & 15);
buffer[num4++] = (byte)IntToHex(num6 & 15);
}
}
return buffer;
}
static char IntToHex(int n)
{
if (n <= 9)
{
return (char)(n + 0x30);
}
return (char)((n - 10) + 0x61);
}
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;
}
public static string UrlDecode(string url)
{
var data = Encoding.UTF8.GetBytes(url);
return new string(Encoding.UTF8.GetChars(UrlDecodeBytesToBytes(data, 0, data.Length)));
}
static byte[] UrlDecodeBytesToBytes(byte[] bytes, int offset, int count)
{
var length = 0;
var sourceArray = new byte[count];
for (var i = 0; i < count; i++)
{
var index = offset + i;
var num4 = bytes[index];
if (num4 == 0x2b)
{
num4 = 0x20;
}
else if ((num4 == 0x25) &&
(i < (count - 2)))
{
var num5 = HexToInt((char)bytes[index + 1]);
var num6 = HexToInt((char)bytes[index + 2]);
if ((num5 >= 0) &&
(num6 >= 0))
{
num4 = (byte)((num5 << 4) | num6);
i += 2;
}
}
sourceArray[length++] = num4;
}
if (length < sourceArray.Length)
{
var destinationArray = new byte[length];
Array.Copy(sourceArray, destinationArray, length);
sourceArray = destinationArray;
}
return sourceArray;
}
public 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;
}
}
}