forked from gordon-matt/elFinder.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpEncoder.cs
More file actions
127 lines (110 loc) · 4.38 KB
/
Copy pathHttpEncoder.cs
File metadata and controls
127 lines (110 loc) · 4.38 KB
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
using System;
using System.Diagnostics;
using System.Text;
namespace elFinder.NetCore.Helpers
{
public static class HttpEncoder
{
public static string DecodePath(string path)
{
return Encoding.UTF8.GetString(UrlTokenDecode(path));
}
public static string EncodePath(string path)
{
return UrlTokenEncode(Encoding.UTF8.GetBytes(path));
}
private static byte[] UrlTokenDecode(string input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
int len = input.Length;
if (len < 1)
{
return new byte[0];
}
///////////////////////////////////////////////////////////////////
// Step 1: Calculate the number of padding chars to append to this string.
// The number of padding chars to append is stored in the last char of the string.
int numPadChars = input[len - 1] - '0';
if (numPadChars < 0 || numPadChars > 10)
{
return null;
}
///////////////////////////////////////////////////////////////////
// Step 2: Create array to store the chars (not including the last char)
// and the padding chars
char[] base64Chars = new char[len - 1 + numPadChars];
////////////////////////////////////////////////////////
// Step 3: Copy in the chars. Transform the "-" to "+", and "*" to "/"
for (int iter = 0; iter < len - 1; iter++)
{
char c = input[iter];
switch (c)
{
case '-': base64Chars[iter] = '+'; break;
case '_': base64Chars[iter] = '/'; break;
default: base64Chars[iter] = c; break;
}
}
////////////////////////////////////////////////////////
// Step 4: Add padding chars
for (int iter = len - 1; iter < base64Chars.Length; iter++)
{
base64Chars[iter] = '=';
}
// Do the actual conversion
return Convert.FromBase64CharArray(base64Chars, 0, base64Chars.Length);
}
private static string UrlTokenEncode(byte[] input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
if (input.Length < 1)
{
return string.Empty;
}
string base64Str = null;
int endPos = 0;
char[] base64Chars = null;
////////////////////////////////////////////////////////
// Step 1: Do a Base64 encoding
base64Str = Convert.ToBase64String(input);
if (base64Str == null)
{
return null;
}
////////////////////////////////////////////////////////
// Step 2: Find how many padding chars are present in the end
for (endPos = base64Str.Length; endPos > 0; endPos--)
{
if (base64Str[endPos - 1] != '=') // Found a non-padding char!
{
break; // Stop here
}
}
////////////////////////////////////////////////////////
// Step 3: Create char array to store all non-padding chars,
// plus a char to indicate how many padding chars are needed
base64Chars = new char[endPos + 1];
base64Chars[endPos] = (char)('0' + base64Str.Length - endPos); // Store a char at the end, to indicate how many padding chars are needed
////////////////////////////////////////////////////////
// Step 3: Copy in the other chars. Transform the "+" to "-", and "/" to "_"
for (int iter = 0; iter < endPos; iter++)
{
char c = base64Str[iter];
switch (c)
{
case '+': base64Chars[iter] = '-'; break;
case '/': base64Chars[iter] = '_'; break;
case '=': Debug.Assert(false); base64Chars[iter] = c; break;
default: base64Chars[iter] = c; break;
}
}
return new string(base64Chars);
}
}
}