From e7073d2639b1c6b01ea0a14e5e2876b725a63227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sun, 31 Aug 2025 21:32:20 +0200 Subject: [PATCH] [MSGINA] Move some utility functions to a separate utils.c file (#8370) - Registry-value getters; string copy functions. - ReadRegSzValue(): Use a meaningful error value when the value type isn't the expected one. - Rename `DuplicationString` -> `DuplicateString` --- dll/win32/msgina/CMakeLists.txt | 3 +- dll/win32/msgina/msgina.c | 78 ++------------------------------ dll/win32/msgina/msgina.h | 24 +++++++--- dll/win32/msgina/utils.c | 79 +++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 82 deletions(-) create mode 100644 dll/win32/msgina/utils.c diff --git a/dll/win32/msgina/CMakeLists.txt b/dll/win32/msgina/CMakeLists.txt index cbea4edcb14..3321e339ca8 100644 --- a/dll/win32/msgina/CMakeLists.txt +++ b/dll/win32/msgina/CMakeLists.txt @@ -7,7 +7,8 @@ list(APPEND SOURCE msgina.c shutdown.c stubs.c - tui.c) + tui.c + utils.c) list(APPEND PCH_SKIP_SOURCE dimmedwindow.cpp diff --git a/dll/win32/msgina/msgina.c b/dll/win32/msgina/msgina.c index d8b23eeec87..bcde8e93394 100644 --- a/dll/win32/msgina/msgina.c +++ b/dll/win32/msgina/msgina.c @@ -56,64 +56,6 @@ WlxNegotiate( return TRUE; } -LONG -ReadRegSzValue( - IN HKEY hKey, - IN LPCWSTR pszValue, - OUT LPWSTR* pValue) -{ - LONG rc; - DWORD dwType; - DWORD cbData = 0; - LPWSTR Value; - - if (!pValue) - return ERROR_INVALID_PARAMETER; - - *pValue = NULL; - rc = RegQueryValueExW(hKey, pszValue, NULL, &dwType, NULL, &cbData); - if (rc != ERROR_SUCCESS) - return rc; - if (dwType != REG_SZ) - return ERROR_FILE_NOT_FOUND; - Value = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR)); - if (!Value) - return ERROR_NOT_ENOUGH_MEMORY; - rc = RegQueryValueExW(hKey, pszValue, NULL, NULL, (LPBYTE)Value, &cbData); - if (rc != ERROR_SUCCESS) - { - HeapFree(GetProcessHeap(), 0, Value); - return rc; - } - /* NULL-terminate the string */ - Value[cbData / sizeof(WCHAR)] = '\0'; - - *pValue = Value; - return ERROR_SUCCESS; -} - -static LONG -ReadRegDwordValue( - IN HKEY hKey, - IN LPCWSTR pszValue, - OUT LPDWORD pValue) -{ - LONG rc; - DWORD dwType; - DWORD cbData; - DWORD dwValue; - - if (!pValue) - return ERROR_INVALID_PARAMETER; - - cbData = sizeof(DWORD); - rc = RegQueryValueExW(hKey, pszValue, NULL, &dwType, (LPBYTE)&dwValue, &cbData); - if (rc == ERROR_SUCCESS && dwType == REG_DWORD) - *pValue = dwValue; - - return ERROR_SUCCESS; -} - static VOID ChooseGinaUI(VOID) { @@ -692,20 +634,6 @@ WlxRemoveStatusMessage( return pGinaUI->RemoveStatusMessage(pgContext); } -static PWSTR -DuplicationString(PWSTR Str) -{ - DWORD cb; - PWSTR NewStr; - - if (Str == NULL) return NULL; - - cb = (wcslen(Str) + 1) * sizeof(WCHAR); - if ((NewStr = LocalAlloc(LMEM_FIXED, cb))) - memcpy(NewStr, Str, cb); - return NewStr; -} - BOOL DoAdminUnlock( @@ -959,9 +887,9 @@ CreateProfile( } *pgContext->pAuthenticationId = Stats.AuthenticationId; - pgContext->pMprNotifyInfo->pszUserName = DuplicationString(UserName); - pgContext->pMprNotifyInfo->pszDomain = DuplicationString(Domain); - pgContext->pMprNotifyInfo->pszPassword = DuplicationString(Password); + pgContext->pMprNotifyInfo->pszUserName = DuplicateString(UserName); + pgContext->pMprNotifyInfo->pszDomain = DuplicateString(Domain); + pgContext->pMprNotifyInfo->pszPassword = DuplicateString(Password); pgContext->pMprNotifyInfo->pszOldPassword = NULL; *pgContext->pdwOptions = 0; *pgContext->pProfile = pProfile; diff --git a/dll/win32/msgina/msgina.h b/dll/win32/msgina/msgina.h index b644aa36ff9..405653bc884 100644 --- a/dll/win32/msgina/msgina.h +++ b/dll/win32/msgina/msgina.h @@ -99,12 +99,6 @@ MyLogonUser( /* msgina.c */ -LONG -ReadRegSzValue( - IN HKEY hKey, - IN LPCWSTR pszValue, - OUT LPWSTR *pValue); - BOOL DoAdminUnlock( IN PGINA_CONTEXT pgContext, @@ -150,6 +144,24 @@ ShutdownDialog( IN DWORD ShutdownOptions, IN PGINA_CONTEXT pgContext); +/* utils.c */ + +LONG +ReadRegSzValue( + _In_ HKEY hKey, + _In_ PCWSTR pszValue, + _Out_ PWSTR* pValue); + +LONG +ReadRegDwordValue( + _In_ HKEY hKey, + _In_ PCWSTR pszValue, + _Out_ PDWORD pValue); + +PWSTR +DuplicateString( + _In_opt_ PCWSTR Str); + #ifdef __cplusplus } // extern "C" diff --git a/dll/win32/msgina/utils.c b/dll/win32/msgina/utils.c new file mode 100644 index 00000000000..aa5b6db3e74 --- /dev/null +++ b/dll/win32/msgina/utils.c @@ -0,0 +1,79 @@ +/* + * PROJECT: ReactOS Logon GINA DLL msgina.dll + * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later) + * PURPOSE: Miscellaneous utility functions. + * COPYRIGHT: Copyright 2006 Hervé Poussineau + * Copyright 2014 Eric Kohl + * Copyright 2025 Hermès Bélusca-Maïto + */ + +#include "msgina.h" + +LONG +ReadRegSzValue( + _In_ HKEY hKey, + _In_ PCWSTR pszValue, + _Out_ PWSTR* pValue) +{ + LONG rc; + DWORD dwType; + DWORD cbData = 0; + PWSTR Value; + + *pValue = NULL; + rc = RegQueryValueExW(hKey, pszValue, NULL, &dwType, NULL, &cbData); + if (rc != ERROR_SUCCESS) + return rc; + if (dwType != REG_SZ) + return ERROR_UNSUPPORTED_TYPE; + Value = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR)); + if (!Value) + return ERROR_NOT_ENOUGH_MEMORY; + rc = RegQueryValueExW(hKey, pszValue, NULL, NULL, (PBYTE)Value, &cbData); + if (rc != ERROR_SUCCESS) + { + HeapFree(GetProcessHeap(), 0, Value); + return rc; + } + /* NULL-terminate the string */ + Value[cbData / sizeof(WCHAR)] = UNICODE_NULL; + + *pValue = Value; + return ERROR_SUCCESS; +} + +LONG +ReadRegDwordValue( + _In_ HKEY hKey, + _In_ PCWSTR pszValue, + _Out_ PDWORD pValue) +{ + LONG rc; + DWORD dwValue, dwType, cbData; + + cbData = sizeof(dwValue); + rc = RegQueryValueExW(hKey, pszValue, NULL, &dwType, (PBYTE)&dwValue, &cbData); + if ((rc == ERROR_SUCCESS) && (dwType == REG_DWORD) && (cbData == sizeof(dwValue))) + { + *pValue = dwValue; + return ERROR_SUCCESS; + } + + return rc; +} + +PWSTR +DuplicateString( + _In_opt_ PCWSTR Str) +{ + PWSTR NewStr; + SIZE_T cb; + + if (!Str) + return NULL; + + cb = (wcslen(Str) + 1) * sizeof(WCHAR); + if ((NewStr = LocalAlloc(LMEM_FIXED, cb))) + memcpy(NewStr, Str, cb); + return NewStr; +}