bswap.h 3.71 KB
Newer Older
1 2
/*
 * Copyright (C) 2000, 2001 Billy Biggs <vektor@dumbterm.net>,
3
 *                          Håkan Hjort <d95hjort@dtek.chalmers.se>
4
 *
5 6 7
 * This file is part of libdvdread.
 *
 * libdvdread is free software; you can redistribute it and/or modify
8 9 10 11
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
12
 * libdvdread is distributed in the hope that it will be useful,
13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
17 18 19
 * You should have received a copy of the GNU General Public License along
 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 21
 */

22 23 24
#ifndef LIBDVDREAD_BSWAP_H
#define LIBDVDREAD_BSWAP_H

25 26 27
#include <config.h>

#if defined(WORDS_BIGENDIAN)
28
/* All bigendian systems are fine, just ignore the swaps. */
29 30 31 32
#define B2N_16(x) (void)(x)
#define B2N_32(x) (void)(x)
#define B2N_64(x) (void)(x)

33
#else
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

/* For __FreeBSD_version */
#if defined(HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif

#if defined(__linux__) || defined(__GLIBC__)
#include <byteswap.h>
#define B2N_16(x) x = bswap_16(x)
#define B2N_32(x) x = bswap_32(x)
#define B2N_64(x) x = bswap_64(x)

#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define B2N_16(x) x = OSSwapBigToHostInt16(x)
#define B2N_32(x) x = OSSwapBigToHostInt32(x)
#define B2N_64(x) x = OSSwapBigToHostInt64(x)

#elif defined(__NetBSD__)
#include <sys/endian.h>
#define B2N_16(x) BE16TOH(x)
#define B2N_32(x) BE32TOH(x)
#define B2N_64(x) BE64TOH(x)

#elif defined(__OpenBSD__)
#include <sys/endian.h>
#define B2N_16(x) x = swap16(x)
#define B2N_32(x) x = swap32(x)
#define B2N_64(x) x = swap64(x)

#elif defined(__FreeBSD__) && __FreeBSD_version >= 470000
#include <sys/endian.h>
#define B2N_16(x) x = be16toh(x)
#define B2N_32(x) x = be32toh(x)
#define B2N_64(x) x = be64toh(x)

70 71 72 73 74 75
#elif defined(__QNXNTO__)
#include <gulliver.h>
#define B2N_16(x) x = ENDIAN_RET16(x)
#define B2N_32(x) x = ENDIAN_RET32(x)
#define B2N_64(x) x = ENDIAN_RET64(x)

76 77 78 79 80 81
#elif defined(__DragonFly__)
#include <sys/endian.h>
#define B2N_16(x) x = bswap16(x)
#define B2N_32(x) x = bswap32(x)
#define B2N_64(x) x = bswap64(x)

82
/* This is a slow but portable implementation, it has multiple evaluation
83
 * problems so beware.
84 85
 * Old FreeBSD's and Solaris don't have <byteswap.h> or any other such
 * functionality!
86 87
 */

KO Myung-Hun's avatar
KO Myung-Hun committed
88
#elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(WIN32) || defined(__CYGWIN__) || defined(__BEOS__) || defined(__OS2__)
89 90
#define B2N_16(x)                             \
 x = ((((x) & 0xff00) >> 8) |                 \
91
      (((x) & 0x00ff) << 8))
92 93 94 95
#define B2N_32(x)                             \
 x = ((((x) & 0xff000000) >> 24) |            \
      (((x) & 0x00ff0000) >>  8) |            \
      (((x) & 0x0000ff00) <<  8) |            \
96
      (((x) & 0x000000ff) << 24))
97
#define B2N_64(x)                             \
98 99 100 101 102 103 104 105 106 107 108 109 110 111
 x = ((((x) & 0xff00000000000000ULL) >> 56) | \
      (((x) & 0x00ff000000000000ULL) >> 40) | \
      (((x) & 0x0000ff0000000000ULL) >> 24) | \
      (((x) & 0x000000ff00000000ULL) >>  8) | \
      (((x) & 0x00000000ff000000ULL) <<  8) | \
      (((x) & 0x0000000000ff0000ULL) << 24) | \
      (((x) & 0x000000000000ff00ULL) << 40) | \
      (((x) & 0x00000000000000ffULL) << 56))

#else

/* If there isn't a header provided with your system with this functionality
 * add the relevant || define( ) to the portable implementation above.
 */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
112
#error "You need to add endian swap macros for your system"
113 114 115 116 117

#endif

#endif /* WORDS_BIGENDIAN */

118
#endif /* LIBDVDREAD_BSWAP_H */