10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5713Swesolows * Common Development and Distribution License (the "License"). 6713Swesolows * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21713Swesolows 220Sstevel@tonic-gate /* 23*9162SPeter.Dunlap@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #ifndef _SYS_BYTEORDER_H 410Sstevel@tonic-gate #define _SYS_BYTEORDER_H 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <sys/isa_defs.h> 440Sstevel@tonic-gate #include <sys/int_types.h> 450Sstevel@tonic-gate 46713Swesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 47713Swesolows (defined(__i386) || defined(__amd64)) 480Sstevel@tonic-gate #include <asm/byteorder.h> 490Sstevel@tonic-gate #endif 500Sstevel@tonic-gate 510Sstevel@tonic-gate #ifdef __cplusplus 520Sstevel@tonic-gate extern "C" { 530Sstevel@tonic-gate #endif 540Sstevel@tonic-gate 550Sstevel@tonic-gate /* 560Sstevel@tonic-gate * macros for conversion between host and (internet) network byte order 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint) 600Sstevel@tonic-gate /* big-endian */ 610Sstevel@tonic-gate #define ntohl(x) (x) 620Sstevel@tonic-gate #define ntohs(x) (x) 630Sstevel@tonic-gate #define htonl(x) (x) 647860Sopensolaris@drydog.com #define htons(x) (x) 657860Sopensolaris@drydog.com #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 667860Sopensolaris@drydog.com #define ntohll(x) (x) 677421SDaniel.Anderson@Sun.COM #define htonll(x) (x) 687860Sopensolaris@drydog.com #endif /* !_XPG4_2 || __EXTENSIONS__ */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate #elif !defined(ntohl) /* little-endian */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate #ifndef _IN_PORT_T 730Sstevel@tonic-gate #define _IN_PORT_T 740Sstevel@tonic-gate typedef uint16_t in_port_t; 750Sstevel@tonic-gate #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate #ifndef _IN_ADDR_T 780Sstevel@tonic-gate #define _IN_ADDR_T 790Sstevel@tonic-gate typedef uint32_t in_addr_t; 800Sstevel@tonic-gate #endif 810Sstevel@tonic-gate 820Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) 830Sstevel@tonic-gate extern uint32_t htonl(uint32_t); 840Sstevel@tonic-gate extern uint16_t htons(uint16_t); 857421SDaniel.Anderson@Sun.COM extern uint32_t ntohl(uint32_t); 860Sstevel@tonic-gate extern uint16_t ntohs(uint16_t); 870Sstevel@tonic-gate #else 880Sstevel@tonic-gate extern in_addr_t htonl(in_addr_t); 890Sstevel@tonic-gate extern in_port_t htons(in_port_t); 907421SDaniel.Anderson@Sun.COM extern in_addr_t ntohl(in_addr_t); 910Sstevel@tonic-gate extern in_port_t ntohs(in_port_t); 927860Sopensolaris@drydog.com #endif /* !_XPG4_2 || __EXTENSIONS__ || _XPG5 */ 937860Sopensolaris@drydog.com 947860Sopensolaris@drydog.com #if defined(_LP64) || defined(_LONGLONG_TYPE) 957860Sopensolaris@drydog.com #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 967421SDaniel.Anderson@Sun.COM extern uint64_t htonll(uint64_t); 977421SDaniel.Anderson@Sun.COM extern uint64_t ntohll(uint64_t); 987860Sopensolaris@drydog.com #endif /* !_XPG4_2 || __EXTENSIONS__ */ 997860Sopensolaris@drydog.com #endif /* _LP64 || _LONGLONG_TYPE */ 1000Sstevel@tonic-gate #endif 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* 1050Sstevel@tonic-gate * Macros to reverse byte order 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate #define BSWAP_8(x) ((x) & 0xff) 1087500SDaniel.Anderson@Sun.COM #if !defined(__i386) && !defined(__amd64) 1090Sstevel@tonic-gate #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 1107500SDaniel.Anderson@Sun.COM #define BSWAP_32(x) (((uint32_t)(x) << 24) | \ 1117500SDaniel.Anderson@Sun.COM (((uint32_t)(x) << 8) & 0xff0000) | \ 1127500SDaniel.Anderson@Sun.COM (((uint32_t)(x) >> 8) & 0xff00) | \ 1137500SDaniel.Anderson@Sun.COM ((uint32_t)(x) >> 24)) 1147500SDaniel.Anderson@Sun.COM #else /* x86 */ 1157500SDaniel.Anderson@Sun.COM #define BSWAP_16(x) htons(x) 1167500SDaniel.Anderson@Sun.COM #define BSWAP_32(x) htonl(x) 1177500SDaniel.Anderson@Sun.COM #endif /* !__i386 && !__amd64 */ 1187500SDaniel.Anderson@Sun.COM 1197860Sopensolaris@drydog.com #if defined(_LP64) || defined(_LONGLONG_TYPE) 1207860Sopensolaris@drydog.com #if (!defined(__i386) && !defined(__amd64)) 1217500SDaniel.Anderson@Sun.COM #define BSWAP_64(x) (((uint64_t)(x) << 56) | \ 1227500SDaniel.Anderson@Sun.COM (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \ 1237500SDaniel.Anderson@Sun.COM (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \ 1247500SDaniel.Anderson@Sun.COM (((uint64_t)(x) << 8) & 0xff00000000ULL) | \ 1257500SDaniel.Anderson@Sun.COM (((uint64_t)(x) >> 8) & 0xff000000ULL) | \ 1267500SDaniel.Anderson@Sun.COM (((uint64_t)(x) >> 24) & 0xff0000ULL) | \ 1277500SDaniel.Anderson@Sun.COM (((uint64_t)(x) >> 40) & 0xff00ULL) | \ 1287500SDaniel.Anderson@Sun.COM ((uint64_t)(x) >> 56)) 1297860Sopensolaris@drydog.com #else /* x86 */ 1307500SDaniel.Anderson@Sun.COM #define BSWAP_64(x) htonll(x) 1317860Sopensolaris@drydog.com #endif /* !__i386 && !__amd64 */ 1327860Sopensolaris@drydog.com #else /* no uint64_t */ 1337860Sopensolaris@drydog.com #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 1347860Sopensolaris@drydog.com #endif /* _LP64 || _LONGLONG_TYPE */ 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #define BMASK_8(x) ((x) & 0xff) 1370Sstevel@tonic-gate #define BMASK_16(x) ((x) & 0xffff) 1380Sstevel@tonic-gate #define BMASK_32(x) ((x) & 0xffffffff) 1390Sstevel@tonic-gate #define BMASK_64(x) (x) 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * Macros to convert from a specific byte order to/from native byte order 1430Sstevel@tonic-gate */ 1440Sstevel@tonic-gate #ifdef _BIG_ENDIAN 1450Sstevel@tonic-gate #define BE_8(x) BMASK_8(x) 1460Sstevel@tonic-gate #define BE_16(x) BMASK_16(x) 1470Sstevel@tonic-gate #define BE_32(x) BMASK_32(x) 1480Sstevel@tonic-gate #define BE_64(x) BMASK_64(x) 1490Sstevel@tonic-gate #define LE_8(x) BSWAP_8(x) 1500Sstevel@tonic-gate #define LE_16(x) BSWAP_16(x) 1510Sstevel@tonic-gate #define LE_32(x) BSWAP_32(x) 1520Sstevel@tonic-gate #define LE_64(x) BSWAP_64(x) 1530Sstevel@tonic-gate #else 1540Sstevel@tonic-gate #define LE_8(x) BMASK_8(x) 1550Sstevel@tonic-gate #define LE_16(x) BMASK_16(x) 1560Sstevel@tonic-gate #define LE_32(x) BMASK_32(x) 1570Sstevel@tonic-gate #define LE_64(x) BMASK_64(x) 1580Sstevel@tonic-gate #define BE_8(x) BSWAP_8(x) 1590Sstevel@tonic-gate #define BE_16(x) BSWAP_16(x) 1600Sstevel@tonic-gate #define BE_32(x) BSWAP_32(x) 1610Sstevel@tonic-gate #define BE_64(x) BSWAP_64(x) 1620Sstevel@tonic-gate #endif 1630Sstevel@tonic-gate 1645331Samw /* 1655331Samw * Macros to read unaligned values from a specific byte order to 1665331Samw * native byte order 1675331Samw */ 1685331Samw 1695331Samw #define BE_IN8(xa) \ 1705331Samw *((uint8_t *)(xa)) 1715331Samw 1727500SDaniel.Anderson@Sun.COM #if !defined(__i386) && !defined(__amd64) 1735331Samw #define BE_IN16(xa) \ 1747500SDaniel.Anderson@Sun.COM (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1)) 1755331Samw 1765331Samw #define BE_IN32(xa) \ 1777500SDaniel.Anderson@Sun.COM (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2)) 1787500SDaniel.Anderson@Sun.COM 1797500SDaniel.Anderson@Sun.COM #else /* x86 */ 1807500SDaniel.Anderson@Sun.COM #define BE_IN16(xa) htons(*((uint16_t *)(void *)(xa))) 1817500SDaniel.Anderson@Sun.COM #define BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa))) 1827500SDaniel.Anderson@Sun.COM #endif /* !__i386 && !__amd64 */ 1835331Samw 184*9162SPeter.Dunlap@Sun.COM #if (!defined(__i386) && !defined(__amd64)) || \ 185*9162SPeter.Dunlap@Sun.COM (!defined(_LP64) && !defined(_LONGLONG_TYPE)) 1865331Samw #define BE_IN64(xa) \ 1877500SDaniel.Anderson@Sun.COM (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa) + 4)) 1887860Sopensolaris@drydog.com #else /* x86 */ 1897500SDaniel.Anderson@Sun.COM #define BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa))) 190*9162SPeter.Dunlap@Sun.COM #endif /* (!__i386 && !__amd64) || (!_LP64 && !_LONGLONG_TYPE) */ 1915331Samw 1925331Samw #define LE_IN8(xa) \ 1935331Samw *((uint8_t *)(xa)) 1945331Samw 1955331Samw #define LE_IN16(xa) \ 1965331Samw (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa)) 1975331Samw 1985331Samw #define LE_IN32(xa) \ 1995331Samw (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa)) 2005331Samw 2015331Samw #define LE_IN64(xa) \ 2025331Samw (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa)) 2035331Samw 2045331Samw /* 2055331Samw * Macros to write unaligned values from native byte order to a specific byte 2065331Samw * order. 2075331Samw */ 2085331Samw 2095331Samw #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 2105331Samw 2115331Samw #define BE_OUT16(xa, yv) \ 2125331Samw BE_OUT8((uint8_t *)(xa) + 1, yv); \ 2135331Samw BE_OUT8((uint8_t *)(xa), (yv) >> 8); 2145331Samw 2155331Samw #define BE_OUT32(xa, yv) \ 2165331Samw BE_OUT16((uint8_t *)(xa) + 2, yv); \ 2175331Samw BE_OUT16((uint8_t *)(xa), (yv) >> 16); 2185331Samw 2197500SDaniel.Anderson@Sun.COM #if (!defined(__i386) && !defined(__amd64)) || \ 2207860Sopensolaris@drydog.com (!defined(_LP64) && !defined(_LONGLONG_TYPE)) 2215331Samw #define BE_OUT64(xa, yv) \ 2225331Samw BE_OUT32((uint8_t *)(xa) + 4, yv); \ 2235331Samw BE_OUT32((uint8_t *)(xa), (yv) >> 32); 2247860Sopensolaris@drydog.com #else /* x86 with uint64_t */ 2257500SDaniel.Anderson@Sun.COM #define BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv)); 2267860Sopensolaris@drydog.com #endif /* (!__i386 && !__amd64) || (!_LP64 && !_LONGLONG_TYPE) */ 2275331Samw 2285331Samw #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 2295331Samw 2305331Samw #define LE_OUT16(xa, yv) \ 2315331Samw LE_OUT8((uint8_t *)(xa), yv); \ 2325331Samw LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8); 2335331Samw 2345331Samw #define LE_OUT32(xa, yv) \ 2355331Samw LE_OUT16((uint8_t *)(xa), yv); \ 2365331Samw LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16); 2375331Samw 2385331Samw #define LE_OUT64(xa, yv) \ 2395331Samw LE_OUT32((uint8_t *)(xa), yv); \ 2405331Samw LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32); 2415331Samw 2427860Sopensolaris@drydog.com #endif /* !_XPG4_2 || __EXTENSIONS__ */ 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate #ifdef __cplusplus 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate #endif 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */ 249