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

@@ -26,8 +26,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseArchive(dataStream);
}
@@ -43,7 +42,7 @@ namespace BurnOutSharp.Builder
public static Archive ParseArchive(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -24,8 +24,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseExecutable(dataStream);
}
@@ -41,7 +40,7 @@ namespace BurnOutSharp.Builder
public static Executable ParseExecutable(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -25,8 +25,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseExecutable(dataStream);
}
@@ -42,7 +41,7 @@ namespace BurnOutSharp.Builder
public static Executable ParseExecutable(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -72,8 +72,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseCabinet(dataStream);
}
@@ -89,7 +88,7 @@ namespace BurnOutSharp.Builder
public static Cabinet ParseCabinet(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -225,8 +225,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseArchive(dataStream);
}
@@ -242,7 +241,7 @@ namespace BurnOutSharp.Builder
public static Archive ParseArchive(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -27,8 +27,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseExecutable(dataStream);
}
@@ -44,7 +43,7 @@ namespace BurnOutSharp.Builder
public static Executable ParseExecutable(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -29,8 +29,7 @@ namespace BurnOutSharp.Builder
return null;
// Create a memory stream and parse that
MemoryStream dataStream = new MemoryStream(data);
dataStream.Position = offset;
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return ParseExecutable(dataStream);
}
@@ -46,7 +45,7 @@ namespace BurnOutSharp.Builder
public static Executable ParseExecutable(Stream data)
{
// If the data is invalid
if (data == null)
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
// If the offset is out of bounds

View File

@@ -55,8 +55,16 @@ namespace BurnOutSharp.Wrappers
/// <returns>A BFPK archive wrapper on success, null on failure</returns>
public static BFPK 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);
}
@@ -67,6 +75,10 @@ namespace BurnOutSharp.Wrappers
/// <returns>A BFPK archive wrapper on success, null on failure</returns>
public static BFPK Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var archive = Builder.BFPK.ParseArchive(data);
if (archive == null)
return null;

View File

@@ -301,8 +301,16 @@ namespace BurnOutSharp.Wrappers
/// <returns>An LE/LX executable wrapper on success, null on failure</returns>
public static LinearExecutable 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);
}
@@ -313,6 +321,10 @@ namespace BurnOutSharp.Wrappers
/// <returns>An LE/LX executable wrapper on success, null on failure</returns>
public static LinearExecutable Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var executable = Builder.LinearExecutable.ParseExecutable(data);
if (executable == null)
return null;

View File

@@ -105,8 +105,16 @@ namespace BurnOutSharp.Wrappers
/// <returns>An MS-DOS executable wrapper on success, null on failure</returns>
public static MSDOS 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);
}
@@ -117,6 +125,10 @@ namespace BurnOutSharp.Wrappers
/// <returns>An MS-DOS executable wrapper on success, null on failure</returns>
public static MSDOS Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var executable = Builder.MSDOS.ParseExecutable(data);
if (executable == null)
return null;

View File

@@ -116,8 +116,16 @@ namespace BurnOutSharp.Wrappers
/// <returns>A cabinet wrapper on success, null on failure</returns>
public static MicrosoftCabinet 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);
}
@@ -128,6 +136,10 @@ namespace BurnOutSharp.Wrappers
/// <returns>A cabinet wrapper on success, null on failure</returns>
public static MicrosoftCabinet Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var cabinet = Builder.MicrosoftCabinet.ParseCabinet(data);
if (cabinet == null)
return null;

View File

@@ -233,8 +233,16 @@ namespace BurnOutSharp.Wrappers
/// <returns>An NE executable wrapper on success, null on failure</returns>
public static NewExecutable 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);
}
@@ -245,6 +253,10 @@ namespace BurnOutSharp.Wrappers
/// <returns>An NE executable wrapper on success, null on failure</returns>
public static NewExecutable Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var executable = Builder.NewExecutable.ParseExecutable(data);
if (executable == null)
return null;

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;