xref: /curl/lib/curl_endian.c (revision c074ba64)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 #include "curl_setup.h"
26 
27 #include "curl_endian.h"
28 
29 /*
30  * Curl_read16_le()
31  *
32  * This function converts a 16-bit integer from the little endian format, as
33  * used in the incoming package to whatever endian format we are using
34  * natively.
35  *
36  * Parameters:
37  *
38  * buf      [in]     - A pointer to a 2 byte buffer.
39  *
40  * Returns the integer.
41  */
Curl_read16_le(const unsigned char * buf)42 unsigned short Curl_read16_le(const unsigned char *buf)
43 {
44   return (unsigned short)(((unsigned short)buf[0]) |
45                           ((unsigned short)buf[1] << 8));
46 }
47 
48 /*
49  * Curl_read32_le()
50  *
51  * This function converts a 32-bit integer from the little endian format, as
52  * used in the incoming package to whatever endian format we are using
53  * natively.
54  *
55  * Parameters:
56  *
57  * buf      [in]     - A pointer to a 4 byte buffer.
58  *
59  * Returns the integer.
60  */
Curl_read32_le(const unsigned char * buf)61 unsigned int Curl_read32_le(const unsigned char *buf)
62 {
63   return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
64          ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
65 }
66 
67 /*
68  * Curl_read16_be()
69  *
70  * This function converts a 16-bit integer from the big endian format, as
71  * used in the incoming package to whatever endian format we are using
72  * natively.
73  *
74  * Parameters:
75  *
76  * buf      [in]     - A pointer to a 2 byte buffer.
77  *
78  * Returns the integer.
79  */
Curl_read16_be(const unsigned char * buf)80 unsigned short Curl_read16_be(const unsigned char *buf)
81 {
82   return (unsigned short)(((unsigned short)buf[0] << 8) |
83                           ((unsigned short)buf[1]));
84 }
85