17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52b616c6cSwesolows * Common Development and Distribution License (the "License"). 62b616c6cSwesolows * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 212b616c6cSwesolows 227c478bd9Sstevel@tonic-gate /* 23*da6c28aaSamw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #ifndef _SYS_BYTEORDER_H 417c478bd9Sstevel@tonic-gate #define _SYS_BYTEORDER_H 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 467c478bd9Sstevel@tonic-gate #include <sys/int_types.h> 477c478bd9Sstevel@tonic-gate 482b616c6cSwesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 492b616c6cSwesolows (defined(__i386) || defined(__amd64)) 507c478bd9Sstevel@tonic-gate #include <asm/byteorder.h> 517c478bd9Sstevel@tonic-gate #endif 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifdef __cplusplus 547c478bd9Sstevel@tonic-gate extern "C" { 557c478bd9Sstevel@tonic-gate #endif 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * macros for conversion between host and (internet) network byte order 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint) 627c478bd9Sstevel@tonic-gate /* big-endian */ 637c478bd9Sstevel@tonic-gate #define ntohl(x) (x) 647c478bd9Sstevel@tonic-gate #define ntohs(x) (x) 657c478bd9Sstevel@tonic-gate #define htonl(x) (x) 667c478bd9Sstevel@tonic-gate #define htons(x) (x) 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #elif !defined(ntohl) /* little-endian */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #ifndef _IN_PORT_T 717c478bd9Sstevel@tonic-gate #define _IN_PORT_T 727c478bd9Sstevel@tonic-gate typedef uint16_t in_port_t; 737c478bd9Sstevel@tonic-gate #endif 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #ifndef _IN_ADDR_T 767c478bd9Sstevel@tonic-gate #define _IN_ADDR_T 777c478bd9Sstevel@tonic-gate typedef uint32_t in_addr_t; 787c478bd9Sstevel@tonic-gate #endif 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) 817c478bd9Sstevel@tonic-gate extern uint32_t htonl(uint32_t); 827c478bd9Sstevel@tonic-gate extern uint16_t htons(uint16_t); 837c478bd9Sstevel@tonic-gate extern uint32_t ntohl(uint32_t); 847c478bd9Sstevel@tonic-gate extern uint16_t ntohs(uint16_t); 857c478bd9Sstevel@tonic-gate #else 867c478bd9Sstevel@tonic-gate extern in_addr_t htonl(in_addr_t); 877c478bd9Sstevel@tonic-gate extern in_port_t htons(in_port_t); 887c478bd9Sstevel@tonic-gate extern in_addr_t ntohl(in_addr_t); 897c478bd9Sstevel@tonic-gate extern in_port_t ntohs(in_port_t); 907c478bd9Sstevel@tonic-gate #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */ 917c478bd9Sstevel@tonic-gate #endif 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Macros to reverse byte order 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate #define BSWAP_8(x) ((x) & 0xff) 997c478bd9Sstevel@tonic-gate #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 1007c478bd9Sstevel@tonic-gate #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 1017c478bd9Sstevel@tonic-gate #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate #define BMASK_8(x) ((x) & 0xff) 1047c478bd9Sstevel@tonic-gate #define BMASK_16(x) ((x) & 0xffff) 1057c478bd9Sstevel@tonic-gate #define BMASK_32(x) ((x) & 0xffffffff) 1067c478bd9Sstevel@tonic-gate #define BMASK_64(x) (x) 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * Macros to convert from a specific byte order to/from native byte order 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN 1127c478bd9Sstevel@tonic-gate #define BE_8(x) BMASK_8(x) 1137c478bd9Sstevel@tonic-gate #define BE_16(x) BMASK_16(x) 1147c478bd9Sstevel@tonic-gate #define BE_32(x) BMASK_32(x) 1157c478bd9Sstevel@tonic-gate #define BE_64(x) BMASK_64(x) 1167c478bd9Sstevel@tonic-gate #define LE_8(x) BSWAP_8(x) 1177c478bd9Sstevel@tonic-gate #define LE_16(x) BSWAP_16(x) 1187c478bd9Sstevel@tonic-gate #define LE_32(x) BSWAP_32(x) 1197c478bd9Sstevel@tonic-gate #define LE_64(x) BSWAP_64(x) 1207c478bd9Sstevel@tonic-gate #else 1217c478bd9Sstevel@tonic-gate #define LE_8(x) BMASK_8(x) 1227c478bd9Sstevel@tonic-gate #define LE_16(x) BMASK_16(x) 1237c478bd9Sstevel@tonic-gate #define LE_32(x) BMASK_32(x) 1247c478bd9Sstevel@tonic-gate #define LE_64(x) BMASK_64(x) 1257c478bd9Sstevel@tonic-gate #define BE_8(x) BSWAP_8(x) 1267c478bd9Sstevel@tonic-gate #define BE_16(x) BSWAP_16(x) 1277c478bd9Sstevel@tonic-gate #define BE_32(x) BSWAP_32(x) 1287c478bd9Sstevel@tonic-gate #define BE_64(x) BSWAP_64(x) 1297c478bd9Sstevel@tonic-gate #endif 1307c478bd9Sstevel@tonic-gate 131*da6c28aaSamw /* 132*da6c28aaSamw * Macros to read unaligned values from a specific byte order to 133*da6c28aaSamw * native byte order 134*da6c28aaSamw */ 135*da6c28aaSamw 136*da6c28aaSamw #define BE_IN8(xa) \ 137*da6c28aaSamw *((uint8_t *)(xa)) 138*da6c28aaSamw 139*da6c28aaSamw #define BE_IN16(xa) \ 140*da6c28aaSamw (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1)) 141*da6c28aaSamw 142*da6c28aaSamw #define BE_IN32(xa) \ 143*da6c28aaSamw (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2)) 144*da6c28aaSamw 145*da6c28aaSamw #define BE_IN64(xa) \ 146*da6c28aaSamw (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4)) 147*da6c28aaSamw 148*da6c28aaSamw #define LE_IN8(xa) \ 149*da6c28aaSamw *((uint8_t *)(xa)) 150*da6c28aaSamw 151*da6c28aaSamw #define LE_IN16(xa) \ 152*da6c28aaSamw (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa)) 153*da6c28aaSamw 154*da6c28aaSamw #define LE_IN32(xa) \ 155*da6c28aaSamw (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa)) 156*da6c28aaSamw 157*da6c28aaSamw #define LE_IN64(xa) \ 158*da6c28aaSamw (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa)) 159*da6c28aaSamw 160*da6c28aaSamw /* 161*da6c28aaSamw * Macros to write unaligned values from native byte order to a specific byte 162*da6c28aaSamw * order. 163*da6c28aaSamw */ 164*da6c28aaSamw 165*da6c28aaSamw #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 166*da6c28aaSamw 167*da6c28aaSamw #define BE_OUT16(xa, yv) \ 168*da6c28aaSamw BE_OUT8((uint8_t *)(xa) + 1, yv); \ 169*da6c28aaSamw BE_OUT8((uint8_t *)(xa), (yv) >> 8); 170*da6c28aaSamw 171*da6c28aaSamw #define BE_OUT32(xa, yv) \ 172*da6c28aaSamw BE_OUT16((uint8_t *)(xa) + 2, yv); \ 173*da6c28aaSamw BE_OUT16((uint8_t *)(xa), (yv) >> 16); 174*da6c28aaSamw 175*da6c28aaSamw #define BE_OUT64(xa, yv) \ 176*da6c28aaSamw BE_OUT32((uint8_t *)(xa) + 4, yv); \ 177*da6c28aaSamw BE_OUT32((uint8_t *)(xa), (yv) >> 32); 178*da6c28aaSamw 179*da6c28aaSamw #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 180*da6c28aaSamw 181*da6c28aaSamw #define LE_OUT16(xa, yv) \ 182*da6c28aaSamw LE_OUT8((uint8_t *)(xa), yv); \ 183*da6c28aaSamw LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8); 184*da6c28aaSamw 185*da6c28aaSamw #define LE_OUT32(xa, yv) \ 186*da6c28aaSamw LE_OUT16((uint8_t *)(xa), yv); \ 187*da6c28aaSamw LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16); 188*da6c28aaSamw 189*da6c28aaSamw #define LE_OUT64(xa, yv) \ 190*da6c28aaSamw LE_OUT32((uint8_t *)(xa), yv); \ 191*da6c28aaSamw LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32); 192*da6c28aaSamw 1937c478bd9Sstevel@tonic-gate #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */ 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate #endif 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */ 200