aboutsummaryrefslogtreecommitdiff
path: root/webrtc/base/byteorder.h
blob: d579e6e185eb73de3ede51bddb809723ff2cfe72 (plain)
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
/*
 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_BASE_BYTEORDER_H_
#define WEBRTC_BASE_BYTEORDER_H_

#if defined(WEBRTC_POSIX) && !defined(__native_client__)
#include <arpa/inet.h>
#endif

#if defined(WEBRTC_WIN)
#include <stdlib.h>
#endif

#include "webrtc/base/basictypes.h"

namespace rtc {

// Reading and writing of little and big-endian numbers from memory
// TODO: Optimized versions, with direct read/writes of
// integers in host-endian format, when the platform supports it.

inline void Set8(void* memory, size_t offset, uint8_t v) {
  static_cast<uint8_t*>(memory)[offset] = v;
}

inline uint8_t Get8(const void* memory, size_t offset) {
  return static_cast<const uint8_t*>(memory)[offset];
}

inline void SetBE16(void* memory, uint16_t v) {
  Set8(memory, 0, static_cast<uint8_t>(v >> 8));
  Set8(memory, 1, static_cast<uint8_t>(v >> 0));
}

inline void SetBE32(void* memory, uint32_t v) {
  Set8(memory, 0, static_cast<uint8_t>(v >> 24));
  Set8(memory, 1, static_cast<uint8_t>(v >> 16));
  Set8(memory, 2, static_cast<uint8_t>(v >> 8));
  Set8(memory, 3, static_cast<uint8_t>(v >> 0));
}

inline void SetBE64(void* memory, uint64_t v) {
  Set8(memory, 0, static_cast<uint8_t>(v >> 56));
  Set8(memory, 1, static_cast<uint8_t>(v >> 48));
  Set8(memory, 2, static_cast<uint8_t>(v >> 40));
  Set8(memory, 3, static_cast<uint8_t>(v >> 32));
  Set8(memory, 4, static_cast<uint8_t>(v >> 24));
  Set8(memory, 5, static_cast<uint8_t>(v >> 16));
  Set8(memory, 6, static_cast<uint8_t>(v >> 8));
  Set8(memory, 7, static_cast<uint8_t>(v >> 0));
}

inline uint16_t GetBE16(const void* memory) {
  return static_cast<uint16_t>((Get8(memory, 0) << 8) | (Get8(memory, 1) << 0));
}

inline uint32_t GetBE32(const void* memory) {
  return (static_cast<uint32_t>(Get8(memory, 0)) << 24) |
         (static_cast<uint32_t>(Get8(memory, 1)) << 16) |
         (static_cast<uint32_t>(Get8(memory, 2)) << 8) |
         (static_cast<uint32_t>(Get8(memory, 3)) << 0);
}

inline uint64_t GetBE64(const void* memory) {
  return (static_cast<uint64_t>(Get8(memory, 0)) << 56) |
         (static_cast<uint64_t>(Get8(memory, 1)) << 48) |
         (static_cast<uint64_t>(Get8(memory, 2)) << 40) |
         (static_cast<uint64_t>(Get8(memory, 3)) << 32) |
         (static_cast<uint64_t>(Get8(memory, 4)) << 24) |
         (static_cast<uint64_t>(Get8(memory, 5)) << 16) |
         (static_cast<uint64_t>(Get8(memory, 6)) << 8) |
         (static_cast<uint64_t>(Get8(memory, 7)) << 0);
}

inline void SetLE16(void* memory, uint16_t v) {
  Set8(memory, 0, static_cast<uint8_t>(v >> 0));
  Set8(memory, 1, static_cast<uint8_t>(v >> 8));
}

inline void SetLE32(void* memory, uint32_t v) {
  Set8(memory, 0, static_cast<uint8_t>(v >> 0));
  Set8(memory, 1, static_cast<uint8_t>(v >> 8));
  Set8(memory, 2, static_cast<uint8_t>(v >> 16));
  Set8(memory, 3, static_cast<uint8_t>(v >> 24));
}

inline void SetLE64(void* memory, uint64_t v) {
  Set8(memory, 0, static_cast<uint8_t>(v >> 0));
  Set8(memory, 1, static_cast<uint8_t>(v >> 8));
  Set8(memory, 2, static_cast<uint8_t>(v >> 16));
  Set8(memory, 3, static_cast<uint8_t>(v >> 24));
  Set8(memory, 4, static_cast<uint8_t>(v >> 32));
  Set8(memory, 5, static_cast<uint8_t>(v >> 40));
  Set8(memory, 6, static_cast<uint8_t>(v >> 48));
  Set8(memory, 7, static_cast<uint8_t>(v >> 56));
}

inline uint16_t GetLE16(const void* memory) {
  return static_cast<uint16_t>((Get8(memory, 0) << 0) | (Get8(memory, 1) << 8));
}

inline uint32_t GetLE32(const void* memory) {
  return (static_cast<uint32_t>(Get8(memory, 0)) << 0) |
         (static_cast<uint32_t>(Get8(memory, 1)) << 8) |
         (static_cast<uint32_t>(Get8(memory, 2)) << 16) |
         (static_cast<uint32_t>(Get8(memory, 3)) << 24);
}

inline uint64_t GetLE64(const void* memory) {
  return (static_cast<uint64_t>(Get8(memory, 0)) << 0) |
         (static_cast<uint64_t>(Get8(memory, 1)) << 8) |
         (static_cast<uint64_t>(Get8(memory, 2)) << 16) |
         (static_cast<uint64_t>(Get8(memory, 3)) << 24) |
         (static_cast<uint64_t>(Get8(memory, 4)) << 32) |
         (static_cast<uint64_t>(Get8(memory, 5)) << 40) |
         (static_cast<uint64_t>(Get8(memory, 6)) << 48) |
         (static_cast<uint64_t>(Get8(memory, 7)) << 56);
}

// Check if the current host is big endian.
inline bool IsHostBigEndian() {
  static const int number = 1;
  return 0 == *reinterpret_cast<const char*>(&number);
}

inline uint16_t HostToNetwork16(uint16_t n) {
  uint16_t result;
  SetBE16(&result, n);
  return result;
}

inline uint32_t HostToNetwork32(uint32_t n) {
  uint32_t result;
  SetBE32(&result, n);
  return result;
}

inline uint64_t HostToNetwork64(uint64_t n) {
  uint64_t result;
  SetBE64(&result, n);
  return result;
}

inline uint16_t NetworkToHost16(uint16_t n) {
  return GetBE16(&n);
}

inline uint32_t NetworkToHost32(uint32_t n) {
  return GetBE32(&n);
}

inline uint64_t NetworkToHost64(uint64_t n) {
  return GetBE64(&n);
}

}  // namespace rtc

#endif  // WEBRTC_BASE_BYTEORDER_H_