summaryrefslogtreecommitdiffstats
path: root/courgette/program_detector.cc
blob: e0fe3e6508b7dcbc81a7960b73b4540c85cf3f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "courgette/program_detector.h"

#include <utility>

#include "courgette/assembly_program.h"
#include "courgette/disassembler.h"
#include "courgette/disassembler_elf_32_arm.h"
#include "courgette/disassembler_elf_32_x86.h"
#include "courgette/disassembler_win32_x64.h"
#include "courgette/disassembler_win32_x86.h"

namespace courgette {

namespace {

// Returns a new instance of Disassembler subclass if binary data given in
// |buffer| and |length| matches a known binary format, otherwise null.
scoped_ptr<Disassembler> DetectDisassembler(const void* buffer, size_t length) {
  scoped_ptr<Disassembler> disassembler;

  disassembler.reset(new DisassemblerWin32X86(buffer, length));
  if (disassembler->ParseHeader())
    return disassembler;

  disassembler.reset(new DisassemblerWin32X64(buffer, length));
  if (disassembler->ParseHeader())
    return disassembler;

  disassembler.reset(new DisassemblerElf32X86(buffer, length));
  if (disassembler->ParseHeader())
    return disassembler;

  disassembler.reset(new DisassemblerElf32ARM(buffer, length));
  if (disassembler->ParseHeader())
    return disassembler;

  return nullptr;
}

}  // namespace

Status DetectExecutableType(const void* buffer,
                            size_t length,
                            ExecutableType* type,
                            size_t* detected_length) {
  scoped_ptr<Disassembler> disassembler(DetectDisassembler(buffer, length));

   if (!disassembler) {  // We failed to detect anything.
    *type = EXE_UNKNOWN;
    *detected_length = 0;
    return C_INPUT_NOT_RECOGNIZED;
  }

  *type = disassembler->kind();
  *detected_length = disassembler->length();
  return C_OK;
}

Status ParseDetectedExecutable(const void* buffer,
                               size_t length,
                               scoped_ptr<AssemblyProgram>* output) {
  output->reset();

  scoped_ptr<Disassembler> disassembler(DetectDisassembler(buffer, length));
  if (!disassembler)
    return C_INPUT_NOT_RECOGNIZED;

  scoped_ptr<AssemblyProgram> program(
      new AssemblyProgram(disassembler->kind()));

  if (!disassembler->Disassemble(program.get()))
    return C_DISASSEMBLY_FAILED;

  if (!program->TrimLabels())
    return C_TRIM_FAILED;

  *output = std::move(program);
  return C_OK;
}

}  // namespace courgette