endian.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD
  7. */
  8. /*
  9. * Licensed to the Apache Software Foundation (ASF) under one
  10. * or more contributor license agreements. See the NOTICE file
  11. * distributed with this work for additional information
  12. * regarding copyright ownership. The ASF licenses this file
  13. * to you under the Apache License, Version 2.0 (the
  14. * "License"); you may not use this file except in compliance
  15. * with the License. You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing,
  20. * software distributed under the License is distributed on an
  21. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  22. * KIND, either express or implied. See the License for the
  23. * specific language governing permissions and limitations
  24. * under the License.
  25. */
  26. #ifndef H_ENDIAN_
  27. #define H_ENDIAN_
  28. #include <inttypes.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Internal helpers */
  33. #ifndef os_bswap_64
  34. #define os_bswap_64(x) ((uint64_t) \
  35. ((((x) & 0xff00000000000000ull) >> 56) | \
  36. (((x) & 0x00ff000000000000ull) >> 40) | \
  37. (((x) & 0x0000ff0000000000ull) >> 24) | \
  38. (((x) & 0x000000ff00000000ull) >> 8) | \
  39. (((x) & 0x00000000ff000000ull) << 8) | \
  40. (((x) & 0x0000000000ff0000ull) << 24) | \
  41. (((x) & 0x000000000000ff00ull) << 40) | \
  42. (((x) & 0x00000000000000ffull) << 56)))
  43. #endif
  44. #ifndef os_bswap_32
  45. #define os_bswap_32(x) ((uint32_t) \
  46. ((((x) & 0xff000000) >> 24) | \
  47. (((x) & 0x00ff0000) >> 8) | \
  48. (((x) & 0x0000ff00) << 8) | \
  49. (((x) & 0x000000ff) << 24)))
  50. #endif
  51. #ifndef os_bswap_16
  52. #define os_bswap_16(x) ((uint16_t) \
  53. ((((x) & 0xff00) >> 8) | \
  54. (((x) & 0x00ff) << 8)))
  55. #endif
  56. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  57. #ifndef ntohll
  58. #define ntohll(x) ((uint64_t)(x))
  59. #endif
  60. #ifndef htonll
  61. #define htonll(x) ((uint64_t)(x))
  62. #endif
  63. #ifndef ntohl
  64. #define ntohl(x) ((uint32_t)(x))
  65. #endif
  66. #ifndef htonl
  67. #define htonl(x) ((uint32_t)(x))
  68. #endif
  69. #ifndef ntohs
  70. #define ntohs(x) ((uint16_t)(x))
  71. #endif
  72. #ifndef htons
  73. #define htons(x) ((uint16_t)(x))
  74. #endif
  75. #ifndef htobe16
  76. #define htobe16(x) ((uint16_t)(x))
  77. #endif
  78. #ifndef htole16
  79. #define htole16(x) os_bswap_16 (x)
  80. #endif
  81. #ifndef be16toh
  82. #define be16toh(x) ((uint16_t)(x))
  83. #endif
  84. #ifndef le16toh
  85. #define le16toh(x) os_bswap_16 (x)
  86. #endif
  87. #ifndef htobe32
  88. #define htobe32(x) ((uint32_t)(x))
  89. #endif
  90. #ifndef htole32
  91. #define htole32(x) os_bswap_32 (x)
  92. #endif
  93. #ifndef be32toh
  94. #define be32toh(x) ((uint32_t)(x))
  95. #endif
  96. #ifndef le32toh
  97. #define le32toh(x) os_bswap_32 (x)
  98. #endif
  99. #ifndef htobe64
  100. #define htobe64(x) ((uint64_t)(x))
  101. #endif
  102. #ifndef htole64
  103. #define htole64(x) os_bswap_64 (x)
  104. #endif
  105. #ifndef be64toh
  106. #define be64toh(x) ((uint64_t)(x))
  107. #endif
  108. #ifndef le64toh
  109. #define le64toh(x) os_bswap_64 (x)
  110. #endif
  111. #else
  112. #ifndef ntohll
  113. #define ntohll(x) os_bswap_64(x)
  114. #endif
  115. #ifndef htonll
  116. #define htonll ntohll
  117. #endif
  118. /* These are not used in NimBLE and ESP-IDF uses them from LwIP */
  119. #if 0
  120. #ifndef ntohl
  121. #define ntohl(x) os_bswap_32(x)
  122. #endif
  123. #ifndef htonl
  124. #define htonl ntohl
  125. #endif
  126. #ifndef htons
  127. #define htons(x) os_bswap_16(x)
  128. #endif
  129. #ifndef ntohs
  130. #define ntohs htons
  131. #endif
  132. #endif
  133. #ifndef htobe16
  134. #define htobe16(x) os_bswap_16(x)
  135. #endif
  136. #ifndef htole16
  137. #define htole16(x) ((uint16_t)(x))
  138. #endif
  139. #ifndef be16toh
  140. #define be16toh(x) os_bswap_16(x)
  141. #endif
  142. #ifndef le16toh
  143. #define le16toh(x) ((uint16_t)(x))
  144. #endif
  145. #ifndef htobe32
  146. #define htobe32(x) os_bswap_32(x)
  147. #endif
  148. #ifndef htole32
  149. #define htole32(x) ((uint32_t)(x))
  150. #endif
  151. #ifndef be32toh
  152. #define be32toh(x) os_bswap_32(x)
  153. #endif
  154. #ifndef le32toh
  155. #define le32toh(x) ((uint32_t)(x))
  156. #endif
  157. #ifndef htobe64
  158. #define htobe64(x) os_bswap_64(x)
  159. #endif
  160. #ifndef htole64
  161. #define htole64(x) ((uint64_t)(x))
  162. #endif
  163. #ifndef be64toh
  164. #define be64toh(x) os_bswap_64(x)
  165. #endif
  166. #ifndef le64toh
  167. #define le64toh(x) ((uint64_t)(x))
  168. #endif
  169. #endif
  170. #if SOC_ESP_NIMBLE_CONTROLLER
  171. void r_put_le16(void *buf, uint16_t x);
  172. #define put_le16 r_put_le16
  173. void r_put_le24(void *buf, uint32_t x);
  174. #define put_le24 r_put_le24
  175. void r_put_le32(void *buf, uint32_t x);
  176. #define put_le32 r_put_le32
  177. void r_put_le64(void *buf, uint64_t x);
  178. #define put_le64 r_put_le64
  179. uint16_t r_get_le16(const void *buf);
  180. #define get_le16 r_get_le16
  181. uint32_t r_get_le24(const void *buf);
  182. #define get_le24 r_get_le24
  183. uint32_t r_get_le32(const void *buf);
  184. #define get_le32 r_get_le32
  185. uint64_t r_get_le64(const void *buf);
  186. #define get_le64 r_get_le64
  187. void r_put_be16(void *buf, uint16_t x);
  188. #define put_be16 r_put_be16
  189. void r_put_be24(void *buf, uint32_t x);
  190. #define put_be24 r_put_be24
  191. void r_put_be32(void *buf, uint32_t x);
  192. #define put_be32 r_put_be32
  193. void r_put_be64(void *buf, uint64_t x);
  194. #define put_be64 r_put_be64
  195. uint16_t r_get_be16(const void *buf);
  196. #define get_be16 r_get_be16
  197. uint32_t r_get_be24(const void *buf);
  198. #define get_be24 r_get_be24
  199. uint32_t r_get_be32(const void *buf);
  200. #define get_be32 r_get_be32
  201. uint64_t r_get_be64(const void *buf);
  202. #define get_be64 r_get_be64
  203. void r_swap_in_place(void *buf, int len);
  204. #define swap_in_place r_swap_in_place
  205. void r_swap_buf(uint8_t *dst, const uint8_t *src, int len);
  206. #define swap_buf r_swap_buf
  207. #else
  208. void put_le16(void *buf, uint16_t x);
  209. void put_le24(void *buf, uint32_t x);
  210. void put_le32(void *buf, uint32_t x);
  211. void put_le64(void *buf, uint64_t x);
  212. uint16_t get_le16(const void *buf);
  213. uint32_t get_le24(const void *buf);
  214. uint32_t get_le32(const void *buf);
  215. uint64_t get_le64(const void *buf);
  216. void put_be16(void *buf, uint16_t x);
  217. void put_be24(void *buf, uint32_t x);
  218. void put_be32(void *buf, uint32_t x);
  219. void put_be64(void *buf, uint64_t x);
  220. uint16_t get_be16(const void *buf);
  221. uint32_t get_be24(const void *buf);
  222. uint32_t get_be32(const void *buf);
  223. uint64_t get_be64(const void *buf);
  224. void swap_in_place(void *buf, int len);
  225. void swap_buf(uint8_t *dst, const uint8_t *src, int len);
  226. #endif
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif