21
21
namespace modsecurity ::actions::transformations {
22
22
23
23
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 ();
36
28
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 ;
53
31
while (i < input_len) {
54
32
if (input[i] == ' \\ ' ) {
55
33
/* Character is an escape. */
@@ -70,14 +48,14 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
70
48
}
71
49
72
50
d++;
73
- count++;
74
51
i += 6 ;
52
+ changed = true ;
75
53
} else if ((i + 3 < input_len) && (input[i + 1 ] == ' x' )
76
54
&& VALID_HEX (input[i + 2 ]) && VALID_HEX (input[i + 3 ])) {
77
55
/* \xHH */
78
56
*d++ = utils::string::x2c (&input[i + 2 ]);
79
- count++;
80
57
i += 4 ;
58
+ changed = true ;
81
59
} else if ((i + 1 < input_len) && ISODIGIT (input[i + 1 ])) {
82
60
/* \OOO (only one byte, \000 - \377) */
83
61
char buf[4 ];
@@ -98,7 +76,7 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
98
76
}
99
77
*d++ = (unsigned char )strtol (buf, NULL , 8 );
100
78
i += 1 + j;
101
- count++ ;
79
+ changed = true ;
102
80
}
103
81
} else if (i + 1 < input_len) {
104
82
/* \C */
@@ -132,23 +110,27 @@ int JsDecode::inplace(unsigned char *input, uint64_t input_len) {
132
110
133
111
*d++ = c;
134
112
i += 2 ;
135
- count++ ;
113
+ changed = true ;
136
114
} else {
137
115
/* Not enough bytes */
138
116
while (i < input_len) {
139
117
*d++ = input[i++];
140
- count++;
141
118
}
142
119
}
143
120
} else {
144
121
*d++ = input[i++];
145
- count++;
146
122
}
147
123
}
148
124
149
125
*d = ' \0 ' ;
150
126
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);
152
134
}
153
135
154
136
0 commit comments