|
| 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 _ENDIANPORTABLE_H_ |
| 29 | +#define _ENDIANPORTABLE_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(WIN32) || defined(WINx64) |
| 68 | +#include <winsock2.h> |
| 69 | + #if(BYTE_ORDER == LITTLE_ENDIAN) |
| 70 | + #define htobe16(x) htons(x) |
| 71 | + #define htole16(x) (x) |
| 72 | + #define be16toh(x) ntohs(x) |
| 73 | + #define le16toh(x) (x) |
| 74 | + |
| 75 | + #define htobe32(x) htonl(x) |
| 76 | + #define htole32(x) (x) |
| 77 | + #define be32toh(x) ntohl(x) |
| 78 | + #define le32toh(x) (x) |
| 79 | + |
| 80 | + #define htobe64(x) htonll(x) |
| 81 | + #define htole64(x) (x) |
| 82 | + #define be64toh(x) ntohll(x) |
| 83 | + #define le64toh(x) (x) |
| 84 | + #else |
| 85 | + #define htobe16(x) (x) |
| 86 | + #define htole16(x) __builtin_bswap16(x) |
| 87 | + #define be16toh(x) (x) |
| 88 | + #define le16toh(x) __builtin_bswap16(x) |
| 89 | + |
| 90 | + #define htobe32(x) (x) |
| 91 | + #define htole32(x) __builtin_bswap32(x) |
| 92 | + #define be32toh(x) (x) |
| 93 | + #define le32toh(x) __builtin_bswap32(x) |
| 94 | + |
| 95 | + #define htobe64(x) (x) |
| 96 | + #define htole64(x) __builtin_bswap64(x) |
| 97 | + #define be64toh(x) (x) |
| 98 | + #define le64toh(x) __builtin_bswap64(x) |
| 99 | + #endif |
| 100 | +#elif defined __BSD__ |
| 101 | + #include <sys/endian.h> |
| 102 | +#elif defined sun |
| 103 | + #include <sys/byteorder.h> |
| 104 | + #include <sys/isa_defs.h> |
| 105 | +#define __LITTLE_ENDIAN 1234 |
| 106 | +#define __BIG_ENDIAN 4321 |
| 107 | +#ifdef _BIG_ENDIAN |
| 108 | +#define __BYTE_ORDER __BIG_ENDIAN |
| 109 | +#define be64toh(x) (x) |
| 110 | +#define be32toh(x) (x) |
| 111 | +#define be16toh(x) (x) |
| 112 | +#define le16toh(x) ((uint16_t)BSWAP_16(x)) |
| 113 | +#define le32toh(x) BSWAP_32(x) |
| 114 | +#define le64toh(x) BSWAP_64(x) |
| 115 | +# else |
| 116 | +#define __BYTE_ORDER __LITTLE_ENDIAN |
| 117 | +#define be64toh(x) BSWAP_64(x) |
| 118 | +#define be32toh(x) ntohl(x) |
| 119 | +#define be16toh(x) ntohs(x) |
| 120 | +#define le16toh(x) (x) |
| 121 | +#define le32toh(x) (x) |
| 122 | +#define le64toh(x) (x) |
| 123 | +#define htole16(x) (x) |
| 124 | +#define htole64(x) (x) |
| 125 | +#endif /* sun */ |
| 126 | + |
| 127 | +#elif defined __APPLE__ |
| 128 | + #include <machine/endian.h> |
| 129 | + #include <libkern/OSByteOrder.h> |
| 130 | +#if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN |
| 131 | +#define be64toh(x) (x) |
| 132 | +#define be32toh(x) (x) |
| 133 | +#define be16toh(x) (x) |
| 134 | +#define le16toh(x) OSSwapInt16(x) |
| 135 | +#define le32toh(x) OSSwapInt32(x) |
| 136 | +#define le64toh(x) OSSwapInt64(x) |
| 137 | +#else |
| 138 | +#define be64toh(x) OSSwapInt64(x) |
| 139 | +#define be32toh(x) OSSwapInt32(x) |
| 140 | +#define be16toh(x) OSSwapInt16(x) |
| 141 | +#define le16toh(x) (x) |
| 142 | +#define le32toh(x) (x) |
| 143 | +#define le64toh(x) (x) |
| 144 | +#endif |
| 145 | + |
| 146 | +#elif defined(_MSC_VER) |
| 147 | +#include <intrin.h> |
| 148 | + |
| 149 | +#define be64toh(x) _byteswap_uint64(x) |
| 150 | +#define be32toh(x) _byteswap_ulong(x) |
| 151 | +#define be16toh(x) _byteswap_ushort(x) |
| 152 | +#define le16toh(x) (x) |
| 153 | +#define le32toh(x) (x) |
| 154 | +#define le64toh(x) (x) |
| 155 | + |
| 156 | +#elif defined _AIX /* AIX is always big endian */ |
| 157 | +#define be64toh(x) (x) |
| 158 | +#define be32toh(x) (x) |
| 159 | +#define be16toh(x) (x) |
| 160 | +#define le32toh(x) \ |
| 161 | + ((((x) & 0xff) << 24) | \ |
| 162 | + (((x) & 0xff00) << 8) | \ |
| 163 | + (((x) & 0xff0000) >> 8) | \ |
| 164 | + (((x) & 0xff000000) >> 24)) |
| 165 | +#define le64toh(x) \ |
| 166 | + ((((x) & 0x00000000000000ffL) << 56) | \ |
| 167 | + (((x) & 0x000000000000ff00L) << 40) | \ |
| 168 | + (((x) & 0x0000000000ff0000L) << 24) | \ |
| 169 | + (((x) & 0x00000000ff000000L) << 8) | \ |
| 170 | + (((x) & 0x000000ff00000000L) >> 8) | \ |
| 171 | + (((x) & 0x0000ff0000000000L) >> 24) | \ |
| 172 | + (((x) & 0x00ff000000000000L) >> 40) | \ |
| 173 | + (((x) & 0xff00000000000000L) >> 56)) |
| 174 | +#else |
| 175 | + #include <endian.h> |
| 176 | +#endif |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +/* |
| 181 | + * On Solaris, be64toh is a function, not a macro, so there's no need to error |
| 182 | + * if it's not defined. |
| 183 | + */ |
| 184 | +#if !defined(__sun) && !defined(be64toh) |
| 185 | +#error Missing definition for be64toh |
| 186 | +#endif |
| 187 | + |
| 188 | +#ifndef be32toh |
| 189 | +#define be32toh(x) ntohl(x) |
| 190 | +#endif |
| 191 | + |
| 192 | +#ifndef be16toh |
| 193 | +#define be16toh(x) ntohs(x) |
| 194 | +#endif |
| 195 | + |
| 196 | +#ifndef htobe64 |
| 197 | +#define htobe64(x) be64toh(x) |
| 198 | +#endif |
| 199 | +#ifndef htobe32 |
| 200 | +#define htobe32(x) be32toh(x) |
| 201 | +#endif |
| 202 | +#ifndef htobe16 |
| 203 | +#define htobe16(x) be16toh(x) |
| 204 | +#endif |
| 205 | + |
| 206 | +#ifndef htole32 |
| 207 | +#define htole32(x) le32toh(x) |
| 208 | +#endif |
| 209 | + |
| 210 | +#endif /* _ENDIANPORTABLE_H_ */ |
0 commit comments