forked from luigoalma/ctrcdnfetch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEndian.hpp
241 lines (215 loc) · 5.64 KB
/
Endian.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef __ENDIAN_HPP__
#define __ENDIAN_HPP__
#include <cstring>
#include "types.h"
namespace Endian {
static union {
u16 foo = 1;
u8 is_little;
} check;
inline u16 Be16(const u16* value) noexcept;
inline u16 Be16(u16 value) noexcept;
inline u32 Be32(const u32* value) noexcept;
inline u32 Be32(u32 value) noexcept;
inline u64 Be64(const u64* value) noexcept;
inline u64 Be64(u64 value) noexcept;
inline u16 Le16(const u16* value) noexcept;
inline u16 Le16(u16 value) noexcept;
inline u32 Le32(const u32* value) noexcept;
inline u32 Le32(u32 value) noexcept;
inline u64 Le64(const u64* value) noexcept;
inline u64 Le64(u64 value) noexcept;
inline u16 Be(const u16* value) noexcept;
inline u16 Be(u16 value) noexcept;
inline u32 Be(const u32* value) noexcept;
inline u32 Be(u32 value) noexcept;
inline u64 Be(const u64* value) noexcept;
inline u64 Be(u64 value) noexcept;
inline u16 Le(const u16* value) noexcept;
inline u16 Le(u16 value) noexcept;
inline u32 Le(const u32* value) noexcept;
inline u32 Le(u32 value) noexcept;
inline u64 Le(const u64* value) noexcept;
inline u64 Le(u64 value) noexcept;
}
u16 Endian::Be16(const u16* value) noexcept {
u16 _value;
memcpy(&_value, value, sizeof(u16));
if (check.is_little) {
#if defined __clang__ || defined __GNUC__
_value = __builtin_bswap16(_value);
#else
u16 tmp = _value;
for(int i = 0; i < sizeof(u16); i++) ((u8 *)&_value)[i] = ((u8 *)&tmp)[sizeof(u16)-1-i];
#endif
}
return _value;
}
u16 Endian::Be16(u16 value) noexcept {
if (check.is_little) {
#if defined __clang__ || defined __GNUC__
value = __builtin_bswap16(value);
#else
u16 tmp = value;
for(int i = 0; i < sizeof(u16); i++) ((u8 *)&value)[i] = ((u8 *)&tmp)[sizeof(u16)-1-i];
#endif
}
return value;
}
u32 Endian::Be32(const u32* value) noexcept {
u32 _value;
memcpy(&_value, value, sizeof(u32));
if (check.is_little) {
#if defined __clang__ || defined __GNUC__
_value = __builtin_bswap32(_value);
#else
u32 tmp = _value;
for(int i = 0; i < sizeof(u32); i++) ((u8 *)&_value)[i] = ((u8 *)&tmp)[sizeof(u32)-1-i];
#endif
}
return _value;
}
u32 Endian::Be32(u32 value) noexcept {
if (check.is_little) {
#if defined __clang__ || defined __GNUC__
value = __builtin_bswap32(value);
#else
u32 tmp = value;
for(int i = 0; i < sizeof(u32); i++) ((u8 *)&value)[i] = ((u8 *)&tmp)[sizeof(u32)-1-i];
#endif
}
return value;
}
u64 Endian::Be64(const u64* value) noexcept {
u64 _value;
memcpy(&_value, value, sizeof(u64));
if (check.is_little) {
#if defined __clang__ || defined __GNUC__
_value = __builtin_bswap64(_value);
#else
u64 tmp = _value;
for(int i = 0; i < sizeof(u64); i++) ((u8 *)&_value)[i] = ((u8 *)&tmp)[sizeof(u64)-1-i];
#endif
}
return _value;
}
u64 Endian::Be64(u64 value) noexcept {
if (check.is_little) {
#if defined __clang__ || defined __GNUC__
value = __builtin_bswap64(value);
#else
u64 tmp = value;
for(int i = 0; i < sizeof(u64); i++) ((u8 *)&value)[i] = ((u8 *)&tmp)[sizeof(u64)-1-i];
#endif
}
return value;
}
u16 Endian::Le16(const u16* value) noexcept {
u16 _value;
memcpy(&_value, value, sizeof(u16));
if (!check.is_little) {
#if defined __clang__ || defined __GNUC__
_value = __builtin_bswap16(_value);
#else
u16 tmp = _value;
for(int i = 0; i < sizeof(u16); i++) ((u8 *)&_value)[i] = ((u8 *)&tmp)[sizeof(u16)-1-i];
#endif
}
return _value;
}
u16 Endian::Le16(u16 value) noexcept {
if (!check.is_little) {
#if defined __clang__ || defined __GNUC__
value = __builtin_bswap16(value);
#else
u16 tmp = value;
for(int i = 0; i < sizeof(u16); i++) ((u8 *)&value)[i] = ((u8 *)&tmp)[sizeof(u16)-1-i];
#endif
}
return value;
}
u32 Endian::Le32(const u32* value) noexcept {
u32 _value;
memcpy(&_value, value, sizeof(u32));
if (!check.is_little) {
#if defined __clang__ || defined __GNUC__
_value = __builtin_bswap32(_value);
#else
u32 tmp = _value;
for(int i = 0; i < sizeof(u32); i++) ((u8 *)&_value)[i] = ((u8 *)&tmp)[sizeof(u32)-1-i];
#endif
}
return _value;
}
u32 Endian::Le32(u32 value) noexcept {
if (!check.is_little) {
#if defined __clang__ || defined __GNUC__
value = __builtin_bswap32(value);
#else
u32 tmp = value;
for(int i = 0; i < sizeof(u32); i++) ((u8 *)&value)[i] = ((u8 *)&tmp)[sizeof(u32)-1-i];
#endif
}
return value;
}
u64 Endian::Le64(const u64* value) noexcept {
u64 _value;
memcpy(&_value, value, sizeof(u64));
if (!check.is_little) {
#if defined __clang__ || defined __GNUC__
_value = __builtin_bswap64(_value);
#else
u64 tmp = _value;
for(int i = 0; i < sizeof(u64); i++) ((u8 *)&_value)[i] = ((u8 *)&tmp)[sizeof(u64)-1-i];
#endif
}
return _value;
}
u64 Endian::Le64(u64 value) noexcept {
if (!check.is_little) {
#if defined __clang__ || defined __GNUC__
value = __builtin_bswap64(value);
#else
u64 tmp = value;
for(int i = 0; i < sizeof(u64); i++) ((u8 *)&value)[i] = ((u8 *)&tmp)[sizeof(u64)-1-i];
#endif
}
return value;
}
u16 Endian::Be(const u16* value) noexcept {
return Be16(value);
}
u16 Endian::Be(u16 value) noexcept {
return Be16(value);
}
u32 Endian::Be(const u32* value) noexcept {
return Be32(value);
}
u32 Endian::Be(u32 value) noexcept {
return Be32(value);
}
u64 Endian::Be(const u64* value) noexcept {
return Be64(value);
}
u64 Endian::Be(u64 value) noexcept {
return Be64(value);
}
u16 Endian::Le(const u16* value) noexcept {
return Le16(value);
}
u16 Endian::Le(u16 value) noexcept {
return Le16(value);
}
u32 Endian::Le(const u32* value) noexcept {
return Le32(value);
}
u32 Endian::Le(u32 value) noexcept {
return Le32(value);
}
u64 Endian::Le(const u64* value) noexcept {
return Le64(value);
}
u64 Endian::Le(u64 value) noexcept {
return Le64(value);
}
#endif