168
|
1 #ifndef BSWAP_H_INCLUDED
|
|
2 #define BSWAP_H_INCLUDED
|
|
3
|
|
4 /*
|
|
5 * Copyright (C) 2000, 2001 Billy Biggs <vektor@dumbterm.net>,
|
|
6 * Håkan Hjort <d95hjort@dtek.chalmers.se>
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22
|
|
23 #include <config.h>
|
|
24
|
|
25 #if defined(WORDS_BIGENDIAN)
|
|
26 /* All bigendian systems are fine, just ignore the swaps. */
|
|
27 #define B2N_16(x) (void)(x)
|
|
28 #define B2N_32(x) (void)(x)
|
|
29 #define B2N_64(x) (void)(x)
|
|
30
|
|
31 #else
|
|
32
|
|
33 /* For __FreeBSD_version */
|
|
34 #if defined(HAVE_SYS_PARAM_H)
|
|
35 #include <sys/param.h>
|
|
36 #endif
|
|
37
|
|
38 #if defined(__linux__)
|
|
39 #include <byteswap.h>
|
|
40 #define B2N_16(x) x = bswap_16(x)
|
|
41 #define B2N_32(x) x = bswap_32(x)
|
|
42 #define B2N_64(x) x = bswap_64(x)
|
|
43
|
|
44 #elif defined(__NetBSD__)
|
|
45 #include <sys/endian.h>
|
|
46 #define B2N_16(x) BE16TOH(x)
|
|
47 #define B2N_32(x) BE32TOH(x)
|
|
48 #define B2N_64(x) BE64TOH(x)
|
|
49
|
|
50 #elif defined(__OpenBSD__)
|
|
51 #include <sys/endian.h>
|
|
52 #define B2N_16(x) x = swap16(x)
|
|
53 #define B2N_32(x) x = swap32(x)
|
|
54 #define B2N_64(x) x = swap64(x)
|
|
55
|
|
56 #elif defined(__FreeBSD__) && __FreeBSD_version >= 470000
|
|
57 #include <sys/endian.h>
|
|
58 #define B2N_16(x) x = be16toh(x)
|
|
59 #define B2N_32(x) x = be32toh(x)
|
|
60 #define B2N_64(x) x = be64toh(x)
|
|
61
|
|
62 /* This is a slow but portable implementation, it has multiple evaluation
|
|
63 * problems so beware.
|
|
64 * Old FreeBSD's and Solaris don't have <byteswap.h> or any other such
|
|
65 * functionality!
|
|
66 */
|
|
67
|
185
|
68 #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(WIN32)
|
168
|
69 #define B2N_16(x) \
|
|
70 x = ((((x) & 0xff00) >> 8) | \
|
|
71 (((x) & 0x00ff) << 8))
|
|
72 #define B2N_32(x) \
|
|
73 x = ((((x) & 0xff000000) >> 24) | \
|
|
74 (((x) & 0x00ff0000) >> 8) | \
|
|
75 (((x) & 0x0000ff00) << 8) | \
|
|
76 (((x) & 0x000000ff) << 24))
|
|
77 #define B2N_64(x) \
|
|
78 x = ((((x) & 0xff00000000000000) >> 56) | \
|
|
79 (((x) & 0x00ff000000000000) >> 40) | \
|
|
80 (((x) & 0x0000ff0000000000) >> 24) | \
|
|
81 (((x) & 0x000000ff00000000) >> 8) | \
|
|
82 (((x) & 0x00000000ff000000) << 8) | \
|
|
83 (((x) & 0x0000000000ff0000) << 24) | \
|
|
84 (((x) & 0x000000000000ff00) << 40) | \
|
|
85 (((x) & 0x00000000000000ff) << 56))
|
|
86
|
|
87 #else
|
|
88
|
|
89 /* If there isn't a header provided with your system with this functionality
|
|
90 * add the relevant || define( ) to the portable implementation above.
|
|
91 */
|
|
92 #error "You need to add endian swap macros for you're system"
|
|
93
|
|
94 #endif
|
|
95
|
|
96 #endif /* WORDS_BIGENDIAN */
|
|
97
|
|
98 #endif /* BSWAP_H_INCLUDED */
|