chop dbus-marshal-basic in half and move it to be insertion rather than append based
[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
108 static 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
225 void          _dbus_pack_int32    (dbus_int32_t         value,
226                                    int                  byte_order,
227                                    unsigned char       *data);
228 dbus_int32_t  _dbus_unpack_int32  (int                  byte_order,
229                                    const unsigned char *data);
230 void          _dbus_pack_uint32   (dbus_uint32_t        value,
231                                    int                  byte_order,
232                                    unsigned char       *data);
233 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
234                                    const unsigned char *data);
235
236 #ifdef DBUS_HAVE_INT64
237 void          _dbus_pack_int64    (dbus_int64_t         value,
238                                    int                  byte_order,
239                                    unsigned char       *data);
240 dbus_int64_t  _dbus_unpack_int64  (int                  byte_order,
241                                    const unsigned char *data);
242 void          _dbus_pack_uint64   (dbus_uint64_t        value,
243                                    int                  byte_order,
244                                    unsigned char       *data);
245 dbus_uint64_t _dbus_unpack_uint64 (int                  byte_order,
246                                    const unsigned char *data);
247 #endif /* DBUS_HAVE_INT64 */
248
249 void        _dbus_marshal_set_int32  (DBusString       *str,
250                                       int               byte_order,
251                                       int               offset,
252                                       dbus_int32_t      value);
253 void        _dbus_marshal_set_uint32 (DBusString       *str,
254                                       int               byte_order,
255                                       int               offset,
256                                       dbus_uint32_t     value);
257 #ifdef DBUS_HAVE_INT64
258 void        _dbus_marshal_set_int64  (DBusString       *str,
259                                       int               byte_order,
260                                       int               offset,
261                                       dbus_int64_t      value);
262 void        _dbus_marshal_set_uint64 (DBusString       *str,
263                                       int               byte_order,
264                                       int               offset,
265                                       dbus_uint64_t     value);
266 #endif /* DBUS_HAVE_INT64 */
267
268 dbus_bool_t   _dbus_marshal_set_string       (DBusString       *str,
269                                               int               byte_order,
270                                               int               offset,
271                                               const DBusString *value,
272                                               int               len);
273 dbus_bool_t   _dbus_marshal_basic_type       (DBusString       *str,
274                                               int               insert_at,
275                                               char              type,
276                                               const void       *value,
277                                               int               byte_order,
278                                               int              *pos_after);
279 dbus_bool_t   _dbus_marshal_basic_type_array (DBusString       *str,
280                                               int               insert_at,
281                                               char              element_type,
282                                               const void       *value,
283                                               int               len,
284                                               int               byte_order,
285                                               int              *pos_after);
286 dbus_uint32_t _dbus_demarshal_uint32         (const DBusString *str,
287                                               int               byte_order,
288                                               int               pos,
289                                               int              *new_pos);
290 void          _dbus_demarshal_basic_type     (const DBusString *str,
291                                               int               type,
292                                               void             *value,
293                                               int               byte_order,
294                                               int               pos,
295                                               int              *new_pos);
296 void          _dbus_marshal_skip_basic_type  (const DBusString *str,
297                                               int               type,
298                                               int               byte_order,
299                                               int              *pos);
300 void          _dbus_marshal_skip_array       (const DBusString *str,
301                                               int               byte_order,
302                                               int               element_type,
303                                               int              *pos);
304 dbus_bool_t   _dbus_type_is_valid            (int               typecode);
305 int           _dbus_type_get_alignment       (int               typecode);
306
307 #endif /* DBUS_MARSHAL_H */