[go: nahoru, domu]

blob: 0406feab41b0524cac589dd3da95c9084f9974d7 [file] [log] [blame]
Peter Huewe9f7cc082019-05-02 18:16:28 +02001/* SPDX-License-Identifier: BSD-2-Clause */
Tadeusz Struk8203a372017-06-26 15:15:18 -07002/***********************************************************************;
3 * Copyright (c) 2015 - 2017, Intel Corporation
4 *
5 * All rights reserved.
Tadeusz Struk8203a372017-06-26 15:15:18 -07006 ***********************************************************************/
7
8#ifndef TSS2_ENDIAN_H
9#define TSS2_ENDIAN_H
10
11#if defined(__linux__) || defined(__unix__)
Tadeusz Strukb20860b2019-08-20 14:46:04 -070012#if defined(__FreeBSD__)
13#include <sys/endian.h>
14#else
Tadeusz Struk8203a372017-06-26 15:15:18 -070015#include <endian.h>
Tadeusz Strukb20860b2019-08-20 14:46:04 -070016#endif
Tadeusz Struk8203a372017-06-26 15:15:18 -070017
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
David R. Bild69babb72017-11-28 11:10:01 -060037#include <stdint.h>
Tadeusz Struk8203a372017-06-26 15:15:18 -070038
David R. Bild69babb72017-11-28 11:10:01 -060039static inline uint16_t endian_conv_16(uint16_t value)
Tadeusz Struk8203a372017-06-26 15:15:18 -070040{
41 return ((value & (0xff)) << 8) | \
42 ((value & (0xff << 8)) >> 8);
43}
44
45static 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
53static 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 */