Skip to content

Commit 278b4fd

Browse files
committed
Vutils
1 parent db33924 commit 278b4fd

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

3rdparty/Others/base64.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ base64_decode(const char *in, unsigned int inlen, unsigned char *out)
125125
unsigned int j;
126126
unsigned char c;
127127

128-
if (inlen & 0x3) {
129-
return 0;
130-
}
128+
//if (inlen & 0x3) {
129+
// return 0;
130+
//}
131131

132132
for (i = j = 0; i < inlen; i++) {
133133
if (in[i] == BASE64_PAD) {

include/Vutils.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,8 @@ std::string vuapi extract_file_directory_A(const std::string& file_path, bool la
664664
std::wstring vuapi extract_file_directory_W(const std::wstring& file_path, bool last_slash = true);
665665
std::string vuapi extract_file_name_A(const std::string& file_path, bool extension = true);
666666
std::wstring vuapi extract_file_name_W(const std::wstring& file_path, bool extension = true);
667-
std::string vuapi extract_file_extension_A(const std::string& file_path);
668-
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path);
667+
std::string vuapi extract_file_extension_A(const std::string& file_path, bool dot = true);
668+
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path, bool dot = true);
669669
std::string vuapi get_current_file_path_A();
670670
std::wstring vuapi get_current_file_path_W();
671671
std::string vuapi get_current_directory_A(bool last_slash = true);

src/details/crypt.cpp

+21-9
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@ uint b64_calc_decode_size(const std::string& text)
4141
// https://en.wikipedia.org/wiki/Base64#Decoding_Base64_with_padding
4242

4343
const auto size = strlen(text.c_str());
44-
if (size < 3)
44+
if (size < 2 || size % 4 != 0)
4545
{
4646
return 0;
4747
}
4848

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

53-
return uint(size * (3.F / 4.F)) - n_padding;
51+
if (size >= 2)
52+
{
53+
if (text[size - 1] == '=') result -= 1;
54+
if (text[size - 2] == '=') result -= 1;
55+
}
56+
57+
return result;
5458
}
5559

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

65-
const auto decoded_size = uint(data.size());
6669
const auto encoded_size = b64_calc_encode_size(data);
67-
text.resize(encoded_size); text.reserve(decoded_size + 1); // padding 1 null byte
70+
if (encoded_size == 0)
71+
{
72+
return false;
73+
}
6874

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

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

81-
const auto encoded_size = uint(strlen(text.c_str()));
8289
const auto decoded_size = b64_calc_decode_size(text);
83-
data.resize(decoded_size); data.reserve(decoded_size + 1); // padding 1 null byte
90+
if (decoded_size == 0)
91+
{
92+
return false;
93+
}
8494

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

src/details/filedir.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,28 @@ std::wstring vuapi extract_file_directory_W(const std::wstring& file_path, bool
264264
return result;
265265
}
266266

267-
std::string vuapi extract_file_extension_A(const std::string& file_path)
267+
std::string vuapi extract_file_extension_A(const std::string& file_path, bool dot)
268268
{
269269
std::string result;
270270

271271
size_t pos_dot = file_path.find_last_of('.');
272272
if (pos_dot != std::string::npos)
273273
{
274+
pos_dot += dot ? 0 : 1;
274275
result = file_path.substr(pos_dot);
275276
}
276277

277278
return result;
278279
}
279280

280-
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path)
281+
std::wstring vuapi extract_file_extension_W(const std::wstring& file_path, bool dot)
281282
{
282283
std::wstring result;
283284

284285
size_t pos_dot = file_path.find_last_of('.');
285286
if (pos_dot != std::wstring::npos)
286287
{
288+
pos_dot += dot ? 0 : 1;
287289
result = file_path.substr(pos_dot);
288290
}
289291

0 commit comments

Comments
 (0)