curl_endian.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "curl_endian.h"
  26. /*
  27. * Curl_read16_le()
  28. *
  29. * This function converts a 16-bit integer from the little endian format, as
  30. * used in the incoming package to whatever endian format we're using
  31. * natively.
  32. *
  33. * Parameters:
  34. *
  35. * buf [in] - A pointer to a 2 byte buffer.
  36. *
  37. * Returns the integer.
  38. */
  39. unsigned short Curl_read16_le(const unsigned char *buf)
  40. {
  41. return (unsigned short)(((unsigned short)buf[0]) |
  42. ((unsigned short)buf[1] << 8));
  43. }
  44. /*
  45. * Curl_read32_le()
  46. *
  47. * This function converts a 32-bit integer from the little endian format, as
  48. * used in the incoming package to whatever endian format we're using
  49. * natively.
  50. *
  51. * Parameters:
  52. *
  53. * buf [in] - A pointer to a 4 byte buffer.
  54. *
  55. * Returns the integer.
  56. */
  57. unsigned int Curl_read32_le(const unsigned char *buf)
  58. {
  59. return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
  60. ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
  61. }
  62. /*
  63. * Curl_read16_be()
  64. *
  65. * This function converts a 16-bit integer from the big endian format, as
  66. * used in the incoming package to whatever endian format we're using
  67. * natively.
  68. *
  69. * Parameters:
  70. *
  71. * buf [in] - A pointer to a 2 byte buffer.
  72. *
  73. * Returns the integer.
  74. */
  75. unsigned short Curl_read16_be(const unsigned char *buf)
  76. {
  77. return (unsigned short)(((unsigned short)buf[0] << 8) |
  78. ((unsigned short)buf[1]));
  79. }