Enable dev build with the latest repo
[platform/framework/web/chromium-efl.git] / courgette / program_detector.cc
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.
4
5 #include "courgette/program_detector.h"
6
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"
12
13 namespace courgette {
14
15 std::unique_ptr<Disassembler> DetectDisassembler(const uint8_t* buffer,
16                                                  size_t length) {
17   std::unique_ptr<Disassembler> disassembler;
18
19   if (DisassemblerWin32X86::QuickDetect(buffer, length)) {
20     disassembler.reset(new DisassemblerWin32X86(buffer, length));
21     if (disassembler->ParseHeader())
22       return disassembler;
23   }
24   if (DisassemblerWin32X64::QuickDetect(buffer, length)) {
25     disassembler.reset(new DisassemblerWin32X64(buffer, length));
26     if (disassembler->ParseHeader())
27       return disassembler;
28   }
29   if (DisassemblerElf32X86::QuickDetect(buffer, length)) {
30     disassembler.reset(new DisassemblerElf32X86(buffer, length));
31     if (disassembler->ParseHeader())
32       return disassembler;
33   }
34   if (DisassemblerElf32ARM::QuickDetect(buffer, length)) {
35     disassembler.reset(new DisassemblerElf32ARM(buffer, length));
36     if (disassembler->ParseHeader())
37       return disassembler;
38   }
39   return nullptr;
40 }
41
42 Status DetectExecutableType(const uint8_t* buffer,
43                             size_t length,
44                             ExecutableType* type,
45                             size_t* detected_length) {
46   std::unique_ptr<Disassembler> disassembler(
47       DetectDisassembler(buffer, length));
48
49   if (!disassembler) {  // We failed to detect anything.
50     *type = EXE_UNKNOWN;
51     *detected_length = 0;
52     return C_INPUT_NOT_RECOGNIZED;
53   }
54
55   *type = disassembler->kind();
56   *detected_length = disassembler->length();
57   return C_OK;
58 }
59
60 }  // namespace courgette