Revert 221932 "Refactor KeySystems code to call a function to po..."
[chromium-blink-merge.git] / courgette / disassembler.h
blob8f6deb10c1c7d08100a4671c66e2e859447a953c
1 // Copyright (c) 2011 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 #ifndef COURGETTE_DISASSEMBLER_H_
6 #define COURGETTE_DISASSEMBLER_H_
8 #include "base/basictypes.h"
10 #include "courgette/courgette.h"
12 namespace courgette {
14 class AssemblyProgram;
16 // A Relative Virtual Address is the address in the image file after it is
17 // loaded into memory relative to the image load address.
18 typedef uint32 RVA;
20 class Disassembler {
21 public:
22 virtual ~Disassembler();
24 virtual ExecutableType kind() { return EXE_UNKNOWN; }
26 // ok() may always be called but returns 'true' only after ParseHeader
27 // succeeds.
28 bool ok() const { return failure_reason_ == NULL; }
30 // Returns 'true' if the buffer appears to be a valid executable of the
31 // expected type. It is not required that this be called before Disassemble.
32 virtual bool ParseHeader() = 0;
34 // Disassembles the item passed to the factory method into the output
35 // parameter 'program'.
36 virtual bool Disassemble(AssemblyProgram* program) = 0;
38 // Returns the length of the source executable. May reduce after ParseHeader.
39 size_t length() const { return length_; }
40 const uint8* start() const { return start_; }
41 const uint8* end() const { return end_; }
43 // Returns a pointer into the memory copy of the file format.
44 // FileOffsetToPointer(0) returns a pointer to the start of the file format.
45 const uint8* OffsetToPointer(size_t offset) const;
47 protected:
48 Disassembler(const void* start, size_t length);
50 bool Good();
51 bool Bad(const char *reason);
53 // These helper functions avoid the need for casts in the main code.
54 uint16 ReadU16(const uint8* address, size_t offset) {
55 return *reinterpret_cast<const uint16*>(address + offset);
58 uint32 ReadU32(const uint8* address, size_t offset) {
59 return *reinterpret_cast<const uint32*>(address + offset);
62 uint64 ReadU64(const uint8* address, size_t offset) {
63 return *reinterpret_cast<const uint64*>(address + offset);
66 static uint32 Read32LittleEndian(const void* address) {
67 return *reinterpret_cast<const uint32*>(address);
70 static uint16 Read16LittleEndian(const void* address) {
71 return *reinterpret_cast<const uint16*>(address);
74 // Reduce the length of the image in memory. Does not actually free
75 // (or realloc) any memory. Usually only called via ParseHeader()
76 void ReduceLength(size_t reduced_length);
78 private:
79 const char* failure_reason_;
82 // Basic information that is always valid after Construction, though
83 // ParseHeader may shorten the length if the executable is shorter than
84 // the total data.
86 size_t length_; // In current memory.
87 const uint8* start_; // In current memory, base for 'file offsets'.
88 const uint8* end_; // In current memory.
90 DISALLOW_COPY_AND_ASSIGN(Disassembler);
93 } // namespace courgette
94 #endif // COURGETTE_DISASSEMBLER_H_