From 278b4fdb4a1152c4db1347aecf3b48d9d40781f6 Mon Sep 17 00:00:00 2001 From: Vic P Date: Sat, 31 Aug 2024 22:11:30 +0700 Subject: [PATCH] Vutils --- 3rdparty/Others/base64.cpp | 6 +++--- include/Vutils.h | 4 ++-- src/details/crypt.cpp | 30 +++++++++++++++++++++--------- src/details/filedir.cpp | 6 ++++-- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/3rdparty/Others/base64.cpp b/3rdparty/Others/base64.cpp index eeded25..acf765e 100644 --- a/3rdparty/Others/base64.cpp +++ b/3rdparty/Others/base64.cpp @@ -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) { diff --git a/include/Vutils.h b/include/Vutils.h index fd52c5f..7fd48ba 100644 --- a/include/Vutils.h +++ b/include/Vutils.h @@ -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); diff --git a/src/details/crypt.cpp b/src/details/crypt.cpp index efac54d..d8dccad 100644 --- a/src/details/crypt.cpp +++ b/src/details/crypt.cpp @@ -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& data, std::string& text) @@ -62,10 +66,14 @@ bool crypt_b64encode_A(const std::vector& 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; } @@ -78,10 +86,14 @@ bool crypt_b64decode_A(const std::string& text, std::vector& 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; } diff --git a/src/details/filedir.cpp b/src/details/filedir.cpp index f69da05..574ead2 100644 --- a/src/details/filedir.cpp +++ b/src/details/filedir.cpp @@ -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); }