Skip to content

Commit 3ff72fb

Browse files
committed
Perform ParityEven7bit, ParityOdd7bit & ParityZero7bit transformations in-place
- Refactored to share implementations of ParityEven7bit & ParityOdd7bit.
1 parent 5d39890 commit 3ff72fb

File tree

6 files changed

+31
-115
lines changed

6 files changed

+31
-115
lines changed

src/actions/transformations/parity_even_7bit.cc

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,12 @@
1515

1616
#include "parity_even_7bit.h"
1717

18-
#include <cstring>
19-
2018

2119
namespace modsecurity::actions::transformations {
2220

2321

2422
bool ParityEven7bit::transform(std::string &value, const Transaction *trans) const {
25-
if (value.empty()) return false;
26-
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-
std::memcpy(input, value.c_str(), value.length()+1);
37-
38-
const auto ret = inplace(input, value.length());
39-
40-
value.assign(reinterpret_cast<char *>(input), value.length());
41-
free(input);
42-
43-
return ret;
44-
}
45-
46-
bool ParityEven7bit::inplace(unsigned char *input, uint64_t input_len) {
47-
uint64_t i;
48-
49-
i = 0;
50-
while (i < input_len) {
51-
unsigned int x = input[i];
52-
53-
input[i] ^= input[i] >> 4;
54-
input[i] &= 0xf;
55-
56-
if ((0x6996 >> input[i]) & 1) {
57-
input[i] = x | 0x80;
58-
} else {
59-
input[i] = x & 0x7f;
60-
}
61-
i++;
62-
}
63-
64-
return true;
23+
return ParityEven7bit::inplace<true>(value);
6524
}
6625

6726

src/actions/transformations/parity_even_7bit.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,28 @@ class ParityEven7bit : public Transformation {
2626
: Transformation(action) { }
2727

2828
bool transform(std::string &value, const Transaction *trans) const override;
29-
static bool inplace(unsigned char *input, uint64_t input_len);
29+
30+
template<bool even>
31+
static bool inplace(std::string &value) {
32+
if (value.empty()) return false;
33+
34+
for(auto &c : value) {
35+
auto &uc = reinterpret_cast<unsigned char&>(c);
36+
unsigned int x = uc;
37+
38+
uc ^= uc >> 4;
39+
uc &= 0xf;
40+
41+
const bool condition = (0x6996 >> uc) & 1;
42+
if (even ? condition : !condition) {
43+
uc = x | 0x80;
44+
} else {
45+
uc = x & 0x7f;
46+
}
47+
}
48+
49+
return true;
50+
}
3051
};
3152

3253
} // namespace modsecurity::actions::transformations

src/actions/transformations/parity_odd_7bit.cc

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,14 @@
1414
*/
1515

1616
#include "parity_odd_7bit.h"
17-
18-
#include <cstring>
17+
#include "parity_even_7bit.h"
1918

2019

2120
namespace modsecurity::actions::transformations {
2221

2322

2423
bool ParityOdd7bit::transform(std::string &value, const Transaction *trans) const {
25-
if (value.empty()) return false;
26-
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-
const auto ret = inplace(input, value.length());
39-
40-
value.assign(reinterpret_cast<char *>(input), value.length());
41-
free(input);
42-
43-
return ret;
44-
}
45-
46-
bool ParityOdd7bit::inplace(unsigned char *input, uint64_t input_len) {
47-
uint64_t i;
48-
49-
i = 0;
50-
while (i < input_len) {
51-
unsigned int x = input[i];
52-
53-
input[i] ^= input[i] >> 4;
54-
input[i] &= 0xf;
55-
56-
if ((0x6996 >> input[i]) & 1) {
57-
input[i] = x & 0x7f;
58-
} else {
59-
input[i] = x | 0x80;
60-
}
61-
i++;
62-
}
63-
64-
return true;
24+
return ParityEven7bit::inplace<false>(value);
6525
}
6626

6727

src/actions/transformations/parity_odd_7bit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ParityOdd7bit : public Transformation {
2626
: Transformation(action) { }
2727

2828
bool transform(std::string &value, const Transaction *trans) const override;
29-
static bool inplace(unsigned char *input, uint64_t input_len);
3029
};
3130

3231
} // namespace modsecurity::actions::transformations

src/actions/transformations/parity_zero_7bit.cc

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,23 @@
1515

1616
#include "parity_zero_7bit.h"
1717

18-
#include <cstring>
19-
2018

2119
namespace modsecurity::actions::transformations {
2220

2321

24-
bool ParityZero7bit::transform(std::string &value, const Transaction *trans) const {
22+
static inline bool inplace(std::string &value) {
2523
if (value.empty()) return false;
2624

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 "";
25+
for(auto &c : value) {
26+
((unsigned char&)c) &= 0x7f;
3427
}
3528

36-
memcpy(input, value.c_str(), value.length()+1);
37-
38-
const auto ret = inplace(input, value.length());
39-
40-
value.assign(reinterpret_cast<char *>(input), value.length());
41-
free(input);
42-
43-
return ret;
29+
return true;
4430
}
4531

4632

47-
bool ParityZero7bit::inplace(unsigned char *input, uint64_t input_len) {
48-
uint64_t i;
49-
50-
i = 0;
51-
while (i < input_len) {
52-
input[i] &= 0x7f;
53-
i++;
54-
}
55-
56-
return true;
33+
bool ParityZero7bit::transform(std::string &value, const Transaction *trans) const {
34+
return inplace(value);
5735
}
5836

5937

src/actions/transformations/parity_zero_7bit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ParityZero7bit : public Transformation {
2626
: Transformation(action) { }
2727

2828
bool transform(std::string &value, const Transaction *trans) const override;
29-
static bool inplace(unsigned char *input, uint64_t input_len);
3029
};
3130

3231
} // namespace modsecurity::actions::transformations

0 commit comments

Comments
 (0)