diff --git a/src/AsmResolver/AsmResolver.csproj b/src/AsmResolver/AsmResolver.csproj
index b688c4181..ddaa59dfb 100644
--- a/src/AsmResolver/AsmResolver.csproj
+++ b/src/AsmResolver/AsmResolver.csproj
@@ -28,13 +28,9 @@
runtime; build; native; contentfiles; analyzers; buildtransitive
-
+
-
-
-
-
diff --git a/src/AsmResolver/IO/MemoryMappedDataSource.cs b/src/AsmResolver/IO/MemoryMappedDataSource.cs
deleted file mode 100644
index f47202bba..000000000
--- a/src/AsmResolver/IO/MemoryMappedDataSource.cs
+++ /dev/null
@@ -1,65 +0,0 @@
-using System;
-using AsmResolver.Shims;
-
-namespace AsmResolver.IO
-{
- ///
- /// Represents a data source that obtains its data from a memory mapped file.
- ///
- public sealed class MemoryMappedDataSource : IDataSource, IDisposable
-#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
- , ISpanDataSource
-#endif
- {
- private readonly MemoryMappedFileShim _file;
-
- ///
- /// Creates a new instance of the class.
- ///
- /// The memory mapped file to use.
- internal MemoryMappedDataSource(MemoryMappedFileShim file)
- {
- _file = file ?? throw new ArgumentNullException(nameof(file));
- }
-
- ///
- public ulong BaseAddress => 0;
-
- ///
- public byte this[ulong address] => _file.ReadByte((long) address);
-
- ///
- public ulong Length => (ulong)_file.Size;
-
- ///
- public bool IsValidAddress(ulong address) => address < Length;
-
- ///
- public int ReadBytes(ulong address, byte[] buffer, int index, int count)
- {
- if (address >= Length)
- throw new ArgumentOutOfRangeException(nameof(address));
- int actualLength = (int)Math.Min(Length - address, (ulong)count);
- _file.GetSpan((long)address, actualLength).CopyTo(buffer.AsSpan(index));
- return actualLength;
- }
-
-#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
- ///
- public unsafe int ReadBytes(ulong address, Span buffer)
- {
- if (!IsValidAddress(address))
- return 0;
-
- int actualLength = (int) Math.Min(Length - address, (ulong)buffer.Length);
-
- _file.GetSpan((long)address, actualLength).CopyTo(buffer);
-
- return actualLength;
- }
-#endif
-
- ///
- public void Dispose() => _file.Dispose();
- }
-}
diff --git a/src/AsmResolver/IO/MemoryMappedInputFile.cs b/src/AsmResolver/IO/MemoryMappedInputFile.cs
index 5f5a82258..05bb2071a 100644
--- a/src/AsmResolver/IO/MemoryMappedInputFile.cs
+++ b/src/AsmResolver/IO/MemoryMappedInputFile.cs
@@ -10,17 +10,17 @@ namespace AsmResolver.IO
public sealed class MemoryMappedInputFile : IInputFile
{
private readonly MemoryMappedFileShim _file;
- private readonly MemoryMappedDataSource _dataSource;
+ private readonly UnmanagedDataSource _dataSource;
///
/// Creates a new reader factory for the provided file.
///
/// The path to the file to read.
- public MemoryMappedInputFile(string filePath)
+ public unsafe MemoryMappedInputFile(string filePath)
{
FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
_file = new MemoryMappedFileShim(filePath);
- _dataSource = new MemoryMappedDataSource(_file);
+ _dataSource = new UnmanagedDataSource(_file.BasePointer, (ulong)_file.Size);
}
///
@@ -33,14 +33,13 @@ public string FilePath
public uint Length => (uint) _dataSource.Length;
///
- public BinaryStreamReader CreateReader(ulong address, uint rva, uint length) =>
- new(_dataSource, address, rva, length);
+ public unsafe BinaryStreamReader CreateReader(ulong address, uint rva, uint length) =>
+ new(_dataSource, address != 0 ? address : (ulong)_file.BasePointer, rva, length);
///
public void Dispose()
{
_file.Dispose();
- _dataSource.Dispose();
}
}
}
diff --git a/src/AsmResolver/Shims/MemoryMappedFileShim.Windows.cs b/src/AsmResolver/Shims/MemoryMappedFileShim.Windows.cs
index 08db1a7a5..bc13cb567 100644
--- a/src/AsmResolver/Shims/MemoryMappedFileShim.Windows.cs
+++ b/src/AsmResolver/Shims/MemoryMappedFileShim.Windows.cs
@@ -11,7 +11,7 @@ internal sealed unsafe partial class MemoryMappedFileShim
private const uint FILE_SHARE_READ = 0x2;
private const uint OPEN_EXISTING = 3;
private const uint PAGE_READONLY = 2;
- // not on msdn, stole from terrafx
+ // not on msdn, taken from https://github.com/terrafx/terrafx.interop.windows/blob/e681ccb7239bf9f5629083cd1e396b2876c24aae/sources/Interop/Windows/Windows/um/memoryapi/FILE.cs#L14
private const uint FILE_MAP_READ = 4;
private void* _fileHandle;
diff --git a/src/AsmResolver/Shims/MemoryMappedFileShim.cs b/src/AsmResolver/Shims/MemoryMappedFileShim.cs
index 272a363cf..954cb5ee2 100644
--- a/src/AsmResolver/Shims/MemoryMappedFileShim.cs
+++ b/src/AsmResolver/Shims/MemoryMappedFileShim.cs
@@ -17,6 +17,7 @@ public MemoryMappedFileShim(string path)
}
public long Size => _size;
+ public byte* BasePointer => _file;
public byte ReadByte(long address)
{
@@ -27,16 +28,6 @@ public byte ReadByte(long address)
return _file[address];
}
- public ReadOnlySpan GetSpan(long offset, int length)
- {
- if (_file == null)
- throw new ObjectDisposedException("disposed");
- long newSize = _size - offset;
- if (offset < 0 || newSize < 0 || length > newSize)
- throw new ArgumentOutOfRangeException();
- return new(_file + offset, length);
- }
-
private void DisposeCore()
{
void* filePointer;
diff --git a/test/AsmResolver.Tests/IO/MemoryMappedIOTest.cs b/test/AsmResolver.Tests/IO/MemoryMappedIOTest.cs
index 636f8d3d7..311efd1d8 100644
--- a/test/AsmResolver.Tests/IO/MemoryMappedIOTest.cs
+++ b/test/AsmResolver.Tests/IO/MemoryMappedIOTest.cs
@@ -1,3 +1,4 @@
+using System;
using System.IO;
using AsmResolver.IO;
using AsmResolver.Tests.Runners;