Ensure file reading operations return the expected data.

This commit is contained in:
2024-11-12 06:58:01 +00:00
parent cde062b0ac
commit 03d3d8f973
7 changed files with 95 additions and 23 deletions

View File

@@ -83,8 +83,8 @@ public static class FAT
var bpbSector = new byte[512];
var fatSector = new byte[512];
imageStream.Position = 0;
imageStream.Read(bpbSector, 0, 512);
imageStream.Read(fatSector, 0, 512);
imageStream.EnsureRead(bpbSector, 0, 512);
imageStream.EnsureRead(fatSector, 0, 512);
Array.Copy(bpbSector, 0x02, atariOem, 0, 6);
Array.Copy(bpbSector, 0x03, dosOem, 0, 8);
@@ -202,12 +202,12 @@ public static class FAT
// First FAT1 sector resides at LBA 0x14
var fat1Sector0 = new byte[512];
imageStream.Position = 0x14 * 512;
imageStream.Read(fat1Sector0, 0, 512);
imageStream.EnsureRead(fat1Sector0, 0, 512);
// First FAT2 sector resides at LBA 0x1A
var fat2Sector0 = new byte[512];
imageStream.Position = 0x1A * 512;
imageStream.Read(fat2Sector0, 0, 512);
imageStream.EnsureRead(fat2Sector0, 0, 512);
bool equalFatIds = fat1Sector0[0] == fat2Sector0[0] && fat1Sector0[1] == fat2Sector0[1];
// Volume is software interleaved 2:1
@@ -221,7 +221,7 @@ public static class FAT
})
{
imageStream.Position = position * 512;
imageStream.Read(tmp, 0, 512);
imageStream.EnsureRead(tmp, 0, 512);
rootMs.Write(tmp, 0, tmp.Length);
}

View File

@@ -0,0 +1,72 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Extensions.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Helpers.
//
// --[ Description ] ----------------------------------------------------------
//
// Provides class extensions.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
using System.IO;
namespace RomRepoMgr.Core;
public static class Extensions
{
/// <summary>
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the
/// position within the stream by the number of bytes read.<br /> Guarantees the whole count of bytes is read or EOF is
/// found
/// </summary>
/// <param name="s">Stream to extend</param>
/// <param name="buffer">
/// An array of bytes. When this method returns, the buffer contains the specified byte array with the
/// values between <see cref="offset" /> and (<see cref="offset" /> + <see cref="count" /> - 1) replaced by the bytes
/// read from the current source.
/// </param>
/// <param name="offset">
/// The zero-based byte offset in <see cref="buffer" /> at which to begin storing the data read from
/// the current stream.
/// </param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if the end
/// of the stream has been reached.
/// </returns>
public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count)
{
var pos = 0;
int read;
do
{
read = s.Read(buffer, pos + offset, count - pos);
pos += read;
} while(read > 0);
return pos;
}
}

View File

@@ -432,7 +432,7 @@ public class Vfs : IDisposable
stream.Position = offset;
return stream.Read(buf, 0, buf.Length);
return stream.EnsureRead(buf, 0, buf.Length);
}
finally
{

View File

@@ -118,12 +118,12 @@ internal sealed class ForcedSeekStream<T> : Stream where T : Stream
for(var i = 0; i < fullBufferReads; i++)
{
buffer = new byte[BUFFER_LEN];
_baseStream.Read(buffer, 0, BUFFER_LEN);
_baseStream.EnsureRead(buffer, 0, BUFFER_LEN);
_backStream.Write(buffer, 0, BUFFER_LEN);
}
buffer = new byte[restToRead];
_baseStream.Read(buffer, 0, restToRead);
_baseStream.EnsureRead(buffer, 0, restToRead);
_backStream.Write(buffer, 0, restToRead);
}

View File

@@ -66,7 +66,7 @@ public sealed class Compression
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
zStream.Write(buffer, 0, buffer.Length);
}
@@ -79,7 +79,7 @@ public sealed class Compression
Maximum = inFs.Length
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
zStream.Write(buffer, 0, buffer.Length);
inFs.Close();

View File

@@ -303,7 +303,7 @@ public class FileExporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
}
@@ -315,7 +315,7 @@ public class FileExporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
inFs.Close();
@@ -470,7 +470,7 @@ public class FileExporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
}
@@ -482,7 +482,7 @@ public class FileExporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
inFs.Close();

View File

@@ -349,7 +349,7 @@ public class FileImporter
});
buffer = new byte[BUFFER_SIZE];
inFs.Read(buffer, 0, (int)BUFFER_SIZE);
inFs.EnsureRead(buffer, 0, (int)BUFFER_SIZE);
checksumWorker.Update(buffer);
}
@@ -360,14 +360,14 @@ public class FileImporter
});
buffer = new byte[remainder];
inFs.Read(buffer, 0, (int)remainder);
inFs.EnsureRead(buffer, 0, (int)remainder);
checksumWorker.Update(buffer);
}
else
{
SetIndeterminateProgress2?.Invoke(this, System.EventArgs.Empty);
buffer = new byte[inFs.Length];
inFs.Read(buffer, 0, (int)inFs.Length);
inFs.EnsureRead(buffer, 0, (int)inFs.Length);
checksumWorker.Update(buffer);
}
@@ -527,7 +527,7 @@ public class FileImporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
zStream.Write(buffer, 0, buffer.Length);
}
@@ -539,7 +539,7 @@ public class FileImporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
zStream.Write(buffer, 0, buffer.Length);
SetIndeterminateProgress2?.Invoke(this, System.EventArgs.Empty);
@@ -802,7 +802,7 @@ public class FileImporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
}
@@ -814,7 +814,7 @@ public class FileImporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
SetIndeterminateProgress2?.Invoke(this, System.EventArgs.Empty);
@@ -1139,7 +1139,7 @@ public class FileImporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
}
@@ -1151,7 +1151,7 @@ public class FileImporter
Value = inFs.Position
});
inFs.Read(buffer, 0, buffer.Length);
inFs.EnsureRead(buffer, 0, buffer.Length);
outFs.Write(buffer, 0, buffer.Length);
SetIndeterminateProgress2?.Invoke(this, System.EventArgs.Empty);