1/* $OpenBSD: endian.h,v 1.25 2014/12/21 04:49:00 guenther Exp $ */ 2 3/*- 4 * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27/* 28 * Public definitions for little- and big-endian systems. 29 * This file should be included as <endian.h> in userspace and as 30 * <sys/endian.h> in the kernel. 31 * 32 * System headers that need endian information but that can't or don't 33 * want to export the public names here should include <sys/_endian.h> 34 * and use the internal names: _BYTE_ORDER, _*_ENDIAN, etc. 35 */ 36 37#ifndef _SYS_ENDIAN_H_ 38#define _SYS_ENDIAN_H_ 39 40#include <sys/cdefs.h> 41#include <sys/_endian.h> 42 43/* Public names */ 44#define LITTLE_ENDIAN _LITTLE_ENDIAN 45#define BIG_ENDIAN _BIG_ENDIAN 46#define PDP_ENDIAN _PDP_ENDIAN 47#define BYTE_ORDER _BYTE_ORDER 48 49 50/* 51 * These are specified to be function-like macros to match the spec 52 */ 53#define htobe16(x) __htobe16(x) 54#define htobe32(x) __htobe32(x) 55#define htobe64(x) __htobe64(x) 56#define htole16(x) __htole16(x) 57#define htole32(x) __htole32(x) 58#define htole64(x) __htole64(x) 59 60/* POSIX names */ 61#define be16toh(x) __htobe16(x) 62#define be32toh(x) __htobe32(x) 63#define be64toh(x) __htobe64(x) 64#define le16toh(x) __htole16(x) 65#define le32toh(x) __htole32(x) 66#define le64toh(x) __htole64(x) 67 68 69#if __BSD_VISIBLE 70#define swap16(x) __swap16(x) 71#define swap32(x) __swap32(x) 72#define swap64(x) __swap64(x) 73 74#define swap16_multi(v, n) do { \ 75 __size_t __swap16_multi_n = (n); \ 76 __uint16_t *__swap16_multi_v = (v); \ 77 \ 78 while (__swap16_multi_n) { \ 79 *__swap16_multi_v = swap16(*__swap16_multi_v); \ 80 __swap16_multi_v++; \ 81 __swap16_multi_n--; \ 82 } \ 83} while (0) 84 85/* original BSD names */ 86#define betoh16(x) __htobe16(x) 87#define betoh32(x) __htobe32(x) 88#define betoh64(x) __htobe64(x) 89#define letoh16(x) __htole16(x) 90#define letoh32(x) __htole32(x) 91#define letoh64(x) __htole64(x) 92 93#ifndef htons 94/* these were exposed here before */ 95#define htons(x) __htobe16(x) 96#define htonl(x) __htobe32(x) 97#define ntohs(x) __htobe16(x) 98#define ntohl(x) __htobe32(x) 99#endif 100 101/* ancient stuff */ 102#define NTOHL(x) (x) = ntohl((u_int32_t)(x)) 103#define NTOHS(x) (x) = ntohs((u_int16_t)(x)) 104#define HTONL(x) (x) = htonl((u_int32_t)(x)) 105#define HTONS(x) (x) = htons((u_int16_t)(x)) 106#endif /* __BSD_VISIBLE */ 107 108#ifdef _KERNEL 109/* to/from memory conversions */ 110#define bemtoh16 __bemtoh16 111#define bemtoh32 __bemtoh32 112#define bemtoh64 __bemtoh64 113#define htobem16 __htobem16 114#define htobem32 __htobem32 115#define htobem64 __htobem64 116#define lemtoh16 __lemtoh16 117#define lemtoh32 __lemtoh32 118#define lemtoh64 __lemtoh64 119#define htolem16 __htolem16 120#define htolem32 __htolem32 121#define htolem64 __htolem64 122#endif /* _KERNEL */ 123 124#endif /* _SYS_ENDIAN_H_ */ 125