Revert "Imported Upstream version 7.44.0"
[platform/upstream/curl.git] / lib / curl_endian.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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 http://curl.haxx.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
23 #include "curl_setup.h"
24
25 #include "curl_endian.h"
26
27 /*
28  * Curl_read16_le()
29  *
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
32  * natively.
33  *
34  * Parameters:
35  *
36  * buf      [in]     - A pointer to a 2 byte buffer.
37  *
38  * Returns the integer.
39  */
40 unsigned short Curl_read16_le(unsigned char *buf)
41 {
42   return (unsigned short)(((unsigned short)buf[0]) |
43                           ((unsigned short)buf[1] << 8));
44 }
45
46 /*
47  * Curl_read32_le()
48  *
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
51  * natively.
52  *
53  * Parameters:
54  *
55  * buf      [in]     - A pointer to a 4 byte buffer.
56  *
57  * Returns the integer.
58  */
59 unsigned int Curl_read32_le(unsigned char *buf)
60 {
61   return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
62          ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
63 }
64
65 /*
66  * Curl_read64_le()
67  *
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
70  * natively.
71  *
72  * Parameters:
73  *
74  * buf      [in]     - A pointer to a 8 byte buffer.
75  *
76  * Returns the integer.
77  */
78 #if defined(HAVE_LONGLONG)
79 unsigned long long Curl_read64_le(unsigned char *buf)
80 {
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);
89 }
90 #else
91 unsigned __int64 Curl_read64_le(unsigned char *buf)
92 {
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);
97 }
98 #endif
99
100 /*
101  * Curl_read16_be()
102  *
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
105  * natively.
106  *
107  * Parameters:
108  *
109  * buf      [in]     - A pointer to a 2 byte buffer.
110  *
111  * Returns the integer.
112  */
113 unsigned short Curl_read16_be(unsigned char *buf)
114 {
115   return (unsigned short)(((unsigned short)buf[0] << 8) |
116                           ((unsigned short)buf[1]));
117 }
118
119 /*
120  * Curl_read32_be()
121  *
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
124  * natively.
125  *
126  * Parameters:
127  *
128  * buf      [in]     - A pointer to a 4 byte buffer.
129  *
130  * Returns the integer.
131  */
132 unsigned int Curl_read32_be(unsigned char *buf)
133 {
134   return ((unsigned int)buf[0] << 24) | ((unsigned int)buf[1] << 16) |
135          ((unsigned int)buf[2] << 8) | ((unsigned int)buf[3]);
136 }
137
138 /*
139  * Curl_read64_be()
140  *
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
143  * natively.
144  *
145  * Parameters:
146  *
147  * buf      [in]     - A pointer to a 8 byte buffer.
148  *
149  * Returns the integer.
150  */
151 #if defined(HAVE_LONGLONG)
152 unsigned long long Curl_read64_be(unsigned char *buf)
153 {
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]);
162 }
163 #else
164 unsigned __int64 Curl_read64_be(unsigned char *buf)
165 {
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]);
170 }
171 #endif
172
173 /*
174  * Curl_write16_le()
175  *
176  * This function converts a 16-bit integer from the native endian format,
177  * to little endian format ready for sending down the wire.
178  *
179  * Parameters:
180  *
181  * value    [in]     - The 16-bit integer value.
182  * buffer   [in]     - A pointer to the output buffer.
183  */
184 void Curl_write16_le(const short value, unsigned char *buffer)
185 {
186   buffer[0] = (char)(value & 0x00FF);
187   buffer[1] = (char)((value & 0xFF00) >> 8);
188 }
189
190 /*
191  * Curl_write32_le()
192  *
193  * This function converts a 32-bit integer from the native endian format,
194  * to little endian format ready for sending down the wire.
195  *
196  * Parameters:
197  *
198  * value    [in]     - The 32-bit integer value.
199  * buffer   [in]     - A pointer to the output buffer.
200  */
201 void Curl_write32_le(const int value, unsigned char *buffer)
202 {
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);
207 }
208
209 #if (CURL_SIZEOF_CURL_OFF_T > 4)
210 /*
211  * Curl_write64_le()
212  *
213  * This function converts a 64-bit integer from the native endian format,
214  * to little endian format ready for sending down the wire.
215  *
216  * Parameters:
217  *
218  * value    [in]     - The 64-bit integer value.
219  * buffer   [in]     - A pointer to the output buffer.
220  */
221 #if defined(HAVE_LONGLONG)
222 void Curl_write64_le(const long long value, unsigned char *buffer)
223 #else
224 void Curl_write64_le(const __int64 value, unsigned char *buffer)
225 #endif
226 {
227   Curl_write32_le((int)value, buffer);
228   Curl_write32_le((int)(value >> 32), buffer + 4);
229 }
230 #endif