ntohl.c 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <stdint.h>
  9. #include <endian.h>
  10. #include <byteswap.h>
  11. #include <netinet/in.h>
  12. #undef ntohl
  13. #undef ntohs
  14. #undef htonl
  15. #undef htons
  16. #if __BYTE_ORDER == __BIG_ENDIAN
  17. uint32_t ntohl (uint32_t x)
  18. {
  19. return x;
  20. }
  21. uint16_t ntohs (uint16_t x)
  22. {
  23. return x;
  24. }
  25. uint32_t htonl (uint32_t x)
  26. {
  27. return x;
  28. }
  29. uint16_t htons (uint16_t x)
  30. {
  31. return x;
  32. }
  33. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  34. uint32_t ntohl (uint32_t x)
  35. {
  36. return __bswap_32(x);
  37. }
  38. uint16_t ntohs (uint16_t x)
  39. {
  40. return __bswap_16(x);
  41. }
  42. uint32_t htonl (uint32_t x)
  43. {
  44. return __bswap_32(x);
  45. }
  46. uint16_t htons (uint16_t x)
  47. {
  48. return __bswap_16(x);
  49. }
  50. #else
  51. #error "You seem to have an unsupported byteorder"
  52. #endif
  53. libc_hidden_def(ntohl)
  54. libc_hidden_def(ntohs)
  55. libc_hidden_def(htonl)
  56. libc_hidden_def(htons)