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> |
---|
28 | /** @file |
---|
29 | * |
---|
30 | * @brief Header file containing definitions of functions and macros that deal |
---|
31 | * with byteswapping within libtrace and libpacketdump. |
---|
32 | * |
---|
33 | * @author Perry Lorier |
---|
34 | * @author Shane Alcock |
---|
35 | * |
---|
36 | * @version $Id$ |
---|
37 | */ |
---|
38 | |
---|
39 | #ifndef LT_BYTESWAP_H_ |
---|
40 | #define LT_BYTESWAP_H_ |
---|
41 | #ifdef __cplusplus |
---|
42 | extern "C" { |
---|
43 | #endif |
---|
44 | |
---|
45 | /** Byteswaps a 64-bit value. |
---|
46 | * |
---|
47 | * @param num The value to be byteswapped. |
---|
48 | * @return The byteswapped 64-bit number |
---|
49 | * |
---|
50 | */ |
---|
51 | uint64_t byteswap64(uint64_t num); |
---|
52 | |
---|
53 | /** Byteswaps a 32-bit value. |
---|
54 | * |
---|
55 | * @param num The value to be byteswapped. |
---|
56 | * @return The byteswapped 32-bit number |
---|
57 | * |
---|
58 | */ |
---|
59 | uint32_t byteswap32(uint32_t num); |
---|
60 | |
---|
61 | /** Byteswaps a 16-bit value. |
---|
62 | * |
---|
63 | * @param num The value to be byteswapped. |
---|
64 | * @return The byteswapped 16-bit number |
---|
65 | * |
---|
66 | */ |
---|
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 |
---|