2004-12-19 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal-basic.h
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  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_H
26 #define DBUS_MARSHAL_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 /****************************************************** Remove later */
39 #undef DBUS_TYPE_INVALID
40 #undef DBUS_TYPE_NIL
41 #undef DBUS_TYPE_CUSTOM
42 #undef DBUS_TYPE_BYTE
43 #undef DBUS_TYPE_INT32
44 #undef DBUS_TYPE_UINT32
45 #undef DBUS_TYPE_INT64
46 #undef DBUS_TYPE_UINT64
47 #undef DBUS_TYPE_DOUBLE
48 #undef DBUS_TYPE_STRING
49 #undef DBUS_TYPE_OBJECT_PATH
50 #undef DBUS_TYPE_ARRAY
51 #undef DBUS_TYPE_DICT
52 #undef DBUS_TYPE_VARIANT
53 #undef DBUS_TYPE_STRUCT
54 #undef DBUS_NUMBER_OF_TYPES
55
56 /* Never a legitimate type */
57 #define DBUS_TYPE_INVALID       ((int) '\0')
58
59 /* Primitive types */
60 #define DBUS_TYPE_BYTE          ((int) 'y')
61 #define DBUS_TYPE_BOOLEAN       ((int) 'b')
62 #define DBUS_TYPE_INT32         ((int) 'i')
63
64 #define DBUS_TYPE_UINT32        ((int) 'u')
65 #define DBUS_TYPE_INT64         ((int) 'x')
66 #define DBUS_TYPE_UINT64        ((int) 't')
67
68 #define DBUS_TYPE_DOUBLE        ((int) 'd')
69 #define DBUS_TYPE_STRING        ((int) 's')
70 #define DBUS_TYPE_OBJECT_PATH   ((int) 'o')
71
72 /* Compound types */
73 #define DBUS_TYPE_ARRAY         ((int) 'a')
74 #define DBUS_TYPE_DICT          ((int) 'm')
75 #define DBUS_TYPE_VARIANT       ((int) 'v')
76
77 /* STRUCT is sort of special since its code can't appear in a type string,
78  * instead DBUS_STRUCT_BEGIN_CHAR has to appear
79  */
80 #define DBUS_TYPE_STRUCT        ((int) 'r')
81
82 /* Does not count INVALID */
83 #define DBUS_NUMBER_OF_TYPES    (13)
84
85 /* characters other than typecodes that appear in type signatures */
86 #define DBUS_STRUCT_BEGIN_CHAR   ((int) '(')
87 #define DBUS_STRUCT_END_CHAR     ((int) ')')
88
89 static const char *
90 _hack_dbus_type_to_string (int type)
91 {
92   switch (type)
93     {
94     case DBUS_TYPE_INVALID:
95       return "invalid";
96     case DBUS_TYPE_BOOLEAN:
97       return "boolean";
98     case DBUS_TYPE_INT32:
99       return "int32";
100     case DBUS_TYPE_UINT32:
101       return "uint32";
102     case DBUS_TYPE_DOUBLE:
103       return "double";
104     case DBUS_TYPE_STRING:
105       return "string";
106     case DBUS_TYPE_STRUCT:
107       return "struct";
108     case DBUS_TYPE_ARRAY:
109       return "array";
110     case DBUS_TYPE_DICT:
111       return "dict";
112     case DBUS_TYPE_VARIANT:
113       return "variant";
114     case DBUS_STRUCT_BEGIN_CHAR:
115       return "begin_struct";
116     case DBUS_STRUCT_END_CHAR:
117       return "end_struct";
118     default:
119       return "unknown";
120     }
121 }
122
123 #define _dbus_type_to_string(t) _hack_dbus_type_to_string(t)
124
125 /****************************************************** Remove later */
126
127 #ifdef WORDS_BIGENDIAN
128 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
129 #else
130 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
131 #endif
132
133 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint32_t) (      \
134     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
135     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
136     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
137     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
138
139 #ifdef DBUS_HAVE_INT64
140
141 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint64_t) (              \
142       (((dbus_uint64_t) (val) &                                                 \
143         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
144       (((dbus_uint64_t) (val) &                                                 \
145         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
146       (((dbus_uint64_t) (val) &                                                 \
147         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
148       (((dbus_uint64_t) (val) &                                                 \
149         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
150       (((dbus_uint64_t) (val) &                                                 \
151         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
152       (((dbus_uint64_t) (val) &                                                 \
153         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
154       (((dbus_uint64_t) (val) &                                                 \
155         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
156       (((dbus_uint64_t) (val) &                                                 \
157         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
158 #endif /* DBUS_HAVE_INT64 */
159
160 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
161 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
162
163 #ifdef DBUS_HAVE_INT64
164 #define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
165 #define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
166 #endif /* DBUS_HAVE_INT64 */
167
168 #ifdef WORDS_BIGENDIAN
169 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
170 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
171 #define DBUS_INT32_TO_LE(val)   (DBUS_INT32_SWAP_LE_BE (val))
172 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
173 #  ifdef DBUS_HAVE_INT64
174 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) (val))
175 #define DBUS_UINT64_TO_BE(val)  ((dbus_uint64_t) (val))
176 #define DBUS_INT64_TO_LE(val)   (DBUS_INT64_SWAP_LE_BE (val))
177 #define DBUS_UINT64_TO_LE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
178 #  endif /* DBUS_HAVE_INT64 */
179 #else
180 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
181 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
182 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
183 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
184 #  ifdef DBUS_HAVE_INT64
185 #define DBUS_INT64_TO_LE(val)   ((dbus_int64_t) (val))
186 #define DBUS_UINT64_TO_LE(val)  ((dbus_uint64_t) (val))
187 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
188 #define DBUS_UINT64_TO_BE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
189 #  endif /* DBUS_HAVE_INT64 */
190 #endif
191
192 /* The transformation is symmetric, so the FROM just maps to the TO. */
193 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
194 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
195 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
196 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
197 #ifdef DBUS_HAVE_INT64
198 #define DBUS_INT64_FROM_LE(val)  (DBUS_INT64_TO_LE (val))
199 #define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
200 #define DBUS_INT64_FROM_BE(val)  (DBUS_INT64_TO_BE (val))
201 #define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
202 #endif /* DBUS_HAVE_INT64 */
203
204 void          _dbus_pack_int32    (dbus_int32_t         value,
205                                    int                  byte_order,
206                                    unsigned char       *data);
207 dbus_int32_t  _dbus_unpack_int32  (int                  byte_order,
208                                    const unsigned char *data);
209 void          _dbus_pack_uint32   (dbus_uint32_t        value,
210                                    int                  byte_order,
211                                    unsigned char       *data);
212 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
213                                    const unsigned char *data);
214 #ifdef DBUS_HAVE_INT64
215 void          _dbus_pack_int64    (dbus_int64_t         value,
216                                    int                  byte_order,
217                                    unsigned char       *data);
218 dbus_int64_t  _dbus_unpack_int64  (int                  byte_order,
219                                    const unsigned char *data);
220 void          _dbus_pack_uint64   (dbus_uint64_t        value,
221                                    int                  byte_order,
222                                    unsigned char       *data);
223 dbus_uint64_t _dbus_unpack_uint64 (int                  byte_order,
224                                    const unsigned char *data);
225 #endif /* DBUS_HAVE_INT64 */
226
227 void        _dbus_marshal_set_int32  (DBusString       *str,
228                                       int               byte_order,
229                                       int               offset,
230                                       dbus_int32_t      value);
231 void        _dbus_marshal_set_uint32 (DBusString       *str,
232                                       int               byte_order,
233                                       int               offset,
234                                       dbus_uint32_t     value);
235 #ifdef DBUS_HAVE_INT64
236 void        _dbus_marshal_set_int64  (DBusString       *str,
237                                       int               byte_order,
238                                       int               offset,
239                                       dbus_int64_t      value);
240 void        _dbus_marshal_set_uint64 (DBusString       *str,
241                                       int               byte_order,
242                                       int               offset,
243                                       dbus_uint64_t     value);
244 #endif /* DBUS_HAVE_INT64 */
245
246 dbus_bool_t _dbus_marshal_set_string      (DBusString         *str,
247                                            int                 byte_order,
248                                            int                 offset,
249                                            const DBusString   *value,
250                                            int                 len);
251 void        _dbus_marshal_set_object_path (DBusString         *str,
252                                            int                 byte_order,
253                                            int                 offset,
254                                            const char        **path,
255                                            int                 path_len);
256
257 dbus_bool_t   _dbus_marshal_int32          (DBusString            *str,
258                                             int                    byte_order,
259                                             dbus_int32_t           value);
260 dbus_bool_t   _dbus_marshal_uint32         (DBusString            *str,
261                                             int                    byte_order,
262                                             dbus_uint32_t          value);
263
264 #ifdef DBUS_HAVE_INT64
265 dbus_bool_t   _dbus_marshal_int64          (DBusString            *str,
266                                             int                    byte_order,
267                                             dbus_int64_t           value);
268 dbus_bool_t   _dbus_marshal_uint64         (DBusString            *str,
269                                             int                    byte_order,
270                                             dbus_uint64_t          value);
271 #endif /* DBUS_HAVE_INT64 */
272 dbus_bool_t   _dbus_marshal_double         (DBusString            *str,
273                                             int                    byte_order,
274                                             double                 value);
275
276 dbus_bool_t   _dbus_marshal_string         (DBusString            *str,
277                                             int                    byte_order,
278                                             const char            *value);
279 dbus_bool_t   _dbus_marshal_string_len     (DBusString            *str,
280                                             int                    byte_order,
281                                             const char            *value,
282                                             int                    len);
283
284 dbus_bool_t   _dbus_marshal_basic_type     (DBusString            *str,
285                                             int                    insert_at,
286                                             char                   type,
287                                             const void            *value,
288                                             int                    byte_order);
289 dbus_bool_t   _dbus_marshal_basic_type_array (DBusString            *str,
290                                               int                    insert_at,
291                                               char                   element_type,
292                                               const void            *value,
293                                               int                    len,
294                                               int                    byte_order);
295 dbus_bool_t   _dbus_marshal_byte_array     (DBusString            *str,
296                                             int                    byte_order,
297                                             const unsigned char   *value,
298                                             int                    len);
299 dbus_bool_t   _dbus_marshal_int32_array    (DBusString            *str,
300                                             int                    byte_order,
301                                             const dbus_int32_t    *value,
302                                             int                    len);
303 dbus_bool_t   _dbus_marshal_uint32_array   (DBusString            *str,
304                                             int                    byte_order,
305                                             const dbus_uint32_t   *value,
306                                             int                    len);
307 #ifdef DBUS_HAVE_INT64
308 dbus_bool_t   _dbus_marshal_int64_array    (DBusString            *str,
309                                             int                    byte_order,
310                                             const dbus_int64_t    *value,
311                                             int                    len);
312 dbus_bool_t   _dbus_marshal_uint64_array   (DBusString            *str,
313                                             int                    byte_order,
314                                             const dbus_uint64_t   *value,
315                                             int                    len);
316 #endif /* DBUS_HAVE_INT64 */
317 dbus_bool_t   _dbus_marshal_double_array   (DBusString            *str,
318                                             int                    byte_order,
319                                             const double          *value,
320                                             int                    len);
321 dbus_bool_t   _dbus_marshal_string_array   (DBusString            *str,
322                                             int                    byte_order,
323                                             const char           **value,
324                                             int                    len);
325 double        _dbus_demarshal_double       (const DBusString      *str,
326                                             int                    byte_order,
327                                             int                    pos,
328                                             int                   *new_pos);
329 dbus_int32_t  _dbus_demarshal_int32        (const DBusString      *str,
330                                             int                    byte_order,
331                                             int                    pos,
332                                             int                   *new_pos);
333 dbus_uint32_t _dbus_demarshal_uint32       (const DBusString      *str,
334                                             int                    byte_order,
335                                             int                    pos,
336                                             int                   *new_pos);
337 #ifdef DBUS_HAVE_INT64
338 dbus_int64_t  _dbus_demarshal_int64        (const DBusString      *str,
339                                             int                    byte_order,
340                                             int                    pos,
341                                             int                   *new_pos);
342 dbus_uint64_t _dbus_demarshal_uint64       (const DBusString      *str,
343                                             int                    byte_order,
344                                             int                    pos,
345                                             int                   *new_pos);
346 #endif /* DBUS_HAVE_INT64 */
347 void          _dbus_demarshal_basic_type   (const DBusString      *str,
348                                             int                    type,
349                                             void                  *value,
350                                             int                    byte_order,
351                                             int                   *pos);
352 char *        _dbus_demarshal_string       (const DBusString      *str,
353                                             int                    byte_order,
354                                             int                    pos,
355                                             int                   *new_pos);
356 dbus_bool_t   _dbus_demarshal_byte_array   (const DBusString      *str,
357                                             int                    byte_order,
358                                             int                    pos,
359                                             int                   *new_pos,
360                                             unsigned char        **array,
361                                             int                   *array_len);
362 dbus_bool_t   _dbus_demarshal_int32_array  (const DBusString      *str,
363                                             int                    byte_order,
364                                             int                    pos,
365                                             int                   *new_pos,
366                                             dbus_int32_t         **array,
367                                             int                   *array_len);
368 dbus_bool_t   _dbus_demarshal_uint32_array (const DBusString      *str,
369                                             int                    byte_order,
370                                             int                    pos,
371                                             int                   *new_pos,
372                                             dbus_uint32_t        **array,
373                                             int                   *array_len);
374 #ifdef DBUS_HAVE_INT64
375 dbus_bool_t   _dbus_demarshal_int64_array  (const DBusString      *str,
376                                             int                    byte_order,
377                                             int                    pos,
378                                             int                   *new_pos,
379                                             dbus_int64_t         **array,
380                                             int                   *array_len);
381 dbus_bool_t   _dbus_demarshal_uint64_array (const DBusString      *str,
382                                             int                    byte_order,
383                                             int                    pos,
384                                             int                   *new_pos,
385                                             dbus_uint64_t        **array,
386                                             int                   *array_len);
387 #endif /* DBUS_HAVE_INT64 */
388 dbus_bool_t   _dbus_demarshal_double_array (const DBusString      *str,
389                                             int                    byte_order,
390                                             int                    pos,
391                                             int                   *new_pos,
392                                             double               **array,
393                                             int                   *array_len);
394 dbus_bool_t   _dbus_demarshal_basic_type_array (const DBusString      *str,
395                                                 int                    type,
396                                                 void                 **array,
397                                                 int                   *array_len,
398                                                 int                    byte_order,
399                                                 int                   *pos);
400
401 dbus_bool_t   _dbus_demarshal_string_array (const DBusString      *str,
402                                             int                    byte_order,
403                                             int                    pos,
404                                             int                   *new_pos,
405                                             char                ***array,
406                                             int                   *array_len);
407 dbus_bool_t   _dbus_decompose_path         (const char*            data,
408                                             int                    len,
409                                             char                ***path,
410                                             int                   *path_len);
411 dbus_bool_t   _dbus_demarshal_object_path  (const DBusString      *str,
412                                             int                    byte_order,
413                                             int                    pos,
414                                             int                   *new_pos,
415                                             char                ***path,
416                                             int                   *path_len);
417
418 void         _dbus_marshal_skip_basic_type (const DBusString      *str,
419                                             int                    type,
420                                             int                    byte_order,
421                                             int                   *pos);
422 void         _dbus_marshal_skip_array      (const DBusString      *str,
423                                             int                    byte_order,
424                                             int                   *pos);
425
426 dbus_bool_t _dbus_marshal_get_arg_end_pos (const DBusString *str,
427                                            int               byte_order,
428                                            int               type,
429                                            int               pos,
430                                            int              *end_pos);
431 dbus_bool_t _dbus_marshal_validate_type   (const DBusString *str,
432                                            int               pos,
433                                            int              *type,
434                                            int              *end_pos);
435 dbus_bool_t _dbus_marshal_validate_arg    (const DBusString *str,
436                                            int               depth,
437                                            int               byte_order,
438                                            int               type,
439                                            int               array_type_pos,
440                                            int               pos,
441                                            int              *end_pos);
442
443 dbus_bool_t _dbus_type_is_valid           (int               typecode);
444
445 #endif /* DBUS_MARSHAL_H */