torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Endian.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for endian conversion.
18 
19 #ifndef TORC_COMMON_ENDIAN_HPP
20 #define TORC_COMMON_ENDIAN_HPP
21 
22 #include <boost/cstdint.hpp>
23 #include <boost/detail/endian.hpp>
24 
25 // pull in ntohs(), ntohl(), htons(), and ntonl()
26 #ifdef _WIN32
27  #include <winsock2.h>
28 #else
29  #include <netinet/in.h>
30 #endif
31 //#if !defined(ntohs) || !defined(ntohl) || !defined(htons) || !defined(htonl)
32 // #error Byte swapping macros have not been imported for this platform.
33 //#endif
34 
35 namespace torc {
36 namespace common {
37 
38 /// \brief Convert 64-bit types from network to host byte order.
39 /// \details Complements ntohs and ntohl, both found in <netinet/in.h> or <winsock2.h>.
40 boost::uint64_t inline ntohll(const boost::uint64_t& x) {
41 #ifdef BOOST_BIG_ENDIAN
42  return x;
43 #else
44  return
45  (x>>56) |
46  ((x<<40) & 0x00FF000000000000ull) |
47  ((x<<24) & 0x0000FF0000000000ull) |
48  ((x<<8) & 0x000000FF00000000ull) |
49  ((x>>8) & 0x00000000FF000000ull) |
50  ((x>>24) & 0x0000000000FF0000ull) |
51  ((x>>40) & 0x000000000000FF00ull) |
52  (x<<56);
53 #endif
54 }
55 
56 /// \brief Convert 64-bit types from host to network byte order.
57 /// \details Complements htons and htonl, both found in <netinet/in.h> or <winsock2.h>.
58 // I'm sure there's a better way to swap the byte order for both ntohll() and htonll() without
59 // replicating the code, but it'll be inlined anyway, and with this approach both ntohll() and
60 // htonll() will show up symmetrically in doxygen.
61 boost::uint64_t inline htonll(const boost::uint64_t& x) {
62 #ifdef BOOST_BIG_ENDIAN
63  return x;
64 #else
65  return
66  (x>>56) |
67  ((x<<40) & 0x00FF000000000000ull) |
68  ((x<<24) & 0x0000FF0000000000ull) |
69  ((x<<8) & 0x000000FF00000000ull) |
70  ((x>>8) & 0x00000000FF000000ull) |
71  ((x>>24) & 0x0000000000FF0000ull) |
72  ((x>>40) & 0x000000000000FF00ull) |
73  (x<<56);
74 #endif
75 }
76 
77 } // namespace common
78 } // namespace torc
79 
80 #endif // TORC_COMMON_ENDIAN_HPP
boost::uint64_t ntohll(const boost::uint64_t &x)
Convert 64-bit types from network to host byte order.
Definition: Endian.hpp:40
boost::uint64_t htonll(const boost::uint64_t &x)
Convert 64-bit types from host to network byte order.
Definition: Endian.hpp:61