Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: third_party/libjingle/overrides/talk/base/byteorder.h

Issue 10209008: Roll libjingle 132:133 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28 #ifndef TALK_BASE_BYTEORDER_H__
29 #define TALK_BASE_BYTEORDER_H__
30
31 #ifdef POSIX
32 #include <arpa/inet.h>
33 #endif
34
35 #ifdef WIN32
36 #include <stdlib.h>
37 #include <winsock2.h>
38 #endif
39
40 #include "talk/base/basictypes.h"
41
42 namespace talk_base {
43
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.
49
50 inline void Set8(void* memory, size_t offset, uint8 v) {
51 static_cast<uint8*>(memory)[offset] = v;
52 }
53 inline uint8 Get8(const void* memory, size_t offset) {
54 return static_cast<const uint8*>(memory)[offset];
55 }
56
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));
60 }
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));
66 }
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));
76 }
77 inline uint16 GetBE16(const void* memory) {
78 return (static_cast<uint16>(Get8(memory, 0)) << 8)
79 | (static_cast<uint16>(Get8(memory, 1)) << 0);
80 }
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);
86 }
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);
96 }
97
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));
101 }
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));
107 }
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));
117 }
118 inline uint16 GetLE16(const void* memory) {
119 return (static_cast<uint16>(Get8(memory, 1)) << 8)
120 | (static_cast<uint16>(Get8(memory, 0)) << 0);
121 }
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);
127 }
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);
137 }
138
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));
143 }
144
145 inline uint16 HostToNetwork16(uint16 n) {
146 #ifdef WIN32
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);
151 #else
152 return htons(n);
153 #endif
154 }
155
156 inline uint32 HostToNetwork32(uint32 n) {
157 #ifdef WIN32
158 return _byteswap_ulong(n);
159 #else
160 return htonl(n);
161 #endif
162 }
163
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);
167 }
168
169 inline uint16 NetworkToHost16(uint16 n) {
170 #ifdef WIN32
171 return _byteswap_ushort(n);
172 #else
173 return ntohs(n);
174 #endif
175 }
176
177 inline uint32 NetworkToHost32(uint32 n) {
178 #ifdef WIN32
179 return _byteswap_ulong(n);
180 #else
181 return ntohl(n);
182 #endif
183 }
184
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);
188 }
189
190 } // namespace talk_base
191
192 #endif // TALK_BASE_BYTEORDER_H__
OLDNEW
« no previous file with comments | « third_party/libjingle/libjingle.gyp ('k') | third_party/libjingle/overrides/talk/base/scoped_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698