2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Helpers.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Contains helpers for HD-Copy 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-05-01 04:17:32 +01:00
|
|
|
|
// Copyright © 2017-2024 Michael Drüing
|
|
|
|
|
|
// Copyright © 2011-2024 Natalia Portillo
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2021-09-21 04:55:28 +01:00
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.Console;
|
2022-11-14 09:43:16 +00:00
|
|
|
|
using Aaru.Helpers;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-06 01:16:28 +01:00
|
|
|
|
namespace Aaru.Images;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public sealed partial class HdCopy
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-15 01:35:06 +00:00
|
|
|
|
static bool TryReadHeader(Stream stream, ref FileHeader fhdr, ref long dataStartOffset)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var numTracks = 82;
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(stream.Length < 16 + 2 * 82) return false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
FileHeader fheader = new();
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/* assume it's a regular HD-Copy file without the disk name */
|
2023-10-03 23:34:59 +01:00
|
|
|
|
dataStartOffset = 2 + 2 * numTracks;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
fheader.lastCylinder = (byte)stream.ReadByte();
|
|
|
|
|
|
fheader.sectorsPerTrack = (byte)stream.ReadByte();
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
if(fheader is { lastCylinder: 0xff, sectorsPerTrack: 0x18 })
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* This is an "extended" HD-Copy file with filename information and 84 tracks */
|
|
|
|
|
|
stream.Seek(0x0e, SeekOrigin.Begin);
|
2021-08-17 18:16:25 +01:00
|
|
|
|
fheader.lastCylinder = (byte)stream.ReadByte();
|
2021-02-28 19:06:32 +01:00
|
|
|
|
fheader.sectorsPerTrack = (byte)stream.ReadByte();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
numTracks = 84;
|
2023-10-03 23:34:59 +01:00
|
|
|
|
dataStartOffset = 16 + 2 * numTracks;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2021-08-17 21:23:10 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
fheader.trackMap = new byte[2 * numTracks];
|
2022-11-14 09:43:16 +00:00
|
|
|
|
stream.EnsureRead(fheader.trackMap, 0, 2 * numTracks);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
/* Some sanity checks on the values we just read.
|
|
|
|
|
|
* We know the image is from a DOS floppy disk, so assume
|
|
|
|
|
|
* some sane cylinder and sectors-per-track count.
|
|
|
|
|
|
*/
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(fheader.sectorsPerTrack is < 8 or > 40) return false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(fheader.lastCylinder is < 37 or >= 84) return false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
// Validate the trackmap. First two tracks need to be present
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(fheader.trackMap[0] != 1 || fheader.trackMap[1] != 1) return false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
// all other tracks must be either present (=1) or absent (=0)
|
2023-10-03 23:34:59 +01:00
|
|
|
|
for(var i = 0; i < 2 * numTracks; i++)
|
2024-05-01 04:51:19 +01:00
|
|
|
|
if(fheader.trackMap[i] > 1)
|
|
|
|
|
|
return false;
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/* return success */
|
|
|
|
|
|
fhdr = fheader;
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2021-08-17 21:23:10 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ErrorNumber ReadTrackIntoCache(Stream stream, int trackNum)
|
|
|
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var trackData = new byte[_imageInfo.SectorSize * _imageInfo.SectorsPerTrack];
|
|
|
|
|
|
var blkHeader = new byte[3];
|
2021-02-28 19:06:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// check that track is present
|
|
|
|
|
|
if(_trackOffset[trackNum] == -1)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
AaruConsole.ErrorWriteLine(Localization.Tried_reading_a_track_that_is_not_present_in_image);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.SectorNotFound;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
stream.Seek(_trackOffset[trackNum], SeekOrigin.Begin);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// read the compressed track data
|
2022-11-14 09:43:16 +00:00
|
|
|
|
stream.EnsureRead(blkHeader, 0, 3);
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var compressedLength = (short)(BitConverter.ToInt16(blkHeader, 0) - 1);
|
|
|
|
|
|
byte escapeByte = blkHeader[2];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var cBuffer = new byte[compressedLength];
|
2022-11-14 09:43:16 +00:00
|
|
|
|
stream.EnsureRead(cBuffer, 0, compressedLength);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// decompress the data
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var sIndex = 0; // source buffer position
|
|
|
|
|
|
var dIndex = 0; // destination buffer position
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
while(sIndex < compressedLength)
|
2023-10-03 23:34:59 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(cBuffer[sIndex] == escapeByte)
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
sIndex++; // skip over escape byte
|
|
|
|
|
|
byte fillByte = cBuffer[sIndex++];
|
|
|
|
|
|
byte fillCount = cBuffer[sIndex++];
|
2021-09-21 04:55:28 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// fill destination buffer
|
2024-05-01 04:05:22 +01:00
|
|
|
|
for(var i = 0; i < fillCount; i++) trackData[dIndex++] = fillByte;
|
2021-09-21 04:55:28 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
else
|
|
|
|
|
|
trackData[dIndex++] = cBuffer[sIndex++];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// check that the number of bytes decompressed matches a whole track
|
|
|
|
|
|
if(dIndex != _imageInfo.SectorSize * _imageInfo.SectorsPerTrack)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
AaruConsole.ErrorWriteLine(Localization.Track_decompression_yielded_incomplete_data);
|
2021-09-21 04:55:28 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.InOutError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
// store track in cache
|
|
|
|
|
|
_trackCache[trackNum] = trackData;
|
|
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|