Skip to content
This repository was archived by the owner on Nov 29, 2024. It is now read-only.

Commit 0b4e457

Browse files
committed
WideCharToMultiByte() and MultiByteToWideChar() optimization
1 parent 4351579 commit 0b4e457

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

lib/EAPBase/include/EAP.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ template<class _Traits, class _Ax>
855855
inline void operator<<(_Inout_ eap::cursor_out &cursor, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val)
856856
{
857857
std::string val_utf8;
858-
WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), val_utf8, NULL, NULL);
858+
WideCharToMultiByte(CP_UTF8, 0, val, val_utf8, NULL, NULL);
859859
cursor << val_utf8;
860860
}
861861

@@ -872,7 +872,7 @@ inline void operator>>(_Inout_ eap::cursor_in &cursor, _Out_ std::basic_string<w
872872
{
873873
std::string val_utf8;
874874
cursor >> val_utf8;
875-
MultiByteToWideChar(CP_UTF8, 0, val_utf8.c_str(), (int)val_utf8.length(), val);
875+
MultiByteToWideChar(CP_UTF8, 0, val_utf8, val);
876876
}
877877

878878

lib/EAPBase/include/Module.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ namespace eap
307307
std::vector<unsigned char> encrypt(_In_ HCRYPTPROV hProv, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val, _Out_opt_ HCRYPTHASH hHash = NULL) const
308308
{
309309
winstd::sanitizing_string val_utf8;
310-
WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), val_utf8, NULL, NULL);
310+
WideCharToMultiByte(CP_UTF8, 0, val, val_utf8, NULL, NULL);
311311
return encrypt(hProv, val_utf8, hHash);
312312
}
313313

@@ -351,7 +351,7 @@ namespace eap
351351
std::vector<unsigned char> encrypt_md5(_In_ HCRYPTPROV hProv, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val) const
352352
{
353353
winstd::sanitizing_string val_utf8;
354-
WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), val_utf8, NULL, NULL);
354+
WideCharToMultiByte(CP_UTF8, 0, val, val_utf8, NULL, NULL);
355355
return encrypt_md5(hProv, val_utf8);
356356
}
357357

@@ -432,7 +432,7 @@ namespace eap
432432
{
433433
winstd::sanitizing_string buf(std::move(decrypt_str(hProv, data, size, hHash)));
434434
std::basic_string<wchar_t, _Traits, _Ax> dec;
435-
MultiByteToWideChar(CP_UTF8, 0, buf.data(), (int)buf.size(), dec);
435+
MultiByteToWideChar(CP_UTF8, 0, buf, dec);
436436
return dec;
437437
}
438438

@@ -504,7 +504,7 @@ namespace eap
504504
{
505505
winstd::sanitizing_string buf(std::move(decrypt_str_md5<char, std::char_traits<char>, sanitizing_allocator<char> >(hProv, data, size)));
506506
std::basic_string<wchar_t, _Traits, _Ax> dec;
507-
MultiByteToWideChar(CP_UTF8, 0, buf.data(), (int)buf.size(), dec);
507+
MultiByteToWideChar(CP_UTF8, 0, buf, dec);
508508
return dec;
509509
}
510510

