blob: a8e77a6253121a7cdde9bd5b9ef85d10b104af71 [file] [log] [blame]
Andrea Falcone1c4977f2020-07-23 10:58:25 -04001/*-
2 * Copyright (c) 1997 Niklas Hallqvist. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#ifndef _SYS_ENDIAN_H_
26#define _SYS_ENDIAN_H_
27
28#include <sys/cdefs.h>
29
30#include <stdint.h>
31
32#define _LITTLE_ENDIAN 1234
33#define _BIG_ENDIAN 4321
34#define _PDP_ENDIAN 3412
35#define _BYTE_ORDER _LITTLE_ENDIAN
36#define __LITTLE_ENDIAN_BITFIELD
37
38#ifndef __LITTLE_ENDIAN
39#define __LITTLE_ENDIAN _LITTLE_ENDIAN
40#endif
41#ifndef __BIG_ENDIAN
42#define __BIG_ENDIAN _BIG_ENDIAN
43#endif
44#define __BYTE_ORDER _BYTE_ORDER
45
46#define __swap16 __builtin_bswap16
47#define __swap32 __builtin_bswap32
48#define __swap64(x) __BIONIC_CAST(static_cast,uint64_t,__builtin_bswap64(x))
49
50/* glibc compatibility. */
51__BEGIN_DECLS
52
53#if __ANDROID_API__ >= 21
54uint32_t htonl(uint32_t __x) __attribute_const__ __INTRODUCED_IN(21);
55uint16_t htons(uint16_t __x) __attribute_const__ __INTRODUCED_IN(21);
56uint32_t ntohl(uint32_t __x) __attribute_const__ __INTRODUCED_IN(21);
57uint16_t ntohs(uint16_t __x) __attribute_const__ __INTRODUCED_IN(21);
58#endif /* __ANDROID_API__ >= 21 */
59
60__END_DECLS
61
62#define htonl(x) __swap32(x)
63#define htons(x) __swap16(x)
64#define ntohl(x) __swap32(x)
65#define ntohs(x) __swap16(x)
66
67/* Bionic additions */
68#define htonq(x) __swap64(x)
69#define ntohq(x) __swap64(x)
70
71#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
72#define LITTLE_ENDIAN _LITTLE_ENDIAN
73#define BIG_ENDIAN _BIG_ENDIAN
74#define PDP_ENDIAN _PDP_ENDIAN
75#define BYTE_ORDER _BYTE_ORDER
76
77#define NTOHL(x) (x) = ntohl(__BIONIC_CAST(static_cast,u_int32_t,(x)))
78#define NTOHS(x) (x) = ntohs(__BIONIC_CAST(static_cast,u_int16_t,(x)))
79#define HTONL(x) (x) = htonl(__BIONIC_CAST(static_cast,u_int32_t,(x)))
80#define HTONS(x) (x) = htons(__BIONIC_CAST(static_cast,u_int16_t,(x)))
81
82#define htobe16(x) __swap16(x)
83#define htobe32(x) __swap32(x)
84#define htobe64(x) __swap64(x)
85#define betoh16(x) __swap16(x)
86#define betoh32(x) __swap32(x)
87#define betoh64(x) __swap64(x)
88
89#define htole16(x) (x)
90#define htole32(x) (x)
91#define htole64(x) (x)
92#define letoh16(x) (x)
93#define letoh32(x) (x)
94#define letoh64(x) (x)
95
96/*
97 * glibc-compatible beXXtoh/leXXtoh synonyms for htobeXX/htoleXX.
98 * The BSDs export both sets of names, bionic historically only
99 * exported the ones above (or on the rhs here), and glibc only
100 * exports these names (on the lhs).
101 */
102#define be16toh(x) htobe16(x)
103#define be32toh(x) htobe32(x)
104#define be64toh(x) htobe64(x)
105#define le16toh(x) htole16(x)
106#define le32toh(x) htole32(x)
107#define le64toh(x) htole64(x)
108
109#endif
110
111#endif