3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef TALK_BASE_BYTEORDER_H__
29
#define TALK_BASE_BYTEORDER_H__
32
#include <arpa/inet.h>
40
#include "talk/base/basictypes.h"
44
// Reading and writing of little and big-endian numbers from memory
45
// TODO: Add HostEndian #defines (HE)
46
// TODO: Consider NetworkEndian as synonym for BigEndian, for clarity in use.
47
// TODO: Consider creating optimized versions, such as direct read/writes of
48
// integers in host-endian format, when the platform supports it.
50
inline void Set8(void* memory, size_t offset, uint8 v) {
51
static_cast<uint8*>(memory)[offset] = v;
53
inline uint8 Get8(const void* memory, size_t offset) {
54
return static_cast<const uint8*>(memory)[offset];
57
inline void SetBE16(void* memory, uint16 v) {
58
Set8(memory, 0, static_cast<uint8>(v >> 8));
59
Set8(memory, 1, static_cast<uint8>(v >> 0));
61
inline void SetBE32(void* memory, uint32 v) {
62
Set8(memory, 0, static_cast<uint8>(v >> 24));
63
Set8(memory, 1, static_cast<uint8>(v >> 16));
64
Set8(memory, 2, static_cast<uint8>(v >> 8));
65
Set8(memory, 3, static_cast<uint8>(v >> 0));
67
inline void SetBE64(void* memory, uint64 v) {
68
Set8(memory, 0, static_cast<uint8>(v >> 56));
69
Set8(memory, 1, static_cast<uint8>(v >> 48));
70
Set8(memory, 2, static_cast<uint8>(v >> 40));
71
Set8(memory, 3, static_cast<uint8>(v >> 32));
72
Set8(memory, 4, static_cast<uint8>(v >> 24));
73
Set8(memory, 5, static_cast<uint8>(v >> 16));
74
Set8(memory, 6, static_cast<uint8>(v >> 8));
75
Set8(memory, 7, static_cast<uint8>(v >> 0));
77
inline uint16 GetBE16(const void* memory) {
78
return (static_cast<uint16>(Get8(memory, 0)) << 8)
79
| (static_cast<uint16>(Get8(memory, 1)) << 0);
81
inline uint32 GetBE32(const void* memory) {
82
return (static_cast<uint32>(Get8(memory, 0)) << 24)
83
| (static_cast<uint32>(Get8(memory, 1)) << 16)
84
| (static_cast<uint32>(Get8(memory, 2)) << 8)
85
| (static_cast<uint32>(Get8(memory, 3)) << 0);
87
inline uint64 GetBE64(const void* memory) {
88
return (static_cast<uint64>(Get8(memory, 0)) << 56)
89
| (static_cast<uint64>(Get8(memory, 1)) << 48)
90
| (static_cast<uint64>(Get8(memory, 2)) << 40)
91
| (static_cast<uint64>(Get8(memory, 3)) << 32)
92
| (static_cast<uint64>(Get8(memory, 4)) << 24)
93
| (static_cast<uint64>(Get8(memory, 5)) << 16)
94
| (static_cast<uint64>(Get8(memory, 6)) << 8)
95
| (static_cast<uint64>(Get8(memory, 7)) << 0);
98
inline void SetLE16(void* memory, uint16 v) {
99
Set8(memory, 1, static_cast<uint8>(v >> 8));
100
Set8(memory, 0, static_cast<uint8>(v >> 0));
102
inline void SetLE32(void* memory, uint32 v) {
103
Set8(memory, 3, static_cast<uint8>(v >> 24));
104
Set8(memory, 2, static_cast<uint8>(v >> 16));
105
Set8(memory, 1, static_cast<uint8>(v >> 8));
106
Set8(memory, 0, static_cast<uint8>(v >> 0));
108
inline void SetLE64(void* memory, uint64 v) {
109
Set8(memory, 7, static_cast<uint8>(v >> 56));
110
Set8(memory, 6, static_cast<uint8>(v >> 48));
111
Set8(memory, 5, static_cast<uint8>(v >> 40));
112
Set8(memory, 4, static_cast<uint8>(v >> 32));
113
Set8(memory, 3, static_cast<uint8>(v >> 24));
114
Set8(memory, 2, static_cast<uint8>(v >> 16));
115
Set8(memory, 1, static_cast<uint8>(v >> 8));
116
Set8(memory, 0, static_cast<uint8>(v >> 0));
118
inline uint16 GetLE16(const void* memory) {
119
return (static_cast<uint16>(Get8(memory, 1)) << 8)
120
| (static_cast<uint16>(Get8(memory, 0)) << 0);
122
inline uint32 GetLE32(const void* memory) {
123
return (static_cast<uint32>(Get8(memory, 3)) << 24)
124
| (static_cast<uint32>(Get8(memory, 2)) << 16)
125
| (static_cast<uint32>(Get8(memory, 1)) << 8)
126
| (static_cast<uint32>(Get8(memory, 0)) << 0);
128
inline uint64 GetLE64(const void* memory) {
129
return (static_cast<uint64>(Get8(memory, 7)) << 56)
130
| (static_cast<uint64>(Get8(memory, 6)) << 48)
131
| (static_cast<uint64>(Get8(memory, 5)) << 40)
132
| (static_cast<uint64>(Get8(memory, 4)) << 32)
133
| (static_cast<uint64>(Get8(memory, 3)) << 24)
134
| (static_cast<uint64>(Get8(memory, 2)) << 16)
135
| (static_cast<uint64>(Get8(memory, 1)) << 8)
136
| (static_cast<uint64>(Get8(memory, 0)) << 0);
139
// Check if the current host is big endian.
140
inline bool IsHostBigEndian() {
141
static const int number = 1;
142
return (0 == *reinterpret_cast<const char*>(&number));
145
inline uint16 HostToNetwork16(uint16 n) {
147
// This and below _byteswap_* are to remove the dependency to ws2_32.dll
148
// especially for chrome, where we don't load the ws2_32.dll in the render
149
// process. This is correct only on little-endian machines.
150
return _byteswap_ushort(n);
156
inline uint32 HostToNetwork32(uint32 n) {
158
return _byteswap_ulong(n);
164
inline uint64 HostToNetwork64(uint64 n) {
165
// If the host is little endian, GetBE64 converts n to big network endian.
166
return IsHostBigEndian() ? n : GetBE64(&n);
169
inline uint16 NetworkToHost16(uint16 n) {
171
return _byteswap_ushort(n);
177
inline uint32 NetworkToHost32(uint32 n) {
179
return _byteswap_ulong(n);
185
inline uint64 NetworkToHost64(uint64 n) {
186
// If the host is little endian, GetBE64 converts n to little endian.
187
return IsHostBigEndian() ? n : GetBE64(&n);
190
} // namespace talk_base
192
#endif // TALK_BASE_BYTEORDER_H__