byte_order.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef __BYTE_ORDER_H__
  2. #define __BYTE_ORDER_H__
  3. // FROM sha2.h:
  4. /*
  5. * BYTE_ORDER NOTE:
  6. *
  7. * Please make sure that your system defines BYTE_ORDER. If your
  8. * architecture is little-endian, make sure it also defines
  9. * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
  10. * equivalent.
  11. *
  12. * If your system does not define the above, then you can do so by
  13. * hand like this:
  14. *
  15. * #define LITTLE_ENDIAN 1234
  16. * #define BIG_ENDIAN 4321
  17. *
  18. * And for little-endian machines, add:
  19. *
  20. * #define BYTE_ORDER LITTLE_ENDIAN
  21. *
  22. * Or for big-endian machines:
  23. *
  24. * #define BYTE_ORDER BIG_ENDIAN
  25. *
  26. * The FreeBSD machine this was written on defines BYTE_ORDER
  27. * appropriately by including <sys/types.h> (which in turn includes
  28. * <machine/endian.h> where the appropriate definitions are actually
  29. * made).
  30. */
  31. #ifndef LITTLE_ENDIAN
  32. #define LITTLE_ENDIAN 1234
  33. #define BIG_ENDIAN 4321
  34. #endif
  35. #ifndef BYTE_ORDER
  36. #define BYTE_ORDER LITTLE_ENDIAN
  37. #endif
  38. #define REVERSE32(w, x) \
  39. { \
  40. uint32_t tmp = (w); \
  41. tmp = (tmp >> 16) | (tmp << 16); \
  42. (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
  43. }
  44. #define REVERSE64(w, x) \
  45. { \
  46. uint64_t tmp = (w); \
  47. tmp = (tmp >> 32) | (tmp << 32); \
  48. tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
  49. (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | ((tmp & 0x0000ffff0000ffffULL) << 16); \
  50. }
  51. #endif