You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004 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. #ifndef TALK_BASE_BYTEORDER_H_
  28. #define TALK_BASE_BYTEORDER_H_
  29. #ifdef POSIX
  30. #include <arpa/inet.h>
  31. #endif
  32. #ifdef WIN32
  33. #include <stdlib.h>
  34. #endif
  35. #include "talk/base/basictypes.h"
  36. namespace talk_base {
  37. // Reading and writing of little and big-endian numbers from memory
  38. // TODO: Optimized versions, with direct read/writes of
  39. // integers in host-endian format, when the platform supports it.
  40. inline void Set8(void* memory, size_t offset, uint8 v) {
  41. static_cast<uint8*>(memory)[offset] = v;
  42. }
  43. inline uint8 Get8(const void* memory, size_t offset) {
  44. return static_cast<const uint8*>(memory)[offset];
  45. }
  46. inline void SetBE16(void* memory, uint16 v) {
  47. Set8(memory, 0, static_cast<uint8>(v >> 8));
  48. Set8(memory, 1, static_cast<uint8>(v >> 0));
  49. }
  50. inline void SetBE32(void* memory, uint32 v) {
  51. Set8(memory, 0, static_cast<uint8>(v >> 24));
  52. Set8(memory, 1, static_cast<uint8>(v >> 16));
  53. Set8(memory, 2, static_cast<uint8>(v >> 8));
  54. Set8(memory, 3, static_cast<uint8>(v >> 0));
  55. }
  56. inline void SetBE64(void* memory, uint64 v) {
  57. Set8(memory, 0, static_cast<uint8>(v >> 56));
  58. Set8(memory, 1, static_cast<uint8>(v >> 48));
  59. Set8(memory, 2, static_cast<uint8>(v >> 40));
  60. Set8(memory, 3, static_cast<uint8>(v >> 32));
  61. Set8(memory, 4, static_cast<uint8>(v >> 24));
  62. Set8(memory, 5, static_cast<uint8>(v >> 16));
  63. Set8(memory, 6, static_cast<uint8>(v >> 8));
  64. Set8(memory, 7, static_cast<uint8>(v >> 0));
  65. }
  66. inline uint16 GetBE16(const void* memory) {
  67. return static_cast<uint16>((Get8(memory, 0) << 8) |
  68. (Get8(memory, 1) << 0));
  69. }
  70. inline uint32 GetBE32(const void* memory) {
  71. return (static_cast<uint32>(Get8(memory, 0)) << 24) |
  72. (static_cast<uint32>(Get8(memory, 1)) << 16) |
  73. (static_cast<uint32>(Get8(memory, 2)) << 8) |
  74. (static_cast<uint32>(Get8(memory, 3)) << 0);
  75. }
  76. inline uint64 GetBE64(const void* memory) {
  77. return (static_cast<uint64>(Get8(memory, 0)) << 56) |
  78. (static_cast<uint64>(Get8(memory, 1)) << 48) |
  79. (static_cast<uint64>(Get8(memory, 2)) << 40) |
  80. (static_cast<uint64>(Get8(memory, 3)) << 32) |
  81. (static_cast<uint64>(Get8(memory, 4)) << 24) |
  82. (static_cast<uint64>(Get8(memory, 5)) << 16) |
  83. (static_cast<uint64>(Get8(memory, 6)) << 8) |
  84. (static_cast<uint64>(Get8(memory, 7)) << 0);
  85. }
  86. inline void SetLE16(void* memory, uint16 v) {
  87. Set8(memory, 0, static_cast<uint8>(v >> 0));
  88. Set8(memory, 1, static_cast<uint8>(v >> 8));
  89. }
  90. inline void SetLE32(void* memory, uint32 v) {
  91. Set8(memory, 0, static_cast<uint8>(v >> 0));
  92. Set8(memory, 1, static_cast<uint8>(v >> 8));
  93. Set8(memory, 2, static_cast<uint8>(v >> 16));
  94. Set8(memory, 3, static_cast<uint8>(v >> 24));
  95. }
  96. inline void SetLE64(void* memory, uint64 v) {
  97. Set8(memory, 0, static_cast<uint8>(v >> 0));
  98. Set8(memory, 1, static_cast<uint8>(v >> 8));
  99. Set8(memory, 2, static_cast<uint8>(v >> 16));
  100. Set8(memory, 3, static_cast<uint8>(v >> 24));
  101. Set8(memory, 4, static_cast<uint8>(v >> 32));
  102. Set8(memory, 5, static_cast<uint8>(v >> 40));
  103. Set8(memory, 6, static_cast<uint8>(v >> 48));
  104. Set8(memory, 7, static_cast<uint8>(v >> 56));
  105. }
  106. inline uint16 GetLE16(const void* memory) {
  107. return static_cast<uint16>((Get8(memory, 0) << 0) |
  108. (Get8(memory, 1) << 8));
  109. }
  110. inline uint32 GetLE32(const void* memory) {
  111. return (static_cast<uint32>(Get8(memory, 0)) << 0) |
  112. (static_cast<uint32>(Get8(memory, 1)) << 8) |
  113. (static_cast<uint32>(Get8(memory, 2)) << 16) |
  114. (static_cast<uint32>(Get8(memory, 3)) << 24);
  115. }
  116. inline uint64 GetLE64(const void* memory) {
  117. return (static_cast<uint64>(Get8(memory, 0)) << 0) |
  118. (static_cast<uint64>(Get8(memory, 1)) << 8) |
  119. (static_cast<uint64>(Get8(memory, 2)) << 16) |
  120. (static_cast<uint64>(Get8(memory, 3)) << 24) |
  121. (static_cast<uint64>(Get8(memory, 4)) << 32) |
  122. (static_cast<uint64>(Get8(memory, 5)) << 40) |
  123. (static_cast<uint64>(Get8(memory, 6)) << 48) |
  124. (static_cast<uint64>(Get8(memory, 7)) << 56);
  125. }
  126. // Check if the current host is big endian.
  127. inline bool IsHostBigEndian() {
  128. static const int number = 1;
  129. return 0 == *reinterpret_cast<const char*>(&number);
  130. }
  131. inline uint16 HostToNetwork16(uint16 n) {
  132. uint16 result;
  133. SetBE16(&result, n);
  134. return result;
  135. }
  136. inline uint32 HostToNetwork32(uint32 n) {
  137. uint32 result;
  138. SetBE32(&result, n);
  139. return result;
  140. }
  141. inline uint64 HostToNetwork64(uint64 n) {
  142. uint64 result;
  143. SetBE64(&result, n);
  144. return result;
  145. }
  146. inline uint16 NetworkToHost16(uint16 n) {
  147. return GetBE16(&n);
  148. }
  149. inline uint32 NetworkToHost32(uint32 n) {
  150. return GetBE32(&n);
  151. }
  152. inline uint64 NetworkToHost64(uint64 n) {
  153. return GetBE64(&n);
  154. }
  155. } // namespace talk_base
  156. #endif // TALK_BASE_BYTEORDER_H_