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*5331Samw * Copyright 2007 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 #pragma ident "%Z%%M% %I% %E% SMI" 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include <sys/isa_defs.h> 460Sstevel@tonic-gate #include <sys/int_types.h> 470Sstevel@tonic-gate 48713Swesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 49713Swesolows (defined(__i386) || defined(__amd64)) 500Sstevel@tonic-gate #include <asm/byteorder.h> 510Sstevel@tonic-gate #endif 520Sstevel@tonic-gate 530Sstevel@tonic-gate #ifdef __cplusplus 540Sstevel@tonic-gate extern "C" { 550Sstevel@tonic-gate #endif 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * macros for conversion between host and (internet) network byte order 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate 610Sstevel@tonic-gate #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint) 620Sstevel@tonic-gate /* big-endian */ 630Sstevel@tonic-gate #define ntohl(x) (x) 640Sstevel@tonic-gate #define ntohs(x) (x) 650Sstevel@tonic-gate #define htonl(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); 830Sstevel@tonic-gate 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); 880Sstevel@tonic-gate 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) */ 910Sstevel@tonic-gate #endif 920Sstevel@tonic-gate 930Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * Macros to reverse byte order 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate #define BSWAP_8(x) ((x) & 0xff) 990Sstevel@tonic-gate #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 1000Sstevel@tonic-gate #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 1010Sstevel@tonic-gate #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate #define BMASK_8(x) ((x) & 0xff) 1040Sstevel@tonic-gate #define BMASK_16(x) ((x) & 0xffff) 1050Sstevel@tonic-gate #define BMASK_32(x) ((x) & 0xffffffff) 1060Sstevel@tonic-gate #define BMASK_64(x) (x) 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * Macros to convert from a specific byte order to/from native byte order 1100Sstevel@tonic-gate */ 1110Sstevel@tonic-gate #ifdef _BIG_ENDIAN 1120Sstevel@tonic-gate #define BE_8(x) BMASK_8(x) 1130Sstevel@tonic-gate #define BE_16(x) BMASK_16(x) 1140Sstevel@tonic-gate #define BE_32(x) BMASK_32(x) 1150Sstevel@tonic-gate #define BE_64(x) BMASK_64(x) 1160Sstevel@tonic-gate #define LE_8(x) BSWAP_8(x) 1170Sstevel@tonic-gate #define LE_16(x) BSWAP_16(x) 1180Sstevel@tonic-gate #define LE_32(x) BSWAP_32(x) 1190Sstevel@tonic-gate #define LE_64(x) BSWAP_64(x) 1200Sstevel@tonic-gate #else 1210Sstevel@tonic-gate #define LE_8(x) BMASK_8(x) 1220Sstevel@tonic-gate #define LE_16(x) BMASK_16(x) 1230Sstevel@tonic-gate #define LE_32(x) BMASK_32(x) 1240Sstevel@tonic-gate #define LE_64(x) BMASK_64(x) 1250Sstevel@tonic-gate #define BE_8(x) BSWAP_8(x) 1260Sstevel@tonic-gate #define BE_16(x) BSWAP_16(x) 1270Sstevel@tonic-gate #define BE_32(x) BSWAP_32(x) 1280Sstevel@tonic-gate #define BE_64(x) BSWAP_64(x) 1290Sstevel@tonic-gate #endif 1300Sstevel@tonic-gate 131*5331Samw /* 132*5331Samw * Macros to read unaligned values from a specific byte order to 133*5331Samw * native byte order 134*5331Samw */ 135*5331Samw 136*5331Samw #define BE_IN8(xa) \ 137*5331Samw *((uint8_t *)(xa)) 138*5331Samw 139*5331Samw #define BE_IN16(xa) \ 140*5331Samw (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1)) 141*5331Samw 142*5331Samw #define BE_IN32(xa) \ 143*5331Samw (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2)) 144*5331Samw 145*5331Samw #define BE_IN64(xa) \ 146*5331Samw (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4)) 147*5331Samw 148*5331Samw #define LE_IN8(xa) \ 149*5331Samw *((uint8_t *)(xa)) 150*5331Samw 151*5331Samw #define LE_IN16(xa) \ 152*5331Samw (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa)) 153*5331Samw 154*5331Samw #define LE_IN32(xa) \ 155*5331Samw (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa)) 156*5331Samw 157*5331Samw #define LE_IN64(xa) \ 158*5331Samw (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa)) 159*5331Samw 160*5331Samw /* 161*5331Samw * Macros to write unaligned values from native byte order to a specific byte 162*5331Samw * order. 163*5331Samw */ 164*5331Samw 165*5331Samw #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 166*5331Samw 167*5331Samw #define BE_OUT16(xa, yv) \ 168*5331Samw BE_OUT8((uint8_t *)(xa) + 1, yv); \ 169*5331Samw BE_OUT8((uint8_t *)(xa), (yv) >> 8); 170*5331Samw 171*5331Samw #define BE_OUT32(xa, yv) \ 172*5331Samw BE_OUT16((uint8_t *)(xa) + 2, yv); \ 173*5331Samw BE_OUT16((uint8_t *)(xa), (yv) >> 16); 174*5331Samw 175*5331Samw #define BE_OUT64(xa, yv) \ 176*5331Samw BE_OUT32((uint8_t *)(xa) + 4, yv); \ 177*5331Samw BE_OUT32((uint8_t *)(xa), (yv) >> 32); 178*5331Samw 179*5331Samw #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 180*5331Samw 181*5331Samw #define LE_OUT16(xa, yv) \ 182*5331Samw LE_OUT8((uint8_t *)(xa), yv); \ 183*5331Samw LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8); 184*5331Samw 185*5331Samw #define LE_OUT32(xa, yv) \ 186*5331Samw LE_OUT16((uint8_t *)(xa), yv); \ 187*5331Samw LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16); 188*5331Samw 189*5331Samw #define LE_OUT64(xa, yv) \ 190*5331Samw LE_OUT32((uint8_t *)(xa), yv); \ 191*5331Samw LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32); 192*5331Samw 1930Sstevel@tonic-gate #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */ 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate #ifdef __cplusplus 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate #endif 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */ 200