File
Blob: src/workerd/api/crypto/endianness.c++
| 1 | // Copyright (c) 2017-2025 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #include "endianness.h" |
| 6 | |
| 7 | #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) |
| 8 | |
| 9 | #define __WINDOWS__ |
| 10 | |
| 11 | #endif |
| 12 | |
| 13 | #if defined(__linux__) || defined(__CYGWIN__) |
| 14 | |
| 15 | #include <endian.h> |
| 16 | |
| 17 | #elifdef __APPLE__ |
| 18 | |
| 19 | #include <libkern/OSByteOrder.h> |
| 20 | |
| 21 | uint16_t htobe16(uint16_t x) { |
| 22 | return OSSwapHostToBigInt16(x); |
| 23 | } |
| 24 | uint16_t htole16(uint16_t x) { |
| 25 | return OSSwapHostToLittleInt16(x); |
| 26 | } |
| 27 | uint16_t be16toh(uint16_t x) { |
| 28 | return OSSwapBigToHostInt16(x); |
| 29 | } |
| 30 | uint16_t le16toh(uint16_t x) { |
| 31 | return OSSwapLittleToHostInt16(x); |
| 32 | } |
| 33 | |
| 34 | uint32_t htobe32(uint32_t x) { |
| 35 | return OSSwapHostToBigInt32(x); |
| 36 | } |
| 37 | uint32_t htole32(uint32_t x) { |
| 38 | return OSSwapHostToLittleInt32(x); |
| 39 | } |
| 40 | uint32_t be32toh(uint32_t x) { |
| 41 | return OSSwapBigToHostInt32(x); |
| 42 | } |
| 43 | uint32_t le32toh(uint32_t x) { |
| 44 | return OSSwapLittleToHostInt32(x); |
| 45 | } |
| 46 | |
| 47 | uint64_t htobe64(uint64_t x) { |
| 48 | return OSSwapHostToBigInt64(x); |
| 49 | } |
| 50 | uint64_t htole64(uint64_t x) { |
| 51 | return OSSwapHostToLittleInt64(x); |
| 52 | } |
| 53 | uint64_t be64toh(uint64_t x) { |
| 54 | return OSSwapBigToHostInt64(x); |
| 55 | } |
| 56 | uint64_t le64toh(uint64_t x) { |
| 57 | return OSSwapLittleToHostInt64(x); |
| 58 | } |
| 59 | |
| 60 | #elifdef __OpenBSD__ |
| 61 | |
| 62 | #include <sys/endian.h> |
| 63 | |
| 64 | #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
| 65 | |
| 66 | #include <sys/endian.h> |
| 67 | |
| 68 | uint16_t be16toh(uint16_t x) { |
| 69 | return betoh16(x); |
| 70 | } |
| 71 | uint16_t le16toh(uint16_t x) { |
| 72 | return letoh16(x); |
| 73 | } |
| 74 | |
| 75 | uint32_t be32toh(uint32_t x) { |
| 76 | return betoh32(x); |
| 77 | } |
| 78 | uint32_t le32toh(uint32_t x) { |
| 79 | return letoh32(x); |
| 80 | } |
| 81 | |
| 82 | uint64_t be64toh(uint64_t x) { |
| 83 | return betoh64(x); |
| 84 | } |
| 85 | uint64_t le64toh(uint64_t x) { |
| 86 | return letoh64(x); |
| 87 | } |
| 88 | |
| 89 | #elifdef __WINDOWS__ |
| 90 | |
| 91 | #include <winsock2.h> |
| 92 | |
| 93 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 94 | |
| 95 | uint16_t htobe16(uint16_t x) { |
| 96 | return htons(x); |
| 97 | } |
| 98 | uint16_t htole16(uint16_t x) { |
| 99 | return x; |
| 100 | } |
| 101 | uint16_t be16toh(uint16_t x) { |
| 102 | return ntohs(x); |
| 103 | } |
| 104 | uint16_t le16toh(uint16_t x) { |
| 105 | return x; |
| 106 | } |
| 107 | |
| 108 | uint32_t htobe32(uint32_t x) { |
| 109 | return htonl(x); |
| 110 | } |
| 111 | uint32_t htole32(uint32_t x) { |
| 112 | return x; |
| 113 | } |
| 114 | uint32_t be32toh(uint32_t x) { |
| 115 | return ntohl(x); |
| 116 | } |
| 117 | uint32_t le32toh(uint32_t x) { |
| 118 | return x; |
| 119 | } |
| 120 | |
| 121 | uint64_t htobe64(uint64_t x) { |
| 122 | return htonll(x); |
| 123 | } |
| 124 | uint64_t htole64(uint64_t x) { |
| 125 | return x; |
| 126 | } |
| 127 | uint64_t be64toh(uint64_t x) { |
| 128 | return ntohll(x); |
| 129 | } |
| 130 | uint64_t le64toh(uint64_t x) { |
| 131 | return x; |
| 132 | } |
| 133 | |
| 134 | #elif BYTE_ORDER == BIG_ENDIAN |
| 135 | |
| 136 | /* that would be xbox 360 */ |
| 137 | uint16_t htobe16(uint16_t x) { |
| 138 | return x; |
| 139 | } |
| 140 | uint16_t htole16(uint16_t x) { |
| 141 | return __builtin_bswap16(x); |
| 142 | } |
| 143 | uint16_t be16toh(uint16_t x) { |
| 144 | return x; |
| 145 | } |
| 146 | uint16_t le16toh(uint16_t x) { |
| 147 | return __builtin_bswap16(x); |
| 148 | } |
| 149 | |
| 150 | uint32_t htobe32(uint32_t x) { |
| 151 | return x; |
| 152 | } |
| 153 | uint32_t htole32(uint32_t x) { |
| 154 | return __builtin_bswap32(x); |
| 155 | } |
| 156 | uint32_t be32toh(uint32_t x) { |
| 157 | return x; |
| 158 | } |
| 159 | uint32_t le32toh(uint32_t x) { |
| 160 | return __builtin_bswap32(x); |
| 161 | } |
| 162 | |
| 163 | uint64_t htobe64(uint64_t x) { |
| 164 | return x; |
| 165 | } |
| 166 | uint64_t htole64(uint64_t x) { |
| 167 | return __builtin_bswap64(x); |
| 168 | } |
| 169 | uint64_t be64toh(uint64_t x) { |
| 170 | return x; |
| 171 | } |
| 172 | uint64_t le64toh(uint64_t x) { |
| 173 | return __builtin_bswap64(x); |
| 174 | } |
| 175 | |
| 176 | #else |
| 177 | |
| 178 | #error byte order not supported |
| 179 | |
| 180 | #endif |
| 181 | |
| 182 | #else |
| 183 | |
| 184 | #error platform not supported |
| 185 | |
| 186 | #endif |