diff --git a/RomRepoMgr.Core/Aaru/FAT.cs b/RomRepoMgr.Core/Aaru/FAT.cs index 5ebf0c6..05ef273 100644 --- a/RomRepoMgr.Core/Aaru/FAT.cs +++ b/RomRepoMgr.Core/Aaru/FAT.cs @@ -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); } diff --git a/RomRepoMgr.Core/Extensions.cs b/RomRepoMgr.Core/Extensions.cs new file mode 100644 index 0000000..44da3b3 --- /dev/null +++ b/RomRepoMgr.Core/Extensions.cs @@ -0,0 +1,72 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : Extensions.cs +// Author(s) : Natalia Portillo +// +// 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2024 Natalia Portillo +// ****************************************************************************/ + +using System.IO; + +namespace RomRepoMgr.Core; + +public static class Extensions +{ + /// + /// 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.
Guarantees the whole count of bytes is read or EOF is + /// found + ///
+ /// Stream to extend + /// + /// An array of bytes. When this method returns, the buffer contains the specified byte array with the + /// values between and ( + - 1) replaced by the bytes + /// read from the current source. + /// + /// + /// The zero-based byte offset in at which to begin storing the data read from + /// the current stream. + /// + /// The maximum number of bytes to be read from the current stream. + /// + /// 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. + /// + 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; + } +} \ No newline at end of file diff --git a/RomRepoMgr.Core/Filesystem/Vfs.cs b/RomRepoMgr.Core/Filesystem/Vfs.cs index 02834c5..9d9401f 100644 --- a/RomRepoMgr.Core/Filesystem/Vfs.cs +++ b/RomRepoMgr.Core/Filesystem/Vfs.cs @@ -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 { diff --git a/RomRepoMgr.Core/ForcedSeekStream.cs b/RomRepoMgr.Core/ForcedSeekStream.cs index cc4c956..480d66a 100644 --- a/RomRepoMgr.Core/ForcedSeekStream.cs +++ b/RomRepoMgr.Core/ForcedSeekStream.cs @@ -118,12 +118,12 @@ internal sealed class ForcedSeekStream : 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); } diff --git a/RomRepoMgr.Core/Workers/Compression.cs b/RomRepoMgr.Core/Workers/Compression.cs index f10f8f9..0f2f232 100644 --- a/RomRepoMgr.Core/Workers/Compression.cs +++ b/RomRepoMgr.Core/Workers/Compression.cs @@ -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(); diff --git a/RomRepoMgr.Core/Workers/FileExporter.cs b/RomRepoMgr.Core/Workers/FileExporter.cs index fbe9cd4..be3f33c 100644 --- a/RomRepoMgr.Core/Workers/FileExporter.cs +++ b/RomRepoMgr.Core/Workers/FileExporter.cs @@ -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(); diff --git a/RomRepoMgr.Core/Workers/FileImporter.cs b/RomRepoMgr.Core/Workers/FileImporter.cs index 7bcdbf6..3b6e83f 100644 --- a/RomRepoMgr.Core/Workers/FileImporter.cs +++ b/RomRepoMgr.Core/Workers/FileImporter.cs @@ -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);