curl_endian.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "curl_endian.h"
  24. /*
  25. * Curl_read16_le()
  26. *
  27. * This function converts a 16-bit integer from the little endian format, as
  28. * used in the incoming package to whatever endian format we're using
  29. * natively.
  30. *
  31. * Parameters:
  32. *
  33. * buf [in] - A pointer to a 2 byte buffer.
  34. *
  35. * Returns the integer.
  36. */
  37. unsigned short Curl_read16_le(const unsigned char *buf)
  38. {
  39. return (unsigned short)(((unsigned short)buf[0]) |
  40. ((unsigned short)buf[1] << 8));
  41. }
  42. /*
  43. * Curl_read32_le()
  44. *
  45. * This function converts a 32-bit integer from the little endian format, as
  46. * used in the incoming package to whatever endian format we're using
  47. * natively.
  48. *
  49. * Parameters:
  50. *
  51. * buf [in] - A pointer to a 4 byte buffer.
  52. *
  53. * Returns the integer.
  54. */
  55. unsigned int Curl_read32_le(const unsigned char *buf)
  56. {
  57. return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
  58. ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
  59. }
  60. /*
  61. * Curl_read16_be()
  62. *
  63. * This function converts a 16-bit integer from the big endian format, as
  64. * used in the incoming package to whatever endian format we're using
  65. * natively.
  66. *
  67. * Parameters:
  68. *
  69. * buf [in] - A pointer to a 2 byte buffer.
  70. *
  71. * Returns the integer.
  72. */
  73. unsigned short Curl_read16_be(const unsigned char *buf)
  74. {
  75. return (unsigned short)(((unsigned short)buf[0] << 8) |
  76. ((unsigned short)buf[1]));
  77. }