Blame src/bswap.h

Packit 015a35
/*
Packit 015a35
 * Copyright (C) 2000, 2001 Billy Biggs <vektor@dumbterm.net>,
Packit 015a35
 *                          HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
Packit 015a35
 *
Packit 015a35
 * This file is part of libdvdread.
Packit 015a35
 *
Packit 015a35
 * libdvdread is free software; you can redistribute it and/or modify
Packit 015a35
 * it under the terms of the GNU General Public License as published by
Packit 015a35
 * the Free Software Foundation; either version 2 of the License, or
Packit 015a35
 * (at your option) any later version.
Packit 015a35
 *
Packit 015a35
 * libdvdread is distributed in the hope that it will be useful,
Packit 015a35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 015a35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 015a35
 * GNU General Public License for more details.
Packit 015a35
 *
Packit 015a35
 * You should have received a copy of the GNU General Public License along
Packit 015a35
 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
Packit 015a35
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 015a35
 */
Packit 015a35
Packit 015a35
#ifndef LIBDVDREAD_BSWAP_H
Packit 015a35
#define LIBDVDREAD_BSWAP_H
Packit 015a35
Packit 015a35
#include <config.h>
Packit 015a35
Packit 015a35
#if defined(WORDS_BIGENDIAN)
Packit 015a35
/* All bigendian systems are fine, just ignore the swaps. */
Packit 015a35
#define B2N_16(x) (void)(x)
Packit 015a35
#define B2N_32(x) (void)(x)
Packit 015a35
#define B2N_64(x) (void)(x)
Packit 015a35
Packit 015a35
#else
Packit 015a35
Packit 015a35
/* For __FreeBSD_version */
Packit 015a35
#if defined(HAVE_SYS_PARAM_H)
Packit 015a35
#include <sys/param.h>
Packit 015a35
#endif
Packit 015a35
Packit 015a35
#if defined(__linux__) || defined(__GLIBC__)
Packit 015a35
#include <byteswap.h>
Packit 015a35
#define B2N_16(x) x = bswap_16(x)
Packit 015a35
#define B2N_32(x) x = bswap_32(x)
Packit 015a35
#define B2N_64(x) x = bswap_64(x)
Packit 015a35
Packit 015a35
#elif defined(__APPLE__)
Packit 015a35
#include <libkern/OSByteOrder.h>
Packit 015a35
#define B2N_16(x) x = OSSwapBigToHostInt16(x)
Packit 015a35
#define B2N_32(x) x = OSSwapBigToHostInt32(x)
Packit 015a35
#define B2N_64(x) x = OSSwapBigToHostInt64(x)
Packit 015a35
Packit 015a35
#elif defined(__NetBSD__)
Packit 015a35
#include <sys/endian.h>
Packit 015a35
#define B2N_16(x) BE16TOH(x)
Packit 015a35
#define B2N_32(x) BE32TOH(x)
Packit 015a35
#define B2N_64(x) BE64TOH(x)
Packit 015a35
Packit 015a35
#elif defined(__OpenBSD__)
Packit 015a35
#include <sys/endian.h>
Packit 015a35
#define B2N_16(x) x = swap16(x)
Packit 015a35
#define B2N_32(x) x = swap32(x)
Packit 015a35
#define B2N_64(x) x = swap64(x)
Packit 015a35
Packit 015a35
#elif defined(__FreeBSD__) && __FreeBSD_version >= 470000
Packit 015a35
#include <sys/endian.h>
Packit 015a35
#define B2N_16(x) x = be16toh(x)
Packit 015a35
#define B2N_32(x) x = be32toh(x)
Packit 015a35
#define B2N_64(x) x = be64toh(x)
Packit 015a35
Packit 015a35
#elif defined(__QNXNTO__)
Packit 015a35
#include <gulliver.h>
Packit 015a35
#define B2N_16(x) x = ENDIAN_RET16(x)
Packit 015a35
#define B2N_32(x) x = ENDIAN_RET32(x)
Packit 015a35
#define B2N_64(x) x = ENDIAN_RET64(x)
Packit 015a35
Packit 015a35
#elif defined(__DragonFly__)
Packit 015a35
#include <sys/endian.h>
Packit 015a35
#define B2N_16(x) x = bswap16(x)
Packit 015a35
#define B2N_32(x) x = bswap32(x)
Packit 015a35
#define B2N_64(x) x = bswap64(x)
Packit 015a35
Packit 015a35
/* This is a slow but portable implementation, it has multiple evaluation
Packit 015a35
 * problems so beware.
Packit 015a35
 * Old FreeBSD's and Solaris don't have <byteswap.h> or any other such
Packit 015a35
 * functionality!
Packit 015a35
 */
Packit 015a35
Packit 015a35
#elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(WIN32) || defined(__CYGWIN__) || defined(__BEOS__) || defined(__OS2__)
Packit 015a35
#define B2N_16(x)                             \
Packit 015a35
 x = ((((x) & 0xff00) >> 8) |                 \
Packit 015a35
      (((x) & 0x00ff) << 8))
Packit 015a35
#define B2N_32(x)                             \
Packit 015a35
 x = ((((x) & 0xff000000) >> 24) |            \
Packit 015a35
      (((x) & 0x00ff0000) >>  8) |            \
Packit 015a35
      (((x) & 0x0000ff00) <<  8) |            \
Packit 015a35
      (((x) & 0x000000ff) << 24))
Packit 015a35
#define B2N_64(x)                             \
Packit 015a35
 x = ((((x) & 0xff00000000000000ULL) >> 56) | \
Packit 015a35
      (((x) & 0x00ff000000000000ULL) >> 40) | \
Packit 015a35
      (((x) & 0x0000ff0000000000ULL) >> 24) | \
Packit 015a35
      (((x) & 0x000000ff00000000ULL) >>  8) | \
Packit 015a35
      (((x) & 0x00000000ff000000ULL) <<  8) | \
Packit 015a35
      (((x) & 0x0000000000ff0000ULL) << 24) | \
Packit 015a35
      (((x) & 0x000000000000ff00ULL) << 40) | \
Packit 015a35
      (((x) & 0x00000000000000ffULL) << 56))
Packit 015a35
Packit 015a35
#else
Packit 015a35
Packit 015a35
/* If there isn't a header provided with your system with this functionality
Packit 015a35
 * add the relevant || define( ) to the portable implementation above.
Packit 015a35
 */
Packit 015a35
#error "You need to add endian swap macros for your system"
Packit 015a35
Packit 015a35
#endif
Packit 015a35
Packit 015a35
#endif /* WORDS_BIGENDIAN */
Packit 015a35
Packit 015a35
#endif /* LIBDVDREAD_BSWAP_H */