Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Aug 31, 2024
1 parent db33924 commit 278b4fd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions 3rdparty/Others/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ base64_decode(const char *in, unsigned int inlen, unsigned char *out)
unsigned int j;
unsigned char c;

if (inlen & 0x3) {
return 0;
}
//if (inlen & 0x3) {
// return 0;
//}

for (i = j = 0; i < inlen; i++) {
if (in[i] == BASE64_PAD) {
Expand Down
4 changes: 2 additions & 2 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,8 @@ std::string vuapi extract_file_directory_A(const std::string& file_path, bool la
std::wstring vuapi extract_file_directory_W(const std::wstring& file_path, bool last_slash = true);
std::string vuapi extract_file_name_A(const std::string& file_path, bool extension = true);
std::wstring vuapi extract_file_name_W(const std::wstring& file_path, bool extension = true);
std::string vuapi extract_file_extension_A(const std::string& file_path);
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path);
std::string vuapi extract_file_extension_A(const std::string& file_path, bool dot = true);
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path, bool dot = true);
std::string vuapi get_current_file_path_A();
std::wstring vuapi get_current_file_path_W();
std::string vuapi get_current_directory_A(bool last_slash = true);
Expand Down
30 changes: 21 additions & 9 deletions src/details/crypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ uint b64_calc_decode_size(const std::string& text)
// https://en.wikipedia.org/wiki/Base64#Decoding_Base64_with_padding

const auto size = strlen(text.c_str());
if (size < 3)
if (size < 2 || size % 4 != 0)
{
return 0;
}

int n_padding = 0;
if (text[size - 1] == '=') n_padding++;
if (text[size - 2] == '=') n_padding++;
size_t result = BASE64_DECODE_OUT_SIZE(size);

return uint(size * (3.F / 4.F)) - n_padding;
if (size >= 2)
{
if (text[size - 1] == '=') result -= 1;
if (text[size - 2] == '=') result -= 1;
}

return result;
}

bool crypt_b64encode_A(const std::vector<vu::byte>& data, std::string& text)
Expand All @@ -62,10 +66,14 @@ bool crypt_b64encode_A(const std::vector<vu::byte>& data, std::string& text)
return true;
}

const auto decoded_size = uint(data.size());
const auto encoded_size = b64_calc_encode_size(data);
text.resize(encoded_size); text.reserve(decoded_size + 1); // padding 1 null byte
if (encoded_size == 0)
{
return false;
}

const auto decoded_size = uint(data.size());
text.resize(encoded_size); text.reserve(decoded_size + 1); // padding 1 null byte
return base64_encode(data.data(), decoded_size, &text[0]) == encoded_size;
}

Expand All @@ -78,10 +86,14 @@ bool crypt_b64decode_A(const std::string& text, std::vector<vu::byte>& data)
return true;
}

const auto encoded_size = uint(strlen(text.c_str()));
const auto decoded_size = b64_calc_decode_size(text);
data.resize(decoded_size); data.reserve(decoded_size + 1); // padding 1 null byte
if (decoded_size == 0)
{
return false;
}

const auto encoded_size = uint(strlen(text.c_str()));
data.resize(decoded_size); data.reserve(decoded_size + 1); // padding 1 null byte
return base64_decode(text.data(), encoded_size, &data[0]) == decoded_size;
}

Expand Down
6 changes: 4 additions & 2 deletions src/details/filedir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,26 +264,28 @@ std::wstring vuapi extract_file_directory_W(const std::wstring& file_path, bool
return result;
}

std::string vuapi extract_file_extension_A(const std::string& file_path)
std::string vuapi extract_file_extension_A(const std::string& file_path, bool dot)
{
std::string result;

size_t pos_dot = file_path.find_last_of('.');
if (pos_dot != std::string::npos)
{
pos_dot += dot ? 0 : 1;
result = file_path.substr(pos_dot);
}

return result;
}

std::wstring vuapi extract_file_extension_W(const std::wstring& file_path)
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path, bool dot)
{
std::wstring result;

size_t pos_dot = file_path.find_last_of('.');
if (pos_dot != std::wstring::npos)
{
pos_dot += dot ? 0 : 1;
result = file_path.substr(pos_dot);
}

Expand Down

0 comments on commit 278b4fd

Please sign in to comment.