bswap.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef _BSWAP_H
  2. #define _BSWAP_H 1
  3. #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
  4. # define __BYTE_ORDER = BYTE_ORDER
  5. #endif
  6. #ifndef __BYTE_ORDER
  7. #ifdef __linux__
  8. #include <endian.h>
  9. #else
  10. #define __LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
  11. #define __BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
  12. #define __PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
  13. #if defined(sun386) || defined(i386)
  14. #define __BYTE_ORDER __LITTLE_ENDIAN
  15. #endif
  16. #if defined(sparc)
  17. #define __BYTE_ORDER __BIG_ENDIAN
  18. #endif
  19. #endif /* __linux__ */
  20. #endif /* __BYTE_ORDER */
  21. #ifndef __BYTE_ORDER
  22. # error "Undefined __BYTE_ORDER"
  23. #endif
  24. #ifdef __linux__
  25. #include <byteswap.h>
  26. #else
  27. #include <string.h>
  28. static __inline__ uint32_t bswap_32(uint32_t x)
  29. {
  30. uint32_t res;
  31. swab((void*)&x, (void*)&res, sizeof(uint32_t));
  32. return res;
  33. }
  34. static __inline__ uint16_t bswap_16(uint16_t x)
  35. {
  36. uint16_t res;
  37. swab((void*)&x, (void*)&res, sizeof(uint16_t));
  38. return res;
  39. }
  40. #endif
  41. #endif