Stream safety and better streams

This commit is contained in:
Matt Nadareski
2022-12-15 14:20:27 -08:00
parent 16e71c910e
commit aded5ee03a
13 changed files with 98 additions and 33 deletions

View File

@@ -871,8 +871,16 @@ namespace BurnOutSharp.Wrappers
/// <returns>A PE executable wrapper on success, null on failure</returns>
public static PortableExecutable Create(byte[] data, int offset)
{
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
// If the data is invalid
if (data == null)
return null;
// If the offset is out of bounds
if (offset < 0 || offset >= data.Length)
return null;
// Create a memory stream and use that
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return Create(dataStream);
}
@@ -883,6 +891,10 @@ namespace BurnOutSharp.Wrappers
/// <returns>A PE executable wrapper on success, null on failure</returns>
public static PortableExecutable Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var executable = Builder.PortableExecutable.ParseExecutable(data);
if (executable == null)
return null;