CUBRID Engine  latest
byte_order.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * byte_order.h - Definitions related to disk storage order
22  */
23 
24 #ifndef _BYTE_ORDER_H_
25 #define _BYTE_ORDER_H_
26 
27 #ident "$Id$"
28 
29 #include "system.h"
30 
31 #if defined (LINUX)
32 #include <arpa/inet.h>
33 #endif /* LINUX */
34 
35 #if defined(sun)
36 #include <sys/types.h>
37 #endif /* sun */
38 
39 #if defined(HPUX)
40 #include <netinet/in.h>
41 #endif /* HPUX */
42 
43 #if defined(_AIX)
44 #include <net/nh.h>
45 #endif /* _AIX */
46 
47 #if defined(WINDOWS)
48 #include <winsock2.h>
49 #if defined(_MSC_VER) && _MSC_VER >= 1700
50 /* We could not find when ntohf/ntohd/htonf/htond were exactly added. The only reference we could find are here:
51 * https://msdn.microsoft.com/en-us/library/windows/desktop/jj710201(v=vs.85).aspx (ntohf, links to others at the bottom).
52 */
53 #define OR_HAVE_NTOHF
54 #define OR_HAVE_NTOHD
55 #define OR_HAVE_HTONF
56 #define OR_HAVE_HTOND
57 #endif
58 #endif /* WINDOWS */
59 
60 #define OR_LITTLE_ENDIAN 1234
61 #define OR_BIG_ENDIAN 4321
62 
63 #if defined(HPUX) || defined(_AIX) || defined(sparc)
64 #define OR_BYTE_ORDER OR_BIG_ENDIAN
65 #else /* HPUX || _AIX || sparc */
66 #define OR_BYTE_ORDER OR_LITTLE_ENDIAN /* WINDOWS, LINUX, x86_SOLARIS */
67 #endif /* HPUX || _AIX || sparc */
68 
69 #if defined(HPUX) || defined(_AIX) || defined(WINDOWS) || defined(LINUX)
70 #define OR_HAVE_NTOHS
71 #define OR_HAVE_NTOHL
72 #define OR_HAVE_HTONS
73 #define OR_HAVE_HTONL
74 #endif /* HPUX || _AIX || WINDOWS || LINUX */
75 
76 
77 
78 typedef union moving_van MOVING_VAN;
80 {
81  struct
82  {
83  unsigned int buf[2];
84  } bits;
85  double dbl;
86 };
87 
88 #if !defined(IA64)
89 #define OR_MOVE_DOUBLE(src, dst) \
90  (((MOVING_VAN *)(dst))->bits = ((MOVING_VAN *)(src))->bits)
91 #else /* !IA64 */
92 #define OR_MOVE_DOUBLE(src, dst) \
93  memcpy(((MOVING_VAN*)(dst))->bits.buf, ((MOVING_VAN *)(src))->bits.buf, \
94  sizeof(MOVING_VAN))
95 #endif /* !IA64 */
96 
97 #if OR_BYTE_ORDER == OR_LITTLE_ENDIAN
98 
99 #ifndef OR_HAVE_NTOHS
100 extern unsigned short ntohs (unsigned short);
101 #endif /* !OR_HAVE_NTOHS */
102 
103 #ifndef OR_HAVE_NTOHL
104 extern unsigned int ntohl (unsigned int);
105 #endif /* !OR_HAVE_NTOHL */
106 
107 extern UINT64 ntohi64 (UINT64);
108 
109 #ifndef OR_HAVE_NTOHF
110 extern float ntohf (UINT32 from);
111 #endif /* !OR_HAVE_NTOHF */
112 
113 #ifndef OR_HAVE_NTOHD
114 extern double ntohd (UINT64 from);
115 #endif /* !OR_HAVE_NTOHD */
116 
117 #ifndef OR_HAVE_HTONS
118 extern unsigned short htons (unsigned short);
119 #endif /* !OR_HAVE_HTONS */
120 
121 #ifndef OR_HAVE_HTONL
122 extern unsigned int htonl (unsigned int);
123 #endif /* !OR_HAVE_HTONL */
124 
125 extern UINT64 htoni64 (UINT64);
126 
127 #ifndef OR_HAVE_HTONF
128 extern UINT32 htonf (float);
129 #endif /* !OR_HAVE_HTONF */
130 
131 #ifndef OR_HAVE_HTOND
132 extern UINT64 htond (double);
133 #endif /* !OR_HAVE_HTOND */
134 
135 #else /* OR_BYTE_ORDER == OR_LITTLE_ENDIAN */
136 
137 #ifndef OR_HAVE_NTOHS
138 #define ntohs(x) (x)
139 #endif /* !OR_HAVE_NTOHS */
140 
141 #ifndef OR_HAVE_NTOHL
142 #define ntohl(x) (x)
143 #endif /* !OR_HAVE_NTOHL */
144 
145 #define ntohi64(x) (x)
146 
147 #ifndef OR_HAVE_NTOHF
148 #define ntohf(ptr, value) (*(value) = *(ptr))
149 #endif /* !OR_HAVE_NTOHF */
150 
151 #ifndef OR_HAVE_NTOHD
152 #define ntohd(ptr, value) OR_MOVE_DOUBLE(ptr, value)
153 #endif /* !OR_HAVE_NTOHD */
154 
155 #ifndef OR_HAVE_HTONS
156 #define htons(x) (x)
157 #endif /* !OR_HAVE_HTONS */
158 
159 #ifndef OR_HAVE_HTONL
160 #define htonl(x) (x)
161 #endif /* !OR_HAVE_HTONL */
162 
163 #define htoni64(x) (x)
164 
165 #ifndef OR_HAVE_HTONF
166 #define htonf(ptr, value) (*(ptr) = *(value))
167 #endif /* !OR_HAVE_HTONF */
168 
169 #ifndef OR_HAVE_HTOND
170 #define htond(ptr, value) OR_MOVE_DOUBLE(value, ptr)
171 #endif /* !OR_HAVE_HTOND */
172 
173 #endif /* OR_BYTE_ORDER == OR_LITTLE_ENDIAN */
174 
175 #endif /* _BYTE_ORDER_H_ */
UINT64 htond(double)
struct moving_van::@158 bits
UINT64 ntohi64(UINT64)
float ntohf(UINT32 from)
unsigned int buf[2]
Definition: byte_order.h:83
double dbl
Definition: byte_order.h:85
unsigned int ntohl(unsigned int)
unsigned short htons(unsigned short)
UINT32 htonf(float)
double ntohd(UINT64 from)
unsigned short ntohs(unsigned short)
unsigned int htonl(unsigned int)
UINT64 htoni64(UINT64)