Refactor IMediaImage.ReadSector(s) to return error status instead of buffer.

This commit is contained in:
2021-09-19 21:16:47 +01:00
parent fbccfb2ca9
commit f51d414abd
187 changed files with 4036 additions and 2081 deletions

View File

@@ -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;
}
}
}