diff options
author | Noel Grandin <noel@peralex.com> | 2021-04-16 11:52:37 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-04-16 14:50:28 +0200 |
commit | 8b127d938d852d0ec3590cc05ee2d5b50d8e70b7 (patch) | |
tree | 49f947e84cd1b53a730e32392d033850b80d3822 | |
parent | aef98f20529f8702579916d68c46cd2129e205cb (diff) |
flatten some code in sal/osl/w32
Change-Id: Ibff36560f0fc49155dc5e8fe587b8f3a41e79516
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114201
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | sal/osl/w32/module.cxx | 58 | ||||
-rw-r--r-- | sal/osl/w32/nlsupport.cxx | 105 | ||||
-rw-r--r-- | sal/osl/w32/pipe.cxx | 32 | ||||
-rw-r--r-- | sal/osl/w32/security.cxx | 552 | ||||
-rw-r--r-- | sal/osl/w32/socket.cxx | 91 | ||||
-rw-r--r-- | sal/osl/w32/time.cxx | 55 |
6 files changed, 435 insertions, 458 deletions
diff --git a/sal/osl/w32/module.cxx b/sal/osl/w32/module.cxx index 67eeb2df0f6d..620be0e6d664 100644 --- a/sal/osl/w32/module.cxx +++ b/sal/osl/w32/module.cxx @@ -182,49 +182,49 @@ osl_getAsciiFunctionSymbol( oslModule Module, const char *pSymbol ) sal_Bool SAL_CALL osl_getModuleURLFromAddress( void *pv, rtl_uString **pustrURL ) { - bool bSuccess = false; /* Assume failure */ static HMODULE hModPsapi = LoadLibraryW( L"PSAPI.DLL" ); static auto lpfnEnumProcessModules = reinterpret_cast<decltype(EnumProcessModules)*>( hModPsapi ? GetProcAddress(hModPsapi, "EnumProcessModules") : nullptr); static auto lpfnGetModuleInformation = reinterpret_cast<decltype(GetModuleInformation)*>( hModPsapi ? GetProcAddress(hModPsapi, "GetModuleInformation") : nullptr); - if (lpfnEnumProcessModules && lpfnGetModuleInformation) - { - DWORD cbNeeded = 0; - HMODULE* lpModules = nullptr; - DWORD nModules = 0; - UINT iModule = 0; - MODULEINFO modinfo; + if (!lpfnEnumProcessModules || !lpfnGetModuleInformation) + return false; - lpfnEnumProcessModules(GetCurrentProcess(), nullptr, 0, &cbNeeded); + bool bSuccess = false; /* Assume failure */ + DWORD cbNeeded = 0; + HMODULE* lpModules = nullptr; + DWORD nModules = 0; + UINT iModule = 0; + MODULEINFO modinfo; - lpModules = static_cast<HMODULE*>(_alloca(cbNeeded)); - lpfnEnumProcessModules(GetCurrentProcess(), lpModules, cbNeeded, &cbNeeded); + lpfnEnumProcessModules(GetCurrentProcess(), nullptr, 0, &cbNeeded); - nModules = cbNeeded / sizeof(HMODULE); + lpModules = static_cast<HMODULE*>(_alloca(cbNeeded)); + lpfnEnumProcessModules(GetCurrentProcess(), lpModules, cbNeeded, &cbNeeded); - for (iModule = 0; !bSuccess && iModule < nModules; iModule++) - { - lpfnGetModuleInformation(GetCurrentProcess(), lpModules[iModule], &modinfo, - sizeof(modinfo)); + nModules = cbNeeded / sizeof(HMODULE); - if (static_cast<BYTE*>(pv) >= static_cast<BYTE*>(modinfo.lpBaseOfDll) - && static_cast<BYTE*>(pv) - < static_cast<BYTE*>(modinfo.lpBaseOfDll) + modinfo.SizeOfImage) - { - ::osl::LongPathBuffer<sal_Unicode> aBuffer(MAX_LONG_PATH); - rtl_uString* ustrSysPath = nullptr; + for (iModule = 0; !bSuccess && iModule < nModules; iModule++) + { + lpfnGetModuleInformation(GetCurrentProcess(), lpModules[iModule], &modinfo, + sizeof(modinfo)); + + if (static_cast<BYTE*>(pv) >= static_cast<BYTE*>(modinfo.lpBaseOfDll) + && static_cast<BYTE*>(pv) + < static_cast<BYTE*>(modinfo.lpBaseOfDll) + modinfo.SizeOfImage) + { + ::osl::LongPathBuffer<sal_Unicode> aBuffer(MAX_LONG_PATH); + rtl_uString* ustrSysPath = nullptr; - GetModuleFileNameW(lpModules[iModule], o3tl::toW(aBuffer), - aBuffer.getBufSizeInSymbols()); + GetModuleFileNameW(lpModules[iModule], o3tl::toW(aBuffer), + aBuffer.getBufSizeInSymbols()); - rtl_uString_newFromStr(&ustrSysPath, aBuffer); - osl_getFileURLFromSystemPath(ustrSysPath, pustrURL); - rtl_uString_release(ustrSysPath); + rtl_uString_newFromStr(&ustrSysPath, aBuffer); + osl_getFileURLFromSystemPath(ustrSysPath, pustrURL); + rtl_uString_release(ustrSysPath); - bSuccess = true; - } + bSuccess = true; } } diff --git a/sal/osl/w32/nlsupport.cxx b/sal/osl/w32/nlsupport.cxx index e253cc0cb7e8..f27b97aa7101 100644 --- a/sal/osl/w32/nlsupport.cxx +++ b/sal/osl/w32/nlsupport.cxx @@ -77,38 +77,39 @@ static BOOL CALLBACK EnumLocalesProcW( LPWSTR lpLocaleStringW ) /* get the ISO language code for this locale */ - if( GetLocaleInfoW( localeId, LOCALE_SISO639LANGNAME , langCode, ELP_LANGUAGE_FIELD_LENGTH ) ) - { - WCHAR ctryCode[ELP_COUNTRY_FIELD_LENGTH]; + if( !GetLocaleInfoW( localeId, LOCALE_SISO639LANGNAME , langCode, ELP_LANGUAGE_FIELD_LENGTH ) ) + /* retry by going on */ + return TRUE; + + WCHAR ctryCode[ELP_COUNTRY_FIELD_LENGTH]; - /* continue if language code does not match */ - if( 0 != wcscmp( langCode, params->Language ) ) - return TRUE; + /* continue if language code does not match */ + if( 0 != wcscmp( langCode, params->Language ) ) + return TRUE; - /* check if country code is set and equals the current locale */ - if( '\0' != params->Country[0] && GetLocaleInfoW( localeId, - LOCALE_SISO3166CTRYNAME , ctryCode, ELP_COUNTRY_FIELD_LENGTH ) ) + /* check if country code is set and equals the current locale */ + if( '\0' != params->Country[0] && GetLocaleInfoW( localeId, + LOCALE_SISO3166CTRYNAME , ctryCode, ELP_COUNTRY_FIELD_LENGTH ) ) + { + /* save return value in TLS and break if found desired locale */ + if( 0 == wcscmp( ctryCode, params->Country ) ) { - /* save return value in TLS and break if found desired locale */ - if( 0 == wcscmp( ctryCode, params->Country ) ) - { - params->Locale = localeId; - return FALSE; - } + params->Locale = localeId; + return FALSE; } - else - { - /* fill with default values for that language */ - LANGID langId = LANGIDFROMLCID( localeId ); + } + else + { + /* fill with default values for that language */ + LANGID langId = LANGIDFROMLCID( localeId ); - /* exchange sublanguage with SUBLANG_NEUTRAL */ - langId = MAKELANGID( PRIMARYLANGID( langId ), SUBLANG_NEUTRAL ); + /* exchange sublanguage with SUBLANG_NEUTRAL */ + langId = MAKELANGID( PRIMARYLANGID( langId ), SUBLANG_NEUTRAL ); - /* and use default sorting order */ - params->Locale = MAKELCID( langId, SORT_DEFAULT ); + /* and use default sorting order */ + params->Locale = MAKELCID( langId, SORT_DEFAULT ); - return FALSE; - } + return FALSE; } /* retry by going on */ @@ -117,29 +118,21 @@ static BOOL CALLBACK EnumLocalesProcW( LPWSTR lpLocaleStringW ) static rtl_TextEncoding GetTextEncodingFromLCID( LCID localeId ) { - rtl_TextEncoding Encoding = RTL_TEXTENCODING_DONTKNOW; - WCHAR ansiCP[6]; - /* query ansi codepage for given locale */ - if( localeId && GetLocaleInfoW( localeId, LOCALE_IDEFAULTANSICODEPAGE, ansiCP, 6 ) ) - { - /* if GetLocaleInfo returns "0", it is a UNICODE only locale */ - if( 0 != wcscmp( ansiCP, L"0" ) ) - { - WCHAR *pwcEnd; - UINT codepage; + WCHAR ansiCP[6]; + if( !localeId || !GetLocaleInfoW( localeId, LOCALE_IDEFAULTANSICODEPAGE, ansiCP, 6 ) ) + return RTL_TEXTENCODING_DONTKNOW; - /* values returned from GetLocaleInfo are decimal based */ - codepage = wcstol( ansiCP, &pwcEnd, 10 ); + /* if GetLocaleInfo returns "0", it is a UNICODE only locale */ + if( 0 == wcscmp( ansiCP, L"0" ) ) + return RTL_TEXTENCODING_UNICODE; - /* find matching rtl encoding */ - Encoding = rtl_getTextEncodingFromWindowsCodePage( codepage ); - } - else - Encoding = RTL_TEXTENCODING_UNICODE; - } + /* values returned from GetLocaleInfo are decimal based */ + WCHAR *pwcEnd; + UINT codepage = wcstol( ansiCP, &pwcEnd, 10 ); - return Encoding; + /* find matching rtl encoding */ + return rtl_getTextEncodingFromWindowsCodePage( codepage ); } rtl_TextEncoding SAL_CALL osl_getTextEncodingFromLocale( rtl_Locale * pLocale ) @@ -165,24 +158,22 @@ rtl_TextEncoding SAL_CALL osl_getTextEncodingFromLocale( rtl_Locale * pLocale ) osl_getProcessLocale( &pLocale ); /* copy in parameters to structure */ - if( pLocale && pLocale->Language && pLocale->Language->length < ELP_LANGUAGE_FIELD_LENGTH ) - { - wcscpy( params.Language, o3tl::toW(pLocale->Language->buffer) ); + if( !pLocale || !pLocale->Language || pLocale->Language->length >= ELP_LANGUAGE_FIELD_LENGTH ) + return RTL_TEXTENCODING_DONTKNOW; - if( pLocale->Country && pLocale->Country->length < ELP_COUNTRY_FIELD_LENGTH ) - wcscpy( params.Country, o3tl::toW(pLocale->Country->buffer) ); + wcscpy( params.Language, o3tl::toW(pLocale->Language->buffer) ); - /* save pointer to local structure in TLS */ - TlsSetValue( g_dwTLSLocaleEncId, ¶ms ); + if( pLocale->Country && pLocale->Country->length < ELP_COUNTRY_FIELD_LENGTH ) + wcscpy( params.Country, o3tl::toW(pLocale->Country->buffer) ); - /* enum all locales known to Windows */ - EnumSystemLocalesW( EnumLocalesProcW, LCID_SUPPORTED ); + /* save pointer to local structure in TLS */ + TlsSetValue( g_dwTLSLocaleEncId, ¶ms ); - /* use the LCID found in iteration */ - return GetTextEncodingFromLCID( params.Locale ); - } + /* enum all locales known to Windows */ + EnumSystemLocalesW( EnumLocalesProcW, LCID_SUPPORTED ); - return RTL_TEXTENCODING_DONTKNOW; + /* use the LCID found in iteration */ + return GetTextEncodingFromLCID( params.Locale ); } void imp_getProcessLocale( rtl_Locale ** ppLocale ) diff --git a/sal/osl/w32/pipe.cxx b/sal/osl/w32/pipe.cxx index 3e71ef1d44d2..c94441e6362a 100644 --- a/sal/osl/w32/pipe.cxx +++ b/sal/osl/w32/pipe.cxx @@ -71,26 +71,26 @@ static oslPipe osl_createPipeImpl(void) static void osl_destroyPipeImpl(oslPipe pPipe) { - if (pPipe) - { - if (pPipe->m_NamedObject) - CloseHandle(pPipe->m_NamedObject); + if (!pPipe) + return; - if (pPipe->m_Security) - { - free(pPipe->m_Security->lpSecurityDescriptor); - free(pPipe->m_Security); - } + if (pPipe->m_NamedObject) + CloseHandle(pPipe->m_NamedObject); - CloseHandle(pPipe->m_ReadEvent); - CloseHandle(pPipe->m_WriteEvent); - CloseHandle(pPipe->m_AcceptEvent); + if (pPipe->m_Security) + { + free(pPipe->m_Security->lpSecurityDescriptor); + free(pPipe->m_Security); + } - if (pPipe->m_Name) - rtl_uString_release(pPipe->m_Name); + CloseHandle(pPipe->m_ReadEvent); + CloseHandle(pPipe->m_WriteEvent); + CloseHandle(pPipe->m_AcceptEvent); - free(pPipe); - } + if (pPipe->m_Name) + rtl_uString_release(pPipe->m_Name); + + free(pPipe); } oslPipe SAL_CALL osl_createPipe(rtl_uString *strPipeName, oslPipeOptions Options, diff --git a/sal/osl/w32/security.cxx b/sal/osl/w32/security.cxx index 75485820716f..21ed64a7862a 100644 --- a/sal/osl/w32/security.cxx +++ b/sal/osl/w32/security.cxx @@ -176,110 +176,100 @@ oslSecurityError SAL_CALL osl_loginUserOnFileServer(rtl_uString *strUserName, sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security) { - if (Security != nullptr) - { - HANDLE hImpersonationToken = nullptr; - PSID psidAdministrators; - SID_IDENTIFIER_AUTHORITY siaNtAuthority = { SECURITY_NT_AUTHORITY }; - bool bSuccess = false; - - /* If Security contains an access token we need to duplicate it to an impersonation - access token. NULL works with CheckTokenMembership() as the current effective - impersonation token - */ + if (!Security) + return false; - if ( static_cast<oslSecurityImpl*>(Security)->m_hToken ) - { - if ( !DuplicateToken (static_cast<oslSecurityImpl*>(Security)->m_hToken, SecurityImpersonation, &hImpersonationToken) ) - return false; - } + HANDLE hImpersonationToken = nullptr; + PSID psidAdministrators; + SID_IDENTIFIER_AUTHORITY siaNtAuthority = { SECURITY_NT_AUTHORITY }; + bool bSuccess = false; - /* CheckTokenMembership() can be used on W2K and higher (NT4 no longer supported by OOo) - and also works on Vista to retrieve the effective user rights. Just checking for - membership of Administrators group is not enough on Vista this would require additional - complicated checks as described in KB article Q118626: http://support.microsoft.com/kb/118626/en-us - */ + /* If Security contains an access token we need to duplicate it to an impersonation + access token. NULL works with CheckTokenMembership() as the current effective + impersonation token + */ - if (AllocateAndInitializeSid(&siaNtAuthority, - 2, - SECURITY_BUILTIN_DOMAIN_RID, - DOMAIN_ALIAS_RID_ADMINS, - 0, 0, 0, 0, 0, 0, - &psidAdministrators)) - { - BOOL fSuccess = FALSE; + if ( static_cast<oslSecurityImpl*>(Security)->m_hToken ) + { + if ( !DuplicateToken (static_cast<oslSecurityImpl*>(Security)->m_hToken, SecurityImpersonation, &hImpersonationToken) ) + return false; + } - if (CheckTokenMembership(hImpersonationToken, psidAdministrators, &fSuccess) && fSuccess) - bSuccess = true; + /* CheckTokenMembership() can be used on W2K and higher (NT4 no longer supported by OOo) + and also works on Vista to retrieve the effective user rights. Just checking for + membership of Administrators group is not enough on Vista this would require additional + complicated checks as described in KB article Q118626: http://support.microsoft.com/kb/118626/en-us + */ - FreeSid(psidAdministrators); - } + if (AllocateAndInitializeSid(&siaNtAuthority, + 2, + SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, + 0, 0, 0, 0, 0, 0, + &psidAdministrators)) + { + BOOL fSuccess = FALSE; - if (hImpersonationToken) - CloseHandle(hImpersonationToken); + if (CheckTokenMembership(hImpersonationToken, psidAdministrators, &fSuccess) && fSuccess) + bSuccess = true; - return bSuccess; - } - else - { - return false; + FreeSid(psidAdministrators); } + + if (hImpersonationToken) + CloseHandle(hImpersonationToken); + + return bSuccess; } void SAL_CALL osl_freeSecurityHandle(oslSecurity Security) { - if (Security) - { - oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); + if (!Security) + return; - if (pSecImpl->m_pNetResource != nullptr) - { - WNetCancelConnection2W(pSecImpl->m_pNetResource->lpRemoteName, 0, true); + oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); - free(pSecImpl->m_pNetResource->lpRemoteName); - free(pSecImpl->m_pNetResource); - } + if (pSecImpl->m_pNetResource != nullptr) + { + WNetCancelConnection2W(pSecImpl->m_pNetResource->lpRemoteName, 0, true); - if (pSecImpl->m_hToken) - CloseHandle(pSecImpl->m_hToken); + free(pSecImpl->m_pNetResource->lpRemoteName); + free(pSecImpl->m_pNetResource); + } - if ( pSecImpl->m_hProfile ) - CloseHandle(pSecImpl->m_hProfile); + if (pSecImpl->m_hToken) + CloseHandle(pSecImpl->m_hToken); - free (pSecImpl); - } + if ( pSecImpl->m_hProfile ) + CloseHandle(pSecImpl->m_hProfile); + + free (pSecImpl); } sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **strIdent) { - if (Security != nullptr) - { - oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); + if (!Security) + return false; - HANDLE hAccessToken = pSecImpl->m_hToken; + oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); - if (hAccessToken == nullptr) - OpenProcessToken(GetCurrentProcess(), TOKEN_DUP_QUERY, &hAccessToken); + HANDLE hAccessToken = pSecImpl->m_hToken; - if (hAccessToken) - { - DWORD nInfoBuffer = 512; - UCHAR* pInfoBuffer = static_cast<UCHAR *>(malloc(nInfoBuffer)); + if (hAccessToken == nullptr) + OpenProcessToken(GetCurrentProcess(), TOKEN_DUP_QUERY, &hAccessToken); + + if (hAccessToken) + { + DWORD nInfoBuffer = 512; + UCHAR* pInfoBuffer = static_cast<UCHAR *>(malloc(nInfoBuffer)); - while (!GetTokenInformation(hAccessToken, TokenUser, - pInfoBuffer, nInfoBuffer, &nInfoBuffer)) + while (!GetTokenInformation(hAccessToken, TokenUser, + pInfoBuffer, nInfoBuffer, &nInfoBuffer)) + { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) - { - if (auto p = static_cast<UCHAR *>(realloc(pInfoBuffer, nInfoBuffer))) - pInfoBuffer = p; - else - { - free(pInfoBuffer); - pInfoBuffer = nullptr; - break; - } - } + if (auto p = static_cast<UCHAR *>(realloc(pInfoBuffer, nInfoBuffer))) + pInfoBuffer = p; else { free(pInfoBuffer); @@ -287,57 +277,62 @@ sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **strIdent) break; } } - - if (pSecImpl->m_hToken == nullptr) - CloseHandle(hAccessToken); - - if (pInfoBuffer) + else { - PSID pSid = reinterpret_cast<PTOKEN_USER>(pInfoBuffer)->User.Sid; + free(pInfoBuffer); + pInfoBuffer = nullptr; + break; + } + } - LPWSTR pSidStr = nullptr; - bool bResult = ConvertSidToStringSidW(pSid, &pSidStr); - if (bResult) - { - rtl_uString_newFromStr(strIdent, o3tl::toU(pSidStr)); - LocalFree(pSidStr); - } - else - { - const DWORD dwError = GetLastError(); - SAL_WARN( - "sal.osl", - "ConvertSidToStringSidW failed. GetLastError returned: " << dwError); - } + if (pSecImpl->m_hToken == nullptr) + CloseHandle(hAccessToken); - free(pInfoBuffer); + if (pInfoBuffer) + { + PSID pSid = reinterpret_cast<PTOKEN_USER>(pInfoBuffer)->User.Sid; - return bResult; + LPWSTR pSidStr = nullptr; + bool bResult = ConvertSidToStringSidW(pSid, &pSidStr); + if (bResult) + { + rtl_uString_newFromStr(strIdent, o3tl::toU(pSidStr)); + LocalFree(pSidStr); + } + else + { + const DWORD dwError = GetLastError(); + SAL_WARN( + "sal.osl", + "ConvertSidToStringSidW failed. GetLastError returned: " << dwError); } + + free(pInfoBuffer); + + return bResult; } - else - { - DWORD needed = 0; + } + else + { + DWORD needed = 0; - WNetGetUserW(nullptr, nullptr, &needed); - if (needed < 16) - needed = 16; + WNetGetUserW(nullptr, nullptr, &needed); + if (needed < 16) + needed = 16; - if (auto Ident = static_cast<sal_Unicode *>(malloc(needed*sizeof(sal_Unicode)))) + if (auto Ident = static_cast<sal_Unicode *>(malloc(needed*sizeof(sal_Unicode)))) + { + if (WNetGetUserW(nullptr, o3tl::toW(Ident), &needed) != NO_ERROR) { - if (WNetGetUserW(nullptr, o3tl::toW(Ident), &needed) != NO_ERROR) - { - wcscpy(o3tl::toW(Ident), L"unknown"); - Ident[7] = L'\0'; - } - - rtl_uString_newFromStr( strIdent, Ident); - free(Ident); - return true; + wcscpy(o3tl::toW(Ident), L"unknown"); + Ident[7] = L'\0'; } + + rtl_uString_newFromStr( strIdent, Ident); + free(Ident); + return true; } } - return false; } @@ -353,24 +348,24 @@ sal_Bool SAL_CALL osl_getShortUserName(oslSecurity Security, rtl_uString **strNa sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory) { + if (!Security) + return false; + rtl_uString *ustrSysDir = nullptr; - bool bSuccess = false; + bool bSuccess = false; - if (Security != nullptr) - { - oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); + oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); - if (pSecImpl->m_pNetResource != nullptr) - { - rtl_uString_newFromStr( &ustrSysDir, o3tl::toU(pSecImpl->m_pNetResource->lpRemoteName)); + if (pSecImpl->m_pNetResource != nullptr) + { + rtl_uString_newFromStr( &ustrSysDir, o3tl::toU(pSecImpl->m_pNetResource->lpRemoteName)); - bSuccess = osl_File_E_None == osl_getFileURLFromSystemPath( ustrSysDir, pustrDirectory ); - } - else - { - bSuccess = GetSpecialFolder(&ustrSysDir, FOLDERID_Documents) && - (osl_File_E_None == osl_getFileURLFromSystemPath(ustrSysDir, pustrDirectory)); - } + bSuccess = osl_File_E_None == osl_getFileURLFromSystemPath( ustrSysDir, pustrDirectory ); + } + else + { + bSuccess = GetSpecialFolder(&ustrSysDir, FOLDERID_Documents) && + (osl_File_E_None == osl_getFileURLFromSystemPath(ustrSysDir, pustrDirectory)); } if ( ustrSysDir ) @@ -381,46 +376,45 @@ sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirect sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory) { + if (!Security) + return false; + bool bSuccess = false; + oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); - if (Security != nullptr) + if (pSecImpl->m_pNetResource != nullptr) { - oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); + rtl_uString *ustrSysDir = nullptr; - if (pSecImpl->m_pNetResource != nullptr) - { - rtl_uString *ustrSysDir = nullptr; - - rtl_uString_newFromStr( &ustrSysDir, o3tl::toU(pSecImpl->m_pNetResource->lpRemoteName)); - bSuccess = osl_File_E_None == osl_getFileURLFromSystemPath( ustrSysDir, pustrDirectory); + rtl_uString_newFromStr( &ustrSysDir, o3tl::toU(pSecImpl->m_pNetResource->lpRemoteName)); + bSuccess = osl_File_E_None == osl_getFileURLFromSystemPath( ustrSysDir, pustrDirectory); - if ( ustrSysDir ) - rtl_uString_release( ustrSysDir ); + if ( ustrSysDir ) + rtl_uString_release( ustrSysDir ); + } + else + { + if (pSecImpl->m_hToken) + { + /* not implemented */ + OSL_ASSERT(false); } else { - if (pSecImpl->m_hToken) - { - /* not implemented */ - OSL_ASSERT(false); - } - else - { - rtl_uString *ustrFile = nullptr; - sal_Unicode sFile[_MAX_PATH]; + rtl_uString *ustrFile = nullptr; + sal_Unicode sFile[_MAX_PATH]; - if ( !GetSpecialFolder( &ustrFile, FOLDERID_RoamingAppData) ) - { - OSL_VERIFY(GetWindowsDirectoryW(o3tl::toW(sFile), _MAX_DIR) > 0); + if ( !GetSpecialFolder( &ustrFile, FOLDERID_RoamingAppData) ) + { + OSL_VERIFY(GetWindowsDirectoryW(o3tl::toW(sFile), _MAX_DIR) > 0); - rtl_uString_newFromStr( &ustrFile, sFile); - } + rtl_uString_newFromStr( &ustrFile, sFile); + } - bSuccess = osl_File_E_None == osl_getFileURLFromSystemPath(ustrFile, pustrDirectory); + bSuccess = osl_File_E_None == osl_getFileURLFromSystemPath(ustrFile, pustrDirectory); - if ( ustrFile ) - rtl_uString_release( ustrFile ); - } + if ( ustrFile ) + rtl_uString_release( ustrFile ); } } @@ -436,81 +430,81 @@ sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security) It is your responsibility to load the user's registry hive into HKEY_USERS with the LoadUserProfile function before calling CreateProcessAsUser. */ - bool bOk = false; RegCloseKey(HKEY_CURRENT_USER); - if (Privilege(SE_RESTORE_NAME, true)) + if (!Privilege(SE_RESTORE_NAME, true)) + return false; + + bool bOk = false; + HANDLE hAccessToken = static_cast<oslSecurityImpl*>(Security)->m_hToken; + + /* try to create user profile */ + if ( !hAccessToken ) { - HANDLE hAccessToken = static_cast<oslSecurityImpl*>(Security)->m_hToken; + /* retrieve security handle if not done before e.g. osl_getCurrentSecurity() + */ + HANDLE hProcess = GetCurrentProcess(); - /* try to create user profile */ - if ( !hAccessToken ) + if (hProcess != nullptr) { - /* retrieve security handle if not done before e.g. osl_getCurrentSecurity() - */ - HANDLE hProcess = GetCurrentProcess(); - - if (hProcess != nullptr) - { - OpenProcessToken(hProcess, TOKEN_IMPERSONATE, &hAccessToken); - CloseHandle(hProcess); - } + OpenProcessToken(hProcess, TOKEN_IMPERSONATE, &hAccessToken); + CloseHandle(hProcess); } + } - rtl_uString *buffer = nullptr; - PROFILEINFOW pi; + rtl_uString *buffer = nullptr; + PROFILEINFOW pi; - getUserNameImpl(Security, &buffer, false); + getUserNameImpl(Security, &buffer, false); - ZeroMemory(&pi, sizeof(pi)); - pi.dwSize = sizeof(pi); - pi.lpUserName = o3tl::toW(rtl_uString_getStr(buffer)); - pi.dwFlags = PI_NOUI; + ZeroMemory(&pi, sizeof(pi)); + pi.dwSize = sizeof(pi); + pi.lpUserName = o3tl::toW(rtl_uString_getStr(buffer)); + pi.dwFlags = PI_NOUI; - if (LoadUserProfileW(hAccessToken, &pi)) - { - UnloadUserProfile(hAccessToken, pi.hProfile); + if (LoadUserProfileW(hAccessToken, &pi)) + { + UnloadUserProfile(hAccessToken, pi.hProfile); - bOk = true; - } + bOk = true; + } - rtl_uString_release(buffer); + rtl_uString_release(buffer); - if (hAccessToken && (hAccessToken != static_cast<oslSecurityImpl*>(Security)->m_hToken)) - CloseHandle(hAccessToken); - } + if (hAccessToken && (hAccessToken != static_cast<oslSecurityImpl*>(Security)->m_hToken)) + CloseHandle(hAccessToken); return bOk; } void SAL_CALL osl_unloadUserProfile(oslSecurity Security) { - if ( static_cast<oslSecurityImpl*>(Security)->m_hProfile != nullptr ) + if ( static_cast<oslSecurityImpl*>(Security)->m_hProfile == nullptr ) + return; + + HANDLE hAccessToken = static_cast<oslSecurityImpl*>(Security)->m_hToken; + + if ( !hAccessToken ) { - HANDLE hAccessToken = static_cast<oslSecurityImpl*>(Security)->m_hToken; + /* retrieve security handle if not done before e.g. osl_getCurrentSecurity() + */ + HANDLE hProcess = GetCurrentProcess(); - if ( !hAccessToken ) + if (hProcess != nullptr) { - /* retrieve security handle if not done before e.g. osl_getCurrentSecurity() - */ - HANDLE hProcess = GetCurrentProcess(); - - if (hProcess != nullptr) - { - OpenProcessToken(hProcess, TOKEN_IMPERSONATE, &hAccessToken); - CloseHandle(hProcess); - } + OpenProcessToken(hProcess, TOKEN_IMPERSONATE, &hAccessToken); + CloseHandle(hProcess); } + } - /* unloading the user profile */ - UnloadUserProfile(hAccessToken, static_cast<oslSecurityImpl*>(Security)->m_hProfile); + /* unloading the user profile */ + UnloadUserProfile(hAccessToken, static_cast<oslSecurityImpl*>(Security)->m_hProfile); - static_cast<oslSecurityImpl*>(Security)->m_hProfile = nullptr; + static_cast<oslSecurityImpl*>(Security)->m_hProfile = nullptr; - if (hAccessToken && (hAccessToken != static_cast<oslSecurityImpl*>(Security)->m_hToken)) - CloseHandle(hAccessToken); - } + if (hAccessToken && (hAccessToken != static_cast<oslSecurityImpl*>(Security)->m_hToken)) + CloseHandle(hAccessToken); } static bool GetSpecialFolder(rtl_uString **strPath, REFKNOWNFOLDERID rFolder) @@ -561,34 +555,28 @@ static bool Privilege(LPCTSTR strPrivilege, bool bEnable) static bool getUserNameImpl(oslSecurity Security, rtl_uString **strName, bool bIncludeDomain) { - if (Security != nullptr) - { - oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); + if (!Security) + return false; - HANDLE hAccessToken = pSecImpl->m_hToken; + oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl*>(Security); - if (hAccessToken == nullptr) - OpenProcessToken(GetCurrentProcess(), TOKEN_DUP_QUERY, &hAccessToken); + HANDLE hAccessToken = pSecImpl->m_hToken; - if (hAccessToken) - { - DWORD nInfoBuffer = 512; - UCHAR* pInfoBuffer = static_cast<UCHAR *>(malloc(nInfoBuffer)); + if (hAccessToken == nullptr) + OpenProcessToken(GetCurrentProcess(), TOKEN_DUP_QUERY, &hAccessToken); + + if (hAccessToken) + { + DWORD nInfoBuffer = 512; + UCHAR* pInfoBuffer = static_cast<UCHAR *>(malloc(nInfoBuffer)); - while (!GetTokenInformation(hAccessToken, TokenUser, - pInfoBuffer, nInfoBuffer, &nInfoBuffer)) + while (!GetTokenInformation(hAccessToken, TokenUser, + pInfoBuffer, nInfoBuffer, &nInfoBuffer)) + { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) - { - if (auto p = static_cast<UCHAR *>(realloc(pInfoBuffer, nInfoBuffer))) - pInfoBuffer = p; - else - { - free(pInfoBuffer); - pInfoBuffer = nullptr; - break; - } - } + if (auto p = static_cast<UCHAR *>(realloc(pInfoBuffer, nInfoBuffer))) + pInfoBuffer = p; else { free(pInfoBuffer); @@ -596,70 +584,76 @@ static bool getUserNameImpl(oslSecurity Security, rtl_uString **strName, bool b break; } } - - if (pSecImpl->m_hToken == nullptr) - CloseHandle(hAccessToken); - - if (pInfoBuffer) + else { - sal_Unicode UserName[128]; - sal_Unicode DomainName[128]; - sal_Unicode Name[257]; - DWORD nUserName = SAL_N_ELEMENTS(UserName); - DWORD nDomainName = SAL_N_ELEMENTS(DomainName); - SID_NAME_USE sUse; - - if (LookupAccountSidW(nullptr, reinterpret_cast<PTOKEN_USER>(pInfoBuffer)->User.Sid, - o3tl::toW(UserName), &nUserName, - o3tl::toW(DomainName), &nDomainName, &sUse)) - { - if (bIncludeDomain) - { - wcscpy(o3tl::toW(Name), o3tl::toW(DomainName)); - wcscat(o3tl::toW(Name), L"/"); - wcscat(o3tl::toW(Name), o3tl::toW(UserName)); - } - else - { - wcscpy(o3tl::toW(Name), o3tl::toW(UserName)); - } - - rtl_uString_newFromStr(strName, Name); - free(pInfoBuffer); - return true; - } + free(pInfoBuffer); + pInfoBuffer = nullptr; + break; } } - else - { - DWORD needed=0; - sal_Unicode *pNameW=nullptr; - WNetGetUserW(nullptr, nullptr, &needed); - pNameW = static_cast<sal_Unicode *>(malloc (needed*sizeof(sal_Unicode))); - assert(pNameW); // Don't handle OOM conditions + if (pSecImpl->m_hToken == nullptr) + CloseHandle(hAccessToken); - if (WNetGetUserW(nullptr, o3tl::toW(pNameW), &needed) == NO_ERROR) + if (pInfoBuffer) + { + sal_Unicode UserName[128]; + sal_Unicode DomainName[128]; + sal_Unicode Name[257]; + DWORD nUserName = SAL_N_ELEMENTS(UserName); + DWORD nDomainName = SAL_N_ELEMENTS(DomainName); + SID_NAME_USE sUse; + + if (LookupAccountSidW(nullptr, reinterpret_cast<PTOKEN_USER>(pInfoBuffer)->User.Sid, + o3tl::toW(UserName), &nUserName, + o3tl::toW(DomainName), &nDomainName, &sUse)) { - rtl_uString_newFromStr( strName, pNameW); + if (bIncludeDomain) + { + wcscpy(o3tl::toW(Name), o3tl::toW(DomainName)); + wcscat(o3tl::toW(Name), L"/"); + wcscat(o3tl::toW(Name), o3tl::toW(UserName)); + } + else + { + wcscpy(o3tl::toW(Name), o3tl::toW(UserName)); + } - if (pNameW) - free(pNameW); + rtl_uString_newFromStr(strName, Name); + free(pInfoBuffer); return true; } - else if (pSecImpl->m_User[0] != '\0') - { - rtl_uString_newFromStr(strName, o3tl::toU(pSecImpl->m_pNetResource->lpRemoteName)); + } + } + else + { + DWORD needed=0; + sal_Unicode *pNameW=nullptr; - if (pNameW) - free(pNameW); + WNetGetUserW(nullptr, nullptr, &needed); + pNameW = static_cast<sal_Unicode *>(malloc (needed*sizeof(sal_Unicode))); + assert(pNameW); // Don't handle OOM conditions - return true; - } + if (WNetGetUserW(nullptr, o3tl::toW(pNameW), &needed) == NO_ERROR) + { + rtl_uString_newFromStr( strName, pNameW); + + if (pNameW) + free(pNameW); + return true; + } + else if (pSecImpl->m_User[0] != '\0') + { + rtl_uString_newFromStr(strName, o3tl::toU(pSecImpl->m_pNetResource->lpRemoteName)); if (pNameW) free(pNameW); + + return true; } + + if (pNameW) + free(pNameW); } return false; diff --git a/sal/osl/w32/socket.cxx b/sal/osl/w32/socket.cxx index 9e819beaa61f..3ac6eacc9602 100644 --- a/sal/osl/w32/socket.cxx +++ b/sal/osl/w32/socket.cxx @@ -503,34 +503,32 @@ oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *strHostname) PADDRINFOW pAddrInfo = nullptr; int ret = GetAddrInfoW( o3tl::toW(strHostname->buffer), nullptr, nullptr, & pAddrInfo); - if (0 == ret) + if (0 != ret) { - oslHostAddr pRet = nullptr; - for (PADDRINFOW pIter = pAddrInfo; pIter; pIter = pIter->ai_next) + SAL_INFO("sal.osl", "GetAddrInfoW failed: " << WSAGetLastError()); + return nullptr; + } + + oslHostAddr pRet = nullptr; + for (PADDRINFOW pIter = pAddrInfo; pIter; pIter = pIter->ai_next) + { + if (AF_INET == pIter->ai_family) { - if (AF_INET == pIter->ai_family) - { - pRet = static_cast<oslHostAddr>( - rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl))); - if (pIter->ai_canonname == nullptr) { - rtl_uString_new(&pRet->pHostName); - } else { - rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(pIter->ai_canonname)); - } - pRet->pSockAddr = createSocketAddr(); - memcpy(& pRet->pSockAddr->m_sockaddr, - pIter->ai_addr, pIter->ai_addrlen); - break; // ignore other results + pRet = static_cast<oslHostAddr>( + rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl))); + if (pIter->ai_canonname == nullptr) { + rtl_uString_new(&pRet->pHostName); + } else { + rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(pIter->ai_canonname)); } + pRet->pSockAddr = createSocketAddr(); + memcpy(& pRet->pSockAddr->m_sockaddr, + pIter->ai_addr, pIter->ai_addrlen); + break; // ignore other results } - FreeAddrInfoW(pAddrInfo); - return pRet; } - else - { - SAL_INFO("sal.osl", "GetAddrInfoW failed: " << WSAGetLastError()); - } - return nullptr; + FreeAddrInfoW(pAddrInfo); + return pRet; } oslHostAddr SAL_CALL osl_createHostAddrByAddr(const oslSocketAddr pAddr) @@ -538,35 +536,32 @@ oslHostAddr SAL_CALL osl_createHostAddrByAddr(const oslSocketAddr pAddr) if (pAddr == nullptr) return nullptr; - if (pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)) - { - const struct sockaddr_in *sin= reinterpret_cast<const struct sockaddr_in *>(&pAddr->m_sockaddr); + if (pAddr->m_sockaddr.sa_family != FAMILY_TO_NATIVE(osl_Socket_FamilyInet)) + return nullptr; - if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) - return nullptr; + const struct sockaddr_in *sin= reinterpret_cast<const struct sockaddr_in *>(&pAddr->m_sockaddr); - WCHAR buf[NI_MAXHOST]; - int ret = GetNameInfoW( - & pAddr->m_sockaddr, sizeof(struct sockaddr), - buf, NI_MAXHOST, - nullptr, 0, 0); - if (0 == ret) - { - oslHostAddr pRet = static_cast<oslHostAddr>( - rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl))); - rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(buf)); - pRet->pSockAddr = createSocketAddr(); - memcpy(& pRet->pSockAddr->m_sockaddr, - & pAddr->m_sockaddr, sizeof(struct sockaddr)); - return pRet; - } - else - { - SAL_INFO("sal.osl", "GetNameInfoW failed: " << WSAGetLastError()); - } + if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) + return nullptr; + + WCHAR buf[NI_MAXHOST]; + int ret = GetNameInfoW( + & pAddr->m_sockaddr, sizeof(struct sockaddr), + buf, NI_MAXHOST, + nullptr, 0, 0); + if (0 != ret) + { + SAL_INFO("sal.osl", "GetNameInfoW failed: " << WSAGetLastError()); + return nullptr; } - return nullptr; + oslHostAddr pRet = static_cast<oslHostAddr>( + rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl))); + rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(buf)); + pRet->pSockAddr = createSocketAddr(); + memcpy(& pRet->pSockAddr->m_sockaddr, + & pAddr->m_sockaddr, sizeof(struct sockaddr)); + return pRet; } oslHostAddr SAL_CALL osl_copyHostAddr(const oslHostAddr Addr) diff --git a/sal/osl/w32/time.cxx b/sal/osl/w32/time.cxx index ae499dcf8a8c..688fef211519 100644 --- a/sal/osl/w32/time.cxx +++ b/sal/osl/w32/time.cxx @@ -131,52 +131,49 @@ sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, Ti sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( const TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal ) { TIME_ZONE_INFORMATION aTimeZoneInformation; - DWORD Success; - sal_Int64 bias; // get timezone information - if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID) - { - bias=aTimeZoneInformation.Bias; + DWORD Success = GetTimeZoneInformation( &aTimeZoneInformation ); + if (Success == TIME_ZONE_ID_INVALID) + return false; - // add bias for daylight saving time - if ( Success== TIME_ZONE_ID_DAYLIGHT ) - bias+=aTimeZoneInformation.DaylightBias; + sal_Int64 bias = aTimeZoneInformation.Bias; - if ( static_cast<sal_Int64>(pSystemTimeVal->Seconds) > ( bias * 60 ) ) - { - pLocalTimeVal->Seconds = static_cast<sal_uInt32>(pSystemTimeVal->Seconds - ( bias * 60) ); - pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec; + // add bias for daylight saving time + if ( Success == TIME_ZONE_ID_DAYLIGHT ) + bias+=aTimeZoneInformation.DaylightBias; - return true; - } - } + if ( static_cast<sal_Int64>(pSystemTimeVal->Seconds) > ( bias * 60 ) ) + { + pLocalTimeVal->Seconds = static_cast<sal_uInt32>(pSystemTimeVal->Seconds - ( bias * 60) ); + pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec; + return true; + } return false; } sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal ) { TIME_ZONE_INFORMATION aTimeZoneInformation; - DWORD Success; - sal_Int64 bias; // get timezone information - if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID) - { - bias=aTimeZoneInformation.Bias; + DWORD Success = GetTimeZoneInformation( &aTimeZoneInformation ); + if ( Success == TIME_ZONE_ID_INVALID ) + return false; - // add bias for daylight saving time - if ( Success== TIME_ZONE_ID_DAYLIGHT ) - bias+=aTimeZoneInformation.DaylightBias; + sal_Int64 bias = aTimeZoneInformation.Bias; - if ( static_cast<sal_Int64>(pLocalTimeVal->Seconds) + ( bias * 60 ) > 0 ) - { - pSystemTimeVal->Seconds = static_cast<sal_uInt32>( pLocalTimeVal->Seconds + ( bias * 60) ); - pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec; + // add bias for daylight saving time + if ( Success == TIME_ZONE_ID_DAYLIGHT ) + bias+=aTimeZoneInformation.DaylightBias; - return true; - } + if ( static_cast<sal_Int64>(pLocalTimeVal->Seconds) + ( bias * 60 ) > 0 ) + { + pSystemTimeVal->Seconds = static_cast<sal_uInt32>( pLocalTimeVal->Seconds + ( bias * 60) ); + pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec; + + return true; } return false; |