-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbin_pack_endian.h
121 lines (104 loc) · 3.89 KB
/
bin_pack_endian.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
+----------------------------------------------------------------------+
| BinPack for PHP |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Huqiu Liao <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef BINPACK_ENDIAN_H_
#define BINPACK_ENDIAN_H_
/*
* use ntohl() and bsswap_64() [or the other corresponding function in other platform] to swap bit order by __LITTLE_ENDIAN
*
http://forums.codeguru.com/showthread.php?298741-C-General-What-do-ntohl()-and-htonl()-actually-do
1. 'ntohs()', 'ntohl()', 'htons()', and 'htonl()' are not part of the C standard, and thus do not guarentee portability.
2. The POSIX implementations of 'ntohs()', 'ntohl()', 'htons()' and 'htonl()'
take arguments of 'uint16_t' and 'uint32_t' argument types and can be found in the header file netinet/in.h.
3. The Windows implementations use 'unsigned short' and 'unsigned long' and can be found in the header file winsock2.h.
4. Other variants of 'ntoht()' and 'htont()' may exist on some sytems, such as 'ntohi()'/'htoni()' or 'ntohll()'/'htonll()'.
*/
/* for uint16_t / uint32_t / uint64_t */
#if defined(_MSC_VER) && _MSC_VER < 1600
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#elif defined(_MSC_VER) /* && _MSC_VER >= 1600 */
#include <stdint.h>
#else
#include <stdint.h>
#endif
/* __BYTE_ORDER */
#ifdef _WIN32
#include <winsock2.h>
#ifdef __cplusplus
/* numeric_limits<T>::min,max */
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#endif
#else
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#else
#if defined(OS_MACOSX) || defined(__APPLE__)
#include <machine/endian.h>
#elif defined(OS_SOLARIS)
#include <sys/isa_defs.h>
#ifdef _LITTLE_ENDIAN
#define LITTLE_ENDIAN
#else
#define BIG_ENDIAN
#endif
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLYBSD)
#include <sys/types.h>
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#endif
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define do_binpack_be32(x) ntohl(x)
/* windows */
#if defined(_byteswap_uint64) || _MSC_VER >= 1400
# define do_binpack_be64(x) (_byteswap_uint64(x))
/* linux */
#elif defined(bswap_64)
# define do_binpack_be64(x) bswap_64(x)
/* Mac OS */
#elif defined(__DARWIN_OSSwapInt64)
# define do_binpack_be64(x) __DARWIN_OSSwapInt64(x)
#else
#define do_binpack_be64(x) \
( ((((uint64_t)x) << 56) & 0xff00000000000000ULL ) | \
((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
((((uint64_t)x) >> 56) & 0x00000000000000ffULL ) )
#endif
#elif __BYTE_ORDER == __BIG_ENDIAN
#define do_binpack_be32(x) (x)
#define do_binpack_be64(x) (x)
#else
#error Not supported __BYTE_ORDER
#endif
#endif