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

90 lines
3.3 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/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2021-2025 Michael Drüing
// Copyright © 2011-2025 Natalia Portillo
2021-03-07 18:09:30 +01:00
// ****************************************************************************/
using System.IO;
using System.Linq;
using Aaru.Helpers;
using Aaru.Logging;
2021-03-07 18:09:30 +01:00
namespace Aaru.Images;
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
{
2025-11-24 19:38:40 +00: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
2024-05-01 04:05:22 +01: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
2024-05-01 04:05:22 +01: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
2024-05-01 04:05:22 +01:00
if(fHeader.diskType is < 1 or > 4) 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);
2025-11-24 19:38:40 +00:00
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
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2025-11-24 19:38:40 +00:00
Localization.Identified_image_with_CHS_equals_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
2025-11-24 19:38:40 +00:00
for(var i = 0; i < numTracks; i++)
2022-03-06 13:29:38 +00:00
{
stream.EnsureRead(buffer, 0, 6);
trackMap[i] = Marshal.ByteArrayToStructureLittleEndian<TrackInfo>(buffer);
2022-03-06 13:29:38 +00:00
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
}
}