1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/program_detector.h"
7 #include "courgette/disassembler.h"
8 #include "courgette/disassembler_elf_32_arm.h"
9 #include "courgette/disassembler_elf_32_x86.h"
10 #include "courgette/disassembler_win32_x64.h"
11 #include "courgette/disassembler_win32_x86.h"
15 std::unique_ptr<Disassembler> DetectDisassembler(const uint8_t* buffer,
17 std::unique_ptr<Disassembler> disassembler;
19 if (DisassemblerWin32X86::QuickDetect(buffer, length)) {
20 disassembler.reset(new DisassemblerWin32X86(buffer, length));
21 if (disassembler->ParseHeader())
24 if (DisassemblerWin32X64::QuickDetect(buffer, length)) {
25 disassembler.reset(new DisassemblerWin32X64(buffer, length));
26 if (disassembler->ParseHeader())
29 if (DisassemblerElf32X86::QuickDetect(buffer, length)) {
30 disassembler.reset(new DisassemblerElf32X86(buffer, length));
31 if (disassembler->ParseHeader())
34 if (DisassemblerElf32ARM::QuickDetect(buffer, length)) {
35 disassembler.reset(new DisassemblerElf32ARM(buffer, length));
36 if (disassembler->ParseHeader())
42 Status DetectExecutableType(const uint8_t* buffer,
45 size_t* detected_length) {
46 std::unique_ptr<Disassembler> disassembler(
47 DetectDisassembler(buffer, length));
49 if (!disassembler) { // We failed to detect anything.
52 return C_INPUT_NOT_RECOGNIZED;
55 *type = disassembler->kind();
56 *detected_length = disassembler->length();
60 } // namespace courgette