1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
29626cfc2SDmitri Tikhonov#ifndef LSQUIC_BYTESWAP_H
39626cfc2SDmitri Tikhonov#define LSQUIC_BYTESWAP_H 1
49626cfc2SDmitri Tikhonov
59626cfc2SDmitri Tikhonov#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
69626cfc2SDmitri Tikhonov#include <sys/endian.h>
75392f7a3SLiteSpeed Tech#define bswap_16 bswap16
85392f7a3SLiteSpeed Tech#define bswap_32 bswap32
95392f7a3SLiteSpeed Tech#define bswap_64 bswap64
109626cfc2SDmitri Tikhonov#elif defined(__APPLE__)
119626cfc2SDmitri Tikhonov#include <libkern/OSByteOrder.h>
129626cfc2SDmitri Tikhonov#define bswap_16 OSSwapInt16
139626cfc2SDmitri Tikhonov#define bswap_32 OSSwapInt32
149626cfc2SDmitri Tikhonov#define bswap_64 OSSwapInt64
159626cfc2SDmitri Tikhonov#elif defined(WIN32)
162d296031SDmitri Tikhonov#include <stdlib.h>
179626cfc2SDmitri Tikhonov#define bswap_16 _byteswap_ushort
189626cfc2SDmitri Tikhonov#define bswap_32 _byteswap_ulong
199626cfc2SDmitri Tikhonov#define bswap_64 _byteswap_uint64
209626cfc2SDmitri Tikhonov#else
219626cfc2SDmitri Tikhonov#include <byteswap.h>
229626cfc2SDmitri Tikhonov#endif
239626cfc2SDmitri Tikhonov
245392f7a3SLiteSpeed Tech#if __BYTE_ORDER == __LITTLE_ENDIAN
255392f7a3SLiteSpeed Tech#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
265392f7a3SLiteSpeed Tech    varname = 0;                                                            \
275392f7a3SLiteSpeed Tech    memcpy((unsigned char *) &(varname) + varwidth / 8 - (nbytes), (src),   \
285392f7a3SLiteSpeed Tech                                                                (nbytes));  \
295392f7a3SLiteSpeed Tech    varname = bswap_##varwidth(varname);                                    \
305392f7a3SLiteSpeed Tech} while (0)
315392f7a3SLiteSpeed Tech#else
325392f7a3SLiteSpeed Tech#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
335392f7a3SLiteSpeed Tech    varname = 0;                                                            \
345392f7a3SLiteSpeed Tech    memcpy((unsigned char *) &(varname) + varwidth / 8 - (nbytes), (src),   \
355392f7a3SLiteSpeed Tech                                                                (nbytes));  \
365392f7a3SLiteSpeed Tech} while (0)
375392f7a3SLiteSpeed Tech#endif
385392f7a3SLiteSpeed Tech
399626cfc2SDmitri Tikhonov#endif
40