Mstdlib-1.24.0
m_endian.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_ENDIAN_H__
25#define __M_ENDIAN_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
33
34__BEGIN_DECLS
35
36
37M_API M_uint16 M_swap16(M_uint16 n) M_WARN_UNUSED_RESULT;
38M_API M_uint32 M_swap32(M_uint32 n) M_WARN_UNUSED_RESULT;
39M_API M_uint64 M_swap64(M_uint64 n) M_WARN_UNUSED_RESULT;
40
41M_BEGIN_IGNORE_REDECLARATIONS
42#if M_BLACKLIST_FUNC == 1
43# ifdef htonl
44# undef htonl
45# else
46 M_DEPRECATED_FOR(M_hton32, M_uint32 htonl(M_uint32 hostlong))
47# endif
48# ifdef htons
49# undef htons
50# else
51 M_DEPRECATED_FOR(M_hton16, M_uint16 htons(M_uint16 hostshort))
52# endif
53# ifdef ntohl
54# undef ntohl
55# else
56 M_DEPRECATED_FOR(M_ntoh32, M_uint32 ntohl(M_uint32 netlong))
57# endif
58# ifdef ntohs
59# undef ntohs
60# else
61 M_DEPRECATED_FOR(M_ntoh16, M_uint16 ntohs(M_uint16 netshort))
62# endif
63# endif
64M_END_IGNORE_REDECLARATIONS
65
66/*! \addtogroup m_endian Endian
67 * \ingroup mstdlib_base
68 *
69 * Conversion between byte orders.
70 *
71 * @{
72 */
73
74
75/*! Endianness */
76typedef enum {
80
81
82/* - - - - - - - - - - - - - - - - - - - - - - - - - */
83
84/*! Convert a 16-bit unsigned integer in host byte order to network byte order.
85 *
86 * \param[in] h16 16-bit unsigned integer in host byte order.
87 *
88 * \return 16-bit unsigned integer in network byte order.
89 */
90M_API M_uint16 M_hton16(M_uint16 h16) M_WARN_UNUSED_RESULT;
91
92
93/*! Convert a 32-bit unsigned integer in host byte order to network byte order.
94 *
95 * \param[in] h32 32-bit unsigned integer in host byte order.
96 *
97 * \return 32-bit unsigned integer in network byte order.
98 */
99M_API M_uint32 M_hton32(M_uint32 h32) M_WARN_UNUSED_RESULT;
100
101
102/*! Convert a 64-bit unsigned integer in host byte order to network byte order.
103 *
104 * \param[in] h64 64-bit unsigned integer in host byte order.
105 *
106 * \return 64-bit unsigned integer in network byte order.
107 */
108M_API M_uint64 M_hton64(M_uint64 h64) M_WARN_UNUSED_RESULT;
109
110
111/*! Convert a 16-bit unsigned integer in host byte order to little endian byte order.
112 *
113 * \param[in] h16 16-bit unsigned integer in host byte order.
114 *
115 * \return 16-bit unsigned integer in little endian byte order.
116 */
117M_API M_uint16 M_htol16(M_uint16 h16) M_WARN_UNUSED_RESULT;
118
119
120/*! Convert a 32-bit unsigned integer in host byte order to little endian byte order.
121 *
122 * \param[in] h32 32-bit unsigned integer in host byte order.
123 *
124 * \return 32-bit unsigned integer in little endian byte order.
125 */
126M_API M_uint32 M_htol32(M_uint32 h32) M_WARN_UNUSED_RESULT;
127
128
129/*! Convert a 64-bit unsigned integer in host byte order to little endian byte order.
130 *
131 * \param[in] h64 64-bit unsigned integer in host byte order.
132 *
133 * \return 64-bit unsigned integer in little endian byte order.
134 */
135M_API M_uint64 M_htol64(M_uint64 h64) M_WARN_UNUSED_RESULT;
136
137
138/*! Convert a 16-bit unsigned integer in network byte order to host byte order.
139 *
140 * \param[in] be16 16-bit unsigned integer in network byte order.
141 *
142 * \return 16-bit unsigned integer in little host byte order.
143 */
144M_API M_uint16 M_ntoh16(M_uint16 be16) M_WARN_UNUSED_RESULT;
145
146
147/*! Convert a 32-bit unsigned integer in network byte order to host byte order.
148 *
149 * \param[in] be32 32-bit unsigned integer in network byte order.
150 *
151 * \return 32-bit unsigned integer in little host byte order.
152 */
153M_API M_uint32 M_ntoh32(M_uint32 be32) M_WARN_UNUSED_RESULT;
154
155
156/*! Convert a 64-bit unsigned integer in network byte order to host byte order.
157 *
158 * \param[in] be64 64-bit unsigned integer in network byte order.
159 *
160 * \return 64-bit unsigned integer in little host byte order.
161 */
162M_API M_uint64 M_ntoh64(M_uint64 be64) M_WARN_UNUSED_RESULT;
163
164
165/*! Convert a 16-bit unsigned integer in little endian byte order to host byte order.
166 *
167 * \param[in] be16 16-bit unsigned integer in little endian byte order.
168 *
169 * \return 16-bit unsigned integer in little host byte order.
170 */
171M_API M_uint16 M_ltoh16(M_uint16 be16) M_WARN_UNUSED_RESULT;
172
173
174/*! Convert a 32-bit unsigned integer in little endian byte order to host byte order.
175 *
176 * \param[in] be32 32-bit unsigned integer in little endian byte order.
177 *
178 * \return 32-bit unsigned integer in little host byte order.
179 */
180M_API M_uint32 M_ltoh32(M_uint32 be32) M_WARN_UNUSED_RESULT;
181
182
183/*! Convert a 64-bit unsigned integer in little endian byte order to host byte order.
184 *
185 * \param[in] be64 64-bit unsigned integer in little endian byte order.
186 *
187 * \return 64-bit unsigned integer in little host byte order.
188 */
189M_API M_uint64 M_ltoh64(M_uint64 be64) M_WARN_UNUSED_RESULT;
190
191/*! @} */
192
193
194/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
195/* Legacy
196 */
197
198# define M_BIG_ENDIAN 1
199# define M_LITTLE_ENDIAN 2
200
201M_API int M_Current_Endian(void);
202
203# define M_hton_u16(n) M_hton16(n)
204# define M_hton_16(n) M_hton16(n)
205# define M_hton_u32(n) M_hton32(n)
206# define M_hton_32(n) M_hton32(n)
207# define M_hton_u64(n) M_hton64(n)
208# define M_hton_64(n) M_hton64(n)
209# define M_ntoh_u16(n) M_hton_u16(n)
210# define M_ntoh_16(n) M_hton_16(n)
211# define M_ntoh_u32(n) M_hton_u32(n)
212# define M_ntoh_32(n) M_hton_32(n)
213# define M_ntoh_u64(n) M_hton_u64(n)
214# define M_ntoh_64(n) M_hton_64(n)
215
216# define M_swapu16(n) M_swap16(n)
217# define M_swapu32(n) M_swap32(n)
218# define M_swapu64(n) M_swap64(n)
219
220__END_DECLS
221
222#endif /* __M_ENDIAN_H__ */
M_endian_t
Definition: m_endian.h:76
M_uint64 M_htol64(M_uint64 h64) M_WARN_UNUSED_RESULT
M_uint16 M_ltoh16(M_uint16 be16) M_WARN_UNUSED_RESULT
M_uint32 M_ltoh32(M_uint32 be32) M_WARN_UNUSED_RESULT
M_uint32 M_ntoh32(M_uint32 be32) M_WARN_UNUSED_RESULT
M_uint64 M_ntoh64(M_uint64 be64) M_WARN_UNUSED_RESULT
M_uint32 M_hton32(M_uint32 h32) M_WARN_UNUSED_RESULT
M_uint16 M_htol16(M_uint16 h16) M_WARN_UNUSED_RESULT
M_uint32 M_htol32(M_uint32 h32) M_WARN_UNUSED_RESULT
M_uint64 M_hton64(M_uint64 h64) M_WARN_UNUSED_RESULT
M_uint64 M_ltoh64(M_uint64 be64) M_WARN_UNUSED_RESULT
M_uint16 M_hton16(M_uint16 h16) M_WARN_UNUSED_RESULT
M_uint16 M_ntoh16(M_uint16 be16) M_WARN_UNUSED_RESULT
@ M_ENDIAN_LITTLE
Definition: m_endian.h:78
@ M_ENDIAN_BIG
Definition: m_endian.h:77