00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/common/Endian.hpp $ 00003 // $Id: Endian.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for endian conversion. 00018 00019 #ifndef TORC_COMMON_ENDIAN_HPP 00020 #define TORC_COMMON_ENDIAN_HPP 00021 00022 #include <boost/cstdint.hpp> 00023 #include <boost/detail/endian.hpp> 00024 00025 // pull in ntohs(), ntohl(), htons(), and ntonl() 00026 #ifdef _WIN32 00027 #include <winsock2.h> 00028 #else 00029 #include <netinet/in.h> 00030 #endif 00031 //#if !defined(ntohs) || !defined(ntohl) || !defined(htons) || !defined(htonl) 00032 // #error Byte swapping macros have not been imported for this platform. 00033 //#endif 00034 00035 namespace torc { 00036 namespace common { 00037 00038 /// \brief Convert 64-bit types from network to host byte order. 00039 /// \details Complements ntohs and ntohl, both found in <netinet/in.h> or <winsock2.h>. 00040 boost::uint64_t inline ntohll(const boost::uint64_t& x) { 00041 #ifdef BOOST_BIG_ENDIAN 00042 return x; 00043 #else 00044 return 00045 (x>>56) | 00046 ((x<<40) & 0x00FF000000000000ull) | 00047 ((x<<24) & 0x0000FF0000000000ull) | 00048 ((x<<8) & 0x000000FF00000000ull) | 00049 ((x>>8) & 0x00000000FF000000ull) | 00050 ((x>>24) & 0x0000000000FF0000ull) | 00051 ((x>>40) & 0x000000000000FF00ull) | 00052 (x<<56); 00053 #endif 00054 } 00055 00056 /// \brief Convert 64-bit types from host to network byte order. 00057 /// \details Complements htons and htonl, both found in <netinet/in.h> or <winsock2.h>. 00058 // I'm sure there's a better way to swap the byte order for both ntohll() and htonll() without 00059 // replicating the code, but it'll be inlined anyway, and with this approach both ntohll() and 00060 // htonll() will show up symmetrically in doxygen. 00061 boost::uint64_t inline htonll(const boost::uint64_t& x) { 00062 #ifdef BOOST_BIG_ENDIAN 00063 return x; 00064 #else 00065 return 00066 (x>>56) | 00067 ((x<<40) & 0x00FF000000000000ull) | 00068 ((x<<24) & 0x0000FF0000000000ull) | 00069 ((x<<8) & 0x000000FF00000000ull) | 00070 ((x>>8) & 0x00000000FF000000ull) | 00071 ((x>>24) & 0x0000000000FF0000ull) | 00072 ((x>>40) & 0x000000000000FF00ull) | 00073 (x<<56); 00074 #endif 00075 } 00076 00077 } // namespace common 00078 } // namespace torc 00079 00080 #endif // TORC_COMMON_ENDIAN_HPP