From f2629280c5a2cdb8ae2f979ae278e1f4bed95e23 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 24 Nov 2020 21:03:21 +0100
Subject: [PATCH 2/3] WIP

---
 include/Makefile                |  8 +++---
 include/endian.h                | 50 +++++++++++++++++++++++++++++++++
 lib/libc/net/ntoh.c             |  2 +-
 sys/cpu/x86_64/include/endian.h | 26 ++++++++---------
 sys/sys/endian.h                | 37 ++++++++++++------------
 5 files changed, 85 insertions(+), 38 deletions(-)
 create mode 100644 include/endian.h

diff --git a/include/Makefile b/include/Makefile
index 16735d0e2a..5840cd4998 100644
--- a/include/Makefile
+++ b/include/Makefile
@@ -10,8 +10,9 @@ CLEANFILES= osreldate.h version vers.c vers.txt
 SUBDIR= arpa protocols rpc rpcsvc xlocale
 INCS=	a.out.h alloca.h ar.h assert.h bitstring.h cpio.h ctype.h db.h \
 	dirent.h disktab.h \
-	dlfcn.h elf.h elf-hints.h err.h float.h fmtmsg.h fnmatch.h fstab.h \
-	fts.h ftw.h getopt.h glob.h grp.h \
+	dlfcn.h elf.h elf-hints.h endian.h err.h \
+	float.h fmtmsg.h fnmatch.h fstab.h fts.h ftw.h \
+	getopt.h glob.h grp.h \
 	iconv.h ieeefp.h ifaddrs.h iso646.h inttypes.h kenv.h \
 	langinfo.h libgen.h limits.h link.h locale.h memory.h \
 	mntopts.h mpool.h mqueue.h monetary.h ndbm.h netconfig.h \
@@ -37,8 +38,7 @@ INCS+=	hesiod.h
 # Only for default SHARED=copies case
 SHDRS=	soundcard.h joystick.h
 
-LHDRS=	aio.h endian.h errno.h fcntl.h linker_set.h \
-	poll.h sched.h syslog.h ucontext.h
+LHDRS=	aio.h errno.h fcntl.h linker_set.h poll.h sched.h syslog.h ucontext.h
 
 # directories which also contain header files that need to be copied.
 # Other directories, like 'bus' and 'netproto', are created using mtree.
diff --git a/include/endian.h b/include/endian.h
new file mode 100644
index 0000000000..d681e2a6bd
--- /dev/null
+++ b/include/endian.h
@@ -0,0 +1,50 @@
+/*-
+ * Copyright (c) 2020 The DragonFly Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _ENDIAN_H_
+#define	_ENDIAN_H_
+
+#include <sys/endian.h>
+
+#ifndef _UINT16_T_DECLARED
+typedef	__uint16_t	uint16_t;
+#define	_UINT16_T_DECLARED
+#endif
+#ifndef _UINT32_T_DECLARED
+typedef	__uint32_t	uint32_t;
+#define	_UINT32_T_DECLARED
+#endif
+#ifndef _UINT64_T_DECLARED
+typedef	__uint64_t	uint64_t;
+#define	_UINT64_T_DECLARED
+#endif
+
+#endif /* !_ENDIAN_H_ */
diff --git a/lib/libc/net/ntoh.c b/lib/libc/net/ntoh.c
index be0c51b165..2bf98ef2ec 100644
--- a/lib/libc/net/ntoh.c
+++ b/lib/libc/net/ntoh.c
@@ -26,7 +26,7 @@
  * $FreeBSD: src/lib/libc/net/ntoh.c,v 1.1 2006/11/06 22:07:47 cognet Exp $
  */
 
-#include <sys/endian.h>
+#include <endian.h>
 
 uint32_t
 htonl(uint32_t hl)
diff --git a/sys/cpu/x86_64/include/endian.h b/sys/cpu/x86_64/include/endian.h
index 9fad122a1b..a37e2912e5 100644
--- a/sys/cpu/x86_64/include/endian.h
+++ b/sys/cpu/x86_64/include/endian.h
@@ -33,37 +33,33 @@
 #ifndef _CPU_ENDIAN_H_
 #define	_CPU_ENDIAN_H_
 
-#ifndef _KERNEL
 #include <sys/cdefs.h>
-#endif
 
 #include <machine/stdint.h>
 
+#if __BSD_VISIBLE
 /*
  * Define the order of 32-bit words in 64-bit words.
  */
 #define	_QUAD_HIGHWORD 1
 #define	_QUAD_LOWWORD 0
