Author: dchapyshev Date: Sat Jan 10 13:09:37 2009 New Revision: 38688
URL: http://svn.reactos.org/svn/reactos?rev=38688&view=rev Log: - Formatting fix. No code change
Modified: trunk/reactos/dll/win32/kernel32/misc/computername.c
Modified: trunk/reactos/dll/win32/kernel32/misc/computername.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/com... ============================================================================== --- trunk/reactos/dll/win32/kernel32/misc/computername.c [iso-8859-1] (original) +++ trunk/reactos/dll/win32/kernel32/misc/computername.c [iso-8859-1] Sat Jan 10 13:09:37 2009 @@ -34,10 +34,13 @@
/* FUNCTIONS *****************************************************************/
-static BOOL GetComputerNameFromRegistry( LPWSTR RegistryKey, - LPWSTR ValueNameStr, - LPWSTR lpBuffer, - LPDWORD nSize ) { +static +BOOL +GetComputerNameFromRegistry(LPWSTR RegistryKey, + LPWSTR ValueNameStr, + LPWSTR lpBuffer, + LPDWORD nSize ) +{ PKEY_VALUE_PARTIAL_INFORMATION KeyInfo; OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING KeyName; @@ -47,65 +50,58 @@ ULONG ReturnSize; NTSTATUS Status;
- RtlInitUnicodeString (&KeyName,RegistryKey); - InitializeObjectAttributes (&ObjectAttributes, - &KeyName, - OBJ_CASE_INSENSITIVE, - NULL, - NULL); - Status = ZwOpenKey (&KeyHandle, - KEY_READ, - &ObjectAttributes); + RtlInitUnicodeString(&KeyName,RegistryKey); + InitializeObjectAttributes(&ObjectAttributes, + &KeyName, + OBJ_CASE_INSENSITIVE, + NULL, + NULL); + + Status = ZwOpenKey(&KeyHandle, + KEY_READ, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { SetLastErrorByStatus (Status); return FALSE; }
- KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + - *nSize * sizeof(WCHAR); - KeyInfo = RtlAllocateHeap (RtlGetProcessHeap (), - 0, - KeyInfoSize); + KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + *nSize * sizeof(WCHAR); + KeyInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, KeyInfoSize); if (KeyInfo == NULL) { - ZwClose (KeyHandle); - SetLastError (ERROR_OUTOFMEMORY); - return FALSE; - } - - RtlInitUnicodeString (&ValueName,ValueNameStr); - - Status = ZwQueryValueKey (KeyHandle, - &ValueName, - KeyValuePartialInformation, - KeyInfo, - KeyInfoSize, - &ReturnSize); + ZwClose(KeyHandle); + SetLastError(ERROR_OUTOFMEMORY); + return FALSE; + } + + RtlInitUnicodeString(&ValueName,ValueNameStr); + + Status = ZwQueryValueKey(KeyHandle, + &ValueName, + KeyValuePartialInformation, + KeyInfo, + KeyInfoSize, + &ReturnSize); if (!NT_SUCCESS(Status)) { - RtlFreeHeap (RtlGetProcessHeap (), - 0, - KeyInfo); - ZwClose (KeyHandle); + RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo); + ZwClose(KeyHandle); *nSize = ReturnSize; - SetLastErrorByStatus (Status); - return FALSE; - } - - if( *nSize > (KeyInfo->DataLength / sizeof(WCHAR)) ) { + SetLastErrorByStatus(Status); + return FALSE; + } + + if (*nSize > (KeyInfo->DataLength / sizeof(WCHAR))) + { *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1; lpBuffer[*nSize] = 0; }
- RtlCopyMemory (lpBuffer, - KeyInfo->Data, - *nSize * sizeof(WCHAR)); - - RtlFreeHeap (RtlGetProcessHeap (), - 0, - KeyInfo); - ZwClose (KeyHandle); + RtlCopyMemory(lpBuffer, KeyInfo->Data, *nSize * sizeof(WCHAR)); + + RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo); + ZwClose(KeyHandle);
return TRUE; } @@ -113,12 +109,11 @@ /* * @implemented */ -BOOL WINAPI -GetComputerNameExW ( - COMPUTER_NAME_FORMAT NameType, - LPWSTR lpBuffer, - LPDWORD nSize - ) +BOOL +WINAPI +GetComputerNameExW(COMPUTER_NAME_FORMAT NameType, + LPWSTR lpBuffer, + LPDWORD nSize) { UNICODE_STRING ResultString; UNICODE_STRING DomainPart; @@ -127,51 +122,31 @@ BOOL ret = TRUE; DWORD HostSize;
- switch( NameType ) { - case ComputerNameNetBIOS: - return GetComputerNameFromRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Control\ComputerName\ComputerName", - L"ComputerName", - lpBuffer, - nSize ); - - case ComputerNameDnsDomain: - return GetComputerNameFromRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Services\Tcpip\Parameters", - L"Domain", - lpBuffer, - nSize ); - - case ComputerNameDnsFullyQualified: - ResultString.Length = 0; - ResultString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR); - ResultString.Buffer = lpBuffer; - - RtlZeroMemory(QueryTable, sizeof(QueryTable)); - RtlInitUnicodeString(&DomainPart, NULL); - QueryTable[0].Name = L"HostName"; - QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; - QueryTable[0].EntryContext = &DomainPart; - - Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE, - L"\Registry\Machine\System" - L"\CurrentControlSet\Services\Tcpip" - L"\Parameters", - QueryTable, NULL, NULL); - - if( NT_SUCCESS(Status) ) { - Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); - HostSize = DomainPart.Length; - if (!NT_SUCCESS(Status)) { - ret = FALSE; - } - RtlAppendUnicodeToString(&ResultString, L"."); - RtlFreeUnicodeString(&DomainPart); - + switch (NameType) + { + case ComputerNameNetBIOS: + return GetComputerNameFromRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Control\ComputerName\ComputerName", + L"ComputerName", + lpBuffer, + nSize); + + case ComputerNameDnsDomain: + return GetComputerNameFromRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Services\Tcpip\Parameters", + L"Domain", + lpBuffer, + nSize); + + case ComputerNameDnsFullyQualified: + ResultString.Length = 0; + ResultString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR); + ResultString.Buffer = lpBuffer; + + RtlZeroMemory(QueryTable, sizeof(QueryTable)); RtlInitUnicodeString(&DomainPart, NULL); - QueryTable[0].Name = L"Domain"; + + QueryTable[0].Name = L"HostName"; QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; QueryTable[0].EntryContext = &DomainPart;
@@ -179,52 +154,85 @@ L"\Registry\Machine\System" L"\CurrentControlSet\Services\Tcpip" L"\Parameters", - QueryTable, NULL, NULL); - - if( NT_SUCCESS(Status) ) { + QueryTable, + NULL, + NULL); + + if (NT_SUCCESS(Status)) + { Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); - if ( (!NT_SUCCESS(Status)) || (!ret)) { - *nSize = HostSize + DomainPart.Length; - SetLastError(ERROR_MORE_DATA); + HostSize = DomainPart.Length; + + if (!NT_SUCCESS(Status)) + { + ret = FALSE; + } + + RtlAppendUnicodeToString(&ResultString, L"."); + RtlFreeUnicodeString(&DomainPart); + + RtlInitUnicodeString(&DomainPart, NULL); + QueryTable[0].Name = L"Domain"; + QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; + QueryTable[0].EntryContext = &DomainPart; + + Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE, + L"\Registry\Machine\System" + L"\CurrentControlSet\Services\Tcpip" + L"\Parameters", + QueryTable, + NULL, + NULL); + + if (NT_SUCCESS(Status)) + { + Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); + if ((!NT_SUCCESS(Status)) || (!ret)) + { + *nSize = HostSize + DomainPart.Length; + SetLastError(ERROR_MORE_DATA); + RtlFreeUnicodeString(&DomainPart); + return FALSE; + } RtlFreeUnicodeString(&DomainPart); - return FALSE; + *nSize = ResultString.Length / sizeof(WCHAR) - 1; + return TRUE; } - RtlFreeUnicodeString(&DomainPart); - *nSize = ResultString.Length / sizeof(WCHAR) - 1; - return TRUE; } - } - return FALSE; - - case ComputerNameDnsHostname: - return GetComputerNameFromRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Services\Tcpip\Parameters", - L"Hostname", - lpBuffer, - nSize ); - - case ComputerNamePhysicalDnsDomain: - return GetComputerNameFromRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Services\Tcpip\Parameters", - L"Domain", - lpBuffer, - nSize ); - - /* XXX Redo these */ - case ComputerNamePhysicalDnsFullyQualified: - return GetComputerNameExW( ComputerNameDnsFullyQualified, - lpBuffer, nSize ); - case ComputerNamePhysicalDnsHostname: - return GetComputerNameExW( ComputerNameDnsHostname, - lpBuffer, nSize ); - case ComputerNamePhysicalNetBIOS: - return GetComputerNameExW( ComputerNameNetBIOS, - lpBuffer, nSize ); - - case ComputerNameMax: - return FALSE; + return FALSE; + + case ComputerNameDnsHostname: + return GetComputerNameFromRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Services\Tcpip\Parameters", + L"Hostname", + lpBuffer, + nSize); + + case ComputerNamePhysicalDnsDomain: + return GetComputerNameFromRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Services\Tcpip\Parameters", + L"Domain", + lpBuffer, + nSize); + + /* XXX Redo these */ + case ComputerNamePhysicalDnsFullyQualified: + return GetComputerNameExW(ComputerNameDnsFullyQualified, + lpBuffer, + nSize); + + case ComputerNamePhysicalDnsHostname: + return GetComputerNameExW(ComputerNameDnsHostname, + lpBuffer, + nSize); + + case ComputerNamePhysicalNetBIOS: + return GetComputerNameExW(ComputerNameNetBIOS, + lpBuffer, + nSize); + + case ComputerNameMax: + return FALSE; }
return FALSE; @@ -235,18 +243,17 @@ */ BOOL WINAPI -GetComputerNameExA ( - COMPUTER_NAME_FORMAT NameType, - LPSTR lpBuffer, - LPDWORD nSize - ) +GetComputerNameExA(COMPUTER_NAME_FORMAT NameType, + LPSTR lpBuffer, + LPDWORD nSize) { UNICODE_STRING UnicodeString; ANSI_STRING AnsiString; BOOL Result; PWCHAR TempBuffer = RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize * sizeof(WCHAR) );
- if( !TempBuffer ) { + if (!TempBuffer) + { return ERROR_OUTOFMEMORY; }
@@ -254,19 +261,20 @@ AnsiString.Length = 0; AnsiString.Buffer = lpBuffer;
- Result = GetComputerNameExW( NameType, TempBuffer, nSize ); - - if( Result ) { + Result = GetComputerNameExW(NameType, TempBuffer, nSize); + + if (Result) + { UnicodeString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR); UnicodeString.Length = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR); UnicodeString.Buffer = TempBuffer;
- RtlUnicodeStringToAnsiString (&AnsiString, - &UnicodeString, - FALSE); - } - - RtlFreeHeap( RtlGetProcessHeap(), 0, TempBuffer ); + RtlUnicodeStringToAnsiString(&AnsiString, + &UnicodeString, + FALSE); + } + + RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer);
return Result; } @@ -274,64 +282,65 @@ /* * @implemented */ -BOOL WINAPI -GetComputerNameA (LPSTR lpBuffer, - LPDWORD lpnSize) -{ - return GetComputerNameExA( ComputerNameNetBIOS, lpBuffer, lpnSize ); -} - - -/* - * @implemented - */ -BOOL WINAPI -GetComputerNameW (LPWSTR lpBuffer, - LPDWORD lpnSize) -{ - return GetComputerNameExW( ComputerNameNetBIOS, lpBuffer, lpnSize ); -} - - -/* - * @implemented - */ -static BOOL -IsValidComputerName ( - COMPUTER_NAME_FORMAT NameType, - LPCWSTR lpComputerName) -{ - PWCHAR p; - ULONG Length; - - /* FIXME: do verification according to NameType */ - - Length = 0; - p = (PWCHAR)lpComputerName; - while (*p != 0) - { - if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' || - *p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L''' || - *p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' || - *p == L'{' || *p == L'}' || *p == L'~')) - return FALSE; - - Length++; - p++; - } - - if (Length == 0 || - Length > MAX_COMPUTERNAME_LENGTH) - return FALSE; - - return TRUE; -} - - -static BOOL SetComputerNameToRegistry( - LPCWSTR RegistryKey, - LPCWSTR ValueNameStr, - LPCWSTR lpBuffer) +BOOL +WINAPI +GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize) +{ + return GetComputerNameExA(ComputerNameNetBIOS, lpBuffer, lpnSize); +} + + +/* + * @implemented + */ +BOOL +WINAPI +GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize) +{ + return GetComputerNameExW(ComputerNameNetBIOS, lpBuffer, lpnSize); +} + + +/* + * @implemented + */ +static +BOOL +IsValidComputerName(COMPUTER_NAME_FORMAT NameType, + LPCWSTR lpComputerName) +{ + PWCHAR p; + ULONG Length; + + /* FIXME: do verification according to NameType */ + + Length = 0; + p = (PWCHAR)lpComputerName; + + while (*p != 0) + { + if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' || + *p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L''' || + *p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' || + *p == L'{' || *p == L'}' || *p == L'~')) + return FALSE; + + Length++; + p++; + } + + if (Length == 0 || Length > MAX_COMPUTERNAME_LENGTH) + return FALSE; + + return TRUE; +} + + +static +BOOL +SetComputerNameToRegistry(LPCWSTR RegistryKey, + LPCWSTR ValueNameStr, + LPCWSTR lpBuffer) { OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING KeyName; @@ -339,39 +348,39 @@ HANDLE KeyHandle; NTSTATUS Status;
- RtlInitUnicodeString (&KeyName, RegistryKey); - InitializeObjectAttributes (&ObjectAttributes, - &KeyName, - OBJ_CASE_INSENSITIVE, - NULL, - NULL ); - - Status = NtOpenKey (&KeyHandle, - KEY_WRITE, - &ObjectAttributes); + RtlInitUnicodeString(&KeyName, RegistryKey); + InitializeObjectAttributes(&ObjectAttributes, + &KeyName, + OBJ_CASE_INSENSITIVE, + NULL, + NULL); + + Status = NtOpenKey(&KeyHandle, + KEY_WRITE, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { - SetLastErrorByStatus (Status); - return FALSE; - } - - RtlInitUnicodeString (&ValueName, ValueNameStr); - - Status = NtSetValueKey (KeyHandle, - &ValueName, - 0, - REG_SZ, - (PVOID)lpBuffer, - (wcslen (lpBuffer) + 1) * sizeof(WCHAR)); + SetLastErrorByStatus(Status); + return FALSE; + } + + RtlInitUnicodeString(&ValueName, ValueNameStr); + + Status = NtSetValueKey(KeyHandle, + &ValueName, + 0, + REG_SZ, + (PVOID)lpBuffer, + (wcslen (lpBuffer) + 1) * sizeof(WCHAR)); if (!NT_SUCCESS(Status)) { - ZwClose (KeyHandle); - SetLastErrorByStatus (Status); - return FALSE; - } - - NtFlushKey (KeyHandle); - ZwClose (KeyHandle); + ZwClose(KeyHandle); + SetLastErrorByStatus(Status); + return FALSE; + } + + NtFlushKey(KeyHandle); + ZwClose(KeyHandle);
return TRUE; } @@ -380,40 +389,41 @@ /* * @implemented */ -BOOL WINAPI -SetComputerNameA (LPCSTR lpComputerName) -{ - return SetComputerNameExA( ComputerNamePhysicalNetBIOS, lpComputerName ); -} - - -/* - * @implemented - */ -BOOL WINAPI -SetComputerNameW (LPCWSTR lpComputerName) -{ - return SetComputerNameExW( ComputerNamePhysicalNetBIOS, lpComputerName ); -} - - -/* - * @implemented - */ -BOOL WINAPI -SetComputerNameExA ( - COMPUTER_NAME_FORMAT NameType, - LPCSTR lpBuffer) +BOOL +WINAPI +SetComputerNameA(LPCSTR lpComputerName) +{ + return SetComputerNameExA(ComputerNamePhysicalNetBIOS, lpComputerName); +} + + +/* + * @implemented + */ +BOOL +WINAPI +SetComputerNameW(LPCWSTR lpComputerName) +{ + return SetComputerNameExW(ComputerNamePhysicalNetBIOS, lpComputerName); +} + + +/* + * @implemented + */ +BOOL +WINAPI +SetComputerNameExA(COMPUTER_NAME_FORMAT NameType, + LPCSTR lpBuffer) { UNICODE_STRING Buffer; BOOL bResult;
- RtlCreateUnicodeStringFromAsciiz (&Buffer, - (LPSTR)lpBuffer); - - bResult = SetComputerNameExW (NameType, Buffer.Buffer); - - RtlFreeUnicodeString (&Buffer); + RtlCreateUnicodeStringFromAsciiz(&Buffer, (LPSTR)lpBuffer); + + bResult = SetComputerNameExW(NameType, Buffer.Buffer); + + RtlFreeUnicodeString(&Buffer);
return bResult; } @@ -422,43 +432,41 @@ /* * @implemented */ -BOOL WINAPI -SetComputerNameExW ( - COMPUTER_NAME_FORMAT NameType, - LPCWSTR lpBuffer) -{ - if (!IsValidComputerName (NameType, lpBuffer)) - { - SetLastError (ERROR_INVALID_PARAMETER); - return FALSE; - } - - switch( NameType ) { - case ComputerNamePhysicalDnsDomain: - return SetComputerNameToRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Services\Tcpip\Parameters", - L"Domain", - lpBuffer ); - - case ComputerNamePhysicalDnsHostname: - return SetComputerNameToRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Services\Tcpip\Parameters", - L"Hostname", - lpBuffer ); - - case ComputerNamePhysicalNetBIOS: - return SetComputerNameToRegistry - ( L"\Registry\Machine\System\CurrentControlSet" - L"\Control\ComputerName\ComputerName", - L"ComputerName", - lpBuffer ); - - default: - SetLastError (ERROR_INVALID_PARAMETER); - return FALSE; - } +BOOL +WINAPI +SetComputerNameExW(COMPUTER_NAME_FORMAT NameType, + LPCWSTR lpBuffer) +{ + if (!IsValidComputerName(NameType, lpBuffer)) + { + SetLastError(ERROR_INVALID_PARAMETER); + return FALSE; + } + + switch( NameType ) + { + case ComputerNamePhysicalDnsDomain: + return SetComputerNameToRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Services\Tcpip\Parameters", + L"Domain", + lpBuffer); + + case ComputerNamePhysicalDnsHostname: + return SetComputerNameToRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Services\Tcpip\Parameters", + L"Hostname", + lpBuffer); + + case ComputerNamePhysicalNetBIOS: + return SetComputerNameToRegistry(L"\Registry\Machine\System\CurrentControlSet" + L"\Control\ComputerName\ComputerName", + L"ComputerName", + lpBuffer); + + default: + SetLastError (ERROR_INVALID_PARAMETER); + return FALSE; + } }
@@ -491,7 +499,7 @@
if (!ComputerName) return FALSE;
- memcpy( ComputerName, Hostname, len ); + memcpy(ComputerName, Hostname, len); ComputerName[len + 1] = 0; return TRUE; } @@ -502,11 +510,9 @@ */ BOOL WINAPI -DnsHostnameToComputerNameW ( - LPCWSTR hostname, - LPWSTR computername, - LPDWORD size - ) +DnsHostnameToComputerNameW(LPCWSTR hostname, + LPWSTR computername, + LPDWORD size) { DWORD len;
@@ -525,7 +531,7 @@ } if (!computername) return FALSE;
- memcpy( computername, hostname, len * sizeof(WCHAR) ); + memcpy(computername, hostname, len * sizeof(WCHAR)); computername[len + 1] = 0; return TRUE; }