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
  • Loading branch information
kevinbackhouse committed Nov 10, 2024
1 parent 6f9387c commit c1c1d34
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 @@ -324,21 +324,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 c1c1d34

Please sign in to comment.