bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | byteorder.h (12.01.10) |
| 3 | Endianness stuff. exFAT uses little-endian byte order. |
| 4 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 5 | Free exFAT implementation. |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 6 | Copyright (C) 2010-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 7 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 10 | the Free Software Foundation, either version 2 of the License, or |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 18 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef BYTEORDER_H_INCLUDED |
| 24 | #define BYTEORDER_H_INCLUDED |
| 25 | |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 26 | #define __GLIBC__ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 27 | #include <stdint.h> |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 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 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 82 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 83 | typedef struct { uint16_t __u16; } le16_t; |
| 84 | typedef struct { uint32_t __u32; } le32_t; |
| 85 | typedef struct { uint64_t __u64; } le64_t; |
| 86 | |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 87 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 88 | static inline uint16_t le16_to_cpu(le16_t v) { return v.__u16; } |
| 89 | static inline uint32_t le32_to_cpu(le32_t v) { return v.__u32; } |
| 90 | static inline uint64_t le64_to_cpu(le64_t v) { return v.__u64; } |
| 91 | |
| 92 | static inline le16_t cpu_to_le16(uint16_t v) { le16_t t = {v}; return t; } |
| 93 | static inline le32_t cpu_to_le32(uint32_t v) { le32_t t = {v}; return t; } |
| 94 | static inline le64_t cpu_to_le64(uint64_t v) { le64_t t = {v}; return t; } |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 95 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 96 | static inline uint16_t le16_to_cpu(le16_t v) { return bswap_16(v.__u16); } |
| 97 | static inline uint32_t le32_to_cpu(le32_t v) { return bswap_32(v.__u32); } |
| 98 | static inline uint64_t le64_to_cpu(le64_t v) { return bswap_64(v.__u64); } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 99 | |
| 100 | static inline le16_t cpu_to_le16(uint16_t v) |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 101 | { le16_t t = {bswap_16(v)}; return t; } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 102 | static inline le32_t cpu_to_le32(uint32_t v) |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 103 | { le32_t t = {bswap_32(v)}; return t; } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 104 | static inline le64_t cpu_to_le64(uint64_t v) |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 105 | { le64_t t = {bswap_64(v)}; return t; } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 106 | #else |
| 107 | #error Wow! You have a PDP machine?! |
| 108 | #endif |
| 109 | |
| 110 | #endif /* ifndef BYTEORDER_H_INCLUDED */ |