From e687140d0583ae5e85e248bdb364eec8b952a921 Mon Sep 17 00:00:00 2001 From: Eduardo Arias Date: Mon, 19 Aug 2024 08:12:00 -0700 Subject: [PATCH] Perform HexDecode transformation in-place - Removed inplace helper function from the class, as it's only referenced by the implementation. --- src/actions/transformations/hex_decode.cc | 44 +++++++---------------- src/actions/transformations/hex_decode.h | 2 -- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/actions/transformations/hex_decode.cc b/src/actions/transformations/hex_decode.cc index 5866079b..1b43b6cd 100644 --- a/src/actions/transformations/hex_decode.cc +++ b/src/actions/transformations/hex_decode.cc @@ -21,46 +21,26 @@ namespace modsecurity::actions::transformations { -bool HexDecode::transform(std::string &value, const Transaction *trans) const { - std::string ret; - unsigned char *input; - int size = 0; +static inline int inplace(std::string &value) { + if (value.empty()) return false; - input = reinterpret_cast - (malloc(sizeof(char) * value.length()+1)); + const auto len = value.length(); + auto d = reinterpret_cast(value.data()); + const auto data = d; - if (input == NULL) { - return ""; + for (int i = 0; i <= len - 2; i += 2) { + *d++ = utils::string::x2c(&data[i]); } - memcpy(input, value.c_str(), value.length()+1); + *d = '\0'; - size = inplace(input, value.length()); - - ret.assign(reinterpret_cast(input), size); - free(input); - - const auto changed = ret != value; - value = ret; - return changed; + value.resize(d - data); + return true; } -int HexDecode::inplace(unsigned char *data, int len) { - unsigned char *d = data; - int count = 0; - - if ((data == NULL) || (len == 0)) { - return 0; - } - - for (int i = 0;i <= len - 2;i += 2) { - *d++ = utils::string::x2c(&data[i]); - count++; - } - *d = '\0'; - - return count; +bool HexDecode::transform(std::string &value, const Transaction *trans) const { + return inplace(value); } diff --git a/src/actions/transformations/hex_decode.h b/src/actions/transformations/hex_decode.h index 4239c86d..158f91aa 100644 --- a/src/actions/transformations/hex_decode.h +++ b/src/actions/transformations/hex_decode.h @@ -26,8 +26,6 @@ class HexDecode : public Transformation { : Transformation(action) { } bool transform(std::string &value, const Transaction *trans) const override; - - static int inplace(unsigned char *data, int len); }; } // namespace modsecurity::actions::transformations