Commit aba3ed75 authored by Zebediah Figura's avatar Zebediah Figura Committed by Alexandre Julliard

setupapi/devinst: Factor out get_device_set().

parent cdb486ae
...@@ -129,10 +129,9 @@ struct device_iface ...@@ -129,10 +129,9 @@ struct device_iface
struct list entry; struct list entry;
}; };
static struct device *get_device(HDEVINFO devinfo, const SP_DEVINFO_DATA *data) static struct DeviceInfoSet *get_device_set(HDEVINFO devinfo)
{ {
struct DeviceInfoSet *set = devinfo; struct DeviceInfoSet *set = devinfo;
struct device *device;
if (!devinfo || devinfo == INVALID_HANDLE_VALUE || set->magic != SETUP_DEVICE_INFO_SET_MAGIC) if (!devinfo || devinfo == INVALID_HANDLE_VALUE || set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
{ {
...@@ -140,6 +139,17 @@ static struct device *get_device(HDEVINFO devinfo, const SP_DEVINFO_DATA *data) ...@@ -140,6 +139,17 @@ static struct device *get_device(HDEVINFO devinfo, const SP_DEVINFO_DATA *data)
return NULL; return NULL;
} }
return set;
}
static struct device *get_device(HDEVINFO devinfo, const SP_DEVINFO_DATA *data)
{
struct DeviceInfoSet *set;
struct device *device;
if (!(set = get_device_set(devinfo)))
return FALSE;
if (!data || data->cbSize != sizeof(*data) || !data->Reserved) if (!data || data->cbSize != sizeof(*data) || !data->Reserved)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
...@@ -159,13 +169,8 @@ static struct device *get_device(HDEVINFO devinfo, const SP_DEVINFO_DATA *data) ...@@ -159,13 +169,8 @@ static struct device *get_device(HDEVINFO devinfo, const SP_DEVINFO_DATA *data)
static struct device_iface *get_device_iface(HDEVINFO devinfo, const SP_DEVICE_INTERFACE_DATA *data) static struct device_iface *get_device_iface(HDEVINFO devinfo, const SP_DEVICE_INTERFACE_DATA *data)
{ {
struct DeviceInfoSet *set = devinfo; if (!get_device_set(devinfo))
return FALSE;
if (!devinfo || devinfo == INVALID_HANDLE_VALUE || set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
{
SetLastError(ERROR_INVALID_HANDLE);
return NULL;
}
if (!data || data->cbSize != sizeof(*data) || !data->Reserved) if (!data || data->cbSize != sizeof(*data) || !data->Reserved)
{ {
...@@ -1387,16 +1392,16 @@ static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName) ...@@ -1387,16 +1392,16 @@ static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName)
/*********************************************************************** /***********************************************************************
* SetupDiCreateDeviceInfoW (SETUPAPI.@) * SetupDiCreateDeviceInfoW (SETUPAPI.@)
*/ */
BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO DeviceInfoSet, PCWSTR DeviceName, BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO devinfo, PCWSTR DeviceName,
const GUID *ClassGuid, PCWSTR DeviceDescription, HWND hwndParent, DWORD CreationFlags, const GUID *ClassGuid, PCWSTR DeviceDescription, HWND hwndParent, DWORD CreationFlags,
SP_DEVINFO_DATA *device_data) SP_DEVINFO_DATA *device_data)
{ {
struct DeviceInfoSet *set = DeviceInfoSet; struct DeviceInfoSet *set;
BOOL ret = FALSE, allocatedInstanceId = FALSE; BOOL ret = FALSE, allocatedInstanceId = FALSE;
LPCWSTR instanceId = NULL; LPCWSTR instanceId = NULL;
TRACE("%p %s %s %s %p %x %p\n", DeviceInfoSet, debugstr_w(DeviceName), TRACE("devinfo %p, name %s, class %s, description %s, hwnd %p, flags %#x, device_data %p.\n",
debugstr_guid(ClassGuid), debugstr_w(DeviceDescription), devinfo, debugstr_w(DeviceName), debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
hwndParent, CreationFlags, device_data); hwndParent, CreationFlags, device_data);
if (!DeviceName) if (!DeviceName)
...@@ -1404,21 +1409,16 @@ BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO DeviceInfoSet, PCWSTR DeviceName, ...@@ -1404,21 +1409,16 @@ BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO DeviceInfoSet, PCWSTR DeviceName,
SetLastError(ERROR_INVALID_DEVINST_NAME); SetLastError(ERROR_INVALID_DEVINST_NAME);
return FALSE; return FALSE;
} }
if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
{ if (!(set = get_device_set(devinfo)))
SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
}
if (!ClassGuid) if (!ClassGuid)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) && if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) &&
!IsEqualGUID(ClassGuid, &set->ClassGuid)) !IsEqualGUID(ClassGuid, &set->ClassGuid))
{ {
...@@ -1561,54 +1561,45 @@ BOOL WINAPI SetupDiRemoveDeviceInterface(HDEVINFO info, PSP_DEVICE_INTERFACE_DAT ...@@ -1561,54 +1561,45 @@ BOOL WINAPI SetupDiRemoveDeviceInterface(HDEVINFO info, PSP_DEVICE_INTERFACE_DAT
/*********************************************************************** /***********************************************************************
* SetupDiEnumDeviceInfo (SETUPAPI.@) * SetupDiEnumDeviceInfo (SETUPAPI.@)
*/ */
BOOL WINAPI SetupDiEnumDeviceInfo( BOOL WINAPI SetupDiEnumDeviceInfo(HDEVINFO devinfo, DWORD index, SP_DEVINFO_DATA *device_data)
HDEVINFO devinfo,
DWORD index,
PSP_DEVINFO_DATA info)
{ {
BOOL ret = FALSE; struct DeviceInfoSet *set;
struct device *device;
DWORD i = 0;
TRACE("%p %d %p\n", devinfo, index, info); TRACE("devinfo %p, index %d, device_data %p\n", devinfo, index, device_data);
if(info==NULL) if (!(set = get_device_set(devinfo)))
return FALSE;
if (!device_data)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
if (devinfo && devinfo != INVALID_HANDLE_VALUE)
{ if (device_data->cbSize != sizeof(SP_DEVINFO_DATA))
struct DeviceInfoSet *list = devinfo;
if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
{
if (index < list->cDevices)
{ {
if (info->cbSize == sizeof(SP_DEVINFO_DATA)) SetLastError(ERROR_INVALID_USER_BUFFER);
return FALSE;
}
if (index >= set->cDevices)
{ {
struct device *device; SetLastError(ERROR_NO_MORE_ITEMS);
DWORD i = 0; return FALSE;
}
LIST_FOR_EACH_ENTRY(device, &list->devices, struct device, entry) LIST_FOR_EACH_ENTRY(device, &set->devices, struct device, entry)
{ {
if (i++ == index) if (i++ == index)
{ {
copy_device_data(info, device); copy_device_data(device_data, device);
break; break;
} }
} }
ret = TRUE;
} return TRUE;
else
SetLastError(ERROR_INVALID_USER_BUFFER);
}
else
SetLastError(ERROR_NO_MORE_ITEMS);
}
else
SetLastError(ERROR_INVALID_HANDLE);
}
else
SetLastError(ERROR_INVALID_HANDLE);
return ret;
} }
/*********************************************************************** /***********************************************************************
...@@ -2338,24 +2329,15 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(const GUID *class, PCWSTR enumstr, HWND p ...@@ -2338,24 +2329,15 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(const GUID *class, PCWSTR enumstr, HWND p
/*********************************************************************** /***********************************************************************
* SetupDiGetDeviceInfoListDetailA (SETUPAPI.@) * SetupDiGetDeviceInfoListDetailA (SETUPAPI.@)
*/ */
BOOL WINAPI SetupDiGetDeviceInfoListDetailA( BOOL WINAPI SetupDiGetDeviceInfoListDetailA(HDEVINFO devinfo, SP_DEVINFO_LIST_DETAIL_DATA_A *DevInfoData)
HDEVINFO DeviceInfoSet,
PSP_DEVINFO_LIST_DETAIL_DATA_A DevInfoData )
{ {
struct DeviceInfoSet *set = DeviceInfoSet; struct DeviceInfoSet *set;
TRACE("%p %p\n", DeviceInfoSet, DevInfoData); TRACE("devinfo %p, detail_data %p.\n", devinfo, DevInfoData);
if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE) if (!(set = get_device_set(devinfo)))
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
}
if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (!DevInfoData || if (!DevInfoData ||
DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A)) DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A))
{ {
...@@ -2371,24 +2353,15 @@ BOOL WINAPI SetupDiGetDeviceInfoListDetailA( ...@@ -2371,24 +2353,15 @@ BOOL WINAPI SetupDiGetDeviceInfoListDetailA(
/*********************************************************************** /***********************************************************************
* SetupDiGetDeviceInfoListDetailW (SETUPAPI.@) * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
*/ */
BOOL WINAPI SetupDiGetDeviceInfoListDetailW( BOOL WINAPI SetupDiGetDeviceInfoListDetailW(HDEVINFO devinfo, SP_DEVINFO_LIST_DETAIL_DATA_W *DevInfoData)
HDEVINFO DeviceInfoSet,
PSP_DEVINFO_LIST_DETAIL_DATA_W DevInfoData )
{ {
struct DeviceInfoSet *set = DeviceInfoSet; struct DeviceInfoSet *set;
TRACE("%p %p\n", DeviceInfoSet, DevInfoData); TRACE("devinfo %p, detail_data %p.\n", devinfo, DevInfoData);
if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE) if (!(set = get_device_set(devinfo)))
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
}
if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (!DevInfoData || if (!DevInfoData ||
DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W)) DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
{ {
...@@ -2624,26 +2597,14 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo, ...@@ -2624,26 +2597,14 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo,
SP_DEVINFO_DATA *device_data, const GUID *class, DWORD index, SP_DEVINFO_DATA *device_data, const GUID *class, DWORD index,
SP_DEVICE_INTERFACE_DATA *iface_data) SP_DEVICE_INTERFACE_DATA *iface_data)
{ {
struct DeviceInfoSet *set = devinfo; struct DeviceInfoSet *set;
struct device *device; struct device *device;
struct device_iface *iface; struct device_iface *iface;
DWORD i = 0; DWORD i = 0;
TRACE("%p, %p, %s, %u, %p\n", devinfo, device_data, debugstr_guid(class), TRACE("devinfo %p, device_data %p, class %s, index %u, iface_data %p.\n",
index, iface_data); devinfo, device_data, debugstr_guid(class), index, iface_data);
if (!devinfo || devinfo == INVALID_HANDLE_VALUE ||
set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (device_data && (device_data->cbSize != sizeof(SP_DEVINFO_DATA) ||
!device_data->Reserved))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (!iface_data || iface_data->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA)) if (!iface_data || iface_data->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
...@@ -2656,7 +2617,8 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo, ...@@ -2656,7 +2617,8 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo,
if (device_data) if (device_data)
{ {
device = (struct device *)device_data->Reserved; if (!(device = get_device(devinfo, device_data)))
return FALSE;
LIST_FOR_EACH_ENTRY(iface, &device->interfaces, struct device_iface, entry) LIST_FOR_EACH_ENTRY(iface, &device->interfaces, struct device_iface, entry)
{ {
...@@ -2673,6 +2635,9 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo, ...@@ -2673,6 +2635,9 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo,
} }
else else
{ {
if (!(set = get_device_set(devinfo)))
return FALSE;
LIST_FOR_EACH_ENTRY(device, &set->devices, struct device, entry) LIST_FOR_EACH_ENTRY(device, &set->devices, struct device, entry)
{ {
LIST_FOR_EACH_ENTRY(iface, &device->interfaces, struct device_iface, entry) LIST_FOR_EACH_ENTRY(iface, &device->interfaces, struct device_iface, entry)
...@@ -2708,31 +2673,21 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo, ...@@ -2708,31 +2673,21 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(HDEVINFO devinfo,
*/ */
BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo) BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
{ {
BOOL ret = FALSE; struct DeviceInfoSet *set;
struct device *device, *device2;
TRACE("%p\n", devinfo); TRACE("devinfo %p.\n", devinfo);
if (devinfo && devinfo != INVALID_HANDLE_VALUE)
{
struct DeviceInfoSet *list = devinfo;
if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC) if (!(set = get_device_set(devinfo)))
{ return FALSE;
struct device *device, *device2;
LIST_FOR_EACH_ENTRY_SAFE(device, device2, &list->devices, LIST_FOR_EACH_ENTRY_SAFE(device, device2, &set->devices, struct device, entry)
struct device, entry)
{ {
SETUPDI_RemoveDevice(device); SETUPDI_RemoveDevice(device);
} }
HeapFree(GetProcessHeap(), 0, list); heap_free(set);
ret = TRUE;
}
}
if (!ret) return TRUE;
SetLastError(ERROR_INVALID_HANDLE);
return ret;
} }
/*********************************************************************** /***********************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment