byte_order.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_BYTE_ORDER_H_
  11. #define RTC_BASE_BYTE_ORDER_H_
  12. #include <stdint.h>
  13. #if defined(WEBRTC_POSIX) && !defined(__native_client__)
  14. #include <arpa/inet.h>
  15. #endif
  16. #include "rtc_base/system/arch.h"
  17. #if defined(WEBRTC_MAC)
  18. #include <libkern/OSByteOrder.h>
  19. #define htobe16(v) OSSwapHostToBigInt16(v)
  20. #define htobe32(v) OSSwapHostToBigInt32(v)
  21. #define htobe64(v) OSSwapHostToBigInt64(v)
  22. #define be16toh(v) OSSwapBigToHostInt16(v)
  23. #define be32toh(v) OSSwapBigToHostInt32(v)
  24. #define be64toh(v) OSSwapBigToHostInt64(v)
  25. #define htole16(v) OSSwapHostToLittleInt16(v)
  26. #define htole32(v) OSSwapHostToLittleInt32(v)
  27. #define htole64(v) OSSwapHostToLittleInt64(v)
  28. #define le16toh(v) OSSwapLittleToHostInt16(v)
  29. #define le32toh(v) OSSwapLittleToHostInt32(v)
  30. #define le64toh(v) OSSwapLittleToHostInt64(v)
  31. #elif defined(WEBRTC_WIN) || defined(__native_client__)
  32. #if defined(WEBRTC_WIN)
  33. #include <stdlib.h>
  34. #include <winsock2.h>
  35. #else
  36. #include <netinet/in.h>
  37. #endif // defined(WEBRTC_WIN)
  38. #if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
  39. #define htobe16(v) htons(v)
  40. #define htobe32(v) htonl(v)
  41. #define be16toh(v) ntohs(v)
  42. #define be32toh(v) ntohl(v)
  43. #define htole16(v) (v)
  44. #define htole32(v) (v)
  45. #define htole64(v) (v)
  46. #define le16toh(v) (v)
  47. #define le32toh(v) (v)
  48. #define le64toh(v) (v)
  49. #if defined(WEBRTC_WIN)
  50. #define htobe64(v) _byteswap_uint64(v)
  51. #define be64toh(v) _byteswap_uint64(v)
  52. #endif // defined(WEBRTC_WIN)
  53. #if defined(__native_client__)
  54. #define htobe64(v) __builtin_bswap64(v)
  55. #define be64toh(v) __builtin_bswap64(v)
  56. #endif // defined(__native_client__)
  57. #elif defined(WEBRTC_ARCH_BIG_ENDIAN)
  58. #define htobe16(v) (v)
  59. #define htobe32(v) (v)
  60. #define be16toh(v) (v)
  61. #define be32toh(v) (v)
  62. #define htole16(v) __builtin_bswap16(v)
  63. #define htole32(v) __builtin_bswap32(v)
  64. #define htole64(v) __builtin_bswap64(v)
  65. #define le16toh(v) __builtin_bswap16(v)
  66. #define le32toh(v) __builtin_bswap32(v)
  67. #define le64toh(v) __builtin_bswap64(v)
  68. #if defined(WEBRTC_WIN)
  69. #define htobe64(v) (v)
  70. #define be64toh(v) (v)
  71. #endif // defined(WEBRTC_WIN)
  72. #if defined(__native_client__)
  73. #define htobe64(v) (v)
  74. #define be64toh(v) (v)
  75. #endif // defined(__native_client__)
  76. #else
  77. #error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
  78. #endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
  79. #elif defined(WEBRTC_POSIX)
  80. #include <endian.h>
  81. #else
  82. #error "Missing byte order functions for this arch."
  83. #endif // defined(WEBRTC_MAC)
  84. namespace rtc {
  85. // Reading and writing of little and big-endian numbers from memory
  86. inline void Set8(void* memory, size_t offset, uint8_t v) {
  87. static_cast<uint8_t*>(memory)[offset] = v;
  88. }
  89. inline uint8_t Get8(const void* memory, size_t offset) {
  90. return static_cast<const uint8_t*>(memory)[offset];
  91. }
  92. inline void SetBE16(void* memory, uint16_t v) {
  93. *static_cast<uint16_t*>(memory) = htobe16(v);
  94. }
  95. inline void SetBE32(void* memory, uint32_t v) {
  96. *static_cast<uint32_t*>(memory) = htobe32(v);
  97. }
  98. inline void SetBE64(void* memory, uint64_t v) {
  99. *static_cast<uint64_t*>(memory) = htobe64(v);
  100. }
  101. inline uint16_t GetBE16(const void* memory) {
  102. return be16toh(*static_cast<const uint16_t*>(memory));
  103. }
  104. inline uint32_t GetBE32(const void* memory) {
  105. return be32toh(*static_cast<const uint32_t*>(memory));
  106. }
  107. inline uint64_t GetBE64(const void* memory) {
  108. return be64toh(*static_cast<const uint64_t*>(memory));
  109. }
  110. inline void SetLE16(void* memory, uint16_t v) {
  111. *static_cast<uint16_t*>(memory) = htole16(v);
  112. }
  113. inline void SetLE32(void* memory, uint32_t v) {
  114. *static_cast<uint32_t*>(memory) = htole32(v);
  115. }
  116. inline void SetLE64(void* memory, uint64_t v) {
  117. *static_cast<uint64_t*>(memory) = htole64(v);
  118. }
  119. inline uint16_t GetLE16(const void* memory) {
  120. return le16toh(*static_cast<const uint16_t*>(memory));
  121. }
  122. inline uint32_t GetLE32(const void* memory) {
  123. return le32toh(*static_cast<const uint32_t*>(memory));
  124. }
  125. inline uint64_t GetLE64(const void* memory) {
  126. return le64toh(*static_cast<const uint64_t*>(memory));
  127. }
  128. // Check if the current host is big endian.
  129. inline bool IsHostBigEndian() {
  130. #if defined(WEBRTC_ARCH_BIG_ENDIAN)
  131. return true;
  132. #else
  133. return false;
  134. #endif
  135. }
  136. inline uint16_t HostToNetwork16(uint16_t n) {
  137. return htobe16(n);
  138. }
  139. inline uint32_t HostToNetwork32(uint32_t n) {
  140. return htobe32(n);
  141. }
  142. inline uint64_t HostToNetwork64(uint64_t n) {
  143. return htobe64(n);
  144. }
  145. inline uint16_t NetworkToHost16(uint16_t n) {
  146. return be16toh(n);
  147. }
  148. inline uint32_t NetworkToHost32(uint32_t n) {
  149. return be32toh(n);
  150. }
  151. inline uint64_t NetworkToHost64(uint64_t n) {
  152. return be64toh(n);
  153. }
  154. } // namespace rtc
  155. #endif // RTC_BASE_BYTE_ORDER_H_