Files
Aaru/Aaru.Images/CopyTape/Read.cs

245 lines
7.9 KiB
C#
Raw Normal View History

2019-05-06 18:37:29 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2019-05-06 18:37:29 +01:00
// ----------------------------------------------------------------------------
//
// Filename : Read.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Reads CopyTape tape 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 © 2011-2025 Natalia Portillo
2019-05-06 18:37:29 +01:00
// ****************************************************************************/
2019-05-06 20:21:57 +01:00
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Helpers;
using Aaru.Logging;
2019-05-06 18:37:29 +01:00
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class CopyTape
2019-05-06 18:37:29 +01:00
{
2023-10-03 23:34:59 +01:00
#region IWritableTapeImage Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
2019-05-06 18:37:29 +01:00
{
2024-05-01 04:39:38 +01:00
List<long> blockPositions = [];
2022-03-06 13:29:38 +00:00
var partialBlockRx = new Regex(PARTIAL_BLOCK_REGEX);
var blockRx = new Regex(BLOCK_REGEX);
var filemarkRx = new Regex(FILEMARK_REGEX);
var eotRx = new Regex(END_OF_TAPE_REGEX);
2019-05-06 18:37:29 +01:00
2024-05-01 04:05:22 +01:00
if(imageFilter.DataForkLength <= 16) return ErrorNumber.InvalidArgument;
2019-05-06 18:37:29 +01:00
2022-03-06 13:29:38 +00:00
_imageStream = imageFilter.GetDataForkStream();
_imageStream.Position = 0;
2019-05-06 20:21:57 +01:00
byte[] header = new byte[9];
byte[] blockHeader = new byte[16];
ulong currentBlock = 0;
uint currentFile = 0;
ulong currentFileStart = 0;
bool inFile = false;
2019-05-06 20:21:57 +01:00
2024-05-01 04:39:38 +01:00
Files = [];
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
while(_imageStream.Position + 9 < _imageStream.Length)
{
_imageStream.EnsureRead(header, 0, 9);
2022-03-06 13:29:38 +00:00
string mark = Encoding.ASCII.GetString(header);
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
Match partialBlockMt = partialBlockRx.Match(mark);
Match filemarkMt = filemarkRx.Match(mark);
Match eotMt = eotRx.Match(mark);
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(eotMt.Success) break;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(filemarkMt.Success)
{
Files.Add(new TapeFile
2019-05-06 20:21:57 +01:00
{
2022-03-06 13:29:38 +00:00
File = currentFile,
FirstBlock = currentFileStart,
LastBlock = currentBlock - 1,
Partition = 0
});
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
inFile = false;
currentFile++;
2022-03-06 13:29:38 +00:00
continue;
}
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(!partialBlockMt.Success)
{
AaruConsole.ErrorWriteLine(Localization.Found_unhandled_header_cannot_open);
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
_imageStream.Position -= 9;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(!inFile)
{
currentFileStart = currentBlock;
inFile = true;
}
_imageStream.EnsureRead(blockHeader, 0, 16);
2022-03-06 13:29:38 +00:00
mark = Encoding.ASCII.GetString(blockHeader);
Match blockMt = blockRx.Match(mark);
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(!blockMt.Success)
{
AaruConsole.ErrorWriteLine(Localization.Cannot_decode_block_header_cannot_open);
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2022-03-06 13:29:38 +00:00
string blkSize = blockMt.Groups["blockSize"].Value;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(string.IsNullOrWhiteSpace(blkSize))
{
AaruConsole.ErrorWriteLine(Localization.Cannot_decode_block_header_cannot_open);
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(!uint.TryParse(blkSize, out uint blockSize))
{
AaruConsole.ErrorWriteLine(Localization.Cannot_decode_block_header_cannot_open);
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2019-05-06 20:21:57 +01:00
if(blockSize == 0 || blockSize + 17 > imageFilter.DataForkLength)
2022-03-06 13:29:38 +00:00
{
AaruConsole.ErrorWriteLine(Localization.Cannot_decode_block_header_cannot_open);
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
_imageStream.Position += blockSize;
2022-03-06 13:29:38 +00:00
int newLine = _imageStream.ReadByte();
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
if(newLine != 0x0A)
{
AaruConsole.ErrorWriteLine(Localization.Cannot_decode_block_header_cannot_open);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
2019-05-06 20:21:57 +01:00
}
2022-03-06 13:29:38 +00:00
blockPositions.Add(_imageStream.Position - blockSize - 17);
currentBlock++;
_imageInfo.ImageSize += blockSize;
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(_imageInfo.SectorSize < blockSize) _imageInfo.SectorSize = blockSize;
2019-05-06 20:21:57 +01:00
}
2022-03-06 13:29:38 +00:00
_blockPositionCache = blockPositions.ToArray();
2024-05-01 04:39:38 +01:00
TapePartitions =
[
new TapePartition
2022-03-06 13:29:38 +00:00
{
FirstBlock = 0,
LastBlock = currentBlock - 1,
Number = 0
}
2024-05-01 04:39:38 +01:00
];
2022-03-06 13:29:38 +00:00
_imageInfo.Sectors = (ulong)_blockPositionCache.LongLength;
_imageInfo.MediaType = MediaType.UnknownTape;
_imageInfo.Application = "CopyTape";
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
2022-03-06 13:29:38 +00:00
IsTape = true;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
{
buffer = null;
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(sectorAddress >= (ulong)_blockPositionCache.LongLength) return ErrorNumber.OutOfRange;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
_imageStream.Position = _blockPositionCache[sectorAddress];
2019-05-06 20:21:57 +01:00
byte[] blockHeader = new byte[16];
var blockRx = new Regex(BLOCK_REGEX);
2019-05-06 20:21:57 +01:00
_imageStream.EnsureRead(blockHeader, 0, 16);
2022-03-06 13:29:38 +00:00
string mark = Encoding.ASCII.GetString(blockHeader);
Match blockMt = blockRx.Match(mark);
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(!blockMt.Success) return ErrorNumber.InvalidArgument;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
string blkSize = blockMt.Groups["blockSize"].Value;
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(string.IsNullOrWhiteSpace(blkSize)) return ErrorNumber.InvalidArgument;
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(!uint.TryParse(blkSize, out uint blockSize)) return ErrorNumber.InvalidArgument;
2019-05-06 20:21:57 +01:00
2024-05-01 04:05:22 +01:00
if(blockSize == 0 || blockSize + 17 > _imageStream.Length) return ErrorNumber.InvalidArgument;
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
buffer = new byte[blockSize];
_imageStream.EnsureRead(buffer, 0, (int)blockSize);
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
return _imageStream.ReadByte() != 0x0A ? ErrorNumber.InvalidArgument : ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
2022-03-06 13:29:38 +00:00
var ms = new MemoryStream();
2019-05-06 20:21:57 +01:00
2022-03-06 13:29:38 +00:00
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
ms.Write(sector, 0, sector.Length);
2019-05-06 20:21:57 +01:00
}
2022-03-06 13:29:38 +00:00
buffer = ms.ToArray();
return ErrorNumber.NoError;
2019-05-06 18:37:29 +01:00
}
2023-10-03 23:34:59 +01:00
#endregion
2019-05-06 18:37:29 +01:00
}