diff compat/endian.c @ 0:d541e748cfd8

Initial revision
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Tue, 10 Feb 2015 11:29:54 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compat/endian.c	Tue Feb 10 11:29:54 2015 +0100
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2015 Guido Berhoerster <guido+pws@berhoerster.name>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <arpa/inet.h>
+
+#include "pws-compat.h"
+#include "endian.h"
+
+uint16_t
+le16toh(uint16_t little16)
+{
+	unsigned char	*b = (unsigned char *)&little16;
+
+	return ((b[0] << 0) | (b[1] << 8));
+}
+
+uint16_t
+htole16(uint16_t host16)
+{
+	uint16_t	little16;
+	unsigned char	*b = (unsigned char *)&little16;
+
+	b[0] = (unsigned char)(host16 >> 0);
+	b[1] = (unsigned char)(host16 >> 8);
+
+	return (little16);
+}
+
+uint32_t
+le32toh(uint32_t little32)
+{
+	unsigned char	*b = (unsigned char *)&little32;
+
+	return ((b[0] << 0) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24));
+}
+
+uint32_t
+htole32(uint32_t host32)
+{
+	uint32_t	little32;
+	unsigned char	*b = (unsigned char *)&little32;
+
+	b[0] = (unsigned char)(host32 >> 0);
+	b[1] = (unsigned char)(host32 >> 8);
+	b[2] = (unsigned char)(host32 >> 16);
+	b[3] = (unsigned char)(host32 >> 24);
+
+	return (little32);
+}
+
+uint16_t
+be16toh(uint16_t big16)
+{
+	return (ntohs(big16));
+}
+
+uint16_t
+htobe16(uint16_t host16)
+{
+	return (htons(host16));
+}
+
+uint32_t
+be32toh(uint32_t big32)
+{
+	return (ntohl(big32));
+}
+
+uint32_t
+htobe32(uint32_t host32)
+{
+	return (htonl(host32));
+}