libaaruformat 1.0 Aaru Data Preservation Suite - Format Library

Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2025 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef LIBAARUFORMAT_ENDIAN_H
20#define LIBAARUFORMAT_ENDIAN_H
21
22#ifdef _MSC_VER
23
24#include <stdlib.h>
25#define bswap_16(x) _byteswap_ushort(x)
26#define bswap_32(x) _byteswap_ulong(x)
27#define bswap_64(x) _byteswap_uint64(x)
28
29#elif defined(__APPLE__)
30
31// Mac OS X / Darwin features
32#include <libkern/OSByteOrder.h>
33
34#define bswap_16(x) OSSwapInt16(x)
35#define bswap_32(x) OSSwapInt32(x)
36#define bswap_64(x) OSSwapInt64(x)
37
38#elif defined(__sun) || defined(sun)
39
40#include <sys/byteorder.h>
41#define bswap_16(x) BSWAP_16(x)
42#define bswap_32(x) BSWAP_32(x)
43#define bswap_64(x) BSWAP_64(x)
44
45#elif defined(__FreeBSD__)
46
47#include <sys/endian.h>
48#define bswap_16(x) bswap16(x)
49#define bswap_32(x) bswap32(x)
50#define bswap_64(x) bswap64(x)
51
52#elif defined(__OpenBSD__)
53
54#include <sys/types.h>
55#define bswap_16(x) swap16(x)
56#define bswap_32(x) swap32(x)
57#define bswap_64(x) swap64(x)
58
59#elif defined(__NetBSD__)
60
61#include <machine/bswap.h>
62#include <sys/types.h>
63#if defined(__BSWAP_RENAME) && !defined(__bswap_32)
64#define bswap_16(x) bswap16(x)
65#define bswap_32(x) bswap32(x)
66#define bswap_64(x) bswap64(x)
67#endif
68
69#elif defined(__linux__)
70
71#include <byteswap.h>
72
73#else
74
75#include <stdint.h>
76
77#define bswap_16(x) ((uint16_t)((((uint16_t)(x) & 0xff00) >> 8) | (((uint16_t)(x) & 0x00ff) << 8)))
78#define bswap_32(x) \
79 ((uint32_t)((((uint32_t)(x) & 0xff000000) >> 24) | (((uint32_t)(x) & 0x00ff0000) >> 8) | \
80 (((uint32_t)(x) & 0x0000ff00) << 8) | (((uint32_t)(x) & 0x000000ff) << 24)))
81#define bswap_64(x) \
82 ((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
83 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
84 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
85 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | (((uint64_t)(x) & 0x00000000000000ffULL) << 56)))
86
87#endif
88
89#endif // LIBAARUFORMAT_ENDIAN_H