]> git.ipfire.org Git - thirdparty/squid.git/blob - include/squid_endian.h
Removed CVS $ markers
[thirdparty/squid.git] / include / squid_endian.h
1 /*
2 * AUTHOR: Alan Barrett
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 *
30 */
31
32 #ifndef SQUID_ENDIAN_H
33 #define SQUID_ENDIAN_H
34
35 /*
36 * Macros to deal with byte swapping. These macros provide
37 * the following interface:
38 *
39 * // Byte-swap
40 * uint16_t bswap16(uint16_t);
41 * uint32_t bswap32(uint32_t);
42 *
43 * // Convert from host byte order to big-endian, and vice versa.
44 * uint16_t htobe16(uint16_t); // equivalent to htons()
45 * uint32_t htobe32(uint32_t); // equivalent to htonl()
46 * uint16_t be16toh(uint16_t); // equivalent to ntohs()
47 * uint32_t be32toh(uint32_t); // equivalent to ntohs()
48 *
49 * // Convert from host byte order to little-endian, and vice versa.
50 * uint16_t htole16(uint16_t);
51 * uint32_t htole32(uint32_t);
52 * uint16_t le16toh(uint16_t);
53 * uint32_t le32toh(uint32_t);
54 */
55
56 /*
57 * Some systems define bswap_16() and bswap_32() in <byteswap.h>
58 *
59 * Some systems define bswap16() and bswap32() in <sys/bswap.h>.
60 *
61 * Some systems define htobe16()/be16toh() and friends in <sys/endian.h>.
62 */
63 #if HAVE_BYTESWAP_H
64 # include <byteswap.h>
65 #endif /* HAVE_BYTESWAP_H */
66 #if HAVE_MACHINE_BYTE_SWAP_H
67 # include <machine/byte_swap.h>
68 #endif /* HAVE_MACHINE_BYTE_SWAP_H */
69 #if HAVE_SYS_BSWAP_H
70 # include <sys/bswap.h>
71 #endif /* HAVE_SYS_BSWAP_H */
72 #if HAVE_SYS_ENDIAN_H
73 # include <sys/endian.h>
74 #endif /* HAVE_SYS_ENDIAN_H */
75
76 /*
77 * Define bswap16() and bswap32() in terms of bswap_16() and bswap_32(),
78 * or the hard way.
79 */
80 #if ! HAVE_BSWAP16 && ! defined(bswap16)
81 # if defined(bswap_16)
82 # define bswap16(x) bswap_16(x)
83 # else
84 # define bswap16(x) \
85 (((((uint16_t)(x)) >> 8) & 0xff) | ((((uint16_t)(x)) & 0xff) << 8))
86 # endif
87 #endif /* ! HAVE_BSWAP16 && ! defined(bswap16) */
88 #if ! HAVE_BSWAP32 && ! defined(bswap32)
89 # if defined(bswap_32)
90 # define bswap32(x) bswap_32(x)
91 # else
92 # define bswap32(x) \
93 (((((uint32_t)(x)) & 0xff000000) >> 24) | \
94 ((((uint32_t)(x)) & 0x00ff0000) >> 8) | \
95 ((((uint32_t)(x)) & 0x0000ff00) << 8) | \
96 ((((uint32_t)(x)) & 0x000000ff) << 24))
97 # endif
98 #endif /* ! HAVE_BSWAP32 && ! defined(bswap32) */
99
100 /*
101 * Define htobe*()/be*toh() in terms of hton*()/ntoh*().
102 *
103 * XXX: If htobe16() is missing, we assume that the other *be*() functions
104 * are also missing.
105 */
106 #if ! HAVE_HTOBE16 && ! defined(htobe16)
107 # ifdef WORDS_BIGENDIAN
108 # define htobe16(x) (x)
109 # define htobe32(x) (x)
110 # define be16toh(x) (x)
111 # define be32toh(x) (x)
112 # else /* ! WORDS_BIGENDIAN */
113 # define htobe16(x) htons(x)
114 # define htobe32(x) htonl(x)
115 # define be16toh(x) ntohs(x)
116 # define be32toh(x) ntohl(x)
117 # endif /* ! WORDS_BIGENDIAN */
118 #endif /* ! HAVE_HTOBE16 && ! defined(htobe16) */
119
120 /*
121 * Define htole*()/le*toh() in terms of bswap*().
122 *
123 * XXX: If htole16() is missing, we assume that the other *le*() functions
124 * are also missing.
125 *
126 * Except OpenBSD - htole16 & 32 exist, but not le16toh etc
127 */
128 #if _SQUID_OPENBSD_
129 # define le16toh(x) htole16(x)
130 # define le32toh(x) htole32(x)
131 #endif
132
133 #if ! HAVE_HTOLE16 && ! defined(htole16)
134 # ifdef WORDS_BIGENDIAN
135 # define htole16(x) bswap16(x)
136 # define htole32(x) bswap32(x)
137 # define le16toh(x) bswap16(x)
138 # define le32toh(x) bswap32(x)
139 # else /* ! WORDS_BIGENDIAN */
140 /*
141 * XXX: What about unusual byte orders like 3412 or 2143 ?
142 * Nothing else in squid seems to care about them,
143 * so we don't worry about them here either.
144 */
145 # define htole16(x) (x)
146 # define htole32(x) (x)
147 # define le16toh(x) (x)
148 # define le32toh(x) (x)
149 # endif /* ! WORDS_BIGENDIAN */
150 #endif /* ! HAVE_HTOLE16 && ! defined(htole16) */
151
152 #endif /* SQUID_ENDIAN_H */