2003-04-30 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.h  Marshalling routines
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #ifndef DBUS_MARSHAL_H
25 #define DBUS_MARSHAL_H
26
27 #include <config.h>
28 #include <dbus/dbus-protocol.h>
29 #include <dbus/dbus-types.h>
30 #include <dbus/dbus-arch-deps.h>
31 #include <dbus/dbus-string.h>
32
33 #ifndef PACKAGE
34 #error "config.h not included here"
35 #endif
36
37 #ifdef WORDS_BIGENDIAN
38 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
39 #else
40 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
41 #endif
42
43 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint32_t) (      \
44     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
45     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
46     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
47     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
48
49 #ifdef DBUS_HAVE_INT64
50
51 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint64_t) (              \
52       (((dbus_uint64_t) (val) &                                                 \
53         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
54       (((dbus_uint64_t) (val) &                                                 \
55         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
56       (((dbus_uint64_t) (val) &                                                 \
57         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
58       (((dbus_uint64_t) (val) &                                                 \
59         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
60       (((dbus_uint64_t) (val) &                                                 \
61         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
62       (((dbus_uint64_t) (val) &                                                 \
63         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
64       (((dbus_uint64_t) (val) &                                                 \
65         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
66       (((dbus_uint64_t) (val) &                                                 \
67         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
68 #endif /* DBUS_HAVE_INT64 */
69
70 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
71 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
72
73 #ifdef DBUS_HAVE_INT64
74 #define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
75 #define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
76 #endif /* DBUS_HAVE_INT64 */
77
78 #ifdef WORDS_BIGENDIAN
79 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
80 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
81 #define DBUS_INT32_TO_LE(val)   (DBUS_INT32_SWAP_LE_BE (val))
82 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
83 #  ifdef DBUS_HAVE_INT64
84 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) (val))
85 #define DBUS_UINT64_TO_BE(val)  ((dbus_uint64_t) (val))
86 #define DBUS_INT64_TO_LE(val)   (DBUS_INT64_SWAP_LE_BE (val))
87 #define DBUS_UINT64_TO_LE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
88 #  endif /* DBUS_HAVE_INT64 */
89 #else
90 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
91 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
92 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
93 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
94 #  ifdef DBUS_HAVE_INT64
95 #define DBUS_INT64_TO_LE(val)   ((dbus_int64_t) (val))
96 #define DBUS_UINT64_TO_LE(val)  ((dbus_uint64_t) (val))
97 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
98 #define DBUS_UINT64_TO_BE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
99 #  endif /* DBUS_HAVE_INT64 */
100 #endif
101
102 /* The transformation is symmetric, so the FROM just maps to the TO. */
103 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
104 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
105 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
106 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
107 #ifdef DBUS_HAVE_INT64
108 #define DBUS_INT64_FROM_LE(val)  (DBUS_INT64_TO_LE (val))
109 #define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
110 #define DBUS_INT64_FROM_BE(val)  (DBUS_INT64_TO_BE (val))
111 #define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
112 #endif /* DBUS_HAVE_INT64 */
113
114 void          _dbus_pack_int32    (dbus_int32_t         value,
115                                    int                  byte_order,
116                                    unsigned char       *data);
117 dbus_int32_t  _dbus_unpack_int32  (int                  byte_order,
118                                    const unsigned char *data);
119 void          _dbus_pack_uint32   (dbus_uint32_t        value,
120                                    int                  byte_order,
121                                    unsigned char       *data);
122 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
123                                    const unsigned char *data);
124 #ifdef DBUS_HAVE_INT64
125 void          _dbus_pack_int64    (dbus_int64_t         value,
126                                    int                  byte_order,
127                                    unsigned char       *data);
128 dbus_int64_t  _dbus_unpack_int64  (int                  byte_order,
129                                    const unsigned char *data);
130 void          _dbus_pack_uint64   (dbus_uint64_t        value,
131                                    int                  byte_order,
132                                    unsigned char       *data);
133 dbus_uint64_t _dbus_unpack_uint64 (int                  byte_order,
134                                    const unsigned char *data);
135 #endif /* DBUS_HAVE_INT64 */
136
137 void        _dbus_marshal_set_int32  (DBusString       *str,
138                                       int               byte_order,
139                                       int               offset,
140                                       dbus_int32_t      value);
141 void        _dbus_marshal_set_uint32 (DBusString       *str,
142                                       int               byte_order,
143                                       int               offset,
144                                       dbus_uint32_t     value);
145 #ifdef DBUS_HAVE_INT64
146 void        _dbus_marshal_set_int64  (DBusString       *str,
147                                       int               byte_order,
148                                       int               offset,
149                                       dbus_int64_t      value);
150 void        _dbus_marshal_set_uint64 (DBusString       *str,
151                                       int               byte_order,
152                                       int               offset,
153                                       dbus_uint64_t     value);
154 #endif /* DBUS_HAVE_INT64 */
155 dbus_bool_t _dbus_marshal_set_string (DBusString       *str,
156                                       int               byte_order,
157                                       int               offset,
158                                       const DBusString *value,
159                                       int               len);
160
161 dbus_bool_t   _dbus_marshal_int32          (DBusString            *str,
162                                             int                    byte_order,
163                                             dbus_int32_t           value);
164 dbus_bool_t   _dbus_marshal_uint32         (DBusString            *str,
165                                             int                    byte_order,
166                                             dbus_uint32_t          value);
167 #ifdef DBUS_HAVE_INT64
168 dbus_bool_t   _dbus_marshal_int64          (DBusString            *str,
169                                             int                    byte_order,
170                                             dbus_int64_t           value);
171 dbus_bool_t   _dbus_marshal_uint64         (DBusString            *str,
172                                             int                    byte_order,
173                                             dbus_uint64_t          value);
174 #endif /* DBUS_HAVE_INT64 */
175 dbus_bool_t   _dbus_marshal_double         (DBusString            *str,
176                                             int                    byte_order,
177                                             double                 value);
178 dbus_bool_t   _dbus_marshal_string         (DBusString            *str,
179                                             int                    byte_order,
180                                             const char            *value);
181 dbus_bool_t   _dbus_marshal_byte_array     (DBusString            *str,
182                                             int                    byte_order,
183                                             const unsigned char   *value,
184                                             int                    len);
185 dbus_bool_t   _dbus_marshal_int32_array    (DBusString            *str,
186                                             int                    byte_order,
187                                             const dbus_int32_t    *value,
188                                             int                    len);
189 dbus_bool_t   _dbus_marshal_uint32_array   (DBusString            *str,
190                                             int                    byte_order,
191                                             const dbus_uint32_t   *value,
192                                             int                    len);
193 #ifdef DBUS_HAVE_INT64
194 dbus_bool_t   _dbus_marshal_int64_array    (DBusString            *str,
195                                             int                    byte_order,
196                                             const dbus_int64_t    *value,
197                                             int                    len);
198 dbus_bool_t   _dbus_marshal_uint64_array   (DBusString            *str,
199                                             int                    byte_order,
200                                             const dbus_uint64_t   *value,
201                                             int                    len);
202 #endif /* DBUS_HAVE_INT64 */
203 dbus_bool_t   _dbus_marshal_double_array   (DBusString            *str,
204                                             int                    byte_order,
205                                             const double          *value,
206                                             int                    len);
207 dbus_bool_t   _dbus_marshal_string_array   (DBusString            *str,
208                                             int                    byte_order,
209                                             const char           **value,
210                                             int                    len);
211 double        _dbus_demarshal_double       (const DBusString      *str,
212                                             int                    byte_order,
213                                             int                    pos,
214                                             int                   *new_pos);
215 dbus_int32_t  _dbus_demarshal_int32        (const DBusString      *str,
216                                             int                    byte_order,
217                                             int                    pos,
218                                             int                   *new_pos);
219 dbus_uint32_t _dbus_demarshal_uint32       (const DBusString      *str,
220                                             int                    byte_order,
221                                             int                    pos,
222                                             int                   *new_pos);
223 #ifdef DBUS_HAVE_INT64
224 dbus_int64_t  _dbus_demarshal_int64        (const DBusString      *str,
225                                             int                    byte_order,
226                                             int                    pos,
227                                             int                   *new_pos);
228 dbus_uint64_t _dbus_demarshal_uint64       (const DBusString      *str,
229                                             int                    byte_order,
230                                             int                    pos,
231                                             int                   *new_pos);
232 #endif /* DBUS_HAVE_INT64 */
233 char *        _dbus_demarshal_string       (const DBusString      *str,
234                                             int                    byte_order,
235                                             int                    pos,
236                                             int                   *new_pos);
237 dbus_bool_t   _dbus_demarshal_byte_array   (const DBusString      *str,
238                                             int                    byte_order,
239                                             int                    pos,
240                                             int                   *new_pos,
241                                             unsigned char        **array,
242                                             int                   *array_len);
243 dbus_bool_t   _dbus_demarshal_int32_array  (const DBusString      *str,
244                                             int                    byte_order,
245                                             int                    pos,
246                                             int                   *new_pos,
247                                             dbus_int32_t         **array,
248                                             int                   *array_len);
249 dbus_bool_t   _dbus_demarshal_uint32_array (const DBusString      *str,
250                                             int                    byte_order,
251                                             int                    pos,
252                                             int                   *new_pos,
253                                             dbus_uint32_t        **array,
254                                             int                   *array_len);
255 #ifdef DBUS_HAVE_INT64
256 dbus_bool_t   _dbus_demarshal_int64_array  (const DBusString      *str,
257                                             int                    byte_order,
258                                             int                    pos,
259                                             int                   *new_pos,
260                                             dbus_int64_t         **array,
261                                             int                   *array_len);
262 dbus_bool_t   _dbus_demarshal_uint64_array (const DBusString      *str,
263                                             int                    byte_order,
264                                             int                    pos,
265                                             int                   *new_pos,
266                                             dbus_uint64_t        **array,
267                                             int                   *array_len);
268 #endif /* DBUS_HAVE_INT64 */
269 dbus_bool_t   _dbus_demarshal_double_array (const DBusString      *str,
270                                             int                    byte_order,
271                                             int                    pos,
272                                             int                   *new_pos,
273                                             double               **array,
274                                             int                   *array_len);
275 dbus_bool_t   _dbus_demarshal_string_array (const DBusString      *str,
276                                             int                    byte_order,
277                                             int                    pos,
278                                             int                   *new_pos,
279                                             char                ***array,
280                                             int                   *array_len);
281
282
283
284
285 dbus_bool_t _dbus_marshal_get_arg_end_pos (const DBusString *str,
286                                            int               byte_order,
287                                            int               type,
288                                            int               pos,
289                                            int              *end_pos);
290 dbus_bool_t _dbus_marshal_validate_type   (const DBusString *str,
291                                            int               pos,
292                                            int              *type,
293                                            int              *end_pos);
294 dbus_bool_t _dbus_marshal_validate_arg    (const DBusString *str,
295                                            int               depth,
296                                            int               byte_order,
297                                            int               type,
298                                            int               array_type_pos,
299                                            int               pos,
300                                            int              *end_pos);
301
302
303 #endif /* DBUS_PROTOCOL_H */