byteorder.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @file byteorder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Byte order conversion functions.
  25. *
  26. * hton* functions convert from host to big-endian (network) byte order.
  27. * htol* functions convert from host to little-endian byte order.
  28. * ntoh* functions convert from big-endian (network) to host byte order.
  29. * ltoh* functions convert from little-endian to host byte order.
  30. */
  31. #ifndef BADVPN_MISC_BYTEORDER_H
  32. #define BADVPN_MISC_BYTEORDER_H
  33. #if (defined(BADVPN_LITTLE_ENDIAN) + defined(BADVPN_BIG_ENDIAN)) != 1
  34. #error Unknown byte order or too many byte orders
  35. #endif
  36. #include <stdint.h>
  37. static uint16_t badvpn_reverse16 (uint16_t x)
  38. {
  39. uint16_t y;
  40. *((uint8_t *)&y+0) = *((uint8_t *)&x+1);
  41. *((uint8_t *)&y+1) = *((uint8_t *)&x+0);
  42. return y;
  43. }
  44. static uint32_t badvpn_reverse32 (uint32_t x)
  45. {
  46. uint32_t y;
  47. *((uint8_t *)&y+0) = *((uint8_t *)&x+3);
  48. *((uint8_t *)&y+1) = *((uint8_t *)&x+2);
  49. *((uint8_t *)&y+2) = *((uint8_t *)&x+1);
  50. *((uint8_t *)&y+3) = *((uint8_t *)&x+0);
  51. return y;
  52. }
  53. static uint64_t badvpn_reverse64 (uint64_t x)
  54. {
  55. uint64_t y;
  56. *((uint8_t *)&y+0) = *((uint8_t *)&x+7);
  57. *((uint8_t *)&y+1) = *((uint8_t *)&x+6);
  58. *((uint8_t *)&y+2) = *((uint8_t *)&x+5);
  59. *((uint8_t *)&y+3) = *((uint8_t *)&x+4);
  60. *((uint8_t *)&y+4) = *((uint8_t *)&x+3);
  61. *((uint8_t *)&y+5) = *((uint8_t *)&x+2);
  62. *((uint8_t *)&y+6) = *((uint8_t *)&x+1);
  63. *((uint8_t *)&y+7) = *((uint8_t *)&x+0);
  64. return y;
  65. }
  66. static uint8_t hton8 (uint8_t x)
  67. {
  68. return x;
  69. }
  70. static uint8_t htol8 (uint8_t x)
  71. {
  72. return x;
  73. }
  74. #if defined(BADVPN_LITTLE_ENDIAN)
  75. static uint16_t hton16 (uint16_t x)
  76. {
  77. return badvpn_reverse16(x);
  78. }
  79. static uint32_t hton32 (uint32_t x)
  80. {
  81. return badvpn_reverse32(x);
  82. }
  83. static uint64_t hton64 (uint64_t x)
  84. {
  85. return badvpn_reverse64(x);
  86. }
  87. static uint16_t htol16 (uint16_t x)
  88. {
  89. return x;
  90. }
  91. static uint32_t htol32 (uint32_t x)
  92. {
  93. return x;
  94. }
  95. static uint64_t htol64 (uint64_t x)
  96. {
  97. return x;
  98. }
  99. #elif defined(BADVPN_BIG_ENDIAN)
  100. static uint16_t hton16 (uint16_t x)
  101. {
  102. return x;
  103. }
  104. static uint32_t hton32 (uint32_t x)
  105. {
  106. return x;
  107. }
  108. static uint64_t hton64 (uint64_t x)
  109. {
  110. return x;
  111. }
  112. static uint16_t htol16 (uint16_t x)
  113. {
  114. return badvpn_reverse16(x);
  115. }
  116. static uint32_t htol32 (uint32_t x)
  117. {
  118. return badvpn_reverse32(x);
  119. }
  120. static uint64_t htol64 (uint64_t x)
  121. {
  122. return badvpn_reverse64(x);
  123. }
  124. #endif
  125. static uint8_t ntoh8 (uint8_t x)
  126. {
  127. return hton8(x);
  128. }
  129. static uint16_t ntoh16 (uint16_t x)
  130. {
  131. return hton16(x);
  132. }
  133. static uint32_t ntoh32 (uint32_t x)
  134. {
  135. return hton32(x);
  136. }
  137. static uint64_t ntoh64 (uint64_t x)
  138. {
  139. return hton64(x);
  140. }
  141. static uint8_t ltoh8 (uint8_t x)
  142. {
  143. return htol8(x);
  144. }
  145. static uint16_t ltoh16 (uint16_t x)
  146. {
  147. return htol16(x);
  148. }
  149. static uint32_t ltoh32 (uint32_t x)
  150. {
  151. return htol32(x);
  152. }
  153. static uint64_t ltoh64 (uint64_t x)
  154. {
  155. return htol64(x);
  156. }
  157. #endif