1 : // Copyright 2011 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/core/address.h"
16 :
17 : #include <iostream>
18 : #include "base/strings/stringprintf.h"
19 :
20 : namespace core {
21 :
22 : static_assert(sizeof(RelativeAddress) == sizeof(uint32_t),
23 : "RelativeAddress has the wrong size.");
24 : static_assert(sizeof(AbsoluteAddress) == sizeof(uint32_t),
25 : "AbsoluteAddress has the wrong size.");
26 : static_assert(sizeof(FileOffsetAddress) == sizeof(uint32_t),
27 : "FileOffsetAddress has the wrong size.");
28 :
29 : namespace detail {
30 :
31 : template <>
32 : const AddressImpl<kRelativeAddressType>
33 E : AddressImpl<kRelativeAddressType>::kInvalidAddress(~0U);
34 :
35 : template <>
36 : const AddressImpl<kAbsoluteAddressType>
37 E : AddressImpl<kAbsoluteAddressType>::kInvalidAddress(~0U);
38 :
39 : template <>
40 : const AddressImpl<kFileOffsetAddressType>
41 E : AddressImpl<kFileOffsetAddressType>::kInvalidAddress(~0U);
42 :
43 : std::ostream& operator<<(std::ostream& str,
44 E : const AddressImpl<kRelativeAddressType>& addr) {
45 E : return str << base::StringPrintf("Relative(0x%08X)", addr.value_);
46 E : }
47 :
48 : std::ostream& operator<<(std::ostream& str,
49 E : const AddressImpl<kAbsoluteAddressType>& addr) {
50 E : return str << base::StringPrintf("Absolute(0x%08X)", addr.value_);
51 E : }
52 :
53 : std::ostream& operator<<(std::ostream& str,
54 E : const AddressImpl<kFileOffsetAddressType>& addr) {
55 E : return str << base::StringPrintf("FileOffset(0x%08X)", addr.value_);
56 E : }
57 :
58 : } // namespace detail
59 :
60 : namespace {
61 :
62 E : int Compare(const AddressVariant& av1, const AddressVariant& av2) {
63 E : if (av1.type() < av2.type())
64 E : return -1;
65 E : if (av1.type() > av2.type())
66 E : return 1;
67 E : if (av1.value() < av2.value())
68 E : return -1;
69 E : if (av1.value() > av2.value())
70 E : return 1;
71 E : return 0;
72 E : }
73 :
74 : } // namespace
75 :
76 E : AddressVariant& AddressVariant::operator=(const AddressVariant& other) {
77 E : type_ = other.type_;
78 E : value_ = other.value_;
79 E : return *this;
80 E : }
81 :
82 E : bool AddressVariant::operator<(const AddressVariant& other) const {
83 E : return Compare(*this, other) < 0;
84 E : }
85 :
86 E : bool AddressVariant::operator<=(const AddressVariant& other) const {
87 E : return Compare(*this, other) <= 0;
88 E : }
89 :
90 E : bool AddressVariant::operator>(const AddressVariant& other) const {
91 E : return Compare(*this, other) > 0;
92 E : }
93 :
94 E : bool AddressVariant::operator>=(const AddressVariant& other) const {
95 E : return Compare(*this, other) >= 0;
96 E : }
97 :
98 E : bool AddressVariant::operator==(const AddressVariant& other) const {
99 E : return Compare(*this, other) == 0;
100 E : }
101 :
102 E : bool AddressVariant::operator!=(const AddressVariant& other) const {
103 E : return Compare(*this, other) != 0;
104 E : }
105 :
106 : bool AddressVariant::Save(OutArchive* out_archive) const {
107 : uint8_t type = static_cast<uint8_t>(type_);
108 : return out_archive->Save(type) && out_archive->Save(value_);
109 : }
110 :
111 : bool AddressVariant::Load(InArchive* in_archive) {
112 : uint8_t type = 0;
113 : if (!in_archive->Load(&type))
114 : return false;
115 : type_ = static_cast<AddressType>(type);
116 : return in_archive->Load(&value_);
117 : }
118 :
119 i : std::ostream& operator<<(std::ostream& str, const AddressVariant& addr) {
120 : static const char* kTypes[] = {"Relative", "Absolute", "FileOffset"};
121 i : DCHECK_GT(arraysize(kTypes), addr.type_);
122 : return str << base::StringPrintf("AddressVariant(%s(0x%08X))",
123 i : kTypes[addr.type_], addr.value_);
124 i : }
125 :
126 : } // namespace core
|