+#endif
 
 /*
  * Definitions for byte order, according to byte significance from low
  * address to high.
  */
-#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
-#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
-#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
-
-#define	_BYTE_ORDER	_LITTLE_ENDIAN
+#define	LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
+#define	BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
+#define	BYTE_ORDER	LITTLE_ENDIAN
 
-/*
- * Deprecated variants that don't have enough underscores to be useful in more
- * strict namespaces.
- */
 #if __BSD_VISIBLE
-#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
-#define	BIG_ENDIAN	_BIG_ENDIAN
-#define	PDP_ENDIAN	_PDP_ENDIAN
-#define	BYTE_ORDER	_BYTE_ORDER
+#define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
+
+#define	_LITTLE_ENDIAN	LITTLE_ENDIAN
+#define	_BIG_ENDIAN	BIG_ENDIAN
+#define	_BYTE_ORDER	BYTE_ORDER
+#define	_PDP_ENDIAN	PDP_ENDIAN
 #endif
 
 #ifdef __GNUC__
diff --git a/sys/sys/endian.h b/sys/sys/endian.h
index d175bd8e8f..9be2371583 100644
--- a/sys/sys/endian.h
+++ b/sys/sys/endian.h
@@ -31,52 +31,52 @@
 #ifndef _SYS_ENDIAN_H_
 #define _SYS_ENDIAN_H_
 
-#ifndef _SYS_TYPES_H_
-#include <sys/types.h>
-#endif
 #include <machine/endian.h>
 
+#ifdef __BSD_VISIBLE
 /*
  * General byte order swapping functions.
  */
 #define	bswap16(x)	__bswap16(x)
 #define	bswap32(x)	__bswap32(x)
 #define	bswap64(x)	__bswap64(x)
+#endif
 
 /*
  * Host to big endian, host to little endian, big endian to host, and little
  * endian to host byte order functions as detailed in byteorder(9).
  */
-#if _BYTE_ORDER == _LITTLE_ENDIAN
-#define	htobe16(x)	bswap16((x))
-#define	htobe32(x)	bswap32((x))
-#define	htobe64(x)	bswap64((x))
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define	htobe16(x)	__bswap16((x))
+#define	htobe32(x)	__bswap32((x))
+#define	htobe64(x)	__bswap64((x))
 #define	htole16(x)	((__uint16_t)(x))
 #define	htole32(x)	((__uint32_t)(x))
 #define	htole64(x)	((__uint64_t)(x))
 
-#define	be16toh(x)	bswap16((x))
-#define	be32toh(x)	bswap32((x))
-#define	be64toh(x)	bswap64((x))
+#define	be16toh(x)	__bswap16((x))
+#define	be32toh(x)	__bswap32((x))
+#define	be64toh(x)	__bswap64((x))
 #define	le16toh(x)	((__uint16_t)(x))
 #define	le32toh(x)	((__uint32_t)(x))
 #define	le64toh(x)	((__uint64_t)(x))
-#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
+#else /* BYTE_ORDER != LITTLE_ENDIAN */
 #define	htobe16(x)	((__uint16_t)(x))
 #define	htobe32(x)	((__uint32_t)(x))
 #define	htobe64(x)	((__uint64_t)(x))
-#define	htole16(x)	bswap16((x))
-#define	htole32(x)	bswap32((x))
-#define	htole64(x)	bswap64((x))
+#define	htole16(x)	__bswap16((x))
+#define	htole32(x)	__bswap32((x))
+#define	htole64(x)	__bswap64((x))
 
 #define	be16toh(x)	((__uint16_t)(x))
 #define	be32toh(x)	((__uint32_t)(x))
 #define	be64toh(x)	((__uint64_t)(x))
-#define	le16toh(x)	bswap16((x))
-#define	le32toh(x)	bswap32((x))
-#define	le64toh(x)	bswap64((x))
-#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
+#define	le16toh(x)	__bswap16((x))
+#define	le32toh(x)	__bswap32((x))
+#define	le64toh(x)	__bswap64((x))
+#endif /* BYTE_ORDER == LITTLE_ENDIAN */
 
+#ifdef __BSD_VISIBLE
 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
 
 static __inline __uint16_t
@@ -184,5 +184,6 @@ le64enc(void *pp, __uint64_t u)
 	le32enc(p, u & 0xffffffff);
 	le32enc(p + 4, u >> 32);
 }
+#endif /* __BSD_VISIBLE */
 
 #endif	/* _SYS_ENDIAN_H_ */
-- 
2.30.0

