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 | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 5 | Copyright (C) 2010-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 6 | |
| 7 | This program is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #ifndef BYTEORDER_H_INCLUDED |
| 22 | #define BYTEORDER_H_INCLUDED |
| 23 | |
| 24 | #define __GLIBC__ |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #if defined(__GLIBC__) |
| 28 | |
| 29 | #include <endian.h> |
| 30 | #include <byteswap.h> |
| 31 | |
| 32 | #elif defined(__APPLE__) |
| 33 | |
| 34 | #include <machine/endian.h> |
| 35 | #include <libkern/OSByteOrder.h> |
| 36 | #define bswap_16(x) OSSwapInt16(x) |
| 37 | #define bswap_32(x) OSSwapInt32(x) |
| 38 | #define bswap_64(x) OSSwapInt64(x) |
| 39 | #define __BYTE_ORDER BYTE_ORDER |
| 40 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 41 | #define __BIG_ENDIAN BIG_ENDIAN |
| 42 | |
| 43 | #elif defined(__FreeBSD__) || defined(__DragonFlyBSD__) || defined(__NetBSD__) |
| 44 | |
| 45 | #include <sys/endian.h> |
| 46 | #define bswap_16(x) bswap16(x) |
| 47 | #define bswap_32(x) bswap32(x) |
| 48 | #define bswap_64(x) bswap64(x) |
| 49 | #define __BYTE_ORDER _BYTE_ORDER |
| 50 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN |
| 51 | #define __BIG_ENDIAN _BIG_ENDIAN |
| 52 | |
| 53 | #elif defined(__OpenBSD__) |
| 54 | |
| 55 | #include <machine/endian.h> |
| 56 | #define bswap_16(x) swap16(x) |
| 57 | #define bswap_32(x) swap32(x) |
| 58 | #define bswap_64(x) swap64(x) |
| 59 | #define __BYTE_ORDER _BYTE_ORDER |
| 60 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN |
| 61 | #define __BIG_ENDIAN _BIG_ENDIAN |
| 62 | |
| 63 | #elif defined(__sun) |
| 64 | |
| 65 | #include <sys/byteorder.h> |
| 66 | #define bswap_16(x) BSWAP_16(x) |
| 67 | #define bswap_32(x) BSWAP_32(x) |
| 68 | #define bswap_64(x) BSWAP_64(x) |
| 69 | #define __LITTLE_ENDIAN 1234 |
| 70 | #define __BIG_ENDIAN 4321 |
| 71 | #ifdef _LITTLE_ENDIAN |
| 72 | #define __BYTE_ORDER __LITTLE_ENDIAN |
| 73 | #else |
| 74 | #define __BYTE_ORDER __BIG_ENDIAN |
| 75 | #endif |
| 76 | |
| 77 | #else |
| 78 | #error No byte order macros available for your platform |
| 79 | #endif |
| 80 | |
| 81 | #define __BYTE_ORDER __LITTLE_ENDIAN |
| 82 | typedef struct { uint16_t __u16; } le16_t; |
| 83 | typedef struct { uint32_t __u32; } le32_t; |
| 84 | typedef struct { uint64_t __u64; } le64_t; |
| 85 | |
| 86 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 87 | static inline uint16_t le16_to_cpu(le16_t v) { return v.__u16; } |
| 88 | static inline uint32_t le32_to_cpu(le32_t v) { return v.__u32; } |
| 89 | static inline uint64_t le64_to_cpu(le64_t v) { return v.__u64; } |
| 90 | |
| 91 | static inline le16_t cpu_to_le16(uint16_t v) { le16_t t = {v}; return t; } |
| 92 | static inline le32_t cpu_to_le32(uint32_t v) { le32_t t = {v}; return t; } |
| 93 | static inline le64_t cpu_to_le64(uint64_t v) { le64_t t = {v}; return t; } |
| 94 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 95 | static inline uint16_t le16_to_cpu(le16_t v) { return bswap_16(v.__u16); } |
| 96 | static inline uint32_t le32_to_cpu(le32_t v) { return bswap_32(v.__u32); } |
| 97 | static inline uint64_t le64_to_cpu(le64_t v) { return bswap_64(v.__u64); } |
| 98 | |
| 99 | static inline le16_t cpu_to_le16(uint16_t v) |
| 100 | { le16_t t = {bswap_16(v)}; return t; } |
| 101 | static inline le32_t cpu_to_le32(uint32_t v) |
| 102 | { le32_t t = {bswap_32(v)}; return t; } |
| 103 | static inline le64_t cpu_to_le64(uint64_t v) |
| 104 | { le64_t t = {bswap_64(v)}; return t; } |
| 105 | #else |
| 106 | #error Wow! You have a PDP machine?! |
| 107 | #endif |
| 108 | |
| 109 | #endif /* ifndef BYTEORDER_H_INCLUDED */ |