mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Separate CRC16 IBM and CRC16 CCITT contexts, use cached tables.
This commit is contained in:
@@ -52,32 +52,41 @@ namespace DiscImageChef.DiscImages
|
||||
Stream stream = imageFilter.GetDataForkStream();
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
if(stream.Length < 512) return false;
|
||||
if(stream.Length < 512)
|
||||
return false;
|
||||
|
||||
byte[] headerB = new byte[256];
|
||||
stream.Read(headerB, 0, 256);
|
||||
CpcDiskInfo header = Marshal.ByteArrayToStructureLittleEndian<CpcDiskInfo>(headerB);
|
||||
|
||||
if(!cpcdskId.SequenceEqual(header.magic) && !edskId.SequenceEqual(header.magic) &&
|
||||
!du54Id.SequenceEqual(header.magic)) return false;
|
||||
if(!cpcdskId.SequenceEqual(header.magic) &&
|
||||
!edskId.SequenceEqual(header.magic) &&
|
||||
!du54Id.SequenceEqual(header.magic))
|
||||
return false;
|
||||
|
||||
extended = edskId.SequenceEqual(header.magic);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "Extended = {0}", extended);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.magic = \"{0}\"",
|
||||
StringHandlers.CToString(header.magic));
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.magic2 = \"{0}\"",
|
||||
StringHandlers.CToString(header.magic2));
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.creator = \"{0}\"",
|
||||
StringHandlers.CToString(header.creator));
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.tracks = {0}", header.tracks);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.sides = {0}", header.sides);
|
||||
if(!extended) DicConsole.DebugWriteLine("CPCDSK plugin", "header.tracksize = {0}", header.tracksize);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.sides = {0}", header.sides);
|
||||
|
||||
if(!extended)
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "header.tracksize = {0}", header.tracksize);
|
||||
else
|
||||
for(int i = 0; i < header.tracks; i++)
|
||||
{
|
||||
for(int j = 0; j < header.sides; j++)
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "Track {0} Side {1} size = {2}", i, j,
|
||||
header.tracksizeTable[i * header.sides + j] * 256);
|
||||
header.tracksizeTable[(i * header.sides) + j] * 256);
|
||||
}
|
||||
|
||||
ulong currentSector = 0;
|
||||
@@ -89,12 +98,14 @@ namespace DiscImageChef.DiscImages
|
||||
|
||||
// Seek to first track descriptor
|
||||
stream.Seek(256, SeekOrigin.Begin);
|
||||
|
||||
for(int i = 0; i < header.tracks; i++)
|
||||
{
|
||||
for(int j = 0; j < header.sides; j++)
|
||||
{
|
||||
// Track not stored in image
|
||||
if(extended && header.tracksizeTable[i * header.sides + j] == 0) continue;
|
||||
if(extended && header.tracksizeTable[(i * header.sides) + j] == 0)
|
||||
continue;
|
||||
|
||||
long trackPos = stream.Position;
|
||||
|
||||
@@ -105,26 +116,35 @@ namespace DiscImageChef.DiscImages
|
||||
if(!trackId.SequenceEqual(trackInfo.magic))
|
||||
{
|
||||
DicConsole.ErrorWriteLine("Not the expected track info.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].magic = \"{0}\"",
|
||||
StringHandlers.CToString(trackInfo.magic), i, j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].bps = {0}",
|
||||
SizeCodeToBytes(trackInfo.bps), i, j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].dataRate = {0}", trackInfo.dataRate,
|
||||
i, j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].filler = 0x{0:X2}", trackInfo.filler,
|
||||
i, j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].gap3 = 0x{0:X2}", trackInfo.gap3, i,
|
||||
j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].padding = {0}", trackInfo.padding, i,
|
||||
j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].recordingMode = {0}",
|
||||
trackInfo.recordingMode, i, j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sectors = {0}", trackInfo.sectors, i,
|
||||
j);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].side = {0}", trackInfo.side, i, j);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].side = {0}", trackInfo.side, i, j);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].track = {0}", trackInfo.track, i, j);
|
||||
|
||||
if(trackInfo.sectors != sectorsPerTrack)
|
||||
@@ -140,21 +160,26 @@ namespace DiscImageChef.DiscImages
|
||||
{
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].id = 0x{0:X2}",
|
||||
trackInfo.sectorsInfo[k - 1].id, i, j, k);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].len = {0}",
|
||||
trackInfo.sectorsInfo[k - 1].len, i, j, k);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].side = {0}",
|
||||
trackInfo.sectorsInfo[k - 1].side, i, j, k);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].size = {0}",
|
||||
SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size), i, j, k);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].st1 = 0x{0:X2}",
|
||||
trackInfo.sectorsInfo[k - 1].st1, i, j, k);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].st2 = 0x{0:X2}",
|
||||
trackInfo.sectorsInfo[k - 1].st2, i, j, k);
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].track = {0}",
|
||||
trackInfo.sectorsInfo[k - 1].track, i, j, k);
|
||||
|
||||
int sectLen = extended
|
||||
? trackInfo.sectorsInfo[k - 1].len
|
||||
int sectLen = extended ? trackInfo.sectorsInfo[k - 1].len
|
||||
: SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size);
|
||||
|
||||
byte[] sector = new byte[sectLen];
|
||||
@@ -185,11 +210,11 @@ namespace DiscImageChef.DiscImages
|
||||
amForCrc[6] = trackInfo.sectorsInfo[k - 1].id;
|
||||
amForCrc[7] = (byte)trackInfo.sectorsInfo[k - 1].size;
|
||||
|
||||
Crc16Context.Data(amForCrc, 8, out byte[] amCrc);
|
||||
CRC16IBMContext.Data(amForCrc, 8, out byte[] amCrc);
|
||||
|
||||
byte[] addressMark = new byte[22];
|
||||
Array.Copy(amForCrc, 0, addressMark, 12, 8);
|
||||
Array.Copy(amCrc, 0, addressMark, 20, 2);
|
||||
Array.Copy(amCrc, 0, addressMark, 20, 2);
|
||||
|
||||
thisTrackAddressMarks[(trackInfo.sectorsInfo[k - 1].id & 0x3F) - 1] = addressMark;
|
||||
}
|
||||
@@ -199,15 +224,17 @@ namespace DiscImageChef.DiscImages
|
||||
sectors.Add(currentSector, thisTrackSectors[s]);
|
||||
addressMarks.Add(currentSector, thisTrackAddressMarks[s]);
|
||||
currentSector++;
|
||||
|
||||
if(thisTrackSectors[s].Length > imageInfo.SectorSize)
|
||||
imageInfo.SectorSize = (uint)thisTrackSectors[s].Length;
|
||||
}
|
||||
|
||||
stream.Seek(trackPos, SeekOrigin.Begin);
|
||||
|
||||
if(extended)
|
||||
{
|
||||
stream.Seek(header.tracksizeTable[i * header.sides + j] * 256, SeekOrigin.Current);
|
||||
imageInfo.ImageSize += (ulong)(header.tracksizeTable[i * header.sides + j] * 256) - 256;
|
||||
stream.Seek(header.tracksizeTable[(i * header.sides) + j] * 256, SeekOrigin.Current);
|
||||
imageInfo.ImageSize += (ulong)(header.tracksizeTable[(i * header.sides) + j] * 256) - 256;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -219,8 +246,8 @@ namespace DiscImageChef.DiscImages
|
||||
}
|
||||
}
|
||||
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "Read {0} sectors", sectors.Count);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "Read {0} tracks", readtracks);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "Read {0} sectors", sectors.Count);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "Read {0} tracks", readtracks);
|
||||
DicConsole.DebugWriteLine("CPCDSK plugin", "All tracks are same size? {0}", allTracksSameSize);
|
||||
|
||||
imageInfo.Application = StringHandlers.CToString(header.creator);
|
||||
@@ -253,7 +280,8 @@ namespace DiscImageChef.DiscImages
|
||||
|
||||
public byte[] ReadSector(ulong sectorAddress)
|
||||
{
|
||||
if(sectors.TryGetValue(sectorAddress, out byte[] sector)) return sector;
|
||||
if(sectors.TryGetValue(sectorAddress, out byte[] sector))
|
||||
return sector;
|
||||
|
||||
throw new ArgumentOutOfRangeException(nameof(sectorAddress), $"Sector address {sectorAddress} not found");
|
||||
}
|
||||
@@ -267,7 +295,7 @@ namespace DiscImageChef.DiscImages
|
||||
if(sectorAddress + length > imageInfo.Sectors)
|
||||
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
var ms = new MemoryStream();
|
||||
|
||||
for(uint i = 0; i < length; i++)
|
||||
{
|
||||
@@ -283,7 +311,8 @@ namespace DiscImageChef.DiscImages
|
||||
if(tag != SectorTagType.FloppyAddressMark)
|
||||
throw new FeatureUnsupportedImageException($"Tag {tag} not supported by image format");
|
||||
|
||||
if(addressMarks.TryGetValue(sectorAddress, out byte[] addressMark)) return addressMark;
|
||||
if(addressMarks.TryGetValue(sectorAddress, out byte[] addressMark))
|
||||
return addressMark;
|
||||
|
||||
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
||||
}
|
||||
@@ -300,7 +329,7 @@ namespace DiscImageChef.DiscImages
|
||||
if(sectorAddress + length > imageInfo.Sectors)
|
||||
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
var ms = new MemoryStream();
|
||||
|
||||
for(uint i = 0; i < length; i++)
|
||||
{
|
||||
|
||||
@@ -55,6 +55,7 @@ namespace DiscImageChef.DiscImages
|
||||
stream.Read(header, 0, 32);
|
||||
|
||||
WCDiskImageFileHeader fheader = Marshal.ByteArrayToStructureLittleEndian<WCDiskImageFileHeader>(header);
|
||||
|
||||
DicConsole.DebugWriteLine("d2f plugin",
|
||||
"Detected WC DISK IMAGE with {0} heads, {1} tracks and {2} sectors per track.",
|
||||
fheader.heads, fheader.cylinders, fheader.sectorsPerTrack);
|
||||
@@ -71,6 +72,7 @@ namespace DiscImageChef.DiscImages
|
||||
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));
|
||||
@@ -78,7 +80,8 @@ namespace DiscImageChef.DiscImages
|
||||
/* 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);
|
||||
for(int head = 0; head < imageInfo.Heads; head++)
|
||||
ReadTrack(stream, cyl, head);
|
||||
}
|
||||
|
||||
/* if there are extra tracks, read them as well */
|
||||
@@ -107,8 +110,13 @@ namespace DiscImageChef.DiscImages
|
||||
}
|
||||
|
||||
/* 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++;
|
||||
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))
|
||||
@@ -116,6 +124,7 @@ namespace DiscImageChef.DiscImages
|
||||
DicConsole.DebugWriteLine("d2f plugin", "Comment present, reading");
|
||||
byte[] sheaderBuffer = new byte[6];
|
||||
stream.Read(sheaderBuffer, 0, 6);
|
||||
|
||||
WCDiskImageSectorHeader sheader =
|
||||
Marshal.ByteArrayToStructureLittleEndian<WCDiskImageSectorHeader>(sheaderBuffer);
|
||||
|
||||
@@ -133,6 +142,7 @@ namespace DiscImageChef.DiscImages
|
||||
DicConsole.DebugWriteLine("d2f plugin", "Directory listing present, reading");
|
||||
byte[] sheaderBuffer = new byte[6];
|
||||
stream.Read(sheaderBuffer, 0, 6);
|
||||
|
||||
WCDiskImageSectorHeader sheader =
|
||||
Marshal.ByteArrayToStructureLittleEndian<WCDiskImageSectorHeader>(sheaderBuffer);
|
||||
|
||||
@@ -145,11 +155,13 @@ namespace DiscImageChef.DiscImages
|
||||
comments += Encoding.ASCII.GetString(dir);
|
||||
}
|
||||
|
||||
if(comments.Length > 0) imageInfo.Comments = comments;
|
||||
if(comments.Length > 0)
|
||||
imageInfo.Comments = comments;
|
||||
|
||||
// save some variables for later use
|
||||
fileHeader = fheader;
|
||||
wcImageFilter = imageFilter;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -189,9 +201,7 @@ namespace DiscImageChef.DiscImages
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a whole track and cache it
|
||||
/// </summary>
|
||||
/// <summary>Read a whole track and cache it</summary>
|
||||
/// <param name="stream">The stream to read from</param>
|
||||
/// <param name="cyl">The cylinder number of the track being read.</param>
|
||||
/// <param name="head">The head number of the track being read.</param>
|
||||
@@ -206,30 +216,36 @@ namespace DiscImageChef.DiscImages
|
||||
/* read the sector header */
|
||||
byte[] sheaderBuffer = new byte[6];
|
||||
stream.Read(sheaderBuffer, 0, 6);
|
||||
|
||||
WCDiskImageSectorHeader sheader =
|
||||
Marshal.ByteArrayToStructureLittleEndian<WCDiskImageSectorHeader>(sheaderBuffer);
|
||||
|
||||
/* validate the sector header */
|
||||
if(sheader.cylinder != cyl || sheader.head != head || sheader.sector != 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));
|
||||
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)
|
||||
{
|
||||
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);
|
||||
CRC16IBMContext.Data(sectorData, 512, out crc);
|
||||
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}",
|
||||
@@ -249,7 +265,8 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user