byteorder.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <stdint.h>
  34. static uint16_t badvpn_reverse16 (uint16_t x)
  35. {
  36. uint16_t y;
  37. *((uint8_t *)&y+0) = *((uint8_t *)&x+1);
  38. *((uint8_t *)&y+1) = *((uint8_t *)&x+0);
  39. return y;
  40. }
  41. static uint32_t badvpn_reverse32 (uint32_t x)
  42. {
  43. uint32_t y;
  44. *((uint8_t *)&y+0) = *((uint8_t *)&x+3);
  45. *((uint8_t *)&y+1) = *((uint8_t *)&x+2);
  46. *((uint8_t *)&y+2) = *((uint8_t *)&x+1);
  47. *((uint8_t *)&y+3) = *((uint8_t *)&x+0);
  48. return y;
  49. }
  50. static uint64_t badvpn_reverse64 (uint64_t x)
  51. {
  52. uint64_t y;
  53. *((uint8_t *)&y+0) = *((uint8_t *)&x+7);
  54. *((uint8_t *)&y+1) = *((uint8_t *)&x+6);
  55. *((uint8_t *)&y+2) = *((uint8_t *)&x+5);
  56. *((uint8_t *)&y+3) = *((uint8_t *)&x+4);
  57. *((uint8_t *)&y+4) = *((uint8_t *)&x+3);
  58. *((uint8_t *)&y+5) = *((uint8_t *)&x+2);
  59. *((uint8_t *)&y+6) = *((uint8_t *)&x+1);
  60. *((uint8_t *)&y+7) = *((uint8_t *)&x+0);
  61. return y;
  62. }
  63. static uint8_t hton8 (uint8_t x)
  64. {
  65. return x;
  66. }
  67. static uint8_t htol8 (uint8_t x)
  68. {
  69. return x;
  70. }
  71. #if defined(BADVPN_LITTLE_ENDIAN)
  72. static uint16_t hton16 (uint16_t x)
  73. {
  74. return badvpn_reverse16(x);
  75. }
  76. static uint32_t hton32 (uint32_t x)
  77. {
  78. return badvpn_reverse32(x);
  79. }
  80. static uint64_t hton64 (uint64_t x)
  81. {
  82. return badvpn_reverse64(x);
  83. }
  84. static uint16_t htol16 (uint16_t x)
  85. {
  86. return x;
  87. }
  88. static uint32_t htol32 (uint32_t x)
  89. {
  90. return x;
  91. }
  92. static uint64_t htol64 (uint64_t x)
  93. {
  94. return x;
  95. }
  96. #elif defined(BADVPN_BIG_ENDIAN)
  97. static uint16_t hton16 (uint16_t x)
  98. {
  99. return x;
  100. }
  101. static uint32_t hton32 (uint32_t x)
  102. {
  103. return x;
  104. }
  105. static uint64_t hton64 (uint64_t x)
  106. {
  107. return x;
  108. }
  109. static uint16_t htol16 (uint16_t x)
  110. {
  111. return badvpn_reverse16(x);
  112. }
  113. static uint32_t htol32 (uint32_t x)
  114. {
  115. return badvpn_reverse32(x);
  116. }
  117. static uint64_t htol64 (uint64_t x)
  118. {
  119. return badvpn_reverse64(x);
  120. }
  121. #endif
  122. static uint8_t ntoh8 (uint8_t x)
  123. {
  124. return hton8(x);
  125. }
  126. static uint16_t ntoh16 (uint16_t x)
  127. {
  128. return hton16(x);
  129. }
  130. static uint32_t ntoh32 (uint32_t x)
  131. {
  132. return hton32(x);
  133. }
  134. static uint64_t ntoh64 (uint64_t x)
  135. {
  136. return hton64(x);
  137. }
  138. static uint8_t ltoh8 (uint8_t x)
  139. {
  140. return htol8(x);
  141. }
  142. static uint16_t ltoh16 (uint16_t x)
  143. {
  144. return htol16(x);
  145. }
  146. static uint32_t ltoh32 (uint32_t x)
  147. {
  148. return htol32(x);
  149. }
  150. static uint64_t ltoh64 (uint64_t x)
  151. {
  152. return htol64(x);
  153. }
  154. #endif