add DBusTypeMark
[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 #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 #define DBUS_MAXIMUM_ARRAY_LENGTH (67108864)
108 #define DBUS_MAXIMUM_ARRAY_LENGTH_BITS 26
109 #define DBUS_MAXIMUM_MESSAGE_LENGTH (DBUS_MAXIMUM_ARRAY_LENGTH * 2)
110 #define DBUS_MAXIMUM_MESSAGE_LENGTH_BITS 27
111
112 static const char *
113 _hack_dbus_type_to_string (int type)
114 {
115   switch (type)
116     {
117     case DBUS_TYPE_INVALID:
118       return "invalid";
119     case DBUS_TYPE_BOOLEAN:
120       return "boolean";
121     case DBUS_TYPE_INT32:
122       return "int32";
123     case DBUS_TYPE_UINT32:
124       return "uint32";
125     case DBUS_TYPE_DOUBLE:
126       return "double";
127     case DBUS_TYPE_STRING:
128       return "string";
129     case DBUS_TYPE_OBJECT_PATH:
130       return "object_path";
131     case DBUS_TYPE_SIGNATURE:
132       return "signature";
133     case DBUS_TYPE_STRUCT:
134       return "struct";
135     case DBUS_TYPE_ARRAY:
136       return "array";
137     case DBUS_TYPE_VARIANT:
138       return "variant";
139     case DBUS_STRUCT_BEGIN_CHAR:
140       return "begin_struct";
141     case DBUS_STRUCT_END_CHAR:
142       return "end_struct";
143     default:
144       return "unknown";
145     }
146 }
147
148 #define _dbus_type_to_string(t) _hack_dbus_type_to_string(t)
149
150 /****************************************************** Remove later */
151
152 #ifdef WORDS_BIGENDIAN
153 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
154 #else
155 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
156 #endif
157
158 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint32_t) (      \
159     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
160     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
161     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
162     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
163
164 #ifdef DBUS_HAVE_INT64
165
166 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint64_t) (              \
167       (((dbus_uint64_t) (val) &                                                 \
168         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
169       (((dbus_uint64_t) (val) &                                                 \
170         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
171       (((dbus_uint64_t) (val) &                                                 \
172         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
173       (((dbus_uint64_t) (val) &                                                 \
174         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
175       (((dbus_uint64_t) (val) &                                                 \
176         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
177       (((dbus_uint64_t) (val) &                                                 \
178         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
179       (((dbus_uint64_t) (val) &                                                 \
180         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
181       (((dbus_uint64_t) (val) &                                                 \
182         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
183 #endif /* DBUS_HAVE_INT64 */
184
185 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
186 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
187
188 #ifdef DBUS_HAVE_INT64
189 #define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
190 #define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
191 #endif /* DBUS_HAVE_INT64 */
192
193 #ifdef WORDS_BIGENDIAN
194 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
195 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
196 #define DBUS_INT32_TO_LE(val)   (DBUS_INT32_SWAP_LE_BE (val))
197 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
198 #  ifdef DBUS_HAVE_INT64
199 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) (val))
200 #define DBUS_UINT64_TO_BE(val)  ((dbus_uint64_t) (val))
201 #define DBUS_INT64_TO_LE(val)   (DBUS_INT64_SWAP_LE_BE (val))
202 #define DBUS_UINT64_TO_LE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
203 #  endif /* DBUS_HAVE_INT64 */
204 #else
205 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
206 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
207 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
208 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
209 #  ifdef DBUS_HAVE_INT64
210 #define DBUS_INT64_TO_LE(val)   ((dbus_int64_t) (val))
211 #define DBUS_UINT64_TO_LE(val)  ((dbus_uint64_t) (val))
212 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
213 #define DBUS_UINT64_TO_BE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
214 #  endif /* DBUS_HAVE_INT64 */
215 #endif
216
217 /* The transformation is symmetric, so the FROM just maps to the TO. */
218 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
219 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
220 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
221 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
222 #ifdef DBUS_HAVE_INT64
223 #define DBUS_INT64_FROM_LE(val)  (DBUS_INT64_TO_LE (val))
224 #define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
225 #define DBUS_INT64_FROM_BE(val)  (DBUS_INT64_TO_BE (val))
226 #define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
227 #endif /* DBUS_HAVE_INT64 */
228
229 void          _dbus_pack_int32    (dbus_int32_t         value,
230                                    int                  byte_order,
231                                    unsigned char       *data);
232 dbus_int32_t  _dbus_unpack_int32  (int                  byte_order,
233                                    const unsigned char *data);
234 void          _dbus_pack_uint32   (dbus_uint32_t        value,
235                                    int                  byte_order,
236                                    unsigned char       *data);
237 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
238                                    const unsigned char *data);
239
240 #ifdef DBUS_HAVE_INT64
241 void          _dbus_pack_int64    (dbus_int64_t         value,
242                                    int                  byte_order,
243                                    unsigned char       *data);
244 dbus_int64_t  _dbus_unpack_int64  (int                  byte_order,
245                                    const unsigned char *data);
246 void          _dbus_pack_uint64   (dbus_uint64_t        value,
247                                    int                  byte_order,
248                                    unsigned char       *data);
249 dbus_uint64_t _dbus_unpack_uint64 (int                  byte_order,
250                                    const unsigned char *data);
251 #endif /* DBUS_HAVE_INT64 */
252
253 void        _dbus_marshal_set_int32  (DBusString       *str,
254                                       int               byte_order,
255                                       int               offset,
256                                       dbus_int32_t      value);
257 void        _dbus_marshal_set_uint32 (DBusString       *str,
258                                       int               byte_order,
259                                       int               offset,
260                                       dbus_uint32_t     value);
261 #ifdef DBUS_HAVE_INT64
262 void        _dbus_marshal_set_int64  (DBusString       *str,
263                                       int               byte_order,
264                                       int               offset,
265                                       dbus_int64_t      value);
266 void        _dbus_marshal_set_uint64 (DBusString       *str,
267                                       int               byte_order,
268                                       int               offset,
269                                       dbus_uint64_t     value);
270 #endif /* DBUS_HAVE_INT64 */
271
272 dbus_bool_t   _dbus_marshal_set_string       (DBusString       *str,
273                                               int               byte_order,
274                                               int               offset,
275                                               const DBusString *value,
276                                               int               len);
277 dbus_bool_t   _dbus_marshal_basic_type       (DBusString       *str,
278                                               int               insert_at,
279                                               char              type,
280                                               const void       *value,
281                                               int               byte_order,
282                                               int              *pos_after);
283 dbus_bool_t   _dbus_marshal_basic_type_array (DBusString       *str,
284                                               int               insert_at,
285                                               char              element_type,
286                                               const void       *value,
287                                               int               len,
288                                               int               byte_order,
289                                               int              *pos_after);
290 dbus_uint32_t _dbus_demarshal_uint32         (const DBusString *str,
291                                               int               byte_order,
292                                               int               pos,
293                                               int              *new_pos);
294 void          _dbus_demarshal_basic_type     (const DBusString *str,
295                                               int               type,
296                                               void             *value,
297                                               int               byte_order,
298                                               int               pos,
299                                               int              *new_pos);
300 void          _dbus_marshal_skip_basic_type  (const DBusString *str,
301                                               int               type,
302                                               int               byte_order,
303                                               int              *pos);
304 void          _dbus_marshal_skip_array       (const DBusString *str,
305                                               int               byte_order,
306                                               int               element_type,
307                                               int              *pos);
308 dbus_bool_t   _dbus_type_is_valid            (int               typecode);
309 int           _dbus_type_get_alignment       (int               typecode);
310
311 #endif /* DBUS_MARSHAL_H */