#ifndef _SYS_ENDIAN_H_
#define _SYS_ENDIAN_H_
#include <sys/featuretest.h>
#define _LITTLE_ENDIAN 1234
#define _BIG_ENDIAN 4321
#define _PDP_ENDIAN 3412
#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
#ifndef _LOCORE
#include <sys/ansi.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#ifndef in_addr_t
typedef __in_addr_t in_addr_t;
#define in_addr_t __in_addr_t
#endif
#ifndef in_port_t
typedef __in_port_t in_port_t;
#define in_port_t __in_port_t
#endif
__BEGIN_DECLS
uint32_t htonl(uint32_t) __attribute__((__const__));
uint16_t htons(uint16_t) __attribute__((__const__));
uint32_t ntohl(uint32_t) __attribute__((__const__));
uint16_t ntohs(uint16_t) __attribute__((__const__));
__END_DECLS
#endif
#endif
#include <machine/endian_machdep.h>
#if _BYTE_ORDER == _LITTLE_ENDIAN
#define _QUAD_HIGHWORD 1
#define _QUAD_LOWWORD 0
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
#define _QUAD_HIGHWORD 0
#define _QUAD_LOWWORD 1
#endif
#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
#define LITTLE_ENDIAN 1234
#define BIG_ENDIAN 4321
#define PDP_ENDIAN 3412
#define BYTE_ORDER _BYTE_ORDER
#ifndef _LOCORE
#include <machine/bswap.h>
#if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
#define ntohl(x) (x)
#define ntohs(x) (x)
#define htonl(x) (x)
#define htons(x) (x)
#define NTOHL(x) (void) (x)
#define NTOHS(x) (void) (x)
#define HTONL(x) (void) (x)
#define HTONS(x) (void) (x)
#else
#if 0
#define ntohl(x) bswap32((uint32_t)(x))
#define ntohs(x) bswap16((uint16_t)(x))
#define htonl(x) bswap32((uint32_t)(x))
#define htons(x) bswap16((uint16_t)(x))
#endif
#define NTOHL(x) (x) = ntohl((uint32_t)(x))
#define NTOHS(x) (x) = ntohs((uint16_t)(x))
#define HTONL(x) (x) = htonl((uint32_t)(x))
#define HTONS(x) (x) = htons((uint16_t)(x))
#endif
#if BYTE_ORDER == BIG_ENDIAN
#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)
#define htole16(x) bswap16((uint16_t)(x))
#define htole32(x) bswap32((uint32_t)(x))
#define htole64(x) bswap64((uint64_t)(x))
#define HTOBE16(x) (void) (x)
#define HTOBE32(x) (void) (x)
#define HTOBE64(x) (void) (x)
#define HTOLE16(x) (x) = bswap16((uint16_t)(x))
#define HTOLE32(x) (x) = bswap32((uint32_t)(x))
#define HTOLE64(x) (x) = bswap64((uint64_t)(x))
#else
#define htobe16(x) bswap16((uint16_t)(x))
#define htobe32(x) bswap32((uint32_t)(x))
#define htobe64(x) bswap64((uint64_t)(x))
#define htole16(x) (x)
#define htole32(x) (x)
#define htole64(x) (x)
#define HTOBE16(x) (x) = bswap16((uint16_t)(x))
#define HTOBE32(x) (x) = bswap32((uint32_t)(x))
#define HTOBE64(x) (x) = bswap64((uint64_t)(x))
#define HTOLE16(x) (void) (x)
#define HTOLE32(x) (void) (x)
#define HTOLE64(x) (void) (x)
#endif
#define be16toh(x) htobe16(x)
#define be32toh(x) htobe32(x)
#define be64toh(x) htobe64(x)
#define le16toh(x) htole16(x)
#define le32toh(x) htole32(x)
#define le64toh(x) htole64(x)
#define BE16TOH(x) HTOBE16(x)
#define BE32TOH(x) HTOBE32(x)
#define BE64TOH(x) HTOBE64(x)
#define LE16TOH(x) HTOLE16(x)
#define LE32TOH(x) HTOLE32(x)
#define LE64TOH(x) HTOLE64(x)
static __inline void __unused
be16enc(void *buf, uint16_t u)
{
uint8_t *p = (uint8_t *)buf;
p[0] = ((unsigned)u >> 8) & 0xff;
p[1] = u & 0xff;
}
static __inline void __unused
le16enc(void *buf, uint16_t u)
{
uint8_t *p = (uint8_t *)buf;
p[0] = u & 0xff;
p[1] = ((unsigned)u >> 8) & 0xff;
}
static __inline uint16_t __unused
be16dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return ((p[0] << 8) | p[1]);
}
static __inline uint16_t __unused
le16dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return ((p[1] << 8) | p[0]);
}
static __inline void __unused
be32enc(void *buf, uint32_t u)
{
uint8_t *p = (uint8_t *)buf;
p[0] = (u >> 24) & 0xff;
p[1] = (u >> 16) & 0xff;
p[2] = (u >> 8) & 0xff;
p[3] = u & 0xff;
}
static __inline void __unused
le32enc(void *buf, uint32_t u)
{
uint8_t *p = (uint8_t *)buf;
p[0] = u & 0xff;
p[1] = (u >> 8) & 0xff;
p[2] = (u >> 16) & 0xff;
p[3] = (u >> 24) & 0xff;
}
static __inline uint32_t __unused
be32dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
}
static __inline uint32_t __unused
le32dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
}
#ifndef _WIN32
static __inline void __unused
be64enc(void *buf, uint64_t u)
{
uint8_t *p = (uint8_t *)buf;
be32enc(p, (uint32_t)(u >> 32));
be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
}
static __inline void __unused
le64enc(void *buf, uint64_t u)
{
uint8_t *p = (uint8_t *)buf;
le32enc(p, (uint32_t)(u & 0xffffffffULL));
le32enc(p + 4, (uint32_t)(u >> 32));
}
static __inline uint64_t __unused
be64dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
}
static __inline uint64_t __unused
le64dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
}
#endif
#endif
#endif
#endif