1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 http://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(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(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);
65 #if (CURL_SIZEOF_CURL_OFF_T > 4)
69 * This function converts a 64-bit integer from the little endian format, as
70 * used in the incoming package to whatever endian format we're using
75 * buf [in] - A pointer to a 8 byte buffer.
77 * Returns the integer.
79 #if defined(HAVE_LONGLONG)
80 unsigned long long Curl_read64_le(unsigned char *buf)
82 return ((unsigned long long)buf[0]) |
83 ((unsigned long long)buf[1] << 8) |
84 ((unsigned long long)buf[2] << 16) |
85 ((unsigned long long)buf[3] << 24) |
86 ((unsigned long long)buf[4] << 32) |
87 ((unsigned long long)buf[5] << 40) |
88 ((unsigned long long)buf[6] << 48) |
89 ((unsigned long long)buf[7] << 56);
92 unsigned __int64 Curl_read64_le(unsigned char *buf)
94 return ((unsigned __int64)buf[0]) | ((unsigned __int64)buf[1] << 8) |
95 ((unsigned __int64)buf[2] << 16) | ((unsigned __int64)buf[3] << 24) |
96 ((unsigned __int64)buf[4] << 32) | ((unsigned __int64)buf[5] << 40) |
97 ((unsigned __int64)buf[6] << 48) | ((unsigned __int64)buf[7] << 56);
101 #endif /* CURL_SIZEOF_CURL_OFF_T > 4 */
106 * This function converts a 16-bit integer from the big endian format, as
107 * used in the incoming package to whatever endian format we're using
112 * buf [in] - A pointer to a 2 byte buffer.
114 * Returns the integer.
116 unsigned short Curl_read16_be(unsigned char *buf)
118 return (unsigned short)(((unsigned short)buf[0] << 8) |
119 ((unsigned short)buf[1]));
125 * This function converts a 32-bit integer from the big endian format, as
126 * used in the incoming package to whatever endian format we're using
131 * buf [in] - A pointer to a 4 byte buffer.
133 * Returns the integer.
135 unsigned int Curl_read32_be(unsigned char *buf)
137 return ((unsigned int)buf[0] << 24) | ((unsigned int)buf[1] << 16) |
138 ((unsigned int)buf[2] << 8) | ((unsigned int)buf[3]);
141 #if (CURL_SIZEOF_CURL_OFF_T > 4)
145 * This function converts a 64-bit integer from the big endian format, as
146 * used in the incoming package to whatever endian format we're using
151 * buf [in] - A pointer to a 8 byte buffer.
153 * Returns the integer.
155 #if defined(HAVE_LONGLONG)
156 unsigned long long Curl_read64_be(unsigned char *buf)
158 return ((unsigned long long)buf[0] << 56) |
159 ((unsigned long long)buf[1] << 48) |
160 ((unsigned long long)buf[2] << 40) |
161 ((unsigned long long)buf[3] << 32) |
162 ((unsigned long long)buf[4] << 24) |
163 ((unsigned long long)buf[5] << 16) |
164 ((unsigned long long)buf[6] << 8) |
165 ((unsigned long long)buf[7]);
168 unsigned __int64 Curl_read64_be(unsigned char *buf)
170 return ((unsigned __int64)buf[0] << 56) | ((unsigned __int64)buf[1] << 48) |
171 ((unsigned __int64)buf[2] << 40) | ((unsigned __int64)buf[3] << 32) |
172 ((unsigned __int64)buf[4] << 24) | ((unsigned __int64)buf[5] << 16) |
173 ((unsigned __int64)buf[6] << 8) | ((unsigned __int64)buf[7]);
177 #endif /* CURL_SIZEOF_CURL_OFF_T > 4 */
182 * This function converts a 16-bit integer from the native endian format,
183 * to little endian format ready for sending down the wire.
187 * value [in] - The 16-bit integer value.
188 * buffer [in] - A pointer to the output buffer.
190 void Curl_write16_le(const short value, unsigned char *buffer)
192 buffer[0] = (char)(value & 0x00FF);
193 buffer[1] = (char)((value & 0xFF00) >> 8);
199 * This function converts a 32-bit integer from the native endian format,
200 * to little endian format ready for sending down the wire.
204 * value [in] - The 32-bit integer value.
205 * buffer [in] - A pointer to the output buffer.
207 void Curl_write32_le(const int value, unsigned char *buffer)
209 buffer[0] = (char)(value & 0x000000FF);
210 buffer[1] = (char)((value & 0x0000FF00) >> 8);
211 buffer[2] = (char)((value & 0x00FF0000) >> 16);
212 buffer[3] = (char)((value & 0xFF000000) >> 24);
215 #if (CURL_SIZEOF_CURL_OFF_T > 4)
219 * This function converts a 64-bit integer from the native endian format,
220 * to little endian format ready for sending down the wire.
224 * value [in] - The 64-bit integer value.
225 * buffer [in] - A pointer to the output buffer.
227 #if defined(HAVE_LONGLONG)
228 void Curl_write64_le(const long long value, unsigned char *buffer)
230 void Curl_write64_le(const __int64 value, unsigned char *buffer)
233 Curl_write32_le((int)value, buffer);
234 Curl_write32_le((int)(value >> 32), buffer + 4);
236 #endif /* CURL_SIZEOF_CURL_OFF_T > 4 */