summaryrefslogtreecommitdiffstats
path: root/courgette/disassembler.cc
blob: edacd4b119a1255c02746c6b70ce91ad5f57eedf (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2011 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/disassembler.h"

#include <algorithm>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/logging.h"

#include "courgette/assembly_program.h"
#include "courgette/courgette.h"
#include "courgette/disassembler_win32_x86.h"
#include "courgette/encoded_program.h"

// COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently
// different target addresses are referenced.  Purely for debugging.
#define COURGETTE_HISTOGRAM_TARGETS 0

namespace courgette {

////////////////////////////////////////////////////////////////////////////////

Disassembler* DetectDisassembler(const void* buffer, size_t length) {
  Disassembler* disassembler = NULL;

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

  delete disassembler;

  return NULL;
}

Status DetectExecutableType(const void* buffer, size_t length,
                            ExecutableType* type,
                            size_t* detected_length) {

  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;

  Disassembler* disassembler = DetectDisassembler(buffer, length);

  if (!disassembler) {
    return C_INPUT_NOT_RECOGNIZED;
  }

  AssemblyProgram* program = new AssemblyProgram();

  if (!disassembler->Disassemble(program)) {
    delete program;
    delete disassembler;
    return C_DISASSEMBLY_FAILED;
  }

  delete disassembler;
  *output = program;
  return C_OK;
}

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