endian.h
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_ENDIAN_H
23 #define NDN_ENDIAN_H
24 
25 #if defined(__APPLE__)
26 
27 #define NDN_CPP_HAVE_ENDIAN_H 1
28 
29 #include <libkern/OSByteOrder.h>
30 #define htobe16(x) OSSwapHostToBigInt16(x)
31 #define htole16(x) OSSwapHostToLittleInt16(x)
32 #define be16toh(x) OSSwapBigToHostInt16(x)
33 #define le16toh(x) OSSwapLittleToHostInt16(x)
34 #define htobe32(x) OSSwapHostToBigInt32(x)
35 #define htole32(x) OSSwapHostToLittleInt32(x)
36 #define be32toh(x) OSSwapBigToHostInt32(x)
37 #define le32toh(x) OSSwapLittleToHostInt32(x)
38 #define htobe64(x) OSSwapHostToBigInt64(x)
39 #define htole64(x) OSSwapHostToLittleInt64(x)
40 #define be64toh(x) OSSwapBigToHostInt64(x)
41 #define le64toh(x) OSSwapLittleToHostInt64(x)
42 
43 #elif defined(_WIN32)
44 
45 #define NDN_CPP_HAVE_ENDIAN_H 1
46 
47 #include <WinSock2.h>
48 #include <stdint.h>
49 #include <ndn-cpp/ndn-cpp-config.h>
50 #define htobe16(x) htons(x)
51 #define be16toh(x) ntohs(x)
52 #define htobe32(x) htonl(x)
53 #define be32toh(x) ntohl(x)
54 #if NDN_CPP_HAVE_HTONLL
55 // Windows 8 has htonll. Assume we have ntohll if we have htonll.
56 #define htobe64(x) htonll(x)
57 #define be64toh(x) ntohll(x)
58 #else
59 // Define directly.
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 // htons(1) == 1 is true for big endian systems.
66 static __inline uint64_t htobe64(uint64_t x) { return htons(1) == 1 ? x : (((uint64_t)htonl((u_long)x)) << 32) + htonl(x >> 32); }
67 static __inline uint64_t be64toh(uint64_t x) { return htons(1) == 1 ? x : (((uint64_t)ntohl((u_long)x)) << 32) + ntohl(x >> 32); }
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 #endif
74 //#define htole16(x)
75 //#define le16toh(x)
76 //#define htole32(x)
77 //#define le32toh(x)
78 //#define htole64(x)
79 //#define le64toh(x)
80 
81 #elif defined(__FreeBSD__)
82 
83 #define NDN_CPP_HAVE_ENDIAN_H 1
84 #include <sys/endian.h>
85 
86 #elif defined(ARDUINO)
87 
88 #define NDN_CPP_HAVE_ENDIAN_H 0
89 
90 #else
91 
92 // Linux, Cygwin, etc.
93 #define NDN_CPP_HAVE_ENDIAN_H 1
94 #include <endian.h>
95 
96 #endif
97 
98 #endif