/// <summary> /// Encode given statements. /// </summary> /// <param name="toEncode">String that contains the statements to encode</param> /// <param name="address">Address of the first instruction.</param> /// <returns>Result of the assemble operation or null if it failed && throwOnError is false.</returns> /// <exception cref="InvalidOperationException">If keystone return an error && throwOnError is true</exception> public KeystoneEncoded Assemble(string toEncode, ulong address) { IntPtr encoding; uint size; uint statementCount; byte[] buffer; int result = KeystoneImports.Assemble(engine, toEncode, address, out encoding, out size, out statementCount); if (result != 0) { if (throwOnError) { throw new InvalidOperationException($"Error while assembling {toEncode}: {ErrorToString(GetLastKeystoneError())}"); } return(null); } buffer = new byte[size]; Marshal.Copy(encoding, buffer, 0, (int)size); KeystoneImports.Free(encoding); return(new KeystoneEncoded(buffer, statementCount, address)); }
public void Dispose() { IntPtr ks = Interlocked.Exchange(ref this.engine, IntPtr.Zero); if (ks != IntPtr.Zero) { int num = (int)KeystoneImports.Close(ks); } GC.SuppressFinalize((object)this); }
/// <summary> /// Return a string associated with a given error code. /// </summary> /// <param name="result">Error code</param> /// <returns>The string</returns> public static string ErrorToString(KeystoneError result) { IntPtr error = KeystoneImports.ErrorToString(result); if (error != IntPtr.Zero) { return(Marshal.PtrToStringAnsi(error)); } return(string.Empty); }
static KeystoneImports() { string str = Path.GetDirectoryName(new Uri(typeof(KeystoneImports).Assembly.CodeBase).LocalPath) + (IntPtr.Size == 8 ? "\\win64\\" : "\\win32\\") + "keystone.dll"; if (!File.Exists(str)) { return; } KeystoneImports.LoadLibrary(str); }
/// <summary> /// Construct the object with a given architecture and a given mode. /// </summary> /// <param name="architecture">Architecture</param> /// <param name="mode">Mode, i.e. endianess, word size etc.</param> /// <param name="throwOnKeystoneError">Throw when there are errors</param> /// <remarks> /// Some architectures are not supported. /// Check with <see cref="IsArchitectureSupported(KeystoneArchitecture)"/> if the engine /// support the architecture. /// </remarks> public Keystone(KeystoneArchitecture architecture, KeystoneMode mode, bool throwOnKeystoneError = true) { internalImpl = SymbolResolver; throwOnError = throwOnKeystoneError; var result = KeystoneImports.Open(architecture, (int)mode, ref engine); if (result != KeystoneError.KS_ERR_OK && throwOnKeystoneError) { throw new InvalidOperationException($"Error while initializing keystone: {ErrorToString(result)}"); } }
/// <summary> /// Release the engine. /// </summary> public void Dispose() { var currentEngine = Interlocked.Exchange(ref engine, IntPtr.Zero); if (currentEngine != IntPtr.Zero) { KeystoneImports.Close(currentEngine); } GC.SuppressFinalize(this); }
public bool SetOption(KeystoneOptionType type, uint value) { KeystoneError result = KeystoneImports.SetOption(this.engine, type, (IntPtr)(long)value); if (result == KeystoneError.KS_ERR_OK) { return(true); } if (this.throwOnError) { throw new InvalidOperationException(string.Format("Error while setting option in keystone: {0}", (object)Keystone.ErrorToString(result))); } return(false); }
public Keystone( KeystoneArchitecture architecture, KeystoneMode mode, bool throwOnKeystoneError = true) { this.internalImpl = new Keystone.ResolverInternal(this.SymbolResolver); this.throwOnError = throwOnKeystoneError; KeystoneError result = KeystoneImports.Open(architecture, (int)mode, ref this.engine); if ((uint)result > 0U & throwOnKeystoneError) { throw new InvalidOperationException(string.Format("Error while initializing keystone: {0}", (object)Keystone.ErrorToString(result))); } }
/// <summary> /// Set an option in the engine. /// </summary> /// <param name="type">Type of option</param> /// <param name="value">Value</param> /// <returns>True is the option is correctly setted, False otherwise && throwOnError is false.</returns> /// <exception cref="InvalidOperationException">If Keystone return an error && throwOnError is true</exception> public bool SetOption(KeystoneOptionType type, uint value) { var result = KeystoneImports.SetOption(engine, type, (IntPtr)value); if (result != KeystoneError.KS_ERR_OK) { if (throwOnError) { throw new InvalidOperationException($"Error while setting option in keystone: {ErrorToString(result)}"); } return(false); } return(true); }
public KeystoneEncoded Assemble(string toEncode, ulong address) { IntPtr encoding; uint size; uint statements; if (KeystoneImports.Assemble(this.engine, toEncode, address, out encoding, out size, out statements) != 0) { if (this.throwOnError) { throw new InvalidOperationException(string.Format("Error while assembling {0}: {1}", (object)toEncode, (object)Keystone.ErrorToString(this.GetLastKeystoneError()))); } return((KeystoneEncoded)null); } byte[] numArray = new byte[(int)size]; Marshal.Copy(encoding, numArray, 0, (int)size); KeystoneImports.Free(encoding); return(new KeystoneEncoded(numArray, statements, address)); }
public static string ErrorToString(KeystoneError result) { IntPtr ptr = KeystoneImports.ErrorToString(result); return(ptr != IntPtr.Zero ? Marshal.PtrToStringAnsi(ptr) : string.Empty); }
public static uint GetKeystoneVersion(ref uint major, ref uint minor) { return(KeystoneImports.Version(ref major, ref minor)); }
public static bool IsArchitectureSupported(KeystoneArchitecture architecture) { return(KeystoneImports.IsArchitectureSupported(architecture)); }
public KeystoneError GetLastKeystoneError() { return(KeystoneImports.GetLastKeystoneError(this.engine)); }