2222namespace modsecurity ::actions::transformations {
2323
2424
25- bool UrlDecodeUni::transform (std::string &value, const Transaction *t) const {
26- std::string ret;
27- unsigned char *input;
28-
29- input = reinterpret_cast <unsigned char *>
30- (malloc (sizeof (char ) * value.length ()+1 ));
31-
32- if (input == NULL ) {
33- return " " ;
34- }
35-
36- memcpy (input, value.c_str (), value.length ()+1 );
37-
38- size_t i = inplace (input, value.length (), t);
39-
40- ret.assign (reinterpret_cast <char *>(input), i);
41- free (input);
42-
43- const auto changed = ret != value;
44- value = ret;
45- return changed;
46- }
47-
48-
4925/* *
5026 *
5127 * IMP1 Assumes NUL-terminated
5228 */
53- int UrlDecodeUni:: inplace (unsigned char *input, uint64_t input_len ,
29+ static inline bool inplace (std::string &value ,
5430 const Transaction *t) {
55- unsigned char *d = input;
56- int64_t i, count, fact, j, xv;
57- int Code, hmap = -1 ;
31+ bool changed = false ;
32+ auto d = reinterpret_cast <unsigned char *>(value.data ());
33+ const unsigned char *input = d;
34+ const auto input_len = value.length ();
5835
59- if (input == NULL ) return -1 ;
36+ std::string::size_type i, count, fact, j, xv;
37+ int Code, hmap = -1 ;
6038
6139 i = count = 0 ;
6240 while (i < input_len) {
@@ -116,19 +94,17 @@ int UrlDecodeUni::inplace(unsigned char *input, uint64_t input_len,
11694 }
11795 }
11896 d++;
119- count++;
12097 i += 6 ;
98+ changed = true ;
12199 } else {
122100 /* Invalid data, skip %u. */
123101 *d++ = input[i++];
124102 *d++ = input[i++];
125- count += 2 ;
126103 }
127104 } else {
128105 /* Not enough bytes (4 data bytes), skip %u. */
129106 *d++ = input[i++];
130107 *d++ = input[i++];
131- count += 2 ;
132108 }
133109 } else {
134110 /* Standard URL encoding. */
@@ -143,8 +119,8 @@ int UrlDecodeUni::inplace(unsigned char *input, uint64_t input_len,
143119
144120 if (VALID_HEX (c1) && VALID_HEX (c2)) {
145121 *d++ = utils::string::x2c (&input[i + 1 ]);
146- count++;
147122 i += 3 ;
123+ changed = true ;
148124 } else {
149125 /* Not a valid encoding, skip this % */
150126 *d++ = input[i++];
@@ -153,25 +129,31 @@ int UrlDecodeUni::inplace(unsigned char *input, uint64_t input_len,
153129 } else {
154130 /* Not enough bytes available, skip this % */
155131 *d++ = input[i++];
156- count++;
157132 }
158133 }
159134 } else {
160135 /* Character is not a percent sign. */
161136 if (input[i] == ' +' ) {
162137 *d++ = ' ' ;
138+ changed = true ;
163139 } else {
164140 *d++ = input[i];
165141 }
166142
167- count++;
168143 i++;
169144 }
170145 }
171146
172147 *d = ' \0 ' ;
173148
174- return count;
149+ value.resize (d - input);
150+ return changed;
151+ }
152+
153+
154+
155+ bool UrlDecodeUni::transform (std::string &value, const Transaction *trans) const {
156+ return inplace (value, trans);
175157}
176158
177159
0 commit comments