diff options
author | dgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-26 00:50:20 +0000 |
---|---|---|
committer | dgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-26 00:50:20 +0000 |
commit | 423a381f4fd3efd99dfd7bc932777ea596cf7b17 (patch) | |
tree | fdbf4a4bc5f2b8d73b90020da470c40a22f4cc2a /courgette/disassembler.cc | |
parent | da1543a1a526aefd1114853cf737846eb5c29640 (diff) | |
download | chromium_src-423a381f4fd3efd99dfd7bc932777ea596cf7b17.zip chromium_src-423a381f4fd3efd99dfd7bc932777ea596cf7b17.tar.gz chromium_src-423a381f4fd3efd99dfd7bc932777ea596cf7b17.tar.bz2 |
Further refactoring, move ImageInfo into Disassembler/DisassemblerWin32X86.
This means that all PE specific knowledge is now contained in a single class
which leaves us in pretty good shape for supporting ELF 32.
There are still widespread assumptions about being 32 bit, but those can be
addressed at a much later date.
BUG=None
TEST=Unittests
Review URL: http://codereview.chromium.org/8166013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107260 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'courgette/disassembler.cc')
-rw-r--r-- | courgette/disassembler.cc | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/courgette/disassembler.cc b/courgette/disassembler.cc index f4ae86d..edacd4b 100644 --- a/courgette/disassembler.cc +++ b/courgette/disassembler.cc @@ -15,7 +15,6 @@ #include "courgette/courgette.h" #include "courgette/disassembler_win32_x86.h" #include "courgette/encoded_program.h" -#include "courgette/image_info.h" // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently // different target addresses are referenced. Purely for debugging. @@ -25,45 +24,56 @@ namespace courgette { //////////////////////////////////////////////////////////////////////////////// -ExecutableType DetectExecutableType(const void* buffer, size_t length) { +Disassembler* DetectDisassembler(const void* buffer, size_t length) { + Disassembler* disassembler = NULL; - bool parsed = false; + disassembler = new DisassemblerWin32X86(buffer, length); + if (disassembler->ParseHeader()) + return disassembler; - PEInfo* pe_info = new PEInfo(); - pe_info->Init(buffer, length); - parsed = pe_info->ParseHeader(); - delete pe_info; + delete disassembler; + + return NULL; +} - if (parsed) - return WIN32_X86; +Status DetectExecutableType(const void* buffer, size_t length, + ExecutableType* type, + size_t* detected_length) { - return UNKNOWN; + Disassembler* disassembler = DetectDisassembler(buffer, length); + + if (disassembler) { + *type = disassembler->kind(); + *detected_length = disassembler->length(); + delete disassembler; + return C_OK; + } + + // We failed to detect anything + *type = UNKNOWN; + *detected_length = 0; + return C_INPUT_NOT_RECOGNIZED; } Status ParseDetectedExecutable(const void* buffer, size_t length, AssemblyProgram** output) { *output = NULL; - PEInfo* pe_info = new PEInfo(); - pe_info->Init(buffer, length); + Disassembler* disassembler = DetectDisassembler(buffer, length); - if (!pe_info->ParseHeader()) { - delete pe_info; + if (!disassembler) { return C_INPUT_NOT_RECOGNIZED; } - Disassembler* disassembler = new DisassemblerWin32X86(pe_info); AssemblyProgram* program = new AssemblyProgram(); if (!disassembler->Disassemble(program)) { delete program; delete disassembler; - delete pe_info; return C_DISASSEMBLY_FAILED; } delete disassembler; - delete pe_info; *output = program; return C_OK; } @@ -72,4 +82,34 @@ void DeleteAssemblyProgram(AssemblyProgram* program) { delete program; } +Disassembler::Disassembler(const void* start, size_t length) + : failure_reason_("uninitialized") { + + start_ = reinterpret_cast<const uint8*>(start); + length_ = length; + end_ = start_ + length_; +}; + +Disassembler::~Disassembler() {}; + +const uint8* Disassembler::OffsetToPointer(size_t offset) const { + assert(start_ + offset <= end_); + return start_ + offset; +} + +bool Disassembler::Good() { + failure_reason_ = NULL; + return true; +} + +bool Disassembler::Bad(const char* reason) { + failure_reason_ = reason; + return false; +} + +void Disassembler::ReduceLength(size_t reduced_length) { + if (reduced_length < length_) + length_ = reduced_length; +} + } // namespace courgette |