ntohl.c 810 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* vi: set sw=4 ts=4:
  2. * Functions to convert between host and network byte order.
  3. *
  4. * Copyright (C) 2003-2006 by Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #include <netinet/in.h>
  9. #undef ntohl
  10. #undef ntohs
  11. #undef htonl
  12. #undef htons
  13. #if __BYTE_ORDER != __BIG_ENDIAN && __BYTE_ORDER != __LITTLE_ENDIAN
  14. # error "You seem to have an unsupported byteorder"
  15. #endif
  16. uint32_t ntohl (uint32_t x)
  17. {
  18. #if __BYTE_ORDER == __BIG_ENDIAN
  19. return x;
  20. #else
  21. return __bswap_32(x);
  22. #endif
  23. }
  24. libc_hidden_def(ntohl)
  25. strong_alias(ntohl,htonl)
  26. libc_hidden_def(htonl)
  27. uint16_t ntohs (uint16_t x)
  28. {
  29. #if __BYTE_ORDER == __BIG_ENDIAN
  30. return x;
  31. #else
  32. return __bswap_16(x);
  33. #endif
  34. }
  35. libc_hidden_def(ntohs)
  36. strong_alias(ntohs,htons)
  37. libc_hidden_def(htons)