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 /* 237421SDaniel.Anderson@Sun.COM * Copyright 2008 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) 627421SDaniel.Anderson@Sun.COM #define ntohll(x) (x) 630Sstevel@tonic-gate #define ntohs(x) (x) 640Sstevel@tonic-gate #define htonl(x) (x) 657421SDaniel.Anderson@Sun.COM #define htonll(x) (x) 660Sstevel@tonic-gate #define htons(x) (x) 670Sstevel@tonic-gate 680Sstevel@tonic-gate #elif !defined(ntohl) /* little-endian */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate #ifndef _IN_PORT_T 710Sstevel@tonic-gate #define _IN_PORT_T 720Sstevel@tonic-gate typedef uint16_t in_port_t; 730Sstevel@tonic-gate #endif 740Sstevel@tonic-gate 750Sstevel@tonic-gate #ifndef _IN_ADDR_T 760Sstevel@tonic-gate #define _IN_ADDR_T 770Sstevel@tonic-gate typedef uint32_t in_addr_t; 780Sstevel@tonic-gate #endif 790Sstevel@tonic-gate 800Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) 810Sstevel@tonic-gate extern uint32_t htonl(uint32_t); 820Sstevel@tonic-gate extern uint16_t htons(uint16_t); 837421SDaniel.Anderson@Sun.COM extern uint32_t ntohl(uint32_t); 840Sstevel@tonic-gate extern uint16_t ntohs(uint16_t); 850Sstevel@tonic-gate #else 860Sstevel@tonic-gate extern in_addr_t htonl(in_addr_t); 870Sstevel@tonic-gate extern in_port_t htons(in_port_t); 887421SDaniel.Anderson@Sun.COM extern in_addr_t ntohl(in_addr_t); 890Sstevel@tonic-gate extern in_port_t ntohs(in_port_t); 900Sstevel@tonic-gate #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */ 917421SDaniel.Anderson@Sun.COM #if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__) 927421SDaniel.Anderson@Sun.COM extern uint64_t htonll(uint64_t); 937421SDaniel.Anderson@Sun.COM extern uint64_t ntohll(uint64_t); 947421SDaniel.Anderson@Sun.COM #endif /* !(_XPG4_2||_XPG5) || __EXTENSIONS__ */ 950Sstevel@tonic-gate #endif 960Sstevel@tonic-gate 970Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Macros to reverse byte order 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate #define BSWAP_8(x) ((x) & 0xff) 103*7500SDaniel.Anderson@Sun.COM #if !defined(__i386) && !defined(__amd64) 1040Sstevel@tonic-gate #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 105*7500SDaniel.Anderson@Sun.COM #define BSWAP_32(x) (((uint32_t)(x) << 24) | \ 106*7500SDaniel.Anderson@Sun.COM (((uint32_t)(x) << 8) & 0xff0000) | \ 107*7500SDaniel.Anderson@Sun.COM (((uint32_t)(x) >> 8) & 0xff00) | \ 108*7500SDaniel.Anderson@Sun.COM ((uint32_t)(x) >> 24)) 109*7500SDaniel.Anderson@Sun.COM #else /* x86 */ 110*7500SDaniel.Anderson@Sun.COM #define BSWAP_16(x) htons(x) 111*7500SDaniel.Anderson@Sun.COM #define BSWAP_32(x) htonl(x) 112*7500SDaniel.Anderson@Sun.COM #endif /* !__i386 && !__amd64 */ 113*7500SDaniel.Anderson@Sun.COM 114*7500SDaniel.Anderson@Sun.COM #if (!defined(__i386) && !defined(__amd64)) || \ 115*7500SDaniel.Anderson@Sun.COM ((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__)) 116*7500SDaniel.Anderson@Sun.COM #define BSWAP_64(x) (((uint64_t)(x) << 56) | \ 117*7500SDaniel.Anderson@Sun.COM (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \ 118*7500SDaniel.Anderson@Sun.COM (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \ 119*7500SDaniel.Anderson@Sun.COM (((uint64_t)(x) << 8) & 0xff00000000ULL) | \ 120*7500SDaniel.Anderson@Sun.COM (((uint64_t)(x) >> 8) & 0xff000000ULL) | \ 121*7500SDaniel.Anderson@Sun.COM (((uint64_t)(x) >> 24) & 0xff0000ULL) | \ 122*7500SDaniel.Anderson@Sun.COM (((uint64_t)(x) >> 40) & 0xff00ULL) | \ 123*7500SDaniel.Anderson@Sun.COM ((uint64_t)(x) >> 56)) 124*7500SDaniel.Anderson@Sun.COM #else /* x86 with non-XPG extensions allowed */ 125*7500SDaniel.Anderson@Sun.COM #define BSWAP_64(x) htonll(x) 126*7500SDaniel.Anderson@Sun.COM #endif /* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */ 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate #define BMASK_8(x) ((x) & 0xff) 1290Sstevel@tonic-gate #define BMASK_16(x) ((x) & 0xffff) 1300Sstevel@tonic-gate #define BMASK_32(x) ((x) & 0xffffffff) 1310Sstevel@tonic-gate #define BMASK_64(x) (x) 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Macros to convert from a specific byte order to/from native byte order 1350Sstevel@tonic-gate */ 1360Sstevel@tonic-gate #ifdef _BIG_ENDIAN 1370Sstevel@tonic-gate #define BE_8(x) BMASK_8(x) 1380Sstevel@tonic-gate #define BE_16(x) BMASK_16(x) 1390Sstevel@tonic-gate #define BE_32(x) BMASK_32(x) 1400Sstevel@tonic-gate #define BE_64(x) BMASK_64(x) 1410Sstevel@tonic-gate #define LE_8(x) BSWAP_8(x) 1420Sstevel@tonic-gate #define LE_16(x) BSWAP_16(x) 1430Sstevel@tonic-gate #define LE_32(x) BSWAP_32(x) 1440Sstevel@tonic-gate #define LE_64(x) BSWAP_64(x) 1450Sstevel@tonic-gate #else 1460Sstevel@tonic-gate #define LE_8(x) BMASK_8(x) 1470Sstevel@tonic-gate #define LE_16(x) BMASK_16(x) 1480Sstevel@tonic-gate #define LE_32(x) BMASK_32(x) 1490Sstevel@tonic-gate #define LE_64(x) BMASK_64(x) 1500Sstevel@tonic-gate #define BE_8(x) BSWAP_8(x) 1510Sstevel@tonic-gate #define BE_16(x) BSWAP_16(x) 1520Sstevel@tonic-gate #define BE_32(x) BSWAP_32(x) 1530Sstevel@tonic-gate #define BE_64(x) BSWAP_64(x) 1540Sstevel@tonic-gate #endif 1550Sstevel@tonic-gate 1565331Samw /* 1575331Samw * Macros to read unaligned values from a specific byte order to 1585331Samw * native byte order 1595331Samw */ 1605331Samw 1615331Samw #define BE_IN8(xa) \ 1625331Samw *((uint8_t *)(xa)) 1635331Samw 164*7500SDaniel.Anderson@Sun.COM #if !defined(__i386) && !defined(__amd64) 1655331Samw #define BE_IN16(xa) \ 166*7500SDaniel.Anderson@Sun.COM (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1)) 1675331Samw 1685331Samw #define BE_IN32(xa) \ 169*7500SDaniel.Anderson@Sun.COM (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2)) 170*7500SDaniel.Anderson@Sun.COM 171*7500SDaniel.Anderson@Sun.COM #else /* x86 */ 172*7500SDaniel.Anderson@Sun.COM #define BE_IN16(xa) htons(*((uint16_t *)(void *)(xa))) 173*7500SDaniel.Anderson@Sun.COM #define BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa))) 174*7500SDaniel.Anderson@Sun.COM #endif /* !__i386 && !__amd64 */ 1755331Samw 176*7500SDaniel.Anderson@Sun.COM #if (!defined(__i386) && !defined(__amd64)) || \ 177*7500SDaniel.Anderson@Sun.COM ((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__)) 1785331Samw #define BE_IN64(xa) \ 179*7500SDaniel.Anderson@Sun.COM (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa) + 4)) 180*7500SDaniel.Anderson@Sun.COM #else /* x86 with non-XPG extensions allowed */ 181*7500SDaniel.Anderson@Sun.COM #define BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa))) 182*7500SDaniel.Anderson@Sun.COM #endif /* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */ 1835331Samw 1845331Samw #define LE_IN8(xa) \ 1855331Samw *((uint8_t *)(xa)) 1865331Samw 1875331Samw #define LE_IN16(xa) \ 1885331Samw (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa)) 1895331Samw 1905331Samw #define LE_IN32(xa) \ 1915331Samw (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa)) 1925331Samw 1935331Samw #define LE_IN64(xa) \ 1945331Samw (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa)) 1955331Samw 1965331Samw /* 1975331Samw * Macros to write unaligned values from native byte order to a specific byte 1985331Samw * order. 1995331Samw */ 2005331Samw 2015331Samw #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 2025331Samw 2035331Samw #define BE_OUT16(xa, yv) \ 2045331Samw BE_OUT8((uint8_t *)(xa) + 1, yv); \ 2055331Samw BE_OUT8((uint8_t *)(xa), (yv) >> 8); 2065331Samw 2075331Samw #define BE_OUT32(xa, yv) \ 2085331Samw BE_OUT16((uint8_t *)(xa) + 2, yv); \ 2095331Samw BE_OUT16((uint8_t *)(xa), (yv) >> 16); 2105331Samw 211*7500SDaniel.Anderson@Sun.COM #if (!defined(__i386) && !defined(__amd64)) || \ 212*7500SDaniel.Anderson@Sun.COM ((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__)) 2135331Samw #define BE_OUT64(xa, yv) \ 2145331Samw BE_OUT32((uint8_t *)(xa) + 4, yv); \ 2155331Samw BE_OUT32((uint8_t *)(xa), (yv) >> 32); 216*7500SDaniel.Anderson@Sun.COM #else /* x86 with non-XPG extensions allowed */ 217*7500SDaniel.Anderson@Sun.COM #define BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv)); 218*7500SDaniel.Anderson@Sun.COM #endif /* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */ 2195331Samw 2205331Samw #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 2215331Samw 2225331Samw #define LE_OUT16(xa, yv) \ 2235331Samw LE_OUT8((uint8_t *)(xa), yv); \ 2245331Samw LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8); 2255331Samw 2265331Samw #define LE_OUT32(xa, yv) \ 2275331Samw LE_OUT16((uint8_t *)(xa), yv); \ 2285331Samw LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16); 2295331Samw 2305331Samw #define LE_OUT64(xa, yv) \ 2315331Samw LE_OUT32((uint8_t *)(xa), yv); \ 2325331Samw LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32); 2335331Samw 2340Sstevel@tonic-gate #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */ 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate #ifdef __cplusplus 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate #endif 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */ 241