Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474
   1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 1991-2000, 2003 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 #include <sys/types.h>
  30 #include <netinet/in.h>
  31 













  32 #ifdef  _LITTLE_ENDIAN














  33 uint32_t
  34 htonl(uint32_t in)
  35 {
  36         uint32_t        i;
  37 
  38         i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
  39             (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
  40             (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
  41             (uint32_t)((in & (uint32_t)0x000000ff) << 24);
  42         return (i);
  43 }
  44 
  45 uint32_t
  46 ntohl(uint32_t in)
  47 {
  48         return (htonl(in));
  49 }
  50 
  51 uint16_t
  52 htons(uint16_t in)
  53 {
  54         register int arg = (int)in;
  55         uint16_t i;
  56 
  57         i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
  58         i |= (uint16_t)((arg & 0xff) << 8);
  59         return ((uint16_t)i);
  60 }
  61 
  62 uint16_t
  63 ntohs(uint16_t in)
  64 {
  65         return (htons(in));
  66 }
  67 
  68 #else   /* _LITTLE_ENDIAN */
  69 
  70 #if defined(lint)





  71 






  72 uint32_t
  73 htonl(uint32_t in)
  74 {
  75         return (in);
  76 }
  77 
  78 uint32_t
  79 ntohl(uint32_t in)
  80 {
  81         return (in);
  82 }
  83 
  84 uint16_t
  85 htons(uint16_t in)
  86 {
  87         return (in);
  88 }
  89 
  90 uint16_t
  91 ntohs(uint16_t in)
   1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.

   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 


  26 #include <sys/types.h>
  27 #include <netinet/in.h>
  28 
  29 /*
  30  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
  31  *
  32  * On little endian machines these functions reverse the byte order of the
  33  * input parameter and returns the result.  This is to convert the byte order
  34  * from host byte order (little endian) to network byte order (big endian),
  35  * or vice versa.
  36  *
  37  * On big endian machines these functions just return the input parameter,
  38  * as the host byte order is the same as the network byte order (big endian).
  39  */
  40 
  41 
  42 #ifdef  _LITTLE_ENDIAN
  43 uint64_t
  44 htonll(uint64_t in)
  45 {
  46         return ((uint64_t)htonl((in >> 32) & 0xffffffff) |
  47             ((uint64_t)htonl(in & 0xffffffff) << 32));
  48 }
  49 
  50 uint64_t
  51 ntohll(uint64_t in)
  52 {
  53         return ((uint64_t)ntohl((in >> 32) & 0xffffffff) |
  54             ((uint64_t)ntohl(in & 0xffffffff) << 32));
  55 }
  56 
  57 uint32_t
  58 htonl(uint32_t in)
  59 {
  60         uint32_t        i;
  61 
  62         i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
  63             (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
  64             (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
  65             (uint32_t)((in & (uint32_t)0x000000ff) << 24);
  66         return (i);
  67 }
  68 
  69 uint32_t
  70 ntohl(uint32_t in)
  71 {
  72         return (htonl(in));
  73 }
  74 
  75 uint16_t
  76 htons(uint16_t in)
  77 {
  78         register int arg = (int)in;
  79         uint16_t i;
  80 
  81         i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
  82         i |= (uint16_t)((arg & 0xff) << 8);
  83         return ((uint16_t)i);
  84 }
  85 
  86 uint16_t
  87 ntohs(uint16_t in)
  88 {
  89         return (htons(in));
  90 }
  91 
  92 #else   /* _LITTLE_ENDIAN */
  93 
  94 #if defined(lint)
  95 uint64_t
  96 htonll(uint64_t in)
  97 {
  98         return (in);
  99 }
 100 
 101 uint64_t
 102 ntohll(uint64_t in)
 103 {
 104         return (in);
 105 }
 106 
 107 uint32_t
 108 htonl(uint32_t in)
 109 {
 110         return (in);
 111 }
 112 
 113 uint32_t
 114 ntohl(uint32_t in)
 115 {
 116         return (in);
 117 }
 118 
 119 uint16_t
 120 htons(uint16_t in)
 121 {
 122         return (in);
 123 }
 124 
 125 uint16_t
 126 ntohs(uint16_t in)