OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2004--2005, Google Inc. | 3 * Copyright 2004--2005, Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 15 matching lines...) Expand all Loading... |
26 */ | 26 */ |
27 | 27 |
28 #ifndef TALK_BASE_BYTEORDER_H__ | 28 #ifndef TALK_BASE_BYTEORDER_H__ |
29 #define TALK_BASE_BYTEORDER_H__ | 29 #define TALK_BASE_BYTEORDER_H__ |
30 | 30 |
31 #ifdef POSIX | 31 #ifdef POSIX |
32 #include <arpa/inet.h> | 32 #include <arpa/inet.h> |
33 #endif | 33 #endif |
34 | 34 |
35 #ifdef WIN32 | 35 #ifdef WIN32 |
| 36 #include <stdlib.h> |
36 #include <winsock2.h> | 37 #include <winsock2.h> |
37 #include "talk/base/win32.h" | |
38 #endif | 38 #endif |
39 | 39 |
40 #include "talk/base/basictypes.h" | 40 #include "talk/base/basictypes.h" |
41 | 41 |
42 namespace talk_base { | 42 namespace talk_base { |
43 | 43 |
44 // Reading and writing of little and big-endian numbers from memory | 44 // Reading and writing of little and big-endian numbers from memory |
45 // TODO: Add HostEndian #defines (HE) | 45 // TODO: Add HostEndian #defines (HE) |
46 // TODO: Consider NetworkEndian as synonym for BigEndian, for clarity in use. | 46 // TODO: Consider NetworkEndian as synonym for BigEndian, for clarity in use. |
47 // TODO: Consider creating optimized versions, such as direct read/writes of | 47 // TODO: Consider creating optimized versions, such as direct read/writes of |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | (static_cast<uint64>(Get8(memory, 0)) << 0); | 136 | (static_cast<uint64>(Get8(memory, 0)) << 0); |
137 } | 137 } |
138 | 138 |
139 // Check if the current host is big endian. | 139 // Check if the current host is big endian. |
140 inline bool IsHostBigEndian() { | 140 inline bool IsHostBigEndian() { |
141 static const int number = 1; | 141 static const int number = 1; |
142 return (0 == *reinterpret_cast<const char*>(&number)); | 142 return (0 == *reinterpret_cast<const char*>(&number)); |
143 } | 143 } |
144 | 144 |
145 inline uint16 HostToNetwork16(uint16 n) { | 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 |
146 return htons(n); | 152 return htons(n); |
| 153 #endif |
147 } | 154 } |
148 | 155 |
149 inline uint32 HostToNetwork32(uint32 n) { | 156 inline uint32 HostToNetwork32(uint32 n) { |
| 157 #ifdef WIN32 |
| 158 return _byteswap_ulong(n); |
| 159 #else |
150 return htonl(n); | 160 return htonl(n); |
| 161 #endif |
151 } | 162 } |
152 | 163 |
153 inline uint64 HostToNetwork64(uint64 n) { | 164 inline uint64 HostToNetwork64(uint64 n) { |
154 // If the host is little endian, GetBE64 converts n to big network endian. | 165 // If the host is little endian, GetBE64 converts n to big network endian. |
155 return IsHostBigEndian() ? n : GetBE64(&n); | 166 return IsHostBigEndian() ? n : GetBE64(&n); |
156 } | 167 } |
157 | 168 |
158 inline uint16 NetworkToHost16(uint16 n) { | 169 inline uint16 NetworkToHost16(uint16 n) { |
| 170 #ifdef WIN32 |
| 171 return _byteswap_ushort(n); |
| 172 #else |
159 return ntohs(n); | 173 return ntohs(n); |
| 174 #endif |
160 } | 175 } |
161 | 176 |
162 inline uint32 NetworkToHost32(uint32 n) { | 177 inline uint32 NetworkToHost32(uint32 n) { |
| 178 #ifdef WIN32 |
| 179 return _byteswap_ulong(n); |
| 180 #else |
163 return ntohl(n); | 181 return ntohl(n); |
| 182 #endif |
164 } | 183 } |
165 | 184 |
166 inline uint64 NetworkToHost64(uint64 n) { | 185 inline uint64 NetworkToHost64(uint64 n) { |
167 // If the host is little endian, GetBE64 converts n to little endian. | 186 // If the host is little endian, GetBE64 converts n to little endian. |
168 return IsHostBigEndian() ? n : GetBE64(&n); | 187 return IsHostBigEndian() ? n : GetBE64(&n); |
169 } | 188 } |
170 | 189 |
171 } // namespace talk_base | 190 } // namespace talk_base |
172 | 191 |
173 #endif // TALK_BASE_BYTEORDER_H__ | 192 #endif // TALK_BASE_BYTEORDER_H__ |
OLD | NEW |