| 1 | /* |
| 2 | * librdkafka - Apache Kafka C library |
| 3 | * |
| 4 | * Copyright (c) 2012-2015 Magnus Edenhill |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer in the documentation |
| 14 | * and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | #ifndef _RDENDIAN_H_ |
| 29 | #define _RDENDIAN_H_ |
| 30 | |
| 31 | /** |
| 32 | * Provides portable endian-swapping macros/functions. |
| 33 | * |
| 34 | * be64toh() |
| 35 | * htobe64() |
| 36 | * be32toh() |
| 37 | * htobe32() |
| 38 | * be16toh() |
| 39 | * htobe16() |
| 40 | * le64toh() |
| 41 | */ |
| 42 | |
| 43 | #ifdef __FreeBSD__ |
| 44 | #include <sys/endian.h> |
| 45 | #elif defined __GLIBC__ |
| 46 | #include <endian.h> |
| 47 | #ifndef be64toh |
| 48 | /* Support older glibc (<2.9) which lack be64toh */ |
| 49 | #include <byteswap.h> |
| 50 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 51 | #define be16toh(x) (x) |
| 52 | #define be32toh(x) (x) |
| 53 | #define be64toh(x) (x) |
| 54 | #define le64toh(x) __bswap_64 (x) |
| 55 | #define le32toh(x) __bswap_32 (x) |
| 56 | #else |
| 57 | #define be16toh(x) __bswap_16 (x) |
| 58 | #define be32toh(x) __bswap_32 (x) |
| 59 | #define be64toh(x) __bswap_64 (x) |
| 60 | #define le64toh(x) (x) |
| 61 | #define le32toh(x) (x) |
| 62 | #endif |
| 63 | #endif |
| 64 | |
| 65 | #elif defined __CYGWIN__ |
| 66 | #include <endian.h> |
| 67 | #elif defined __BSD__ |
| 68 | #include <sys/endian.h> |
| 69 | #elif defined __sun |
| 70 | #include <sys/byteorder.h> |
| 71 | #include <sys/isa_defs.h> |
| 72 | #define __LITTLE_ENDIAN 1234 |
| 73 | #define __BIG_ENDIAN 4321 |
| 74 | #ifdef _BIG_ENDIAN |
| 75 | #define __BYTE_ORDER __BIG_ENDIAN |
| 76 | #define be64toh(x) (x) |
| 77 | #define be32toh(x) (x) |
| 78 | #define be16toh(x) (x) |
| 79 | #define le16toh(x) ((uint16_t)BSWAP_16(x)) |
| 80 | #define le32toh(x) BSWAP_32(x) |
| 81 | #define le64toh(x) BSWAP_64(x) |
| 82 | # else |
| 83 | #define __BYTE_ORDER __LITTLE_ENDIAN |
| 84 | #define be64toh(x) BSWAP_64(x) |
| 85 | #define be32toh(x) ntohl(x) |
| 86 | #define be16toh(x) ntohs(x) |
| 87 | #define le16toh(x) (x) |
| 88 | #define le32toh(x) (x) |
| 89 | #define le64toh(x) (x) |
| 90 | #define htole16(x) (x) |
| 91 | #define htole64(x) (x) |
| 92 | #endif /* __sun */ |
| 93 | |
| 94 | #elif defined __APPLE__ |
| 95 | #include <machine/endian.h> |
| 96 | #include <libkern/OSByteOrder.h> |
| 97 | #if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN |
| 98 | #define be64toh(x) (x) |
| 99 | #define be32toh(x) (x) |
| 100 | #define be16toh(x) (x) |
| 101 | #define le16toh(x) OSSwapInt16(x) |
| 102 | #define le32toh(x) OSSwapInt32(x) |
| 103 | #define le64toh(x) OSSwapInt64(x) |
| 104 | #else |
| 105 | #define be64toh(x) OSSwapInt64(x) |
| 106 | #define be32toh(x) OSSwapInt32(x) |
| 107 | #define be16toh(x) OSSwapInt16(x) |
| 108 | #define le16toh(x) (x) |
| 109 | #define le32toh(x) (x) |
| 110 | #define le64toh(x) (x) |
| 111 | #endif |
| 112 | |
| 113 | #elif defined(_MSC_VER) |
| 114 | #include <intrin.h> |
| 115 | |
| 116 | #define be64toh(x) _byteswap_uint64(x) |
| 117 | #define be32toh(x) _byteswap_ulong(x) |
| 118 | #define be16toh(x) _byteswap_ushort(x) |
| 119 | #define le16toh(x) (x) |
| 120 | #define le32toh(x) (x) |
| 121 | #define le64toh(x) (x) |
| 122 | |
| 123 | #elif defined _AIX /* AIX is always big endian */ |
| 124 | #define be64toh(x) (x) |
| 125 | #define be32toh(x) (x) |
| 126 | #define be16toh(x) (x) |
| 127 | #define le32toh(x) \ |
| 128 | ((((x) & 0xff) << 24) | \ |
| 129 | (((x) & 0xff00) << 8) | \ |
| 130 | (((x) & 0xff0000) >> 8) | \ |
| 131 | (((x) & 0xff000000) >> 24)) |
| 132 | #define le64toh(x) \ |
| 133 | ((((x) & 0x00000000000000ffL) << 56) | \ |
| 134 | (((x) & 0x000000000000ff00L) << 40) | \ |
| 135 | (((x) & 0x0000000000ff0000L) << 24) | \ |
| 136 | (((x) & 0x00000000ff000000L) << 8) | \ |
| 137 | (((x) & 0x000000ff00000000L) >> 8) | \ |
| 138 | (((x) & 0x0000ff0000000000L) >> 24) | \ |
| 139 | (((x) & 0x00ff000000000000L) >> 40) | \ |
| 140 | (((x) & 0xff00000000000000L) >> 56)) |
| 141 | #else |
| 142 | #include <endian.h> |
| 143 | #endif |
| 144 | |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * On Solaris, be64toh is a function, not a macro, so there's no need to error |
| 149 | * if it's not defined. |
| 150 | */ |
| 151 | #if !defined(__sun) && !defined(be64toh) |
| 152 | #error Missing definition for be64toh |
| 153 | #endif |
| 154 | |
| 155 | #ifndef be32toh |
| 156 | #define be32toh(x) ntohl(x) |
| 157 | #endif |
| 158 | |
| 159 | #ifndef be16toh |
| 160 | #define be16toh(x) ntohs(x) |
| 161 | #endif |
| 162 | |
| 163 | #ifndef htobe64 |
| 164 | #define htobe64(x) be64toh(x) |
| 165 | #endif |
| 166 | #ifndef htobe32 |
| 167 | #define htobe32(x) be32toh(x) |
| 168 | #endif |
| 169 | #ifndef htobe16 |
| 170 | #define htobe16(x) be16toh(x) |
| 171 | #endif |
| 172 | |
| 173 | #ifndef htole32 |
| 174 | #define htole32(x) le32toh(x) |
| 175 | #endif |
| 176 | |
| 177 | #endif /* _RDENDIAN_H_ */ |
| 178 | |