Skip to content

Commit

Permalink
Support loading FASM and FASM64 from directory of Reloaded.Injector DLL
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jun 1, 2019
1 parent f06f84a commit 018bbde
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
57 changes: 46 additions & 11 deletions Source/Reloaded.Assembler/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -76,17 +77,13 @@ public Assembler(int textSize = 0x10000, int resultSize = 0x8000)

IntPtr fasmDllHandle;

// Set functions
if (IntPtr.Size == 4)
fasmDllHandle = LoadLibraryW("FASM.dll");
else if (IntPtr.Size == 8)
fasmDllHandle = LoadLibraryW("FASMX64.dll");
else
{
// Does not actually check OS or architecture but it should be good enough for our purposes.
// Users should know that this is a Windows lib for x86/x64
throw new FasmWrapperException("Only 32bit and 64bit desktop architectures are supported (X86 and X86_64).");
}
// Get path of FASM dll
string fasmDllPath = GetFasmDLLPath();
fasmDllHandle = LoadLibraryW(fasmDllPath);

// Throw exception if dll not loaded.
if (fasmDllHandle == null)
throw new FasmWrapperException("Failed to load FASM dll. The FASM dll pointer from LoadLibraryW is null.");

// Obtain delegates to FASM functions.
IntPtr assembleAddress = GetProcAddress(fasmDllHandle, "fasm_Assemble");
Expand Down Expand Up @@ -192,6 +189,44 @@ public byte[] Assemble(string mnemonics, ushort passLimit = 100)
}
}

/// <summary>
/// Retrieves the path of the FASM dll to load.
/// </summary>
private string GetFasmDLLPath()
{
const string FASM86DLL = "FASM.dll";
const string FASM64DLL = "FASMX64.dll";

// Check current directory.
if (IntPtr.Size == 4 && File.Exists(FASM86DLL))
return FASM86DLL;
else if (IntPtr.Size == 8 && File.Exists(FASM64DLL))
return FASM64DLL;

// Check DLL Directory
string assemblyDirectory = GetExecutingDLLDirectory();
string asmDirectoryFasm86 = Path.Combine(assemblyDirectory, FASM86DLL);
string asmDirectoryFasm64 = Path.Combine(assemblyDirectory, FASM64DLL);

if (IntPtr.Size == 4 && File.Exists(asmDirectoryFasm86))
return asmDirectoryFasm86;
else if (IntPtr.Size == 8 && File.Exists(asmDirectoryFasm64))
return asmDirectoryFasm64;

throw new FasmWrapperException("Appropriate FASM DLL for X86/64 has not been found in either current or library directory.");
}

/// <summary>
/// Gets the directory of the currently executing assembly.
/// </summary>
private string GetExecutingDLLDirectory()
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}

/// <summary>
/// Attempts to allocate the memory to store the text to be supplied to FASM assembler.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/Reloaded.Assembler/Reloaded.Assembler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408?s=400&amp;u=b591dd9f053703e87a08ccc56287a9119e9758cb&amp;v=4</PackageIconUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Assembler</RepositoryUrl>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

Expand Down

0 comments on commit 018bbde

Please sign in to comment.