API: Legacy: Make icsneoFindDevices edge cases match icsneo40

pull/35/head
Paul Hollinsky 2021-06-14 14:41:54 -04:00
parent da4ab6ef62
commit a8562aca60
1 changed files with 22 additions and 12 deletions

View File

@ -166,41 +166,51 @@ static inline unsigned int GetVnetAgnosticNetid(size_t fullNetid)
//Basic Functions //Basic Functions
int LegacyDLLExport icsneoFindDevices(NeoDeviceEx *devs, int *devCount, unsigned int *devTypes, unsigned int devTypeCount, POptionsFindNeoEx *POptionsFindNeoEx, unsigned int *zero) int LegacyDLLExport icsneoFindDevices(NeoDeviceEx *devs, int *devCount, unsigned int *devTypes, unsigned int devTypeCount, POptionsFindNeoEx *POptionsFindNeoEx, unsigned int *zero)
{ {
if (!devs) if (!devs || !devCount)
return 0; return 0;
constexpr size_t MAX_DEVICES = 255; if (*devCount < 0 || *devCount > 255)
NeoDevice Nd[MAX_DEVICES]; return 0;
// Find the devices without filtering by the device type
// We allow this to find more than the requested number,
// as we may filter out some devices.
constexpr const size_t MAX_DEVICES = 255;
NeoDevice foundDevices[MAX_DEVICES];
int NumDevices = MAX_DEVICES; int NumDevices = MAX_DEVICES;
int Count = 0; int filteredDeviceCount = 0;
if (!icsneoFindNeoDevices(0, Nd, &NumDevices)) if (!icsneoFindNeoDevices(0, foundDevices, &NumDevices))
return 0; return 0;
for (auto i = 0; i < NumDevices; i++) for (auto i = 0; i < NumDevices; i++)
{ {
// Check if the next device would overrun the user's buffer
// We check this up here since the documentation allows zero
// to be specified.
if (filteredDeviceCount >= *devCount)
break;
if (devTypes && devTypeCount) if (devTypes && devTypeCount)
{ {
for (auto j = 0; j < devTypeCount; j++) for (auto j = 0; j < devTypeCount; j++)
{ {
if (Nd[i].DeviceType == devTypes[j]) if (foundDevices[i].DeviceType == devTypes[j])
{ {
devs[Count++].neoDevice = Nd[i]; devs[filteredDeviceCount++].neoDevice = foundDevices[i];
break; break;
} }
} }
} }
else else
{ {
devs[Count++].neoDevice = Nd[i]; devs[filteredDeviceCount++].neoDevice = foundDevices[i];
} }
} }
if (devCount) *devCount = filteredDeviceCount;
*devCount = Count; return 1; // If the function succeeds but no devices are found 1 will still be returned and devCount will equal 0
return NumDevices > 0;
} }
int LegacyDLLExport icsneoFindNeoDevices(unsigned long DeviceTypes, NeoDevice *pNeoDevice, int *pNumDevices) int LegacyDLLExport icsneoFindNeoDevices(unsigned long DeviceTypes, NeoDevice *pNeoDevice, int *pNumDevices)