General code refactor and reformat.

This commit is contained in:
2018-12-31 13:17:27 +00:00
parent 6b12cd1a8e
commit f2caa8c40f
413 changed files with 3554 additions and 6549 deletions

View File

@@ -33,18 +33,172 @@
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Text;
using DiscImageChef.Checksums;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.Console;
using DiscImageChef.Checksums;
namespace DiscImageChef.DiscImages
{
public partial class WCDiskImage
{
public bool Open(IFilter imageFilter)
{
string comments = string.Empty;
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
byte[] header = new byte[32];
stream.Read(header, 0, 32);
IntPtr hdrPtr = Marshal.AllocHGlobal(32);
Marshal.Copy(header, 0, hdrPtr, 32);
WCDiskImageFileHeader fheader =
(WCDiskImageFileHeader)Marshal.PtrToStructure(hdrPtr, typeof(WCDiskImageFileHeader));
Marshal.FreeHGlobal(hdrPtr);
DicConsole.DebugWriteLine("d2f plugin",
"Detected WC DISK IMAGE with {0} heads, {1} tracks and {2} sectors per track.",
fheader.heads, fheader.cylinders, fheader.sectorsPerTrack);
imageInfo.Cylinders = fheader.cylinders;
imageInfo.SectorsPerTrack = fheader.sectorsPerTrack;
imageInfo.SectorSize = 512; // only 512 bytes per sector supported
imageInfo.Heads = fheader.heads;
imageInfo.Sectors = imageInfo.Heads * imageInfo.Cylinders * imageInfo.SectorsPerTrack;
imageInfo.ImageSize = imageInfo.Sectors * imageInfo.SectorSize;
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
imageInfo.MediaType = Geometry.GetMediaType(((ushort)imageInfo.Cylinders, (byte)imageInfo.Heads,
(ushort)imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
/* buffer the entire disk in memory */
for(int cyl = 0; cyl < imageInfo.Cylinders; cyl++)
{
for(int head = 0; head < imageInfo.Heads; head++) ReadTrack(stream, cyl, head);
}
/* if there are extra tracks, read them as well */
if(fheader.extraTracks[0] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 1 (head 0) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders, 0);
}
if(fheader.extraTracks[1] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 1 (head 1) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders, 1);
}
if(fheader.extraTracks[2] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 2 (head 0) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders + 1, 0);
}
if(fheader.extraTracks[3] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 2 (head 1) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders + 1, 1);
}
/* adjust number of cylinders */
if(fheader.extraTracks[0] == 1 || fheader.extraTracks[1] == 1) imageInfo.Cylinders++;
if(fheader.extraTracks[2] == 1 || fheader.extraTracks[3] == 1) imageInfo.Cylinders++;
/* read the comment and directory data if present */
if(fheader.extraFlags.HasFlag(ExtraFlag.Comment))
{
DicConsole.DebugWriteLine("d2f plugin", "Comment present, reading");
byte[] sheaderBuffer = new byte[6];
stream.Read(sheaderBuffer, 0, 6);
IntPtr sectPtr = Marshal.AllocHGlobal(6);
Marshal.Copy(sheaderBuffer, 0, sectPtr, 6);
WCDiskImageSectorHeader sheader =
(WCDiskImageSectorHeader)Marshal.PtrToStructure(sectPtr, typeof(WCDiskImageSectorHeader));
Marshal.FreeHGlobal(sectPtr);
if(sheader.flag != SectorFlag.Comment)
throw new InvalidDataException(string.Format("Invalid sector type '{0}' encountered",
sheader.flag.ToString()));
byte[] comm = new byte[sheader.crc];
stream.Read(comm, 0, sheader.crc);
comments += Encoding.ASCII.GetString(comm) + Environment.NewLine;
}
if(fheader.extraFlags.HasFlag(ExtraFlag.Directory))
{
DicConsole.DebugWriteLine("d2f plugin", "Directory listing present, reading");
byte[] sheaderBuffer = new byte[6];
stream.Read(sheaderBuffer, 0, 6);
IntPtr sectPtr = Marshal.AllocHGlobal(6);
Marshal.Copy(sheaderBuffer, 0, sectPtr, 6);
WCDiskImageSectorHeader sheader =
(WCDiskImageSectorHeader)Marshal.PtrToStructure(sectPtr, typeof(WCDiskImageSectorHeader));
Marshal.FreeHGlobal(sectPtr);
if(sheader.flag != SectorFlag.Directory)
throw new InvalidDataException(string.Format("Invalid sector type '{0}' encountered",
sheader.flag.ToString()));
byte[] dir = new byte[sheader.crc];
stream.Read(dir, 0, sheader.crc);
comments += Encoding.ASCII.GetString(dir);
}
if(comments.Length > 0) imageInfo.Comments = comments;
// save some variables for later use
fileHeader = fheader;
wcImageFilter = imageFilter;
return true;
}
public byte[] ReadSector(ulong sectorAddress)
{
int sectorNumber = (int)(sectorAddress % imageInfo.SectorsPerTrack) + 1;
int trackNumber = (int)(sectorAddress / imageInfo.SectorsPerTrack);
int headNumber = imageInfo.Heads > 1 ? trackNumber % 2 : 0;
int cylinderNumber = imageInfo.Heads > 1 ? trackNumber / 2 : trackNumber;
if(badSectors[(cylinderNumber, headNumber, sectorNumber)])
{
DicConsole.DebugWriteLine("d2f plugin", "reading bad sector {0} ({1},{2},{3})", sectorAddress,
cylinderNumber, headNumber, sectorNumber);
/* if we have sector data, return that */
if(sectorCache.ContainsKey((cylinderNumber, headNumber, sectorNumber)))
return sectorCache[(cylinderNumber, headNumber, sectorNumber)];
/* otherwise, return an empty sector */
return new byte[512];
}
return sectorCache[(cylinderNumber, headNumber, sectorNumber)];
}
public byte[] ReadSectors(ulong sectorAddress, uint length)
{
byte[] result = new byte[length * imageInfo.SectorSize];
if(sectorAddress + length > imageInfo.Sectors)
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
for(int i = 0; i < length; i++)
ReadSector(sectorAddress + (ulong)i).CopyTo(result, i * imageInfo.SectorSize);
return result;
}
/// <summary>
/// Read a whole track and cache it
/// </summary>
@@ -55,42 +209,44 @@ namespace DiscImageChef.DiscImages
{
byte[] sectorData;
byte[] crc;
short calculatedCRC;
short calculatedCRC;
for (int sect = 1; sect < imageInfo.SectorsPerTrack + 1; sect++)
for(int sect = 1; sect < imageInfo.SectorsPerTrack + 1; sect++)
{
/* read the sector header */
byte[] sheaderBuffer = new byte[6];
stream.Read(sheaderBuffer, 0, 6);
IntPtr sectPtr = Marshal.AllocHGlobal(6);
Marshal.Copy(sheaderBuffer, 0, sectPtr, 6);
WCDiskImageSectorHeader sheader = (WCDiskImageSectorHeader)Marshal.PtrToStructure(sectPtr, typeof(WCDiskImageSectorHeader));
WCDiskImageSectorHeader sheader =
(WCDiskImageSectorHeader)Marshal.PtrToStructure(sectPtr, typeof(WCDiskImageSectorHeader));
Marshal.FreeHGlobal(sectPtr);
/* validate the sector header */
if ((sheader.cylinder != cyl) || (sheader.head != head) || (sheader.sector != sect))
throw new InvalidDataException(String.Format("Unexpected sector encountered. Found CHS {0},{1},{2} but expected {3},{4},{5}",
sheader.cylinder, sheader.head, sheader.sector, cyl, head, sect));
if(sheader.cylinder != cyl || sheader.head != head || sheader.sector != sect)
throw new
InvalidDataException(string.Format("Unexpected sector encountered. Found CHS {0},{1},{2} but expected {3},{4},{5}",
sheader.cylinder, sheader.head, sheader.sector, cyl, head,
sect));
sectorData = new byte[512];
/* read the sector data */
switch (sheader.flag)
switch(sheader.flag)
{
case SectorFlag.Normal: /* read a normal sector and store it in cache */
stream.Read(sectorData, 0, 512);
sectorCache[(cyl, head, sect)] = sectorData;
Crc16Context.Data(sectorData, 512, out crc);
calculatedCRC = (short)(256 * crc[0] | crc[1]);
calculatedCRC = (short)((256 * crc[0]) | crc[1]);
/*
DicConsole.DebugWriteLine("d2f plugin", "CHS {0},{1},{2}: Regular sector, stored CRC=0x{3:x4}, calculated CRC=0x{4:x4}",
cyl, head, sect, sheader.crc, 256 * crc[0] + crc[1]);
*/
badSectors[(cyl, head, sect)] = sheader.crc != calculatedCRC;
if (calculatedCRC != sheader.crc)
{
DicConsole.DebugWriteLine("d2f plugin", "CHS {0},{1},{2}: CRC mismatch: stored CRC=0x{3:x4}, calculated CRC=0x{4:x4}",
cyl, head, sect, sheader.crc, calculatedCRC);
}
if(calculatedCRC != sheader.crc)
DicConsole.DebugWriteLine("d2f plugin",
"CHS {0},{1},{2}: CRC mismatch: stored CRC=0x{3:x4}, calculated CRC=0x{4:x4}",
cyl, head, sect, sheader.crc, calculatedCRC);
break;
case SectorFlag.BadSector:
@@ -106,169 +262,17 @@ namespace DiscImageChef.DiscImages
DicConsole.DebugWriteLine("d2f plugin", "CHS {0},{1},{2}: RepeatByte sector, fill byte 0x{0:x2}",
cyl, head, sect, sheader.crc & 0xff);
*/
for (int i = 0; i < 512; i++) sectorData[i] = (byte)(sheader.crc & 0xff);
for(int i = 0; i < 512; i++) sectorData[i] = (byte)(sheader.crc & 0xff);
sectorCache[(cyl, head, sect)] = sectorData;
badSectors[(cyl, head, sect)] = false;
badSectors[(cyl, head, sect)] = false;
break;
default:
throw new InvalidDataException(String.Format("Invalid sector type '{0}' encountered", sheader.flag.ToString()));
throw new InvalidDataException(string.Format("Invalid sector type '{0}' encountered",
sheader.flag.ToString()));
}
}
}
public bool Open(IFilter imageFilter)
{
string comments = String.Empty;
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
byte[] header = new byte[32];
stream.Read(header, 0, 32);
IntPtr hdrPtr = Marshal.AllocHGlobal(32);
Marshal.Copy(header, 0, hdrPtr, 32);
WCDiskImageFileHeader fheader = (WCDiskImageFileHeader)Marshal.PtrToStructure(hdrPtr, typeof(WCDiskImageFileHeader));
Marshal.FreeHGlobal(hdrPtr);
DicConsole.DebugWriteLine("d2f plugin",
"Detected WC DISK IMAGE with {0} heads, {1} tracks and {2} sectors per track.",
fheader.heads, fheader.cylinders, fheader.sectorsPerTrack);
imageInfo.Cylinders = (uint)fheader.cylinders;
imageInfo.SectorsPerTrack = fheader.sectorsPerTrack;
imageInfo.SectorSize = 512; // only 512 bytes per sector supported
imageInfo.Heads = fheader.heads;
imageInfo.Sectors = imageInfo.Heads * imageInfo.Cylinders * imageInfo.SectorsPerTrack;
imageInfo.ImageSize = imageInfo.Sectors * imageInfo.SectorSize;
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
imageInfo.MediaType = Geometry.GetMediaType(((ushort)imageInfo.Cylinders, (byte)imageInfo.Heads,
(ushort)imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
/* buffer the entire disk in memory */
for (int cyl = 0; cyl < imageInfo.Cylinders; cyl++)
{
for (int head = 0; head < imageInfo.Heads; head++)
{
ReadTrack(stream, cyl, head);
}
}
/* if there are extra tracks, read them as well */
if (fheader.extraTracks[0] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 1 (head 0) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders, 0);
}
if (fheader.extraTracks[1] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 1 (head 1) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders, 1);
}
if (fheader.extraTracks[2] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 2 (head 0) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders + 1, 0);
}
if (fheader.extraTracks[3] == 1)
{
DicConsole.DebugWriteLine("d2f plugin", "Extra track 2 (head 1) present, reading");
ReadTrack(stream, (int)imageInfo.Cylinders + 1, 1);
}
/* adjust number of cylinders */
if (fheader.extraTracks[0] == 1 || fheader.extraTracks[1] == 1)
imageInfo.Cylinders++;
if (fheader.extraTracks[2] == 1 || fheader.extraTracks[3] == 1)
imageInfo.Cylinders++;
/* read the comment and directory data if present */
if (fheader.extraFlags.HasFlag(ExtraFlag.Comment))
{
DicConsole.DebugWriteLine("d2f plugin", "Comment present, reading");
byte[] sheaderBuffer = new byte[6];
stream.Read(sheaderBuffer, 0, 6);
IntPtr sectPtr = Marshal.AllocHGlobal(6);
Marshal.Copy(sheaderBuffer, 0, sectPtr, 6);
WCDiskImageSectorHeader sheader = (WCDiskImageSectorHeader)Marshal.PtrToStructure(sectPtr, typeof(WCDiskImageSectorHeader));
Marshal.FreeHGlobal(sectPtr);
if (sheader.flag != SectorFlag.Comment)
throw new InvalidDataException(String.Format("Invalid sector type '{0}' encountered", sheader.flag.ToString()));
byte[] comm = new byte[sheader.crc];
stream.Read(comm, 0, sheader.crc);
comments += Encoding.ASCII.GetString(comm) + Environment.NewLine;
}
if (fheader.extraFlags.HasFlag(ExtraFlag.Directory))
{
DicConsole.DebugWriteLine("d2f plugin", "Directory listing present, reading");
byte[] sheaderBuffer = new byte[6];
stream.Read(sheaderBuffer, 0, 6);
IntPtr sectPtr = Marshal.AllocHGlobal(6);
Marshal.Copy(sheaderBuffer, 0, sectPtr, 6);
WCDiskImageSectorHeader sheader = (WCDiskImageSectorHeader)Marshal.PtrToStructure(sectPtr, typeof(WCDiskImageSectorHeader));
Marshal.FreeHGlobal(sectPtr);
if (sheader.flag != SectorFlag.Directory)
throw new InvalidDataException(String.Format("Invalid sector type '{0}' encountered", sheader.flag.ToString()));
byte[] dir = new byte[sheader.crc];
stream.Read(dir, 0, sheader.crc);
comments += Encoding.ASCII.GetString(dir);
}
if (comments.Length > 0)
imageInfo.Comments = comments;
// save some variables for later use
fileHeader = fheader;
wcImageFilter = imageFilter;
return true;
}
public byte[] ReadSector(ulong sectorAddress)
{
int sectorNumber = (int)(sectorAddress % imageInfo.SectorsPerTrack) + 1;
int trackNumber = (int)(sectorAddress / imageInfo.SectorsPerTrack);
int headNumber = imageInfo.Heads > 1 ? trackNumber % 2 : 0;
int cylinderNumber = imageInfo.Heads > 1 ? trackNumber / 2 : trackNumber;
if (badSectors[(cylinderNumber, headNumber, sectorNumber)])
{
DicConsole.DebugWriteLine("d2f plugin", "reading bad sector {0} ({1},{2},{3})",
sectorAddress, cylinderNumber, headNumber, sectorNumber);
/* if we have sector data, return that */
if (sectorCache.ContainsKey((cylinderNumber, headNumber, sectorNumber)))
{
return sectorCache[(cylinderNumber, headNumber, sectorNumber)];
}
/* otherwise, return an empty sector */
return new byte[512];
}
return sectorCache[(cylinderNumber, headNumber, sectorNumber)];
}
public byte[] ReadSectors(ulong sectorAddress, uint length)
{
byte[] result = new byte[length * imageInfo.SectorSize];
if (sectorAddress + length > imageInfo.Sectors)
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
for (int i = 0; i < length; i++)
ReadSector(sectorAddress + (ulong)i).CopyTo(result, i * imageInfo.SectorSize);
return result;
}
}
}