diff --git a/DiscImageChef.Partitions/Acorn.cs b/DiscImageChef.Partitions/Acorn.cs index 9d0ea7441..fc3524a4b 100644 --- a/DiscImageChef.Partitions/Acorn.cs +++ b/DiscImageChef.Partitions/Acorn.cs @@ -36,6 +36,7 @@ using System.Runtime.InteropServices; using System.Text; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -70,10 +71,7 @@ namespace DiscImageChef.Partitions if(sector.Length < 512) return false; - IntPtr bbPtr = Marshal.AllocHGlobal(512); - Marshal.Copy(sector, 0, bbPtr, 512); - AcornBootBlock bootBlock = (AcornBootBlock)Marshal.PtrToStructure(bbPtr, typeof(AcornBootBlock)); - Marshal.FreeHGlobal(bbPtr); + AcornBootBlock bootBlock = Marshal.ByteArrayToStructureLittleEndian(sector); int checksum = 0; for(int i = 0; i < 0x1FF; i++) checksum = (checksum & 0xFF) + (checksum >> 8) + sector[i]; @@ -112,10 +110,7 @@ namespace DiscImageChef.Partitions { case TYPE_LINUX: { - IntPtr tablePtr = Marshal.AllocHGlobal(512); - Marshal.Copy(map, 0, tablePtr, 512); - LinuxTable table = (LinuxTable)Marshal.PtrToStructure(tablePtr, typeof(LinuxTable)); - Marshal.FreeHGlobal(tablePtr); + LinuxTable table = Marshal.ByteArrayToStructureLittleEndian(map); foreach(LinuxEntry entry in table.entries) { @@ -139,10 +134,7 @@ namespace DiscImageChef.Partitions case TYPE_RISCIX_MFM: case TYPE_RISCIX_SCSI: { - IntPtr tablePtr = Marshal.AllocHGlobal(512); - Marshal.Copy(map, 0, tablePtr, 512); - RiscIxTable table = (RiscIxTable)Marshal.PtrToStructure(tablePtr, typeof(RiscIxTable)); - Marshal.FreeHGlobal(tablePtr); + RiscIxTable table = Marshal.ByteArrayToStructureLittleEndian(map); if(table.magic == RISCIX_MAGIC) foreach(RiscIxEntry entry in table.partitions) diff --git a/DiscImageChef.Partitions/Apricot.cs b/DiscImageChef.Partitions/Apricot.cs index 93a3e8955..8c32e83bf 100644 --- a/DiscImageChef.Partitions/Apricot.cs +++ b/DiscImageChef.Partitions/Apricot.cs @@ -36,6 +36,7 @@ using System.Runtime.InteropServices; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -73,10 +74,7 @@ namespace DiscImageChef.Partitions if(sector.Length < 512) return false; - IntPtr lblPtr = Marshal.AllocHGlobal(512); - Marshal.Copy(sector, 0, lblPtr, 512); - ApricotLabel label = (ApricotLabel)Marshal.PtrToStructure(lblPtr, typeof(ApricotLabel)); - Marshal.FreeHGlobal(lblPtr); + ApricotLabel label = Marshal.ByteArrayToStructureLittleEndian(sector); // Not much to check but... ulong deviceSectors = imagePlugin.Info.Sectors; @@ -273,15 +271,18 @@ namespace DiscImageChef.Partitions /// Operating system. public byte operatingSystem; /// Software write protection. - [MarshalAs(UnmanagedType.U1)] public bool writeProtected; + [MarshalAs(UnmanagedType.U1)] + public bool writeProtected; /// Copy protected. - [MarshalAs(UnmanagedType.U1)] public bool copyProtected; + [MarshalAs(UnmanagedType.U1)] + public bool copyProtected; /// Boot type. public byte bootType; /// Partitions. public byte partitionCount; /// Is hard disk?. - [MarshalAs(UnmanagedType.U1)] public bool winchester; + [MarshalAs(UnmanagedType.U1)] + public bool winchester; /// Sector size. public ushort sectorSize; /// Sectors per track. @@ -334,15 +335,18 @@ namespace DiscImageChef.Partitions /// Major BIOS version. public byte biosMajorVersion; /// Diagnostics enabled?. - [MarshalAs(UnmanagedType.U1)] public bool diagnosticsFlag; + [MarshalAs(UnmanagedType.U1)] + public bool diagnosticsFlag; /// Printer device. public byte prnDevice; /// Bell volume. public byte bellVolume; /// Cache enabled?. - [MarshalAs(UnmanagedType.U1)] public bool enableCache; + [MarshalAs(UnmanagedType.U1)] + public bool enableCache; /// Graphics enabled?. - [MarshalAs(UnmanagedType.U1)] public bool enableGraphics; + [MarshalAs(UnmanagedType.U1)] + public bool enableGraphics; /// Length in sectors of DOS. public byte dosLength; /// Length in sectors of FONT file. @@ -358,7 +362,8 @@ namespace DiscImageChef.Partitions /// Keyboard click volume. public byte keyboardVolume; /// Auto-repeat enabled?. - [MarshalAs(UnmanagedType.U1)] public bool autorepeat; + [MarshalAs(UnmanagedType.U1)] + public bool autorepeat; /// Auto-repeat lead-in. public byte autorepeatLeadIn; /// Auto-repeat interval. @@ -373,7 +378,8 @@ namespace DiscImageChef.Partitions /// Screen line width. public byte lineWidth; /// Screen disabled?. - [MarshalAs(UnmanagedType.U1)] public bool imageOff; + [MarshalAs(UnmanagedType.U1)] + public bool imageOff; /// Spare area for screen values expansion. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 13)] public byte[] spareScreen; @@ -388,13 +394,16 @@ namespace DiscImageChef.Partitions /// Stop bits. public byte stopBits; /// Parity enabled?. - [MarshalAs(UnmanagedType.U1)] public bool parityCheck; + [MarshalAs(UnmanagedType.U1)] + public bool parityCheck; /// Parity type. public byte parityType; /// Xon/Xoff enabled on TX. - [MarshalAs(UnmanagedType.U1)] public bool txXonXoff; + [MarshalAs(UnmanagedType.U1)] + public bool txXonXoff; /// Xon/Xoff enabled on RX. - [MarshalAs(UnmanagedType.U1)] public bool rxXonXoff; + [MarshalAs(UnmanagedType.U1)] + public bool rxXonXoff; /// Xon character. public byte xonCharacter; /// Xoff character. @@ -402,30 +411,39 @@ namespace DiscImageChef.Partitions /// Xon/Xoff buffer on RX. public ushort rxXonXoffBuffer; /// DTR/DSR enabled?. - [MarshalAs(UnmanagedType.U1)] public bool dtrDsr; + [MarshalAs(UnmanagedType.U1)] + public bool dtrDsr; /// CTS/RTS enabled?. - [MarshalAs(UnmanagedType.U1)] public bool ctsRts; + [MarshalAs(UnmanagedType.U1)] + public bool ctsRts; /// NULLs after CR. public byte nullsAfterCr; /// NULLs after 0xFF. public byte nullsAfterFF; /// Send LF after CR in serial port. - [MarshalAs(UnmanagedType.U1)] public bool lfAfterCRSerial; + [MarshalAs(UnmanagedType.U1)] + public bool lfAfterCRSerial; /// BIOS error report in serial port. - [MarshalAs(UnmanagedType.U1)] public bool biosErrorReportSerial; + [MarshalAs(UnmanagedType.U1)] + public bool biosErrorReportSerial; /// Spare area for serial port values expansion. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 13)] public byte[] spareSerial; /// Send LF after CR in parallel port. - [MarshalAs(UnmanagedType.U1)] public bool lfAfterCrParallel; + [MarshalAs(UnmanagedType.U1)] + public bool lfAfterCrParallel; /// Select line supported?. - [MarshalAs(UnmanagedType.U1)] public bool selectLine; + [MarshalAs(UnmanagedType.U1)] + public bool selectLine; /// Paper empty supported?. - [MarshalAs(UnmanagedType.U1)] public bool paperEmpty; + [MarshalAs(UnmanagedType.U1)] + public bool paperEmpty; /// Fault line supported?. - [MarshalAs(UnmanagedType.U1)] public bool faultLine; + [MarshalAs(UnmanagedType.U1)] + public bool faultLine; /// BIOS error report in parallel port. - [MarshalAs(UnmanagedType.U1)] public bool biosErrorReportParallel; + [MarshalAs(UnmanagedType.U1)] + public bool biosErrorReportParallel; /// Spare area for parallel port values expansion. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)] public byte[] spareParallel; @@ -433,9 +451,11 @@ namespace DiscImageChef.Partitions [MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)] public byte[] spareWinchester; /// Parking enabled?. - [MarshalAs(UnmanagedType.U1)] public bool parkingEnabled; + [MarshalAs(UnmanagedType.U1)] + public bool parkingEnabled; /// Format protection?. - [MarshalAs(UnmanagedType.U1)] public bool formatProtection; + [MarshalAs(UnmanagedType.U1)] + public bool formatProtection; /// Spare area for RAM disk values expansion. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] spareRamDisk; diff --git a/DiscImageChef.Partitions/BSD.cs b/DiscImageChef.Partitions/BSD.cs index b40c2ed23..6654b028c 100644 --- a/DiscImageChef.Partitions/BSD.cs +++ b/DiscImageChef.Partitions/BSD.cs @@ -37,6 +37,7 @@ using System.Runtime.InteropServices; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -73,7 +74,7 @@ namespace DiscImageChef.Partitions { byte[] sector = new byte[MAX_LABEL_SIZE]; Array.Copy(tmp, offset, sector, 0, MAX_LABEL_SIZE); - dl = GetDiskLabel(sector); + dl = Marshal.ByteArrayToStructureLittleEndian(sector); DicConsole.DebugWriteLine("BSD plugin", "dl.magic on sector {0} at offset {1} = 0x{2:X8} (expected 0x{3:X8})", location + sectorOffset, offset, dl.d_magic, DISKMAGIC); @@ -206,21 +207,13 @@ namespace DiscImageChef.Partitions } } - static DiskLabel GetDiskLabel(byte[] disklabel) - { - GCHandle handle = GCHandle.Alloc(disklabel, GCHandleType.Pinned); - DiskLabel dl = (DiskLabel)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DiskLabel)); - handle.Free(); - return dl; - } - static DiskLabel SwapDiskLabel(DiskLabel dl) { - dl = (DiskLabel)Helpers.Marshal.SwapStructureMembersEndian(dl); + dl = (DiskLabel)Marshal.SwapStructureMembersEndian(dl); for(int i = 0; i < dl.d_drivedata.Length; i++) dl.d_drivedata[i] = Swapping.Swap(dl.d_drivedata[i]); for(int i = 0; i < dl.d_spare.Length; i++) dl.d_spare[i] = Swapping.Swap(dl.d_spare[i]); for(int i = 0; i < dl.d_partitions.Length; i++) - dl.d_partitions[i] = (BSDPartition)Helpers.Marshal.SwapStructureMembersEndian(dl.d_partitions[i]); + dl.d_partitions[i] = (BSDPartition)Marshal.SwapStructureMembersEndian(dl.d_partitions[i]); return dl; } diff --git a/DiscImageChef.Partitions/DEC.cs b/DiscImageChef.Partitions/DEC.cs index a2cb7a7c0..8170cb977 100644 --- a/DiscImageChef.Partitions/DEC.cs +++ b/DiscImageChef.Partitions/DEC.cs @@ -36,6 +36,7 @@ using System.Linq; using System.Runtime.InteropServices; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -57,10 +58,7 @@ namespace DiscImageChef.Partitions byte[] sector = imagePlugin.ReadSector(31 + sectorOffset); if(sector.Length < 512) return false; - IntPtr tablePtr = Marshal.AllocHGlobal(512); - Marshal.Copy(sector, 0, tablePtr, 512); - DECLabel table = (DECLabel)Marshal.PtrToStructure(tablePtr, typeof(DECLabel)); - Marshal.FreeHGlobal(tablePtr); + DECLabel table = Marshal.ByteArrayToStructureLittleEndian(sector); if(table.pt_magic != PT_MAGIC || table.pt_valid != PT_VALID) return false; diff --git a/DiscImageChef.Partitions/DragonFlyBSD.cs b/DiscImageChef.Partitions/DragonFlyBSD.cs index b2623cece..e9ee0f390 100644 --- a/DiscImageChef.Partitions/DragonFlyBSD.cs +++ b/DiscImageChef.Partitions/DragonFlyBSD.cs @@ -35,6 +35,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -57,10 +58,7 @@ namespace DiscImageChef.Partitions byte[] sectors = imagePlugin.ReadSectors(sectorOffset, nSectors); if(sectors.Length < 2048) return false; - IntPtr labelPtr = Marshal.AllocHGlobal(2048); - Marshal.Copy(sectors, 0, labelPtr, 2048); - Disklabel64 disklabel = (Disklabel64)Marshal.PtrToStructure(labelPtr, typeof(Disklabel64)); - Marshal.FreeHGlobal(labelPtr); + Disklabel64 disklabel = Marshal.ByteArrayToStructureLittleEndian(sectors); if(disklabel.d_magic != 0xC4464C59) return false; diff --git a/DiscImageChef.Partitions/GPT.cs b/DiscImageChef.Partitions/GPT.cs index 2bdf11def..7226b2039 100644 --- a/DiscImageChef.Partitions/GPT.cs +++ b/DiscImageChef.Partitions/GPT.cs @@ -38,6 +38,7 @@ using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Enums; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -83,12 +84,7 @@ namespace DiscImageChef.Partitions else return false; - try - { - GCHandle handle = GCHandle.Alloc(hdrBytes, GCHandleType.Pinned); - hdr = (GptHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(GptHeader)); - handle.Free(); - } + try { hdr = Marshal.ByteArrayToStructureLittleEndian(hdrBytes); } catch { return false; } DicConsole.DebugWriteLine("GPT Plugin", "hdr.revision = 0x{0:X8}", hdr.revision); @@ -138,10 +134,7 @@ namespace DiscImageChef.Partitions { byte[] entryBytes = new byte[hdr.entriesSize]; Array.Copy(entriesBytes, hdr.entriesSize * i, entryBytes, 0, hdr.entriesSize); - GCHandle handle = GCHandle.Alloc(entryBytes, GCHandleType.Pinned); - GptEntry entry = (GptEntry)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(GptEntry)); - handle.Free(); - entries.Add(entry); + entries.Add(Marshal.ByteArrayToStructureLittleEndian(entryBytes)); } #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body catch diff --git a/DiscImageChef.Partitions/MBR.cs b/DiscImageChef.Partitions/MBR.cs index fd5e1d107..18be0c991 100644 --- a/DiscImageChef.Partitions/MBR.cs +++ b/DiscImageChef.Partitions/MBR.cs @@ -38,7 +38,7 @@ using DiscImageChef.CommonTypes.Enums; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; using DiscImageChef.Helpers; -using Marshal = System.Runtime.InteropServices.Marshal; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -210,24 +210,14 @@ namespace DiscImageChef.Partitions byte[] sector = imagePlugin.ReadSector(sectorOffset); - GCHandle handle = GCHandle.Alloc(sector, GCHandleType.Pinned); - MasterBootRecord mbr = - (MasterBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(MasterBootRecord)); - TimedMasterBootRecord mbrTime = - (TimedMasterBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), - typeof(TimedMasterBootRecord)); + MasterBootRecord mbr = Marshal.ByteArrayToStructureLittleEndian(sector); + TimedMasterBootRecord mbrTime = Marshal.ByteArrayToStructureLittleEndian(sector); SerializedMasterBootRecord mbrSerial = - (SerializedMasterBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), - typeof(SerializedMasterBootRecord)); - ModernMasterBootRecord mbrModern = - (ModernMasterBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), - typeof(ModernMasterBootRecord)); - NecMasterBootRecord mbrNec = - (NecMasterBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NecMasterBootRecord)); + Marshal.ByteArrayToStructureLittleEndian(sector); + ModernMasterBootRecord mbrModern = Marshal.ByteArrayToStructureLittleEndian(sector); + NecMasterBootRecord mbrNec = Marshal.ByteArrayToStructureLittleEndian(sector); DiskManagerMasterBootRecord mbrOntrack = - (DiskManagerMasterBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), - typeof(DiskManagerMasterBootRecord)); - handle.Free(); + Marshal.ByteArrayToStructureLittleEndian(sector); DicConsole.DebugWriteLine("MBR plugin", "xmlmedia = {0}", imagePlugin.Info.XmlMediaType); DicConsole.DebugWriteLine("MBR plugin", "mbr.magic = {0:X4}", mbr.magic); @@ -369,11 +359,7 @@ namespace DiscImageChef.Partitions { sector = imagePlugin.ReadSector(lbaStart); - handle = GCHandle.Alloc(sector, GCHandleType.Pinned); - ExtendedBootRecord ebr = - (ExtendedBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), - typeof(ExtendedBootRecord)); - handle.Free(); + ExtendedBootRecord ebr = Marshal.ByteArrayToStructureLittleEndian(sector); DicConsole.DebugWriteLine("MBR plugin", "ebr.magic == MBR_Magic = {0}", ebr.magic == MBR_MAGIC); @@ -493,10 +479,7 @@ namespace DiscImageChef.Partitions byte[] sector = imagePlugin.ReadSector(start); - GCHandle handle = GCHandle.Alloc(sector, GCHandleType.Pinned); - ExtendedBootRecord mnx = - (ExtendedBootRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ExtendedBootRecord)); - handle.Free(); + ExtendedBootRecord mnx = Marshal.ByteArrayToStructureLittleEndian(sector); DicConsole.DebugWriteLine("MBR plugin", "mnx.magic == MBR_Magic = {0}", mnx.magic == MBR_MAGIC); diff --git a/DiscImageChef.Partitions/PC98.cs b/DiscImageChef.Partitions/PC98.cs index af262102a..1b71bca95 100644 --- a/DiscImageChef.Partitions/PC98.cs +++ b/DiscImageChef.Partitions/PC98.cs @@ -38,7 +38,7 @@ using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; using DiscImageChef.Helpers; -using Marshal = System.Runtime.InteropServices.Marshal; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -61,10 +61,7 @@ namespace DiscImageChef.Partitions // Prevent false positives with some FAT BPBs if(Encoding.ASCII.GetString(bootSector, 0x36, 3) == "FAT") return false; - IntPtr tablePtr = Marshal.AllocHGlobal(256); - Marshal.Copy(sector, 0, tablePtr, 256); - PC98Table table = (PC98Table)Marshal.PtrToStructure(tablePtr, typeof(PC98Table)); - Marshal.FreeHGlobal(tablePtr); + PC98Table table = Marshal.ByteArrayToStructureLittleEndian(sector); ulong counter = 0; diff --git a/DiscImageChef.Partitions/RioKarma.cs b/DiscImageChef.Partitions/RioKarma.cs index 476cf753d..7eb83a6d9 100644 --- a/DiscImageChef.Partitions/RioKarma.cs +++ b/DiscImageChef.Partitions/RioKarma.cs @@ -36,6 +36,7 @@ using System.Linq; using System.Runtime.InteropServices; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -54,10 +55,7 @@ namespace DiscImageChef.Partitions byte[] sector = imagePlugin.ReadSector(sectorOffset); if(sector.Length < 512) return false; - IntPtr tablePtr = Marshal.AllocHGlobal(512); - Marshal.Copy(sector, 0, tablePtr, 512); - RioKarmaTable table = (RioKarmaTable)Marshal.PtrToStructure(tablePtr, typeof(RioKarmaTable)); - Marshal.FreeHGlobal(tablePtr); + RioKarmaTable table = Marshal.ByteArrayToStructureLittleEndian(sector); if(table.magic != KARMA_MAGIC) return false; diff --git a/DiscImageChef.Partitions/Sun.cs b/DiscImageChef.Partitions/Sun.cs index b24ac1047..e1c93b2f1 100644 --- a/DiscImageChef.Partitions/Sun.cs +++ b/DiscImageChef.Partitions/Sun.cs @@ -38,6 +38,7 @@ using System.Text; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -86,11 +87,9 @@ namespace DiscImageChef.Partitions byte[] sunSector = imagePlugin.ReadSector(sectorOffset); - GCHandle handle = GCHandle.Alloc(sunSector, GCHandleType.Pinned); - dk_label dkl = (dk_label)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(dk_label)); - dk_label8 dkl8 = (dk_label8)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(dk_label8)); - dk_label16 dkl16 = (dk_label16)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(dk_label16)); - handle.Free(); + dk_label dkl = Marshal.ByteArrayToStructureLittleEndian(sunSector); + dk_label8 dkl8 = Marshal.ByteArrayToStructureLittleEndian(sunSector); + dk_label16 dkl16 = Marshal.ByteArrayToStructureLittleEndian(sunSector); DicConsole.DebugWriteLine("Sun plugin", "dkl.dkl_magic = 0x{0:X4}", dkl.dkl_magic); DicConsole.DebugWriteLine("Sun plugin", "dkl8.dkl_vtoc.v_sanity = 0x{0:X8}", dkl8.dkl_vtoc.v_sanity); @@ -106,11 +105,9 @@ namespace DiscImageChef.Partitions { sunSector = imagePlugin.ReadSector(sectorOffset + 1); - handle = GCHandle.Alloc(sunSector, GCHandleType.Pinned); - dkl = (dk_label)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(dk_label)); - dkl8 = (dk_label8)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(dk_label8)); - dkl16 = (dk_label16)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(dk_label16)); - handle.Free(); + dkl = Marshal.ByteArrayToStructureLittleEndian(sunSector); + dkl8 = Marshal.ByteArrayToStructureLittleEndian(sunSector); + dkl16 = Marshal.ByteArrayToStructureLittleEndian(sunSector); if(dkl.dkl_magic == DKL_MAGIC || dkl.dkl_magic == DKL_CIGAM) if(dkl16.dkl_vtoc.v_sanity == VTOC_SANE || dkl16.dkl_vtoc.v_sanity == VTOC_ENAS) @@ -322,9 +319,9 @@ namespace DiscImageChef.Partitions static dk_label SwapDiskLabel(dk_label label) { DicConsole.DebugWriteLine("Sun plugin", "Swapping dk_label"); - label = (dk_label)Helpers.Marshal.SwapStructureMembersEndian(label); + label = (dk_label)Marshal.SwapStructureMembersEndian(label); for(int i = 0; i < label.dkl_map.Length; i++) - label.dkl_map[i] = (dk_map)Helpers.Marshal.SwapStructureMembersEndian(label.dkl_map[i]); + label.dkl_map[i] = (dk_map)Marshal.SwapStructureMembersEndian(label.dkl_map[i]); return label; } @@ -332,9 +329,9 @@ namespace DiscImageChef.Partitions static dk_label8 SwapDiskLabel(dk_label8 label) { DicConsole.DebugWriteLine("Sun plugin", "Swapping dk_label8"); - label = (dk_label8)Helpers.Marshal.SwapStructureMembersEndian(label); + label = (dk_label8)Marshal.SwapStructureMembersEndian(label); for(int i = 0; i < label.dkl_map.Length; i++) - label.dkl_map[i] = (dk_map)Helpers.Marshal.SwapStructureMembersEndian(label.dkl_map[i]); + label.dkl_map[i] = (dk_map)Marshal.SwapStructureMembersEndian(label.dkl_map[i]); for(int i = 0; i < label.dkl_vtoc.v_bootinfo.Length; i++) label.dkl_vtoc.v_bootinfo[i] = Swapping.Swap(label.dkl_vtoc.v_bootinfo[i]); @@ -355,7 +352,7 @@ namespace DiscImageChef.Partitions static dk_label16 SwapDiskLabel(dk_label16 label) { DicConsole.DebugWriteLine("Sun plugin", "Swapping dk_label16"); - label = (dk_label16)Helpers.Marshal.SwapStructureMembersEndian(label); + label = (dk_label16)Marshal.SwapStructureMembersEndian(label); for(int i = 0; i < label.dkl_vtoc.v_bootinfo.Length; i++) label.dkl_vtoc.v_bootinfo[i] = Swapping.Swap(label.dkl_vtoc.v_bootinfo[i]); for(int i = 0; i < label.dkl_vtoc.v_part.Length; i++) diff --git a/DiscImageChef.Partitions/VTOC.cs b/DiscImageChef.Partitions/VTOC.cs index 2756ea762..4ea3cb2c7 100644 --- a/DiscImageChef.Partitions/VTOC.cs +++ b/DiscImageChef.Partitions/VTOC.cs @@ -37,6 +37,7 @@ using System.Runtime.InteropServices; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -80,19 +81,16 @@ namespace DiscImageChef.Partitions PDInfo pd; PDInfoOld pdold; - GCHandle handle; if(magic == PD_MAGIC) { - handle = GCHandle.Alloc(pdsector, GCHandleType.Pinned); - pd = (PDInfo)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(PDInfo)); - pdold = (PDInfoOld)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(PDInfoOld)); - handle.Free(); + pd = Marshal.ByteArrayToStructureLittleEndian(pdsector); + pdold = Marshal.ByteArrayToStructureLittleEndian(pdsector); } else { - pd = Helpers.Marshal.ByteArrayToStructureBigEndian(pdsector); - pdold = Helpers.Marshal.ByteArrayToStructureBigEndian(pdsector); + pd = Marshal.ByteArrayToStructureBigEndian(pdsector); + pdold = Marshal.ByteArrayToStructureBigEndian(pdsector); } DicConsole.DebugWriteLine("VTOC plugin", "pdinfo.driveid = {0}", pd.driveid); @@ -149,15 +147,10 @@ namespace DiscImageChef.Partitions { magicFound = true; DicConsole.DebugWriteLine("VTOC plugin", "New VTOC found at {0}", pdloc + sectorOffset + 1); - if(magic == VTOC_SANE) - { - handle = GCHandle.Alloc(vtocsector, GCHandleType.Pinned); - vtoc = (vtoc)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(vtoc)); - handle.Free(); - } + if(magic == VTOC_SANE) vtoc = Marshal.ByteArrayToStructureLittleEndian(vtocsector); else { - vtoc = Helpers.Marshal.ByteArrayToStructureBigEndian(vtocsector); + vtoc = Marshal.ByteArrayToStructureBigEndian(vtocsector); for(int i = 0; i < vtoc.v_part.Length; i++) { vtoc.v_part[i].p_tag = (pTag)Swapping.Swap((ushort)vtoc.v_part[i].p_tag); @@ -178,15 +171,10 @@ namespace DiscImageChef.Partitions magicFound = true; useOld = true; DicConsole.DebugWriteLine("VTOC plugin", "Old VTOC found at {0}", pdloc + sectorOffset + 1); - if(magic == VTOC_SANE) - { - handle = GCHandle.Alloc(vtocsector, GCHandleType.Pinned); - vtocOld = (vtocold)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(vtocold)); - handle.Free(); - } + if(magic == VTOC_SANE) vtocOld = Marshal.ByteArrayToStructureLittleEndian(vtocsector); else { - vtocOld = Helpers.Marshal.ByteArrayToStructureBigEndian(vtocsector); + vtocOld = Marshal.ByteArrayToStructureBigEndian(vtocsector); for(int i = 0; i < vtocOld.v_part.Length; i++) { vtocOld.v_part[i].p_tag = (pTag)Swapping.Swap((ushort)vtocOld.v_part[i].p_tag); @@ -224,15 +212,10 @@ namespace DiscImageChef.Partitions { magicFound = true; DicConsole.DebugWriteLine("VTOC plugin", "New VTOC found."); - if(magic == VTOC_SANE) - { - handle = GCHandle.Alloc(vtocsector, GCHandleType.Pinned); - vtoc = (vtoc)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(vtoc)); - handle.Free(); - } + if(magic == VTOC_SANE) vtoc = Marshal.ByteArrayToStructureLittleEndian(vtocsector); else { - vtoc = Helpers.Marshal.ByteArrayToStructureBigEndian(vtocsector); + vtoc = Marshal.ByteArrayToStructureBigEndian(vtocsector); for(int i = 0; i < vtoc.v_part.Length; i++) { vtoc.v_part[i].p_tag = (pTag)Swapping.Swap((ushort)vtoc.v_part[i].p_tag); diff --git a/DiscImageChef.Partitions/XENIX.cs b/DiscImageChef.Partitions/XENIX.cs index 0b0c5ba8a..a4fce16e2 100644 --- a/DiscImageChef.Partitions/XENIX.cs +++ b/DiscImageChef.Partitions/XENIX.cs @@ -35,6 +35,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; +using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Partitions { @@ -60,9 +61,7 @@ namespace DiscImageChef.Partitions byte[] tblsector = imagePlugin.ReadSector(42 + sectorOffset); - GCHandle handle = GCHandle.Alloc(tblsector, GCHandleType.Pinned); - Partable xnxtbl = (Partable)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Partable)); - handle.Free(); + Partable xnxtbl = Marshal.ByteArrayToStructureLittleEndian(tblsector); DicConsole.DebugWriteLine("XENIX plugin", "xnxtbl.p_magic = 0x{0:X4} (should be 0x{1:X4})", xnxtbl.p_magic, PAMAGIC);