1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include "curl_endian.h"
30 * This function converts a 16-bit integer from the little endian format, as
31 * used in the incoming package to whatever endian format we're using
36 * buf [in] - A pointer to a 2 byte buffer.
38 * Returns the integer.
40 unsigned short Curl_read16_le(const unsigned char *buf)
42 return (unsigned short)(((unsigned short)buf[0]) |
43 ((unsigned short)buf[1] << 8));
49 * This function converts a 32-bit integer from the little endian format, as
50 * used in the incoming package to whatever endian format we're using
55 * buf [in] - A pointer to a 4 byte buffer.
57 * Returns the integer.
59 unsigned int Curl_read32_le(const unsigned char *buf)
61 return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
62 ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
68 * This function converts a 16-bit integer from the big endian format, as
69 * used in the incoming package to whatever endian format we're using
74 * buf [in] - A pointer to a 2 byte buffer.
76 * Returns the integer.
78 unsigned short Curl_read16_be(const unsigned char *buf)
80 return (unsigned short)(((unsigned short)buf[0] << 8) |
81 ((unsigned short)buf[1]));
84 #if (CURL_SIZEOF_CURL_OFF_T > 4)
88 * This function converts a 32-bit integer from the native endian format,
89 * to little endian format ready for sending down the wire.
93 * value [in] - The 32-bit integer value.
94 * buffer [in] - A pointer to the output buffer.
96 static void write32_le(const int value, unsigned char *buffer)
98 buffer[0] = (char)(value & 0x000000FF);
99 buffer[1] = (char)((value & 0x0000FF00) >> 8);
100 buffer[2] = (char)((value & 0x00FF0000) >> 16);
101 buffer[3] = (char)((value & 0xFF000000) >> 24);
107 * This function converts a 64-bit integer from the native endian format,
108 * to little endian format ready for sending down the wire.
112 * value [in] - The 64-bit integer value.
113 * buffer [in] - A pointer to the output buffer.
115 #if defined(HAVE_LONGLONG)
116 void Curl_write64_le(const long long value, unsigned char *buffer)
118 void Curl_write64_le(const __int64 value, unsigned char *buffer)
121 write32_le((int)value, buffer);
122 write32_le((int)(value >> 32), buffer + 4);
124 #endif /* CURL_SIZEOF_CURL_OFF_T > 4 */