byteorder.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "gear_config.h"
  42. #include <libgearman/common.h>
  43. #ifndef swap64
  44. /* Byte swap a 64-bit number. */
  45. static inline uint64_t swap64(uint64_t in)
  46. {
  47. #ifndef WORDS_BIGENDIAN
  48. /* Little endian, flip the bytes around until someone makes a faster/better
  49. * way to do this. */
  50. uint64_t rv= 0;
  51. uint8_t x= 0;
  52. for(x= 0; x < 8; x++)
  53. {
  54. rv= (rv << 8) | (in & 0xff);
  55. in >>= 8;
  56. }
  57. return rv;
  58. #else
  59. /* big-endian machines don't need byte swapping */
  60. return in;
  61. #endif
  62. }
  63. #endif
  64. #ifndef HAVE_HTONLL
  65. uint64_t ntohll(uint64_t value)
  66. {
  67. return swap64(value);
  68. }
  69. uint64_t htonll(uint64_t value)
  70. {
  71. return swap64(value);
  72. }
  73. #endif