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);
68 * This function converts a 64-bit integer from the little endian format, as
69 * used in the incoming package to whatever endian format we're using
74 * buf [in] - A pointer to a 8 byte buffer.
76 * Returns the integer.
78 #if defined(HAVE_LONGLONG)
79 unsigned long long Curl_read64_le(unsigned char *buf)
81 return ((unsigned long long)buf[0]) |
82 ((unsigned long long)buf[1] << 8) |
83 ((unsigned long long)buf[2] << 16) |
84 ((unsigned long long)buf[3] << 24) |
85 ((unsigned long long)buf[4] << 32) |
86 ((unsigned long long)buf[5] << 40) |
87 ((unsigned long long)buf[6] << 48) |
88 ((unsigned long long)buf[7] << 56);
91 unsigned __int64 Curl_read64_le(unsigned char *buf)
93 return ((unsigned __int64)buf[0]) | ((unsigned __int64)buf[1] << 8) |
94 ((unsigned __int64)buf[2] << 16) | ((unsigned __int64)buf[3] << 24) |
95 ((unsigned __int64)buf[4] << 32) | ((unsigned __int64)buf[5] << 40) |
96 ((unsigned __int64)buf[6] << 48) | ((unsigned __int64)buf[7] << 56);
103 * This function converts a 16-bit integer from the big endian format, as
104 * used in the incoming package to whatever endian format we're using
109 * buf [in] - A pointer to a 2 byte buffer.
111 * Returns the integer.
113 unsigned short Curl_read16_be(unsigned char *buf)
115 return (unsigned short)(((unsigned short)buf[0] << 8) |
116 ((unsigned short)buf[1]));
122 * This function converts a 32-bit integer from the big endian format, as
123 * used in the incoming package to whatever endian format we're using
128 * buf [in] - A pointer to a 4 byte buffer.
130 * Returns the integer.
132 unsigned int Curl_read32_be(unsigned char *buf)
134 return ((unsigned int)buf[0] << 24) | ((unsigned int)buf[1] << 16) |
135 ((unsigned int)buf[2] << 8) | ((unsigned int)buf[3]);
141 * This function converts a 64-bit integer from the big endian format, as
142 * used in the incoming package to whatever endian format we're using
147 * buf [in] - A pointer to a 8 byte buffer.
149 * Returns the integer.
151 #if defined(HAVE_LONGLONG)
152 unsigned long long Curl_read64_be(unsigned char *buf)
154 return ((unsigned long long)buf[0] << 56) |
155 ((unsigned long long)buf[1] << 48) |
156 ((unsigned long long)buf[2] << 40) |
157 ((unsigned long long)buf[3] << 32) |
158 ((unsigned long long)buf[4] << 24) |
159 ((unsigned long long)buf[5] << 16) |
160 ((unsigned long long)buf[6] << 8) |
161 ((unsigned long long)buf[7]);
164 unsigned __int64 Curl_read64_be(unsigned char *buf)
166 return ((unsigned __int64)buf[0] << 56) | ((unsigned __int64)buf[1] << 48) |
167 ((unsigned __int64)buf[2] << 40) | ((unsigned __int64)buf[3] << 32) |
168 ((unsigned __int64)buf[4] << 24) | ((unsigned __int64)buf[5] << 16) |
169 ((unsigned __int64)buf[6] << 8) | ((unsigned __int64)buf[7]);
176 * This function converts a 16-bit integer from the native endian format,
177 * to little endian format ready for sending down the wire.
181 * value [in] - The 16-bit integer value.
182 * buffer [in] - A pointer to the output buffer.
184 void Curl_write16_le(const short value, unsigned char *buffer)
186 buffer[0] = (char)(value & 0x00FF);
187 buffer[1] = (char)((value & 0xFF00) >> 8);
193 * This function converts a 32-bit integer from the native endian format,
194 * to little endian format ready for sending down the wire.
198 * value [in] - The 32-bit integer value.
199 * buffer [in] - A pointer to the output buffer.
201 void Curl_write32_le(const int value, unsigned char *buffer)
203 buffer[0] = (char)(value & 0x000000FF);
204 buffer[1] = (char)((value & 0x0000FF00) >> 8);
205 buffer[2] = (char)((value & 0x00FF0000) >> 16);
206 buffer[3] = (char)((value & 0xFF000000) >> 24);
209 #if (CURL_SIZEOF_CURL_OFF_T > 4)
213 * This function converts a 64-bit integer from the native endian format,
214 * to little endian format ready for sending down the wire.
218 * value [in] - The 64-bit integer value.
219 * buffer [in] - A pointer to the output buffer.
221 #if defined(HAVE_LONGLONG)
222 void Curl_write64_le(const long long value, unsigned char *buffer)
224 void Curl_write64_le(const __int64 value, unsigned char *buffer)
227 Curl_write32_le((int)value, buffer);
228 Curl_write32_le((int)(value >> 32), buffer + 4);