• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3  * Copyright (c) 2015 - 2017, Intel Corporation
4  *
5  * All rights reserved.
6  ***********************************************************************/
7 
8 #ifndef TSS2_ENDIAN_H
9 #define TSS2_ENDIAN_H
10 
11 #if defined(__linux__) || defined(__unix__)
12 #if defined(__FreeBSD__)
13 #include <sys/endian.h>
14 #else
15 #include <endian.h>
16 #endif
17 
18 #define HOST_TO_BE_16(value) htobe16(value)
19 #define HOST_TO_BE_32(value) htobe32(value)
20 #define HOST_TO_BE_64(value) htobe64(value)
21 #define BE_TO_HOST_16(value) be16toh(value)
22 #define BE_TO_HOST_32(value) be32toh(value)
23 #define BE_TO_HOST_64(value) be64toh(value)
24 
25 #else /* linux || unix */
26 
27 #if defined(WORDS_BIGENDIAN)
28 
29 #define HOST_TO_BE_16(value) (value)
30 #define HOST_TO_BE_32(value) (value)
31 #define HOST_TO_BE_64(value) (value)
32 #define BE_TO_HOST_16(value) (value)
33 #define BE_TO_HOST_32(value) (value)
34 #define BE_TO_HOST_64(value) (value)
35 
36 #else
37 #include <stdint.h>
38 
endian_conv_16(uint16_t value)39 static inline uint16_t endian_conv_16(uint16_t value)
40 {
41     return ((value & (0xff))      << 8) | \
42            ((value & (0xff << 8)) >> 8);
43 }
44 
endian_conv_32(uint32_t value)45 static inline uint32_t endian_conv_32(uint32_t value)
46 {
47     return ((value & (0xff))       << 24) | \
48            ((value & (0xff << 8))  << 8)  | \
49            ((value & (0xff << 16)) >> 8)  | \
50            ((value & (0xff << 24)) >> 24);
51 }
52 
endian_conv_64(uint64_t value)53 static inline uint64_t endian_conv_64(uint64_t value)
54 {
55     return ((value & (0xffULL))       << 56) | \
56            ((value & (0xffULL << 8))  << 40) | \
57            ((value & (0xffULL << 16)) << 24) | \
58            ((value & (0xffULL << 24)) << 8)  | \
59            ((value & (0xffULL << 32)) >> 8)  | \
60            ((value & (0xffULL << 40)) >> 24) | \
61            ((value & (0xffULL << 48)) >> 40) | \
62            ((value & (0xffULL << 56)) >> 56);
63 }
64 
65 #define HOST_TO_BE_16(value) endian_conv_16(value)
66 #define HOST_TO_BE_32(value) endian_conv_32(value)
67 #define HOST_TO_BE_64(value) endian_conv_64(value)
68 #define BE_TO_HOST_16(value) endian_conv_16(value)
69 #define BE_TO_HOST_32(value) endian_conv_32(value)
70 #define BE_TO_HOST_64(value) endian_conv_64(value)
71 
72 #endif /* WORDS_BIGENDIAN */
73 #endif /* linux || unix */
74 #endif /* TSS2_ENDIAN_H */
75