view compat/endian.c @ 13:2bb1bbac1d0a default tip

Added tag version-1.0.0 for changeset 1926dfc9feb0
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Sun, 04 Aug 2019 21:37:56 +0200
parents d541e748cfd8
children
line wrap: on
line source

/*
 * 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));
}