forked from idealvin/coost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte_order.h
76 lines (56 loc) · 1.35 KB
/
byte_order.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#ifdef _WIN32
#include <WinSock2.h>
#pragma comment(lib,"WS2_32.lib")
inline unsigned short hton16(unsigned short v) {
return htons(v);
}
inline unsigned int hton32(unsigned int v) {
return htonl(v);
}
inline unsigned long long hton64(unsigned long long v) {
return htonll(v);
}
inline unsigned short ntoh16(unsigned short v) {
return ntohs(v);
}
inline unsigned int ntoh32(unsigned int v) {
return ntohl(v);
}
inline unsigned long long ntoh64(unsigned long long v) {
return ntohll(v);
}
#else
#include <stdint.h>
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define htobe16 OSSwapHostToBigInt16
#define htobe32 OSSwapHostToBigInt32
#define htobe64 OSSwapHostToBigInt64
#define be16toh OSSwapBigToHostInt16
#define be32toh OSSwapBigToHostInt32
#define be64toh OSSwapBigToHostInt64
#elif defined(__linux__)
#include <endian.h>
#else
#include <sys/endian.h>
#endif
inline uint16_t hton16(uint16_t v) {
return htobe16(v);
}
inline uint32_t hton32(uint32_t v) {
return htobe32(v);
}
inline uint64_t hton64(uint64_t v) {
return htobe64(v);
}
inline uint16_t ntoh16(uint16_t v) {
return be16toh(v);
}
inline uint32_t ntoh32(uint32_t v) {
return be32toh(v);
}
inline uint64_t ntoh64(uint64_t v) {
return be64toh(v);
}
#endif