lib/EAPBase/src/Credentials.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void eap::credentials_pass::save(_In_ IXMLDOMDocument *pDoc, _In_ IXMLDOMNode *p
251251
switch (m_enc_alg) {
252252
case enc_alg_kph: {
253253
sanitizing_string password_utf8;
254-
WideCharToMultiByte(CP_UTF8, 0, m_password.c_str(), -1, password_utf8, NULL, NULL);
254+
WideCharToMultiByte(CP_UTF8, 0, m_password, password_utf8, NULL, NULL);
255255
wstring password_enc(std::move(kph_encrypt<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >(cp, password_utf8.c_str())));
256256
com_obj<IXMLDOMElement> pXmlElPassword;
257257
if (FAILED(hr = eapxml::put_element_value(pDoc, pConfigRoot, bstr(L"Password"), namespace_eapmetadata, bstr(password_enc), std::addressof(pXmlElPassword))))
@@ -308,7 +308,7 @@ void eap::credentials_pass::load(_In_ IXMLDOMNode *pConfigRoot)
308308
} else if (encryption && CompareStringEx(LOCALE_NAME_INVARIANT, NORM_IGNORECASE, encryption, encryption.length(), _L("KPH"), -1, NULL, NULL, 0) == CSTR_EQUAL) {
309309
// Decrypt password.
310310
sanitizing_string password_utf8(std::move(kph_decrypt<OLECHAR>(password)));
311-
MultiByteToWideChar(CP_UTF8, 0, password_utf8.c_str(), -1, m_password);
311+
MultiByteToWideChar(CP_UTF8, 0, password_utf8, m_password);
312312
m_enc_alg = enc_alg_kph;
313313
} else if (encryption && encryption[0]) {
314314
// Encryption is defined but unrecognized.
@@ -354,7 +354,7 @@ void eap::credentials_pass::store(_In_z_ LPCTSTR pszTargetName, _In_ unsigned in
354354

355355
// Convert password to UTF-8.
356356
sanitizing_string cred_utf8;
357-
WideCharToMultiByte(CP_UTF8, 0, m_password.c_str(), (int)m_password.length(), cred_utf8, NULL, NULL);
357+
WideCharToMultiByte(CP_UTF8, 0, m_password, cred_utf8, NULL, NULL);
358358

359359
// Encrypt the password using user's key.
360360
DATA_BLOB cred_blob = { (DWORD)cred_utf8.size() , const_cast<LPBYTE>(reinterpret_cast<LPCBYTE>(cred_utf8.data())) };

lib/MSCHAPv2/src/Method.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void eap::method_mschapv2::process_request_packet(
101101
case phase_init: {
102102
// Convert username to UTF-8.
103103
sanitizing_string identity_utf8;
104-
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity.c_str(), (int)m_cred.m_identity.length(), identity_utf8, NULL, NULL);
104+
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity, identity_utf8, NULL, NULL);
105105

106106
// Randomize Peer-Challenge
107107
m_challenge_client.randomize(m_cp);
@@ -209,7 +209,7 @@ void eap::method_mschapv2::process_success(_In_ const list<string> &argv)
209209

210210
// Calculate expected authenticator response.
211211
sanitizing_string identity_utf8;
212-
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity.c_str(), (int)m_cred.m_identity.length(), identity_utf8, NULL, NULL);
212+
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity, identity_utf8, NULL, NULL);
213213
authenticator_response resp_exp(m_cp, m_challenge_server, m_challenge_client, identity_utf8.c_str(), m_cred.m_password.c_str(), m_nt_resp);
214214

215215
// Compare against provided authemticator response.
@@ -249,7 +249,7 @@ void eap::method_mschapv2::process_error(_In_ const list<string> &argv)
249249
throw invalid_argument(string_printf(__FUNCTION__ " Incorrect MSCHAPv2 challenge length (expected: %uB, received: %uB).", sizeof(m_challenge_server), resp.size()));
250250
memcpy(&m_challenge_server, resp.data(), sizeof(m_challenge_server));
251251
} else if ((val[0] == 'M' || val[0] == 'm') && val[1] == '=') {
252-
MultiByteToWideChar(CP_UTF8, 0, val.data() + 2, -1, m_cfg.m_last_msg);
252+
MultiByteToWideChar(CP_UTF8, 0, val.data() + 2, (int)val.length() - 2, m_cfg.m_last_msg);
253253
m_module.log_event(&EAPMETHOD_METHOD_FAILURE_ERROR1, event_data((unsigned int)eap_type_legacy_mschapv2), event_data(m_cfg.m_last_msg), event_data::blank);
254254
}
255255
}

lib/PAP/src/Method.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ void eap::method_pap::process_request_packet(
8484
case phase_init: {
8585
// Convert username and password to UTF-8.
8686
sanitizing_string identity_utf8, password_utf8;
87-
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity.c_str(), (int)m_cred.m_identity.length(), identity_utf8, NULL, NULL);
88-
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_password.c_str(), (int)m_cred.m_password.length(), password_utf8, NULL, NULL);
87+
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity, identity_utf8, NULL, NULL);
88+
WideCharToMultiByte(CP_UTF8, 0, m_cred.m_password, password_utf8, NULL, NULL);
8989

9090
// PAP passwords must be padded to 16B boundary according to RFC 5281. Will not add random extra padding here, as length obfuscation should be done by outer transport layers.
9191
size_t padding_password_ex = (16 - password_utf8.length()) % 16;

lib/TLS/src/Method.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void eap::method_tls::begin_session(
206206
m_sc_target_name.insert(m_sc_target_name.end(), name->begin(), name->end());
207207
#else
208208
string buf;
209-
WideCharToMultiByte(CP_ACP, 0, name->c_str(), -1, buf, NULL, NULL);
209+
WideCharToMultiByte(CP_ACP, 0, name, buf, NULL, NULL);
210210
m_sc_target_name.insert(m_sc_target_name.end(), buf.begin(), buf.end());
211211
#endif
212212
}

0 commit comments

Comments
 (0)