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*7421SDaniel.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) 62*7421SDaniel.Anderson@Sun.COM #define ntohll(x) (x) 630Sstevel@tonic-gate #define ntohs(x) (x) 640Sstevel@tonic-gate #define htonl(x) (x) 65*7421SDaniel.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); 83*7421SDaniel.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); 88*7421SDaniel.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) */ 91*7421SDaniel.Anderson@Sun.COM #if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__) 92*7421SDaniel.Anderson@Sun.COM extern uint64_t htonll(uint64_t); 93*7421SDaniel.Anderson@Sun.COM extern uint64_t ntohll(uint64_t); 94*7421SDaniel.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) 1030Sstevel@tonic-gate #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 1040Sstevel@tonic-gate #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 1050Sstevel@tonic-gate #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #define BMASK_8(x) ((x) & 0xff) 1080Sstevel@tonic-gate #define BMASK_16(x) ((x) & 0xffff) 1090Sstevel@tonic-gate #define BMASK_32(x) ((x) & 0xffffffff) 1100Sstevel@tonic-gate #define BMASK_64(x) (x) 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Macros to convert from a specific byte order to/from native byte order 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate #ifdef _BIG_ENDIAN 1160Sstevel@tonic-gate #define BE_8(x) BMASK_8(x) 1170Sstevel@tonic-gate #define BE_16(x) BMASK_16(x) 1180Sstevel@tonic-gate #define BE_32(x) BMASK_32(x) 1190Sstevel@tonic-gate #define BE_64(x) BMASK_64(x) 1200Sstevel@tonic-gate #define LE_8(x) BSWAP_8(x) 1210Sstevel@tonic-gate #define LE_16(x) BSWAP_16(x) 1220Sstevel@tonic-gate #define LE_32(x) BSWAP_32(x) 1230Sstevel@tonic-gate #define LE_64(x) BSWAP_64(x) 1240Sstevel@tonic-gate #else 1250Sstevel@tonic-gate #define LE_8(x) BMASK_8(x) 1260Sstevel@tonic-gate #define LE_16(x) BMASK_16(x) 1270Sstevel@tonic-gate #define LE_32(x) BMASK_32(x) 1280Sstevel@tonic-gate #define LE_64(x) BMASK_64(x) 1290Sstevel@tonic-gate #define BE_8(x) BSWAP_8(x) 1300Sstevel@tonic-gate #define BE_16(x) BSWAP_16(x) 1310Sstevel@tonic-gate #define BE_32(x) BSWAP_32(x) 1320Sstevel@tonic-gate #define BE_64(x) BSWAP_64(x) 1330Sstevel@tonic-gate #endif 1340Sstevel@tonic-gate 1355331Samw /* 1365331Samw * Macros to read unaligned values from a specific byte order to 1375331Samw * native byte order 1385331Samw */ 1395331Samw 1405331Samw #define BE_IN8(xa) \ 1415331Samw *((uint8_t *)(xa)) 1425331Samw 1435331Samw #define BE_IN16(xa) \ 1445331Samw (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1)) 1455331Samw 1465331Samw #define BE_IN32(xa) \ 1475331Samw (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2)) 1485331Samw 1495331Samw #define BE_IN64(xa) \ 1505331Samw (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4)) 1515331Samw 1525331Samw #define LE_IN8(xa) \ 1535331Samw *((uint8_t *)(xa)) 1545331Samw 1555331Samw #define LE_IN16(xa) \ 1565331Samw (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa)) 1575331Samw 1585331Samw #define LE_IN32(xa) \ 1595331Samw (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa)) 1605331Samw 1615331Samw #define LE_IN64(xa) \ 1625331Samw (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa)) 1635331Samw 1645331Samw /* 1655331Samw * Macros to write unaligned values from native byte order to a specific byte 1665331Samw * order. 1675331Samw */ 1685331Samw 1695331Samw #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 1705331Samw 1715331Samw #define BE_OUT16(xa, yv) \ 1725331Samw BE_OUT8((uint8_t *)(xa) + 1, yv); \ 1735331Samw BE_OUT8((uint8_t *)(xa), (yv) >> 8); 1745331Samw 1755331Samw #define BE_OUT32(xa, yv) \ 1765331Samw BE_OUT16((uint8_t *)(xa) + 2, yv); \ 1775331Samw BE_OUT16((uint8_t *)(xa), (yv) >> 16); 1785331Samw 1795331Samw #define BE_OUT64(xa, yv) \ 1805331Samw BE_OUT32((uint8_t *)(xa) + 4, yv); \ 1815331Samw BE_OUT32((uint8_t *)(xa), (yv) >> 32); 1825331Samw 1835331Samw #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 1845331Samw 1855331Samw #define LE_OUT16(xa, yv) \ 1865331Samw LE_OUT8((uint8_t *)(xa), yv); \ 1875331Samw LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8); 1885331Samw 1895331Samw #define LE_OUT32(xa, yv) \ 1905331Samw LE_OUT16((uint8_t *)(xa), yv); \ 1915331Samw LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16); 1925331Samw 1935331Samw #define LE_OUT64(xa, yv) \ 1945331Samw LE_OUT32((uint8_t *)(xa), yv); \ 1955331Samw LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32); 1965331Samw 1970Sstevel@tonic-gate #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */ 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate #ifdef __cplusplus 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate #endif 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */ 204