| 1001 | } |
| 1002 | |
| 1003 | static HKEY RegConvertKey(LPTSTR aBuf, LPTSTR *aSubkey = NULL, bool *aIsRemoteRegistry = NULL) |
| 1004 | { |
| 1005 | const size_t COMPUTER_NAME_BUF_SIZE = 128; |
| 1006 | |
| 1007 | LPTSTR key_name_pos = aBuf, computer_name_end = NULL; |
| 1008 | |
| 1009 | if (*aBuf == '\\' && aBuf[1] == '\\') // Something like \\ComputerName\HKLM. |
| 1010 | { |
| 1011 | if ( !(computer_name_end = _tcschr(aBuf + 2, '\\')) |
| 1012 | || (computer_name_end - aBuf) >= COMPUTER_NAME_BUF_SIZE ) |
| 1013 | return NULL; |
| 1014 | key_name_pos = computer_name_end + 1; |
| 1015 | } |
| 1016 | |
| 1017 | // Copy root key name into temporary buffer for use by _tcsicmp(). |
| 1018 | TCHAR key_name[20]; |
| 1019 | int i; |
| 1020 | for (i = 0; key_name_pos[i] && key_name_pos[i] != '\\'; ++i) |
| 1021 | { |
| 1022 | if (i == 19) |
| 1023 | return NULL; // Too long to be valid. |
| 1024 | key_name[i] = key_name_pos[i]; |
| 1025 | } |
| 1026 | key_name[i] = '\0'; |
| 1027 | |
| 1028 | // Set output parameters for caller. |
| 1029 | if (aSubkey) |
| 1030 | *aSubkey = key_name_pos + i + (key_name_pos[i] == '\\'); |
| 1031 | if (aIsRemoteRegistry) |
| 1032 | *aIsRemoteRegistry = (computer_name_end != NULL); |
| 1033 | |
| 1034 | HKEY root_key = RegConvertRootKeyType(key_name); |
| 1035 | if (!root_key) // Invalid or unsupported root key name. |
| 1036 | return NULL; |
| 1037 | |
| 1038 | if (!aIsRemoteRegistry || !computer_name_end) // Either caller didn't want it opened, or it doesn't need to be. |
| 1039 | return root_key; // If it's a remote key, this value should only be used by the caller as an indicator. |
| 1040 | // Otherwise, it's a remote computer whose registry the caller wants us to open: |
| 1041 | // It seems best to require the two leading backslashes in case the computer name contains |
| 1042 | // spaces (just in case spaces are allowed on some OSes or perhaps for Unix interoperability, etc.). |
| 1043 | // Therefore, make no attempt to trim leading and trailing spaces from the computer name: |
| 1044 | TCHAR computer_name[COMPUTER_NAME_BUF_SIZE]; |
| 1045 | tcslcpy(computer_name, aBuf, _countof(computer_name)); |
| 1046 | computer_name[computer_name_end - aBuf] = '\0'; |
| 1047 | HKEY remote_key; |
| 1048 | return (RegConnectRegistry(computer_name, root_key, &remote_key) == ERROR_SUCCESS) ? remote_key : NULL; |
| 1049 | } |
| 1050 | |
| 1051 | static HKEY RegConvertRootKeyType(LPTSTR aName); |
| 1052 | static LPTSTR RegConvertRootKeyType(HKEY aKey); |
nothing calls this directly
no outgoing calls
no test coverage detected