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 15
15
16
16
#include " parity_even_7bit.h"
17
17
18
- #include < cstring>
19
-
20
18
21
19
namespace modsecurity ::actions::transformations {
22
20
23
21
24
22
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);
65
24
}
66
25
67
26
Original file line number Diff line number Diff line change @@ -26,7 +26,28 @@ class ParityEven7bit : public Transformation {
26
26
: Transformation(action) { }
27
27
28
28
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
+ }
30
51
};
31
52
32
53
} // namespace modsecurity::actions::transformations
Original file line number Diff line number Diff line change 14
14
*/
15
15
16
16
#include " parity_odd_7bit.h"
17
-
18
- #include < cstring>
17
+ #include " parity_even_7bit.h"
19
18
20
19
21
20
namespace modsecurity ::actions::transformations {
22
21
23
22
24
23
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);
65
25
}
66
26
67
27
Original file line number Diff line number Diff line change @@ -26,7 +26,6 @@ class ParityOdd7bit : public Transformation {
26
26
: Transformation(action) { }
27
27
28
28
bool transform (std::string &value, const Transaction *trans) const override ;
29
- static bool inplace (unsigned char *input, uint64_t input_len);
30
29
};
31
30
32
31
} // namespace modsecurity::actions::transformations
Original file line number Diff line number Diff line change 15
15
16
16
#include " parity_zero_7bit.h"
17
17
18
- #include < cstring>
19
-
20
18
21
19
namespace modsecurity ::actions::transformations {
22
20
23
21
24
- bool ParityZero7bit::transform (std::string &value, const Transaction *trans) const {
22
+ static inline bool inplace (std::string &value) {
25
23
if (value.empty ()) return false ;
26
24
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 ;
34
27
}
35
28
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 ;
44
30
}
45
31
46
32
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);
57
35
}
58
36
59
37
Original file line number Diff line number Diff line change @@ -26,7 +26,6 @@ class ParityZero7bit : public Transformation {
26
26
: Transformation(action) { }
27
27
28
28
bool transform (std::string &value, const Transaction *trans) const override ;
29
- static bool inplace (unsigned char *input, uint64_t input_len);
30
29
};
31
30
32
31
} // namespace modsecurity::actions::transformations
You can’t perform that action at this time.
0 commit comments