1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-recursive.c 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 #include "dbus-marshal-recursive.h"
25 #include "dbus-marshal-basic.h"
26 #include "dbus-signature.h"
27 #include "dbus-internals.h"
28
29 /**
30 * @addtogroup DBusMarshal
31 * @{
32 */
33
34 /** turn this on to get deluged in TypeReader verbose spam */
35 #define RECURSIVE_MARSHAL_READ_TRACE 0
36
37 /** turn this on to get deluged in TypeWriter verbose spam */
38 #define RECURSIVE_MARSHAL_WRITE_TRACE 0
39
40 static void
free_fixups(DBusList ** fixups)41 free_fixups (DBusList **fixups)
42 {
43 DBusList *link;
44
45 link = _dbus_list_get_first_link (fixups);
46 while (link != NULL)
47 {
48 DBusList *next;
49
50 next = _dbus_list_get_next_link (fixups, link);
51
52 dbus_free (link->data);
53 _dbus_list_free_link (link);
54
55 link = next;
56 }
57
58 *fixups = NULL;
59 }
60
61 static void
apply_and_free_fixups(DBusList ** fixups,DBusTypeReader * reader)62 apply_and_free_fixups (DBusList **fixups,
63 DBusTypeReader *reader)
64 {
65 DBusList *link;
66
67 #if RECURSIVE_MARSHAL_WRITE_TRACE
68 if (*fixups)
69 _dbus_verbose (" %d FIXUPS to apply\n",
70 _dbus_list_get_length (fixups));
71 #endif
72
73 link = _dbus_list_get_first_link (fixups);
74 while (link != NULL)
75 {
76 DBusList *next;
77
78 next = _dbus_list_get_next_link (fixups, link);
79
80 if (reader)
81 {
82 DBusArrayLenFixup *f;
83
84 f = link->data;
85
86 #if RECURSIVE_MARSHAL_WRITE_TRACE
87 _dbus_verbose (" applying FIXUP to reader %p at pos %d new_len = %d old len %d\n",
88 reader, f->len_pos_in_reader, f->new_len,
89 _dbus_marshal_read_uint32 (reader->value_str,
90 f->len_pos_in_reader,
91 reader->byte_order, NULL));
92 #endif
93
94 _dbus_marshal_set_uint32 ((DBusString*) reader->value_str,
95 f->len_pos_in_reader,
96 f->new_len,
97 reader->byte_order);
98 }
99
100 dbus_free (link->data);
101 _dbus_list_free_link (link);
102
103 link = next;
104 }
105
106 *fixups = NULL;
107 }
108
109 /**
110 * Virtual table for a type reader.
111 */
112 struct DBusTypeReaderClass
113 {
114 const char *name; /**< name for debugging */
115 int id; /**< index in all_reader_classes */
116 dbus_bool_t types_only; /**< only iterates over types, not values */
117 void (* recurse) (DBusTypeReader *sub,
118 DBusTypeReader *parent); /**< recurse with this reader as sub */
119 dbus_bool_t (* check_finished) (const DBusTypeReader *reader); /**< check whether reader is at the end */
120 void (* next) (DBusTypeReader *reader,
121 int current_type); /**< go to the next value */
122 };
123
124 static int
element_type_get_alignment(const DBusString * str,int pos)125 element_type_get_alignment (const DBusString *str,
126 int pos)
127 {
128 return _dbus_type_get_alignment (_dbus_first_type_in_signature (str, pos));
129 }
130
131 static void
reader_init(DBusTypeReader * reader,int byte_order,const DBusString * type_str,int type_pos,const DBusString * value_str,int value_pos)132 reader_init (DBusTypeReader *reader,
133 int byte_order,
134 const DBusString *type_str,
135 int type_pos,
136 const DBusString *value_str,
137 int value_pos)
138 {
139 reader->byte_order = byte_order;
140 reader->finished = FALSE;
141 reader->type_str = type_str;
142 reader->type_pos = type_pos;
143 reader->value_str = value_str;
144 reader->value_pos = value_pos;
145 }
146
147 static void
base_reader_recurse(DBusTypeReader * sub,DBusTypeReader * parent)148 base_reader_recurse (DBusTypeReader *sub,
149 DBusTypeReader *parent)
150 {
151 /* point subreader at the same place as parent */
152 reader_init (sub,
153 parent->byte_order,
154 parent->type_str,
155 parent->type_pos,
156 parent->value_str,
157 parent->value_pos);
158 }
159
160 static void
struct_or_dict_entry_types_only_reader_recurse(DBusTypeReader * sub,DBusTypeReader * parent)161 struct_or_dict_entry_types_only_reader_recurse (DBusTypeReader *sub,
162 DBusTypeReader *parent)
163 {
164 base_reader_recurse (sub, parent);
165
166 _dbus_assert (_dbus_string_get_byte (sub->type_str,
167 sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR ||
168 _dbus_string_get_byte (sub->type_str,
169 sub->type_pos) == DBUS_DICT_ENTRY_BEGIN_CHAR);
170
171 sub->type_pos += 1;
172 }
173
174 static void
struct_or_dict_entry_reader_recurse(DBusTypeReader * sub,DBusTypeReader * parent)175 struct_or_dict_entry_reader_recurse (DBusTypeReader *sub,
176 DBusTypeReader *parent)
177 {
178 struct_or_dict_entry_types_only_reader_recurse (sub, parent);
179
180 /* struct and dict entry have 8 byte alignment */
181 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
182 }
183
184 static void
array_types_only_reader_recurse(DBusTypeReader * sub,DBusTypeReader * parent)185 array_types_only_reader_recurse (DBusTypeReader *sub,
186 DBusTypeReader *parent)
187 {
188 base_reader_recurse (sub, parent);
189
190 /* point type_pos at the array element type */
191 sub->type_pos += 1;
192
193 /* Init with values likely to crash things if misused */
194 sub->u.array.start_pos = _DBUS_INT_MAX;
195 sub->array_len_offset = 7;
196 }
197
198 /** compute position of array length given array_len_offset, which is
199 the offset back from start_pos to end of the len */
200 #define ARRAY_READER_LEN_POS(reader) \
201 ((reader)->u.array.start_pos - ((int)(reader)->array_len_offset) - 4)
202
203 static int
array_reader_get_array_len(const DBusTypeReader * reader)204 array_reader_get_array_len (const DBusTypeReader *reader)
205 {
206 dbus_uint32_t array_len;
207 int len_pos;
208
209 len_pos = ARRAY_READER_LEN_POS (reader);
210
211 _dbus_assert (_DBUS_ALIGN_VALUE (len_pos, 4) == (unsigned) len_pos);
212 array_len = _dbus_unpack_uint32 (reader->byte_order,
213 _dbus_string_get_const_data_len (reader->value_str, len_pos, 4));
214
215 #if RECURSIVE_MARSHAL_READ_TRACE
216 _dbus_verbose (" reader %p len_pos %d array len %u len_offset %d\n",
217 reader, len_pos, array_len, reader->array_len_offset);
218 #endif
219
220 _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
221
222 return array_len;
223 }
224
225 static void
array_reader_recurse(DBusTypeReader * sub,DBusTypeReader * parent)226 array_reader_recurse (DBusTypeReader *sub,
227 DBusTypeReader *parent)
228 {
229 int alignment;
230 int len_pos;
231
232 array_types_only_reader_recurse (sub, parent);
233
234 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
235
236 len_pos = sub->value_pos;
237
238 sub->value_pos += 4; /* for the length */
239
240 alignment = element_type_get_alignment (sub->type_str,
241 sub->type_pos);
242
243 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
244
245 sub->u.array.start_pos = sub->value_pos;
246 _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
247 sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
248
249 #if RECURSIVE_MARSHAL_READ_TRACE
250 _dbus_verbose (" type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
251 sub,
252 sub->u.array.start_pos,
253 sub->array_len_offset,
254 array_reader_get_array_len (sub),
255 _dbus_type_to_string (_dbus_first_type_in_signature (sub->type_str,
256 sub->type_pos)));
257 #endif
258 }
259
260 static void
variant_reader_recurse(DBusTypeReader * sub,DBusTypeReader * parent)261 variant_reader_recurse (DBusTypeReader *sub,
262 DBusTypeReader *parent)
263 {
264 int sig_len;
265 int contained_alignment;
266
267 base_reader_recurse (sub, parent);
268
269 /* Variant is 1 byte sig length (without nul), signature with nul,
270 * padding to 8-boundary, then values
271 */
272
273 sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
274
275 sub->type_str = sub->value_str;
276 sub->type_pos = sub->value_pos + 1;
277
278 sub->value_pos = sub->type_pos + sig_len + 1;
279
280 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (sub->type_str,
281 sub->type_pos));
282
283 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
284
285 #if RECURSIVE_MARSHAL_READ_TRACE
286 _dbus_verbose (" type reader %p variant containing '%s'\n",
287 sub,
288 _dbus_string_get_const_data_len (sub->type_str,
289 sub->type_pos, 0));
290 #endif
291 }
292
293 static dbus_bool_t
array_reader_check_finished(const DBusTypeReader * reader)294 array_reader_check_finished (const DBusTypeReader *reader)
295 {
296 int end_pos;
297
298 /* return the array element type if elements remain, and
299 * TYPE_INVALID otherwise
300 */
301
302 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
303
304 _dbus_assert (reader->value_pos <= end_pos);
305 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
306
307 return reader->value_pos == end_pos;
308 }
309
310 static void
skip_one_complete_type(const DBusString * type_str,int * type_pos)311 skip_one_complete_type (const DBusString *type_str,
312 int *type_pos)
313 {
314 _dbus_type_signature_next (_dbus_string_get_const_data (type_str),
315 type_pos);
316 }
317
318 /**
319 * Skips to the next "complete" type inside a type signature.
320 * The signature is read starting at type_pos, and the next
321 * type position is stored in the same variable.
322 *
323 * @param type_str a type signature (must be valid)
324 * @param type_pos an integer position in the type signature (in and out)
325 */
326 void
_dbus_type_signature_next(const char * type_str,int * type_pos)327 _dbus_type_signature_next (const char *type_str,
328 int *type_pos)
329 {
330 const unsigned char *p;
331 const unsigned char *start;
332
333 _dbus_assert (type_str != NULL);
334 _dbus_assert (type_pos != NULL);
335
336 start = type_str;
337 p = start + *type_pos;
338
339 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
340 _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
341
342 while (*p == DBUS_TYPE_ARRAY)
343 ++p;
344
345 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
346 _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
347
348 if (*p == DBUS_STRUCT_BEGIN_CHAR)
349 {
350 int depth;
351
352 depth = 1;
353
354 while (TRUE)
355 {
356 _dbus_assert (*p != DBUS_TYPE_INVALID);
357
358 ++p;
359
360 _dbus_assert (*p != DBUS_TYPE_INVALID);
361
362 if (*p == DBUS_STRUCT_BEGIN_CHAR)
363 depth += 1;
364 else if (*p == DBUS_STRUCT_END_CHAR)
365 {
366 depth -= 1;
367 if (depth == 0)
368 {
369 ++p;
370 break;
371 }
372 }
373 }
374 }
375 else if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
376 {
377 int depth;
378
379 depth = 1;
380
381 while (TRUE)
382 {
383 _dbus_assert (*p != DBUS_TYPE_INVALID);
384
385 ++p;
386
387 _dbus_assert (*p != DBUS_TYPE_INVALID);
388
389 if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
390 depth += 1;
391 else if (*p == DBUS_DICT_ENTRY_END_CHAR)
392 {
393 depth -= 1;
394 if (depth == 0)
395 {
396 ++p;
397 break;
398 }
399 }
400 }
401 }
402 else
403 {
404 ++p;
405 }
406
407 *type_pos = (int) (p - start);
408 }
409
410 static int
find_len_of_complete_type(const DBusString * type_str,int type_pos)411 find_len_of_complete_type (const DBusString *type_str,
412 int type_pos)
413 {
414 int end;
415
416 end = type_pos;
417
418 skip_one_complete_type (type_str, &end);
419
420 return end - type_pos;
421 }
422
423 static void
base_reader_next(DBusTypeReader * reader,int current_type)424 base_reader_next (DBusTypeReader *reader,
425 int current_type)
426 {
427 switch (current_type)
428 {
429 case DBUS_TYPE_DICT_ENTRY:
430 case DBUS_TYPE_STRUCT:
431 case DBUS_TYPE_VARIANT:
432 /* Scan forward over the entire container contents */
433 {
434 DBusTypeReader sub;
435
436 if (reader->klass->types_only && current_type == DBUS_TYPE_VARIANT)
437 ;
438 else
439 {
440 /* Recurse into the struct or variant */
441 _dbus_type_reader_recurse (reader, &sub);
442
443 /* Skip everything in this subreader */
444 while (_dbus_type_reader_next (&sub))
445 {
446 /* nothing */;
447 }
448 }
449 if (!reader->klass->types_only)
450 reader->value_pos = sub.value_pos;
451
452 /* Now we are at the end of this container; for variants, the
453 * subreader's type_pos is totally inapplicable (it's in the
454 * value string) but we know that we increment by one past the
455 * DBUS_TYPE_VARIANT
456 */
457 if (current_type == DBUS_TYPE_VARIANT)
458 reader->type_pos += 1;
459 else
460 reader->type_pos = sub.type_pos;
461 }
462 break;
463
464 case DBUS_TYPE_ARRAY:
465 {
466 if (!reader->klass->types_only)
467 _dbus_marshal_skip_array (reader->value_str,
468 _dbus_first_type_in_signature (reader->type_str,
469 reader->type_pos + 1),
470 reader->byte_order,
471 &reader->value_pos);
472
473 skip_one_complete_type (reader->type_str, &reader->type_pos);
474 }
475 break;
476
477 default:
478 if (!reader->klass->types_only)
479 _dbus_marshal_skip_basic (reader->value_str,
480 current_type, reader->byte_order,
481 &reader->value_pos);
482
483 reader->type_pos += 1;
484 break;
485 }
486 }
487
488 static void
struct_reader_next(DBusTypeReader * reader,int current_type)489 struct_reader_next (DBusTypeReader *reader,
490 int current_type)
491 {
492 int t;
493
494 base_reader_next (reader, current_type);
495
496 /* for STRUCT containers we return FALSE at the end of the struct,
497 * for INVALID we return FALSE at the end of the signature.
498 * In both cases we arrange for get_current_type() to return INVALID
499 * which is defined to happen iff we're at the end (no more next())
500 */
501 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
502 if (t == DBUS_STRUCT_END_CHAR)
503 {
504 reader->type_pos += 1;
505 reader->finished = TRUE;
506 }
507 }
508
509 static void
dict_entry_reader_next(DBusTypeReader * reader,int current_type)510 dict_entry_reader_next (DBusTypeReader *reader,
511 int current_type)
512 {
513 int t;
514
515 base_reader_next (reader, current_type);
516
517 /* for STRUCT containers we return FALSE at the end of the struct,
518 * for INVALID we return FALSE at the end of the signature.
519 * In both cases we arrange for get_current_type() to return INVALID
520 * which is defined to happen iff we're at the end (no more next())
521 */
522 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
523 if (t == DBUS_DICT_ENTRY_END_CHAR)
524 {
525 reader->type_pos += 1;
526 reader->finished = TRUE;
527 }
528 }
529
530 static void
array_types_only_reader_next(DBusTypeReader * reader,int current_type)531 array_types_only_reader_next (DBusTypeReader *reader,
532 int current_type)
533 {
534 /* We have one "element" to be iterated over
535 * in each array, which is its element type.
536 * So the finished flag indicates whether we've
537 * iterated over it yet or not.
538 */
539 reader->finished = TRUE;
540 }
541
542 static void
array_reader_next(DBusTypeReader * reader,int current_type)543 array_reader_next (DBusTypeReader *reader,
544 int current_type)
545 {
546 /* Skip one array element */
547 int end_pos;
548
549 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
550
551 #if RECURSIVE_MARSHAL_READ_TRACE
552 _dbus_verbose (" reader %p array next START start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
553 reader,
554 reader->u.array.start_pos,
555 end_pos, reader->value_pos,
556 _dbus_type_to_string (current_type));
557 #endif
558
559 _dbus_assert (reader->value_pos < end_pos);
560 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
561
562 switch (_dbus_first_type_in_signature (reader->type_str,
563 reader->type_pos))
564 {
565 case DBUS_TYPE_DICT_ENTRY:
566 case DBUS_TYPE_STRUCT:
567 case DBUS_TYPE_VARIANT:
568 {
569 DBusTypeReader sub;
570
571 /* Recurse into the struct or variant */
572 _dbus_type_reader_recurse (reader, &sub);
573
574 /* Skip everything in this element */
575 while (_dbus_type_reader_next (&sub))
576 {
577 /* nothing */;
578 }
579
580 /* Now we are at the end of this element */
581 reader->value_pos = sub.value_pos;
582 }
583 break;
584
585 case DBUS_TYPE_ARRAY:
586 {
587 _dbus_marshal_skip_array (reader->value_str,
588 _dbus_first_type_in_signature (reader->type_str,
589 reader->type_pos + 1),
590 reader->byte_order,
591 &reader->value_pos);
592 }
593 break;
594
595 default:
596 {
597 _dbus_marshal_skip_basic (reader->value_str,
598 current_type, reader->byte_order,
599 &reader->value_pos);
600 }
601 break;
602 }
603
604 #if RECURSIVE_MARSHAL_READ_TRACE
605 _dbus_verbose (" reader %p array next END start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
606 reader,
607 reader->u.array.start_pos,
608 end_pos, reader->value_pos,
609 _dbus_type_to_string (current_type));
610 #endif
611
612 _dbus_assert (reader->value_pos <= end_pos);
613
614 if (reader->value_pos == end_pos)
615 {
616 skip_one_complete_type (reader->type_str,
617 &reader->type_pos);
618 }
619 }
620
621 static const DBusTypeReaderClass body_reader_class = {
622 "body", 0,
623 FALSE,
624 NULL, /* body is always toplevel, so doesn't get recursed into */
625 NULL,
626 base_reader_next
627 };
628
629 static const DBusTypeReaderClass body_types_only_reader_class = {
630 "body types", 1,
631 TRUE,
632 NULL, /* body is always toplevel, so doesn't get recursed into */
633 NULL,
634 base_reader_next
635 };
636
637 static const DBusTypeReaderClass struct_reader_class = {
638 "struct", 2,
639 FALSE,
640 struct_or_dict_entry_reader_recurse,
641 NULL,
642 struct_reader_next
643 };
644
645 static const DBusTypeReaderClass struct_types_only_reader_class = {
646 "struct types", 3,
647 TRUE,
648 struct_or_dict_entry_types_only_reader_recurse,
649 NULL,
650 struct_reader_next
651 };
652
653 static const DBusTypeReaderClass dict_entry_reader_class = {
654 "dict_entry", 4,
655 FALSE,
656 struct_or_dict_entry_reader_recurse,
657 NULL,
658 dict_entry_reader_next
659 };
660
661 static const DBusTypeReaderClass dict_entry_types_only_reader_class = {
662 "dict_entry types", 5,
663 TRUE,
664 struct_or_dict_entry_types_only_reader_recurse,
665 NULL,
666 dict_entry_reader_next
667 };
668
669 static const DBusTypeReaderClass array_reader_class = {
670 "array", 6,
671 FALSE,
672 array_reader_recurse,
673 array_reader_check_finished,
674 array_reader_next
675 };
676
677 static const DBusTypeReaderClass array_types_only_reader_class = {
678 "array types", 7,
679 TRUE,
680 array_types_only_reader_recurse,
681 NULL,
682 array_types_only_reader_next
683 };
684
685 static const DBusTypeReaderClass variant_reader_class = {
686 "variant", 8,
687 FALSE,
688 variant_reader_recurse,
689 NULL,
690 base_reader_next
691 };
692
693 static const DBusTypeReaderClass const *
694 all_reader_classes[] = {
695 &body_reader_class,
696 &body_types_only_reader_class,
697 &struct_reader_class,
698 &struct_types_only_reader_class,
699 &dict_entry_reader_class,
700 &dict_entry_types_only_reader_class,
701 &array_reader_class,
702 &array_types_only_reader_class,
703 &variant_reader_class
704 };
705
706 /**
707 * Initializes a type reader.
708 *
709 * @param reader the reader
710 * @param byte_order the byte order of the block to read
711 * @param type_str the signature of the block to read
712 * @param type_pos location of signature
713 * @param value_str the string containing values block
714 * @param value_pos start of values block
715 */
716 void
_dbus_type_reader_init(DBusTypeReader * reader,int byte_order,const DBusString * type_str,int type_pos,const DBusString * value_str,int value_pos)717 _dbus_type_reader_init (DBusTypeReader *reader,
718 int byte_order,
719 const DBusString *type_str,
720 int type_pos,
721 const DBusString *value_str,
722 int value_pos)
723 {
724 reader->klass = &body_reader_class;
725
726 reader_init (reader, byte_order, type_str, type_pos,
727 value_str, value_pos);
728
729 #if RECURSIVE_MARSHAL_READ_TRACE
730 _dbus_verbose (" type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
731 reader, reader->type_pos, reader->value_pos,
732 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
733 #endif
734 }
735
736 /**
737 * Like _dbus_type_reader_init() but the iteration is over the
738 * signature, not over values.
739 *
740 * @param reader the reader
741 * @param type_str the signature string
742 * @param type_pos location in the signature string
743 */
744 void
_dbus_type_reader_init_types_only(DBusTypeReader * reader,const DBusString * type_str,int type_pos)745 _dbus_type_reader_init_types_only (DBusTypeReader *reader,
746 const DBusString *type_str,
747 int type_pos)
748 {
749 reader->klass = &body_types_only_reader_class;
750
751 reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
752 type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */);
753
754 #if RECURSIVE_MARSHAL_READ_TRACE
755 _dbus_verbose (" type reader %p init types only type_pos = %d remaining sig '%s'\n",
756 reader, reader->type_pos,
757 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
758 #endif
759 }
760
761 /**
762 * Gets the type of the value the reader is currently pointing to;
763 * or for a types-only reader gets the type it's currently pointing to.
764 * If the reader is at the end of a block or end of a container such
765 * as an array, returns #DBUS_TYPE_INVALID.
766 *
767 * @param reader the reader
768 */
769 int
_dbus_type_reader_get_current_type(const DBusTypeReader * reader)770 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
771 {
772 int t;
773
774 if (reader->finished ||
775 (reader->klass->check_finished &&
776 (* reader->klass->check_finished) (reader)))
777 t = DBUS_TYPE_INVALID;
778 else
779 t = _dbus_first_type_in_signature (reader->type_str,
780 reader->type_pos);
781
782 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
783 _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
784 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
785 _dbus_assert (t != DBUS_DICT_ENTRY_BEGIN_CHAR);
786
787 #if 0
788 _dbus_verbose (" type reader %p current type_pos = %d type = %s\n",
789 reader, reader->type_pos,
790 _dbus_type_to_string (t));
791 #endif
792
793 return t;
794 }
795
796 /**
797 * Gets the type of an element of the array the reader is currently
798 * pointing to. It's an error to call this if
799 * _dbus_type_reader_get_current_type() doesn't return #DBUS_TYPE_ARRAY
800 * for this reader.
801 *
802 * @param reader the reader
803 */
804 int
_dbus_type_reader_get_element_type(const DBusTypeReader * reader)805 _dbus_type_reader_get_element_type (const DBusTypeReader *reader)
806 {
807 int element_type;
808
809 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
810
811 element_type = _dbus_first_type_in_signature (reader->type_str,
812 reader->type_pos + 1);
813
814 return element_type;
815 }
816
817 /**
818 * Gets the current position in the value block
819 * @param reader the reader
820 */
821 int
_dbus_type_reader_get_value_pos(const DBusTypeReader * reader)822 _dbus_type_reader_get_value_pos (const DBusTypeReader *reader)
823 {
824 return reader->value_pos;
825 }
826
827 /**
828 * Get the address of the marshaled value in the data being read. The
829 * address may not be aligned; you have to align it to the type of the
830 * value you want to read. Most of the demarshal routines do this for
831 * you.
832 *
833 * @param reader the reader
834 * @param value_location the address of the marshaled value
835 */
836 void
_dbus_type_reader_read_raw(const DBusTypeReader * reader,const unsigned char ** value_location)837 _dbus_type_reader_read_raw (const DBusTypeReader *reader,
838 const unsigned char **value_location)
839 {
840 _dbus_assert (!reader->klass->types_only);
841
842 *value_location = _dbus_string_get_const_data_len (reader->value_str,
843 reader->value_pos,
844 0);
845 }
846
847 /**
848 * Reads a basic-typed value, as with _dbus_marshal_read_basic().
849 *
850 * @param reader the reader
851 * @param value the address of the value
852 */
853 void
_dbus_type_reader_read_basic(const DBusTypeReader * reader,void * value)854 _dbus_type_reader_read_basic (const DBusTypeReader *reader,
855 void *value)
856 {
857 int t;
858
859 _dbus_assert (!reader->klass->types_only);
860
861 t = _dbus_type_reader_get_current_type (reader);
862
863 _dbus_marshal_read_basic (reader->value_str,
864 reader->value_pos,
865 t, value,
866 reader->byte_order,
867 NULL);
868
869
870 #if RECURSIVE_MARSHAL_READ_TRACE
871 _dbus_verbose (" type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
872 reader, reader->type_pos, reader->value_pos,
873 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
874 #endif
875 }
876
877 /**
878 * Returns the number of bytes in the array.
879 *
880 * @param reader the reader to read from
881 * @returns the number of bytes in the array
882 */
883 int
_dbus_type_reader_get_array_length(const DBusTypeReader * reader)884 _dbus_type_reader_get_array_length (const DBusTypeReader *reader)
885 {
886 _dbus_assert (!reader->klass->types_only);
887 _dbus_assert (reader->klass == &array_reader_class);
888
889 return array_reader_get_array_len (reader);
890 }
891
892 /**
893 * Reads a block of fixed-length basic values, from the current point
894 * in an array to the end of the array. Does not work for arrays of
895 * string or container types.
896 *
897 * This function returns the array in-place; it does not make a copy,
898 * and it does not swap the bytes.
899 *
900 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
901 * and the "value" argument should be a "const double**" and so on.
902 *
903 * @param reader the reader to read from
904 * @param value place to return the array values
905 * @param n_elements place to return number of array elements
906 */
907 void
_dbus_type_reader_read_fixed_multi(const DBusTypeReader * reader,void * value,int * n_elements)908 _dbus_type_reader_read_fixed_multi (const DBusTypeReader *reader,
909 void *value,
910 int *n_elements)
911 {
912 int element_type;
913 int end_pos;
914 int remaining_len;
915 int alignment;
916 int total_len;
917
918 _dbus_assert (!reader->klass->types_only);
919 _dbus_assert (reader->klass == &array_reader_class);
920
921 element_type = _dbus_first_type_in_signature (reader->type_str,
922 reader->type_pos);
923
924 _dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
925 _dbus_assert (dbus_type_is_fixed (element_type));
926
927 alignment = _dbus_type_get_alignment (element_type);
928
929 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
930
931 total_len = array_reader_get_array_len (reader);
932 end_pos = reader->u.array.start_pos + total_len;
933 remaining_len = end_pos - reader->value_pos;
934
935 #if RECURSIVE_MARSHAL_READ_TRACE
936 _dbus_verbose ("end_pos %d total_len %d remaining_len %d value_pos %d\n",
937 end_pos, total_len, remaining_len, reader->value_pos);
938 #endif
939
940 _dbus_assert (remaining_len <= total_len);
941
942 if (remaining_len == 0)
943 *(const DBusBasicValue**) value = NULL;
944 else
945 *(const DBusBasicValue**) value =
946 (void*) _dbus_string_get_const_data_len (reader->value_str,
947 reader->value_pos,
948 remaining_len);
949
950 *n_elements = remaining_len / alignment;
951 _dbus_assert ((remaining_len % alignment) == 0);
952
953 #if RECURSIVE_MARSHAL_READ_TRACE
954 _dbus_verbose (" type reader %p read fixed array type_pos = %d value_pos = %d remaining sig '%s'\n",
955 reader, reader->type_pos, reader->value_pos,
956 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
957 #endif
958 }
959
960 /**
961 * Initialize a new reader pointing to the first type and
962 * corresponding value that's a child of the current container. It's
963 * an error to call this if the current type is a non-container.
964 *
965 * Note that DBusTypeReader traverses values, not types. So if you
966 * have an empty array of array of int, you can't recurse into it. You
967 * can only recurse into each element.
968 *
969 * @param reader the reader
970 * @param sub a reader to init pointing to the first child
971 */
972 void
_dbus_type_reader_recurse(DBusTypeReader * reader,DBusTypeReader * sub)973 _dbus_type_reader_recurse (DBusTypeReader *reader,
974 DBusTypeReader *sub)
975 {
976 int t;
977
978 t = _dbus_first_type_in_signature (reader->type_str, reader->type_pos);
979
980 switch (t)
981 {
982 case DBUS_TYPE_STRUCT:
983 if (reader->klass->types_only)
984 sub->klass = &struct_types_only_reader_class;
985 else
986 sub->klass = &struct_reader_class;
987 break;
988 case DBUS_TYPE_DICT_ENTRY:
989 if (reader->klass->types_only)
990 sub->klass = &dict_entry_types_only_reader_class;
991 else
992 sub->klass = &dict_entry_reader_class;
993 break;
994 case DBUS_TYPE_ARRAY:
995 if (reader->klass->types_only)
996 sub->klass = &array_types_only_reader_class;
997 else
998 sub->klass = &array_reader_class;
999 break;
1000 case DBUS_TYPE_VARIANT:
1001 if (reader->klass->types_only)
1002 _dbus_assert_not_reached ("can't recurse into variant typecode");
1003 else
1004 sub->klass = &variant_reader_class;
1005 break;
1006 default:
1007 _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
1008 #ifndef DBUS_DISABLE_CHECKS
1009 if (t == DBUS_TYPE_INVALID)
1010 _dbus_warn_check_failed ("You can't recurse into an empty array or off the end of a message body\n");
1011 #endif /* DBUS_DISABLE_CHECKS */
1012
1013 _dbus_assert_not_reached ("don't yet handle recursing into this type");
1014 }
1015
1016 _dbus_assert (sub->klass == all_reader_classes[sub->klass->id]);
1017
1018 (* sub->klass->recurse) (sub, reader);
1019
1020 #if RECURSIVE_MARSHAL_READ_TRACE
1021 _dbus_verbose (" type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
1022 sub, sub->type_pos, sub->value_pos,
1023 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
1024 #endif
1025 }
1026
1027 /**
1028 * Skip to the next value on this "level". e.g. the next field in a
1029 * struct, the next value in an array. Returns FALSE at the end of the
1030 * current container.
1031 *
1032 * @param reader the reader
1033 * @returns FALSE if nothing more to read at or below this level
1034 */
1035 dbus_bool_t
_dbus_type_reader_next(DBusTypeReader * reader)1036 _dbus_type_reader_next (DBusTypeReader *reader)
1037 {
1038 int t;
1039
1040 t = _dbus_type_reader_get_current_type (reader);
1041
1042 #if RECURSIVE_MARSHAL_READ_TRACE
1043 _dbus_verbose (" type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1044 reader, reader->type_pos, reader->value_pos,
1045 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1046 _dbus_type_to_string (t));
1047 #endif
1048
1049 if (t == DBUS_TYPE_INVALID)
1050 return FALSE;
1051
1052 (* reader->klass->next) (reader, t);
1053
1054 #if RECURSIVE_MARSHAL_READ_TRACE
1055 _dbus_verbose (" type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1056 reader, reader->type_pos, reader->value_pos,
1057 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1058 _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
1059 #endif
1060
1061 return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
1062 }
1063
1064 /**
1065 * Check whether there's another value on this "level". e.g. the next
1066 * field in a struct, the next value in an array. Returns FALSE at the
1067 * end of the current container.
1068 *
1069 * You probably don't want to use this; it makes for an awkward for/while
1070 * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
1071 *
1072 * @param reader the reader
1073 * @returns FALSE if nothing more to read at or below this level
1074 */
1075 dbus_bool_t
_dbus_type_reader_has_next(const DBusTypeReader * reader)1076 _dbus_type_reader_has_next (const DBusTypeReader *reader)
1077 {
1078 /* Not efficient but works for now. */
1079 DBusTypeReader copy;
1080
1081 copy = *reader;
1082 return _dbus_type_reader_next (©);
1083 }
1084
1085 /**
1086 * Gets the string and range of said string containing the signature
1087 * of the current value. Essentially a more complete version of
1088 * _dbus_type_reader_get_current_type() (returns the full type
1089 * rather than only the outside of the onion).
1090 *
1091 * Note though that the first byte in a struct signature is
1092 * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
1093 * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
1094 * signature is always the same as the current type. Another
1095 * difference is that this function will still return a signature when
1096 * inside an empty array; say you recurse into empty array of int32,
1097 * the signature is "i" but the current type will always be
1098 * #DBUS_TYPE_INVALID since there are no elements to be currently
1099 * pointing to.
1100 *
1101 * @param reader the reader
1102 * @param str_p place to return the string with the type in it
1103 * @param start_p place to return start of the type
1104 * @param len_p place to return the length of the type
1105 */
1106 void
_dbus_type_reader_get_signature(const DBusTypeReader * reader,const DBusString ** str_p,int * start_p,int * len_p)1107 _dbus_type_reader_get_signature (const DBusTypeReader *reader,
1108 const DBusString **str_p,
1109 int *start_p,
1110 int *len_p)
1111 {
1112 *str_p = reader->type_str;
1113 *start_p = reader->type_pos;
1114 *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
1115 }
1116
1117 typedef struct
1118 {
1119 DBusString replacement; /**< Marshaled value including alignment padding */
1120 int padding; /**< How much of the replacement block is padding */
1121 } ReplacementBlock;
1122
1123 static dbus_bool_t
replacement_block_init(ReplacementBlock * block,DBusTypeReader * reader)1124 replacement_block_init (ReplacementBlock *block,
1125 DBusTypeReader *reader)
1126 {
1127 if (!_dbus_string_init (&block->replacement))
1128 return FALSE;
1129
1130 /* % 8 is the padding to have the same align properties in
1131 * our replacement string as we do at the position being replaced
1132 */
1133 block->padding = reader->value_pos % 8;
1134
1135 if (!_dbus_string_lengthen (&block->replacement, block->padding))
1136 goto oom;
1137
1138 return TRUE;
1139
1140 oom:
1141 _dbus_string_free (&block->replacement);
1142 return FALSE;
1143 }
1144
1145 static dbus_bool_t
replacement_block_replace(ReplacementBlock * block,DBusTypeReader * reader,const DBusTypeReader * realign_root)1146 replacement_block_replace (ReplacementBlock *block,
1147 DBusTypeReader *reader,
1148 const DBusTypeReader *realign_root)
1149 {
1150 DBusTypeWriter writer;
1151 DBusTypeReader realign_reader;
1152 DBusList *fixups;
1153 int orig_len;
1154
1155 _dbus_assert (realign_root != NULL);
1156
1157 orig_len = _dbus_string_get_length (&block->replacement);
1158
1159 realign_reader = *realign_root;
1160
1161 #if RECURSIVE_MARSHAL_WRITE_TRACE
1162 _dbus_verbose ("INITIALIZING replacement block writer %p at value_pos %d\n",
1163 &writer, _dbus_string_get_length (&block->replacement));
1164 #endif
1165 _dbus_type_writer_init_values_only (&writer,
1166 realign_reader.byte_order,
1167 realign_reader.type_str,
1168 realign_reader.type_pos,
1169 &block->replacement,
1170 _dbus_string_get_length (&block->replacement));
1171
1172 _dbus_assert (realign_reader.value_pos <= reader->value_pos);
1173
1174 #if RECURSIVE_MARSHAL_WRITE_TRACE
1175 _dbus_verbose ("COPYING from reader at value_pos %d to writer %p starting after value_pos %d\n",
1176 realign_reader.value_pos, &writer, reader->value_pos);
1177 #endif
1178 fixups = NULL;
1179 if (!_dbus_type_writer_write_reader_partial (&writer,
1180 &realign_reader,
1181 reader,
1182 block->padding,
1183 _dbus_string_get_length (&block->replacement) - block->padding,
1184 &fixups))
1185 goto oom;
1186
1187 #if RECURSIVE_MARSHAL_WRITE_TRACE
1188 _dbus_verbose ("REPLACEMENT at padding %d len %d\n", block->padding,
1189 _dbus_string_get_length (&block->replacement) - block->padding);
1190 _dbus_verbose_bytes_of_string (&block->replacement, block->padding,
1191 _dbus_string_get_length (&block->replacement) - block->padding);
1192 _dbus_verbose ("TO BE REPLACED at value_pos = %d (align pad %d) len %d realign_reader.value_pos %d\n",
1193 reader->value_pos, reader->value_pos % 8,
1194 realign_reader.value_pos - reader->value_pos,
1195 realign_reader.value_pos);
1196 _dbus_verbose_bytes_of_string (reader->value_str,
1197 reader->value_pos,
1198 realign_reader.value_pos - reader->value_pos);
1199 #endif
1200
1201 /* Move the replacement into position
1202 * (realign_reader should now be at the end of the block to be replaced)
1203 */
1204 if (!_dbus_string_replace_len (&block->replacement, block->padding,
1205 _dbus_string_get_length (&block->replacement) - block->padding,
1206 (DBusString*) reader->value_str,
1207 reader->value_pos,
1208 realign_reader.value_pos - reader->value_pos))
1209 goto oom;
1210
1211 /* Process our fixups now that we can't have an OOM error */
1212 apply_and_free_fixups (&fixups, reader);
1213
1214 return TRUE;
1215
1216 oom:
1217 _dbus_string_set_length (&block->replacement, orig_len);
1218 free_fixups (&fixups);
1219 return FALSE;
1220 }
1221
1222 static void
replacement_block_free(ReplacementBlock * block)1223 replacement_block_free (ReplacementBlock *block)
1224 {
1225 _dbus_string_free (&block->replacement);
1226 }
1227
1228 /* In the variable-length case, we have to fix alignment after we insert.
1229 * The strategy is as follows:
1230 *
1231 * - pad a new string to have the same alignment as the
1232 * start of the current basic value
1233 * - write the new basic value
1234 * - copy from the original reader to the new string,
1235 * which will fix the alignment of types following
1236 * the new value
1237 * - this copy has to start at realign_root,
1238 * but not really write anything until it
1239 * passes the value being set
1240 * - as an optimization, we can stop copying
1241 * when the source and dest values are both
1242 * on an 8-boundary, since we know all following
1243 * padding and alignment will be identical
1244 * - copy the new string back to the original
1245 * string, replacing the relevant part of the
1246 * original string
1247 * - now any arrays in the original string that
1248 * contained the replaced string may have the
1249 * wrong length; so we have to fix that
1250 */
1251 static dbus_bool_t
reader_set_basic_variable_length(DBusTypeReader * reader,int current_type,const void * value,const DBusTypeReader * realign_root)1252 reader_set_basic_variable_length (DBusTypeReader *reader,
1253 int current_type,
1254 const void *value,
1255 const DBusTypeReader *realign_root)
1256 {
1257 dbus_bool_t retval;
1258 ReplacementBlock block;
1259 DBusTypeWriter writer;
1260
1261 _dbus_assert (realign_root != NULL);
1262
1263 retval = FALSE;
1264
1265 if (!replacement_block_init (&block, reader))
1266 return FALSE;
1267
1268 /* Write the new basic value */
1269 #if RECURSIVE_MARSHAL_WRITE_TRACE
1270 _dbus_verbose ("INITIALIZING writer %p to write basic value at value_pos %d of replacement string\n",
1271 &writer, _dbus_string_get_length (&block.replacement));
1272 #endif
1273 _dbus_type_writer_init_values_only (&writer,
1274 reader->byte_order,
1275 reader->type_str,
1276 reader->type_pos,
1277 &block.replacement,
1278 _dbus_string_get_length (&block.replacement));
1279 #if RECURSIVE_MARSHAL_WRITE_TRACE
1280 _dbus_verbose ("WRITING basic value to writer %p (replacement string)\n", &writer);
1281 #endif
1282 if (!_dbus_type_writer_write_basic (&writer, current_type, value))
1283 goto out;
1284
1285 if (!replacement_block_replace (&block,
1286 reader,
1287 realign_root))
1288 goto out;
1289
1290 retval = TRUE;
1291
1292 out:
1293 replacement_block_free (&block);
1294 return retval;
1295 }
1296
1297 static void
reader_set_basic_fixed_length(DBusTypeReader * reader,int current_type,const void * value)1298 reader_set_basic_fixed_length (DBusTypeReader *reader,
1299 int current_type,
1300 const void *value)
1301 {
1302 _dbus_marshal_set_basic ((DBusString*) reader->value_str,
1303 reader->value_pos,
1304 current_type,
1305 value,
1306 reader->byte_order,
1307 NULL, NULL);
1308 }
1309
1310 /**
1311 * Sets a new value for the basic type value pointed to by the reader,
1312 * leaving the reader valid to continue reading. Any other readers
1313 * will be invalidated if you set a variable-length type such as a
1314 * string.
1315 *
1316 * The provided realign_root is the reader to start from when
1317 * realigning the data that follows the newly-set value. The reader
1318 * parameter must point to a value below the realign_root parameter.
1319 * If the type being set is fixed-length, then realign_root may be
1320 * #NULL. Only values reachable from realign_root will be realigned,
1321 * so if your string contains other values you will need to deal with
1322 * those somehow yourself. It is OK if realign_root is the same
1323 * reader as the reader parameter, though if you aren't setting the
1324 * root it may not be such a good idea.
1325 *
1326 * @todo DBusTypeReader currently takes "const" versions of the type
1327 * and value strings, and this function modifies those strings by
1328 * casting away the const, which is of course bad if we want to get
1329 * picky. (To be truly clean you'd have an object which contained the
1330 * type and value strings and set_basic would be a method on that
1331 * object... this would also make DBusTypeReader the same thing as
1332 * DBusTypeMark. But since DBusMessage is effectively that object for
1333 * D-Bus it doesn't seem worth creating some random object.)
1334 *
1335 * @todo optimize this by only rewriting until the old and new values
1336 * are at the same alignment. Frequently this should result in only
1337 * replacing the value that's immediately at hand.
1338 *
1339 * @param reader reader indicating where to set a new value
1340 * @param value address of the value to set
1341 * @param realign_root realign from here
1342 * @returns #FALSE if not enough memory
1343 */
1344 dbus_bool_t
_dbus_type_reader_set_basic(DBusTypeReader * reader,const void * value,const DBusTypeReader * realign_root)1345 _dbus_type_reader_set_basic (DBusTypeReader *reader,
1346 const void *value,
1347 const DBusTypeReader *realign_root)
1348 {
1349 int current_type;
1350
1351 _dbus_assert (!reader->klass->types_only);
1352 _dbus_assert (reader->value_str == realign_root->value_str);
1353 _dbus_assert (reader->value_pos >= realign_root->value_pos);
1354
1355 current_type = _dbus_type_reader_get_current_type (reader);
1356
1357 #if RECURSIVE_MARSHAL_WRITE_TRACE
1358 _dbus_verbose (" SET BASIC type reader %p type_pos = %d value_pos = %d remaining sig '%s' realign_root = %p with value_pos %d current_type = %s\n",
1359 reader, reader->type_pos, reader->value_pos,
1360 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1361 realign_root,
1362 realign_root ? realign_root->value_pos : -1,
1363 _dbus_type_to_string (current_type));
1364 _dbus_verbose_bytes_of_string (realign_root->value_str, realign_root->value_pos,
1365 _dbus_string_get_length (realign_root->value_str) -
1366 realign_root->value_pos);
1367 #endif
1368
1369 _dbus_assert (dbus_type_is_basic (current_type));
1370
1371 if (dbus_type_is_fixed (current_type))
1372 {
1373 reader_set_basic_fixed_length (reader, current_type, value);
1374 return TRUE;
1375 }
1376 else
1377 {
1378 _dbus_assert (realign_root != NULL);
1379 return reader_set_basic_variable_length (reader, current_type,
1380 value, realign_root);
1381 }
1382 }
1383
1384 /**
1385 * Recursively deletes any value pointed to by the reader, leaving the
1386 * reader valid to continue reading. Any other readers will be
1387 * invalidated.
1388 *
1389 * The provided realign_root is the reader to start from when
1390 * realigning the data that follows the newly-set value.
1391 * See _dbus_type_reader_set_basic() for more details on the
1392 * realign_root paramter.
1393 *
1394 * @todo for now this does not delete the typecodes associated with
1395 * the value, so this function should only be used for array elements.
1396 *
1397 * @param reader reader indicating where to delete a value
1398 * @param realign_root realign from here
1399 * @returns #FALSE if not enough memory
1400 */
1401 dbus_bool_t
_dbus_type_reader_delete(DBusTypeReader * reader,const DBusTypeReader * realign_root)1402 _dbus_type_reader_delete (DBusTypeReader *reader,
1403 const DBusTypeReader *realign_root)
1404 {
1405 dbus_bool_t retval;
1406 ReplacementBlock block;
1407
1408 _dbus_assert (realign_root != NULL);
1409 _dbus_assert (reader->klass == &array_reader_class);
1410
1411 retval = FALSE;
1412
1413 if (!replacement_block_init (&block, reader))
1414 return FALSE;
1415
1416 if (!replacement_block_replace (&block,
1417 reader,
1418 realign_root))
1419 goto out;
1420
1421 retval = TRUE;
1422
1423 out:
1424 replacement_block_free (&block);
1425 return retval;
1426 }
1427
1428 /**
1429 * Compares two readers, which must be iterating over the same value data.
1430 * Returns #TRUE if the first parameter is further along than the second parameter.
1431 *
1432 * @param lhs left-hand-side (first) parameter
1433 * @param rhs left-hand-side (first) parameter
1434 * @returns whether lhs is greater than rhs
1435 */
1436 dbus_bool_t
_dbus_type_reader_greater_than(const DBusTypeReader * lhs,const DBusTypeReader * rhs)1437 _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
1438 const DBusTypeReader *rhs)
1439 {
1440 _dbus_assert (lhs->value_str == rhs->value_str);
1441
1442 return lhs->value_pos > rhs->value_pos;
1443 }
1444
1445 /*
1446 *
1447 *
1448 * DBusTypeWriter
1449 *
1450 *
1451 *
1452 */
1453
1454 /**
1455 * Initialize a write iterator, which is used to write out values in
1456 * serialized D-Bus format.
1457 *
1458 * The type_pos passed in is expected to be inside an already-valid,
1459 * though potentially empty, type signature. This means that the byte
1460 * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
1461 * other valid type. #DBusTypeWriter won't enforce that the signature
1462 * is already valid (you can append the nul byte at the end if you
1463 * like), but just be aware that you need the nul byte eventually and
1464 * #DBusTypeWriter isn't going to write it for you.
1465 *
1466 * @param writer the writer to init
1467 * @param byte_order the byte order to marshal into
1468 * @param type_str the string to write typecodes into
1469 * @param type_pos where to insert typecodes
1470 * @param value_str the string to write values into
1471 * @param value_pos where to insert values
1472 *
1473 */
1474 void
_dbus_type_writer_init(DBusTypeWriter * writer,int byte_order,DBusString * type_str,int type_pos,DBusString * value_str,int value_pos)1475 _dbus_type_writer_init (DBusTypeWriter *writer,
1476 int byte_order,
1477 DBusString *type_str,
1478 int type_pos,
1479 DBusString *value_str,
1480 int value_pos)
1481 {
1482 writer->byte_order = byte_order;
1483 writer->type_str = type_str;
1484 writer->type_pos = type_pos;
1485 writer->value_str = value_str;
1486 writer->value_pos = value_pos;
1487 writer->container_type = DBUS_TYPE_INVALID;
1488 writer->type_pos_is_expectation = FALSE;
1489 writer->enabled = TRUE;
1490
1491 #if RECURSIVE_MARSHAL_WRITE_TRACE
1492 _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1493 writer->type_str ?
1494 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1495 "unknown");
1496 #endif
1497 }
1498
1499 /**
1500 * Initialize a write iterator, with the signature to be provided
1501 * later.
1502 *
1503 * @param writer the writer to init
1504 * @param byte_order the byte order to marshal into
1505 * @param value_str the string to write values into
1506 * @param value_pos where to insert values
1507 *
1508 */
1509 void
_dbus_type_writer_init_types_delayed(DBusTypeWriter * writer,int byte_order,DBusString * value_str,int value_pos)1510 _dbus_type_writer_init_types_delayed (DBusTypeWriter *writer,
1511 int byte_order,
1512 DBusString *value_str,
1513 int value_pos)
1514 {
1515 _dbus_type_writer_init (writer, byte_order,
1516 NULL, 0, value_str, value_pos);
1517 }
1518
1519 /**
1520 * Adds type string to the writer, if it had none.
1521 *
1522 * @param writer the writer to init
1523 * @param type_str type string to add
1524 * @param type_pos type position
1525 *
1526 */
1527 void
_dbus_type_writer_add_types(DBusTypeWriter * writer,DBusString * type_str,int type_pos)1528 _dbus_type_writer_add_types (DBusTypeWriter *writer,
1529 DBusString *type_str,
1530 int type_pos)
1531 {
1532 if (writer->type_str == NULL) /* keeps us from using this as setter */
1533 {
1534 writer->type_str = type_str;
1535 writer->type_pos = type_pos;
1536 }
1537 }
1538
1539 /**
1540 * Removes type string from the writer.
1541 *
1542 * @param writer the writer to remove from
1543 */
1544 void
_dbus_type_writer_remove_types(DBusTypeWriter * writer)1545 _dbus_type_writer_remove_types (DBusTypeWriter *writer)
1546 {
1547 writer->type_str = NULL;
1548 writer->type_pos = -1;
1549 }
1550
1551 /**
1552 * Like _dbus_type_writer_init(), except the type string
1553 * passed in should correspond to an existing signature that
1554 * matches what you're going to write out. The writer will
1555 * check what you write vs. this existing signature.
1556 *
1557 * @param writer the writer to init
1558 * @param byte_order the byte order to marshal into
1559 * @param type_str the string with signature
1560 * @param type_pos start of signature
1561 * @param value_str the string to write values into
1562 * @param value_pos where to insert values
1563 *
1564 */
1565 void
_dbus_type_writer_init_values_only(DBusTypeWriter * writer,int byte_order,const DBusString * type_str,int type_pos,DBusString * value_str,int value_pos)1566 _dbus_type_writer_init_values_only (DBusTypeWriter *writer,
1567 int byte_order,
1568 const DBusString *type_str,
1569 int type_pos,
1570 DBusString *value_str,
1571 int value_pos)
1572 {
1573 _dbus_type_writer_init (writer, byte_order,
1574 (DBusString*)type_str, type_pos,
1575 value_str, value_pos);
1576
1577 writer->type_pos_is_expectation = TRUE;
1578 }
1579
1580 static dbus_bool_t
_dbus_type_writer_write_basic_no_typecode(DBusTypeWriter * writer,int type,const void * value)1581 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1582 int type,
1583 const void *value)
1584 {
1585 if (writer->enabled)
1586 return _dbus_marshal_write_basic (writer->value_str,
1587 writer->value_pos,
1588 type,
1589 value,
1590 writer->byte_order,
1591 &writer->value_pos);
1592 else
1593 return TRUE;
1594 }
1595
1596 /* If our parent is an array, things are a little bit complicated.
1597 *
1598 * The parent must have a complete element type, such as
1599 * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1600 * unclosed parens, or an "a" with no following type.
1601 *
1602 * To recurse, the only allowed operation is to recurse into the
1603 * first type in the element type. So for "i" you can't recurse, for
1604 * "ai" you can recurse into the array, for "(ii)" you can recurse
1605 * into the struct.
1606 *
1607 * If you recurse into the array for "ai", then you must specify
1608 * "i" for the element type of the array you recurse into.
1609 *
1610 * While inside an array at any level, we need to avoid writing to
1611 * type_str, since the type only appears once for the whole array,
1612 * it does not appear for each array element.
1613 *
1614 * While inside an array type_pos points to the expected next
1615 * typecode, rather than the next place we could write a typecode.
1616 */
1617 static void
writer_recurse_init_and_check(DBusTypeWriter * writer,int container_type,DBusTypeWriter * sub)1618 writer_recurse_init_and_check (DBusTypeWriter *writer,
1619 int container_type,
1620 DBusTypeWriter *sub)
1621 {
1622 _dbus_type_writer_init (sub,
1623 writer->byte_order,
1624 writer->type_str,
1625 writer->type_pos,
1626 writer->value_str,
1627 writer->value_pos);
1628
1629 sub->container_type = container_type;
1630
1631 if (writer->type_pos_is_expectation ||
1632 (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1633 sub->type_pos_is_expectation = TRUE;
1634 else
1635 sub->type_pos_is_expectation = FALSE;
1636
1637 sub->enabled = writer->enabled;
1638
1639 #ifndef DBUS_DISABLE_CHECKS
1640 if (writer->type_pos_is_expectation && writer->type_str)
1641 {
1642 int expected;
1643
1644 expected = _dbus_first_type_in_signature (writer->type_str, writer->type_pos);
1645
1646 if (expected != sub->container_type)
1647 {
1648 _dbus_warn_check_failed ("Writing an element of type %s, but the expected type here is %s\n",
1649 _dbus_type_to_string (sub->container_type),
1650 _dbus_type_to_string (expected));
1651 _dbus_assert_not_reached ("bad array element or variant content written");
1652 }
1653 }
1654 #endif /* DBUS_DISABLE_CHECKS */
1655
1656 #if RECURSIVE_MARSHAL_WRITE_TRACE
1657 _dbus_verbose (" type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s' enabled = %d\n",
1658 writer,
1659 _dbus_type_to_string (writer->container_type),
1660 writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1661 writer->type_str ?
1662 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1663 "unknown",
1664 writer->enabled);
1665 _dbus_verbose (" type writer %p recurse sub %s type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
1666 sub,
1667 _dbus_type_to_string (sub->container_type),
1668 sub->type_pos, sub->value_pos,
1669 sub->type_pos_is_expectation,
1670 sub->enabled);
1671 #endif
1672 }
1673
1674 static dbus_bool_t
write_or_verify_typecode(DBusTypeWriter * writer,int typecode)1675 write_or_verify_typecode (DBusTypeWriter *writer,
1676 int typecode)
1677 {
1678 /* A subwriter inside an array or variant will have type_pos
1679 * pointing to the expected typecode; a writer not inside an array
1680 * or variant has type_pos pointing to the next place to insert a
1681 * typecode.
1682 */
1683 #if RECURSIVE_MARSHAL_WRITE_TRACE
1684 _dbus_verbose (" type writer %p write_or_verify start type_pos = %d remaining sig '%s' enabled = %d\n",
1685 writer, writer->type_pos,
1686 writer->type_str ?
1687 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1688 "unknown",
1689 writer->enabled);
1690 #endif
1691
1692 if (writer->type_str == NULL)
1693 return TRUE;
1694
1695 if (writer->type_pos_is_expectation)
1696 {
1697 #ifndef DBUS_DISABLE_CHECKS
1698 {
1699 int expected;
1700
1701 expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1702
1703 if (expected != typecode)
1704 {
1705 _dbus_warn_check_failed ("Array or variant type requires that type %s be written, but %s was written\n",
1706 _dbus_type_to_string (expected), _dbus_type_to_string (typecode));
1707 _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1708 }
1709 }
1710 #endif /* DBUS_DISABLE_CHECKS */
1711
1712 /* if immediately inside an array we'd always be appending an element,
1713 * so the expected type doesn't change; if inside a struct or something
1714 * below an array, we need to move through said struct or something.
1715 */
1716 if (writer->container_type != DBUS_TYPE_ARRAY)
1717 writer->type_pos += 1;
1718 }
1719 else
1720 {
1721 if (!_dbus_string_insert_byte (writer->type_str,
1722 writer->type_pos,
1723 typecode))
1724 return FALSE;
1725
1726 writer->type_pos += 1;
1727 }
1728
1729 #if RECURSIVE_MARSHAL_WRITE_TRACE
1730 _dbus_verbose (" type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1731 writer, writer->type_pos,
1732 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1733 #endif
1734
1735 return TRUE;
1736 }
1737
1738 static dbus_bool_t
writer_recurse_struct_or_dict_entry(DBusTypeWriter * writer,int begin_char,const DBusString * contained_type,int contained_type_start,int contained_type_len,DBusTypeWriter * sub)1739 writer_recurse_struct_or_dict_entry (DBusTypeWriter *writer,
1740 int begin_char,
1741 const DBusString *contained_type,
1742 int contained_type_start,
1743 int contained_type_len,
1744 DBusTypeWriter *sub)
1745 {
1746 /* FIXME right now contained_type is ignored; we could probably
1747 * almost trivially fix the code so if it's present we
1748 * write it out and then set type_pos_is_expectation
1749 */
1750
1751 /* Ensure that we'll be able to add alignment padding and the typecode */
1752 if (writer->enabled)
1753 {
1754 if (!_dbus_string_alloc_space (sub->value_str, 8))
1755 return FALSE;
1756 }
1757
1758 if (!write_or_verify_typecode (sub, begin_char))
1759 _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
1760
1761 if (writer->enabled)
1762 {
1763 if (!_dbus_string_insert_bytes (sub->value_str,
1764 sub->value_pos,
1765 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1766 '\0'))
1767 _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
1768 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1769 }
1770
1771 return TRUE;
1772 }
1773
1774
1775 static dbus_bool_t
writer_recurse_array(DBusTypeWriter * writer,const DBusString * contained_type,int contained_type_start,int contained_type_len,DBusTypeWriter * sub,dbus_bool_t is_array_append)1776 writer_recurse_array (DBusTypeWriter *writer,
1777 const DBusString *contained_type,
1778 int contained_type_start,
1779 int contained_type_len,
1780 DBusTypeWriter *sub,
1781 dbus_bool_t is_array_append)
1782 {
1783 dbus_uint32_t value = 0;
1784 int alignment;
1785 int aligned;
1786
1787 #ifndef DBUS_DISABLE_CHECKS
1788 if (writer->container_type == DBUS_TYPE_ARRAY &&
1789 writer->type_str)
1790 {
1791 if (!_dbus_string_equal_substring (contained_type,
1792 contained_type_start,
1793 contained_type_len,
1794 writer->type_str,
1795 writer->u.array.element_type_pos + 1))
1796 {
1797 _dbus_warn_check_failed ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
1798 _dbus_string_get_const_data_len (contained_type,
1799 contained_type_start,
1800 contained_type_len));
1801 _dbus_assert_not_reached ("incompatible type for child array");
1802 }
1803 }
1804 #endif /* DBUS_DISABLE_CHECKS */
1805
1806 if (writer->enabled && !is_array_append)
1807 {
1808 /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
1809 * before array values
1810 */
1811 if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
1812 return FALSE;
1813 }
1814
1815 if (writer->type_str != NULL)
1816 {
1817 sub->type_pos += 1; /* move to point to the element type, since type_pos
1818 * should be the expected type for further writes
1819 */
1820 sub->u.array.element_type_pos = sub->type_pos;
1821 }
1822
1823 if (!writer->type_pos_is_expectation)
1824 {
1825 /* sub is a toplevel/outermost array so we need to write the type data */
1826
1827 /* alloc space for array typecode, element signature */
1828 if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
1829 return FALSE;
1830
1831 if (!_dbus_string_insert_byte (writer->type_str,
1832 writer->type_pos,
1833 DBUS_TYPE_ARRAY))
1834 _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
1835
1836 if (!_dbus_string_copy_len (contained_type,
1837 contained_type_start, contained_type_len,
1838 sub->type_str,
1839 sub->u.array.element_type_pos))
1840 _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
1841 }
1842
1843 if (writer->type_str != NULL)
1844 {
1845 /* If the parent is an array, we hold type_pos pointing at the array element type;
1846 * otherwise advance it to reflect the array value we just recursed into
1847 */
1848 if (writer->container_type != DBUS_TYPE_ARRAY)
1849 writer->type_pos += 1 + contained_type_len;
1850 else
1851 _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
1852 }
1853
1854 if (writer->enabled)
1855 {
1856 /* Write (or jump over, if is_array_append) the length */
1857 sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
1858
1859 if (is_array_append)
1860 {
1861 sub->value_pos += 4;
1862 }
1863 else
1864 {
1865 if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32,
1866 &value))
1867 _dbus_assert_not_reached ("should not have failed to insert array len");
1868 }
1869
1870 _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
1871
1872 /* Write alignment padding for array elements
1873 * Note that we write the padding *even for empty arrays*
1874 * to avoid wonky special cases
1875 */
1876 alignment = element_type_get_alignment (contained_type, contained_type_start);
1877
1878 aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
1879 if (aligned != sub->value_pos)
1880 {
1881 if (!is_array_append)
1882 {
1883 if (!_dbus_string_insert_bytes (sub->value_str,
1884 sub->value_pos,
1885 aligned - sub->value_pos,
1886 '\0'))
1887 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
1888 }
1889
1890 sub->value_pos = aligned;
1891 }
1892
1893 sub->u.array.start_pos = sub->value_pos;
1894
1895 if (is_array_append)
1896 {
1897 dbus_uint32_t len;
1898
1899 _dbus_assert (_DBUS_ALIGN_VALUE (sub->u.array.len_pos, 4) ==
1900 (unsigned) sub->u.array.len_pos);
1901 len = _dbus_unpack_uint32 (sub->byte_order,
1902 _dbus_string_get_const_data_len (sub->value_str,
1903 sub->u.array.len_pos,
1904 4));
1905
1906 sub->value_pos += len;
1907 }
1908 }
1909 else
1910 {
1911 /* not enabled, so we won't write the len_pos; set it to -1 to so indicate */
1912 sub->u.array.len_pos = -1;
1913 sub->u.array.start_pos = sub->value_pos;
1914 }
1915
1916 _dbus_assert (sub->u.array.len_pos < sub->u.array.start_pos);
1917 _dbus_assert (is_array_append || sub->u.array.start_pos == sub->value_pos);
1918
1919 #if RECURSIVE_MARSHAL_WRITE_TRACE
1920 _dbus_verbose (" type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d value_pos = %d\n", sub,
1921 sub->type_str ?
1922 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0) :
1923 "unknown",
1924 sub->u.array.start_pos, sub->u.array.len_pos, sub->value_pos);
1925 #endif
1926
1927 return TRUE;
1928 }
1929
1930 /* Variant value will normally have:
1931 * 1 byte signature length not including nul
1932 * signature typecodes (nul terminated)
1933 * padding to alignment of contained type
1934 * body according to signature
1935 *
1936 * The signature string can only have a single type
1937 * in it but that type may be complex/recursive.
1938 *
1939 * So a typical variant type with the integer 3 will have these
1940 * octets:
1941 * 0x1 'i' '\0' [1 byte padding to alignment boundary] 0x0 0x0 0x0 0x3
1942 *
1943 * The main world of hurt for writing out a variant is that the type
1944 * string is the same string as the value string. Which means
1945 * inserting to the type string will move the value_pos; and it means
1946 * that inserting to the type string could break type alignment.
1947 */
1948 static dbus_bool_t
writer_recurse_variant(DBusTypeWriter * writer,const DBusString * contained_type,int contained_type_start,int contained_type_len,DBusTypeWriter * sub)1949 writer_recurse_variant (DBusTypeWriter *writer,
1950 const DBusString *contained_type,
1951 int contained_type_start,
1952 int contained_type_len,
1953 DBusTypeWriter *sub)
1954 {
1955 int contained_alignment;
1956
1957 if (writer->enabled)
1958 {
1959 /* Allocate space for the worst case, which is 1 byte sig
1960 * length, nul byte at end of sig, and 7 bytes padding to
1961 * 8-boundary.
1962 */
1963 if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
1964 return FALSE;
1965 }
1966
1967 /* write VARIANT typecode to the parent's type string */
1968 if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
1969 return FALSE;
1970
1971 /* If not enabled, mark that we have no type_str anymore ... */
1972
1973 if (!writer->enabled)
1974 {
1975 sub->type_str = NULL;
1976 sub->type_pos = -1;
1977
1978 return TRUE;
1979 }
1980
1981 /* If we're enabled then continue ... */
1982
1983 if (!_dbus_string_insert_byte (sub->value_str,
1984 sub->value_pos,
1985 contained_type_len))
1986 _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
1987
1988 sub->value_pos += 1;
1989
1990 /* Here we switch over to the expected type sig we're about to write */
1991 sub->type_str = sub->value_str;
1992 sub->type_pos = sub->value_pos;
1993
1994 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
1995 sub->value_str, sub->value_pos))
1996 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
1997
1998 sub->value_pos += contained_type_len;
1999
2000 if (!_dbus_string_insert_byte (sub->value_str,
2001 sub->value_pos,
2002 DBUS_TYPE_INVALID))
2003 _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
2004
2005 sub->value_pos += 1;
2006
2007 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (contained_type, contained_type_start));
2008
2009 if (!_dbus_string_insert_bytes (sub->value_str,
2010 sub->value_pos,
2011 _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment) - sub->value_pos,
2012 '\0'))
2013 _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
2014 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
2015
2016 return TRUE;
2017 }
2018
2019 static dbus_bool_t
_dbus_type_writer_recurse_contained_len(DBusTypeWriter * writer,int container_type,const DBusString * contained_type,int contained_type_start,int contained_type_len,DBusTypeWriter * sub,dbus_bool_t is_array_append)2020 _dbus_type_writer_recurse_contained_len (DBusTypeWriter *writer,
2021 int container_type,
2022 const DBusString *contained_type,
2023 int contained_type_start,
2024 int contained_type_len,
2025 DBusTypeWriter *sub,
2026 dbus_bool_t is_array_append)
2027 {
2028 writer_recurse_init_and_check (writer, container_type, sub);
2029
2030 switch (container_type)
2031 {
2032 case DBUS_TYPE_STRUCT:
2033 return writer_recurse_struct_or_dict_entry (writer,
2034 DBUS_STRUCT_BEGIN_CHAR,
2035 contained_type,
2036 contained_type_start, contained_type_len,
2037 sub);
2038 break;
2039 case DBUS_TYPE_DICT_ENTRY:
2040 return writer_recurse_struct_or_dict_entry (writer,
2041 DBUS_DICT_ENTRY_BEGIN_CHAR,
2042 contained_type,
2043 contained_type_start, contained_type_len,
2044 sub);
2045 break;
2046 case DBUS_TYPE_ARRAY:
2047 return writer_recurse_array (writer,
2048 contained_type, contained_type_start, contained_type_len,
2049 sub, is_array_append);
2050 break;
2051 case DBUS_TYPE_VARIANT:
2052 return writer_recurse_variant (writer,
2053 contained_type, contained_type_start, contained_type_len,
2054 sub);
2055 break;
2056 default:
2057 _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
2058 return FALSE;
2059 break;
2060 }
2061 }
2062
2063 /**
2064 * Opens a new container and writes out the initial information for that container.
2065 *
2066 * @param writer the writer
2067 * @param container_type the type of the container to open
2068 * @param contained_type the array element type or variant content type
2069 * @param contained_type_start position to look for the type
2070 * @param sub the new sub-writer to write container contents
2071 * @returns #FALSE if no memory
2072 */
2073 dbus_bool_t
_dbus_type_writer_recurse(DBusTypeWriter * writer,int container_type,const DBusString * contained_type,int contained_type_start,DBusTypeWriter * sub)2074 _dbus_type_writer_recurse (DBusTypeWriter *writer,
2075 int container_type,
2076 const DBusString *contained_type,
2077 int contained_type_start,
2078 DBusTypeWriter *sub)
2079 {
2080 int contained_type_len;
2081
2082 if (contained_type)
2083 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2084 else
2085 contained_type_len = 0;
2086
2087 return _dbus_type_writer_recurse_contained_len (writer, container_type,
2088 contained_type,
2089 contained_type_start,
2090 contained_type_len,
2091 sub,
2092 FALSE);
2093 }
2094
2095 /**
2096 * Append to an existing array. Essentially, the writer will read an
2097 * existing length at the write location; jump over that length; and
2098 * write new fields. On unrecurse(), the existing length will be
2099 * updated.
2100 *
2101 * @param writer the writer
2102 * @param contained_type element type
2103 * @param contained_type_start position of element type
2104 * @param sub the subwriter to init
2105 * @returns #FALSE if no memory
2106 */
2107 dbus_bool_t
_dbus_type_writer_append_array(DBusTypeWriter * writer,const DBusString * contained_type,int contained_type_start,DBusTypeWriter * sub)2108 _dbus_type_writer_append_array (DBusTypeWriter *writer,
2109 const DBusString *contained_type,
2110 int contained_type_start,
2111 DBusTypeWriter *sub)
2112 {
2113 int contained_type_len;
2114
2115 if (contained_type)
2116 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2117 else
2118 contained_type_len = 0;
2119
2120 return _dbus_type_writer_recurse_contained_len (writer, DBUS_TYPE_ARRAY,
2121 contained_type,
2122 contained_type_start,
2123 contained_type_len,
2124 sub,
2125 TRUE);
2126 }
2127
2128 static int
writer_get_array_len(DBusTypeWriter * writer)2129 writer_get_array_len (DBusTypeWriter *writer)
2130 {
2131 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2132 return writer->value_pos - writer->u.array.start_pos;
2133 }
2134
2135 /**
2136 * Closes a container created by _dbus_type_writer_recurse()
2137 * and writes any additional information to the values block.
2138 *
2139 * @param writer the writer
2140 * @param sub the sub-writer created by _dbus_type_writer_recurse()
2141 * @returns #FALSE if no memory
2142 */
2143 dbus_bool_t
_dbus_type_writer_unrecurse(DBusTypeWriter * writer,DBusTypeWriter * sub)2144 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
2145 DBusTypeWriter *sub)
2146 {
2147 /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
2148 _dbus_assert (!writer->type_pos_is_expectation ||
2149 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
2150
2151 #if RECURSIVE_MARSHAL_WRITE_TRACE
2152 _dbus_verbose (" type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2153 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2154 _dbus_type_to_string (writer->container_type));
2155 _dbus_verbose (" type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2156 sub, sub->type_pos, sub->value_pos,
2157 sub->type_pos_is_expectation,
2158 _dbus_type_to_string (sub->container_type));
2159 #endif
2160
2161 if (sub->container_type == DBUS_TYPE_STRUCT)
2162 {
2163 if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
2164 return FALSE;
2165 }
2166 else if (sub->container_type == DBUS_TYPE_DICT_ENTRY)
2167 {
2168 if (!write_or_verify_typecode (sub, DBUS_DICT_ENTRY_END_CHAR))
2169 return FALSE;
2170 }
2171 else if (sub->container_type == DBUS_TYPE_ARRAY)
2172 {
2173 if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */
2174 {
2175 dbus_uint32_t len;
2176
2177 /* Set the array length */
2178 len = writer_get_array_len (sub);
2179 _dbus_marshal_set_uint32 (sub->value_str,
2180 sub->u.array.len_pos,
2181 len,
2182 sub->byte_order);
2183 #if RECURSIVE_MARSHAL_WRITE_TRACE
2184 _dbus_verbose (" filled in sub array len to %u at len_pos %d\n",
2185 len, sub->u.array.len_pos);
2186 #endif
2187 }
2188 #if RECURSIVE_MARSHAL_WRITE_TRACE
2189 else
2190 {
2191 _dbus_verbose (" not filling in sub array len because we were disabled when we passed the len\n");
2192 }
2193 #endif
2194 }
2195
2196 /* Now get type_pos right for the parent writer. Here are the cases:
2197 *
2198 * Cases !writer->type_pos_is_expectation:
2199 * (in these cases we want to update to the new insertion point)
2200 *
2201 * - if we recursed into a STRUCT then we didn't know in advance
2202 * what the types in the struct would be; so we have to fill in
2203 * that information now.
2204 * writer->type_pos = sub->type_pos
2205 *
2206 * - if we recursed into anything else, we knew the full array
2207 * type, or knew the single typecode marking VARIANT, so
2208 * writer->type_pos is already correct.
2209 * writer->type_pos should remain as-is
2210 *
2211 * - note that the parent is never an ARRAY or VARIANT, if it were
2212 * then type_pos_is_expectation would be TRUE. The parent
2213 * is thus known to be a toplevel or STRUCT.
2214 *
2215 * Cases where writer->type_pos_is_expectation:
2216 * (in these cases we want to update to next expected type to write)
2217 *
2218 * - we recursed from STRUCT into STRUCT and we didn't increment
2219 * type_pos in the parent just to stay consistent with the
2220 * !writer->type_pos_is_expectation case (though we could
2221 * special-case this in recurse_struct instead if we wanted)
2222 * writer->type_pos = sub->type_pos
2223 *
2224 * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
2225 * for parent should have been incremented already
2226 * writer->type_pos should remain as-is
2227 *
2228 * - we recursed from ARRAY into a sub-element, so type_pos in the
2229 * parent is the element type and should remain the element type
2230 * for the benefit of the next child element
2231 * writer->type_pos should remain as-is
2232 *
2233 * - we recursed from VARIANT into its value, so type_pos in the
2234 * parent makes no difference since there's only one value
2235 * and we just finished writing it and won't use type_pos again
2236 * writer->type_pos should remain as-is
2237 *
2238 *
2239 * For all these, DICT_ENTRY is the same as STRUCT
2240 */
2241 if (writer->type_str != NULL)
2242 {
2243 if ((sub->container_type == DBUS_TYPE_STRUCT ||
2244 sub->container_type == DBUS_TYPE_DICT_ENTRY) &&
2245 (writer->container_type == DBUS_TYPE_STRUCT ||
2246 writer->container_type == DBUS_TYPE_DICT_ENTRY ||
2247 writer->container_type == DBUS_TYPE_INVALID))
2248 {
2249 /* Advance the parent to the next struct field */
2250 writer->type_pos = sub->type_pos;
2251 }
2252 }
2253
2254 writer->value_pos = sub->value_pos;
2255
2256 #if RECURSIVE_MARSHAL_WRITE_TRACE
2257 _dbus_verbose (" type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
2258 writer, writer->type_pos, writer->value_pos,
2259 writer->type_str ?
2260 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
2261 "unknown");
2262 #endif
2263
2264 return TRUE;
2265 }
2266
2267 /**
2268 * Writes out a basic type.
2269 *
2270 * @param writer the writer
2271 * @param type the type to write
2272 * @param value the address of the value to write
2273 * @returns #FALSE if no memory
2274 */
2275 dbus_bool_t
_dbus_type_writer_write_basic(DBusTypeWriter * writer,int type,const void * value)2276 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
2277 int type,
2278 const void *value)
2279 {
2280 dbus_bool_t retval;
2281
2282 /* First ensure that our type realloc will succeed */
2283 if (!writer->type_pos_is_expectation && writer->type_str != NULL)
2284 {
2285 if (!_dbus_string_alloc_space (writer->type_str, 1))
2286 return FALSE;
2287 }
2288
2289 retval = FALSE;
2290
2291 if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
2292 goto out;
2293
2294 if (!write_or_verify_typecode (writer, type))
2295 _dbus_assert_not_reached ("failed to write typecode after prealloc");
2296
2297 retval = TRUE;
2298
2299 out:
2300 #if RECURSIVE_MARSHAL_WRITE_TRACE
2301 _dbus_verbose (" type writer %p basic type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
2302 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2303 writer->enabled);
2304 #endif
2305
2306 return retval;
2307 }
2308
2309 /**
2310 * Writes a block of fixed-length basic values, i.e. those that are
2311 * both dbus_type_is_fixed() and _dbus_type_is_basic(). The block
2312 * must be written inside an array.
2313 *
2314 * The value parameter should be the address of said array of values,
2315 * so e.g. if it's an array of double, pass in "const double**"
2316 *
2317 * @param writer the writer
2318 * @param element_type type of stuff in the array
2319 * @param value address of the array
2320 * @param n_elements number of elements in the array
2321 * @returns #FALSE if no memory
2322 */
2323 dbus_bool_t
_dbus_type_writer_write_fixed_multi(DBusTypeWriter * writer,int element_type,const void * value,int n_elements)2324 _dbus_type_writer_write_fixed_multi (DBusTypeWriter *writer,
2325 int element_type,
2326 const void *value,
2327 int n_elements)
2328 {
2329 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2330 _dbus_assert (dbus_type_is_fixed (element_type));
2331 _dbus_assert (writer->type_pos_is_expectation);
2332 _dbus_assert (n_elements >= 0);
2333
2334 #if RECURSIVE_MARSHAL_WRITE_TRACE
2335 _dbus_verbose (" type writer %p entering fixed multi type_pos = %d value_pos = %d n_elements %d\n",
2336 writer, writer->type_pos, writer->value_pos, n_elements);
2337 #endif
2338
2339 if (!write_or_verify_typecode (writer, element_type))
2340 _dbus_assert_not_reached ("OOM should not happen if only verifying typecode");
2341
2342 if (writer->enabled)
2343 {
2344 if (!_dbus_marshal_write_fixed_multi (writer->value_str,
2345 writer->value_pos,
2346 element_type,
2347 value,
2348 n_elements,
2349 writer->byte_order,
2350 &writer->value_pos))
2351 return FALSE;
2352 }
2353
2354 #if RECURSIVE_MARSHAL_WRITE_TRACE
2355 _dbus_verbose (" type writer %p fixed multi written new type_pos = %d new value_pos = %d n_elements %d\n",
2356 writer, writer->type_pos, writer->value_pos, n_elements);
2357 #endif
2358
2359 return TRUE;
2360 }
2361
2362 static void
enable_if_after(DBusTypeWriter * writer,DBusTypeReader * reader,const DBusTypeReader * start_after)2363 enable_if_after (DBusTypeWriter *writer,
2364 DBusTypeReader *reader,
2365 const DBusTypeReader *start_after)
2366 {
2367 if (start_after)
2368 {
2369 if (!writer->enabled && _dbus_type_reader_greater_than (reader, start_after))
2370 {
2371 _dbus_type_writer_set_enabled (writer, TRUE);
2372 #if RECURSIVE_MARSHAL_WRITE_TRACE
2373 _dbus_verbose ("ENABLING writer %p at %d because reader at value_pos %d is after reader at value_pos %d\n",
2374 writer, writer->value_pos, reader->value_pos, start_after->value_pos);
2375 #endif
2376 }
2377
2378 _dbus_assert ((!writer->enabled && !_dbus_type_reader_greater_than (reader, start_after)) ||
2379 (writer->enabled && _dbus_type_reader_greater_than (reader, start_after)));
2380 }
2381 }
2382
2383 static dbus_bool_t
append_fixup(DBusList ** fixups,const DBusArrayLenFixup * fixup)2384 append_fixup (DBusList **fixups,
2385 const DBusArrayLenFixup *fixup)
2386 {
2387 DBusArrayLenFixup *f;
2388
2389 f = dbus_new (DBusArrayLenFixup, 1);
2390 if (f == NULL)
2391 return FALSE;
2392
2393 *f = *fixup;
2394
2395 if (!_dbus_list_append (fixups, f))
2396 {
2397 dbus_free (f);
2398 return FALSE;
2399 }
2400
2401 _dbus_assert (f->len_pos_in_reader == fixup->len_pos_in_reader);
2402 _dbus_assert (f->new_len == fixup->new_len);
2403
2404 return TRUE;
2405 }
2406
2407 /* This loop is trivial if you ignore all the start_after nonsense,
2408 * so if you're trying to figure it out, start by ignoring that
2409 */
2410 static dbus_bool_t
writer_write_reader_helper(DBusTypeWriter * writer,DBusTypeReader * reader,const DBusTypeReader * start_after,int start_after_new_pos,int start_after_new_len,DBusList ** fixups,dbus_bool_t inside_start_after)2411 writer_write_reader_helper (DBusTypeWriter *writer,
2412 DBusTypeReader *reader,
2413 const DBusTypeReader *start_after,
2414 int start_after_new_pos,
2415 int start_after_new_len,
2416 DBusList **fixups,
2417 dbus_bool_t inside_start_after)
2418 {
2419 int current_type;
2420
2421 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
2422 {
2423 if (dbus_type_is_container (current_type))
2424 {
2425 DBusTypeReader subreader;
2426 DBusTypeWriter subwriter;
2427 const DBusString *sig_str;
2428 int sig_start;
2429 int sig_len;
2430 dbus_bool_t enabled_at_recurse;
2431 dbus_bool_t past_start_after;
2432 int reader_array_len_pos;
2433 int reader_array_start_pos;
2434 dbus_bool_t this_is_start_after;
2435
2436 /* type_pos is checked since e.g. in a struct the struct
2437 * and its first field have the same value_pos.
2438 * type_str will differ in reader/start_after for variants
2439 * where type_str is inside the value_str
2440 */
2441 if (!inside_start_after && start_after &&
2442 reader->value_pos == start_after->value_pos &&
2443 reader->type_str == start_after->type_str &&
2444 reader->type_pos == start_after->type_pos)
2445 this_is_start_after = TRUE;
2446 else
2447 this_is_start_after = FALSE;
2448
2449 _dbus_type_reader_recurse (reader, &subreader);
2450
2451 if (current_type == DBUS_TYPE_ARRAY)
2452 {
2453 reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader);
2454 reader_array_start_pos = subreader.u.array.start_pos;
2455 }
2456 else
2457 {
2458 /* quiet gcc */
2459 reader_array_len_pos = -1;
2460 reader_array_start_pos = -1;
2461 }
2462
2463 _dbus_type_reader_get_signature (&subreader, &sig_str,
2464 &sig_start, &sig_len);
2465
2466 #if RECURSIVE_MARSHAL_WRITE_TRACE
2467 _dbus_verbose ("about to recurse into %s reader at %d subreader at %d writer at %d start_after reader at %d write target len %d inside_start_after = %d this_is_start_after = %d\n",
2468 _dbus_type_to_string (current_type),
2469 reader->value_pos,
2470 subreader.value_pos,
2471 writer->value_pos,
2472 start_after ? start_after->value_pos : -1,
2473 _dbus_string_get_length (writer->value_str),
2474 inside_start_after, this_is_start_after);
2475 #endif
2476
2477 if (!inside_start_after && !this_is_start_after)
2478 enable_if_after (writer, &subreader, start_after);
2479 enabled_at_recurse = writer->enabled;
2480 if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
2481 sig_str, sig_start, sig_len,
2482 &subwriter, FALSE))
2483 goto oom;
2484
2485 #if RECURSIVE_MARSHAL_WRITE_TRACE
2486 _dbus_verbose ("recursed into subwriter at %d write target len %d\n",
2487 subwriter.value_pos,
2488 _dbus_string_get_length (subwriter.value_str));
2489 #endif
2490
2491 if (!writer_write_reader_helper (&subwriter, &subreader, start_after,
2492 start_after_new_pos, start_after_new_len,
2493 fixups,
2494 inside_start_after ||
2495 this_is_start_after))
2496 goto oom;
2497
2498 #if RECURSIVE_MARSHAL_WRITE_TRACE
2499 _dbus_verbose ("about to unrecurse from %s subreader at %d writer at %d subwriter at %d write target len %d\n",
2500 _dbus_type_to_string (current_type),
2501 subreader.value_pos,
2502 writer->value_pos,
2503 subwriter.value_pos,
2504 _dbus_string_get_length (writer->value_str));
2505 #endif
2506
2507 if (!inside_start_after && !this_is_start_after)
2508 enable_if_after (writer, &subreader, start_after);
2509 past_start_after = writer->enabled;
2510 if (!_dbus_type_writer_unrecurse (writer, &subwriter))
2511 goto oom;
2512
2513 /* If we weren't enabled when we recursed, we didn't
2514 * write an array len; if we passed start_after
2515 * somewhere inside the array, then we need to generate
2516 * a fixup.
2517 */
2518 if (start_after != NULL &&
2519 !enabled_at_recurse && past_start_after &&
2520 current_type == DBUS_TYPE_ARRAY &&
2521 fixups != NULL)
2522 {
2523 DBusArrayLenFixup fixup;
2524 int bytes_written_after_start_after;
2525 int bytes_before_start_after;
2526 int old_len;
2527
2528 /* this subwriter access is moderately unkosher since we
2529 * already unrecursed, but it works as long as unrecurse
2530 * doesn't break us on purpose
2531 */
2532 bytes_written_after_start_after = writer_get_array_len (&subwriter);
2533
2534 bytes_before_start_after =
2535 start_after->value_pos - reader_array_start_pos;
2536
2537 fixup.len_pos_in_reader = reader_array_len_pos;
2538 fixup.new_len =
2539 bytes_before_start_after +
2540 start_after_new_len +
2541 bytes_written_after_start_after;
2542
2543 _dbus_assert (_DBUS_ALIGN_VALUE (fixup.len_pos_in_reader, 4) ==
2544 (unsigned) fixup.len_pos_in_reader);
2545
2546 old_len = _dbus_unpack_uint32 (reader->byte_order,
2547 _dbus_string_get_const_data_len (reader->value_str,
2548 fixup.len_pos_in_reader, 4));
2549
2550 if (old_len != fixup.new_len && !append_fixup (fixups, &fixup))
2551 goto oom;
2552
2553 #if RECURSIVE_MARSHAL_WRITE_TRACE
2554 _dbus_verbose ("Generated fixup len_pos_in_reader = %d new_len = %d reader_array_start_pos = %d start_after->value_pos = %d bytes_before_start_after = %d start_after_new_len = %d bytes_written_after_start_after = %d\n",
2555 fixup.len_pos_in_reader,
2556 fixup.new_len,
2557 reader_array_start_pos,
2558 start_after->value_pos,
2559 bytes_before_start_after,
2560 start_after_new_len,
2561 bytes_written_after_start_after);
2562 #endif
2563 }
2564 }
2565 else
2566 {
2567 DBusBasicValue val;
2568
2569 _dbus_assert (dbus_type_is_basic (current_type));
2570
2571 #if RECURSIVE_MARSHAL_WRITE_TRACE
2572 _dbus_verbose ("Reading basic value %s at %d\n",
2573 _dbus_type_to_string (current_type),
2574 reader->value_pos);
2575 #endif
2576
2577 _dbus_type_reader_read_basic (reader, &val);
2578
2579 #if RECURSIVE_MARSHAL_WRITE_TRACE
2580 _dbus_verbose ("Writing basic value %s at %d write target len %d inside_start_after = %d\n",
2581 _dbus_type_to_string (current_type),
2582 writer->value_pos,
2583 _dbus_string_get_length (writer->value_str),
2584 inside_start_after);
2585 #endif
2586 if (!inside_start_after)
2587 enable_if_after (writer, reader, start_after);
2588 if (!_dbus_type_writer_write_basic (writer, current_type, &val))
2589 goto oom;
2590 #if RECURSIVE_MARSHAL_WRITE_TRACE
2591 _dbus_verbose ("Wrote basic value %s, new value_pos %d write target len %d\n",
2592 _dbus_type_to_string (current_type),
2593 writer->value_pos,
2594 _dbus_string_get_length (writer->value_str));
2595 #endif
2596 }
2597
2598 _dbus_type_reader_next (reader);
2599 }
2600
2601 return TRUE;
2602
2603 oom:
2604 if (fixups)
2605 apply_and_free_fixups (fixups, NULL); /* NULL for reader to apply to */
2606
2607 return FALSE;
2608 }
2609
2610 /**
2611 * Iterate through all values in the given reader, writing a copy of
2612 * each value to the writer. The reader will be moved forward to its
2613 * end position.
2614 *
2615 * If a reader start_after is provided, it should be a reader for the
2616 * same data as the reader to be written. Only values occurring after
2617 * the value pointed to by start_after will be written to the writer.
2618 *
2619 * If start_after is provided, then the copy of the reader will be
2620 * partial. This means that array lengths will not have been copied.
2621 * The assumption is that you wrote a new version of the value at
2622 * start_after to the writer. You have to pass in the start position
2623 * and length of the new value. (If you are deleting the value
2624 * at start_after, pass in 0 for the length.)
2625 *
2626 * If the fixups parameter is non-#NULL, then any array length that
2627 * was read but not written due to start_after will be provided
2628 * as a #DBusArrayLenFixup. The fixup contains the position of the
2629 * array length in the source data, and the correct array length
2630 * assuming you combine the source data before start_after with
2631 * the written data at start_after and beyond.
2632 *
2633 * @param writer the writer to copy to
2634 * @param reader the reader to copy from
2635 * @param start_after #NULL or a reader showing where to start
2636 * @param start_after_new_pos the position of start_after equivalent in the target data
2637 * @param start_after_new_len the length of start_after equivalent in the target data
2638 * @param fixups list to append #DBusArrayLenFixup if the write was partial
2639 * @returns #FALSE if no memory
2640 */
2641 dbus_bool_t
_dbus_type_writer_write_reader_partial(DBusTypeWriter * writer,DBusTypeReader * reader,const DBusTypeReader * start_after,int start_after_new_pos,int start_after_new_len,DBusList ** fixups)2642 _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
2643 DBusTypeReader *reader,
2644 const DBusTypeReader *start_after,
2645 int start_after_new_pos,
2646 int start_after_new_len,
2647 DBusList **fixups)
2648 {
2649 DBusTypeWriter orig;
2650 int orig_type_len;
2651 int orig_value_len;
2652 int new_bytes;
2653 int orig_enabled;
2654
2655 orig = *writer;
2656 orig_type_len = _dbus_string_get_length (writer->type_str);
2657 orig_value_len = _dbus_string_get_length (writer->value_str);
2658 orig_enabled = writer->enabled;
2659
2660 if (start_after)
2661 _dbus_type_writer_set_enabled (writer, FALSE);
2662
2663 if (!writer_write_reader_helper (writer, reader, start_after,
2664 start_after_new_pos,
2665 start_after_new_len,
2666 fixups, FALSE))
2667 goto oom;
2668
2669 _dbus_type_writer_set_enabled (writer, orig_enabled);
2670 return TRUE;
2671
2672 oom:
2673 if (!writer->type_pos_is_expectation)
2674 {
2675 new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
2676 _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
2677 }
2678 new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
2679 _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
2680
2681 *writer = orig;
2682
2683 return FALSE;
2684 }
2685
2686 /**
2687 * Iterate through all values in the given reader, writing a copy of
2688 * each value to the writer. The reader will be moved forward to its
2689 * end position.
2690 *
2691 * @param writer the writer to copy to
2692 * @param reader the reader to copy from
2693 * @returns #FALSE if no memory
2694 */
2695 dbus_bool_t
_dbus_type_writer_write_reader(DBusTypeWriter * writer,DBusTypeReader * reader)2696 _dbus_type_writer_write_reader (DBusTypeWriter *writer,
2697 DBusTypeReader *reader)
2698 {
2699 return _dbus_type_writer_write_reader_partial (writer, reader, NULL, 0, 0, NULL);
2700 }
2701
2702 /**
2703 * If disabled, a writer can still be iterated forward and recursed/unrecursed
2704 * but won't write any values. Types will still be written unless the
2705 * writer is a "values only" writer, because the writer needs access to
2706 * a valid signature to be able to iterate.
2707 *
2708 * @param writer the type writer
2709 * @param enabled #TRUE if values should be written
2710 */
2711 void
_dbus_type_writer_set_enabled(DBusTypeWriter * writer,dbus_bool_t enabled)2712 _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
2713 dbus_bool_t enabled)
2714 {
2715 writer->enabled = enabled != FALSE;
2716 }
2717
2718 /** @} */ /* end of DBusMarshal group */
2719
2720 /* tests in dbus-marshal-recursive-util.c */
2721