Skip to content

Commit

Permalink
[Opt](exec) remove the unless mem alloc in base64 (apache#32019)
Browse files Browse the repository at this point in the history
  • Loading branch information
HappenLee authored Mar 11, 2024
1 parent 83c2f5a commit 3381eb8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
21 changes: 7 additions & 14 deletions be/src/util/url_coding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ static void encode_base64_internal(const std::string& in, std::string* out,
const unsigned char* basis, bool padding) {
size_t len = in.size();
// Every 3 source bytes will be encoded into 4 bytes.
std::unique_ptr<unsigned char[]> buf(new unsigned char[(((len + 2) / 3) * 4)]);
out->resize((len + 2) / 3 * 4);
unsigned char* d = (unsigned char*)out->data();
const auto* s = reinterpret_cast<const unsigned char*>(in.data());
unsigned char* d = buf.get();
while (len > 2) {
*d++ = basis[(s[0] >> 2) & 0x3f];
*d++ = basis[((s[0] & 3) << 4) | (s[1] >> 4)];
Expand All @@ -117,13 +117,7 @@ static void encode_base64_internal(const std::string& in, std::string* out,
*d++ = '=';
}
}
out->assign((char*)buf.get(), d - buf.get());
}

void base64url_encode(const std::string& in, std::string* out) {
static unsigned char basis64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
encode_base64_internal(in, out, basis64, false);
out->resize((char*)d - out->data());
}

void base64_encode(const std::string& in, std::string* out) {
Expand Down Expand Up @@ -254,15 +248,13 @@ int64_t base64_decode(const char* data, size_t length, char* decoded_data) {
}

bool base64_decode(const std::string& in, std::string* out) {
char* tmp = new char[in.length()];
out->resize(in.length());

int64_t len = base64_decode(in.c_str(), in.length(), tmp);
int64_t len = base64_decode(in.c_str(), in.length(), out->data());
if (len < 0) {
delete[] tmp;
return false;
}
out->assign(tmp, len);
delete[] tmp;
out->resize(len);
return true;
}

Expand All @@ -286,6 +278,7 @@ void escape_for_html(const std::string& in, std::stringstream* out) {
}
}
}

std::string escape_for_html_to_string(const std::string& in) {
std::stringstream str;
escape_for_html(in, &str);
Expand Down
2 changes: 0 additions & 2 deletions be/src/util/url_coding.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace doris {
// behaviour when encoding a string, which is only to encode certain
// characters (excluding, e.g., ' ')
void url_encode(const std::string& in, std::string* out);
void url_encode(const std::vector<uint8_t>& in, std::string* out);

// Utility method to decode a string that was URL-encoded. Returns
// true unless the string could not be correctly decoded.
Expand All @@ -41,7 +40,6 @@ void url_encode(const std::vector<uint8_t>& in, std::string* out);
// certain characters like ' '.
bool url_decode(const std::string& in, std::string* out);

void base64url_encode(const std::string& in, std::string* out);
void base64_encode(const std::string& in, std::string* out);
size_t base64_encode(const unsigned char* data, size_t length, unsigned char* encoded_data);

Expand Down

0 comments on commit 3381eb8

Please sign in to comment.