Files
Aaru/Aaru.Images/NDIF/Read.cs

461 lines
19 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Read.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Reads Apple New Disk Image Format.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Exceptions;
using Aaru.CommonTypes.Interfaces;
using Aaru.Compression;
using Aaru.Console;
using Aaru.Helpers;
2020-02-29 18:03:35 +00:00
using Claunia.Encoding;
using Claunia.RsrcFork;
2016-09-30 04:57:35 +01:00
using SharpCompress.Compressors.ADC;
2017-12-21 14:30:38 +00:00
using Version = Resources.Version;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class Ndif
2017-12-19 20:33:03 +00:00
{
public bool Open(IFilter imageFilter)
2017-12-19 20:33:03 +00:00
{
2020-02-29 18:03:35 +00:00
if(!imageFilter.HasResourceFork() ||
imageFilter.GetResourceForkLength() == 0)
return false;
2017-12-19 20:33:03 +00:00
ResourceFork rsrcFork;
Resource rsrc;
short[] bcems;
2017-12-19 20:33:03 +00:00
try
{
rsrcFork = new ResourceFork(imageFilter.GetResourceForkStream());
2020-02-29 18:03:35 +00:00
if(!rsrcFork.ContainsKey(NDIF_RESOURCE))
return false;
2017-12-19 20:33:03 +00:00
rsrc = rsrcFork.GetResource(NDIF_RESOURCE);
2017-12-19 20:33:03 +00:00
bcems = rsrc.GetIds();
2020-02-29 18:03:35 +00:00
if(bcems == null ||
bcems.Length == 0)
return false;
}
catch(InvalidCastException)
{
return false;
2017-12-19 20:33:03 +00:00
}
2020-07-20 21:11:32 +01:00
_imageInfo.Sectors = 0;
2020-02-29 18:03:35 +00:00
foreach(byte[] bcem in bcems.Select(id => rsrc.GetResource(NDIF_RESOURCEID)))
{
2020-02-29 18:03:35 +00:00
if(bcem.Length < 128)
return false;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_header = Marshal.ByteArrayToStructureBigEndian<ChunkHeader>(bcem);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("NDIF plugin", "footer.type = {0}", _header.version);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.driver = {0}", _header.driver);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("NDIF plugin", "footer.name = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.PascalToString(_header.name,
2020-02-29 18:03:35 +00:00
Encoding.GetEncoding("macintosh")));
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("NDIF plugin", "footer.sectors = {0}", _header.sectors);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.maxSectorsPerChunk = {0}",
_header.maxSectorsPerChunk);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.dataOffset = {0}", _header.dataOffset);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.crc = 0x{0:X7}", _header.crc);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.segmented = {0}", _header.segmented);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.p1 = 0x{0:X8}", _header.p1);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.p2 = 0x{0:X8}", _header.p2);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.unknown[0] = 0x{0:X8}", _header.unknown[0]);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.unknown[1] = 0x{0:X8}", _header.unknown[1]);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.unknown[2] = 0x{0:X8}", _header.unknown[2]);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.unknown[3] = 0x{0:X8}", _header.unknown[3]);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.unknown[4] = 0x{0:X8}", _header.unknown[4]);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.encrypted = {0}", _header.encrypted);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.hash = 0x{0:X8}", _header.hash);
AaruConsole.DebugWriteLine("NDIF plugin", "footer.chunks = {0}", _header.chunks);
2017-12-19 20:33:03 +00:00
// Block chunks and headers
2020-07-20 21:11:32 +01:00
_chunks = new Dictionary<ulong, BlockChunk>();
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
for(int i = 0; i < _header.chunks; i++)
2017-12-19 20:33:03 +00:00
{
// Obsolete read-only NDIF only prepended the header and then put the image without any kind of block references.
// So let's falsify a block chunk
2020-02-29 18:03:35 +00:00
var bChnk = new BlockChunk();
byte[] sector = new byte[4];
Array.Copy(bcem, 128 + 0 + (i * 12), sector, 1, 3);
2017-12-19 20:33:03 +00:00
bChnk.sector = BigEndianBitConverter.ToUInt32(sector, 0);
bChnk.type = bcem[128 + 3 + (i * 12)];
2020-02-29 18:03:35 +00:00
bChnk.offset = BigEndianBitConverter.ToUInt32(bcem, 128 + 4 + (i * 12));
bChnk.length = BigEndianBitConverter.ToUInt32(bcem, 128 + 8 + (i * 12));
2017-12-19 20:33:03 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].type = 0x{1:X2}", i, bChnk.type);
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].sector = {1}", i, bChnk.sector);
AaruConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].offset = {1}", i, bChnk.offset);
AaruConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].length = {1}", i, bChnk.length);
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
if(bChnk.type == CHUNK_TYPE_END)
break;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
bChnk.offset += _header.dataOffset;
bChnk.sector += (uint)_imageInfo.Sectors;
2017-12-19 20:33:03 +00:00
// TODO: Handle compressed chunks
switch(bChnk.type)
{
case CHUNK_TYPE_KENCODE:
throw new
ImageNotSupportedException("Chunks compressed with KenCode are not yet supported.");
case CHUNK_TYPE_LZH:
throw new ImageNotSupportedException("Chunks compressed with LZH are not yet supported.");
case CHUNK_TYPE_STUFFIT:
throw new
ImageNotSupportedException("Chunks compressed with StuffIt! are not yet supported.");
}
2017-12-19 20:33:03 +00:00
// TODO: Handle compressed chunks
2020-02-29 18:03:35 +00:00
if((bChnk.type > CHUNK_TYPE_COPY && bChnk.type < CHUNK_TYPE_KENCODE) ||
(bChnk.type > CHUNK_TYPE_ADC && bChnk.type < CHUNK_TYPE_STUFFIT) ||
(bChnk.type > CHUNK_TYPE_STUFFIT && bChnk.type < CHUNK_TYPE_END) ||
bChnk.type == 1)
throw new ImageNotSupportedException($"Unsupported chunk type 0x{bChnk.type:X8} found");
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_chunks.Add(bChnk.sector, bChnk);
2017-12-19 20:33:03 +00:00
}
2020-07-20 21:11:32 +01:00
_imageInfo.Sectors += _header.sectors;
2017-12-19 20:33:03 +00:00
}
2020-07-20 21:11:32 +01:00
if(_header.segmented > 0)
2020-02-29 18:03:35 +00:00
throw new ImageNotSupportedException("Segmented images are not yet supported.");
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_header.encrypted > 0)
2020-02-29 18:03:35 +00:00
throw new ImageNotSupportedException("Encrypted images are not yet supported.");
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
switch(_imageInfo.Sectors)
2017-12-19 20:33:03 +00:00
{
case 1440:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.DOS_35_DS_DD_9;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case 1600:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.AppleSonyDS;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case 2880:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.DOS_35_HD;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case 3360:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.DMF;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
default:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.GENERIC_HDD;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
}
if(rsrcFork.ContainsKey(0x76657273))
{
Resource versRsrc = rsrcFork.GetResource(0x76657273);
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
if(versRsrc != null)
{
byte[] vers = versRsrc.GetResource(versRsrc.GetIds()[0]);
2020-02-29 18:03:35 +00:00
var version = new Version(vers);
2017-12-19 20:33:03 +00:00
string release = null;
string dev = null;
string pre = null;
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
string major = $"{version.MajorVersion}";
string minor = $".{version.MinorVersion / 10}";
if(version.MinorVersion % 10 > 0)
release = $".{version.MinorVersion % 10}";
2017-12-19 20:33:03 +00:00
switch(version.DevStage)
{
2017-12-21 14:30:38 +00:00
case Version.DevelopmentStage.Alpha:
2017-12-19 20:33:03 +00:00
dev = "a";
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
2017-12-21 14:30:38 +00:00
case Version.DevelopmentStage.Beta:
2017-12-19 20:33:03 +00:00
dev = "b";
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
2017-12-21 14:30:38 +00:00
case Version.DevelopmentStage.PreAlpha:
2017-12-19 20:33:03 +00:00
dev = "d";
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
}
2020-02-29 18:03:35 +00:00
if(dev == null &&
version.PreReleaseVersion > 0)
dev = "f";
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
if(dev != null)
pre = $"{version.PreReleaseVersion}";
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.ApplicationVersion = $"{major}{minor}{release}{dev}{pre}";
_imageInfo.Application = version.VersionString;
_imageInfo.Comments = version.VersionMessage;
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
if(version.MajorVersion == 3)
2020-07-20 21:11:32 +01:00
_imageInfo.Application = "ShrinkWrap™";
2020-02-29 18:03:35 +00:00
else if(version.MajorVersion == 6)
2020-07-20 21:11:32 +01:00
_imageInfo.Application = "DiskCopy";
2017-12-19 20:33:03 +00:00
}
}
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("NDIF plugin", "Image application = {0} version {1}", _imageInfo.Application,
_imageInfo.ApplicationVersion);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_sectorCache = new Dictionary<ulong, byte[]>();
_chunkCache = new Dictionary<ulong, byte[]>();
_currentChunkCacheSize = 0;
_imageStream = imageFilter.GetDataForkStream();
_bufferSize = _header.maxSectorsPerChunk * SECTOR_SIZE;
2020-07-20 21:11:32 +01:00
_imageInfo.CreationTime = imageFilter.GetCreationTime();
_imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.MediaTitle = StringHandlers.PascalToString(_header.name, Encoding.GetEncoding("macintosh"));
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.SectorSize = SECTOR_SIZE;
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
_imageInfo.ImageSize = _imageInfo.Sectors * SECTOR_SIZE;
_imageInfo.ApplicationVersion = "6";
_imageInfo.Application = "Apple DiskCopy";
2020-07-20 21:11:32 +01:00
switch(_imageInfo.MediaType)
2017-12-19 20:33:03 +00:00
{
case MediaType.AppleSonyDS:
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 10;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case MediaType.DOS_35_DS_DD_9:
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 9;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case MediaType.DOS_35_HD:
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 18;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case MediaType.DMF:
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 21;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
default:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.GENERIC_HDD;
_imageInfo.Cylinders = (uint)(_imageInfo.Sectors / 16 / 63);
_imageInfo.Heads = 16;
_imageInfo.SectorsPerTrack = 63;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
}
return true;
}
public byte[] ReadSector(ulong sectorAddress)
2017-12-19 20:33:03 +00:00
{
2020-07-20 21:11:32 +01:00
if(sectorAddress > _imageInfo.Sectors - 1)
2017-12-19 20:33:03 +00:00
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
$"Sector address {sectorAddress} not found");
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_sectorCache.TryGetValue(sectorAddress, out byte[] sector))
2020-02-29 18:03:35 +00:00
return sector;
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
var currentChunk = new BlockChunk();
bool chunkFound = false;
ulong chunkStartSector = 0;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
foreach(KeyValuePair<ulong, BlockChunk> kvp in _chunks.Where(kvp => sectorAddress >= kvp.Key))
{
currentChunk = kvp.Value;
chunkFound = true;
chunkStartSector = kvp.Key;
}
2017-12-19 20:33:03 +00:00
long relOff = ((long)sectorAddress - (long)chunkStartSector) * SECTOR_SIZE;
2017-12-19 20:33:03 +00:00
if(relOff < 0)
throw new ArgumentOutOfRangeException(nameof(relOff),
$"Got a negative offset for sector {sectorAddress}. This should not happen.");
2017-12-19 20:33:03 +00:00
if(!chunkFound)
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
$"Sector address {sectorAddress} not found");
2017-12-19 20:33:03 +00:00
if((currentChunk.type & CHUNK_TYPE_COMPRESSED_MASK) == CHUNK_TYPE_COMPRESSED_MASK)
2017-12-19 20:33:03 +00:00
{
2020-07-20 21:11:32 +01:00
if(!_chunkCache.TryGetValue(chunkStartSector, out byte[] buffer))
2017-12-19 20:33:03 +00:00
{
byte[] cmpBuffer = new byte[currentChunk.length];
2020-07-20 21:11:32 +01:00
_imageStream.Seek(currentChunk.offset, SeekOrigin.Begin);
_imageStream.Read(cmpBuffer, 0, cmpBuffer.Length);
2020-02-29 18:03:35 +00:00
var cmpMs = new MemoryStream(cmpBuffer);
int realSize;
2017-12-19 20:33:03 +00:00
switch(currentChunk.type)
{
case CHUNK_TYPE_ADC:
{
Stream decStream = new ADCStream(cmpMs);
byte[] tmpBuffer = new byte[_bufferSize];
realSize = decStream.Read(tmpBuffer, 0, (int)_bufferSize);
2018-06-22 08:08:38 +01:00
buffer = new byte[realSize];
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
2020-02-29 18:03:35 +00:00
break;
}
case CHUNK_TYPE_RLE:
{
byte[] tmpBuffer = new byte[_bufferSize];
2018-06-22 08:08:38 +01:00
realSize = 0;
2020-02-29 18:03:35 +00:00
var rle = new AppleRle(cmpMs);
for(int i = 0; i < _bufferSize; i++)
{
int b = rle.ProduceByte();
2020-02-29 18:03:35 +00:00
if(b == -1)
break;
tmpBuffer[i] = (byte)b;
realSize++;
}
buffer = new byte[realSize];
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
2020-02-29 18:03:35 +00:00
break;
}
default:
throw new
ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
}
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_currentChunkCacheSize + realSize > MAX_CACHE_SIZE)
2017-12-19 20:33:03 +00:00
{
2020-07-20 21:11:32 +01:00
_chunkCache.Clear();
_currentChunkCacheSize = 0;
2017-12-19 20:33:03 +00:00
}
2020-07-20 21:11:32 +01:00
_chunkCache.Add(chunkStartSector, buffer);
_currentChunkCacheSize += (uint)realSize;
2017-12-19 20:33:03 +00:00
}
sector = new byte[SECTOR_SIZE];
Array.Copy(buffer, relOff, sector, 0, SECTOR_SIZE);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_sectorCache.Count >= MAX_CACHED_SECTORS)
_sectorCache.Clear();
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_sectorCache.Add(sectorAddress, sector);
2017-12-19 20:33:03 +00:00
return sector;
}
switch(currentChunk.type)
{
case CHUNK_TYPE_NOCOPY:
sector = new byte[SECTOR_SIZE];
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_sectorCache.Count >= MAX_CACHED_SECTORS)
_sectorCache.Clear();
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_sectorCache.Add(sectorAddress, sector);
2020-02-29 18:03:35 +00:00
return sector;
case CHUNK_TYPE_COPY:
2020-07-20 21:11:32 +01:00
_imageStream.Seek(currentChunk.offset + relOff, SeekOrigin.Begin);
sector = new byte[SECTOR_SIZE];
2020-07-20 21:11:32 +01:00
_imageStream.Read(sector, 0, sector.Length);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_sectorCache.Count >= MAX_CACHED_SECTORS)
_sectorCache.Clear();
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_sectorCache.Add(sectorAddress, sector);
2020-02-29 18:03:35 +00:00
return sector;
2017-12-19 20:33:03 +00:00
}
throw new ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
2017-12-19 20:33:03 +00:00
}
public byte[] ReadSectors(ulong sectorAddress, uint length)
2017-12-19 20:33:03 +00:00
{
2020-07-20 21:11:32 +01:00
if(sectorAddress > _imageInfo.Sectors - 1)
2017-12-19 20:33:03 +00:00
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
$"Sector address {sectorAddress} not found");
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
2017-12-19 20:33:03 +00:00
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
2020-02-29 18:03:35 +00:00
var ms = new MemoryStream();
2017-12-19 20:33:03 +00:00
for(uint i = 0; i < length; i++)
{
byte[] sector = ReadSector(sectorAddress + i);
ms.Write(sector, 0, sector.Length);
}
return ms.ToArray();
}
}
}