-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
byteorder.h
178 lines (152 loc) · 6.32 KB
/
byteorder.h
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2004-2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program 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 3 of the License, or (at your
* option) any later version. This program 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 program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBDENG2_BYTEORDER_H
#define LIBDENG2_BYTEORDER_H
#include "../libcore.h"
namespace de {
/**
* Interface for a byte order converter.
*
* @ingroup data
*/
class DENG2_PUBLIC ByteOrder
{
public:
virtual ~ByteOrder() {}
/**
* Converts a 16-bit unsigned integer from network byte order to the host order.
*
* @param networkValue Value in network byte order.
* @param hostValue Value in the host's host byte order.
*/
virtual void networkToHost(duint16 const &networkValue, duint16 &hostValue) const = 0;
/**
* Converts a 32-bit unsigned integer from network byte order to the host order.
*
* @param networkValue Value in network byte order.
* @param hostValue Value in the host's host byte order.
*/
virtual void networkToHost(duint32 const &networkValue, duint32 &hostValue) const = 0;
/**
* Converts a 64-bit unsigned integer from network byte order to the host order.
*
* @param networkValue Value in network byte order.
* @param hostValue Value in the host's host byte order.
*/
virtual void networkToHost(duint64 const &networkValue, duint64 &hostValue) const = 0;
/**
* Converts a 16-bit unsigned integer from host byte order to the network order.
*
* @param hostValue Value in the host's host byte order.
* @param networkValue Value in network byte order is written here.
*/
virtual void hostToNetwork(duint16 const &hostValue, duint16 &networkValue) const = 0;
/**
* Converts a 32-bit unsigned integer from host byte order to the network order.
*
* @param hostValue Value in the host's host byte order.
* @param networkValue Value in network byte order is written here.
*/
virtual void hostToNetwork(duint32 const &hostValue, duint32 &networkValue) const = 0;
/**
* Converts a 64-bit unsigned integer from host byte order to the network order.
*
* @param hostValue Value in the host's host byte order.
* @param networkValue Value in network byte order is written here.
*/
virtual void hostToNetwork(duint64 const &hostValue, duint64 &networkValue) const = 0;
// The signed variants.
void hostToNetwork(dint16 const &hostValue, dint16 &networkValue) const;
void hostToNetwork(dint32 const &hostValue, dint32 &networkValue) const;
void hostToNetwork(dint64 const &hostValue, dint64 &networkValue) const;
void networkToHost(dint16 const &networkValue, dint16 &hostValue) const;
void networkToHost(dint32 const &networkValue, dint32 &hostValue) const;
void networkToHost(dint64 const &networkValue, dint64 &hostValue) const;
// Floating point.
void hostToNetwork(dfloat const &hostValue, dfloat &networkValue) const;
void hostToNetwork(ddouble const &hostValue, ddouble &networkValue) const;
void networkToHost(dfloat const &networkValue, dfloat &hostValue) const;
void networkToHost(ddouble const &networkValue, ddouble &hostValue) const;
// Convenience.
template <typename T>
T toNetwork(T const &hostValue) const {
T networkValue;
hostToNetwork(hostValue, networkValue);
return networkValue;
}
template <typename T>
T toHost(T const &networkValue) const {
T hostValue;
networkToHost(networkValue, hostValue);
return hostValue;
}
};
/**
* Big-endian byte order converter.
*
* @ingroup data
*/
class DENG2_PUBLIC BigEndianByteOrder : public ByteOrder
{
public:
using ByteOrder::networkToHost;
using ByteOrder::hostToNetwork;
void networkToHost(duint16 const &networkValue, duint16 &hostValue) const;
void networkToHost(duint32 const &networkValue, duint32 &hostValue) const;
void networkToHost(duint64 const &networkValue, duint64 &hostValue) const;
void hostToNetwork(duint16 const &hostValue, duint16 &networkValue) const;
void hostToNetwork(duint32 const &hostValue, duint32 &networkValue) const;
void hostToNetwork(duint64 const &hostValue, duint64 &networkValue) const;
};
/// Network byte order is big endian.
typedef BigEndianByteOrder NetworkByteOrder;
/**
* Little-endian byte order converter.
*
* @ingroup data
*/
class DENG2_PUBLIC LittleEndianByteOrder : public ByteOrder
{
public:
using ByteOrder::networkToHost;
using ByteOrder::hostToNetwork;
void networkToHost(duint16 const &networkValue, duint16 &hostValue) const;
void networkToHost(duint32 const &networkValue, duint32 &hostValue) const;
void networkToHost(duint64 const &networkValue, duint64 &hostValue) const;
void hostToNetwork(duint16 const &hostValue, duint16 &networkValue) const;
void hostToNetwork(duint32 const &hostValue, duint32 &networkValue) const;
void hostToNetwork(duint64 const &hostValue, duint64 &networkValue) const;
};
// Swaps the bytes of a 16-bit unsigned integer.
inline duint16 swap16(duint16 const &n) {
return duint16((n & 0xff) << 8) | duint16((n & 0xff00) >> 8);
}
/// Swaps the bytes of a 32-bit unsigned integer.
inline duint32 swap32(duint32 const &n) {
return ( ((n & 0xff) << 24) | ((n & 0xff00) << 8)
| ((n & 0xff0000) >> 8) | ((n & 0xff000000) >> 24));
}
/// Swaps the bytes in a 64-bit unsigned integer.
duint64 swap64(duint64 const &n);
/// Globally available big-endian byte order converter.
DENG2_PUBLIC extern BigEndianByteOrder bigEndianByteOrder;
/// Globally available little-endian byte order converter.
DENG2_PUBLIC extern LittleEndianByteOrder littleEndianByteOrder;
}
#endif /* LIBDENG2_BYTEORDER_H */