From: Iain Patterson Date: Thu, 28 Jul 2016 15:15:53 +0000 (+0100) Subject: Use UTF-8 functions when working with explicit encodings. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=2f3ab42db3d63e674e520a68fe507119af519288;p=nssm.git Use UTF-8 functions when working with explicit encodings. LSA functions explicitly operate on UTF-16 strings. GetProcAddress() explicitly operates on multibyte strings. --- diff --git a/account.cpp b/account.cpp index 96120ac..6150301 100644 --- a/account.cpp +++ b/account.cpp @@ -66,32 +66,20 @@ int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) { } LSA_UNICODE_STRING lsa_username; -#ifdef UNICODE - lsa_username.Buffer = (wchar_t *) expanded; - lsa_username.Length = (unsigned short) _tcslen(expanded) * sizeof(TCHAR); - lsa_username.MaximumLength = lsa_username.Length + sizeof(TCHAR); -#else - size_t buflen; - mbstowcs_s(&buflen, NULL, 0, expanded, _TRUNCATE); - lsa_username.MaximumLength = (unsigned short) buflen * sizeof(wchar_t); - lsa_username.Length = lsa_username.MaximumLength - sizeof(wchar_t); - lsa_username.Buffer = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, lsa_username.MaximumLength); - if (lsa_username.Buffer) mbstowcs_s(&buflen, lsa_username.Buffer, lsa_username.MaximumLength, expanded, _TRUNCATE); - else { + int ret = to_utf16(expanded, &lsa_username.Buffer, (unsigned long *) &lsa_username.Length); + HeapFree(GetProcessHeap(), 0, expanded); + if (ret) { if (policy == &handle) LsaClose(handle); - HeapFree(GetProcessHeap(), 0, expanded); print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("LSA_UNICODE_STRING"), _T("username_sid()")); return 4; } -#endif + lsa_username.Length *= sizeof(wchar_t); + lsa_username.MaximumLength = lsa_username.Length + sizeof(wchar_t); LSA_REFERENCED_DOMAIN_LIST *translated_domains; LSA_TRANSLATED_SID *translated_sid; NTSTATUS status = LsaLookupNames(*policy, 1, &lsa_username, &translated_domains, &translated_sid); -#ifndef UNICODE HeapFree(GetProcessHeap(), 0, lsa_username.Buffer); -#endif - HeapFree(GetProcessHeap(), 0, expanded); if (policy == &handle) LsaClose(handle); if (status != STATUS_SUCCESS) { LsaFreeMemory(translated_domains); @@ -143,7 +131,7 @@ int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) { else *sub = translated_sid->RelativeId; } - int ret = 0; + ret = 0; if (translated_sid->Use == SidTypeWellKnownGroup && ! well_known_sid(*sid)) { print_message(stderr, NSSM_GUI_INVALID_USERNAME, username); ret = 10; @@ -194,21 +182,14 @@ int canonicalise_username(const TCHAR *username, TCHAR **canon) { memmove((char *) lsa_canon.Buffer + trust->Name.Length, L"\\", sizeof(wchar_t)); memmove((char *) lsa_canon.Buffer + trust->Name.Length + sizeof(wchar_t), translated_name->Name.Buffer, translated_name->Name.Length); -#ifdef UNICODE - *canon = lsa_canon.Buffer; -#else - size_t buflen; - wcstombs_s(&buflen, NULL, 0, lsa_canon.Buffer, _TRUNCATE); - *canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen); - if (! *canon) { + unsigned long canonlen; + if (from_utf16(lsa_canon.Buffer, canon, &canonlen)) { LsaFreeMemory(translated_domains); LsaFreeMemory(translated_name); print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("username_sid")); return 10; } - wcstombs_s(&buflen, *canon, buflen, lsa_canon.Buffer, _TRUNCATE); HeapFree(GetProcessHeap(), 0, lsa_canon.Buffer); -#endif LsaFreeMemory(translated_domains); LsaFreeMemory(translated_name); diff --git a/imports.cpp b/imports.cpp index f50591c..18d84f8 100644 --- a/imports.cpp +++ b/imports.cpp @@ -29,18 +29,10 @@ FARPROC get_import(HMODULE library, const char *function, unsigned long *error) *error = GetLastError(); if (*error != ERROR_PROC_NOT_FOUND) { TCHAR *function_name; -#ifdef UNICODE - size_t buflen; - mbstowcs_s(&buflen, NULL, 0, function, _TRUNCATE); - function_name = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(TCHAR)); - if (function_name) mbstowcs_s(&buflen, function_name, buflen * sizeof(TCHAR), function, _TRUNCATE); -#else - function_name = (TCHAR *) function; -#endif - log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_GETPROCADDRESS_FAILED, function_name, error_string(*error), 0); -#ifdef UNICODE - if (function_name) HeapFree(GetProcessHeap(), 0, function_name); -#endif + if (! from_utf8(function, &function_name, 0)) { + log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_GETPROCADDRESS_FAILED, function_name, error_string(*error), 0); + HeapFree(GetProcessHeap(), 0, function_name); + } } }