ROSE  0.11.145.0
ByteOrder.h
1 #ifndef ROSE_BinaryAnalysis_ByteOrder_H
2 #define ROSE_BinaryAnalysis_ByteOrder_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BitOps.h>
7 #include <boost/numeric/conversion/cast.hpp>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 namespace ByteOrder {
12 
13 // Caution: the symbols LITTLE_ENDIAN and BIG_ENDIAN are already defined on some systems, so we use other names in ROSE.
14 enum Endianness {
15  ORDER_UNSPECIFIED=0,
16  ORDER_LSB,
17  ORDER_MSB
18 };
19 
21 Endianness hostOrder();
22 
24 void convert(void *bytes, size_t nbytes, Endianness from, Endianness to);
25 
27 template<class T>
28 typename std::enable_if<std::is_integral<T>::value, T>::type
29 swapBytes(const T &x) {
30  return BitOps::reverseBytes(x);
31 }
32 
34 template<class T>
35 typename std::enable_if<std::is_integral<T>::value, T>::type
36 leToHost(const T &x) {
37  return BitOps::fromLittleEndian(x);
38 }
39 
41 template<class T>
42 typename std::enable_if<std::is_integral<T>::value, T>::type
43 beToHost(const T &x) {
44  return BitOps::fromBigEndian(x);
45 }
46 
48 template<class T>
49 typename std::enable_if<std::is_integral<T>::value, T>::type
50 diskToHost(Endianness sex, const T &x) {
51  return ORDER_LSB == sex ? BitOps::fromLittleEndian(x) : BitOps::fromBigEndian(x);
52 }
53 
61 template<class Source, class Destination>
62 void
63 hostToLe(const Source &src, Destination *dst /*out*/,
64  typename std::enable_if<std::is_integral<Source>::value && std::is_integral<Destination>::value>::type* = nullptr) {
65  ASSERT_not_null(dst);
66  *dst = BitOps::toLittleEndian(boost::numeric_cast<Destination>(src));
67 }
68 
69 template<class T>
70 void
71 hostToLe(const rose_rva_t &in, T *out) {
72  hostToLe(in.get_rva(), out);
73 }
83 template<class Source, class Destination>
84 void
85 hostToBe(const Source &src, Destination *dst,
86  typename std::enable_if<std::is_integral<Source>::value && std::is_integral<Destination>::value>::type* = nullptr) {
87  ASSERT_not_null(dst);
88  *dst = BitOps::toBigEndian(boost::numeric_cast<Destination>(src));
89 }
90 
91 template<class T>
92 void
93 hostToBe(const rose_rva_t &in, T *out) {
94  hostToBe(in.get_rva(), out);
95 }
103 template<class Source, class Destination>
104 void
105 hostToDisk(Endianness sex, const Source &src, Destination *dst,
106  typename std::enable_if<std::is_integral<Source>::value && std::is_integral<Destination>::value>::type* = nullptr) {
107  ASSERT_not_null(dst);
108  *dst = ORDER_LSB == sex ?
109  BitOps::toLittleEndian(boost::numeric_cast<Destination>(src)) :
110  BitOps::toBigEndian(boost::numeric_cast<Destination>(src));
111 }
112 
113 // [Robb Matzke 2023-06-09]: deprecated
114 Endianness host_order() ROSE_DEPRECATED("use hostOrder");
115 int8_t swap_bytes(int8_t n) ROSE_DEPRECATED("use swapBytes");
116 uint8_t swap_bytes(uint8_t n) ROSE_DEPRECATED("use swapBytes");
117 int16_t swap_bytes(int16_t n) ROSE_DEPRECATED("use swapBytes");
118 uint16_t swap_bytes(uint16_t n) ROSE_DEPRECATED("use swapBytes");
119 int32_t swap_bytes(int32_t n) ROSE_DEPRECATED("use swapBytes");
120 uint32_t swap_bytes(uint32_t n) ROSE_DEPRECATED("use swapBytes");
121 int64_t swap_bytes(int64_t n) ROSE_DEPRECATED("use swapBytes");
122 uint64_t swap_bytes(uint64_t n) ROSE_DEPRECATED("use swapBytes");
123 int8_t le_to_host(int8_t n) ROSE_DEPRECATED("use leToHost");
124 uint8_t le_to_host(uint8_t n) ROSE_DEPRECATED("use leToHost");
125 int16_t le_to_host(int16_t n) ROSE_DEPRECATED("use leToHost");
126 uint16_t le_to_host(uint16_t n) ROSE_DEPRECATED("use leToHost");
127 int32_t le_to_host(int32_t n) ROSE_DEPRECATED("use leToHost");
128 uint32_t le_to_host(uint32_t n) ROSE_DEPRECATED("use leToHost");
129 int64_t le_to_host(int64_t n) ROSE_DEPRECATED("use leToHost");
130 uint64_t le_to_host(uint64_t n) ROSE_DEPRECATED("use leToHost");
131 void host_to_le(unsigned h, uint8_t *n) ROSE_DEPRECATED("use hostToLe");
132 void host_to_le(unsigned h, uint16_t *n) ROSE_DEPRECATED("use hostToLe");
133 void host_to_le(unsigned h, uint32_t *n) ROSE_DEPRECATED("use hostToLe");
134 void host_to_le(rose_addr_t h, uint64_t *n) ROSE_DEPRECATED("use hostToLe");
135 void host_to_le(int h, int8_t *n) ROSE_DEPRECATED("use hostToLe");
136 void host_to_le(int h, int16_t *n) ROSE_DEPRECATED("use hostToLe");
137 void host_to_le(int h, int32_t *n) ROSE_DEPRECATED("use hostToLe");
138 void host_to_le(int64_t h, int64_t *n) ROSE_DEPRECATED("use hostToLe");
139 void host_to_le(rose_rva_t h, uint32_t *n) ROSE_DEPRECATED("use hostToLe");
140 void host_to_le(rose_rva_t h, uint64_t *n) ROSE_DEPRECATED("use hostToLe");
141 uint8_t be_to_host(uint8_t n) ROSE_DEPRECATED("use beToHost");
142 uint16_t be_to_host(uint16_t n) ROSE_DEPRECATED("use beToHost");
143 uint32_t be_to_host(uint32_t n) ROSE_DEPRECATED("use beToHost");
144 uint64_t be_to_host(uint64_t n) ROSE_DEPRECATED("use beToHost");
145 int8_t be_to_host(int8_t n) ROSE_DEPRECATED("use beToHost");
146 int16_t be_to_host(int16_t n) ROSE_DEPRECATED("use beToHost");
147 int32_t be_to_host(int32_t n) ROSE_DEPRECATED("use beToHost");
148 int64_t be_to_host(int64_t n) ROSE_DEPRECATED("use beToHost");
149 void host_to_be(unsigned h, uint8_t *n) ROSE_DEPRECATED("use hostToBe");
150 void host_to_be(unsigned h, uint16_t *n) ROSE_DEPRECATED("use hostToBe");
151 void host_to_be(unsigned h, uint32_t *n) ROSE_DEPRECATED("use hostToBe");
152 void host_to_be(rose_addr_t h, uint64_t *n) ROSE_DEPRECATED("use hostToBe");
153 void host_to_be(int h, int8_t *n) ROSE_DEPRECATED("use hostToBe");
154 void host_to_be(int h, int16_t *n) ROSE_DEPRECATED("use hostToBe");
155 void host_to_be(int h, int32_t *n) ROSE_DEPRECATED("use hostToBe");
156 void host_to_be(int64_t h, int64_t *n) ROSE_DEPRECATED("use hostToBe");
157 void host_to_be(rose_rva_t h, uint32_t *n) ROSE_DEPRECATED("use hostToBe");
158 void host_to_be(rose_rva_t h, uint64_t *n) ROSE_DEPRECATED("use hostToBe");
159 uint8_t disk_to_host(Endianness sex, uint8_t n) ROSE_DEPRECATED("use diskToHost");
160 uint16_t disk_to_host(Endianness sex, uint16_t n) ROSE_DEPRECATED("use diskToHost");
161 uint32_t disk_to_host(Endianness sex, uint32_t n) ROSE_DEPRECATED("use diskToHost");
162 uint64_t disk_to_host(Endianness sex, uint64_t n) ROSE_DEPRECATED("use diskToHost");
163 int8_t disk_to_host(Endianness sex, int8_t n) ROSE_DEPRECATED("use diskToHost");
164 int16_t disk_to_host(Endianness sex, int16_t n) ROSE_DEPRECATED("use diskToHost");
165 int32_t disk_to_host(Endianness sex, int32_t n) ROSE_DEPRECATED("use diskToHost");
166 int64_t disk_to_host(Endianness sex, int64_t n) ROSE_DEPRECATED("use diskToHost");
167 void host_to_disk(Endianness sex, unsigned h, uint8_t *np) ROSE_DEPRECATED("use hostToDisk");
168 void host_to_disk(Endianness sex, unsigned h, uint16_t *np) ROSE_DEPRECATED("use hostToDisk");
169 void host_to_disk(Endianness sex, unsigned h, uint32_t *np) ROSE_DEPRECATED("use hostToDisk");
170 void host_to_disk(Endianness sex, rose_addr_t h, uint64_t *np) ROSE_DEPRECATED("use hostToDisk");
171 void host_to_disk(Endianness sex, rose_rva_t h, uint64_t *np) ROSE_DEPRECATED("use hostToDisk");
172 void host_to_disk(Endianness sex, int h, int8_t *np) ROSE_DEPRECATED("use hostToDisk");
173 void host_to_disk(Endianness sex, int h, int16_t *np) ROSE_DEPRECATED("use hostToDisk");
174 void host_to_disk(Endianness sex, int h, int32_t *np) ROSE_DEPRECATED("use hostToDisk");
175 void host_to_disk(Endianness sex, int64_t h, int64_t *np) ROSE_DEPRECATED("use hostToDisk");
176 
177 } // namespace
178 } // namespace
179 } // namespace
180 
181 #endif
182 #endif
std::enable_if< std::is_integral< T >::value, T >::type fromBigEndian(const T x)
Convert integral value from big endian to host order.
Definition: BitOps.h:464
std::enable_if< std::is_integral< T >::value, T >::type fromLittleEndian(const T x)
Convert integral value from little endian to host order.
Definition: BitOps.h:472
rose_addr_t get_rva() const
Returns the numeric RVA.
Definition: Rva.C:86
UnsignedTarget convert(UnsignedSource x, bool b=false)
Extend or truncate a value.
Definition: BitOps.h:249
std::enable_if< std::is_integral< T >::value, T >::type toLittleEndian(const T x)
Convert integral value from host order to little endian.
Definition: BitOps.h:456
Main namespace for the ROSE library.
A relative virtual address optionally associated with a SgAsmSection.
Definition: Cxx_Grammar.h:8423
std::enable_if< std::is_integral< T >::value, T >::type toBigEndian(const T x)
Convert integral value from host order to big endian.
Definition: BitOps.h:448
std::enable_if< std::is_integral< T >::value, T >::type reverseBytes(const T &x)
Reverse the bytes.
Definition: BitOps.h:411