1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 #ifndef H_ENDIAN_ 21 #define H_ENDIAN_ 22 23 #include <inttypes.h> 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 /* Internal helpers */ 30 #ifndef os_bswap_64 31 #define os_bswap_64(x) ((uint64_t) \ 32 ((((x) & 0xff00000000000000ull) >> 56) | \ 33 (((x) & 0x00ff000000000000ull) >> 40) | \ 34 (((x) & 0x0000ff0000000000ull) >> 24) | \ 35 (((x) & 0x000000ff00000000ull) >> 8) | \ 36 (((x) & 0x00000000ff000000ull) << 8) | \ 37 (((x) & 0x0000000000ff0000ull) << 24) | \ 38 (((x) & 0x000000000000ff00ull) << 40) | \ 39 (((x) & 0x00000000000000ffull) << 56))) 40 #endif 41 42 #ifndef os_bswap_32 43 #define os_bswap_32(x) ((uint32_t) \ 44 ((((x) & 0xff000000) >> 24) | \ 45 (((x) & 0x00ff0000) >> 8) | \ 46 (((x) & 0x0000ff00) << 8) | \ 47 (((x) & 0x000000ff) << 24))) 48 #endif 49 50 #ifndef os_bswap_16 51 #define os_bswap_16(x) ((uint16_t) \ 52 ((((x) & 0xff00) >> 8) | \ 53 (((x) & 0x00ff) << 8))) 54 #endif 55 56 #if defined (__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) 57 58 #ifndef ntohll 59 #define ntohll(x) ((uint64_t)(x)) 60 #endif 61 62 #ifndef htonll 63 #define htonll(x) ((uint64_t)(x)) 64 #endif 65 66 #ifndef ntohl 67 #define ntohl(x) ((uint32_t)(x)) 68 #endif 69 70 #ifndef htonl 71 #define htonl(x) ((uint32_t)(x)) 72 #endif 73 74 #ifndef ntohs 75 #define ntohs(x) ((uint16_t)(x)) 76 #endif 77 78 #ifndef htons 79 #define htons(x) ((uint16_t)(x)) 80 #endif 81 82 #ifndef htobe16 83 #define htobe16(x) ((uint16_t)(x)) 84 #endif 85 86 #ifndef htole16 87 #define htole16(x) os_bswap_16 (x) 88 #endif 89 90 #ifndef be16toh 91 #define be16toh(x) ((uint16_t)(x)) 92 #endif 93 94 #ifndef le16toh 95 #define le16toh(x) os_bswap_16 (x) 96 #endif 97 98 #ifndef htobe32 99 #define htobe32(x) ((uint32_t)(x)) 100 #endif 101 102 #ifndef htole32 103 #define htole32(x) os_bswap_32 (x) 104 #endif 105 106 #ifndef be32toh 107 #define be32toh(x) ((uint32_t)(x)) 108 #endif 109 110 #ifndef le32toh 111 #define le32toh(x) os_bswap_32 (x) 112 #endif 113 114 #ifndef htobe64 115 #define htobe64(x) ((uint64_t)(x)) 116 #endif 117 118 #ifndef htole64 119 #define htole64(x) os_bswap_64 (x) 120 #endif 121 122 #ifndef be64toh 123 #define be64toh(x) ((uint64_t)(x)) 124 #endif 125 126 #ifndef le64toh 127 #define le64toh(x) os_bswap_64 (x) 128 #endif 129 130 #else 131 132 #ifndef ntohll 133 #define ntohll(x) os_bswap_64(x) 134 #endif 135 136 #ifndef htonll 137 #define htonll ntohll 138 #endif 139 140 #ifndef ntohl 141 #define ntohl(x) os_bswap_32(x) 142 #endif 143 144 #ifndef htonl 145 #define htonl ntohl 146 #endif 147 148 #ifndef htons 149 #define htons(x) os_bswap_16(x) 150 #endif 151 152 #ifndef ntohs 153 #define ntohs htons 154 #endif 155 156 #ifndef htobe16 157 #define htobe16(x) os_bswap_16(x) 158 #endif 159 160 #ifndef htole16 161 #define htole16(x) ((uint16_t)(x)) 162 #endif 163 164 #ifndef be16toh 165 #define be16toh(x) os_bswap_16(x) 166 #endif 167 168 #ifndef le16toh 169 #define le16toh(x) ((uint16_t)(x)) 170 #endif 171 172 #ifndef htobe32 173 #define htobe32(x) os_bswap_32(x) 174 #endif 175 176 #ifndef htole32 177 #define htole32(x) ((uint32_t)(x)) 178 #endif 179 180 #ifndef be32toh 181 #define be32toh(x) os_bswap_32(x) 182 #endif 183 184 #ifndef le32toh 185 #define le32toh(x) ((uint32_t)(x)) 186 #endif 187 188 #ifndef htobe64 189 #define htobe64(x) os_bswap64(x) 190 #endif 191 192 #ifndef htole64 193 #define htole64(x) ((uint64_t)(x)) 194 #endif 195 196 #ifndef be64toh 197 #define be64toh(x) os_bswap64(x) 198 #endif 199 200 #ifndef le64toh 201 #define le64toh(x) ((uint64_t)(x)) 202 #endif 203 204 #endif 205 206 void put_le16(void *buf, uint16_t x); 207 void put_le32(void *buf, uint32_t x); 208 void put_le64(void *buf, uint64_t x); 209 uint16_t get_le16(const void *buf); 210 uint32_t get_le32(const void *buf); 211 uint64_t get_le64(const void *buf); 212 void put_be16(void *buf, uint16_t x); 213 void put_be32(void *buf, uint32_t x); 214 void put_be64(void *buf, uint64_t x); 215 uint16_t get_be16(const void *buf); 216 uint32_t get_be32(const void *buf); 217 uint64_t get_be64(const void *buf); 218 void swap_in_place(void *buf, int len); 219 void swap_buf(uint8_t *dst, const uint8_t *src, int len); 220 221 #ifdef __cplusplus 222 } 223 #endif 224 225 #endif 226