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