blob: 10746e1ef96db62c12d4651e4aa1ad7bc728a80c [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 byteorder.h (12.01.10)
3 Endianness stuff. exFAT uses little-endian byte order.
4
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04005 Free exFAT implementation.
bigbiff bigbiffca829c42013-01-28 08:14:25 -05006 Copyright (C) 2010-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05007
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04008 This program is free software; you can redistribute it and/or modify
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009 it under the terms of the GNU General Public License as published by
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040010 the Free Software Foundation, either version 2 of the License, or
bigbiff bigbiff9c754052013-01-09 09:09:08 -050011 (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
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040018 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021*/
22
23#ifndef BYTEORDER_H_INCLUDED
24#define BYTEORDER_H_INCLUDED
25
26#define __GLIBC__
27#include <stdint.h>
28
29#if defined(__GLIBC__)
30
31#include <endian.h>
32#include <byteswap.h>
33
34#elif defined(__APPLE__)
35
36#include <machine/endian.h>
37#include <libkern/OSByteOrder.h>
38#define bswap_16(x) OSSwapInt16(x)
39#define bswap_32(x) OSSwapInt32(x)
40#define bswap_64(x) OSSwapInt64(x)
41#define __BYTE_ORDER BYTE_ORDER
42#define __LITTLE_ENDIAN LITTLE_ENDIAN
43#define __BIG_ENDIAN BIG_ENDIAN
44
45#elif defined(__FreeBSD__) || defined(__DragonFlyBSD__) || defined(__NetBSD__)
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#define __BYTE_ORDER _BYTE_ORDER
52#define __LITTLE_ENDIAN _LITTLE_ENDIAN
53#define __BIG_ENDIAN _BIG_ENDIAN
54
55#elif defined(__OpenBSD__)
56
57#include <machine/endian.h>
58#define bswap_16(x) swap16(x)
59#define bswap_32(x) swap32(x)
60#define bswap_64(x) swap64(x)
61#define __BYTE_ORDER _BYTE_ORDER
62#define __LITTLE_ENDIAN _LITTLE_ENDIAN
63#define __BIG_ENDIAN _BIG_ENDIAN
64
65#elif defined(__sun)
66
67#include <sys/byteorder.h>
68#define bswap_16(x) BSWAP_16(x)
69#define bswap_32(x) BSWAP_32(x)
70#define bswap_64(x) BSWAP_64(x)
71#define __LITTLE_ENDIAN 1234
72#define __BIG_ENDIAN 4321
73#ifdef _LITTLE_ENDIAN
74#define __BYTE_ORDER __LITTLE_ENDIAN
75#else
76#define __BYTE_ORDER __BIG_ENDIAN
77#endif
78
79#else
80#error No byte order macros available for your platform
81#endif
82
bigbiff bigbiff9c754052013-01-09 09:09:08 -050083typedef struct { uint16_t __u16; } le16_t;
84typedef struct { uint32_t __u32; } le32_t;
85typedef struct { uint64_t __u64; } le64_t;
86
87#if __BYTE_ORDER == __LITTLE_ENDIAN
88static inline uint16_t le16_to_cpu(le16_t v) { return v.__u16; }
89static inline uint32_t le32_to_cpu(le32_t v) { return v.__u32; }
90static inline uint64_t le64_to_cpu(le64_t v) { return v.__u64; }
91
92static inline le16_t cpu_to_le16(uint16_t v) { le16_t t = {v}; return t; }
93static inline le32_t cpu_to_le32(uint32_t v) { le32_t t = {v}; return t; }
94static inline le64_t cpu_to_le64(uint64_t v) { le64_t t = {v}; return t; }
95#elif __BYTE_ORDER == __BIG_ENDIAN
96static inline uint16_t le16_to_cpu(le16_t v) { return bswap_16(v.__u16); }
97static inline uint32_t le32_to_cpu(le32_t v) { return bswap_32(v.__u32); }
98static inline uint64_t le64_to_cpu(le64_t v) { return bswap_64(v.__u64); }
99
100static inline le16_t cpu_to_le16(uint16_t v)
101 { le16_t t = {bswap_16(v)}; return t; }
102static inline le32_t cpu_to_le32(uint32_t v)
103 { le32_t t = {bswap_32(v)}; return t; }
104static inline le64_t cpu_to_le64(uint64_t v)
105 { le64_t t = {bswap_64(v)}; return t; }
106#else
107#error Wow! You have a PDP machine?!
108#endif
109
110#endif /* ifndef BYTEORDER_H_INCLUDED */