22 #ifndef WINPR_ENDIAN_H
23 #define WINPR_ENDIAN_H
25 #include <winpr/winpr.h>
26 #include <winpr/wtypes.h>
27 #include <winpr/platform.h>
28 #include <winpr/assert.h>
29 #include <winpr/cast.h>
31 #define WINPR_ENDIAN_CAST(t, val) WINPR_CXX_COMPAT_CAST(t, val)
38 static INLINE UINT8 winpr_Data_Get_UINT8(
const void* d)
41 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
45 static INLINE INT8 winpr_Data_Get_INT8(
const void* d)
48 const INT8* ptr = WINPR_ENDIAN_CAST(
const INT8*, d);
52 static INLINE UINT16 winpr_Data_Get_UINT16_NE(
const void* d)
54 const UINT16* ptr = WINPR_ENDIAN_CAST(
const UINT16*, d);
58 static INLINE UINT16 winpr_Data_Get_UINT16(
const void* d)
61 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
62 const size_t typesize =
sizeof(UINT16);
64 for (
size_t x = 0; x < typesize; x++)
67 v |= ptr[typesize - x - 1];
72 static INLINE UINT16 winpr_Data_Get_UINT16_BE(
const void* d)
75 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
76 const size_t typesize =
sizeof(UINT16);
78 for (
size_t x = 0; x < typesize; x++)
86 static INLINE INT16 winpr_Data_Get_INT16_NE(
const void* d)
89 const INT16* ptr = WINPR_ENDIAN_CAST(
const INT16*, d);
93 static INLINE INT16 winpr_Data_Get_INT16(
const void* d)
95 const UINT16 u16 = winpr_Data_Get_UINT16(d);
96 return WINPR_ENDIAN_CAST(INT16, u16);
99 static INLINE INT16 winpr_Data_Get_INT16_BE(
const void* d)
101 const UINT16 u16 = winpr_Data_Get_UINT16_BE(d);
102 return WINPR_ENDIAN_CAST(INT16, u16);
105 static INLINE UINT32 winpr_Data_Get_UINT32_NE(
const void* d)
108 const UINT32* ptr = WINPR_ENDIAN_CAST(
const UINT32*, d);
112 static INLINE UINT32 winpr_Data_Get_UINT32(
const void* d)
115 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
116 const size_t typesize =
sizeof(UINT32);
118 for (
size_t x = 0; x < typesize; x++)
121 v |= ptr[typesize - x - 1];
126 static INLINE UINT32 winpr_Data_Get_UINT32_BE(
const void* d)
129 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
130 const size_t typesize =
sizeof(UINT32);
132 for (
size_t x = 0; x < typesize; x++)
140 static INLINE INT32 winpr_Data_Get_INT32_NE(
const void* d)
143 const INT32* ptr = WINPR_ENDIAN_CAST(
const INT32*, d);
147 static INLINE INT32 winpr_Data_Get_INT32(
const void* d)
149 const UINT32 u32 = winpr_Data_Get_UINT32(d);
150 return WINPR_ENDIAN_CAST(INT32, u32);
153 static INLINE INT32 winpr_Data_Get_INT32_BE(
const void* d)
155 const UINT32 u32 = winpr_Data_Get_UINT32_BE(d);
156 return WINPR_ENDIAN_CAST(INT32, u32);
159 static INLINE UINT64 winpr_Data_Get_UINT64_NE(
const void* d)
162 const UINT64* ptr = WINPR_ENDIAN_CAST(
const UINT64*, d);
166 static INLINE UINT64 winpr_Data_Get_UINT64(
const void* d)
169 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
170 const size_t typesize =
sizeof(UINT64);
172 for (
size_t x = 0; x < typesize; x++)
175 v |= ptr[typesize - x - 1];
180 static INLINE UINT64 winpr_Data_Get_UINT64_BE(
const void* d)
183 const UINT8* ptr = WINPR_ENDIAN_CAST(
const UINT8*, d);
184 const size_t typesize =
sizeof(UINT64);
186 for (
size_t x = 0; x < typesize; x++)
194 static INLINE INT64 winpr_Data_Get_INT64_NE(
const void* d)
197 const INT64* b = WINPR_ENDIAN_CAST(
const INT64*, d);
201 static INLINE INT64 winpr_Data_Get_INT64(
const void* d)
203 const UINT64 u64 = winpr_Data_Get_UINT64(d);
204 return WINPR_ENDIAN_CAST(INT64, u64);
207 static INLINE INT64 winpr_Data_Get_INT64_BE(
const void* d)
209 const UINT64 u64 = winpr_Data_Get_UINT64_BE(d);
210 return WINPR_ENDIAN_CAST(INT64, u64);
213 static INLINE
void winpr_Data_Write_UINT8_NE(
void* d, UINT8 v)
216 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
220 static INLINE
void winpr_Data_Write_UINT8(
void* d, UINT8 v)
223 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
227 static INLINE
void winpr_Data_Write_UINT16_NE(
void* d, UINT16 v)
230 UINT16* b = WINPR_ENDIAN_CAST(UINT16*, d);
234 static INLINE
void winpr_Data_Write_UINT16(
void* d, UINT16 v)
237 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
239 b[1] = (v >> 8) & 0xFF;
242 static INLINE
void winpr_Data_Write_UINT16_BE(
void* d, UINT16 v)
245 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
247 b[0] = (v >> 8) & 0xFF;
250 static INLINE
void winpr_Data_Write_UINT32_NE(
void* d, UINT32 v)
253 UINT32* b = WINPR_ENDIAN_CAST(UINT32*, d);
257 static INLINE
void winpr_Data_Write_UINT32(
void* d, UINT32 v)
260 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
261 winpr_Data_Write_UINT16(b, v & 0xFFFF);
262 winpr_Data_Write_UINT16(b + 2, (v >> 16) & 0xFFFF);
265 static INLINE
void winpr_Data_Write_UINT32_BE(
void* d, UINT32 v)
268 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
269 winpr_Data_Write_UINT16_BE(b, (v >> 16) & 0xFFFF);
270 winpr_Data_Write_UINT16_BE(b + 2, v & 0xFFFF);
273 static INLINE
void winpr_Data_Write_UINT64_NE(
void* d, UINT64 v)
276 UINT64* b = WINPR_ENDIAN_CAST(UINT64*, d);
280 static INLINE
void winpr_Data_Write_UINT64(
void* d, UINT64 v)
283 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
284 winpr_Data_Write_UINT32(b, v & 0xFFFFFFFF);
285 winpr_Data_Write_UINT32(b + 4, (v >> 32) & 0xFFFFFFFF);
288 static INLINE
void winpr_Data_Write_UINT64_BE(
void* d, UINT64 v)
291 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
292 winpr_Data_Write_UINT32_BE(b, (v >> 32) & 0xFFFFFFFF);
293 winpr_Data_Write_UINT32_BE(b + 4, v & 0xFFFFFFFF);
296 static INLINE
void winpr_Data_Write_INT8_NE(
void* d, INT8 v)
299 INT8* b = WINPR_ENDIAN_CAST(INT8*, d);
303 static INLINE
void winpr_Data_Write_INT8(
void* d, INT8 v)
306 INT8* b = WINPR_ENDIAN_CAST(INT8*, d);
310 static INLINE
void winpr_Data_Write_INT16_NE(
void* d, INT16 v)
313 INT16* b = WINPR_ENDIAN_CAST(INT16*, d);
317 static INLINE
void winpr_Data_Write_INT16(
void* d, INT16 v)
320 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
322 b[1] = (v >> 8) & 0xFF;
325 static INLINE
void winpr_Data_Write_INT16_BE(
void* d, INT16 v)
328 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
330 b[0] = (v >> 8) & 0xFF;
333 static INLINE
void winpr_Data_Write_INT32_NE(
void* d, INT32 v)
336 INT32* pu = WINPR_ENDIAN_CAST(INT32*, d);
340 static INLINE
void winpr_Data_Write_INT32(
void* d, INT32 v)
343 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
344 winpr_Data_Write_UINT16(b, v & 0xFFFF);
345 winpr_Data_Write_UINT16(b + 2, (v >> 16) & 0xFFFF);
348 static INLINE
void winpr_Data_Write_INT32_BE(
void* d, INT32 v)
351 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
352 winpr_Data_Write_UINT16_BE(b, (v >> 16) & 0xFFFF);
353 winpr_Data_Write_UINT16_BE(b + 2, v & 0xFFFF);
356 static INLINE
void winpr_Data_Write_INT64_NE(
void* d, INT64 v)
359 INT64* pu = WINPR_ENDIAN_CAST(INT64*, d);
363 static INLINE
void winpr_Data_Write_INT64(
void* d, INT64 v)
366 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
367 winpr_Data_Write_UINT32(b, v & 0xFFFFFFFF);
368 winpr_Data_Write_UINT32(b + 4, (v >> 32) & 0xFFFFFFFF);
371 static INLINE
void winpr_Data_Write_INT64_BE(
void* d, INT64 v)
374 BYTE* b = WINPR_ENDIAN_CAST(BYTE*, d);
375 winpr_Data_Write_UINT32_BE(b, (v >> 32) & 0xFFFFFFFF);
376 winpr_Data_Write_UINT32_BE(b + 4, v & 0xFFFFFFFF);
379 #if defined(WINPR_DEPRECATED)
380 #define Data_Read_UINT8_NE(_d, _v) _v = winpr_Data_Get_UINT8(_d)
382 #define Data_Read_UINT8(_d, _v) _v = winpr_Data_Get_UINT8(_d)
384 #define Data_Read_UINT16_NE(_d, _v) _v = winpr_Data_Get_UINT16_NE(_d)
386 #define Data_Read_UINT16(_d, _v) _v = winpr_Data_Get_UINT16(_d)
388 #define Data_Read_UINT16_BE(_d, _v) _v = winpr_Data_Get_UINT16_BE(_d)
390 #define Data_Read_UINT32_NE(_d, _v) _v = winpr_Data_Get_UINT32_NE(_d)
392 #define Data_Read_UINT32(_d, _v) _v = winpr_Data_Get_UINT32(_d)
394 #define Data_Read_UINT32_BE(_d, _v) _v = winpr_Data_Get_UINT32_BE(_d)
396 #define Data_Read_UINT64_NE(_d, _v) _v = winpr_Data_Get_UINT64_NE(_d)
398 #define Data_Read_UINT64(_d, _v) _v = winpr_Data_Get_UINT64(_d)
400 #define Data_Read_UINT64_BE(_d, _v) _v = winpr_Data_Get_UINT64_BE(_d)
402 #define Data_Write_UINT8_NE(_d, _v) winpr_Data_Write_UINT8_NE(_d, _v)
404 #define Data_Write_UINT8(_d, _v) winpr_Data_Write_UINT8(_d, _v)
406 #define Data_Write_UINT16_NE(_d, _v) winpr_Data_Write_UINT16_NE(_d, _v)
407 #define Data_Write_UINT16(_d, _v) winpr_Data_Write_UINT16(_d, _v)
409 #define Data_Write_UINT16_BE(_d, _v) winpr_Data_Write_UINT16_BE(_d, _v)
411 #define Data_Write_UINT32_NE(_d, _v) winpr_Data_Write_UINT32_NE(_d, _v)
413 #define Data_Write_UINT32(_d, _v) winpr_Data_Write_UINT32(_d, _v)
415 #define Data_Write_UINT32_BE(_d, _v) winpr_Data_Write_UINT32_BE(_d, _v)
417 #define Data_Write_UINT64_NE(_d, _v) winpr_Data_Write_UINT64_NE(_d, _v)
419 #define Data_Write_UINT64(_d, _v) winpr_Data_Write_UINT64(_d, _v)
421 #define Data_Write_UINT64_BE(_d, _v) winpr_Data_Write_UINT64_BE(_d, _v)