/usr/include/cxxtools/byteorder.h is in libcxxtools-dev 2.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | /*
* Copyright (C) 2007 Tommi Maekitalo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* As a special exception, you may use this file as part of a free
* software library without restriction. Specifically, if other files
* instantiate templates or use macros or inline functions from this
* file, or you compile this file and link it with other files to
* produce an executable, this file does not by itself cause the
* resulting executable to be covered by the GNU General Public
* License. This exception does not however invalidate any other
* reasons why the executable file might be covered by the GNU Library
* General Public License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CXXTOOLS_BYTEORDER_H
#define CXXTOOLS_BYTEORDER_H
#include <algorithm>
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define CXXTOOLS_LITTLE_ENDIAN
#elif __BYTE_ORDER == __BIG_ENDIAN
#define CXXTOOLS_BIG_ENDIAN
#endif
namespace cxxtools
{
template <typename T, unsigned sizeofT>
class Reverser;
template <typename T>
class Reverser<T, 2>
{
public:
T operator() (T value)
{
return value >> 8 & 0xff
| value << 8 & 0xff00;
}
};
template <typename T>
class Reverser<T, 4>
{
public:
T operator() (T value)
{
return value >> 24 & 0xff
| value >> 8 & 0xff00
| value << 8 & 0xff0000
| value << 24 & 0xff000000;
}
};
template <typename T>
class Reverser<T, 8>
{
public:
T operator() (T value)
{
return value >> 56 & 0xffll
| value >> 40 & 0xff00ll
| value >> 24 & 0xff0000ll
| value >> 8 & 0xff000000ll
| value << 8 & 0xff00000000ll
| value << 24 & 0xff0000000000ll
| value << 40 & 0xff000000000000ll
| value << 56 & 0xff00000000000000ll;
}
};
template <typename T>
T reverse(T value)
{
return Reverser<T, sizeof(T)>() (value);
}
/// Returns true, if machine is big-endian (high byte first).
/// e.g. PowerPC
inline bool isBigEndian()
{
const int i = 1;
return *reinterpret_cast<const int8_t*>(&i) == 0;
}
/// Returns true, if machine is little-endian (low byte first).
/// e.g. x86
inline bool isLittleEndian()
{
const int i = 1;
return *reinterpret_cast<const int8_t*>(&i) == 1;
}
/// Converts a native value in little endian.
template <typename T>
T hostToLe(T value)
{
#if defined(CXXTOOLS_BIG_ENDIAN)
return reverse(value);
#elif defined(CXXTOOLS_LITTLE_ENDIAN)
return value;
#else
return isBigEndian() ? reverse(value) : value;
#endif
}
/// Converts a little endian value to native.
template <typename T>
T leToHost(T value)
{
#if defined(CXXTOOLS_BIG_ENDIAN)
return reverse(value);
#elif defined(CXXTOOLS_LITTLE_ENDIAN)
return value;
#else
return isBigEndian() ? reverse(value) : value;
#endif
}
/// Converts a native value in big endian.
template <typename T>
T hostToBe(T value)
{
#if defined(CXXTOOLS_BIG_ENDIAN)
return value;
#elif defined(CXXTOOLS_LITTLE_ENDIAN)
return reverse(value);
#else
return isBigEndian() ? value : reverse(value);
#endif
}
/// Converts a big endian value to native.
template <typename T>
T beToHost(T value)
{
#if defined(CXXTOOLS_BIG_ENDIAN)
return value;
#elif defined(CXXTOOLS_LITTLE_ENDIAN)
return reverse(value);
#else
return isBigEndian() ? value : reverse(value);
#endif
}
}
#endif // CXXTOOLS_BYTEORDER_H
|