aboutsummaryrefslogtreecommitdiff
path: root/include/libcasio/cdefs/endian.h
blob: 57122ec16f5fe59323ba7f9d9637f5c307c7b98b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ****************************************************************************
 * libcasio/cdefs/endian.h -- libcasio's implementation of `endian.h`.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcasio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libcasio; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#ifndef  LIBCASIO_CDEFS_ENDIAN_H
# define LIBCASIO_CDEFS_ENDIAN_H
# include "../cdefs.h"

/* These function always exist. They are the default ones, if the platform
 * does not have any equivalent function (or has one but indicates that
 * fact badly). */

CASIO_EXTERN casio_uint16_t CASIO_EXPORT casio_be16toh
	OF((casio_uint16_t casio__x));
CASIO_EXTERN casio_uint16_t CASIO_EXPORT casio_le16toh
	OF((casio_uint16_t casio__x));
CASIO_EXTERN casio_uint32_t CASIO_EXPORT casio_be32toh
	OF((casio_uint32_t casio__x));
CASIO_EXTERN casio_uint32_t CASIO_EXPORT casio_le32toh
	OF((casio_uint32_t casio__x));

CASIO_EXTERN casio_uint16_t CASIO_EXPORT casio_htobe16
	OF((casio_uint16_t casio__x));
CASIO_EXTERN casio_uint16_t CASIO_EXPORT casio_htole16
	OF((casio_uint16_t casio__x));
CASIO_EXTERN casio_uint32_t CASIO_EXPORT casio_htobe32
	OF((casio_uint32_t casio__x));
CASIO_EXTERN casio_uint32_t CASIO_EXPORT casio_htole32
	OF((casio_uint32_t casio__x));

# if defined(__APPLE__)
#  include <libkern/OSByteOrder.h>

#  define casio_int_be16toh(CASIO__X) OSSwapBigToHostInt16(CASIO__X)
#  define casio_int_le16toh(CASIO__X) OSSwapLittleToHostInt16(CASIO__X)
#  define casio_int_be32toh(CASIO__X) OSSwapBigToHostInt32(CASIO__X)
#  define casio_int_le32toh(CASIO__X) OSSwapLittleToHostInt32(CASIO__X)

#  define casio_int_htobe16(CASIO__X) OSSwapHostToBigInt16(CASIO__X)
#  define casio_int_htole16(CASIO__X) OSSwapHostToLittleInt16(CASIO__X)
#  define casio_int_htobe32(CASIO__X) OSSwapHostToBigInt32(CASIO__X)
#  define casio_int_htole32(CASIO__X) OSSwapHostToLittleInt32(CASIO__X)

# elif defined(__OpenBSD__)
#  include <sys/endian.h>
#  define LIBCASIO_ENDIAN_DEFINED

# elif defined(_WIN16) || defined(_WIN32) || defined(_WIN64) \
	|| defined(__WINDOWS__)
#  include <winsock2.h>
#  include <sys/param.h>

#  if BYTE_ORDER == LITTLE_ENDIAN
#   define casio_int_be16toh(CASIO__X) ntohs(CASIO__X)
#   define casio_int_le16toh(CASIO__X) (CASIO__X)
#   define casio_int_be32toh(CASIO__X) ntohl(CASIO__X)
#   define casio_int_le32toh(CASIO__X) (CASIO__X)

#   define casio_int_htobe16(CASIO__X) htons(CASIO__X)
#   define casio_int_htole16(CASIO__X) (CASIO__X)
#   define casio_int_htobe32(CASIO__X) htonl(CASIO__X)
#   define casio_int_htole32(CASIO__X) (CASIO__X)
#  else
#   define casio_int_be16toh(CASIO__X) (CASIO__X)
#   define casio_int_le16toh(CASIO__X) ntohs(CASIO__X)
#   define casio_int_be32toh(CASIO__X) (CASIO__X)
#   define casio_int_le32toh(CASIO__X) ntohl(CASIO__X)

#   define casio_int_htobe16(CASIO__X) (CASIO__X)
#   define casio_int_htole16(CASIO__X) htons(CASIO__X)
#   define casio_int_htobe32(CASIO__X) (CASIO__X)
#   define casio_int_htole32(CASIO__X) htonl(CASIO__X)
#  endif

# elif defined(__GLIBC__) && defined(__USE_MISC)
#  include <endian.h>
#  define LIBCASIO_ENDIAN_DEFINED

# endif

/* *e*toh, hto*e* macros are already defined, just make `casio_` aliases. */

# ifdef LIBCASIO_ENDIAN_DEFINED
#  define casio_int_be16toh(CASIO__X) be16toh(CASIO__X)
#  define casio_int_le16toh(CASIO__X) le16toh(CASIO__X)
#  define casio_int_be32toh(CASIO__X) be32toh(CASIO__X)
#  define casio_int_le32toh(CASIO__X) le32toh(CASIO__X)

#  define casio_int_htobe16(CASIO__X) htobe16(CASIO__X)
#  define casio_int_htole16(CASIO__X) htole16(CASIO__X)
#  define casio_int_htobe32(CASIO__X) htobe32(CASIO__X)
#  define casio_int_htole32(CASIO__X) htole32(CASIO__X)
# endif

/* If we aren't including this from libcasio's endian source functions,
 * `LIBCASIO_NO_ENDIAN` should not be defined. Otherwise, as we will be
 * defining the "dynamic" symbols, do not define the functions. */

# if !defined(LIBCASIO_NO_ENDIAN) && defined(casio_int_be16toh)
#  define casio_be16toh(CASIO__X) casio_int_be16toh(CASIO__X)
#  define casio_le16toh(CASIO__X) casio_int_le16toh(CASIO__X)
#  define casio_be32toh(CASIO__X) casio_int_be32toh(CASIO__X)
#  define casio_le32toh(CASIO__X) casio_int_le32toh(CASIO__X)

#  define casio_htobe16(CASIO__X) casio_int_htobe16(CASIO__X)
#  define casio_htole16(CASIO__X) casio_int_htole16(CASIO__X)
#  define casio_htobe32(CASIO__X) casio_int_htobe32(CASIO__X)
#  define casio_htole32(CASIO__X) casio_int_htole32(CASIO__X)
# endif

#endif /* LIBCASIO_CDEFS_ENDIAN_H */