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