/release/src/include/bcmendian.h

https://github.com/SgtPepperKSU/TomatoVPN · C Header · 168 lines · 122 code · 16 blank · 30 comment · 3 complexity · 5385b9d9219f673aeac30f7cf542809e MD5 · raw file

  1. /*
  2. * local version of endian.h - byte order defines
  3. *
  4. * Copyright 2005, Broadcom Corporation
  5. * All Rights Reserved.
  6. *
  7. * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
  8. * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
  9. * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
  10. * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
  11. *
  12. * $Id: bcmendian.h,v 1.1.1.8 2005/03/07 07:31:12 kanki Exp $
  13. */
  14. #ifndef _BCMENDIAN_H_
  15. #define _BCMENDIAN_H_
  16. #include <typedefs.h>
  17. /* Byte swap a 16 bit value */
  18. #define BCMSWAP16(val) \
  19. ((uint16)( \
  20. (((uint16)(val) & (uint16)0x00ffU) << 8) | \
  21. (((uint16)(val) & (uint16)0xff00U) >> 8) ))
  22. /* Byte swap a 32 bit value */
  23. #define BCMSWAP32(val) \
  24. ((uint32)( \
  25. (((uint32)(val) & (uint32)0x000000ffUL) << 24) | \
  26. (((uint32)(val) & (uint32)0x0000ff00UL) << 8) | \
  27. (((uint32)(val) & (uint32)0x00ff0000UL) >> 8) | \
  28. (((uint32)(val) & (uint32)0xff000000UL) >> 24) ))
  29. static INLINE uint16
  30. bcmswap16(uint16 val)
  31. {
  32. return BCMSWAP16(val);
  33. }
  34. static INLINE uint32
  35. bcmswap32(uint32 val)
  36. {
  37. return BCMSWAP32(val);
  38. }
  39. /* buf - start of buffer of shorts to swap */
  40. /* len - byte length of buffer */
  41. static INLINE void
  42. bcmswap16_buf(uint16 *buf, uint len)
  43. {
  44. len = len/2;
  45. while(len--){
  46. *buf = bcmswap16(*buf);
  47. buf++;
  48. }
  49. }
  50. #ifndef hton16
  51. #ifndef IL_BIGENDIAN
  52. #define HTON16(i) BCMSWAP16(i)
  53. #define hton16(i) bcmswap16(i)
  54. #define hton32(i) bcmswap32(i)
  55. #define ntoh16(i) bcmswap16(i)
  56. #define ntoh32(i) bcmswap32(i)
  57. #define ltoh16(i) (i)
  58. #define ltoh32(i) (i)
  59. #define htol16(i) (i)
  60. #define htol32(i) (i)
  61. #else
  62. #define HTON16(i) (i)
  63. #define hton16(i) (i)
  64. #define hton32(i) (i)
  65. #define ntoh16(i) (i)
  66. #define ntoh32(i) (i)
  67. #define ltoh16(i) bcmswap16(i)
  68. #define ltoh32(i) bcmswap32(i)
  69. #define htol16(i) bcmswap16(i)
  70. #define htol32(i) bcmswap32(i)
  71. #endif
  72. #endif
  73. #ifndef IL_BIGENDIAN
  74. #define ltoh16_buf(buf, i)
  75. #define htol16_buf(buf, i)
  76. #else
  77. #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
  78. #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
  79. #endif
  80. /*
  81. * load 16-bit value from unaligned little endian byte array.
  82. */
  83. static INLINE uint16
  84. ltoh16_ua(uint8 *bytes)
  85. {
  86. return (bytes[1]<<8)+bytes[0];
  87. }
  88. /*
  89. * load 32-bit value from unaligned little endian byte array.
  90. */
  91. static INLINE uint32
  92. ltoh32_ua(uint8 *bytes)
  93. {
  94. return (bytes[3]<<24)+(bytes[2]<<16)+(bytes[1]<<8)+bytes[0];
  95. }
  96. /*
  97. * load 16-bit value from unaligned big(network) endian byte array.
  98. */
  99. static INLINE uint16
  100. ntoh16_ua(uint8 *bytes)
  101. {
  102. return (bytes[0]<<8)+bytes[1];
  103. }
  104. /*
  105. * load 32-bit value from unaligned big(network) endian byte array.
  106. */
  107. static INLINE uint32
  108. ntoh32_ua(uint8 *bytes)
  109. {
  110. return (bytes[0]<<24)+(bytes[1]<<16)+(bytes[2]<<8)+bytes[3];
  111. }
  112. /* get_ua adapted from Linux asm-mips/unaligned.h */
  113. #ifdef IL_BIGENDIAN
  114. #define get_ua(ptr) \
  115. ({ \
  116. __typeof__(*(ptr)) __val; \
  117. \
  118. switch (sizeof(*(ptr))) { \
  119. case 1: \
  120. __val = *(uint8 *)ptr; \
  121. break; \
  122. case 2: \
  123. __val = ntoh16_ua((uint8 *)ptr); \
  124. break; \
  125. case 4: \
  126. __val = ntoh32_ua((uint8 *)ptr); \
  127. break; \
  128. } \
  129. \
  130. __val; \
  131. })
  132. #else
  133. #define get_ua(ptr) \
  134. ({ \
  135. __typeof__(*(ptr)) __val; \
  136. \
  137. switch (sizeof(*(ptr))) { \
  138. case 1: \
  139. __val = *(uint8 *)ptr; \
  140. break; \
  141. case 2: \
  142. __val = ltoh16_ua((uint8 *)ptr); \
  143. break; \
  144. case 4: \
  145. __val = ltoh32_ua((uint8 *)ptr); \
  146. break; \
  147. } \
  148. \
  149. __val; \
  150. })
  151. #endif
  152. #endif /* _BCMENDIAN_H_ */