Copy input stream before using

This commit is contained in:
Katy Coe
2019-10-22 00:37:38 +02:00
parent c88f058a39
commit ec0e771f5a
2 changed files with 11 additions and 3 deletions

View File

@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Reflection;
using NoisyCowStudios.Bin2Object;
@@ -42,6 +43,8 @@ namespace Il2CppInspector
internal class FileFormatReader
{
// Helper method to try all defined file formats when the contents of the binary is unknown
public static IFileFormatReader Load(string filename) => Load(new FileStream(filename, FileMode.Open, FileAccess.Read));
public static IFileFormatReader Load(Stream stream) {
var types = Assembly.GetExecutingAssembly().DefinedTypes
.Where(x => x.ImplementedInterfaces.Contains(typeof(IFileFormatReader)) && !x.IsGenericTypeDefinition);
@@ -79,13 +82,18 @@ namespace Il2CppInspector
}
public static T Load(string filename) {
using (var stream = new FileStream(filename, FileMode.Open))
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
return Load(stream);
}
public static T Load(Stream stream) {
// Copy the original stream in case we modify it
var ms = new MemoryStream();
stream.Position = 0;
var pe = (T) Activator.CreateInstance(typeof(T), stream);
stream.CopyTo(ms);
ms.Position = 0;
var pe = (T) Activator.CreateInstance(typeof(T), ms);
return pe.Init() ? pe : null;
}

View File

@@ -159,7 +159,7 @@ namespace Il2CppInspector
Console.WriteLine("Detected metadata version " + metadata.Version);
// Load the il2cpp code file (try all available file formats)
IFileFormatReader stream = FileFormatReader.Load(new MemoryStream(File.ReadAllBytes(codeFile)));
IFileFormatReader stream = FileFormatReader.Load(codeFile);
if (stream == null) {
Console.Error.WriteLine("Unsupported executable file format");
return null;