2121namespace modsecurity ::actions::transformations {
2222
2323
24- bool JsDecode::transform (std::string &value, const Transaction *trans) const {
25- std::string ret;
26- unsigned char *input;
27-
28- input = reinterpret_cast <unsigned char *>
29- (malloc (sizeof (char ) * value.length ()+1 ));
30-
31- if (input == NULL ) {
32- return " " ;
33- }
34-
35- memcpy (input, value.c_str (), value.length ()+1 );
24+ static inline int inplace (std::string &value) {
25+ auto d = reinterpret_cast <unsigned char *>(value.data ());
26+ const unsigned char *input = d;
27+ const auto input_len = value.length ();
3628
37- size_t i = inplace (input, value.length ());
38-
39- ret.assign (reinterpret_cast <char *>(input), i);
40- free (input);
41-
42- const auto changed = ret != value;
43- value = ret;
44- return changed;
45- }
46-
47-
48- int JsDecode::inplace (unsigned char *input, uint64_t input_len) {
49- unsigned char *d = (unsigned char *)input;
50- int64_t i, count;
51-
52- i = count = 0 ;
29+ bool changed = false ;
30+ std::string::size_type i = 0 ;
5331 while (i < input_len) {
5432 if (input[i] == ' \\ ' ) {
5533 /* Character is an escape. */
@@ -70,14 +48,14 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
7048 }
7149
7250 d++;
73- count++;
7451 i += 6 ;
52+ changed = true ;
7553 } else if ((i + 3 < input_len) && (input[i + 1 ] == ' x' )
7654 && VALID_HEX (input[i + 2 ]) && VALID_HEX (input[i + 3 ])) {
7755 /* \xHH */
7856 *d++ = utils::string::x2c (&input[i + 2 ]);
79- count++;
8057 i += 4 ;
58+ changed = true ;
8159 } else if ((i + 1 < input_len) && ISODIGIT (input[i + 1 ])) {
8260 /* \OOO (only one byte, \000 - \377) */
8361 char buf[4 ];
@@ -98,7 +76,7 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
9876 }
9977 *d++ = (unsigned char )strtol (buf, NULL , 8 );
10078 i += 1 + j;
101- count++ ;
79+ changed = true ;
10280 }
10381 } else if (i + 1 < input_len) {
10482 /* \C */
@@ -132,23 +110,27 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
132110
133111 *d++ = c;
134112 i += 2 ;
135- count++ ;
113+ changed = true ;
136114 } else {
137115 /* Not enough bytes */
138116 while (i < input_len) {
139117 *d++ = input[i++];
140- count++;
141118 }
142119 }
143120 } else {
144121 *d++ = input[i++];
145- count++;
146122 }
147123 }
148124
149125 *d = ' \0 ' ;
150126
151- return count;
127+ value.resize (d - input);
128+ return changed;
129+ }
130+
131+
132+ bool JsDecode::transform (std::string &value, const Transaction *trans) const {
133+ return inplace (value);
152134}
153135
154136
0 commit comments