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