1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright (c) 2015, Joyent, Inc. 14 */ 15 16 #include <endian.h> 17 18 /* 19 * endian(3C) routines 20 */ 21 22 uint16_t 23 htole16(uint16_t in) 24 { 25 return (((in & 0xff) << 8) | ((in & 0xff00) >> 8)); 26 } 27 28 uint32_t 29 htole32(uint32_t in) 30 { 31 return (((in & 0xffUL) << 24) | 32 (in & 0xff00UL) << 8 | 33 (in & 0xff0000UL) >> 8 | 34 ((in & 0xff000000UL) >> 24)); 35 } 36 37 uint64_t 38 htole64(uint64_t in) 39 { 40 return (((in & 0xffULL) << 56) | 41 ((in & 0xff00ULL) << 40) | 42 ((in & 0xff0000ULL) << 24) | 43 ((in & 0xff000000ULL) << 8) | 44 ((in & 0xff00000000ULL) >> 8) | 45 ((in & 0xff0000000000ULL) >> 24) | 46 ((in & 0xff000000000000ULL) >> 40) | 47 ((in & 0xff00000000000000ULL) >> 56)); 48 } 49 50 uint16_t 51 letoh16(uint16_t in) 52 { 53 return (((in & 0xff) << 8) | ((in & 0xff00) >> 8)); 54 } 55 56 uint16_t 57 le16toh(uint16_t in) 58 { 59 return (((in & 0xff) << 8) | ((in & 0xff00) >> 8)); 60 } 61 62 uint32_t 63 letoh32(uint32_t in) 64 { 65 return (((in & 0xffUL) << 24) | 66 (in & 0xff00UL) << 8 | 67 (in & 0xff0000UL) >> 8 | 68 ((in & 0xff000000UL) >> 24)); 69 } 70 71 uint32_t 72 le32toh(uint32_t in) 73 { 74 return (((in & 0xffUL) << 24) | 75 (in & 0xff00UL) << 8 | 76 (in & 0xff0000UL) >> 8 | 77 ((in & 0xff000000UL) >> 24)); 78 } 79 80 uint64_t 81 letoh64(uint64_t in) 82 { 83 return (((in & 0xffULL) << 56) | 84 ((in & 0xff00ULL) << 40) | 85 ((in & 0xff0000ULL) << 24) | 86 ((in & 0xff000000ULL) << 8) | 87 ((in & 0xff00000000ULL) >> 8) | 88 ((in & 0xff0000000000ULL) >> 24) | 89 ((in & 0xff000000000000ULL) >> 40) | 90 ((in & 0xff00000000000000ULL) >> 56)); 91 } 92 93 uint64_t 94 le64toh(uint64_t in) 95 { 96 return (((in & 0xffULL) << 56) | 97 ((in & 0xff00ULL) << 40) | 98 ((in & 0xff0000ULL) << 24) | 99 ((in & 0xff000000ULL) << 8) | 100 ((in & 0xff00000000ULL) >> 8) | 101 ((in & 0xff0000000000ULL) >> 24) | 102 ((in & 0xff000000000000ULL) >> 40) | 103 ((in & 0xff00000000000000ULL) >> 56)); 104 } 105 106 /* Anything to or from big-endian is a no-op */ 107 108 uint16_t 109 htobe16(uint16_t in) 110 { 111 return (in); 112 } 113 114 uint32_t 115 htobe32(uint32_t in) 116 { 117 return (in); 118 } 119 120 uint64_t 121 htobe64(uint64_t in) 122 { 123 return (in); 124 } 125 126 uint16_t 127 betoh16(uint16_t in) 128 { 129 return (in); 130 } 131 132 uint16_t 133 be16toh(uint16_t in) 134 { 135 return (in); 136 } 137 138 uint32_t 139 betoh32(uint32_t in) 140 { 141 return (in); 142 } 143 144 uint32_t 145 be32toh(uint32_t in) 146 { 147 return (in); 148 } 149 150 uint64_t 151 betoh64(uint64_t in) 152 { 153 return (in); 154 } 155 156 uint64_t 157 be64toh(uint64_t in) 158 { 159 return (in); 160 } 161