Skip to content

Remove unnecessary copying in transformations #2368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/actions/transformations/css_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,11 @@ namespace transformations {
std::string CssDecode::evaluate(const std::string &value,
Transaction *transaction) {

char *tmp = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0';
std::string ret = value;

CssDecode::css_decode_inplace(reinterpret_cast<unsigned char *>(tmp),
value.size());
auto size = CssDecode::css_decode_inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.size());
ret.resize(size);

std::string ret(tmp, 0, value.size());
free(tmp);
return ret;
}

Expand Down
13 changes: 3 additions & 10 deletions src/actions/transformations/escape_seq_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,10 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,

std::string EscapeSeqDecode::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret = value;

unsigned char *tmp = (unsigned char *) malloc(sizeof(char)
* value.size() + 1);
memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0';

int size = ansi_c_sequences_decode_inplace(tmp, value.size());

std::string ret("");
ret.assign(reinterpret_cast<char *>(tmp), size);
free(tmp);
auto size = ansi_c_sequences_decode_inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.size());
ret.resize(size);

return ret;
}
Expand Down
19 changes: 3 additions & 16 deletions src/actions/transformations/hex_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,10 @@ namespace transformations {

std::string HexDecode::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
int size = 0;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

size = inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), size);
free(input);
auto size = inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length());
ret.resize(size);

return ret;
}
Expand Down
18 changes: 3 additions & 15 deletions src/actions/transformations/html_entity_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@ namespace transformations {

std::string HtmlEntityDecode::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

size_t i = inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), i);
free(input);
auto i = inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length());
ret.resize(i);

return ret;
}
Expand Down
18 changes: 3 additions & 15 deletions src/actions/transformations/js_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,10 @@ namespace transformations {

std::string JsDecode::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

size_t i = inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), i);
free(input);
auto i = inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length());
ret.resize(i);

return ret;
}
Expand Down
16 changes: 4 additions & 12 deletions src/actions/transformations/normalise_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,11 @@ NormalisePath::NormalisePath(const std::string &action)

std::string NormalisePath::evaluate(const std::string &value,
Transaction *transaction) {
int changed = 0;
std::string ret = value;
int changed;

char *tmp = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0';

int i = normalize_path_inplace((unsigned char *)tmp,
value.size(), 0, &changed);

std::string ret("");
ret.assign(tmp, i);
free(tmp);
auto size = normalize_path_inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length(), 0, &changed);
ret.resize(size);

return ret;
}
Expand Down
15 changes: 3 additions & 12 deletions src/actions/transformations/normalise_path_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,11 @@ namespace transformations {

std::string NormalisePathWin::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret = value;
int changed;

char *tmp = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0';

int i = NormalisePath::normalize_path_inplace(
reinterpret_cast<unsigned char *>(tmp),
value.size(), 1, &changed);

std::string ret("");
ret.assign(tmp, i);
free(tmp);
auto size = NormalisePath::normalize_path_inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length(), 1, &changed);
ret.resize(size);

return ret;
}
Expand Down
17 changes: 2 additions & 15 deletions src/actions/transformations/parity_even_7bit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,9 @@ namespace transformations {

std::string ParityEven7bit::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

std::memcpy(input, value.c_str(), value.length()+1);

inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), value.length());
free(input);
inplace(reinterpret_cast<unsigned char*>(&ret[0]), ret.size());

return ret;
}
Expand Down
19 changes: 2 additions & 17 deletions src/actions/transformations/parity_odd_7bit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,8 @@ namespace transformations {

std::string ParityOdd7bit::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), value.length());
free(input);

std::string ret = value;
inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length());
return ret;
}

Expand Down
17 changes: 2 additions & 15 deletions src/actions/transformations/parity_zero_7bit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,9 @@ namespace transformations {

std::string ParityZero7bit::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), value.length());
free(input);
inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length());

return ret;
}
Expand Down
19 changes: 3 additions & 16 deletions src/actions/transformations/sql_hex_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,10 @@ namespace transformations {

std::string SqlHexDecode::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
int size = 0;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

size = inplace(input, value.length());

ret.assign(reinterpret_cast<char *>(input), size);
free(input);
auto size = inplace(reinterpret_cast<unsigned char*>(&ret[0]), ret.size());
ret.resize(size);

return ret;
}
Expand Down
17 changes: 5 additions & 12 deletions src/actions/transformations/url_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,16 @@ UrlDecode::UrlDecode(const std::string &action)

std::string UrlDecode::evaluate(const std::string &value,
Transaction *transaction) {
unsigned char *val = NULL;
std::string ret = value;
int invalid_count = 0;
int changed;

val = (unsigned char *) malloc(sizeof(char) * value.size() + 1);
memcpy(val, value.c_str(), value.size() + 1);
val[value.size()] = '\0';

int size = utils::urldecode_nonstrict_inplace(val, value.size(),
int size = utils::urldecode_nonstrict_inplace(
reinterpret_cast<unsigned char *>(&ret[0]), ret.size(),
&invalid_count, &changed);
std::string out;

out.append((const char *)val, size);

free(val);
ret.resize(size);

return out;
return ret;
}


Expand Down
18 changes: 3 additions & 15 deletions src/actions/transformations/url_decode_uni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,10 @@ namespace transformations {

std::string UrlDecodeUni::evaluate(const std::string &value,
Transaction *t) {
std::string ret;
unsigned char *input;
std::string ret = value;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

size_t i = inplace(input, value.length(), t);

ret.assign(reinterpret_cast<char *>(input), i);
free(input);
size_t i = inplace(reinterpret_cast<unsigned char *>(&ret[0]), ret.length(), t);
ret.resize(i);

return ret;
}
Expand Down
17 changes: 3 additions & 14 deletions src/actions/transformations/utf8_to_unicode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,10 @@ namespace transformations {
std::string Utf8ToUnicode::evaluate(const std::string &value,
Transaction *transaction) {
std::string ret;
unsigned char *input;
int changed = 0;
char *out;

input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));

if (input == NULL) {
return "";
}

memcpy(input, value.c_str(), value.length()+1);

out = inplace(input, value.size() + 1, &changed);
free(input);
out = inplace(reinterpret_cast<const unsigned char *>(&value[0]), value.size() + 1, &changed);
if (out != NULL) {
ret.assign(reinterpret_cast<char *>(out),
strlen(reinterpret_cast<char *>(out)));
Expand All @@ -61,7 +50,7 @@ std::string Utf8ToUnicode::evaluate(const std::string &value,
}


char *Utf8ToUnicode::inplace(unsigned char *input,
char *Utf8ToUnicode::inplace(const unsigned char *input,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the in-place method here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, apart from the name the function is not really in place.

uint64_t input_len, int *changed) {
unsigned int count = 0;
char *data;
Expand Down Expand Up @@ -89,7 +78,7 @@ char *Utf8ToUnicode::inplace(unsigned char *input,
int unicode_len = 0;
unsigned int d = 0;
unsigned char c;
unsigned char *utf = (unsigned char *)&input[i];
const unsigned char *utf = &input[i];

c = *utf;

Expand Down
2 changes: 1 addition & 1 deletion src/actions/transformations/utf8_to_unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Utf8ToUnicode : public Transformation {
std::string evaluate(const std::string &exp,
Transaction *transaction) override;

static char *inplace(unsigned char *input, uint64_t input_len,
static char *inplace(const unsigned char *input, uint64_t input_len,
int *changed);
};

Expand Down