From dc1884f5d8308efde61b9bbc4e2ea21eb82e4997 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 20 Jun 2018 18:52:54 +0100 Subject: [PATCH] Do not assume pointers are 32-bit in several Windows device calls. --- DiscImageChef.Devices/Windows/Command.cs | 2 +- DiscImageChef.Devices/Windows/Usb.cs | 28 +++++++++---------- DiscImageChef.Devices/Windows/UsbFunctions.cs | 4 +-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/DiscImageChef.Devices/Windows/Command.cs b/DiscImageChef.Devices/Windows/Command.cs index 1f701873..b25f1b3d 100644 --- a/DiscImageChef.Devices/Windows/Command.cs +++ b/DiscImageChef.Devices/Windows/Command.cs @@ -737,7 +737,7 @@ namespace DiscImageChef.Devices.Windows commandData.deviceDataBufferSize]; IntPtr hBuf = Marshal.AllocHGlobal(commandB.Length); Marshal.StructureToPtr(commandData, hBuf, true); - IntPtr descriptorOffset = new IntPtr(hBuf.ToInt32() + commandData.size); + IntPtr descriptorOffset = IntPtr.Add(hBuf, commandData.size); Marshal.StructureToPtr(commandDescriptor, descriptorOffset, true); Marshal.Copy(hBuf, commandB, 0, commandB.Length); Marshal.FreeHGlobal(hBuf); diff --git a/DiscImageChef.Devices/Windows/Usb.cs b/DiscImageChef.Devices/Windows/Usb.cs index d55e8f82..3786cab1 100644 --- a/DiscImageChef.Devices/Windows/Usb.cs +++ b/DiscImageChef.Devices/Windows/Usb.cs @@ -58,7 +58,7 @@ namespace DiscImageChef.Devices.Windows // We start at the "root" of the device tree and look for all // devices that match the interface GUID of a Hub Controller IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return new ReadOnlyCollection(hostList); + if(h == INVALID_HANDLE_VALUE) return new ReadOnlyCollection(hostList); IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); bool success; @@ -128,7 +128,7 @@ namespace DiscImageChef.Devices.Windows // Use the "enumerator form" of the SetupDiGetClassDevs API // to generate a list of all USB devices IntPtr h = SetupDiGetClassDevs(0, DEV_ENUM, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans; + if(h == INVALID_HANDLE_VALUE) return ans; IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); @@ -184,7 +184,7 @@ namespace DiscImageChef.Devices.Windows // Use the "enumerator form" of the SetupDiGetClassDevs API // to generate a list of all USB devices IntPtr h = SetupDiGetClassDevs(0, DEV_ENUM, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans; + if(h == INVALID_HANDLE_VALUE) return ans; IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); @@ -271,7 +271,7 @@ namespace DiscImageChef.Devices.Windows // Open a handle to the Host Controller h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return root; + if(h == INVALID_HANDLE_VALUE) return root; UsbRootHubName hubName = new UsbRootHubName(); int nBytes = Marshal.SizeOf(hubName); @@ -290,7 +290,7 @@ namespace DiscImageChef.Devices.Windows // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h2.ToInt32() != INVALID_HANDLE_VALUE) + if(h2 != INVALID_HANDLE_VALUE) { UsbNodeInformation nodeInfo = new UsbNodeInformation {NodeType = (int)UsbHubNode.UsbHub}; nBytes = Marshal.SizeOf(nodeInfo); @@ -379,7 +379,7 @@ namespace DiscImageChef.Devices.Windows // Open a handle to the Hub device IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return new ReadOnlyCollection(portList); + if(h == INVALID_HANDLE_VALUE) return new ReadOnlyCollection(portList); int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx)); IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes); @@ -482,7 +482,7 @@ namespace DiscImageChef.Devices.Windows // Open a handle to the Hub device IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return device; + if(h == INVALID_HANDLE_VALUE) return device; int nBytesReturned; int nBytes = BUFFER_SIZE; @@ -519,7 +519,7 @@ namespace DiscImageChef.Devices.Windows // the Request structure. Because this location is not "covered" // by the structure allocation, we're forced to zero out this // chunk of memory by using the StringToHGlobalAuto() hack above - IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request)); + IntPtr ptrStringDesc = IntPtr.Add(ptrRequest, Marshal.SizeOf(request)); UsbStringDescriptor stringDesc = (UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor)); device.DeviceManufacturer = stringDesc.bString; @@ -549,7 +549,7 @@ namespace DiscImageChef.Devices.Windows nBytes, out nBytesReturned, IntPtr.Zero)) { // the location of the string descriptor is immediately after the Request structure - IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request)); + IntPtr ptrStringDesc = IntPtr.Add(ptrRequest, Marshal.SizeOf(request)); UsbStringDescriptor stringDesc = (UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor)); device.DeviceProduct = stringDesc.bString; @@ -579,7 +579,7 @@ namespace DiscImageChef.Devices.Windows nBytes, out nBytesReturned, IntPtr.Zero)) { // the location of the string descriptor is immediately after the Request structure - IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request)); + IntPtr ptrStringDesc = IntPtr.Add(ptrRequest, Marshal.SizeOf(request)); UsbStringDescriptor stringDesc = (UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor)); device.DeviceSerialNumber = stringDesc.bString; @@ -602,7 +602,7 @@ namespace DiscImageChef.Devices.Windows if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes, dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero)) { - IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest)); + IntPtr ptrStringDesc = IntPtr.Add(dcrPtrRequest, Marshal.SizeOf(dcrRequest)); device.BinaryDeviceDescriptors = new byte[nBytesReturned]; Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned); } @@ -650,7 +650,7 @@ namespace DiscImageChef.Devices.Windows // Open a handle to the Host Controller h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return hub; + if(h == INVALID_HANDLE_VALUE) return hub; // Get the DevicePath for downstream hub UsbNodeConnectionName nodeName = new UsbNodeConnectionName {ConnectionIndex = PortPortNumber}; int nBytes = Marshal.SizeOf(nodeName); @@ -669,7 +669,7 @@ namespace DiscImageChef.Devices.Windows // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h2.ToInt32() != INVALID_HANDLE_VALUE) + if(h2 != INVALID_HANDLE_VALUE) { UsbNodeInformation nodeInfo = new UsbNodeInformation {NodeType = (int)UsbHubNode.UsbHub}; nBytes = Marshal.SizeOf(nodeInfo); @@ -759,7 +759,7 @@ namespace DiscImageChef.Devices.Windows const int FILE_SHARE_READ = 0x1; const int FILE_SHARE_WRITE = 0x2; const int OPEN_EXISTING = 0x3; - const int INVALID_HANDLE_VALUE = -1; + static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424; const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408; diff --git a/DiscImageChef.Devices/Windows/UsbFunctions.cs b/DiscImageChef.Devices/Windows/UsbFunctions.cs index d9111c16..a64472bf 100644 --- a/DiscImageChef.Devices/Windows/UsbFunctions.cs +++ b/DiscImageChef.Devices/Windows/UsbFunctions.cs @@ -209,7 +209,7 @@ namespace DiscImageChef.Devices.Windows // We start at the "root" of the device tree and look for all // devices that match the interface GUID of a disk IntPtr h = SetupDiGetClassDevs(ref diskGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); - if(h.ToInt32() != INVALID_HANDLE_VALUE) + if(h != INVALID_HANDLE_VALUE) { bool success; int i = 0; @@ -274,7 +274,7 @@ namespace DiscImageChef.Devices.Windows int ans = -1; IntPtr h = CreateFile(devicePath.TrimEnd('\\'), 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); - if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans; + if(h == INVALID_HANDLE_VALUE) return ans; StorageDeviceNumber sdn = new StorageDeviceNumber(); int nBytes = Marshal.SizeOf(sdn);