File tree Expand file tree Collapse file tree 6 files changed +31
-115
lines changed
src/actions/transformations Expand file tree Collapse file tree 6 files changed +31
-115
lines changed Original file line number Diff line number Diff line change 1515
1616#include " parity_even_7bit.h"
1717
18- #include < cstring>
19-
2018
2119namespace modsecurity ::actions::transformations {
2220
2321
2422bool 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1414 */
1515
1616#include " parity_odd_7bit.h"
17-
18- #include < cstring>
17+ #include " parity_even_7bit.h"
1918
2019
2120namespace modsecurity ::actions::transformations {
2221
2322
2423bool 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1515
1616#include " parity_zero_7bit.h"
1717
18- #include < cstring>
19-
2018
2119namespace 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments