1 // Copyright 2017 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <stdint.h> 6 7 #ifndef __BYTE_ORDER__ 8 #error __BYTE_ORDER__ not defined! 9 #endif 10 11 #ifndef __ORDER_LITTLE_ENDIAN__ 12 #error __ORDER_LITTLE_ENDIAN__ not defined! 13 #endif 14 htons(uint16_t val)15uint16_t htons(uint16_t val) { 16 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 17 return __builtin_bswap16(val); 18 #else 19 return val; 20 #endif 21 } 22 ntohs(uint16_t val)23uint16_t ntohs(uint16_t val) { 24 return htons(val); 25 } 26 htonl(uint32_t val)27uint32_t htonl(uint32_t val) { 28 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 29 return __builtin_bswap32(val); 30 #else 31 return val; 32 #endif 33 } 34 ntohl(uint32_t val)35uint32_t ntohl(uint32_t val) { 36 return htonl(val); 37 } 38 39