ntohl.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vi: set sw=4 ts=4:
  2. * Functions to convert between host and network byte order.
  3. *
  4. * Copyright (C) 2003 by Erik Andersen <andersen@uclibc.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Library General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <stdint.h>
  21. #include <endian.h>
  22. #include <byteswap.h>
  23. #if __BYTE_ORDER == __BIG_ENDIAN
  24. extern uint32_t ntohl (uint32_t x)
  25. {
  26. return x;
  27. }
  28. extern uint16_t ntohs (uint16_t x)
  29. {
  30. return x;
  31. }
  32. extern uint32_t htonl (uint32_t x)
  33. {
  34. return x;
  35. }
  36. extern uint16_t htons (uint16_t x)
  37. {
  38. return x;
  39. }
  40. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  41. extern uint32_t ntohl (uint32_t x)
  42. {
  43. return __bswap_32(x);
  44. }
  45. extern uint16_t ntohs (uint16_t x)
  46. {
  47. return __bswap_16(x);
  48. }
  49. extern uint32_t htonl (uint32_t x)
  50. {
  51. return __bswap_32(x);
  52. }
  53. extern uint16_t htons (uint16_t x)
  54. {
  55. return __bswap_16(x);
  56. }
  57. #else
  58. #error "You seem to have an unsupported byteorder"
  59. #endif