2005-01-26 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 typedef struct DBusTypeMark        DBusTypeMark;
37 typedef struct DBusTypeReader      DBusTypeReader;
38 typedef struct DBusTypeWriter      DBusTypeWriter;
39 typedef struct DBusTypeReaderClass DBusTypeReaderClass;
40 typedef struct DBusArrayLenFixup   DBusArrayLenFixup;
41
42 /** The mark is a way to compress a #DBusTypeReader; it isn't all that
43  * successful though. The idea was to use this for caching header
44  * fields in dbus-message.c. However now I'm thinking why not cache
45  * the actual values (e.g. char*) and if the field needs to be set or
46  * deleted, just linear search for it. Those operations are uncommon,
47  * and getting the values should be fast and not involve all this type
48  * reader nonsense.
49  *
50  * @todo DBusTypeMark isn't used right now and probably won't be, we should delete it
51  */
52 struct DBusTypeMark
53 {
54   dbus_uint32_t type_pos_in_value_str : 1; /**< true if the type_pos is in value_str and not type_str */
55   dbus_uint32_t container_type : 3; /**< the "id" of the container type */
56   dbus_uint32_t array_len_offset : 3; /**< bytes back from start_pos that len ends */
57   dbus_uint32_t type_pos : DBUS_MAXIMUM_MESSAGE_LENGTH_BITS; /**< position in type_str */
58   dbus_uint32_t value_pos : DBUS_MAXIMUM_MESSAGE_LENGTH_BITS; /**< position in value_str */
59   dbus_uint32_t array_start_pos : DBUS_MAXIMUM_MESSAGE_LENGTH_BITS; /**< start of the array the reader was iterating over */
60 };
61
62 /**
63  * The type reader is an iterator for reading values from a block of
64  * values.
65  */
66 struct DBusTypeReader
67 {
68   dbus_uint32_t byte_order : 8; /**< byte order of the block */
69
70   dbus_uint32_t finished : 1;   /**< marks we're at end iterator for cases
71                                  * where we don't have another way to tell
72                                  */
73   dbus_uint32_t array_len_offset : 3; /**< bytes back from start_pos that len ends */
74   const DBusString *type_str;   /**< string containing signature of block */
75   int type_pos;                 /**< current position in signature */
76   const DBusString *value_str;  /**< string containing values of block */
77   int value_pos;                /**< current position in values */
78
79   const DBusTypeReaderClass *klass; /**< the vtable for the reader */
80   union
81   {
82     struct {
83       int start_pos;                /**< for array readers, the start of the array values */
84     } array;
85   } u; /**< class-specific data */
86 };
87
88 /**
89  * The type writer is an iterator for writing to a block of values.
90  */
91 struct DBusTypeWriter
92 {
93   dbus_uint32_t byte_order : 8;            /**< byte order to write values with */
94
95   dbus_uint32_t container_type : 8;        /**< what are we inside? (e.g. struct, variant, array) */
96
97   dbus_uint32_t type_pos_is_expectation : 1; /**< type_pos can be either an insertion point for or an expected next type */
98
99   dbus_uint32_t enabled : 1; /**< whether to write values */
100
101   DBusString *type_str; /**< where to write typecodes (or read type expectations) */
102   int type_pos;         /**< current pos in type_str */
103   DBusString *value_str; /**< where to write values */
104   int value_pos;         /**< next position to write */
105
106   union
107   {
108     struct {
109       int start_pos; /**< position of first element in the array */
110       int len_pos;   /**< position of length of the array */
111       int element_type_pos; /**< position of array element type in type_str */
112     } array;
113   } u; /**< class-specific data */
114 };
115
116 /**
117  * When modifying an existing block of values, array lengths may need
118  * to be adjusted; those adjustments are described by this struct.
119  */
120 struct DBusArrayLenFixup
121 {
122   int len_pos_in_reader; /**< where the length was in the original block */
123   int new_len;           /**< the new value of the length in the written-out block */
124 };
125
126 void        _dbus_type_reader_init                      (DBusTypeReader        *reader,
127                                                          int                    byte_order,
128                                                          const DBusString      *type_str,
129                                                          int                    type_pos,
130                                                          const DBusString      *value_str,
131                                                          int                    value_pos);
132 void        _dbus_type_reader_init_from_mark            (DBusTypeReader        *reader,
133                                                          int                    byte_order,
134                                                          const DBusString      *type_str,
135                                                          const DBusString      *value_str,
136                                                          const DBusTypeMark    *mark);
137 void        _dbus_type_reader_init_types_only           (DBusTypeReader        *reader,
138                                                          const DBusString      *type_str,
139                                                          int                    type_pos);
140 void        _dbus_type_reader_init_types_only_from_mark (DBusTypeReader        *reader,
141                                                          const DBusString      *type_str,
142                                                          const DBusTypeMark    *mark);
143 void        _dbus_type_reader_save_mark                 (const DBusTypeReader  *reader,
144                                                          DBusTypeMark          *mark);
145 int         _dbus_type_reader_get_current_type          (const DBusTypeReader  *reader);
146 int         _dbus_type_reader_get_element_type          (const DBusTypeReader  *reader);
147 int         _dbus_type_reader_get_value_pos             (const DBusTypeReader  *reader);
148 void        _dbus_type_reader_read_basic                (const DBusTypeReader  *reader,
149                                                          void                  *value);
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 void        _dbus_type_writer_init                 (DBusTypeWriter        *writer,
172                                                     int                    byte_order,
173                                                     DBusString            *type_str,
174                                                     int                    type_pos,
175                                                     DBusString            *value_str,
176                                                     int                    value_pos);
177 void        _dbus_type_writer_init_types_delayed   (DBusTypeWriter        *writer,
178                                                     int                    byte_order,
179                                                     DBusString            *value_str,
180                                                     int                    value_pos);
181 void        _dbus_type_writer_add_types            (DBusTypeWriter        *writer,
182                                                     DBusString            *type_str,
183                                                     int                    type_pos);
184 void        _dbus_type_writer_remove_types         (DBusTypeWriter        *writer);
185 void        _dbus_type_writer_init_values_only     (DBusTypeWriter        *writer,
186                                                     int                    byte_order,
187                                                     const DBusString      *type_str,
188                                                     int                    type_pos,
189                                                     DBusString            *value_str,
190                                                     int                    value_pos);
191 dbus_bool_t _dbus_type_writer_write_basic          (DBusTypeWriter        *writer,
192                                                     int                    type,
193                                                     const void            *value);
194 dbus_bool_t _dbus_type_writer_write_fixed_multi    (DBusTypeWriter        *writer,
195                                                     int                    element_type,
196                                                     const void            *value,
197                                                     int                    n_elements);
198 dbus_bool_t _dbus_type_writer_recurse              (DBusTypeWriter        *writer,
199                                                     int                    container_type,
200                                                     const DBusString      *contained_type,
201                                                     int                    contained_type_start,
202                                                     DBusTypeWriter        *sub);
203 dbus_bool_t _dbus_type_writer_unrecurse            (DBusTypeWriter        *writer,
204                                                     DBusTypeWriter        *sub);
205 dbus_bool_t _dbus_type_writer_append_array         (DBusTypeWriter        *writer,
206                                                     const DBusString      *contained_type,
207                                                     int                    contained_type_start,
208                                                     DBusTypeWriter        *sub);
209 dbus_bool_t _dbus_type_writer_write_reader         (DBusTypeWriter        *writer,
210                                                     DBusTypeReader        *reader);
211 dbus_bool_t _dbus_type_writer_write_reader_partial (DBusTypeWriter        *writer,
212                                                     DBusTypeReader        *reader,
213                                                     const DBusTypeReader  *start_after,
214                                                     int                    start_after_new_pos,
215                                                     int                    start_after_new_len,
216                                                     DBusList             **fixups);
217 void        _dbus_type_writer_set_enabled          (DBusTypeWriter        *writer,
218                                                     dbus_bool_t            enabled);
219
220
221 #endif /* DBUS_MARSHAL_RECURSIVE_H */