bswap.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. printf ("bswap_32: %x > %x\n",x,res);
  33. return res;
  34. }
  35. static __inline__ uint16_t bswap_16(uint16_t x)
  36. {
  37. uint16_t res;
  38. swab((void*)&x, (void*)&res, sizeof(uint16_t));
  39. printf ("bswap_32: %x > %x\n",x,res);
  40. return res;
  41. }
  42. #endif
  43. #endif