Skip to content

Commit aafccfa

Browse files
committed
Perform HexDecode transformation in-place
- Removed inplace helper function from the class, as it's only referenced by the implementation.
1 parent 3b641c1 commit aafccfa

File tree

2 files changed

+12
-34
lines changed

2 files changed

+12
-34
lines changed

src/actions/transformations/hex_decode.cc

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,26 @@
2121
namespace modsecurity::actions::transformations {
2222

2323

24-
bool HexDecode::transform(std::string &value, const Transaction *trans) const {
25-
std::string ret;
26-
unsigned char *input;
27-
int size = 0;
24+
static inline int inplace(std::string &value) {
25+
if (value.empty()) return false;
2826

29-
input = reinterpret_cast<unsigned char *>
30-
(malloc(sizeof(char) * value.length()+1));
27+
const auto len = value.length();
28+
auto d = reinterpret_cast<unsigned char *>(value.data());
29+
const auto data = d;
3130

32-
if (input == NULL) {
33-
return "";
31+
for (int i = 0; i <= len - 2; i += 2) {
32+
*d++ = utils::string::x2c(&data[i]);
3433
}
3534

36-
memcpy(input, value.c_str(), value.length()+1);
37-
38-
size = inplace(input, value.length());
39-
40-
ret.assign(reinterpret_cast<char *>(input), size);
41-
free(input);
35+
*d = '\0';
4236

43-
const auto changed = ret != value;
44-
value = ret;
45-
return changed;
37+
value.resize(d - data);
38+
return true;
4639
}
4740

4841

49-
int HexDecode::inplace(unsigned char *data, int len) {
50-
unsigned char *d = data;
51-
int count = 0;
52-
53-
if ((data == NULL) || (len == 0)) {
54-
return 0;
55-
}
56-
57-
for (int i = 0;i <= len - 2;i += 2) {
58-
*d++ = utils::string::x2c(&data[i]);
59-
count++;
60-
}
61-
*d = '\0';
62-
63-
return count;
42+
bool HexDecode::transform(std::string &value, const Transaction *trans) const {
43+
return inplace(value);
6444
}
6545

6646

src/actions/transformations/hex_decode.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class HexDecode : public Transformation {
2626
: Transformation(action) { }
2727

2828
bool transform(std::string &value, const Transaction *trans) const override;
29-
30-
static int inplace(unsigned char *data, int len);
3129
};
3230

3331
} // namespace modsecurity::actions::transformations

0 commit comments

Comments
 (0)