byteorder.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Taken from libmemcached.
  3. */
  4. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  5. *
  6. * Gearmand client and server library.
  7. *
  8. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  9. * Copyright (C) 2008 Brian Aker, Eric Day
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are
  14. * met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following disclaimer
  21. * in the documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * * The names of its contributors may not be used to endorse or
  25. * promote products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. */
  41. #include <libgearman/common.h>
  42. #ifndef swap64
  43. /* Byte swap a 64-bit number. */
  44. static inline uint64_t swap64(uint64_t in)
  45. {
  46. #ifndef WORDS_BIGENDIAN
  47. /* Little endian, flip the bytes around until someone makes a faster/better
  48. * way to do this. */
  49. uint64_t rv= 0;
  50. uint8_t x= 0;
  51. for(x= 0; x < 8; x++)
  52. {
  53. rv= (rv << 8) | (in & 0xff);
  54. in >>= 8;
  55. }
  56. return rv;
  57. #else
  58. /* big-endian machines don't need byte swapping */
  59. return in;
  60. #endif
  61. }
  62. #endif
  63. uint64_t ntohll(uint64_t value)
  64. {
  65. return swap64(value);
  66. }
  67. uint64_t htonll(uint64_t value)
  68. {
  69. return swap64(value);
  70. }