2003-08-15 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.h  Marshalling routines
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #ifndef DBUS_MARSHAL_H
25 #define DBUS_MARSHAL_H
26
27 #include <config.h>
28 #include <dbus/dbus-protocol.h>
29 #include <dbus/dbus-types.h>
30 #include <dbus/dbus-arch-deps.h>
31 #include <dbus/dbus-string.h>
32 #include <dbus/dbus-objectid.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_UINT32_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint32_t) (      \
45     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
46     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
47     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
48     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
49
50 #ifdef DBUS_HAVE_INT64
51
52 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint64_t) (              \
53       (((dbus_uint64_t) (val) &                                                 \
54         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
55       (((dbus_uint64_t) (val) &                                                 \
56         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
57       (((dbus_uint64_t) (val) &                                                 \
58         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
59       (((dbus_uint64_t) (val) &                                                 \
60         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
61       (((dbus_uint64_t) (val) &                                                 \
62         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
63       (((dbus_uint64_t) (val) &                                                 \
64         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
65       (((dbus_uint64_t) (val) &                                                 \
66         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
67       (((dbus_uint64_t) (val) &                                                 \
68         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
69 #endif /* DBUS_HAVE_INT64 */
70
71 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
72 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
73
74 #ifdef DBUS_HAVE_INT64
75 #define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
76 #define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
77 #endif /* DBUS_HAVE_INT64 */
78
79 #ifdef WORDS_BIGENDIAN
80 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
81 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
82 #define DBUS_INT32_TO_LE(val)   (DBUS_INT32_SWAP_LE_BE (val))
83 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
84 #  ifdef DBUS_HAVE_INT64
85 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) (val))
86 #define DBUS_UINT64_TO_BE(val)  ((dbus_uint64_t) (val))
87 #define DBUS_INT64_TO_LE(val)   (DBUS_INT64_SWAP_LE_BE (val))
88 #define DBUS_UINT64_TO_LE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
89 #  endif /* DBUS_HAVE_INT64 */
90 #else
91 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
92 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
93 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
94 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
95 #  ifdef DBUS_HAVE_INT64
96 #define DBUS_INT64_TO_LE(val)   ((dbus_int64_t) (val))
97 #define DBUS_UINT64_TO_LE(val)  ((dbus_uint64_t) (val))
98 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
99 #define DBUS_UINT64_TO_BE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
100 #  endif /* DBUS_HAVE_INT64 */
101 #endif
102
103 /* The transformation is symmetric, so the FROM just maps to the TO. */
104 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
105 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
106 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
107 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
108 #ifdef DBUS_HAVE_INT64
109 #define DBUS_INT64_FROM_LE(val)  (DBUS_INT64_TO_LE (val))
110 #define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
111 #define DBUS_INT64_FROM_BE(val)  (DBUS_INT64_TO_BE (val))
112 #define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
113 #endif /* DBUS_HAVE_INT64 */
114
115 void          _dbus_pack_int32    (dbus_int32_t         value,
116                                    int                  byte_order,
117                                    unsigned char       *data);
118 dbus_int32_t  _dbus_unpack_int32  (int                  byte_order,
119                                    const unsigned char *data);
120 void          _dbus_pack_uint32   (dbus_uint32_t        value,
121                                    int                  byte_order,
122                                    unsigned char       *data);
123 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
124                                    const unsigned char *data);
125 #ifdef DBUS_HAVE_INT64
126 void          _dbus_pack_int64    (dbus_int64_t         value,
127                                    int                  byte_order,
128                                    unsigned char       *data);
129 dbus_int64_t  _dbus_unpack_int64  (int                  byte_order,
130                                    const unsigned char *data);
131 void          _dbus_pack_uint64   (dbus_uint64_t        value,
132                                    int                  byte_order,
133                                    unsigned char       *data);
134 dbus_uint64_t _dbus_unpack_uint64 (int                  byte_order,
135                                    const unsigned char *data);
136 #endif /* DBUS_HAVE_INT64 */
137
138 void        _dbus_marshal_set_int32  (DBusString       *str,
139                                       int               byte_order,
140                                       int               offset,
141                                       dbus_int32_t      value);
142 void        _dbus_marshal_set_uint32 (DBusString       *str,
143                                       int               byte_order,
144                                       int               offset,
145                                       dbus_uint32_t     value);
146 #ifdef DBUS_HAVE_INT64
147 void        _dbus_marshal_set_int64  (DBusString       *str,
148                                       int               byte_order,
149                                       int               offset,
150                                       dbus_int64_t      value);
151 void        _dbus_marshal_set_uint64 (DBusString       *str,
152                                       int               byte_order,
153                                       int               offset,
154                                       dbus_uint64_t     value);
155 #endif /* DBUS_HAVE_INT64 */
156
157 dbus_bool_t _dbus_marshal_set_string    (DBusString         *str,
158                                          int                 byte_order,
159                                          int                 offset,
160                                          const DBusString   *value,
161                                          int                 len);
162 void        _dbus_marshal_set_object_id (DBusString         *str,
163                                          int                 byte_order,
164                                          int                 offset,
165                                          const DBusObjectID *value);
166
167 dbus_bool_t   _dbus_marshal_int32          (DBusString            *str,
168                                             int                    byte_order,
169                                             dbus_int32_t           value);
170 dbus_bool_t   _dbus_marshal_uint32         (DBusString            *str,
171                                             int                    byte_order,
172                                             dbus_uint32_t          value);
173 #ifdef DBUS_HAVE_INT64
174 dbus_bool_t   _dbus_marshal_int64          (DBusString            *str,
175                                             int                    byte_order,
176                                             dbus_int64_t           value);
177 dbus_bool_t   _dbus_marshal_uint64         (DBusString            *str,
178                                             int                    byte_order,
179                                             dbus_uint64_t          value);
180 #endif /* DBUS_HAVE_INT64 */
181 dbus_bool_t   _dbus_marshal_double         (DBusString            *str,
182                                             int                    byte_order,
183                                             double                 value);
184 dbus_bool_t   _dbus_marshal_string         (DBusString            *str,
185                                             int                    byte_order,
186                                             const char            *value);
187 dbus_bool_t   _dbus_marshal_byte_array     (DBusString            *str,
188                                             int                    byte_order,
189                                             const unsigned char   *value,
190                                             int                    len);
191 dbus_bool_t   _dbus_marshal_int32_array    (DBusString            *str,
192                                             int                    byte_order,
193                                             const dbus_int32_t    *value,
194                                             int                    len);
195 dbus_bool_t   _dbus_marshal_uint32_array   (DBusString            *str,
196                                             int                    byte_order,
197                                             const dbus_uint32_t   *value,
198                                             int                    len);
199 #ifdef DBUS_HAVE_INT64
200 dbus_bool_t   _dbus_marshal_int64_array    (DBusString            *str,
201                                             int                    byte_order,
202                                             const dbus_int64_t    *value,
203                                             int                    len);
204 dbus_bool_t   _dbus_marshal_uint64_array   (DBusString            *str,
205                                             int                    byte_order,
206                                             const dbus_uint64_t   *value,
207                                             int                    len);
208 #endif /* DBUS_HAVE_INT64 */
209 dbus_bool_t   _dbus_marshal_double_array   (DBusString            *str,
210                                             int                    byte_order,
211                                             const double          *value,
212                                             int                    len);
213 dbus_bool_t   _dbus_marshal_string_array   (DBusString            *str,
214                                             int                    byte_order,
215                                             const char           **value,
216                                             int                    len);
217 dbus_bool_t   _dbus_marshal_object_id      (DBusString            *str,
218                                             int                    byte_order,
219                                             const DBusObjectID    *value);
220
221 double        _dbus_demarshal_double       (const DBusString      *str,
222                                             int                    byte_order,
223                                             int                    pos,
224                                             int                   *new_pos);
225 dbus_int32_t  _dbus_demarshal_int32        (const DBusString      *str,
226                                             int                    byte_order,
227                                             int                    pos,
228                                             int                   *new_pos);
229 dbus_uint32_t _dbus_demarshal_uint32       (const DBusString      *str,
230                                             int                    byte_order,
231                                             int                    pos,
232                                             int                   *new_pos);
233 #ifdef DBUS_HAVE_INT64
234 dbus_int64_t  _dbus_demarshal_int64        (const DBusString      *str,
235                                             int                    byte_order,
236                                             int                    pos,
237                                             int                   *new_pos);
238 dbus_uint64_t _dbus_demarshal_uint64       (const DBusString      *str,
239                                             int                    byte_order,
240                                             int                    pos,
241                                             int                   *new_pos);
242 #endif /* DBUS_HAVE_INT64 */
243 char *        _dbus_demarshal_string       (const DBusString      *str,
244                                             int                    byte_order,
245                                             int                    pos,
246                                             int                   *new_pos);
247 dbus_bool_t   _dbus_demarshal_byte_array   (const DBusString      *str,
248                                             int                    byte_order,
249                                             int                    pos,
250                                             int                   *new_pos,
251                                             unsigned char        **array,
252                                             int                   *array_len);
253 dbus_bool_t   _dbus_demarshal_int32_array  (const DBusString      *str,
254                                             int                    byte_order,
255                                             int                    pos,
256                                             int                   *new_pos,
257                                             dbus_int32_t         **array,
258                                             int                   *array_len);
259 dbus_bool_t   _dbus_demarshal_uint32_array (const DBusString      *str,
260                                             int                    byte_order,
261                                             int                    pos,
262                                             int                   *new_pos,
263                                             dbus_uint32_t        **array,
264                                             int                   *array_len);
265 #ifdef DBUS_HAVE_INT64
266 dbus_bool_t   _dbus_demarshal_int64_array  (const DBusString      *str,
267                                             int                    byte_order,
268                                             int                    pos,
269                                             int                   *new_pos,
270                                             dbus_int64_t         **array,
271                                             int                   *array_len);
272 dbus_bool_t   _dbus_demarshal_uint64_array (const DBusString      *str,
273                                             int                    byte_order,
274                                             int                    pos,
275                                             int                   *new_pos,
276                                             dbus_uint64_t        **array,
277                                             int                   *array_len);
278 #endif /* DBUS_HAVE_INT64 */
279 dbus_bool_t   _dbus_demarshal_double_array (const DBusString      *str,
280                                             int                    byte_order,
281                                             int                    pos,
282                                             int                   *new_pos,
283                                             double               **array,
284                                             int                   *array_len);
285 dbus_bool_t   _dbus_demarshal_string_array (const DBusString      *str,
286                                             int                    byte_order,
287                                             int                    pos,
288                                             int                   *new_pos,
289                                             char                ***array,
290                                             int                   *array_len);
291 void          _dbus_demarshal_object_id    (const DBusString      *str,
292                                             int                    byte_order,
293                                             int                    pos,
294                                             int                   *new_pos,
295                                             DBusObjectID          *value);
296
297 dbus_bool_t _dbus_marshal_get_arg_end_pos (const DBusString *str,
298                                            int               byte_order,
299                                            int               type,
300                                            int               pos,
301                                            int              *end_pos);
302 dbus_bool_t _dbus_marshal_validate_type   (const DBusString *str,
303                                            int               pos,
304                                            int              *type,
305                                            int              *end_pos);
306 dbus_bool_t _dbus_marshal_validate_arg    (const DBusString *str,
307                                            int               depth,
308                                            int               byte_order,
309                                            int               type,
310                                            int               array_type_pos,
311                                            int               pos,
312                                            int              *end_pos);
313
314
315 #endif /* DBUS_PROTOCOL_H */