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

214 lines
7.8 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2019-05-06 18:37:29 +01:00
// ****************************************************************************/
using System;
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;
2019-05-06 18:37:29 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages.CopyTape
2019-05-06 18:37:29 +01:00
{
public partial class CopyTape
{
2019-05-06 20:21:57 +01:00
public bool Open(IFilter imageFilter)
{
List<long> blockPositions = new List<long>();
2020-02-29 18:03:35 +00:00
var partialBlockRx = new Regex(PartialBlockRegex);
var blockRx = new Regex(BlockRegex);
var filemarkRx = new Regex(FilemarkRegex);
var eotRx = new Regex(EndOfTapeRegex);
2019-05-06 18:37:29 +01:00
2020-02-29 18:03:35 +00:00
if(imageFilter.GetDataForkLength() <= 16)
return false;
2019-05-06 18:37:29 +01:00
2019-05-06 20:21:57 +01:00
imageStream = imageFilter.GetDataForkStream();
imageStream.Position = 0;
byte[] header = new byte[9];
byte[] blockHeader = new byte[16];
ulong currentBlock = 0;
uint currentFile = 0;
ulong currentFileStart = 0;
bool inFile = false;
Files = new List<TapeFile>();
while(imageStream.Position + 9 < imageStream.Length)
{
imageStream.Read(header, 0, 9);
string mark = Encoding.ASCII.GetString(header);
Match partialBlockMt = partialBlockRx.Match(mark);
Match filemarkMt = filemarkRx.Match(mark);
Match eotMt = eotRx.Match(mark);
2020-02-29 18:03:35 +00:00
if(eotMt.Success)
break;
2019-05-06 20:21:57 +01:00
if(filemarkMt.Success)
{
Files.Add(new TapeFile
{
2020-02-29 18:03:35 +00:00
File = currentFile, FirstBlock = currentFileStart, LastBlock = currentBlock - 1, Partition = 0
2019-05-06 20:21:57 +01:00
});
2020-02-29 18:03:35 +00:00
2019-05-06 20:21:57 +01:00
inFile = false;
currentFile++;
2020-02-29 18:03:35 +00:00
2019-05-06 20:21:57 +01:00
continue;
}
2020-02-29 18:03:35 +00:00
if(!partialBlockMt.Success)
throw new ArgumentException("Found unhandled header, cannot open.");
2019-05-06 20:21:57 +01:00
imageStream.Position -= 9;
if(!inFile)
{
currentFileStart = currentBlock;
inFile = true;
}
imageStream.Read(blockHeader, 0, 16);
mark = Encoding.ASCII.GetString(blockHeader);
Match blockMt = blockRx.Match(mark);
2020-02-29 18:03:35 +00:00
if(!blockMt.Success)
throw new ArgumentException("Cannot decode block header, cannot open.");
2019-05-06 20:21:57 +01:00
string blkSize = blockMt.Groups["blockSize"].Value;
if(string.IsNullOrWhiteSpace(blkSize))
throw new ArgumentException("Cannot decode block header, cannot open.");
if(!uint.TryParse(blkSize, out uint blockSize))
throw new ArgumentException("Cannot decode block header, cannot open.");
2020-02-29 18:03:35 +00:00
if(blockSize == 0 ||
blockSize + 17 > imageFilter.GetDataForkLength())
2019-05-06 20:21:57 +01:00
throw new ArgumentException("Cannot decode block header, cannot open.");
imageStream.Position += blockSize;
int newLine = imageStream.ReadByte();
2020-02-29 18:03:35 +00:00
if(newLine != 0x0A)
throw new ArgumentException("Cannot decode block header, cannot open.");
2019-05-06 20:21:57 +01:00
blockPositions.Add(imageStream.Position - blockSize - 17);
currentBlock++;
imageInfo.ImageSize += blockSize;
2020-02-29 18:03:35 +00:00
if(imageInfo.SectorSize < blockSize)
imageInfo.SectorSize = blockSize;
2019-05-06 20:21:57 +01:00
}
blockPositionCache = blockPositions.ToArray();
TapePartitions = new List<TapePartition>
{
2020-02-29 18:03:35 +00:00
new TapePartition
{
FirstBlock = 0, LastBlock = currentBlock - 1, Number = 0
}
2019-05-06 20:21:57 +01:00
};
imageInfo.Sectors = (ulong)blockPositionCache.LongLength;
imageInfo.MediaType = MediaType.UnknownTape;
imageInfo.Application = "CopyTape";
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
2019-05-06 22:42:13 +01:00
IsTape = true;
2019-05-06 20:21:57 +01:00
return true;
}
public byte[] ReadSector(ulong sectorAddress)
{
if(sectorAddress >= (ulong)blockPositionCache.LongLength)
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
$"Sector address {sectorAddress} not found");
imageStream.Position = blockPositionCache[sectorAddress];
byte[] blockHeader = new byte[16];
2020-02-29 18:03:35 +00:00
var blockRx = new Regex(BlockRegex);
2019-05-06 20:21:57 +01:00
imageStream.Read(blockHeader, 0, 16);
string mark = Encoding.ASCII.GetString(blockHeader);
Match blockMt = blockRx.Match(mark);
2020-02-29 18:03:35 +00:00
if(!blockMt.Success)
throw new ArgumentException("Cannot decode block header, cannot read.");
2019-05-06 20:21:57 +01:00
string blkSize = blockMt.Groups["blockSize"].Value;
if(string.IsNullOrWhiteSpace(blkSize))
throw new ArgumentException("Cannot decode block header, cannot read.");
if(!uint.TryParse(blkSize, out uint blockSize))
throw new ArgumentException("Cannot decode block header, cannot read.");
2020-02-29 18:03:35 +00:00
if(blockSize == 0 ||
blockSize + 17 > imageStream.Length)
2019-05-06 20:21:57 +01:00
throw new ArgumentException("Cannot decode block header, cannot read.");
byte[] data = new byte[blockSize];
imageStream.Read(data, 0, (int)blockSize);
2020-02-29 18:03:35 +00:00
if(imageStream.ReadByte() != 0x0A)
throw new ArgumentException("Cannot decode block header, cannot read.");
2019-05-06 20:21:57 +01:00
return data;
}
public byte[] ReadSectors(ulong sectorAddress, uint length)
{
2020-02-29 18:03:35 +00:00
var dataMs = new MemoryStream();
2019-05-06 20:21:57 +01:00
for(uint i = 0; i < length; i++)
{
byte[] data = ReadSector(sectorAddress + i);
dataMs.Write(data, 0, data.Length);
}
return dataMs.ToArray();
}
2019-05-06 18:37:29 +01:00
}
}