Files
Aaru/Aaru.Images/DiskDupe/Helpers.cs

91 lines
3.4 KiB
C#
Raw Normal View History

2021-03-07 18:09:30 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Helpers.cs
// Author(s) : Michael Drüing <michael@drueing.de>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains helpers for DiskDupe DDI disk images.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2021-2022 Michael Drüing
// Copyright © 2011-2022 Natalia Portillo
2021-03-07 18:09:30 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.DiscImages;
2021-03-07 18:09:30 +01:00
using System.IO;
using System.Linq;
using Aaru.Console;
using Aaru.Helpers;
2022-03-06 13:29:38 +00:00
public sealed partial class DiskDupe
2021-03-07 18:09:30 +01:00
{
2022-03-06 13:29:38 +00:00
bool TryReadHeader(Stream stream, ref FileHeader fhdr, ref TrackInfo[] tmap, ref long[] toffsets)
2021-03-07 18:09:30 +01:00
{
var buffer = new byte[6];
FileHeader fHeader;
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
stream.Seek(0, SeekOrigin.Begin);
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
if(stream.Length < 256)
return false;
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
// read and check signature
fHeader.signature = new byte[10];
stream.EnsureRead(fHeader.signature, 0, 10);
2021-08-17 21:23:10 +01:00
2022-03-06 13:29:38 +00:00
if(!fHeader.signature.SequenceEqual(_headerMagic))
return false;
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
// read and check disk type byte
fHeader.diskType = (byte)stream.ReadByte();
2021-08-17 21:23:10 +01:00
2022-03-16 11:47:00 +00:00
if(fHeader.diskType is < 1 or > 4)
2022-03-06 13:29:38 +00:00
return false;
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
// seek to start of the trackmap
stream.Seek(TRACKMAP_OFFSET, SeekOrigin.Begin);
int numTracks = _diskTypes[fHeader.diskType].cyl * _diskTypes[fHeader.diskType].hd;
int trackLen = 512 * _diskTypes[fHeader.diskType].spt; // the length of a single track, in bytes
var trackMap = new TrackInfo[numTracks];
var trackOffsets = new long[numTracks];
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("DiskDupe plugin", "Identified image with C/H/S = {0}/{1}/{2}",
_diskTypes[fHeader.diskType].cyl, _diskTypes[fHeader.diskType].hd,
_diskTypes[fHeader.diskType].spt);
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
// read the trackmap and store the track offsets
2022-03-07 07:36:44 +00:00
for(var i = 0; i < numTracks; i++)
2022-03-06 13:29:38 +00:00
{
stream.EnsureRead(buffer, 0, 6);
2022-03-06 13:29:38 +00:00
trackMap[i] = Marshal.ByteArrayToStructureBigEndian<TrackInfo>(buffer);
trackOffsets[i] = trackLen * trackMap[i].trackNumber;
}
2021-03-07 18:09:30 +01:00
2022-03-06 13:29:38 +00:00
fhdr = fHeader;
tmap = trackMap;
toffsets = trackOffsets;
2021-08-17 21:23:10 +01:00
2022-03-06 13:29:38 +00:00
return true;
2021-03-07 18:09:30 +01:00
}
}