1 //////////////////////////////////////////////////////////////////////
3 // Tiny XDR implementation for flightgear
4 // written by Oliver Schroeder
5 // released to the puiblic domain
7 // This implementation is not complete, but implements
10 // For further reading on XDR read RFC 1832.
14 //////////////////////////////////////////////////////////////////////
16 #ifndef TINY_XDR_HEADER
17 #define TINY_XDR_HEADER
19 #if defined HAVE_CONFIG_H
23 #include <simgear/compiler.h>
24 #if defined HAVE_STDINT_H
30 //////////////////////////////////////////////////////////////////////
32 // There are many sick systems out there:
34 // check for sizeof(float) and sizeof(double)
35 // if sizeof(float) != 4 this code must be patched
36 // if sizeof(double) != 8 this code must be patched
38 // Those are comments I fetched out of glibc source:
39 // - s390 is big-endian
40 // - Sparc is big-endian, but v9 supports endian conversion
41 // on loads/stores and GCC supports such a mode. Be prepared.
42 // - The MIPS architecture has selectable endianness.
43 // - x86_64 is little-endian.
44 // - CRIS is little-endian.
45 // - m68k is big-endian.
46 // - Alpha is little-endian.
47 // - PowerPC can be little or big endian.
48 // - SH is bi-endian but with a big-endian FPU.
49 // - hppa1.1 big-endian.
50 // - ARM is (usually) little-endian but with a big-endian FPU.
52 //////////////////////////////////////////////////////////////////////
53 inline uint32_t bswap_32(unsigned int b) {
59 inline uint64_t bswap_64(unsigned long long b) {
61 x = ((x >> 32) & 0x00000000FFFFFFFFLL) | ((x << 32) & 0xFFFFFFFF00000000LL);
62 x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
63 x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL);
67 #if BYTE_ORDER == BIG_ENDIAN
68 # define SWAP32(arg) arg
69 # define SWAP64(arg) arg
73 # define SWAP32(arg) bswap_32(arg)
74 # define SWAP64(arg) bswap_64(arg)
79 #define XDR_BYTES_PER_UNIT 4
81 typedef uint32_t xdr_data_t; /* 4 Bytes */
82 typedef uint64_t xdr_data2_t; /* 8 Bytes */
84 /* XDR 8bit integers */
85 xdr_data_t XDR_encode_int8 ( int8_t n_Val );
86 xdr_data_t XDR_encode_uint8 ( uint8_t n_Val );
87 int8_t XDR_decode_int8 ( xdr_data_t n_Val );
88 uint8_t XDR_decode_uint8 ( xdr_data_t n_Val );
90 /* XDR 16bit integers */
91 xdr_data_t XDR_encode_int16 ( int16_t n_Val );
92 xdr_data_t XDR_encode_uint16 ( uint16_t n_Val );
93 int16_t XDR_decode_int16 ( xdr_data_t n_Val );
94 uint16_t XDR_decode_uint16 ( xdr_data_t n_Val );
96 /* XDR 32bit integers */
97 xdr_data_t XDR_encode_int32 ( int32_t n_Val );
98 xdr_data_t XDR_encode_uint32 ( const uint32_t n_Val );
99 int32_t XDR_decode_int32 ( xdr_data_t n_Val );
100 uint32_t XDR_decode_uint32 ( const xdr_data_t n_Val );
102 /* XDR 64bit integers */
103 xdr_data2_t XDR_encode_int64 ( int64_t n_Val );
104 xdr_data2_t XDR_encode_uint64 ( uint64_t n_Val );
105 int64_t XDR_decode_int64 ( xdr_data2_t n_Val );
106 uint64_t XDR_decode_uint64 ( xdr_data2_t n_Val );
108 //////////////////////////////////////////////////
110 // FIXME: #1 these funtions must be fixed for
111 // none IEEE-encoding architecturs
112 // (eg. vax, big suns etc)
113 // FIXME: #2 some compilers return 'double'
114 // regardless of return-type 'float'
115 // this must be fixed, too
116 // FIXME: #3 some machines may need to use a
117 // different endianess for floats!
119 //////////////////////////////////////////////////
121 xdr_data_t XDR_encode_float ( float f_Val );
122 float XDR_decode_float ( xdr_data_t f_Val );
125 xdr_data2_t XDR_encode_double ( double d_Val );
126 double XDR_decode_double ( xdr_data2_t d_Val );