| 1 |
#ifndef AWS_COMMON_BYTE_ORDER_INL |
| 2 |
#define AWS_COMMON_BYTE_ORDER_INL |
| 3 |
|
| 4 |
/** |
| 5 |
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 |
* SPDX-License-Identifier: Apache-2.0. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <aws/common/byte_order.h> |
| 10 |
#include <aws/common/common.h> |
| 11 |
|
| 12 |
#ifdef _WIN32 |
| 13 |
# include <stdlib.h> |
| 14 |
#else |
| 15 |
# include <netinet/in.h> |
| 16 |
#endif /* _MSC_VER */ |
| 17 |
|
| 18 |
AWS_EXTERN_C_BEGIN |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns 1 if machine is big endian, 0 if little endian. |
| 22 |
* If you compile with even -O1 optimization, this check is completely optimized |
| 23 |
* out at compile time and code which calls "if (aws_is_big_endian())" will do |
| 24 |
* the right thing without branching. |
| 25 |
*/ |
| 26 |
AWS_STATIC_IMPL int aws_is_big_endian(void) { |
| 27 |
const uint16_t z = 0x100; |
| 28 |
return *(const uint8_t *)&z; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Convert 64 bit integer from host to network byte order. |
| 33 |
*/ |
| 34 |
AWS_STATIC_IMPL uint64_t aws_hton64(uint64_t x) { |
| 35 |
if (aws_is_big_endian()) { |
| 36 |
return x; |
| 37 |
} |
| 38 |
#if (defined(__GNUC__) || defined(__clang__)) && !defined(CBMC) |
| 39 |
return __builtin_bswap64(x); |
| 40 |
#elif defined(_MSC_VER) |
| 41 |
return _byteswap_uint64(x); |
| 42 |
#else |
| 43 |
uint32_t low = x & UINT32_MAX; |
| 44 |
uint32_t high = (uint32_t)(x >> 32); |
| 45 |
return ((uint64_t)htonl(low)) << 32 | htonl(high); |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Convert 64 bit integer from network to host byte order. |
| 51 |
*/ |
| 52 |
AWS_STATIC_IMPL uint64_t aws_ntoh64(uint64_t x) { |
| 53 |
return aws_hton64(x); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Convert 32 bit integer from host to network byte order. |
| 58 |
*/ |
| 59 |
AWS_STATIC_IMPL uint32_t aws_hton32(uint32_t x) { |
| 60 |
#ifdef _WIN32 |
| 61 |
return aws_is_big_endian() ? x : _byteswap_ulong(x); |
| 62 |
#else |
| 63 |
return htonl(x); |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Convert 32 bit float from host to network byte order. |
| 69 |
*/ |
| 70 |
AWS_STATIC_IMPL float aws_htonf32(float x) { |
| 71 |
if (aws_is_big_endian()) { |
| 72 |
return x; |
| 73 |
} |
| 74 |
|
| 75 |
uint8_t *f_storage = (uint8_t *)&x; |
| 76 |
|
| 77 |
float ret_value; |
| 78 |
uint8_t *ret_storage = (uint8_t *)&ret_value; |
| 79 |
|
| 80 |
ret_storage[0] = f_storage[3]; |
| 81 |
ret_storage[1] = f_storage[2]; |
| 82 |
ret_storage[2] = f_storage[1]; |
| 83 |
ret_storage[3] = f_storage[0]; |
| 84 |
|
| 85 |
return ret_value; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Convert 64 bit double from host to network byte order. |
| 90 |
*/ |
| 91 |
AWS_STATIC_IMPL double aws_htonf64(double x) { |
| 92 |
if (aws_is_big_endian()) { |
| 93 |
return x; |
| 94 |
} |
| 95 |
|
| 96 |
uint8_t *f_storage = (uint8_t *)&x; |
| 97 |
|
| 98 |
double ret_value; |
| 99 |
uint8_t *ret_storage = (uint8_t *)&ret_value; |
| 100 |
|
| 101 |
ret_storage[0] = f_storage[7]; |
| 102 |
ret_storage[1] = f_storage[6]; |
| 103 |
ret_storage[2] = f_storage[5]; |
| 104 |
ret_storage[3] = f_storage[4]; |
| 105 |
ret_storage[4] = f_storage[3]; |
| 106 |
ret_storage[5] = f_storage[2]; |
| 107 |
ret_storage[6] = f_storage[1]; |
| 108 |
ret_storage[7] = f_storage[0]; |
| 109 |
|
| 110 |
return ret_value; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Convert 32 bit integer from network to host byte order. |
| 115 |
*/ |
| 116 |
AWS_STATIC_IMPL uint32_t aws_ntoh32(uint32_t x) { |
| 117 |
#ifdef _WIN32 |
| 118 |
return aws_is_big_endian() ? x : _byteswap_ulong(x); |
| 119 |
#else |
| 120 |
return ntohl(x); |
| 121 |
#endif |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Convert 32 bit integer from little endian to host byte order. |
| 126 |
*/ |
| 127 |
AWS_STATIC_IMPL uint32_t aws_letoh32(uint32_t x) { |
| 128 |
#ifdef _WIN32 |
| 129 |
AWS_FATAL_ASSERT(!aws_is_big_endian()); |
| 130 |
return x; |
| 131 |
#elif (defined(__GNUC__) || defined(__clang__)) |
| 132 |
return aws_is_big_endian() ? __builtin_bswap32(x) : x; |
| 133 |
#else |
| 134 |
return aws_is_big_endian() |
| 135 |
? ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000) |
| 136 |
: x; |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Convert 32 bit float from network to host byte order. |
| 142 |
*/ |
| 143 |
AWS_STATIC_IMPL float aws_ntohf32(float x) { |
| 144 |
return aws_htonf32(x); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Convert 32 bit float from network to host byte order. |
| 149 |
*/ |
| 150 |
AWS_STATIC_IMPL double aws_ntohf64(double x) { |
| 151 |
return aws_htonf64(x); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Convert 16 bit integer from host to network byte order. |
| 156 |
*/ |
| 157 |
AWS_STATIC_IMPL uint16_t aws_hton16(uint16_t x) { |
| 158 |
#ifdef _WIN32 |
| 159 |
return aws_is_big_endian() ? x : _byteswap_ushort(x); |
| 160 |
#else |
| 161 |
return htons(x); |
| 162 |
#endif |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Convert 16 bit integer from network to host byte order. |
| 167 |
*/ |
| 168 |
AWS_STATIC_IMPL uint16_t aws_ntoh16(uint16_t x) { |
| 169 |
#ifdef _WIN32 |
| 170 |
return aws_is_big_endian() ? x : _byteswap_ushort(x); |
| 171 |
#else |
| 172 |
return ntohs(x); |
| 173 |
#endif |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Convert 16 bit integer from little endian to host byte order. |
| 178 |
*/ |
| 179 |
AWS_STATIC_IMPL uint16_t aws_letoh16(uint16_t x) { |
| 180 |
#ifdef _WIN32 |
| 181 |
AWS_FATAL_ASSERT(!aws_is_big_endian()); |
| 182 |
return x; |
| 183 |
#elif (defined(__GNUC__) || defined(__clang__)) |
| 184 |
return aws_is_big_endian() ? __builtin_bswap16(x) : x; |
| 185 |
#else |
| 186 |
return aws_is_big_endian() ? ((x >> 8) | (x << 8)) : x; |
| 187 |
#endif |
| 188 |
} |
| 189 |
|
| 190 |
AWS_EXTERN_C_END |
| 191 |
|
| 192 |
#endif /* AWS_COMMON_BYTE_ORDER_INL */ |