2003-03-12 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-dict.h>
29 #include <dbus/dbus-protocol.h>
30 #include <dbus/dbus-types.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 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
50 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
51
52 #ifdef WORDS_BIGENDIAN
53 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
54 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
55 #define DBUS_INT32_TO_LE(val)   (DBUS_INT32_SWAP_LE_BE (val))
56 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
57 #else
58 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
59 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
60 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
61 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
62 #endif
63
64 /* The transformation is symmetric, so the FROM just maps to the TO. */
65 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
66 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
67 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
68 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
69
70 void          _dbus_pack_int32    (dbus_int32_t         value,
71                                    int                  byte_order,
72                                    unsigned char       *data);
73 dbus_int32_t  _dbus_unpack_int32  (int                  byte_order,
74                                    const unsigned char *data);
75 void          _dbus_pack_uint32   (dbus_uint32_t        value,
76                                    int                  byte_order,
77                                    unsigned char       *data);
78 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
79                                    const unsigned char *data);
80
81 void        _dbus_marshal_set_int32  (DBusString       *str,
82                                       int               byte_order,
83                                       int               offset,
84                                       dbus_int32_t      value);
85 void        _dbus_marshal_set_uint32 (DBusString       *str,
86                                       int               byte_order,
87                                       int               offset,
88                                       dbus_uint32_t     value);
89 dbus_bool_t _dbus_marshal_set_string (DBusString       *str,
90                                       int               byte_order,
91                                       int               offset,
92                                       const DBusString *value,
93                                       int               len);
94
95
96 dbus_bool_t _dbus_marshal_int32       (DBusString          *str,
97                                        int                  byte_order,
98                                        dbus_int32_t         value);
99 dbus_bool_t _dbus_marshal_uint32      (DBusString          *str,
100                                        int                  byte_order,
101                                        dbus_uint32_t        value);
102 dbus_bool_t _dbus_marshal_double      (DBusString          *str,
103                                        int                  byte_order,
104                                        double               value);
105 dbus_bool_t _dbus_marshal_string      (DBusString          *str,
106                                        int                  byte_order,
107                                        const char          *value);
108 dbus_bool_t _dbus_marshal_byte_array  (DBusString          *str,
109                                        int                  byte_order,
110                                        const unsigned char *value,
111                                        int                  len);
112 dbus_bool_t _dbus_marshal_int32_array (DBusString          *str,
113                                        int                  byte_order,
114                                        const dbus_int32_t  *value,
115                                        int                  len);
116 dbus_bool_t _dbus_marshal_uint32_array (DBusString          *str,
117                                         int                  byte_order,
118                                         const dbus_uint32_t  *value,
119                                         int                  len);
120 dbus_bool_t _dbus_marshal_double_array (DBusString          *str,
121                                         int                  byte_order,
122                                         const double        *value,
123                                         int                  len);
124 dbus_bool_t _dbus_marshal_string_array (DBusString          *str,
125                                         int                  byte_order,
126                                         const char         **value,
127                                         int                  len);
128 dbus_bool_t _dbus_marshal_dict         (DBusString          *str,
129                                         int                  byte_order,
130                                         DBusDict            *dict);
131
132 double         _dbus_demarshal_double       (const DBusString *str,
133                                              int               byte_order,
134                                              int               pos,
135                                              int              *new_pos);
136 dbus_int32_t   _dbus_demarshal_int32        (const DBusString *str,
137                                              int               byte_order,
138                                              int               pos,
139                                              int              *new_pos);
140 dbus_uint32_t  _dbus_demarshal_uint32       (const DBusString *str,
141                                              int               byte_order,
142                                              int               pos,
143                                              int              *new_pos);
144 char *         _dbus_demarshal_string       (const DBusString *str,
145                                              int               byte_order,
146                                              int               pos,
147                                              int              *new_pos);
148 unsigned char *_dbus_demarshal_byte_array   (const DBusString *str,
149                                              int               byte_order,
150                                              int               pos,
151                                              int              *new_pos,
152                                              int              *array_len);
153 dbus_int32_t * _dbus_demarshal_int32_array  (const DBusString *str,
154                                              int               byte_order,
155                                              int               pos,
156                                              int              *new_pos,
157                                              int              *array_len);
158 dbus_uint32_t *_dbus_demarshal_uint32_array (const DBusString *str,
159                                              int               byte_order,
160                                              int               pos,
161                                              int              *new_pos,
162                                              int              *array_len);
163 double *       _dbus_demarshal_double_array (const DBusString *str,
164                                              int               byte_order,
165                                              int               pos,
166                                              int              *new_pos,
167                                              int              *array_len);
168 char **        _dbus_demarshal_string_array (const DBusString *str,
169                                              int               byte_order,
170                                              int               pos,
171                                              int              *new_pos,
172                                              int              *array_len);
173 DBusDict *     _dbus_demarshal_dict         (const DBusString *str,
174                                              int               byte_order,
175                                              int               pos,
176                                              int              *new_pos);
177
178
179
180 dbus_bool_t _dbus_marshal_get_arg_end_pos (const DBusString *str,
181                                            int               byte_order,
182                                            int               pos,
183                                            int              *end_pos);
184 dbus_bool_t _dbus_marshal_validate_arg    (const DBusString *str,
185                                            int               byte_order,
186                                            int               pos,
187                                            int              *end_pos);
188
189
190 #endif /* DBUS_PROTOCOL_H */