1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2021, 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  ***************************************************************************/
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  */
Curl_read16_le(const unsigned char * buf)40 unsigned short Curl_read16_le(const 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  */
Curl_read32_le(const unsigned char * buf)59 unsigned int Curl_read32_le(const 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_read16_be()
67  *
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
70  * natively.
71  *
72  * Parameters:
73  *
74  * buf      [in]     - A pointer to a 2 byte buffer.
75  *
76  * Returns the integer.
77  */
Curl_read16_be(const unsigned char * buf)78 unsigned short Curl_read16_be(const unsigned char *buf)
79 {
80   return (unsigned short)(((unsigned short)buf[0] << 8) |
81                           ((unsigned short)buf[1]));
82 }
83