Skip to content

Commit

Permalink
base64decode should return immediately if the input is empty
Browse files Browse the repository at this point in the history
(cherry picked from commit eda7171)
  • Loading branch information
kevinbackhouse authored and kmilos committed Nov 11, 2024
1 parent dfa0d5a commit 4b431be
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static char to_hex(char code) {

/// @brief Convert a hex character to its integer value.
static char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : static_cast<char>(tolower(ch)) - 'a' + 10;
return 0xF & (isdigit(ch) ? ch - '0' : static_cast<char>(tolower(ch)) - 'a' + 10);
}

std::string urlencode(const std::string& str) {
Expand Down Expand Up @@ -168,7 +168,7 @@ int base64encode(const void* data_buf, size_t dataLength, char* result, size_t r
size_t base64decode(const char* in, char* out, size_t out_size) {
size_t result = 0;
size_t input_length = in ? ::strlen(in) : 0;
if (!in || input_length % 4 != 0)
if (!in || input_length % 4 != 0 || input_length == 0)
return result;

auto encoding_table = reinterpret_cast<const unsigned char*>(base64_encode);
Expand Down Expand Up @@ -323,21 +323,26 @@ Uri Uri::Parse(const std::string& uri) {
auto hostEnd = std::find(authEnd, (pathStart != uriEnd) ? pathStart : queryStart,
':'); // check for port

result.Host = std::string(hostStart, hostEnd);
if (hostStart < hostEnd) {
result.Host = std::string(hostStart, hostEnd);
}

// port
if ((hostEnd != uriEnd) && (*hostEnd == ':')) // we have a port
{
++hostEnd;
auto portEnd = (pathStart != uriEnd) ? pathStart : queryStart;
result.Port = std::string(hostEnd, portEnd);
if (hostEnd < portEnd) {
result.Port = std::string(hostEnd, portEnd);
}
}
if (result.Port.empty() && result.Protocol == "http")
result.Port = "80";

// path
if (pathStart != uriEnd)
if (pathStart < queryStart) {
result.Path = std::string(pathStart, queryStart);
}

// query
if (queryStart != uriEnd)
Expand Down

0 comments on commit 4b431be

Please sign in to comment.