1 /*
2  * This file is part of libtrace
3  *
4  * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5  * New Zealand.
6  *
7  * Authors: Daniel Lawson
8  *          Perry Lorier
9  *          Shane Alcock
10  *
11  * All rights reserved.
12  *
13  * This code has been developed by the University of Waikato WAND
14  * research group. For further information please see http://www.wand.net.nz/
15  *
16  * libtrace is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * libtrace is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with libtrace; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *
30  * $Id: libtrace_int.h 1848 2013-08-02 06:19:52Z rjs51 $
31  *
32  */
33 
34 #include <arpa/inet.h>
35 #include <inttypes.h>
36 /** @file
37  *
38  * @brief Header file containing definitions of functions and macros that deal
39  * with byteswapping within libtrace and libpacketdump.
40  *
41  * @author Perry Lorier
42  * @author Shane Alcock
43  *
44  * @version $Id$
45  */
46 
47 #ifndef LT_BYTESWAP_H_
48 #define LT_BYTESWAP_H_
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /** Byteswaps a 64-bit value.
54  *
55  * @param num           The value to be byteswapped.
56  * @return The byteswapped 64-bit number
57  *
58  */
59 uint64_t byteswap64(uint64_t num);
60 
61 /** Byteswaps a 32-bit value.
62  *
63  * @param num           The value to be byteswapped.
64  * @return The byteswapped 32-bit number
65  *
66  */
67 uint32_t byteswap32(uint32_t num);
68 
69 /** Byteswaps a 16-bit value.
70  *
71  * @param num           The value to be byteswapped.
72  * @return The byteswapped 16-bit number
73  *
74  */
75 uint16_t byteswap16(uint16_t num);
76 
77 
78 #if __BYTE_ORDER == __BIG_ENDIAN
79 #define bswap_host_to_be64(num) ((uint64_t)(num))
80 #define bswap_host_to_le64(num) byteswap64(num)
81 #define bswap_host_to_be32(num) ((uint32_t)(num))
82 #define bswap_host_to_le32(num) byteswap32(num)
83 #define bswap_host_to_be16(num) ((uint16_t)(num))
84 #define bswap_host_to_le16(num) byteswap16(num)
85 
86 #define bswap_be_to_host64(num) ((uint64_t)(num))
87 #define bswap_le_to_host64(num) byteswap64(num)
88 #define bswap_be_to_host32(num) ((uint32_t)(num))
89 #define bswap_le_to_host32(num) byteswap32(num)
90 #define bswap_be_to_host16(num) ((uint16_t)(num))
91 #define bswap_le_to_host16(num) byteswap16(num)
92 
93 /* We use ntoh*() here, because the compiler may
94  * attempt to optimise it
95  */
96 #elif __BYTE_ORDER == __LITTLE_ENDIAN
97 #define bswap_host_to_be64(num) (byteswap64(num))
98 #define bswap_host_to_le64(num) ((uint64_t)(num))
99 #define bswap_host_to_be32(num) (htonl(num))
100 #define bswap_host_to_le32(num) ((uint32_t)(num))
101 #define bswap_host_to_be16(num) (htons(num))
102 #define bswap_host_to_le16(num) ((uint16_t)(num))
103 
104 #define bswap_be_to_host64(num) (byteswap64(num))
105 #define bswap_le_to_host64(num) ((uint64_t)(num))
106 #define bswap_be_to_host32(num) (ntohl(num))
107 #define bswap_le_to_host32(num) ((uint32_t)(num))
108 #define bswap_be_to_host16(num) (ntohs(num))
109 #define bswap_le_to_host16(num) ((uint16_t)(num))
110 
111 #else
112 #error "Unknown byte order"
113 #endif
114 
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 #endif
120