WAND Trace processing  4.0.5
lt_bswap.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4  * All rights reserved.
5  *
6  * This file is part of libtrace.
7  *
8  * This code has been developed by the University of Waikato WAND
9  * research group. For further information please see http://www.wand.net.nz/
10  *
11  * libtrace is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * libtrace is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  *
24  *
25  */
26 #include <arpa/inet.h>
27 #include <inttypes.h>
39 #ifndef LT_BYTESWAP_H_
40 #define LT_BYTESWAP_H_
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
51 uint64_t byteswap64(uint64_t num);
52 
59 uint32_t byteswap32(uint32_t num);
60 
67 uint16_t byteswap16(uint16_t num);
68 
69 
70 #if __BYTE_ORDER == __BIG_ENDIAN
71 #define bswap_host_to_be64(num) ((uint64_t)(num))
72 #define bswap_host_to_le64(num) byteswap64(num)
73 #define bswap_host_to_be32(num) ((uint32_t)(num))
74 #define bswap_host_to_le32(num) byteswap32(num)
75 #define bswap_host_to_be16(num) ((uint16_t)(num))
76 #define bswap_host_to_le16(num) byteswap16(num)
77 
78 #define bswap_be_to_host64(num) ((uint64_t)(num))
79 #define bswap_le_to_host64(num) byteswap64(num)
80 #define bswap_be_to_host32(num) ((uint32_t)(num))
81 #define bswap_le_to_host32(num) byteswap32(num)
82 #define bswap_be_to_host16(num) ((uint16_t)(num))
83 #define bswap_le_to_host16(num) byteswap16(num)
84 
85 /* We use ntoh*() here, because the compiler may
86  * attempt to optimise it
87  */
88 #elif __BYTE_ORDER == __LITTLE_ENDIAN
89 #define bswap_host_to_be64(num) (byteswap64(num))
90 #define bswap_host_to_le64(num) ((uint64_t)(num))
91 #define bswap_host_to_be32(num) (htonl(num))
92 #define bswap_host_to_le32(num) ((uint32_t)(num))
93 #define bswap_host_to_be16(num) (htons(num))
94 #define bswap_host_to_le16(num) ((uint16_t)(num))
95 
96 #define bswap_be_to_host64(num) (byteswap64(num))
97 #define bswap_le_to_host64(num) ((uint64_t)(num))
98 #define bswap_be_to_host32(num) (ntohl(num))
99 #define bswap_le_to_host32(num) ((uint32_t)(num))
100 #define bswap_be_to_host16(num) (ntohs(num))
101 #define bswap_le_to_host16(num) ((uint16_t)(num))
102 
103 #else
104 #error "Unknown byte order"
105 #endif
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
uint32_t byteswap32(uint32_t num)
Byteswaps a 32-bit value.
Definition: byteswap.c:35
uint64_t byteswap64(uint64_t num)
Byteswaps a 64-bit value.
Definition: byteswap.c:29
uint16_t byteswap16(uint16_t num)
Byteswaps a 16-bit value.
Definition: byteswap.c:43