justinstenning / SharpDisasm

SharpDisasm - x86 / x86-64 disassembler for .NET
https://www.nuget.org/packages/SharpDisasm
Other
212 stars 39 forks source link

Add support for generating different 'Instruction' class #18

Closed shlomoartsi closed 6 years ago

shlomoartsi commented 6 years ago

A support is added to generate a different Instruction class (ie MyInstruction). For that we define an IInstruction interface for Instruction class. in addition a IInstructionFactory is injected to the ctor of Disassembler in order to generate a different class implementing IInstruction . The backward compatibility is maintained, and if no implementation of IInstructionFactory is injected than an InstructionFactory class that generate the Instruction class is used.

justinstenning commented 6 years ago

The PR doesn't appear to have your changes in it.

shlomoartsi commented 6 years ago

Hi, I'll try to explain my required change in more details and examples. I've used your project for implementing a binary obfuscation project. For that i needed to add more info for Instruction class, such as previous and next instruction fields. I didn't want to add these fields on the project itself, but on my custom class that defines an instruction. I've found that the most structured way for doing this is to change the project to use interface of IInstruction instead of actual class of Instruction , and to use dependency injection for factory of IInstruction implementation instead of using "new Instruction()" where needed. An example for the dependency injection is the Disassembler ctor. This injection allows the consumer of the project to have an actual control of the creation of Instruction class (and by the way promote the testablity of Disassembler class):

public Disassembler(IAssemblyCode code, ArchitectureMode architecture, ulong address = 0x0, bool copyBinaryToInstruction = false, Vendor vendor = Vendor.Any, IInstructionFactory instructionFactory = null) { ... if (instructionFactory == null) { this.InstructionFactory = new InstructionFactory(); } else { this.InstructionFactory = instructionFactory; } ... }

using the factory: _public IInstruction NextInstruction() { int length = 0; if ((length = Udis86.udis86.ud_disassemble(ref _u)) > 0) { var instruction = InstructionFactory.Create(ref _u, CopyBinaryToInstruction); if (!instruction.Error) { BytesDecoded += length; } return instruction; } return null; }_

justinstenning commented 6 years ago

@shlomoartsi are you saying you have made these changes and would like to prepare in a pull request, or is this a feature request? If a feature request, can you please update the issue #17 with these details also.

shlomoartsi commented 6 years ago

Hi, Yes i've made those changes (and more) already and would like create a pull request. Regards, Shlomo

On Mon, Jul 2, 2018 at 12:40 PM Justin Stenning notifications@github.com wrote:

@shlomoartsi https://github.com/shlomoartsi are you saying you have made these changes and would like to prepare in a pull request, or is this a feature request? If a feature request, can you please update the issue

17 https://github.com/spazzarama/SharpDisasm/issues/17 with these

details also.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/spazzarama/SharpDisasm/pull/18#issuecomment-401736657, or mute the thread https://github.com/notifications/unsubscribe-auth/Am19PY-NbUV_eUUjJXmUKSJl6tbWIqUWks5uCeqJgaJpZM4U-s3_ .