fgms 0.11.8
The
FlightGear MultiPlayer Server
project
tiny_xdr.hxx
Go to the documentation of this file.
1 /**
2  * @file tiny_xdr.hxx
3  * @author Oliver Schroeder
4  * @brief Tiny XDR implementation for flightgear
5  *
6  * - This implementation is not complete, but implements everything we need.
7  * - For further reading on XDR read RFC 1832 - http://www.ietf.org/rfc/rfc1832.txt
8  */
9 
10 //////////////////////////////////////////////////////////////////////
11 //
12 // Tiny XDR implementation for flightgear
13 // written by Oliver Schroeder
14 // released to the puiblic domain
15 //
16 //
17 //
18 //
19 //
20 //
21 //////////////////////////////////////////////////////////////////////
22 
23 #ifndef TINY_XDR_HEADER
24 #define TINY_XDR_HEADER
25 
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 #include <simgear/misc/stdint.hxx>
31 
32 #define SWAP16(arg) sgIsLittleEndian() ? sg_bswap_16(arg) : arg
33 #define SWAP32(arg) sgIsLittleEndian() ? sg_bswap_32(arg) : arg
34 #define SWAP64(arg) sgIsLittleEndian() ? sg_bswap_64(arg) : arg
35 #define XDR_BYTES_PER_UNIT 4
36 
37 /** @brief 4 Bytes */
38 typedef uint32_t xdr_data_t; /* 4 Bytes */
39 
40 /** @brief 8 Bytes */
41 typedef uint64_t xdr_data2_t; /* 8 Bytes */
42 
43 #ifdef FG_NDEBUG
44 # undef FG_TMPDEBUG
45 # define FG_NDEBUG
46 #endif
47 #define FG_NDEBUG
48 
49 /**
50  * @brief xdr encode 8, 16 and 32 Bit values
51  */
52 template<typename TYPE>
53 xdr_data_t XDR_encode ( TYPE Val )
54 {
55  union
56  {
57  xdr_data_t encoded;
58  TYPE raw;
59  } tmp;
60 
61  tmp.raw = Val;
62  tmp.encoded = SWAP32(tmp.encoded);
63  return (tmp.encoded);
64 }
65 
66 /**
67  * @brief xdr decode 8, 16 and 32 Bit values
68  */
69 template<typename TYPE>
70 TYPE XDR_decode ( xdr_data_t Val )
71 {
72  union
73  {
74  xdr_data_t encoded;
75  TYPE raw;
76  } tmp;
77 
78  tmp.encoded = SWAP32(Val);
79  return (tmp.raw);
80 }
81 
82 /**
83  * @brief xdr encode 64 Bit values
84  */
85 template<typename TYPE>
87 {
88  union
89  {
90  xdr_data2_t encoded;
91  TYPE raw;
92  } tmp;
93 
94  tmp.raw = Val;
95  tmp.encoded = SWAP64(tmp.encoded);
96  return (tmp.encoded);
97 }
98 
99 /**
100  * @brief xdr decode 64 Bit values
101  */
102 template<typename TYPE>
104 {
105  union
106  {
107  xdr_data2_t encoded;
108  TYPE raw;
109  } tmp;
110 
111  tmp.encoded = SWAP64 (Val);
112  return (tmp.raw);
113 }
114 
115 
116 //////////////////////////////////////////////////////////////////////
117 //
118 // encode to network byte order
119 //
120 /////////////////////////////////////////////////////////////////////
121 
122 /**
123  * @brief encode 8-Bit values to network byte order
124  * (actually encodes nothing, just to satisfy the templates)
125  */
126 template<typename TYPE>
127 uint8_t
128 NET_encode8 ( TYPE Val )
129 {
130  union
131  {
132  uint8_t netbyte;
133  TYPE raw;
134  } tmp;
135 
136  tmp.raw = Val;
137  return (tmp.netbyte);
138 }
139 
140 /**
141  * @brief Decode 8-Bit values from network byte order
142  * (actually decodes nothing, just to satisfy the templates)
143  */
144 template<typename TYPE>
145 TYPE
146 NET_decode8 ( uint8_t Val )
147 {
148  union
149  {
150  uint8_t netbyte;
151  TYPE raw;
152  } tmp;
153 
154  tmp.netbyte = Val;
155  return (tmp.raw);
156 }
157 
158 /**
159  * @brief Encode 16-Bit values to network byte order
160  */
161 template<typename TYPE>
162 uint16_t
163 NET_encode16 ( TYPE Val )
164 {
165  union
166  {
167  uint16_t netbyte;
168  TYPE raw;
169  } tmp;
170 
171  tmp.raw = Val;
172  tmp.netbyte = SWAP16(tmp.netbyte);
173  return (tmp.netbyte);
174 }
175 
176 /**
177  * @brief Decode 16-Bit values from network byte order
178  */
179 template<typename TYPE>
180 TYPE
181 NET_decode16 ( uint16_t Val )
182 {
183  union
184  {
185  uint16_t netbyte;
186  TYPE raw;
187  } tmp;
188 
189  tmp.netbyte = SWAP16(Val);
190  return (tmp.raw);
191 }
192 
193 /**
194  * @brief Encode 32-Bit values to network byte order
195  */
196 template<typename TYPE>
197 uint32_t
198 NET_encode32 ( TYPE Val )
199 {
200  union
201  {
202  uint32_t netbyte;
203  TYPE raw;
204  } tmp;
205 
206  tmp.raw = Val;
207  tmp.netbyte = SWAP32(tmp.netbyte);
208  return (tmp.netbyte);
209 }
210 
211 /**
212  * @brief Decode 32-Bit values from network byte order
213  */
214 template<typename TYPE>
215 TYPE
216 NET_decode32 ( uint32_t Val )
217 {
218  union
219  {
220  uint32_t netbyte;
221  TYPE raw;
222  } tmp;
223 
224  tmp.netbyte = SWAP32(Val);
225  return (tmp.raw);
226 }
227 
228 /**
229  * @brief Encode 64-Bit values to network byte order
230  */
231 template<typename TYPE>
232 uint64_t
233 NET_encode64 ( TYPE Val )
234 {
235  union
236  {
237  uint64_t netbyte;
238  TYPE raw;
239  } tmp;
240 
241  tmp.raw = Val;
242  tmp.netbyte = SWAP64(tmp.netbyte);
243  return (tmp.netbyte);
244 }
245 
246 /**
247  * @brief Decode 64-Bit values from network byte order
248  */
249 template<typename TYPE>
250 TYPE
251 NET_decode64 ( uint64_t Val )
252 {
253  union
254  {
255  uint64_t netbyte;
256  TYPE raw;
257  } tmp;
258 
259  tmp.netbyte = SWAP64(Val);
260  return (tmp.raw);
261 }
262 
263 #endif
264 
265 
uint32_t xdr_data_t
4 Bytes
Definition: tiny_xdr.hxx:38
uint64_t NET_encode64(TYPE Val)
Encode 64-Bit values to network byte order.
Definition: tiny_xdr.hxx:233
#define SWAP16(arg)
Definition: tiny_xdr.hxx:32
uint64_t xdr_data2_t
8 Bytes
Definition: tiny_xdr.hxx:41
#define SWAP32(arg)
Definition: tiny_xdr.hxx:33
Stream based logging mechanism.
TYPE NET_decode16(uint16_t Val)
Decode 16-Bit values from network byte order.
Definition: tiny_xdr.hxx:181
uint32_t NET_encode32(TYPE Val)
Encode 32-Bit values to network byte order.
Definition: tiny_xdr.hxx:198
#define SWAP64(arg)
Definition: tiny_xdr.hxx:34
uint8_t NET_encode8(TYPE Val)
encode 8-Bit values to network byte order (actually encodes nothing, just to satisfy the templates) ...
Definition: tiny_xdr.hxx:128
uint16_t NET_encode16(TYPE Val)
Encode 16-Bit values to network byte order.
Definition: tiny_xdr.hxx:163
TYPE NET_decode8(uint8_t Val)
Decode 8-Bit values from network byte order (actually decodes nothing, just to satisfy the templates)...
Definition: tiny_xdr.hxx:146
xdr_data_t XDR_encode(TYPE Val)
xdr encode 8, 16 and 32 Bit values
Definition: tiny_xdr.hxx:53
TYPE XDR_decode64(xdr_data2_t Val)
xdr decode 64 Bit values
Definition: tiny_xdr.hxx:103
TYPE XDR_decode(xdr_data_t Val)
xdr decode 8, 16 and 32 Bit values
Definition: tiny_xdr.hxx:70
TYPE NET_decode64(uint64_t Val)
Decode 64-Bit values from network byte order.
Definition: tiny_xdr.hxx:251
TYPE NET_decode32(uint32_t Val)
Decode 32-Bit values from network byte order.
Definition: tiny_xdr.hxx:216
xdr_data2_t XDR_encode64(TYPE Val)
xdr encode 64 Bit values
Definition: tiny_xdr.hxx:86