1c5e77784SJessica Hamilton/*
2c5e77784SJessica Hamilton * Copyright 2017 Haiku, Inc. All rights reserved.
3c5e77784SJessica Hamilton * Distributed under the terms of the MIT License.
4c5e77784SJessica Hamilton */
5c5e77784SJessica Hamilton#ifndef _BSD_ENDIAN_H_
6c5e77784SJessica Hamilton#define _BSD_ENDIAN_H_
7c5e77784SJessica Hamilton
8c5e77784SJessica Hamilton
9c5e77784SJessica Hamilton#include_next <endian.h>
1049506076SAdrien Destugues#include <features.h>
11c5e77784SJessica Hamilton
12c5e77784SJessica Hamilton
1349506076SAdrien Destugues#ifdef _DEFAULT_SOURCE
14c5e77784SJessica Hamilton
15c5e77784SJessica Hamilton#include <config/HaikuConfig.h>
16c5e77784SJessica Hamilton#include <support/ByteOrder.h>
173d61d207SLeorize#include <support/SupportDefs.h>
18c5e77784SJessica Hamilton
19c5e77784SJessica Hamilton#ifdef __cplusplus
20c5e77784SJessica Hamiltonextern "C" {
21c5e77784SJessica Hamilton#endif
22c5e77784SJessica Hamilton
23c5e77784SJessica Hamilton/*
24c5e77784SJessica Hamilton * General byte order swapping functions.
25c5e77784SJessica Hamilton */
26c5e77784SJessica Hamilton#define	bswap16(x)	__swap_int16(x)
27c5e77784SJessica Hamilton#define	bswap32(x)	__swap_int32(x)
28c5e77784SJessica Hamilton#define	bswap64(x)	__swap_int64(x)
29c5e77784SJessica Hamilton
30c5e77784SJessica Hamilton/*
31c5e77784SJessica Hamilton * Host to big endian, host to little endian, big endian to host, and little
32c5e77784SJessica Hamilton * endian to host byte order functions as detailed in byteorder(9).
33c5e77784SJessica Hamilton */
34c5e77784SJessica Hamilton#if BYTE_ORDER == LITTLE_ENDIAN
35c5e77784SJessica Hamilton#define	htobe16(x)	bswap16((x))
36c5e77784SJessica Hamilton#define	htobe32(x)	bswap32((x))
37c5e77784SJessica Hamilton#define	htobe64(x)	bswap64((x))
38c5e77784SJessica Hamilton#define	htole16(x)	((uint16_t)(x))
39c5e77784SJessica Hamilton#define	htole32(x)	((uint32_t)(x))
40c5e77784SJessica Hamilton#define	htole64(x)	((uint64_t)(x))
41c5e77784SJessica Hamilton
42c5e77784SJessica Hamilton#define	be16toh(x)	bswap16((x))
43c5e77784SJessica Hamilton#define	be32toh(x)	bswap32((x))
44c5e77784SJessica Hamilton#define	be64toh(x)	bswap64((x))
45c5e77784SJessica Hamilton#define	le16toh(x)	((uint16_t)(x))
46c5e77784SJessica Hamilton#define	le32toh(x)	((uint32_t)(x))
47c5e77784SJessica Hamilton#define	le64toh(x)	((uint64_t)(x))
48c5e77784SJessica Hamilton#else /* BYTE_ORDER != LITTLE_ENDIAN */
49c5e77784SJessica Hamilton#define	htobe16(x)	((uint16_t)(x))
50c5e77784SJessica Hamilton#define	htobe32(x)	((uint32_t)(x))
51c5e77784SJessica Hamilton#define	htobe64(x)	((uint64_t)(x))
52c5e77784SJessica Hamilton#define	htole16(x)	bswap16((x))
53c5e77784SJessica Hamilton#define	htole32(x)	bswap32((x))
54c5e77784SJessica Hamilton#define	htole64(x)	bswap64((x))
55c5e77784SJessica Hamilton
56c5e77784SJessica Hamilton#define	be16toh(x)	((uint16_t)(x))
57c5e77784SJessica Hamilton#define	be32toh(x)	((uint32_t)(x))
58c5e77784SJessica Hamilton#define	be64toh(x)	((uint64_t)(x))
59c5e77784SJessica Hamilton#define	le16toh(x)	bswap16((x))
60c5e77784SJessica Hamilton#define	le32toh(x)	bswap32((x))
61c5e77784SJessica Hamilton#define	le64toh(x)	bswap64((x))
62c5e77784SJessica Hamilton#endif /* BYTE_ORDER == LITTLE_ENDIAN */
63c5e77784SJessica Hamilton
643d61d207SLeorize/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
653d61d207SLeorize
663d61d207SLeorizestatic __inline uint32_t
673d61d207SLeorizebe32dec(const void *pp)
683d61d207SLeorize{
693d61d207SLeorize	uint8_t const *p = (uint8_t const *)pp;
703d61d207SLeorize
713d61d207SLeorize	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
723d61d207SLeorize}
733d61d207SLeorize
743d61d207SLeorizestatic __inline uint64_t
753d61d207SLeorizebe64dec(const void *pp)
763d61d207SLeorize{
773d61d207SLeorize	uint8_t const *p = (uint8_t const *)pp;
783d61d207SLeorize
793d61d207SLeorize	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
803d61d207SLeorize}
813d61d207SLeorize
823d61d207SLeorizestatic __inline void
833d61d207SLeorizebe32enc(void *pp, uint32_t u)
843d61d207SLeorize{
853d61d207SLeorize	uint8_t *p = (uint8_t *)pp;
863d61d207SLeorize
873d61d207SLeorize	p[0] = (u >> 24) & 0xff;
883d61d207SLeorize	p[1] = (u >> 16) & 0xff;
893d61d207SLeorize	p[2] = (u >> 8) & 0xff;
903d61d207SLeorize	p[3] = u & 0xff;
913d61d207SLeorize}
923d61d207SLeorize
933d61d207SLeorizestatic __inline void
943d61d207SLeorizebe64enc(void *pp, uint64_t u)
953d61d207SLeorize{
963d61d207SLeorize	uint8_t *p = (uint8_t *)pp;
973d61d207SLeorize
983d61d207SLeorize	be32enc(p, (uint32_t)(u >> 32));
993d61d207SLeorize	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
1003d61d207SLeorize}
1013d61d207SLeorize
102c5e77784SJessica Hamilton#ifdef __cplusplus
103c5e77784SJessica Hamilton}
104c5e77784SJessica Hamilton#endif
105c5e77784SJessica Hamilton
106c5e77784SJessica Hamilton
107c5e77784SJessica Hamilton#endif
108c5e77784SJessica Hamilton
109c5e77784SJessica Hamilton
110c5e77784SJessica Hamilton#endif	/* _BSD_ENDIAN_H_ */
111