Home | History | Annotate | Line # | Download | only in libsa
byteorder.c revision 1.1
      1 /*	$NetBSD: byteorder.c,v 1.1 2001/10/30 23:35:33 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include "byteorder.h"
     39 
     40 typedef union {
     41 	uint16_t val;
     42 	uint8_t bytes[2];
     43 } un16;
     44 
     45 typedef union {
     46 	uint32_t val;
     47 	uint8_t bytes[4];
     48 } un32;
     49 
     50 /* 16-bit */
     51 
     52 uint16_t
     53 sa_htobe16(uint16_t val)
     54 {
     55 	un16 un;
     56 
     57 	un.bytes[1] = val & 0xff;
     58 	un.bytes[0] = (val >> 8) & 0xff;
     59 
     60 	return (un.val);
     61 }
     62 
     63 uint16_t
     64 sa_htole16(uint16_t val)
     65 {
     66 	un16 un;
     67 
     68 	un.bytes[0] = val & 0xff;
     69 	un.bytes[1] = (val >> 8) & 0xff;
     70 
     71 	return (un.val);
     72 }
     73 
     74 uint16_t
     75 sa_be16toh(uint16_t val)
     76 {
     77 	un16 un;
     78 
     79 	un.val = val;
     80 
     81 	return ((un.bytes[0] << 8) |
     82 		 un.bytes[1]);
     83 }
     84 
     85 uint16_t
     86 sa_le16toh(uint16_t val)
     87 {
     88 	un16 un;
     89 
     90 	un.val = val;
     91 
     92 	return ((un.bytes[1] << 8) |
     93 		 un.bytes[0]);
     94 }
     95 
     96 /* 32-bit */
     97 
     98 uint32_t
     99 sa_htobe32(uint32_t val)
    100 {
    101 	un32 un;
    102 
    103 	un.bytes[3] = val & 0xff;
    104 	un.bytes[2] = (val >> 8) & 0xff;
    105 	un.bytes[1] = (val >> 16) & 0xff;
    106 	un.bytes[0] = (val >> 24) & 0xff;
    107 
    108 	return (un.val);
    109 }
    110 
    111 uint32_t
    112 sa_htole32(uint32_t val)
    113 {
    114 	un32 un;
    115 
    116 	un.bytes[0] = val & 0xff;
    117 	un.bytes[1] = (val >> 8) & 0xff;
    118 	un.bytes[2] = (val >> 16) & 0xff;
    119 	un.bytes[3] = (val >> 24) & 0xff;
    120 
    121 	return (un.val);
    122 }
    123 
    124 uint32_t
    125 sa_be32toh(uint32_t val)
    126 {
    127 	un32 un;
    128 
    129 	un.val = val;
    130 
    131 	return ((un.bytes[0] << 24) |
    132 		(un.bytes[1] << 16) |
    133 		(un.bytes[2] << 8) |
    134 		 un.bytes[3]);
    135 }
    136 
    137 uint32_t
    138 sa_le32toh(uint32_t val)
    139 {
    140 	un32 un;
    141 
    142 	un.val = val;
    143 
    144 	return ((un.bytes[3] << 24) |
    145 		(un.bytes[2] << 16) |
    146 		(un.bytes[1] << 8) |
    147 		 un.bytes[0]);
    148 }
    149