2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2016-09-06 23:21:10 +01:00
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Filename : Read.cs
|
2016-09-06 23:21:10 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Disk image plugins.
|
2016-09-06 23:21:10 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Reads Apple New Disk Image Format.
|
2016-09-06 23:21:10 +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-09-06 23:21:10 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2017-12-21 07:08:26 +00:00
|
|
|
|
using System.Linq;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
using Claunia.Encoding;
|
2016-09-06 23:21:10 +01:00
|
|
|
|
using Claunia.RsrcFork;
|
|
|
|
|
|
using DiscImageChef.CommonTypes;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
|
|
|
|
|
using DiscImageChef.CommonTypes.Exceptions;
|
|
|
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2018-01-21 15:58:12 +00:00
|
|
|
|
using DiscImageChef.Compression;
|
2016-09-06 23:21:10 +01:00
|
|
|
|
using DiscImageChef.Console;
|
2016-09-30 04:57:35 +01:00
|
|
|
|
using SharpCompress.Compressors.ADC;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using Version = Resources.Version;
|
2016-09-06 23:21:10 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.DiscImages
|
|
|
|
|
|
{
|
2018-07-23 23:25:43 +01:00
|
|
|
|
public partial class Ndif
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2017-12-28 19:56:36 +00:00
|
|
|
|
public bool Open(IFilter imageFilter)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
if(!imageFilter.HasResourceFork() || imageFilter.GetResourceForkLength() == 0) return false;
|
|
|
|
|
|
|
|
|
|
|
|
ResourceFork rsrcFork;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
Resource rsrc;
|
|
|
|
|
|
short[] bcems;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
rsrcFork = new ResourceFork(imageFilter.GetResourceForkStream());
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(!rsrcFork.ContainsKey(NDIF_RESOURCE)) return false;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
rsrc = rsrcFork.GetResource(NDIF_RESOURCE);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
bcems = rsrc.GetIds();
|
|
|
|
|
|
|
|
|
|
|
|
if(bcems == null || bcems.Length == 0) return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(InvalidCastException) { return false; }
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.Sectors = 0;
|
2017-12-24 00:12:31 +00:00
|
|
|
|
foreach(byte[] bcem in bcems.Select(id => rsrc.GetResource(NDIF_RESOURCEID)))
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(bcem.Length < 128) return false;
|
|
|
|
|
|
|
|
|
|
|
|
header = BigEndianMarshal.ByteArrayToStructureBigEndian<ChunkHeader>(bcem);
|
|
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.type = {0}", header.version);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.driver = {0}", header.driver);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.name = {0}",
|
|
|
|
|
|
StringHandlers.PascalToString(header.name,
|
|
|
|
|
|
Encoding.GetEncoding("macintosh")));
|
2018-01-28 20:29:46 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.sectors = {0}", header.sectors);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.maxSectorsPerChunk = {0}", header.maxSectorsPerChunk);
|
2018-01-28 20:29:46 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.dataOffset = {0}", header.dataOffset);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.crc = 0x{0:X7}", header.crc);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.segmented = {0}", header.segmented);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.p1 = 0x{0:X8}", header.p1);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.p2 = 0x{0:X8}", header.p2);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.unknown[0] = 0x{0:X8}", header.unknown[0]);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.unknown[1] = 0x{0:X8}", header.unknown[1]);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.unknown[2] = 0x{0:X8}", header.unknown[2]);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.unknown[3] = 0x{0:X8}", header.unknown[3]);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.unknown[4] = 0x{0:X8}", header.unknown[4]);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.encrypted = {0}", header.encrypted);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.hash = 0x{0:X8}", header.hash);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "footer.chunks = {0}", header.chunks);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
// Block chunks and headers
|
|
|
|
|
|
chunks = new Dictionary<ulong, BlockChunk>();
|
|
|
|
|
|
|
|
|
|
|
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < header.chunks; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 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
|
2018-01-28 20:29:46 +00:00
|
|
|
|
BlockChunk bChnk = new BlockChunk();
|
|
|
|
|
|
byte[] sector = new byte[4];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
Array.Copy(bcem, 128 + 0 + i * 12, sector, 1, 3);
|
|
|
|
|
|
bChnk.sector = BigEndianBitConverter.ToUInt32(sector, 0);
|
2018-06-22 08:08:38 +01:00
|
|
|
|
bChnk.type = bcem[128 + 3 + i * 12];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
bChnk.offset = BigEndianBitConverter.ToUInt32(bcem, 128 + 4 + i * 12);
|
|
|
|
|
|
bChnk.length = BigEndianBitConverter.ToUInt32(bcem, 128 + 8 + i * 12);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].type = 0x{1:X2}", i, bChnk.type);
|
2018-01-28 20:29:46 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].sector = {1}", i, bChnk.sector);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].offset = {1}", i, bChnk.offset);
|
|
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "bHdr.chunk[{0}].length = {1}", i, bChnk.length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(bChnk.type == CHUNK_TYPE_END) break;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
bChnk.offset += header.dataOffset;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
bChnk.sector += (uint)imageInfo.Sectors;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
// TODO: Handle compressed chunks
|
2017-12-24 00:12:31 +00:00
|
|
|
|
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-21 04:43:29 +00:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
// TODO: Handle compressed chunks
|
2018-01-28 20:29:46 +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)
|
2017-12-21 17:58:51 +00:00
|
|
|
|
throw new ImageNotSupportedException($"Unsupported chunk type 0x{bChnk.type:X8} found");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
chunks.Add(bChnk.sector, bChnk);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.Sectors += header.sectors;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(header.segmented > 0) throw new ImageNotSupportedException("Segmented images are not yet supported.");
|
|
|
|
|
|
|
|
|
|
|
|
if(header.encrypted > 0) throw new ImageNotSupportedException("Encrypted images are not yet supported.");
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
switch(imageInfo.Sectors)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
case 1440:
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.DOS_35_DS_DD_9;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case 1600:
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.AppleSonyDS;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case 2880:
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.DOS_35_HD;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case 3360:
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.DMF;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(rsrcFork.ContainsKey(0x76657273))
|
|
|
|
|
|
{
|
|
|
|
|
|
Resource versRsrc = rsrcFork.GetResource(0x76657273);
|
|
|
|
|
|
if(versRsrc != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] vers = versRsrc.GetResource(versRsrc.GetIds()[0]);
|
|
|
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Version version = new Version(vers);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
string release = null;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
string dev = null;
|
|
|
|
|
|
string pre = null;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2018-06-22 08:08:38 +01: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";
|
|
|
|
|
|
break;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
case Version.DevelopmentStage.Beta:
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dev = "b";
|
|
|
|
|
|
break;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
case Version.DevelopmentStage.PreAlpha:
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dev = "d";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(dev == null && version.PreReleaseVersion > 0) dev = "f";
|
|
|
|
|
|
|
2017-12-21 17:58:51 +00:00
|
|
|
|
if(dev != null) pre = $"{version.PreReleaseVersion}";
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.ApplicationVersion = $"{major}{minor}{release}{dev}{pre}";
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Application = version.VersionString;
|
|
|
|
|
|
imageInfo.Comments = version.VersionMessage;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
if(version.MajorVersion == 3) imageInfo.Application = "ShrinkWrap™";
|
2018-06-22 08:08:38 +01:00
|
|
|
|
else if(version.MajorVersion == 6) imageInfo.Application = "DiskCopy";
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
DicConsole.DebugWriteLine("NDIF plugin", "Image application = {0} version {1}", imageInfo.Application,
|
|
|
|
|
|
imageInfo.ApplicationVersion);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
sectorCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
chunkCache = new Dictionary<ulong, byte[]>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
currentChunkCacheSize = 0;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageStream = imageFilter.GetDataForkStream();
|
|
|
|
|
|
buffersize = header.maxSectorsPerChunk * SECTOR_SIZE;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.MediaTitle =
|
2018-01-28 20:29:46 +00:00
|
|
|
|
StringHandlers.PascalToString(header.name, Encoding.GetEncoding("macintosh"));
|
|
|
|
|
|
imageInfo.SectorSize = SECTOR_SIZE;
|
|
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
|
imageInfo.ImageSize = imageInfo.Sectors * SECTOR_SIZE;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.ApplicationVersion = "6";
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Application = "Apple DiskCopy";
|
2017-12-20 17:15:26 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
switch(imageInfo.MediaType)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
case MediaType.AppleSonyDS:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 80;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 10;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.DOS_35_DS_DD_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.DMF:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = 80;
|
|
|
|
|
|
imageInfo.Heads = 2;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 21;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
|
|
|
|
|
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
|
|
|
|
|
|
imageInfo.Heads = 16;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 63;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress > imageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
2017-12-21 17:58:51 +00:00
|
|
|
|
$"Sector address {sectorAddress} not found");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
|
if(sectorCache.TryGetValue(sectorAddress, out byte[] sector)) return sector;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
BlockChunk currentChunk = new BlockChunk();
|
|
|
|
|
|
bool chunkFound = false;
|
|
|
|
|
|
ulong chunkStartSector = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
|
foreach(KeyValuePair<ulong, BlockChunk> kvp in chunks.Where(kvp => sectorAddress >= kvp.Key))
|
|
|
|
|
|
{
|
2018-01-28 20:29:46 +00:00
|
|
|
|
currentChunk = kvp.Value;
|
|
|
|
|
|
chunkFound = true;
|
2017-12-21 07:08:26 +00:00
|
|
|
|
chunkStartSector = kvp.Key;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-20 17:15:26 +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),
|
2017-12-21 17:58:51 +00:00
|
|
|
|
$"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),
|
2017-12-21 17:58:51 +00:00
|
|
|
|
$"Sector address {sectorAddress} not found");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if((currentChunk.type & CHUNK_TYPE_COMPRESSED_MASK) == CHUNK_TYPE_COMPRESSED_MASK)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
|
if(!chunkCache.TryGetValue(chunkStartSector, out byte[] buffer))
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
byte[] cmpBuffer = new byte[currentChunk.length];
|
|
|
|
|
|
imageStream.Seek(currentChunk.offset, SeekOrigin.Begin);
|
|
|
|
|
|
imageStream.Read(cmpBuffer, 0, cmpBuffer.Length);
|
|
|
|
|
|
MemoryStream cmpMs = new MemoryStream(cmpBuffer);
|
2018-01-28 20:29:46 +00:00
|
|
|
|
int realSize;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2018-01-21 15:58:12 +00:00
|
|
|
|
switch(currentChunk.type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case CHUNK_TYPE_ADC:
|
|
|
|
|
|
{
|
|
|
|
|
|
Stream decStream = new ADCStream(cmpMs);
|
|
|
|
|
|
byte[] tmpBuffer = new byte[buffersize];
|
2018-06-22 08:08:38 +01:00
|
|
|
|
realSize = decStream.Read(tmpBuffer, 0, (int)buffersize);
|
|
|
|
|
|
buffer = new byte[realSize];
|
2018-01-21 15:58:12 +00:00
|
|
|
|
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case CHUNK_TYPE_RLE:
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] tmpBuffer = new byte[buffersize];
|
2018-06-22 08:08:38 +01:00
|
|
|
|
realSize = 0;
|
|
|
|
|
|
AppleRle rle = new AppleRle(cmpMs);
|
2018-01-21 15:58:12 +00:00
|
|
|
|
for(int i = 0; i < buffersize; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
int b = rle.ProduceByte();
|
|
|
|
|
|
if(b == -1) break;
|
|
|
|
|
|
|
|
|
|
|
|
tmpBuffer[i] = (byte)b;
|
|
|
|
|
|
realSize++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buffer = new byte[realSize];
|
|
|
|
|
|
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new
|
|
|
|
|
|
ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(currentChunkCacheSize + realSize > MAX_CACHE_SIZE)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
chunkCache.Clear();
|
|
|
|
|
|
currentChunkCacheSize = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
chunkCache.Add(chunkStartSector, buffer);
|
|
|
|
|
|
currentChunkCacheSize += (uint)realSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
sector = new byte[SECTOR_SIZE];
|
|
|
|
|
|
Array.Copy(buffer, relOff, sector, 0, SECTOR_SIZE);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
|
|
|
|
|
|
|
return sector;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
|
switch(currentChunk.type)
|
|
|
|
|
|
{
|
2017-12-21 04:43:29 +00:00
|
|
|
|
case CHUNK_TYPE_NOCOPY:
|
|
|
|
|
|
sector = new byte[SECTOR_SIZE];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
|
return sector;
|
|
|
|
|
|
case CHUNK_TYPE_COPY:
|
|
|
|
|
|
imageStream.Seek(currentChunk.offset + relOff, SeekOrigin.Begin);
|
|
|
|
|
|
sector = new byte[SECTOR_SIZE];
|
|
|
|
|
|
imageStream.Read(sector, 0, sector.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
|
return sector;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-21 17:58:51 +00:00
|
|
|
|
throw new ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress > imageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
2017-12-21 17:58:51 +00:00
|
|
|
|
$"Sector address {sectorAddress} not found");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] sector = ReadSector(sectorAddress + i);
|
|
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|