/
url_decodeuni.go
143 lines (121 loc) · 3.31 KB
/
url_decodeuni.go
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
package transformations
import (
"github.com/asalih/guardian/helpers"
)
func init() {
TransformationMaps.funcMap["urlDecodeUni"] = func(variableData interface{}) interface{} {
return urlDecodeUni(variableData.(string), 20127)
}
}
func urlDecodeUni(
input string,
unicodeCodePage int,
) string {
if input == "" {
return ""
}
newString := make([]byte, len(input))
index, newIndex, xv, code, fact := 0, 0, 0, 0, 0
hmap := byte(0)
hmapFound := false
inputLength := len(input)
for index < inputLength {
if input[index] == '%' {
// Character is a percent sign.
if (index+1) < inputLength && ((input[index+1] == 'u') || (input[index+1] == 'U')) {
// IIS-specific %u encoding.
if index+5 < inputLength {
// We have at least 4 data bytes.
if helpers.ValidHex(input[index+2]) &&
helpers.ValidHex(input[index+3]) &&
helpers.ValidHex(input[index+4]) &&
helpers.ValidHex(input[index+5]) {
code = 0
fact = 1
hmapFound = false
if len(unicodemap) > 0 && unicodeCodePage > 0 {
for i := 5; i >= 2; i-- {
if helpers.ValidHex(input[index+i]) {
if input[index+i] >= 97 {
xv = int(input[index+i]) - 97 + 10
} else if input[index+i] >= 65 {
xv = int(input[index+i]) - 65 + 10
} else {
xv = int(input[index+i]) - 48
}
code += xv * fact
fact *= 16
}
}
if code >= 0 && code <= 65535 {
hmap, hmapFound = unicodemap[unicodeCodePage][code]
}
}
if hmapFound {
newString[newIndex] = hmap
} else {
// We first make use of the lower byte here, ignoring the higher byte.
newString[newIndex] = helpers.X2c(input[index+4], input[index+5])
// Full width ASCII (ff01 - ff5e) needs 0x20 added
if (newString[newIndex] > 0x00) &&
(newString[newIndex] < 0x5f) &&
(input[index+2] == 'f' || input[index+2] == 'F' &&
(input[index+3] == 'f' || input[index+3] == 'F')) {
newString[newIndex] += 0x20
}
}
newIndex++
index += 6
} else {
// Invalid data, skip %u.
newString[newIndex] = input[index]
newIndex++
newString[newIndex] = input[index+1]
newIndex++
index += 2
}
} else {
// Not enough bytes (4 data bytes), skip %u.
newString[newIndex] = input[index]
newIndex++
newString[newIndex] = input[index+1]
newIndex++
index += 2
}
} else {
// Standard URL encoding.
// Are there enough bytes available?
if index+2 < inputLength {
// Yes
// Decode a %xx combo only if it is valid.
c1, c2 := input[index+1], input[index+2]
if helpers.ValidHex(c1) && helpers.ValidHex(c2) {
newString[newIndex] = helpers.X2c(c1, c2)
newIndex++
index += 3
} else {
// Not a valid encoding, skip this %
newString[newIndex] = input[index]
newIndex++
index++
}
} else {
// Not enough bytes available, skip this %
newString[newIndex] = input[index]
newIndex++
index++
}
}
} else {
// Character is not a percent sign.
if input[index] == '+' {
newString[newIndex] = ' '
} else {
newString[newIndex] = input[index]
}
newIndex++
index++
}
}
return string(newString[:newIndex])
}