|
| 1 | +/** |
| 2 | + * @file compat/endian.h |
| 3 | + * @author Martin Pulec <[email protected]> |
| 4 | + * |
| 5 | + * endian.h compat macro for macOS, Windows |
| 6 | + * |
| 7 | + * It will be likely possible to remove later, at least for macOS, because it is |
| 8 | + * required by POSIX 2024. |
| 9 | + */ |
| 10 | +/* |
| 11 | + * Copyright (c) 2025 CESNET, zájmové sdružení právnických osob |
| 12 | + * All rights reserved. |
| 13 | + * |
| 14 | + * Redistribution and use in source and binary forms, with or without |
| 15 | + * modification, is permitted provided that the following conditions |
| 16 | + * are met: |
| 17 | + * |
| 18 | + * 1. Redistributions of source code must retain the above copyright |
| 19 | + * notice, this list of conditions and the following disclaimer. |
| 20 | + * |
| 21 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | + * notice, this list of conditions and the following disclaimer in the |
| 23 | + * documentation and/or other materials provided with the distribution. |
| 24 | + * |
| 25 | + * 3. Neither the name of CESNET nor the names of its contributors may be |
| 26 | + * used to endorse or promote products derived from this software without |
| 27 | + * specific prior written permission. |
| 28 | + * |
| 29 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS |
| 30 | + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, |
| 31 | + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 32 | + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 33 | + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 34 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 35 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 36 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 37 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 38 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 39 | + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 40 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 41 | + */ |
| 42 | + |
| 43 | +#ifndef COMPAT_ENDIAN_H_A5D7C443_895D_465E_A46D_BF5E6DAA833F |
| 44 | +#define COMPAT_ENDIAN_H_A5D7C443_895D_465E_A46D_BF5E6DAA833F |
| 45 | + |
| 46 | +#ifdef __APPLE__ |
| 47 | + // https://github.com/zeromq/zmqpp/issues/164#issuecomment-246346344 |
| 48 | + #include <libkern/OSByteOrder.h> |
| 49 | + #ifndef be16toh |
| 50 | + #define be16toh(x) OSSwapBigToHostInt16(x) |
| 51 | + #define be32toh(x) OSSwapBigToHostInt32(x) |
| 52 | + #define be64toh(x) OSSwapBigToHostInt64(x) |
| 53 | + |
| 54 | + #define htobe16(x) OSSwapHostToBigInt16(x) |
| 55 | + #define htobe32(x) OSSwapHostToBigInt32(x) |
| 56 | + #define htobe64(x) OSSwapHostToBigInt64(x) |
| 57 | + |
| 58 | + #define htole16(x) OSSwapHostToLittleInt16(x) |
| 59 | + #define htole32(x) OSSwapHostToLittleInt32(x) |
| 60 | + #define htole64(x) OSSwapHostToLittleInt64(x) |
| 61 | + |
| 62 | + #define le16toh(x) OSSwapLittleToHostInt16(x) |
| 63 | + #define le32toh(x) OSSwapLittleToHostInt32(x) |
| 64 | + #define le64toh(x) OSSwapLittleToHostInt64(x) |
| 65 | + #endif |
| 66 | + // BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN |
| 67 | + #include <machine/endian.h> |
| 68 | + |
| 69 | +#elif defined _WIN32 |
| 70 | + // little endian archs |
| 71 | + #if defined(_M_AMD64) || defined(_M_IX86) || defined(_M_ARM) || \ |
| 72 | + defined(_M_ARM64) || \ |
| 73 | + (defined(__BYTE_ORDER__) && \ |
| 74 | + __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 75 | + #include <stdlib.h> |
| 76 | + #ifndef be16toh |
| 77 | + #define be16toh(x) _byteswap_ushort(x) |
| 78 | + #define be32toh(x) _byteswap_ulong(x) |
| 79 | + #define be64toh(x) _byteswap_uint64(x) |
| 80 | + |
| 81 | + #define htobe16(x) _byteswap_ushort(x) |
| 82 | + #define htobe32(x) _byteswap_ulong(x) |
| 83 | + #define htobe64(x) _byteswap_uint64(x) |
| 84 | + |
| 85 | + #define htole16(x) (x) |
| 86 | + #define htole32(x) (x) |
| 87 | + #define htole64(x) (x) |
| 88 | + |
| 89 | + #define le16toh(x) (x) |
| 90 | + #define le32toh(x) (x) |
| 91 | + #define le64toh(x) (x) |
| 92 | + #endif |
| 93 | + #ifndef BYTE_ORDER |
| 94 | + #define LITTLE_ENDIAN 1234 |
| 95 | + #define BIG_ENDIAN 4321 |
| 96 | + #define BYTE_ORDER LITTLE_ENDIAN |
| 97 | + #endif |
| 98 | + #else |
| 99 | + #error "Unrecognized Win32 configuration (big-endian or unhandled)" |
| 100 | + #endif |
| 101 | + |
| 102 | +#else |
| 103 | + #include <endian.h> |
| 104 | +#endif |
| 105 | + |
| 106 | +#endif // defined COMPAT_ENDIAN_H_A5D7C443_895D_465E_A46D_BF5E6DAA833F |
0 commit comments