2005-05-11 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / dbus / dbus-marshal-recursive.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-recursive.h  Marshalling routines for recursive types
3  *
4  * Copyright (C) 2004, 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
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_RECURSIVE_H
25 #define DBUS_MARSHAL_RECURSIVE_H
26
27 #include <config.h>
28 #include <dbus/dbus-protocol.h>
29 #include <dbus/dbus-list.h>
30
31 #ifndef PACKAGE
32 #error "config.h not included here"
33 #endif
34
35 typedef struct DBusTypeMark        DBusTypeMark;
36 typedef struct DBusTypeReader      DBusTypeReader;
37 typedef struct DBusTypeWriter      DBusTypeWriter;
38 typedef struct DBusTypeReaderClass DBusTypeReaderClass;
39 typedef struct DBusArrayLenFixup   DBusArrayLenFixup;
40
41 /** The mark is a way to compress a #DBusTypeReader; it isn't all that
42  * successful though. The idea was to use this for caching header
43  * fields in dbus-message.c. However now I'm thinking why not cache
44  * the actual values (e.g. char*) and if the field needs to be set or
45  * deleted, just linear search for it. Those operations are uncommon,
46  * and getting the values should be fast and not involve all this type
47  * reader nonsense.
48  *
49  * @todo DBusTypeMark isn't used right now and probably won't be, we should delete it
50  */
51 struct DBusTypeMark
52 {
53   dbus_uint32_t type_pos_in_value_str : 1; /**< true if the type_pos is in value_str and not type_str */
54   dbus_uint32_t container_type : 3; /**< the "id" of the container type */
55   dbus_uint32_t array_len_offset : 3; /**< bytes back from start_pos that len ends */
56   dbus_uint32_t type_pos : DBUS_MAXIMUM_MESSAGE_LENGTH_BITS; /**< position in type_str */
57   dbus_uint32_t value_pos : DBUS_MAXIMUM_MESSAGE_LENGTH_BITS; /**< position in value_str */
58   dbus_uint32_t array_start_pos : DBUS_MAXIMUM_MESSAGE_LENGTH_BITS; /**< start of the array the reader was iterating over */
59 };
60
61 /**
62  * The type reader is an iterator for reading values from a block of
63  * values.
64  */
65 struct DBusTypeReader
66 {
67   dbus_uint32_t byte_order : 8; /**< byte order of the block */
68
69   dbus_uint32_t finished : 1;   /**< marks we're at end iterator for cases
70                                  * where we don't have another way to tell
71                                  */
72   dbus_uint32_t array_len_offset : 3; /**< bytes back from start_pos that len ends */
73   const DBusString *type_str;   /**< string containing signature of block */
74   int type_pos;                 /**< current position in signature */
75   const DBusString *value_str;  /**< string containing values of block */
76   int value_pos;                /**< current position in values */
77
78   const DBusTypeReaderClass *klass; /**< the vtable for the reader */
79   union
80   {
81     struct {
82       int start_pos;                /**< for array readers, the start of the array values */
83     } array;
84   } u; /**< class-specific data */
85 };
86
87 /**
88  * The type writer is an iterator for writing to a block of values.
89  */
90 struct DBusTypeWriter
91 {
92   dbus_uint32_t byte_order : 8;            /**< byte order to write values with */
93
94   dbus_uint32_t container_type : 8;        /**< what are we inside? (e.g. struct, variant, array) */
95
96   dbus_uint32_t type_pos_is_expectation : 1; /**< type_pos can be either an insertion point for or an expected next type */
97
98   dbus_uint32_t enabled : 1; /**< whether to write values */
99
100   DBusString *type_str; /**< where to write typecodes (or read type expectations) */
101   int type_pos;         /**< current pos in type_str */
102   DBusString *value_str; /**< where to write values */
103   int value_pos;         /**< next position to write */
104
105   union
106   {
107     struct {
108       int start_pos; /**< position of first element in the array */
109       int len_pos;   /**< position of length of the array */
110       int element_type_pos; /**< position of array element type in type_str */
111     } array;
112   } u; /**< class-specific data */
113 };
114
115 /**
116  * When modifying an existing block of values, array lengths may need
117  * to be adjusted; those adjustments are described by this struct.
118  */
119 struct DBusArrayLenFixup
120 {
121   int len_pos_in_reader; /**< where the length was in the original block */
122   int new_len;           /**< the new value of the length in the written-out block */
123 };
124
125 void        _dbus_type_reader_init                      (DBusTypeReader        *reader,
126                                                          int                    byte_order,
127                                                          const DBusString      *type_str,
128                                                          int                    type_pos,
129                                                          const DBusString      *value_str,
130                                                          int                    value_pos);
131 void        _dbus_type_reader_init_from_mark            (DBusTypeReader        *reader,
132                                                          int                    byte_order,
133                                                          const DBusString      *type_str,
134                                                          const DBusString      *value_str,
135                                                          const DBusTypeMark    *mark);
136 void        _dbus_type_reader_init_types_only           (DBusTypeReader        *reader,
137                                                          const DBusString      *type_str,
138                                                          int                    type_pos);
139 void        _dbus_type_reader_init_types_only_from_mark (DBusTypeReader        *reader,
140                                                          const DBusString      *type_str,
141                                                          const DBusTypeMark    *mark);
142 void        _dbus_type_reader_save_mark                 (const DBusTypeReader  *reader,
143                                                          DBusTypeMark          *mark);
144 int         _dbus_type_reader_get_current_type          (const DBusTypeReader  *reader);
145 int         _dbus_type_reader_get_element_type          (const DBusTypeReader  *reader);
146 int         _dbus_type_reader_get_value_pos             (const DBusTypeReader  *reader);
147 void        _dbus_type_reader_read_basic                (const DBusTypeReader  *reader,
148                                                          void                  *value);
149 int         _dbus_type_reader_get_array_length          (const DBusTypeReader  *reader);
150 void        _dbus_type_reader_read_fixed_multi          (const DBusTypeReader  *reader,
151                                                          void                  *value,
152                                                          int                   *n_elements);
153 void        _dbus_type_reader_read_raw                  (const DBusTypeReader  *reader,
154                                                          const unsigned char  **value_location);
155 void        _dbus_type_reader_recurse                   (DBusTypeReader        *reader,
156                                                          DBusTypeReader        *subreader);
157 dbus_bool_t _dbus_type_reader_next                      (DBusTypeReader        *reader);
158 dbus_bool_t _dbus_type_reader_has_next                  (const DBusTypeReader  *reader);
159 void        _dbus_type_reader_get_signature             (const DBusTypeReader  *reader,
160                                                          const DBusString     **str_p,
161                                                          int                   *start_p,
162                                                          int                   *len_p);
163 dbus_bool_t _dbus_type_reader_set_basic                 (DBusTypeReader        *reader,
164                                                          const void            *value,
165                                                          const DBusTypeReader  *realign_root);
166 dbus_bool_t _dbus_type_reader_delete                    (DBusTypeReader        *reader,
167                                                          const DBusTypeReader  *realign_root);
168 dbus_bool_t _dbus_type_reader_greater_than              (const DBusTypeReader  *lhs,
169                                                          const DBusTypeReader  *rhs);
170
171 dbus_bool_t _dbus_type_reader_equal_values              (const DBusTypeReader *lhs,
172                                                          const DBusTypeReader *rhs);
173
174 void        _dbus_type_signature_next                   (const char            *signature,
175                                                          int                   *type_pos);
176
177 void        _dbus_type_writer_init                 (DBusTypeWriter        *writer,
178                                                     int                    byte_order,
179                                                     DBusString            *type_str,
180                                                     int                    type_pos,
181                                                     DBusString            *value_str,
182                                                     int                    value_pos);
183 void        _dbus_type_writer_init_types_delayed   (DBusTypeWriter        *writer,
184                                                     int                    byte_order,
185                                                     DBusString            *value_str,
186                                                     int                    value_pos);
187 void        _dbus_type_writer_add_types            (DBusTypeWriter        *writer,
188                                                     DBusString            *type_str,
189                                                     int                    type_pos);
190 void        _dbus_type_writer_remove_types         (DBusTypeWriter        *writer);
191 void        _dbus_type_writer_init_values_only     (DBusTypeWriter        *writer,
192                                                     int                    byte_order,
193                                                     const DBusString      *type_str,
194                                                     int                    type_pos,
195                                                     DBusString            *value_str,
196                                                     int                    value_pos);
197 dbus_bool_t _dbus_type_writer_write_basic          (DBusTypeWriter        *writer,
198                                                     int                    type,
199                                                     const void            *value);
200 dbus_bool_t _dbus_type_writer_write_fixed_multi    (DBusTypeWriter        *writer,
201                                                     int                    element_type,
202                                                     const void            *value,
203                                                     int                    n_elements);
204 dbus_bool_t _dbus_type_writer_recurse              (DBusTypeWriter        *writer,
205                                                     int                    container_type,
206                                                     const DBusString      *contained_type,
207                                                     int                    contained_type_start,
208                                                     DBusTypeWriter        *sub);
209 dbus_bool_t _dbus_type_writer_unrecurse            (DBusTypeWriter        *writer,
210                                                     DBusTypeWriter        *sub);
211 dbus_bool_t _dbus_type_writer_append_array         (DBusTypeWriter        *writer,
212                                                     const DBusString      *contained_type,
213                                                     int                    contained_type_start,
214                                                     DBusTypeWriter        *sub);
215 dbus_bool_t _dbus_type_writer_write_reader         (DBusTypeWriter        *writer,
216                                                     DBusTypeReader        *reader);
217 dbus_bool_t _dbus_type_writer_write_reader_partial (DBusTypeWriter        *writer,
218                                                     DBusTypeReader        *reader,
219                                                     const DBusTypeReader  *start_after,
220                                                     int                    start_after_new_pos,
221                                                     int                    start_after_new_len,
222                                                     DBusList             **fixups);
223 void        _dbus_type_writer_set_enabled          (DBusTypeWriter        *writer,
224                                                     dbus_bool_t            enabled);
225
226
227 #endif /* DBUS_MARSHAL_RECURSIVE_H */