2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2016-08-21 17:35:35 +01:00
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Filename : Read.cs
|
2016-08-21 17:35:35 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Disk image plugins.
|
2016-08-21 17:35:35 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Reads DIM disk images.
|
2016-08-21 17:35:35 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-08-21 17:35:35 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using System.IO;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using DiscImageChef.CommonTypes;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
|
|
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using DiscImageChef.Console;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
namespace DiscImageChef.DiscImages
|
2016-08-21 17:35:35 +01:00
|
|
|
|
{
|
2018-07-23 23:25:43 +01:00
|
|
|
|
public partial class Dim
|
2016-08-21 17:35:35 +01:00
|
|
|
|
{
|
2017-12-28 19:56:36 +00:00
|
|
|
|
public bool Open(IFilter imageFilter)
|
2016-08-21 17:35:35 +01:00
|
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-21 17:35:35 +01:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(stream.Length < DATA_OFFSET) return false;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
long diskSize = stream.Length - DATA_OFFSET;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
|
|
|
|
|
comment = new byte[60];
|
2018-01-28 20:29:46 +00:00
|
|
|
|
hdrId = new byte[13];
|
2016-08-21 17:35:35 +01:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
dskType = (DiskType)stream.ReadByte();
|
|
|
|
|
|
stream.Seek(0xAB, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(hdrId, 0, 13);
|
|
|
|
|
|
stream.Seek(0xC2, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(comment, 0, 60);
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(!headerId.SequenceEqual(hdrId)) return false;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.Unknown;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
|
|
|
|
|
switch(dskType)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 8 spt, 1024 bps
|
2017-12-20 17:15:26 +00:00
|
|
|
|
case DiskType.Hd2:
|
2016-08-21 17:35:35 +01:00
|
|
|
|
if(diskSize % (2 * 8 * 1024) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("DIM shows unknown image with {0} tracks", diskSize / (2 * 8 * 1024));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize / (2 * 8 * 1024) == 77) imageInfo.MediaType = MediaType.SHARP_525;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 1024;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
break;
|
|
|
|
|
|
// 9 spt, 1024 bps
|
2017-12-20 17:15:26 +00:00
|
|
|
|
case DiskType.Hs2:
|
2016-08-21 17:35:35 +01:00
|
|
|
|
if(diskSize % (2 * 9 * 512) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("DIM shows unknown image with {0} tracks", diskSize / (2 * 9 * 512));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize / (2 * 9 * 512) == 80) imageInfo.MediaType = MediaType.SHARP_525_9;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 512;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
break;
|
|
|
|
|
|
// 15 spt, 512 bps
|
2017-12-20 17:15:26 +00:00
|
|
|
|
case DiskType.Hc2:
|
2016-08-21 17:35:35 +01:00
|
|
|
|
if(diskSize % (2 * 15 * 512) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("DIM shows unknown image with {0} tracks", diskSize / (2 * 15 * 512));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize / (2 * 15 * 512) == 80) imageInfo.MediaType = MediaType.DOS_525_HD;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 512;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
break;
|
|
|
|
|
|
// 9 spt, 1024 bps
|
2017-12-20 17:15:26 +00:00
|
|
|
|
case DiskType.Hde2:
|
2016-08-21 17:35:35 +01:00
|
|
|
|
if(diskSize % (2 * 9 * 512) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("DIM shows unknown image with {0} tracks", diskSize / (2 * 9 * 512));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize / (2 * 9 * 512) == 80) imageInfo.MediaType = MediaType.SHARP_35_9;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 512;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
break;
|
|
|
|
|
|
// 18 spt, 512 bps
|
2017-12-20 17:15:26 +00:00
|
|
|
|
case DiskType.Hq2:
|
2016-08-21 17:35:35 +01:00
|
|
|
|
if(diskSize % (2 * 18 * 512) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("DIM shows unknown image with {0} tracks", diskSize / (2 * 18 * 512));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize / (2 * 18 * 512) == 80) imageInfo.MediaType = MediaType.DOS_35_HD;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 512;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
break;
|
|
|
|
|
|
// 26 spt, 256 bps
|
|
|
|
|
|
case DiskType.N88:
|
|
|
|
|
|
if(diskSize % (2 * 26 * 256) == 0)
|
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize % (2 * 26 * 256) == 77) imageInfo.MediaType = MediaType.NEC_8_DD;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 256;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(diskSize % (2 * 26 * 128) == 0)
|
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(diskSize % (2 * 26 * 128) == 77) imageInfo.MediaType = MediaType.NEC_8_SD;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.SectorSize = 256;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("DIM shows unknown image with {0} tracks", diskSize / (2 * 26 * 256));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2016-08-21 17:35:35 +01:00
|
|
|
|
break;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
default: return false;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
DicConsole.VerboseWriteLine("DIM image contains a disk of type {0}", imageInfo.MediaType);
|
|
|
|
|
|
if(!string.IsNullOrEmpty(imageInfo.Comments))
|
|
|
|
|
|
DicConsole.VerboseWriteLine("DIM comments: {0}", imageInfo.Comments);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
|
dimImageFilter = imageFilter;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.ImageSize = (ulong)diskSize;
|
|
|
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
|
imageInfo.Sectors = imageInfo.ImageSize / imageInfo.SectorSize;
|
|
|
|
|
|
imageInfo.Comments = StringHandlers.CToString(comment, Encoding.GetEncoding(932));
|
|
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
switch(imageInfo.MediaType)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
case MediaType.SHARP_525:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 77;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 8;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.SHARP_525_9:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 80;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 9;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.DOS_525_HD:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 80;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 15;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.SHARP_35_9:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 80;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 9;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.DOS_35_HD:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 80;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 18;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.NEC_8_DD:
|
|
|
|
|
|
case MediaType.NEC_8_SD:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 77;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 26;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-08-21 17:35:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2016-08-21 17:35:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
return ReadSectors(sectorAddress, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2016-08-21 17:35:35 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress > imageInfo.Sectors - 1)
|
2016-08-21 17:35:35 +01:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2016-08-21 17:35:35 +01:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
byte[] buffer = new byte[length * imageInfo.SectorSize];
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
|
Stream stream = dimImageFilter.GetDataForkStream();
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
stream.Seek((long)(DATA_OFFSET + sectorAddress * imageInfo.SectorSize), SeekOrigin.Begin);
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
stream.Read(buffer, 0, (int)(length * imageInfo.SectorSize));
|
2016-08-21 17:35:35 +01:00
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|