lsquic_byteswap.h revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_BYTESWAP_H
3#define LSQUIC_BYTESWAP_H 1
4
5#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
6#include <sys/endian.h>
7#define bswap_16 bswap16
8#define bswap_32 bswap32
9#define bswap_64 bswap64
10#elif defined(__APPLE__)
11#include <libkern/OSByteOrder.h>
12#define bswap_16 OSSwapInt16
13#define bswap_32 OSSwapInt32
14#define bswap_64 OSSwapInt64
15#elif defined(WIN32)
16#include <stdlib.h>
17#define bswap_16 _byteswap_ushort
18#define bswap_32 _byteswap_ulong
19#define bswap_64 _byteswap_uint64
20#else
21#include <byteswap.h>
22#endif
23
24#if __BYTE_ORDER == __LITTLE_ENDIAN
25#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
26    varname = 0;                                                            \
27    memcpy((unsigned char *) &(varname) + varwidth / 8 - (nbytes), (src),   \
28                                                                (nbytes));  \
29    varname = bswap_##varwidth(varname);                                    \
30} while (0)
31#else
32#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
33    varname = 0;                                                            \
34    memcpy((unsigned char *) &(varname) + varwidth / 8 - (nbytes), (src),   \
35                                                                (nbytes));  \
36} while (0)
37#endif
38
39#endif
40