| 1 | #ifndef AWS_COMMON_BYTE_ORDER_INL |
| 2 | #define AWS_COMMON_BYTE_ORDER_INL |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 8 | * You may not use this file except in compliance with the License. |
| 9 | * A copy of the License is located at |
| 10 | * |
| 11 | * http://aws.amazon.com/apache2.0 |
| 12 | * |
| 13 | * or in the "license" file accompanying this file. This file is distributed |
| 14 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 15 | * express or implied. See the License for the specific language governing |
| 16 | * permissions and limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <aws/common/byte_order.h> |
| 20 | #include <aws/common/common.h> |
| 21 | |
| 22 | #ifdef _MSC_VER |
| 23 | # include <stdlib.h> |
| 24 | #else |
| 25 | # include <netinet/in.h> |
| 26 | #endif /* _MSC_VER */ |
| 27 | |
| 28 | AWS_EXTERN_C_BEGIN |
| 29 | |
| 30 | /** |
| 31 | * Returns 1 if machine is big endian, 0 if little endian. |
| 32 | * If you compile with even -O1 optimization, this check is completely optimized |
| 33 | * out at compile time and code which calls "if (aws_is_big_endian())" will do |
| 34 | * the right thing without branching. |
| 35 | */ |
| 36 | AWS_STATIC_IMPL int aws_is_big_endian(void) { |
| 37 | const uint16_t z = 0x100; |
| 38 | return *(const uint8_t *)&z; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Convert 64 bit integer from host to network byte order. |
| 43 | */ |
| 44 | AWS_STATIC_IMPL uint64_t aws_hton64(uint64_t x) { |
| 45 | if (aws_is_big_endian()) { |
| 46 | return x; |
| 47 | } |
| 48 | #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(CBMC) |
| 49 | uint64_t v; |
| 50 | __asm__("bswap %q0" : "=r" (v) : "0" (x)); |
| 51 | return v; |
| 52 | #elif defined(_MSC_VER) |
| 53 | return _byteswap_uint64(x); |
| 54 | #else |
| 55 | uint32_t low = (uint32_t)x; |
| 56 | uint32_t high = (uint32_t)(x >> 32); |
| 57 | return ((uint64_t)htonl(low)) << 32 | htonl(high); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Convert 64 bit integer from network to host byte order. |
| 63 | */ |
| 64 | AWS_STATIC_IMPL uint64_t aws_ntoh64(uint64_t x) { |
| 65 | return aws_hton64(x); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Convert 32 bit integer from host to network byte order. |
| 70 | */ |
| 71 | AWS_STATIC_IMPL uint32_t aws_hton32(uint32_t x) { |
| 72 | #ifdef _MSC_VER |
| 73 | return aws_is_big_endian() ? x : _byteswap_ulong(x); |
| 74 | #else |
| 75 | return htonl(x); |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Convert 32 bit float from host to network byte order. |
| 81 | */ |
| 82 | AWS_STATIC_IMPL float aws_htonf32(float x) { |
| 83 | if (aws_is_big_endian()) { |
| 84 | return x; |
| 85 | } |
| 86 | |
| 87 | uint8_t *f_storage = (uint8_t *)&x; |
| 88 | |
| 89 | float ret_value; |
| 90 | uint8_t *ret_storage = (uint8_t *)&ret_value; |
| 91 | |
| 92 | ret_storage[0] = f_storage[3]; |
| 93 | ret_storage[1] = f_storage[2]; |
| 94 | ret_storage[2] = f_storage[1]; |
| 95 | ret_storage[3] = f_storage[0]; |
| 96 | |
| 97 | return ret_value; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Convert 64 bit double from host to network byte order. |
| 102 | */ |
| 103 | AWS_STATIC_IMPL double aws_htonf64(double x) { |
| 104 | if (aws_is_big_endian()) { |
| 105 | return x; |
| 106 | } |
| 107 | |
| 108 | uint8_t *f_storage = (uint8_t *)&x; |
| 109 | |
| 110 | double ret_value; |
| 111 | uint8_t *ret_storage = (uint8_t *)&ret_value; |
| 112 | |
| 113 | ret_storage[0] = f_storage[7]; |
| 114 | ret_storage[1] = f_storage[6]; |
| 115 | ret_storage[2] = f_storage[5]; |
| 116 | ret_storage[3] = f_storage[4]; |
| 117 | ret_storage[4] = f_storage[3]; |
| 118 | ret_storage[5] = f_storage[2]; |
| 119 | ret_storage[6] = f_storage[1]; |
| 120 | ret_storage[7] = f_storage[0]; |
| 121 | |
| 122 | return ret_value; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Convert 32 bit integer from network to host byte order. |
| 127 | */ |
| 128 | AWS_STATIC_IMPL uint32_t aws_ntoh32(uint32_t x) { |
| 129 | #ifdef _MSC_VER |
| 130 | return aws_is_big_endian() ? x : _byteswap_ulong(x); |
| 131 | #else |
| 132 | return ntohl(x); |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Convert 32 bit float from network to host byte order. |
| 138 | */ |
| 139 | AWS_STATIC_IMPL float aws_ntohf32(float x) { |
| 140 | return aws_htonf32(x); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Convert 32 bit float from network to host byte order. |
| 145 | */ |
| 146 | AWS_STATIC_IMPL double aws_ntohf64(double x) { |
| 147 | return aws_htonf64(x); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Convert 24 bit integer from host to network byte order. |
| 152 | */ |
| 153 | AWS_STATIC_IMPL uint32_t aws_hton24(uint32_t x) { |
| 154 | AWS_PRECONDITION(x <= 0xFFFFFF, "Input [x] must be representable with at most 3 bytes." ); |
| 155 | if (aws_is_big_endian()) { |
| 156 | return x; |
| 157 | } else { |
| 158 | return aws_hton32(x) >> 8; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Convert 24 bit integer from network to host byte order. |
| 164 | */ |
| 165 | AWS_STATIC_IMPL uint32_t aws_ntoh24(uint32_t x) { |
| 166 | AWS_PRECONDITION((x) <= 0xFFFFFFF, "Input [x] must be representable with at most 3 bytes." ); |
| 167 | if (aws_is_big_endian()) { |
| 168 | return x; |
| 169 | } else { |
| 170 | return aws_ntoh32(x) >> 8; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Convert 16 bit integer from host to network byte order. |
| 176 | */ |
| 177 | AWS_STATIC_IMPL uint16_t aws_hton16(uint16_t x) { |
| 178 | #ifdef _MSC_VER |
| 179 | return aws_is_big_endian() ? x : _byteswap_ushort(x); |
| 180 | #else |
| 181 | return htons(x); |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Convert 16 bit integer from network to host byte order. |
| 187 | */ |
| 188 | AWS_STATIC_IMPL uint16_t aws_ntoh16(uint16_t x) { |
| 189 | #ifdef _MSC_VER |
| 190 | return aws_is_big_endian() ? x : _byteswap_ushort(x); |
| 191 | #else |
| 192 | return ntohs(x); |
| 193 | #endif |
| 194 | } |
| 195 | |
| 196 | AWS_EXTERN_C_END |
| 197 | |
| 198 | #endif /* AWS_COMMON_BYTE_ORDER_INL */ |
| 199 | |