endianops.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #ifndef _UCSI_COMMON_H
  21. #define _UCSI_COMMON_H 1
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <stdint.h>
  27. #include <byteswap.h>
  28. #include <endian.h>
  29. #define __ucsi_packed __attribute__((packed))
  30. #if __BYTE_ORDER == __BIG_ENDIAN
  31. #define EBIT2(x1,x2) x1 x2
  32. #define EBIT3(x1,x2,x3) x1 x2 x3
  33. #define EBIT4(x1,x2,x3,x4) x1 x2 x3 x4
  34. #define EBIT5(x1,x2,x3,x4,x5) x1 x2 x3 x4 x5
  35. #define EBIT6(x1,x2,x3,x4,x5,x6) x1 x2 x3 x4 x5 x6
  36. #define EBIT7(x1,x2,x3,x4,x5,x6,x7) x1 x2 x3 x4 x5 x6 x7
  37. #define EBIT8(x1,x2,x3,x4,x5,x6,x7,x8) x1 x2 x3 x4 x5 x6 x7 x8
  38. static inline void bswap16(uint8_t *buf) {
  39. (void) buf;
  40. }
  41. static inline void bswap32(uint8_t *buf) {
  42. (void) buf;
  43. }
  44. static inline void bswap64(uint8_t *buf) {
  45. (void) buf;
  46. }
  47. static inline void bswap24(uint8_t *buf) {
  48. (void) buf;
  49. }
  50. static inline void bswap40(uint8_t *buf) {
  51. (void) buf;
  52. }
  53. static inline void bswap48(uint8_t *buf) {
  54. (void) buf;
  55. }
  56. #else
  57. #define EBIT2(x1,x2) x2 x1
  58. #define EBIT3(x1,x2,x3) x3 x2 x1
  59. #define EBIT4(x1,x2,x3,x4) x4 x3 x2 x1
  60. #define EBIT5(x1,x2,x3,x4,x5) x5 x4 x3 x2 x1
  61. #define EBIT6(x1,x2,x3,x4,x5,x6) x6 x5 x4 x3 x2 x1
  62. #define EBIT7(x1,x2,x3,x4,x5,x6,x7) x7 x6 x5 x4 x3 x2 x1
  63. #define EBIT8(x1,x2,x3,x4,x5,x6,x7,x8) x8 x7 x6 x5 x4 x3 x2 x1
  64. static inline void bswap16(uint8_t * buf) {
  65. *((uint16_t*)buf) = bswap_16((*(uint16_t*)buf));
  66. }
  67. static inline void bswap32(uint8_t * buf) {
  68. *((uint32_t*)buf) = bswap_32((*(uint32_t*)buf));
  69. }
  70. static inline void bswap64(uint8_t * buf) {
  71. *((uint64_t*)buf) = bswap_64((*(uint64_t*)buf));
  72. }
  73. static inline void bswap24(uint8_t * buf) {
  74. uint8_t tmp0 = buf[0];
  75. buf[0] = buf[2];
  76. buf[2] = tmp0;
  77. }
  78. static inline void bswap40(uint8_t * buf) {
  79. uint8_t tmp0 = buf[0];
  80. uint8_t tmp1 = buf[1];
  81. buf[0] = buf[4];
  82. buf[1] = buf[3];
  83. buf[3] = tmp1;
  84. buf[4] = tmp0;
  85. }
  86. static inline void bswap48(uint8_t * buf) {
  87. uint8_t tmp0 = buf[0];
  88. uint8_t tmp1 = buf[1];
  89. uint8_t tmp2 = buf[2];
  90. buf[0] = buf[5];
  91. buf[1] = buf[4];
  92. buf[2] = buf[3];
  93. buf[3] = tmp2;
  94. buf[4] = tmp1;
  95. buf[5] = tmp0;
  96. }
  97. #endif // __BYTE_ORDER
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif