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:
@@ -353,17 +353,22 @@ namespace Aaru.DiscImages
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ReadSector(ulong sectorAddress)
|
||||
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
|
||||
{
|
||||
buffer = null;
|
||||
|
||||
if(sectorAddress > _imageInfo.Sectors - 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
||||
$"Sector address {sectorAddress} not found");
|
||||
return ErrorNumber.OutOfRange;
|
||||
|
||||
if((_bitmap[sectorAddress / 8] & (1 << (int)(sectorAddress % 8))) == 0)
|
||||
return new byte[_imageInfo.SectorSize];
|
||||
{
|
||||
buffer = new byte[_imageInfo.SectorSize];
|
||||
|
||||
if(_sectorCache.TryGetValue(sectorAddress, out byte[] sector))
|
||||
return sector;
|
||||
return ErrorNumber.NoError;
|
||||
}
|
||||
|
||||
if(_sectorCache.TryGetValue(sectorAddress, out buffer))
|
||||
return ErrorNumber.NoError;
|
||||
|
||||
ulong blockOff = BlockOffset(sectorAddress);
|
||||
|
||||
@@ -377,27 +382,28 @@ namespace Aaru.DiscImages
|
||||
// How many bytes of CRC blocks to skip
|
||||
((long)(blockOff / (CHECK_FREQUENCY / _imageInfo.SectorSize)) * Marshal.SizeOf<CCheck>());
|
||||
|
||||
sector = new byte[_imageInfo.SectorSize];
|
||||
buffer = new byte[_imageInfo.SectorSize];
|
||||
_imageStream.Seek(imageOff, SeekOrigin.Begin);
|
||||
_imageStream.Read(sector, 0, (int)_imageInfo.SectorSize);
|
||||
_imageStream.Read(buffer, 0, (int)_imageInfo.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 than available");
|
||||
return ErrorNumber.OutOfRange;
|
||||
|
||||
var ms = new MemoryStream();
|
||||
|
||||
@@ -412,15 +418,25 @@ namespace Aaru.DiscImages
|
||||
}
|
||||
|
||||
if(allEmpty)
|
||||
return new byte[_imageInfo.SectorSize * length];
|
||||
{
|
||||
buffer = new byte[_imageInfo.SectorSize * length];
|
||||
|
||||
return ErrorNumber.NoError;
|
||||
}
|
||||
|
||||
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