ntohl.c 948 B

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