• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-basic.h  Marshalling routines for basic (primitive) types
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  * Copyright (C) 2004, 2005  Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 
25 #ifndef DBUS_MARSHAL_BASIC_H
26 #define DBUS_MARSHAL_BASIC_H
27 
28 #include <config.h>
29 #include <dbus/dbus-protocol.h>
30 #include <dbus/dbus-types.h>
31 #include <dbus/dbus-arch-deps.h>
32 #include <dbus/dbus-string.h>
33 
34 #ifndef PACKAGE
35 #error "config.h not included here"
36 #endif
37 
38 #ifdef WORDS_BIGENDIAN
39 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
40 #else
41 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
42 #endif
43 
44 #define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val)	((dbus_uint16_t) (      \
45     (dbus_uint16_t) ((dbus_uint16_t) (val) >> 8) |                      \
46     (dbus_uint16_t) ((dbus_uint16_t) (val) << 8)))
47 
48 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)	((dbus_uint32_t) (      \
49     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
50     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
51     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
52     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
53 
54 #ifdef DBUS_HAVE_INT64
55 
56 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)	((dbus_uint64_t) (              \
57       (((dbus_uint64_t) (val) &                                                 \
58 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
59       (((dbus_uint64_t) (val) &                                                 \
60 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
61       (((dbus_uint64_t) (val) &                                                 \
62 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
63       (((dbus_uint64_t) (val) &                                                 \
64 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
65       (((dbus_uint64_t) (val) &                                                 \
66 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
67       (((dbus_uint64_t) (val) &                                                 \
68 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
69       (((dbus_uint64_t) (val) &                                                 \
70 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
71       (((dbus_uint64_t) (val) &                                                 \
72 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
73 #endif /* DBUS_HAVE_INT64 */
74 
75 #define DBUS_UINT16_SWAP_LE_BE(val) (DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
76 #define DBUS_INT16_SWAP_LE_BE(val)  ((dbus_int16_t)DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
77 
78 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
79 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
80 
81 #ifdef DBUS_HAVE_INT64
82 #  define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
83 #  define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
84 #endif /* DBUS_HAVE_INT64 */
85 
86 #ifdef WORDS_BIGENDIAN
87 
88 #  define DBUS_INT16_TO_BE(val)	((dbus_int16_t) (val))
89 #  define DBUS_UINT16_TO_BE(val)	((dbus_uint16_t) (val))
90 #  define DBUS_INT16_TO_LE(val)	(DBUS_INT16_SWAP_LE_BE (val))
91 #  define DBUS_UINT16_TO_LE(val)	(DBUS_UINT16_SWAP_LE_BE (val))
92 #  define DBUS_INT32_TO_BE(val)	((dbus_int32_t) (val))
93 #  define DBUS_UINT32_TO_BE(val)	((dbus_uint32_t) (val))
94 #  define DBUS_INT32_TO_LE(val)	(DBUS_INT32_SWAP_LE_BE (val))
95 #  define DBUS_UINT32_TO_LE(val)	(DBUS_UINT32_SWAP_LE_BE (val))
96 #  ifdef DBUS_HAVE_INT64
97 #    define DBUS_INT64_TO_BE(val)	((dbus_int64_t) (val))
98 #    define DBUS_UINT64_TO_BE(val)	((dbus_uint64_t) (val))
99 #    define DBUS_INT64_TO_LE(val)	(DBUS_INT64_SWAP_LE_BE (val))
100 #    define DBUS_UINT64_TO_LE(val)	(DBUS_UINT64_SWAP_LE_BE (val))
101 #  endif /* DBUS_HAVE_INT64 */
102 
103 #else /* WORDS_BIGENDIAN */
104 
105 #  define DBUS_INT16_TO_LE(val)	((dbus_int16_t) (val))
106 #  define DBUS_UINT16_TO_LE(val)	((dbus_uint16_t) (val))
107 #  define DBUS_INT16_TO_BE(val)	((dbus_int16_t) DBUS_UINT16_SWAP_LE_BE (val))
108 #  define DBUS_UINT16_TO_BE(val)	(DBUS_UINT16_SWAP_LE_BE (val))
109 #  define DBUS_INT32_TO_LE(val)	((dbus_int32_t) (val))
110 #  define DBUS_UINT32_TO_LE(val)	((dbus_uint32_t) (val))
111 #  define DBUS_INT32_TO_BE(val)	((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
112 #  define DBUS_UINT32_TO_BE(val)	(DBUS_UINT32_SWAP_LE_BE (val))
113 #  ifdef DBUS_HAVE_INT64
114 #    define DBUS_INT64_TO_LE(val)	((dbus_int64_t) (val))
115 #    define DBUS_UINT64_TO_LE(val)	((dbus_uint64_t) (val))
116 #    define DBUS_INT64_TO_BE(val)	((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
117 #    define DBUS_UINT64_TO_BE(val)	(DBUS_UINT64_SWAP_LE_BE (val))
118 #  endif /* DBUS_HAVE_INT64 */
119 #endif
120 
121 /* The transformation is symmetric, so the FROM just maps to the TO. */
122 #define DBUS_INT16_FROM_LE(val)	 (DBUS_INT16_TO_LE (val))
123 #define DBUS_UINT16_FROM_LE(val) (DBUS_UINT16_TO_LE (val))
124 #define DBUS_INT16_FROM_BE(val)	 (DBUS_INT16_TO_BE (val))
125 #define DBUS_UINT16_FROM_BE(val) (DBUS_UINT16_TO_BE (val))
126 #define DBUS_INT32_FROM_LE(val)	 (DBUS_INT32_TO_LE (val))
127 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
128 #define DBUS_INT32_FROM_BE(val)	 (DBUS_INT32_TO_BE (val))
129 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
130 #ifdef DBUS_HAVE_INT64
131 #  define DBUS_INT64_FROM_LE(val)	 (DBUS_INT64_TO_LE (val))
132 #  define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
133 #  define DBUS_INT64_FROM_BE(val)	 (DBUS_INT64_TO_BE (val))
134 #  define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
135 #endif /* DBUS_HAVE_INT64 */
136 
137 #ifndef DBUS_HAVE_INT64
138 /**
139  * An 8-byte struct you could use to access int64 without having
140  * int64 support
141  */
142 typedef struct
143 {
144   dbus_uint32_t first32;  /**< first 32 bits in the 8 bytes (beware endian issues) */
145   dbus_uint32_t second32; /**< second 32 bits in the 8 bytes (beware endian issues) */
146 } DBus8ByteStruct;
147 #endif /* DBUS_HAVE_INT64 */
148 
149 /**
150  * A simple 8-byte value union that lets you access 8 bytes as if they
151  * were various types; useful when dealing with basic types via
152  * void pointers and varargs.
153  */
154 typedef union
155 {
156   dbus_int16_t  i16;   /**< as int16 */
157   dbus_uint16_t u16;   /**< as int16 */
158   dbus_int32_t  i32;   /**< as int32 */
159   dbus_uint32_t u32;   /**< as int32 */
160 #ifdef DBUS_HAVE_INT64
161   dbus_int64_t  i64;   /**< as int64 */
162   dbus_uint64_t u64;   /**< as int64 */
163 #else
164   DBus8ByteStruct u64; /**< as 8-byte-struct */
165 #endif
166   double dbl;          /**< as double */
167   unsigned char byt;   /**< as byte */
168   char *str;           /**< as char* */
169 } DBusBasicValue;
170 
171 #ifdef DBUS_DISABLE_ASSERT
172 #define _dbus_unpack_uint16(byte_order, data)           \
173    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
174      DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)(data)) :    \
175      DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)(data)))
176 
177 #define _dbus_unpack_uint32(byte_order, data)           \
178    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
179      DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) :    \
180      DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data)))
181 #endif
182 
183 #ifndef _dbus_unpack_uint16
184 dbus_uint16_t _dbus_unpack_uint16 (int                  byte_order,
185                                    const unsigned char *data);
186 #endif
187 
188 void          _dbus_pack_uint32   (dbus_uint32_t        value,
189                                    int                  byte_order,
190                                    unsigned char       *data);
191 #ifndef _dbus_unpack_uint32
192 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
193                                    const unsigned char *data);
194 #endif
195 
196 dbus_bool_t   _dbus_marshal_set_basic         (DBusString       *str,
197                                                int               pos,
198                                                int               type,
199                                                const void       *value,
200                                                int               byte_order,
201                                                int              *old_end_pos,
202                                                int              *new_end_pos);
203 dbus_bool_t   _dbus_marshal_write_basic       (DBusString       *str,
204                                                int               insert_at,
205                                                int               type,
206                                                const void       *value,
207                                                int               byte_order,
208                                                int              *pos_after);
209 dbus_bool_t   _dbus_marshal_write_fixed_multi (DBusString       *str,
210                                                int               insert_at,
211                                                int               element_type,
212                                                const void       *value,
213                                                int               n_elements,
214                                                int               byte_order,
215                                                int              *pos_after);
216 void          _dbus_marshal_read_basic        (const DBusString *str,
217                                                int               pos,
218                                                int               type,
219                                                void             *value,
220                                                int               byte_order,
221                                                int              *new_pos);
222 void          _dbus_marshal_read_fixed_multi  (const DBusString *str,
223                                                int               pos,
224                                                int               element_type,
225                                                void             *value,
226                                                int               n_elements,
227                                                int               byte_order,
228                                                int              *new_pos);
229 void          _dbus_marshal_skip_basic        (const DBusString *str,
230                                                int               type,
231                                                int               byte_order,
232                                                int              *pos);
233 void          _dbus_marshal_skip_array        (const DBusString *str,
234                                                int               element_type,
235                                                int               byte_order,
236                                                int              *pos);
237 void          _dbus_marshal_set_uint32        (DBusString       *str,
238                                                int               pos,
239                                                dbus_uint32_t     value,
240                                                int               byte_order);
241 dbus_uint32_t _dbus_marshal_read_uint32       (const DBusString *str,
242                                                int               pos,
243                                                int               byte_order,
244                                                int              *new_pos);
245 dbus_bool_t   _dbus_type_is_valid             (int               typecode);
246 int           _dbus_type_get_alignment        (int               typecode);
247 dbus_bool_t   _dbus_type_is_fixed             (int               typecode);
248 int           _dbus_type_get_alignment        (int               typecode);
249 const char*   _dbus_type_to_string            (int               typecode);
250 
251 int           _dbus_first_type_in_signature   (const DBusString *str,
252                                                int               pos);
253 
254 int           _dbus_first_type_in_signature_c_str   (const char       *str,
255 						     int               pos);
256 
257 void _dbus_swap_array (unsigned char *data,
258                        int            n_elements,
259                        int            alignment);
260 
261 #endif /* DBUS_MARSHAL_BASIC_H */
262