ROSE 0.11.145.147
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/BinaryAnalysis/RelativeVirtualAddress.h>
7#include <Rose/BitOps.h>
8#include <ROSE_DEPRECATED.h>
9
10#include <boost/numeric/conversion/cast.hpp>
11
12namespace Rose {
13namespace BinaryAnalysis {
14
16namespace ByteOrder {
17
20 // Caution: the symbols LITTLE_ENDIAN and BIG_ENDIAN are already defined on some systems, so we use other names in ROSE.
26};
27
30
32void convert(void *bytes, size_t nbytes, Endianness from, Endianness to);
33
35template<class T>
36typename std::enable_if<std::is_integral<T>::value, T>::type
37swapBytes(const T &x) {
38 return BitOps::reverseBytes(x);
39}
40
42template<class T>
43typename std::enable_if<std::is_integral<T>::value, T>::type
44leToHost(const T &x) {
46}
47
49template<class T>
50typename std::enable_if<std::is_integral<T>::value, T>::type
51beToHost(const T &x) {
52 return BitOps::fromBigEndian(x);
53}
54
56template<class T>
57typename std::enable_if<std::is_integral<T>::value, T>::type
58diskToHost(Endianness sex, const T &x) {
60}
61
69template<class Source, class Destination>
70void
71hostToLe(const Source &src, Destination *dst /*out*/,
72 typename std::enable_if<std::is_integral<Source>::value && std::is_integral<Destination>::value>::type* = nullptr) {
73 ASSERT_not_null(dst);
74 *dst = BitOps::toLittleEndian(boost::numeric_cast<Destination>(src));
75}
76
77template<class T>
78void
79hostToLe(const RelativeVirtualAddress &in, T *out) {
80 hostToLe(in.rva(), out);
81}
91template<class Source, class Destination>
92void
93hostToBe(const Source &src, Destination *dst,
94 typename std::enable_if<std::is_integral<Source>::value && std::is_integral<Destination>::value>::type* = nullptr) {
95 ASSERT_not_null(dst);
96 *dst = BitOps::toBigEndian(boost::numeric_cast<Destination>(src));
97}
98
99template<class T>
100void
102 hostToBe(in.rva(), out);
103}
111template<class Source, class Destination>
112void
113hostToDisk(Endianness sex, const Source &src, Destination *dst,
114 typename std::enable_if<std::is_integral<Source>::value && std::is_integral<Destination>::value>::type* = nullptr) {
115 ASSERT_not_null(dst);
116 *dst = ORDER_LSB == sex ?
117 BitOps::toLittleEndian(boost::numeric_cast<Destination>(src)) :
118 BitOps::toBigEndian(boost::numeric_cast<Destination>(src));
119}
120
121// [Robb Matzke 2023-06-09]: deprecated
122Endianness host_order() ROSE_DEPRECATED("use hostOrder");
123int8_t swap_bytes(int8_t n) ROSE_DEPRECATED("use swapBytes");
124uint8_t swap_bytes(uint8_t n) ROSE_DEPRECATED("use swapBytes");
125int16_t swap_bytes(int16_t n) ROSE_DEPRECATED("use swapBytes");
126uint16_t swap_bytes(uint16_t n) ROSE_DEPRECATED("use swapBytes");
127int32_t swap_bytes(int32_t n) ROSE_DEPRECATED("use swapBytes");
128uint32_t swap_bytes(uint32_t n) ROSE_DEPRECATED("use swapBytes");
129int64_t swap_bytes(int64_t n) ROSE_DEPRECATED("use swapBytes");
130uint64_t swap_bytes(uint64_t n) ROSE_DEPRECATED("use swapBytes");
131int8_t le_to_host(int8_t n) ROSE_DEPRECATED("use leToHost");
132uint8_t le_to_host(uint8_t n) ROSE_DEPRECATED("use leToHost");
133int16_t le_to_host(int16_t n) ROSE_DEPRECATED("use leToHost");
134uint16_t le_to_host(uint16_t n) ROSE_DEPRECATED("use leToHost");
135int32_t le_to_host(int32_t n) ROSE_DEPRECATED("use leToHost");
136uint32_t le_to_host(uint32_t n) ROSE_DEPRECATED("use leToHost");
137int64_t le_to_host(int64_t n) ROSE_DEPRECATED("use leToHost");
138uint64_t le_to_host(uint64_t n) ROSE_DEPRECATED("use leToHost");
139void host_to_le(unsigned h, uint8_t *n) ROSE_DEPRECATED("use hostToLe");
140void host_to_le(unsigned h, uint16_t *n) ROSE_DEPRECATED("use hostToLe");
141void host_to_le(unsigned h, uint32_t *n) ROSE_DEPRECATED("use hostToLe");
142void host_to_le(rose_addr_t h, uint64_t *n) ROSE_DEPRECATED("use hostToLe");
143void host_to_le(int h, int8_t *n) ROSE_DEPRECATED("use hostToLe");
144void host_to_le(int h, int16_t *n) ROSE_DEPRECATED("use hostToLe");
145void host_to_le(int h, int32_t *n) ROSE_DEPRECATED("use hostToLe");
146void host_to_le(int64_t h, int64_t *n) ROSE_DEPRECATED("use hostToLe");
147void host_to_le(RelativeVirtualAddress h, uint32_t *n) ROSE_DEPRECATED("use hostToLe");
148void host_to_le(RelativeVirtualAddress h, uint64_t *n) ROSE_DEPRECATED("use hostToLe");
149uint8_t be_to_host(uint8_t n) ROSE_DEPRECATED("use beToHost");
150uint16_t be_to_host(uint16_t n) ROSE_DEPRECATED("use beToHost");
151uint32_t be_to_host(uint32_t n) ROSE_DEPRECATED("use beToHost");
152uint64_t be_to_host(uint64_t n) ROSE_DEPRECATED("use beToHost");
153int8_t be_to_host(int8_t n) ROSE_DEPRECATED("use beToHost");
154int16_t be_to_host(int16_t n) ROSE_DEPRECATED("use beToHost");
155int32_t be_to_host(int32_t n) ROSE_DEPRECATED("use beToHost");
156int64_t be_to_host(int64_t n) ROSE_DEPRECATED("use beToHost");
157void host_to_be(unsigned h, uint8_t *n) ROSE_DEPRECATED("use hostToBe");
158void host_to_be(unsigned h, uint16_t *n) ROSE_DEPRECATED("use hostToBe");
159void host_to_be(unsigned h, uint32_t *n) ROSE_DEPRECATED("use hostToBe");
160void host_to_be(rose_addr_t h, uint64_t *n) ROSE_DEPRECATED("use hostToBe");
161void host_to_be(int h, int8_t *n) ROSE_DEPRECATED("use hostToBe");
162void host_to_be(int h, int16_t *n) ROSE_DEPRECATED("use hostToBe");
163void host_to_be(int h, int32_t *n) ROSE_DEPRECATED("use hostToBe");
164void host_to_be(int64_t h, int64_t *n) ROSE_DEPRECATED("use hostToBe");
165void host_to_be(RelativeVirtualAddress h, uint32_t *n) ROSE_DEPRECATED("use hostToBe");
166void host_to_be(RelativeVirtualAddress h, uint64_t *n) ROSE_DEPRECATED("use hostToBe");
167uint8_t disk_to_host(Endianness sex, uint8_t n) ROSE_DEPRECATED("use diskToHost");
168uint16_t disk_to_host(Endianness sex, uint16_t n) ROSE_DEPRECATED("use diskToHost");
169uint32_t disk_to_host(Endianness sex, uint32_t n) ROSE_DEPRECATED("use diskToHost");
170uint64_t disk_to_host(Endianness sex, uint64_t n) ROSE_DEPRECATED("use diskToHost");
171int8_t disk_to_host(Endianness sex, int8_t n) ROSE_DEPRECATED("use diskToHost");
172int16_t disk_to_host(Endianness sex, int16_t n) ROSE_DEPRECATED("use diskToHost");
173int32_t disk_to_host(Endianness sex, int32_t n) ROSE_DEPRECATED("use diskToHost");
174int64_t disk_to_host(Endianness sex, int64_t n) ROSE_DEPRECATED("use diskToHost");
175void host_to_disk(Endianness sex, unsigned h, uint8_t *np) ROSE_DEPRECATED("use hostToDisk");
176void host_to_disk(Endianness sex, unsigned h, uint16_t *np) ROSE_DEPRECATED("use hostToDisk");
177void host_to_disk(Endianness sex, unsigned h, uint32_t *np) ROSE_DEPRECATED("use hostToDisk");
178void host_to_disk(Endianness sex, rose_addr_t h, uint64_t *np) ROSE_DEPRECATED("use hostToDisk");
179void host_to_disk(Endianness sex, RelativeVirtualAddress h, uint64_t *np) ROSE_DEPRECATED("use hostToDisk");
180void host_to_disk(Endianness sex, int h, int8_t *np) ROSE_DEPRECATED("use hostToDisk");
181void host_to_disk(Endianness sex, int h, int16_t *np) ROSE_DEPRECATED("use hostToDisk");
182void host_to_disk(Endianness sex, int h, int32_t *np) ROSE_DEPRECATED("use hostToDisk");
183void host_to_disk(Endianness sex, int64_t h, int64_t *np) ROSE_DEPRECATED("use hostToDisk");
184
185} // namespace
186} // namespace
187} // namespace
188
189#endif
190#endif
Optionally bound relative virtual address.
rose_addr_t rva() const
Returns the offset.
std::enable_if< std::is_integral< T >::value, T >::type diskToHost(Endianness sex, const T &x)
Convert specified order to host order.
Definition ByteOrder.h:58
void hostToBe(const Source &src, Destination *dst, typename std::enable_if< std::is_integral< Source >::value &&std::is_integral< Destination >::value >::type *=nullptr)
Convert host order to big-endian.
Definition ByteOrder.h:93
void hostToLe(const Source &src, Destination *dst, typename std::enable_if< std::is_integral< Source >::value &&std::is_integral< Destination >::value >::type *=nullptr)
Convert host order to little-endian.
Definition ByteOrder.h:71
@ ORDER_MSB
Most significant byte first, i.e., big-endian.
Definition ByteOrder.h:23
@ ORDER_UNSPECIFIED
Endianness is unspecified and unknown.
Definition ByteOrder.h:21
@ ORDER_LSB
Least significant byte first, i.e., little-endian.
Definition ByteOrder.h:22
std::enable_if< std::is_integral< T >::value, T >::type beToHost(const T &x)
Convert a big-endian integer to host order.
Definition ByteOrder.h:51
void convert(void *bytes, size_t nbytes, Endianness from, Endianness to)
Convert data from one byte order to another in place.
std::enable_if< std::is_integral< T >::value, T >::type leToHost(const T &x)
Convert a little-endian integer to host order.
Definition ByteOrder.h:44
Endianness hostOrder()
Byte order of host machine.
std::enable_if< std::is_integral< T >::value, T >::type swapBytes(const T &x)
Reverse bytes.
Definition ByteOrder.h:37
void hostToDisk(Endianness sex, const Source &src, Destination *dst, typename std::enable_if< std::is_integral< Source >::value &&std::is_integral< Destination >::value >::type *=nullptr)
Convert host value to specified endianness.
Definition ByteOrder.h:113
std::enable_if< std::is_integral< T >::value, T >::type reverseBytes(const T &x)
Reverse the bytes.
Definition BitOps.h:481
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:524
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:544
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:554
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:534
The ROSE library.