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"
11 #include "courgette/image_utils.h"
15 class AssemblyProgram
;
19 virtual ~Disassembler();
21 virtual ExecutableType
kind() { return EXE_UNKNOWN
; }
23 // ok() may always be called but returns 'true' only after ParseHeader
25 bool ok() const { return failure_reason_
== NULL
; }
27 // Returns 'true' if the buffer appears to be a valid executable of the
28 // expected type. It is not required that this be called before Disassemble.
29 virtual bool ParseHeader() = 0;
31 // Disassembles the item passed to the factory method into the output
32 // parameter 'program'.
33 virtual bool Disassemble(AssemblyProgram
* program
) = 0;
35 // Returns the length of the source executable. May reduce after ParseHeader.
36 size_t length() const { return length_
; }
37 const uint8
* start() const { return start_
; }
38 const uint8
* end() const { return end_
; }
40 // Returns a pointer into the memory copy of the file format.
41 // FileOffsetToPointer(0) returns a pointer to the start of the file format.
42 const uint8
* OffsetToPointer(size_t offset
) const;
45 Disassembler(const void* start
, size_t length
);
48 bool Bad(const char *reason
);
50 // Returns true if the array lies within our memory region.
51 bool IsArrayInBounds(size_t offset
, size_t elements
, size_t element_size
) {
52 return offset
<= length() && elements
<= (length() - offset
) / element_size
;
55 // Reduce the length of the image in memory. Does not actually free
56 // (or realloc) any memory. Usually only called via ParseHeader()
57 void ReduceLength(size_t reduced_length
);
60 const char* failure_reason_
;
63 // Basic information that is always valid after Construction, though
64 // ParseHeader may shorten the length if the executable is shorter than
67 size_t length_
; // In current memory.
68 const uint8
* start_
; // In current memory, base for 'file offsets'.
69 const uint8
* end_
; // In current memory.
71 DISALLOW_COPY_AND_ASSIGN(Disassembler
);
74 } // namespace courgette
76 #endif // COURGETTE_DISASSEMBLER_H_