byteorder.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file byteorder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Byte order conversion functions.
  32. *
  33. * hton* functions convert from host to big-endian (network) byte order.
  34. * htol* functions convert from host to little-endian byte order.
  35. * ntoh* functions convert from big-endian (network) to host byte order.
  36. * ltoh* functions convert from little-endian to host byte order.
  37. */
  38. #ifndef BADVPN_MISC_BYTEORDER_H
  39. #define BADVPN_MISC_BYTEORDER_H
  40. #if (defined(BADVPN_LITTLE_ENDIAN) + defined(BADVPN_BIG_ENDIAN)) != 1
  41. #error Unknown byte order or too many byte orders
  42. #endif
  43. #include <stdint.h>
  44. static uint16_t badvpn_reverse16 (uint16_t x)
  45. {
  46. uint16_t y;
  47. *((uint8_t *)&y+0) = *((uint8_t *)&x+1);
  48. *((uint8_t *)&y+1) = *((uint8_t *)&x+0);
  49. return y;
  50. }
  51. static uint32_t badvpn_reverse32 (uint32_t x)
  52. {
  53. uint32_t y;
  54. *((uint8_t *)&y+0) = *((uint8_t *)&x+3);
  55. *((uint8_t *)&y+1) = *((uint8_t *)&x+2);
  56. *((uint8_t *)&y+2) = *((uint8_t *)&x+1);
  57. *((uint8_t *)&y+3) = *((uint8_t *)&x+0);
  58. return y;
  59. }
  60. static uint64_t badvpn_reverse64 (uint64_t x)
  61. {
  62. uint64_t y;
  63. *((uint8_t *)&y+0) = *((uint8_t *)&x+7);
  64. *((uint8_t *)&y+1) = *((uint8_t *)&x+6);
  65. *((uint8_t *)&y+2) = *((uint8_t *)&x+5);
  66. *((uint8_t *)&y+3) = *((uint8_t *)&x+4);
  67. *((uint8_t *)&y+4) = *((uint8_t *)&x+3);
  68. *((uint8_t *)&y+5) = *((uint8_t *)&x+2);
  69. *((uint8_t *)&y+6) = *((uint8_t *)&x+1);
  70. *((uint8_t *)&y+7) = *((uint8_t *)&x+0);
  71. return y;
  72. }
  73. static uint8_t hton8 (uint8_t x)
  74. {
  75. return x;
  76. }
  77. static uint8_t htol8 (uint8_t x)
  78. {
  79. return x;
  80. }
  81. #if defined(BADVPN_LITTLE_ENDIAN)
  82. static uint16_t hton16 (uint16_t x)
  83. {
  84. return badvpn_reverse16(x);
  85. }
  86. static uint32_t hton32 (uint32_t x)
  87. {
  88. return badvpn_reverse32(x);
  89. }
  90. static uint64_t hton64 (uint64_t x)
  91. {
  92. return badvpn_reverse64(x);
  93. }
  94. static uint16_t htol16 (uint16_t x)
  95. {
  96. return x;
  97. }
  98. static uint32_t htol32 (uint32_t x)
  99. {
  100. return x;
  101. }
  102. static uint64_t htol64 (uint64_t x)
  103. {
  104. return x;
  105. }
  106. #elif defined(BADVPN_BIG_ENDIAN)
  107. static uint16_t hton16 (uint16_t x)
  108. {
  109. return x;
  110. }
  111. static uint32_t hton32 (uint32_t x)
  112. {
  113. return x;
  114. }
  115. static uint64_t hton64 (uint64_t x)
  116. {
  117. return x;
  118. }
  119. static uint16_t htol16 (uint16_t x)
  120. {
  121. return badvpn_reverse16(x);
  122. }
  123. static uint32_t htol32 (uint32_t x)
  124. {
  125. return badvpn_reverse32(x);
  126. }
  127. static uint64_t htol64 (uint64_t x)
  128. {
  129. return badvpn_reverse64(x);
  130. }
  131. #endif
  132. static uint8_t ntoh8 (uint8_t x)
  133. {
  134. return hton8(x);
  135. }
  136. static uint16_t ntoh16 (uint16_t x)
  137. {
  138. return hton16(x);
  139. }
  140. static uint32_t ntoh32 (uint32_t x)
  141. {
  142. return hton32(x);
  143. }
  144. static uint64_t ntoh64 (uint64_t x)
  145. {
  146. return hton64(x);
  147. }
  148. static uint8_t ltoh8 (uint8_t x)
  149. {
  150. return htol8(x);
  151. }
  152. static uint16_t ltoh16 (uint16_t x)
  153. {
  154. return htol16(x);
  155. }
  156. static uint32_t ltoh32 (uint32_t x)
  157. {
  158. return htol32(x);
  159. }
  160. static uint64_t ltoh64 (uint64_t x)
  161. {
  162. return htol64(x);
  163. }
  164. #endif