OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COURGETTE_IMAGE_UTILS_H_ | 5 #ifndef COURGETTE_IMAGE_UTILS_H_ |
6 #define COURGETTE_IMAGE_UTILS_H_ | 6 #define COURGETTE_IMAGE_UTILS_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 }; | 94 }; |
95 | 95 |
96 // An interface for sequential visit of RVAs. | 96 // An interface for sequential visit of RVAs. |
97 // Use case: Translating from RVA locations to RVA targets is platform-specific, | 97 // Use case: Translating from RVA locations to RVA targets is platform-specific, |
98 // and works differently for abs32 vs. rel32. A function that sequentually | 98 // and works differently for abs32 vs. rel32. A function that sequentually |
99 // visits RVA targets only requires an RvaVisitor. The caller can provide an | 99 // visits RVA targets only requires an RvaVisitor. The caller can provide an |
100 // implementation that stores a fixed list of RVA locations, and translates each | 100 // implementation that stores a fixed list of RVA locations, and translates each |
101 // to the matching RVA target on demand without extra storage. | 101 // to the matching RVA target on demand without extra storage. |
102 class RvaVisitor { | 102 class RvaVisitor { |
103 public: | 103 public: |
| 104 virtual ~RvaVisitor() { } |
| 105 |
104 // Returns the number of remaining RVAs to visit. | 106 // Returns the number of remaining RVAs to visit. |
105 virtual size_t Remaining() const = 0; | 107 virtual size_t Remaining() const = 0; |
106 | 108 |
107 // Returns the current RVA. | 109 // Returns the current RVA. |
108 virtual RVA Get() const = 0; | 110 virtual RVA Get() const = 0; |
109 | 111 |
110 // Advances to the next RVA. | 112 // Advances to the next RVA. |
111 virtual void Next() = 0; | 113 virtual void Next() = 0; |
112 }; | 114 }; |
113 | 115 |
114 // RvaVisitor whose data are backed by std::vector<T>. Translating from T to RVA | 116 // RvaVisitor whose data are backed by std::vector<T>. Translating from T to RVA |
115 // is should be implemented in Get(). | 117 // is should be implemented in Get(). |
116 template <typename T> | 118 template <typename T> |
117 class VectorRvaVisitor : public RvaVisitor { | 119 class VectorRvaVisitor : public RvaVisitor { |
118 public: | 120 public: |
119 // Assumes |v| does not change for the lifetime of this instance. | 121 // Assumes |v| does not change for the lifetime of this instance. |
120 explicit VectorRvaVisitor(const std::vector<T>& v) | 122 explicit VectorRvaVisitor(const std::vector<T>& v) |
121 : it_(v.begin()), end_(v.end()) {} | 123 : it_(v.begin()), end_(v.end()) { } |
| 124 ~VectorRvaVisitor() override { } |
122 | 125 |
123 // RvaVisitor interfaces. | 126 // RvaVisitor interfaces. |
124 size_t Remaining() const override { return std::distance(it_, end_); } | 127 size_t Remaining() const override { return std::distance(it_, end_); } |
125 virtual RVA Get() const override = 0; | 128 virtual RVA Get() const override = 0; |
126 void Next() override { ++it_; } | 129 void Next() override { ++it_; } |
127 | 130 |
128 protected: | 131 protected: |
129 typename std::vector<T>::const_iterator it_; | 132 typename std::vector<T>::const_iterator it_; |
130 typename std::vector<T>::const_iterator end_; | 133 typename std::vector<T>::const_iterator end_; |
131 }; | 134 }; |
132 | 135 |
133 // RvaVisitor that simply stores a list of RVAs for traversal. For testing. | 136 // RvaVisitor that simply stores a list of RVAs for traversal. For testing. |
134 class TrivialRvaVisitor : public VectorRvaVisitor<RVA> { | 137 class TrivialRvaVisitor : public VectorRvaVisitor<RVA> { |
135 public: | 138 public: |
136 explicit TrivialRvaVisitor(const std::vector<RVA>& rvas) | 139 explicit TrivialRvaVisitor(const std::vector<RVA>& rvas) |
137 : VectorRvaVisitor<RVA>(rvas) {} | 140 : VectorRvaVisitor<RVA>(rvas) { } |
| 141 ~TrivialRvaVisitor() override { } |
138 | 142 |
139 // VectorRvaVisitor<RVA> interfaces. | 143 // VectorRvaVisitor<RVA> interfaces. |
140 RVA Get() const override { return *it_; } | 144 RVA Get() const override { return *it_; } |
141 }; | 145 }; |
142 | 146 |
143 // These helper functions avoid the need for casts in the main code. | 147 // These helper functions avoid the need for casts in the main code. |
144 inline uint16_t ReadU16(const uint8_t* address, size_t offset) { | 148 inline uint16_t ReadU16(const uint8_t* address, size_t offset) { |
145 return *reinterpret_cast<const uint16_t*>(address + offset); | 149 return *reinterpret_cast<const uint16_t*>(address + offset); |
146 } | 150 } |
147 | 151 |
(...skipping 13 matching lines...) Expand all Loading... |
161 return *reinterpret_cast<const uint32_t*>(address); | 165 return *reinterpret_cast<const uint32_t*>(address); |
162 } | 166 } |
163 | 167 |
164 inline uint64_t Read64LittleEndian(const void* address) { | 168 inline uint64_t Read64LittleEndian(const void* address) { |
165 return *reinterpret_cast<const uint64_t*>(address); | 169 return *reinterpret_cast<const uint64_t*>(address); |
166 } | 170 } |
167 | 171 |
168 } // namespace courgette | 172 } // namespace courgette |
169 | 173 |
170 #endif // COURGETTE_IMAGE_UTILS_H_ | 174 #endif // COURGETTE_IMAGE_UTILS_H_ |
OLD | NEW |