byteorder.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* <MIT License>
  2. Copyright (c) 2013-2014 Marek Majkowski <marek@popcount.org>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. </MIT License>
  19. Original location:
  20. https://github.com/majek/csiphash/
  21. Solution inspired by code from:
  22. Samuel Neves (supercop/crypto_auth/siphash24/little)
  23. djb (supercop/crypto_auth/siphash24/little2)
  24. Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
  25. */
  26. #ifdef HAVE_SYS_PARAM_H
  27. #include <sys/param.h>
  28. #endif
  29. /* This code is extracted from csiphash.h */
  30. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  31. __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  32. # define _le64toh(x) ((uint64_t)(x))
  33. #elif defined(_WIN32)
  34. /* Windows is always little endian, unless you're on xbox360
  35. http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */
  36. # define _le64toh(x) ((uint64_t)(x))
  37. #elif defined(__APPLE__)
  38. # include <libkern/OSByteOrder.h>
  39. # define _le64toh(x) OSSwapLittleToHostInt64(x)
  40. #elif defined(sun) || defined(__sun)
  41. # include <sys/byteorder.h>
  42. # define _le64toh(x) LE_64(x)
  43. #else
  44. /* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
  45. # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(OpenBSD)
  46. # include <sys/endian.h>
  47. # else
  48. # include <endian.h>
  49. # endif
  50. # if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
  51. __BYTE_ORDER == __LITTLE_ENDIAN
  52. # define _le64toh(x) ((uint64_t)(x))
  53. # else
  54. # if defined(OpenBSD)
  55. # define _le64toh(x) letoh64(x)
  56. # else
  57. # define _le64toh(x) le64toh(x)
  58. # endif
  59. # endif
  60. #endif