api: simplify workflow

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-07-24 08:29:33 +02:00 committed by Jason A. Donenfeld
parent c213663a90
commit 355ce73841

View File

@ -42,33 +42,22 @@ GetDeviceRegistryProperty(
_In_ HDEVINFO DevInfo, _In_ HDEVINFO DevInfo,
_In_ SP_DEVINFO_DATA *DevInfoData, _In_ SP_DEVINFO_DATA *DevInfoData,
_In_ DWORD Property, _In_ DWORD Property,
_Out_opt_ DWORD *PropertyRegDataType, _Out_opt_ DWORD *ValueType,
_Out_ void **PropertyBuffer, _Out_ void **Buf,
_Out_opt_ DWORD *PropertySize) _Inout_ DWORD *BufLen)
{ {
HANDLE Heap = GetProcessHeap(); HANDLE Heap = GetProcessHeap();
DWORD Result, Size = 256;
for (;;) for (;;)
{ {
void *Buf = HeapAlloc(Heap, 0, Size); *Buf = HeapAlloc(Heap, 0, *BufLen);
if (!Buf) if (!*Buf)
return ERROR_OUTOFMEMORY; return ERROR_OUTOFMEMORY;
DWORD ValueType; if (SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, ValueType, *Buf, *BufLen, BufLen))
if (!SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, &ValueType, Buf, Size, &Size)) return ERROR_SUCCESS;
{ DWORD Result = GetLastError();
Result = GetLastError(); HeapFree(Heap, 0, *Buf);
HeapFree(Heap, 0, Buf); if (Result != ERROR_INSUFFICIENT_BUFFER)
if (Result == ERROR_INSUFFICIENT_BUFFER)
continue;
return Result; return Result;
}
if (PropertyRegDataType)
*PropertyRegDataType = ValueType;
*PropertyBuffer = Buf;
if (PropertySize)
*PropertySize = Size;
return ERROR_SUCCESS;
} }
} }
@ -92,24 +81,23 @@ GetDeviceRegistryString(
_In_ HDEVINFO DevInfo, _In_ HDEVINFO DevInfo,
_In_ SP_DEVINFO_DATA *DevInfoData, _In_ SP_DEVINFO_DATA *DevInfoData,
_In_ DWORD Property, _In_ DWORD Property,
_Out_ WCHAR **PropertyBuffer) _Out_ WCHAR **Buf)
{ {
DWORD Result, ValueType, Size; DWORD Result, ValueType, Size = 256 * sizeof(WCHAR);
Result = GetDeviceRegistryProperty(DevInfo, DevInfoData, Property, &ValueType, PropertyBuffer, &Size); Result = GetDeviceRegistryProperty(DevInfo, DevInfoData, Property, &ValueType, Buf, &Size);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
return Result; return Result;
switch (ValueType) switch (ValueType)
{ {
case REG_SZ: case REG_SZ:
case REG_EXPAND_SZ: case REG_EXPAND_SZ:
case REG_MULTI_SZ: case REG_MULTI_SZ:
Result = RegistryGetString(PropertyBuffer, Size / sizeof(WCHAR), ValueType); Result = RegistryGetString(Buf, Size / sizeof(WCHAR), ValueType);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
HeapFree(GetProcessHeap(), 0, *PropertyBuffer); HeapFree(GetProcessHeap(), 0, *Buf);
return Result; return Result;
default: default:
HeapFree(GetProcessHeap(), 0, *PropertyBuffer); HeapFree(GetProcessHeap(), 0, *Buf);
return ERROR_INVALID_DATATYPE; return ERROR_INVALID_DATATYPE;
} }
} }
@ -134,24 +122,23 @@ GetDeviceRegistryMultiString(
_In_ HDEVINFO DevInfo, _In_ HDEVINFO DevInfo,
_In_ SP_DEVINFO_DATA *DevInfoData, _In_ SP_DEVINFO_DATA *DevInfoData,
_In_ DWORD Property, _In_ DWORD Property,
_Out_ WCHAR **PropertyBuffer) _Out_ WCHAR **Buf)
{ {
DWORD Result, ValueType, Size; DWORD Result, ValueType, Size = 256 * sizeof(WCHAR);
Result = GetDeviceRegistryProperty(DevInfo, DevInfoData, Property, &ValueType, PropertyBuffer, &Size); Result = GetDeviceRegistryProperty(DevInfo, DevInfoData, Property, &ValueType, Buf, &Size);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
return Result; return Result;
switch (ValueType) switch (ValueType)
{ {
case REG_SZ: case REG_SZ:
case REG_EXPAND_SZ: case REG_EXPAND_SZ:
case REG_MULTI_SZ: case REG_MULTI_SZ:
Result = RegistryGetMultiString(PropertyBuffer, Size / sizeof(WCHAR), ValueType); Result = RegistryGetMultiString(Buf, Size / sizeof(WCHAR), ValueType);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
HeapFree(GetProcessHeap(), 0, *PropertyBuffer); HeapFree(GetProcessHeap(), 0, *Buf);
return Result; return Result;
default: default:
HeapFree(GetProcessHeap(), 0, *PropertyBuffer); HeapFree(GetProcessHeap(), 0, *Buf);
return ERROR_INVALID_DATATYPE; return ERROR_INVALID_DATATYPE;
} }
} }
@ -188,15 +175,12 @@ GetDriverInfoDetail(
if (!*DriverDetailData) if (!*DriverDetailData)
return ERROR_OUTOFMEMORY; return ERROR_OUTOFMEMORY;
(*DriverDetailData)->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W); (*DriverDetailData)->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W);
if (!SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DriverData, *DriverDetailData, Size, &Size)) if (SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DriverData, *DriverDetailData, Size, &Size))
{ return ERROR_SUCCESS;
DWORD Result = GetLastError(); DWORD Result = GetLastError();
HeapFree(Heap, 0, *DriverDetailData); HeapFree(Heap, 0, *DriverDetailData);
if (Result == ERROR_INSUFFICIENT_BUFFER) if (Result != ERROR_INSUFFICIENT_BUFFER)
continue;
return Result; return Result;
}
return ERROR_SUCCESS;
} }
} }
@ -325,7 +309,6 @@ cleanupKey:
static WINTUN_STATUS static WINTUN_STATUS
GetDevInfoData(_In_ const GUID *CfgInstanceID, _Out_ HDEVINFO *DevInfo, _Out_ SP_DEVINFO_DATA *DevInfoData) GetDevInfoData(_In_ const GUID *CfgInstanceID, _Out_ HDEVINFO *DevInfo, _Out_ SP_DEVINFO_DATA *DevInfoData)
{ {
DWORD Result;
*DevInfo = SetupDiGetClassDevsExW(&CLASS_NET_GUID, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL); *DevInfo = SetupDiGetClassDevsExW(&CLASS_NET_GUID, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL);
if (!*DevInfo) if (!*DevInfo)
return GetLastError(); return GetLastError();
@ -334,18 +317,14 @@ GetDevInfoData(_In_ const GUID *CfgInstanceID, _Out_ HDEVINFO *DevInfo, _Out_ SP
DevInfoData->cbSize = sizeof(SP_DEVINFO_DATA); DevInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiEnumDeviceInfo(*DevInfo, MemberIndex, DevInfoData)) if (!SetupDiEnumDeviceInfo(*DevInfo, MemberIndex, DevInfoData))
{ {
Result = GetLastError(); if (GetLastError() == ERROR_NO_MORE_ITEMS)
if (Result == ERROR_NO_MORE_ITEMS)
break; break;
continue; continue;
} }
GUID CfgInstanceID2; GUID CfgInstanceID2;
Result = GetNetCfgInstanceId(*DevInfo, DevInfoData, &CfgInstanceID2); if (GetNetCfgInstanceId(*DevInfo, DevInfoData, &CfgInstanceID2) == ERROR_SUCCESS &&
if (Result != ERROR_SUCCESS || memcmp(CfgInstanceID, &CfgInstanceID2, sizeof(GUID)) != 0) !memcmp(CfgInstanceID, &CfgInstanceID2, sizeof(GUID)))
continue; return ERROR_SUCCESS;
return ERROR_SUCCESS;
} }
SetupDiDestroyDeviceInfoList(*DevInfo); SetupDiDestroyDeviceInfoList(*DevInfo);
return ERROR_FILE_NOT_FOUND; return ERROR_FILE_NOT_FOUND;
@ -619,8 +598,7 @@ WintunGetAdapter(_In_z_count_c_(MAX_POOL) const WCHAR *Pool, _In_z_ const WCHAR
} }
GUID CfgInstanceID; GUID CfgInstanceID;
Result = GetNetCfgInstanceId(DevInfo, &DevInfoData, &CfgInstanceID); if (GetNetCfgInstanceId(DevInfo, &DevInfoData, &CfgInstanceID) != ERROR_SUCCESS)
if (Result != ERROR_SUCCESS)
continue; continue;
/* TODO: is there a better way than comparing ifnames? */ /* TODO: is there a better way than comparing ifnames? */