mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Refactor IMediaImage.ReadSector(s) to return error status instead of buffer.
This commit is contained in:
@@ -194,14 +194,15 @@ namespace Aaru.DiscImages
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ReadSector(ulong sectorAddress)
|
||||
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
|
||||
{
|
||||
if(sectorAddress > _imageInfo.Sectors - 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
||||
$"Sector address {sectorAddress} not found");
|
||||
buffer = null;
|
||||
|
||||
if(_sectorCache.TryGetValue(sectorAddress, out byte[] sector))
|
||||
return sector;
|
||||
if(sectorAddress > _imageInfo.Sectors - 1)
|
||||
return ErrorNumber.OutOfRange;
|
||||
|
||||
if(_sectorCache.TryGetValue(sectorAddress, out buffer))
|
||||
return ErrorNumber.NoError;
|
||||
|
||||
ulong index = sectorAddress * _vHdr.sectorSize / _vHdr.blockSize;
|
||||
ulong secOff = sectorAddress * _vHdr.sectorSize % _vHdr.blockSize;
|
||||
@@ -209,44 +210,54 @@ namespace Aaru.DiscImages
|
||||
uint ibmOff = _ibm[(int)index];
|
||||
|
||||
if(ibmOff == VDI_EMPTY)
|
||||
return new byte[_vHdr.sectorSize];
|
||||
{
|
||||
buffer = new byte[_vHdr.sectorSize];
|
||||
|
||||
return ErrorNumber.NoError;
|
||||
}
|
||||
|
||||
ulong imageOff = _vHdr.offsetData + ((ulong)ibmOff * _vHdr.blockSize);
|
||||
|
||||
byte[] cluster = new byte[_vHdr.blockSize];
|
||||
_imageStream.Seek((long)imageOff, SeekOrigin.Begin);
|
||||
_imageStream.Read(cluster, 0, (int)_vHdr.blockSize);
|
||||
sector = new byte[_vHdr.sectorSize];
|
||||
Array.Copy(cluster, (int)secOff, sector, 0, _vHdr.sectorSize);
|
||||
buffer = new byte[_vHdr.sectorSize];
|
||||
Array.Copy(cluster, (int)secOff, buffer, 0, _vHdr.sectorSize);
|
||||
|
||||
if(_sectorCache.Count > MAX_CACHED_SECTORS)
|
||||
_sectorCache.Clear();
|
||||
|
||||
_sectorCache.Add(sectorAddress, sector);
|
||||
_sectorCache.Add(sectorAddress, buffer);
|
||||
|
||||
return sector;
|
||||
return ErrorNumber.NoError;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
||||
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
|
||||
{
|
||||
buffer = null;
|
||||
|
||||
if(sectorAddress > _imageInfo.Sectors - 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
||||
$"Sector address {sectorAddress} not found");
|
||||
return ErrorNumber.OutOfRange;
|
||||
|
||||
if(sectorAddress + length > _imageInfo.Sectors)
|
||||
throw new ArgumentOutOfRangeException(nameof(length),
|
||||
$"Requested more sectors ({sectorAddress} + {length}) than available ({_imageInfo.Sectors})");
|
||||
return ErrorNumber.OutOfRange;
|
||||
|
||||
var ms = new MemoryStream();
|
||||
|
||||
for(uint i = 0; i < length; i++)
|
||||
{
|
||||
byte[] sector = ReadSector(sectorAddress + i);
|
||||
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
|
||||
|
||||
if(errno != ErrorNumber.NoError)
|
||||
return errno;
|
||||
|
||||
ms.Write(sector, 0, sector.Length);
|
||||
}
|
||||
|
||||
return ms.ToArray();
|
||||
buffer = ms.ToArray();
|
||||
|
||||
return ErrorNumber.NoError;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user