2005-02-24 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / dbus / dbus-marshal-basic.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-basic.c  Marshalling routines for basic (primitive) types
3  *
4  * Copyright (C) 2002 CodeFactory AB
5  * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dbus-internals.h"
26 #include "dbus-marshal-basic.h"
27 #include "dbus-signature.h"
28
29 #include <string.h>
30
31 /**
32  * @defgroup DBusMarshal marshaling and unmarshaling
33  * @ingroup  DBusInternals
34  * @brief functions to marshal/unmarshal data from the wire
35  *
36  * Types and functions related to converting primitive data types from
37  * wire format to native machine format, and vice versa.
38  *
39  * A signature is just a string with multiple types one after the other.
40  * for example a type is "i" or "(ii)", a signature is "i(ii)"
41  * where i is int and (ii) is struct { int; int; }
42  *
43  * @{
44  */
45
46 static void
47 pack_2_octets (dbus_uint16_t   value,
48                int             byte_order,
49                unsigned char  *data)
50 {
51   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
52
53   if ((byte_order) == DBUS_LITTLE_ENDIAN)
54     *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
55   else
56     *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
57 }
58
59 static void
60 pack_4_octets (dbus_uint32_t   value,
61                int             byte_order,
62                unsigned char  *data)
63 {
64   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
65
66   if ((byte_order) == DBUS_LITTLE_ENDIAN)
67     *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
68   else
69     *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
70 }
71
72 static void
73 pack_8_octets (DBusBasicValue     value,
74                int                byte_order,
75                unsigned char     *data)
76 {
77   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
78
79 #ifdef DBUS_HAVE_INT64
80   if ((byte_order) == DBUS_LITTLE_ENDIAN)
81     *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
82   else
83     *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
84 #else
85   *(DBus8ByteStruct*)data = value.u64;
86   swap_8_octets ((DBusBasicValue*)data, byte_order);
87 #endif
88 }
89
90 /**
91  * Packs a 32 bit unsigned integer into a data pointer.
92  *
93  * @param value the value
94  * @param byte_order the byte order to use
95  * @param data the data pointer
96  */
97 void
98 _dbus_pack_uint32 (dbus_uint32_t   value,
99                    int             byte_order,
100                    unsigned char  *data)
101 {
102   pack_4_octets (value, byte_order, data);
103 }
104
105 #ifndef DBUS_HAVE_INT64
106 /* from ORBit */
107 static void
108 swap_bytes (unsigned char *data,
109             unsigned int   len)
110 {
111   unsigned char *p1 = data;
112   unsigned char *p2 = data + len - 1;
113
114   while (p1 < p2)
115     {
116       unsigned char tmp = *p1;
117       *p1 = *p2;
118       *p2 = tmp;
119
120       --p2;
121       ++p1;
122     }
123 }
124 #endif /* !DBUS_HAVE_INT64 */
125
126 static void
127 swap_8_octets (DBusBasicValue    *value,
128                int                byte_order)
129 {
130   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
131     {
132 #ifdef DBUS_HAVE_INT64
133       value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
134 #else
135       swap_bytes ((unsigned char *)value, 8);
136 #endif
137     }
138 }
139
140 #if 0
141 static DBusBasicValue
142 unpack_8_octets (int                  byte_order,
143                  const unsigned char *data)
144 {
145   DBusBasicValue r;
146
147   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
148   _dbus_assert (sizeof (r) == 8);
149
150 #ifdef DBUS_HAVE_INT64
151   if (byte_order == DBUS_LITTLE_ENDIAN)
152     r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
153   else
154     r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
155 #else
156   r.u64 = *(DBus8ByteStruct*)data;
157   swap_8_octets (&r, byte_order);
158 #endif
159
160   return r;
161 }
162 #endif
163
164 #ifndef _dbus_unpack_uint16
165 /**
166  * Unpacks a 16 bit unsigned integer from a data pointer
167  *
168  * @param byte_order The byte order to use
169  * @param data the data pointer
170  * @returns the integer
171  */
172 dbus_uint16_t
173 _dbus_unpack_uint16 (int                  byte_order,
174                      const unsigned char *data)
175 {
176   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
177
178   if (byte_order == DBUS_LITTLE_ENDIAN)
179     return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
180   else
181     return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
182 }
183 #endif /* _dbus_unpack_uint16 */
184
185 #ifndef _dbus_unpack_uint32
186 /**
187  * Unpacks a 32 bit unsigned integer from a data pointer
188  *
189  * @param byte_order The byte order to use
190  * @param data the data pointer
191  * @returns the integer
192  */
193 dbus_uint32_t
194 _dbus_unpack_uint32 (int                  byte_order,
195                      const unsigned char *data)
196 {
197   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
198
199   if (byte_order == DBUS_LITTLE_ENDIAN)
200     return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
201   else
202     return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
203 }
204 #endif /* _dbus_unpack_uint32 */
205
206 static void
207 set_2_octets (DBusString          *str,
208               int                  offset,
209               dbus_uint16_t        value,
210               int                  byte_order)
211 {
212   char *data;
213
214   _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
215                 byte_order == DBUS_BIG_ENDIAN);
216
217   data = _dbus_string_get_data_len (str, offset, 2);
218
219   pack_2_octets (value, byte_order, data);
220 }
221
222 static void
223 set_4_octets (DBusString          *str,
224               int                  offset,
225               dbus_uint32_t        value,
226               int                  byte_order)
227 {
228   char *data;
229
230   _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
231                 byte_order == DBUS_BIG_ENDIAN);
232
233   data = _dbus_string_get_data_len (str, offset, 4);
234
235   pack_4_octets (value, byte_order, data);
236 }
237
238 static void
239 set_8_octets (DBusString          *str,
240               int                  offset,
241               DBusBasicValue       value,
242               int                  byte_order)
243 {
244   char *data;
245
246   _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
247                 byte_order == DBUS_BIG_ENDIAN);
248
249   data = _dbus_string_get_data_len (str, offset, 8);
250
251   pack_8_octets (value, byte_order, data);
252 }
253
254 /**
255  * Sets the 4 bytes at the given offset to a marshaled unsigned
256  * integer, replacing anything found there previously.
257  *
258  * @param str the string to write the marshalled int to
259  * @param pos the byte offset where int should be written
260  * @param value the value
261  * @param byte_order the byte order to use
262  *
263  */
264 void
265 _dbus_marshal_set_uint32 (DBusString          *str,
266                           int                  pos,
267                           dbus_uint32_t        value,
268                           int                  byte_order)
269 {
270   set_4_octets (str, pos, value, byte_order);
271 }
272
273 /**
274  * Sets the existing marshaled string at the given offset with
275  * a new marshaled string. The given offset must point to
276  * an existing string or the wrong length will be deleted
277  * and replaced with the new string.
278  *
279  * Note: no attempt is made by this function to re-align
280  * any data which has been already marshalled after this
281  * string. Use with caution.
282  *
283  * @param str the string to write the marshalled string to
284  * @param pos the position of the marshaled string length
285  * @param value the value
286  * @param byte_order the byte order to use
287  * @param old_end_pos place to store byte after the nul byte of the old value
288  * @param new_end_pos place to store byte after the nul byte of the new value
289  * @returns #TRUE on success, #FALSE if no memory
290  *
291  */
292 static dbus_bool_t
293 set_string (DBusString          *str,
294             int                  pos,
295             const char          *value,
296             int                  byte_order,
297             int                 *old_end_pos,
298             int                 *new_end_pos)
299 {
300   int old_len, new_len;
301   DBusString dstr;
302
303   _dbus_string_init_const (&dstr, value);
304
305   _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
306   old_len = _dbus_unpack_uint32 (byte_order,
307                                  _dbus_string_get_const_data_len (str, pos, 4));
308
309   new_len = _dbus_string_get_length (&dstr);
310
311   if (!_dbus_string_replace_len (&dstr, 0, new_len,
312                                  str, pos + 4, old_len))
313     return FALSE;
314
315   _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
316
317   if (old_end_pos)
318     *old_end_pos = pos + 4 + old_len + 1;
319   if (new_end_pos)
320     *new_end_pos = pos + 4 + new_len + 1;
321
322   return TRUE;
323 }
324
325 /**
326  * Sets the existing marshaled signature at the given offset to a new
327  * marshaled signature. Same basic ideas as set_string().
328  *
329  * @param str the string to write the marshalled signature to
330  * @param pos the position of the marshaled signature length
331  * @param value the value
332  * @param byte_order the byte order to use
333  * @param old_end_pos place to store byte after the nul byte of the old value
334  * @param new_end_pos place to store byte after the nul byte of the new value
335  * @returns #TRUE on success, #FALSE if no memory
336  *
337  */
338 static dbus_bool_t
339 set_signature (DBusString          *str,
340                int                  pos,
341                const char          *value,
342                int                  byte_order,
343                int                 *old_end_pos,
344                int                 *new_end_pos)
345 {
346   int old_len, new_len;
347   DBusString dstr;
348
349   _dbus_string_init_const (&dstr, value);
350
351   old_len = _dbus_string_get_byte (str, pos);
352   new_len = _dbus_string_get_length (&dstr);
353
354   if (!_dbus_string_replace_len (&dstr, 0, new_len,
355                                  str, pos + 1, old_len))
356     return FALSE;
357
358   _dbus_string_set_byte (str, pos, new_len);
359
360   if (old_end_pos)
361     *old_end_pos = pos + 1 + old_len + 1;
362   if (new_end_pos)
363     *new_end_pos = pos + 1 + new_len + 1;
364
365   return TRUE;
366 }
367
368 /**
369  * Sets an existing basic type value to a new value.
370  * Arguments work the same way as _dbus_marshal_basic_type().
371  *
372  * @param str the string
373  * @param pos location of the current value
374  * @param type the type of the current and new values
375  * @param value the address of the new value
376  * @param byte_order byte order for marshaling
377  * @param old_end_pos location to store end position of the old value, or #NULL
378  * @param new_end_pos location to store end position of the new value, or #NULL
379  * @returns #FALSE if no memory
380  */
381 dbus_bool_t
382 _dbus_marshal_set_basic (DBusString       *str,
383                          int               pos,
384                          int               type,
385                          const void       *value,
386                          int               byte_order,
387                          int              *old_end_pos,
388                          int              *new_end_pos)
389 {
390   const DBusBasicValue *vp;
391
392   vp = value;
393
394   switch (type)
395     {
396     case DBUS_TYPE_BYTE:
397       _dbus_string_set_byte (str, pos, vp->byt);
398       if (old_end_pos)
399         *old_end_pos = pos + 1;
400       if (new_end_pos)
401         *new_end_pos = pos + 1;
402       return TRUE;
403       break;
404     case DBUS_TYPE_INT16:
405     case DBUS_TYPE_UINT16:
406       pos = _DBUS_ALIGN_VALUE (pos, 2);
407       set_2_octets (str, pos, vp->u16, byte_order);
408       if (old_end_pos)
409         *old_end_pos = pos + 2;
410       if (new_end_pos)
411         *new_end_pos = pos + 2;
412       return TRUE;
413       break;
414     case DBUS_TYPE_BOOLEAN:
415     case DBUS_TYPE_INT32:
416     case DBUS_TYPE_UINT32:
417       pos = _DBUS_ALIGN_VALUE (pos, 4);
418       set_4_octets (str, pos, vp->u32, byte_order);
419       if (old_end_pos)
420         *old_end_pos = pos + 4;
421       if (new_end_pos)
422         *new_end_pos = pos + 4;
423       return TRUE;
424       break;
425     case DBUS_TYPE_INT64:
426     case DBUS_TYPE_UINT64:
427     case DBUS_TYPE_DOUBLE:
428       pos = _DBUS_ALIGN_VALUE (pos, 8);
429       set_8_octets (str, pos, *vp, byte_order);
430       if (old_end_pos)
431         *old_end_pos = pos + 8;
432       if (new_end_pos)
433         *new_end_pos = pos + 8;
434       return TRUE;
435       break;
436     case DBUS_TYPE_STRING:
437     case DBUS_TYPE_OBJECT_PATH:
438       pos = _DBUS_ALIGN_VALUE (pos, 4);
439       _dbus_assert (vp->str != NULL);
440       return set_string (str, pos, vp->str, byte_order,
441                          old_end_pos, new_end_pos);
442       break;
443     case DBUS_TYPE_SIGNATURE:
444       _dbus_assert (vp->str != NULL);
445       return set_signature (str, pos, vp->str, byte_order,
446                             old_end_pos, new_end_pos);
447       break;
448     default:
449       _dbus_assert_not_reached ("not a basic type");
450       return FALSE;
451       break;
452     }
453 }
454
455 /**
456  * Convenience function to demarshal a 32 bit unsigned integer.
457  *
458  * @param str the string containing the data
459  * @param byte_order the byte order
460  * @param pos the position in the string
461  * @param new_pos the new position of the string
462  * @returns the demarshaled integer.
463  */
464 dbus_uint32_t
465 _dbus_marshal_read_uint32  (const DBusString *str,
466                             int               pos,
467                             int               byte_order,
468                             int              *new_pos)
469 {
470   pos = _DBUS_ALIGN_VALUE (pos, 4);
471
472   if (new_pos)
473     *new_pos = pos + 4;
474
475   _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
476   
477   return _dbus_unpack_uint32 (byte_order,
478                               _dbus_string_get_const_data (str) + pos);
479 }
480
481 /**
482  * Demarshals a basic-typed value. The "value" pointer is always
483  * the address of a variable of the basic type. So e.g.
484  * if the basic type is "double" then the pointer is
485  * a double*, and if it's "char*" then the pointer is
486  * a "char**".
487  *
488  * A value of type #DBusBasicValue is guaranteed to be large enough to
489  * hold any of the types that may be returned, which is handy if you
490  * are trying to do things generically. For example you can pass
491  * a DBusBasicValue* in to this function, and then pass the same
492  * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
493  * move a value from one place to another.
494  *
495  * @param str the string containing the data
496  * @param pos position in the string
497  * @param type type of value to demarshal
498  * @param value pointer to return value data
499  * @param byte_order the byte order
500  * @param new_pos pointer to update with new position, or #NULL
501  **/
502 void
503 _dbus_marshal_read_basic (const DBusString      *str,
504                           int                    pos,
505                           int                    type,
506                           void                  *value,
507                           int                    byte_order,
508                           int                   *new_pos)
509 {
510   const char *str_data;
511   DBusBasicValue *vp;
512
513   _dbus_assert (dbus_type_is_basic (type));
514
515   str_data = _dbus_string_get_const_data (str);
516   vp = value;
517
518   switch (type)
519     {
520     case DBUS_TYPE_BYTE:
521       vp->byt = _dbus_string_get_byte (str, pos);
522       (pos)++;
523       break;
524     case DBUS_TYPE_INT16:
525     case DBUS_TYPE_UINT16:
526       pos = _DBUS_ALIGN_VALUE (pos, 2);
527       vp->u16 = *(dbus_uint16_t *)(str_data + pos);
528       if (byte_order != DBUS_COMPILER_BYTE_ORDER)
529         vp->u16 = DBUS_UINT16_SWAP_LE_BE (vp->u16);
530       pos += 2;
531       break;
532     case DBUS_TYPE_INT32:
533     case DBUS_TYPE_UINT32:
534     case DBUS_TYPE_BOOLEAN:
535       pos = _DBUS_ALIGN_VALUE (pos, 4);
536       vp->u32 = *(dbus_uint32_t *)(str_data + pos);
537       if (byte_order != DBUS_COMPILER_BYTE_ORDER)
538         vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
539       pos += 4;
540       break;
541     case DBUS_TYPE_INT64:
542     case DBUS_TYPE_UINT64:
543     case DBUS_TYPE_DOUBLE:
544       pos = _DBUS_ALIGN_VALUE (pos, 8);
545 #ifdef DBUS_HAVE_INT64
546       if (byte_order != DBUS_COMPILER_BYTE_ORDER)
547         vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
548       else
549         vp->u64 = *(dbus_uint64_t*)(str_data + pos);
550 #else
551       vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
552       swap_8_octets (vp, byte_order);
553 #endif
554       pos += 8;
555       break;
556     case DBUS_TYPE_STRING:
557     case DBUS_TYPE_OBJECT_PATH:
558       {
559         int len;
560
561         len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
562
563         vp->str = (char*) str_data + pos;
564
565         pos += len + 1; /* length plus nul */
566       }
567       break;
568     case DBUS_TYPE_SIGNATURE:
569       {
570         int len;
571
572         len = _dbus_string_get_byte (str, pos);
573         pos += 1;
574
575         vp->str = (char*) str_data + pos;
576
577         pos += len + 1; /* length plus nul */
578       }
579       break;
580     default:
581       _dbus_warn ("type %s %d not a basic type\n",
582                   _dbus_type_to_string (type), type);
583       _dbus_assert_not_reached ("not a basic type");
584       break;
585     }
586
587   if (new_pos)
588     *new_pos = pos;
589 }
590
591 /**
592  * Reads a block of fixed-length basic values, as an optimization
593  * vs. reading each one individually into a new buffer.
594  *
595  * This function returns the data in-place; it does not make a copy,
596  * and it does not swap the bytes.
597  *
598  * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
599  * and the "value" argument should be a "const double**" and so on.
600  *
601  * @todo we aren't using this function (except in the test suite)
602  * 
603  * @param str the string to read from
604  * @param pos position to read from
605  * @param element_type type of array elements
606  * @param value place to return the array
607  * @param n_elements number of array elements to read
608  * @param byte_order the byte order, used to read the array length
609  * @param new_pos #NULL or location to store a position after the elements
610  */
611 void
612 _dbus_marshal_read_fixed_multi  (const DBusString *str,
613                                  int               pos,
614                                  int               element_type,
615                                  void             *value,
616                                  int               n_elements,
617                                  int               byte_order,
618                                  int              *new_pos)
619 {
620   int array_len;
621   int alignment;
622
623   _dbus_assert (_dbus_type_is_fixed (element_type));
624   _dbus_assert (dbus_type_is_basic (element_type));
625
626 #if 0
627   _dbus_verbose ("reading %d elements of %s\n",
628                  n_elements, _dbus_type_to_string (element_type));
629 #endif
630   
631   alignment = _dbus_type_get_alignment (element_type);
632
633   pos = _DBUS_ALIGN_VALUE (pos, alignment);
634   
635   array_len = n_elements * alignment;
636
637   *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
638   if (new_pos)
639     *new_pos = pos + array_len;
640 }
641
642 static dbus_bool_t
643 marshal_2_octets (DBusString   *str,
644                   int           insert_at,
645                   dbus_uint16_t value,
646                   int           byte_order,
647                   int          *pos_after)
648 {
649   dbus_bool_t retval;
650   int orig_len;
651
652   _dbus_assert (sizeof (value) == 2);
653
654   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
655     value = DBUS_UINT16_SWAP_LE_BE (value);
656
657   orig_len = _dbus_string_get_length (str);
658
659   retval = _dbus_string_insert_2_aligned (str, insert_at,
660                                           (const unsigned char *)&value);
661
662   if (pos_after)
663     {
664       *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
665       _dbus_assert (*pos_after <= _dbus_string_get_length (str));
666     }
667
668   return retval;
669 }
670
671 static dbus_bool_t
672 marshal_4_octets (DBusString   *str,
673                   int           insert_at,
674                   dbus_uint32_t value,
675                   int           byte_order,
676                   int          *pos_after)
677 {
678   dbus_bool_t retval;
679   int orig_len;
680
681   _dbus_assert (sizeof (value) == 4);
682
683   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
684     value = DBUS_UINT32_SWAP_LE_BE (value);
685
686   orig_len = _dbus_string_get_length (str);
687
688   retval = _dbus_string_insert_4_aligned (str, insert_at,
689                                           (const unsigned char *)&value);
690
691   if (pos_after)
692     {
693       *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
694       _dbus_assert (*pos_after <= _dbus_string_get_length (str));
695     }
696
697   return retval;
698 }
699
700 static dbus_bool_t
701 marshal_8_octets (DBusString    *str,
702                   int            insert_at,
703                   DBusBasicValue value,
704                   int            byte_order,
705                   int           *pos_after)
706 {
707   dbus_bool_t retval;
708   int orig_len;
709
710   _dbus_assert (sizeof (value) == 8);
711
712   swap_8_octets (&value, byte_order);
713
714   orig_len = _dbus_string_get_length (str);
715
716   retval = _dbus_string_insert_8_aligned (str, insert_at,
717                                           (const unsigned char *)&value);
718
719   if (pos_after)
720     *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
721
722   return retval;
723 }
724
725 enum
726   {
727     MARSHAL_AS_STRING,
728     MARSHAL_AS_SIGNATURE,
729     MARSHAL_AS_BYTE_ARRAY
730   };
731
732 static dbus_bool_t
733 marshal_len_followed_by_bytes (int                  marshal_as,
734                                DBusString          *str,
735                                int                  insert_at,
736                                const unsigned char *value,
737                                int                  data_len, /* doesn't include nul if any */
738                                int                  byte_order,
739                                int                 *pos_after)
740 {
741   int pos;
742   DBusString value_str;
743   int value_len;
744
745   _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
746   if (insert_at > _dbus_string_get_length (str))
747     _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
748                 insert_at, _dbus_string_get_length (str), data_len);
749   
750   if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
751     value_len = data_len;
752   else
753     value_len = data_len + 1; /* value has a nul */
754
755   _dbus_string_init_const_len (&value_str, value, value_len);
756
757   pos = insert_at;
758
759   if (marshal_as == MARSHAL_AS_SIGNATURE)
760     {
761       _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
762       _dbus_assert (data_len <= 255); /* same as max sig len right now */
763       
764       if (!_dbus_string_insert_byte (str, pos, data_len))
765         goto oom;
766
767       pos += 1;
768     }
769   else
770     {
771       if (!marshal_4_octets (str, pos, data_len,
772                              byte_order, &pos))
773         goto oom;
774     }
775
776   if (!_dbus_string_copy_len (&value_str, 0, value_len,
777                               str, pos))
778     goto oom;
779
780 #if 0
781   /* too expensive */
782   _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
783                                               str, pos));
784   _dbus_verbose_bytes_of_string (str, pos, value_len);
785 #endif
786
787   pos += value_len;
788
789   if (pos_after)
790     *pos_after = pos;
791
792   return TRUE;
793
794  oom:
795   /* Delete what we've inserted */
796   _dbus_string_delete (str, insert_at, pos - insert_at);
797
798   return FALSE;
799 }
800
801 static dbus_bool_t
802 marshal_string (DBusString    *str,
803                 int            insert_at,
804                 const char    *value,
805                 int            byte_order,
806                 int           *pos_after)
807 {
808   return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
809                                         str, insert_at, value,
810                                         strlen (value),
811                                         byte_order, pos_after);
812 }
813
814 static dbus_bool_t
815 marshal_signature (DBusString    *str,
816                    int            insert_at,
817                    const char    *value,
818                    int           *pos_after)
819 {
820   return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
821                                         str, insert_at, value,
822                                         strlen (value),
823                                         DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
824                                         pos_after);
825 }
826
827 /**
828  * Marshals a basic-typed value. The "value" pointer is always the
829  * address of a variable containing the basic type value.
830  * So for example for int32 it will be dbus_int32_t*, and
831  * for string it will be const char**. This is for symmetry
832  * with _dbus_marshal_read_basic() and to have a simple
833  * consistent rule.
834  *
835  * @param str string to marshal to
836  * @param insert_at where to insert the value
837  * @param type type of value
838  * @param value pointer to a variable containing the value
839  * @param byte_order byte order
840  * @param pos_after #NULL or the position after the type
841  * @returns #TRUE on success
842  **/
843 dbus_bool_t
844 _dbus_marshal_write_basic (DBusString *str,
845                            int         insert_at,
846                            int         type,
847                            const void *value,
848                            int         byte_order,
849                            int        *pos_after)
850 {
851   const DBusBasicValue *vp;
852
853   _dbus_assert (dbus_type_is_basic (type));
854
855   vp = value;
856
857   switch (type)
858     {
859     case DBUS_TYPE_BYTE:
860       if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
861         return FALSE;
862       if (pos_after)
863         *pos_after = insert_at + 1;
864       return TRUE;
865       break;
866     case DBUS_TYPE_INT16:
867     case DBUS_TYPE_UINT16:
868       return marshal_2_octets (str, insert_at, vp->u16,
869                                byte_order, pos_after);
870       break;
871     case DBUS_TYPE_BOOLEAN:
872       return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
873                                byte_order, pos_after);
874       break;
875     case DBUS_TYPE_INT32:
876     case DBUS_TYPE_UINT32:
877       return marshal_4_octets (str, insert_at, vp->u32,
878                                byte_order, pos_after);
879       break;
880     case DBUS_TYPE_INT64:
881     case DBUS_TYPE_UINT64:
882     case DBUS_TYPE_DOUBLE:
883       return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
884       break;
885
886     case DBUS_TYPE_STRING:
887     case DBUS_TYPE_OBJECT_PATH:
888       _dbus_assert (vp->str != NULL);
889       return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
890       break;
891     case DBUS_TYPE_SIGNATURE:
892       _dbus_assert (vp->str != NULL);
893       return marshal_signature (str, insert_at, vp->str, pos_after);
894       break;
895     default:
896       _dbus_assert_not_reached ("not a basic type");
897       return FALSE;
898       break;
899     }
900 }
901
902 static dbus_bool_t
903 marshal_1_octets_array (DBusString          *str,
904                         int                  insert_at,
905                         const unsigned char *value,
906                         int                  n_elements,
907                         int                  byte_order,
908                         int                 *pos_after)
909 {
910   int pos;
911   DBusString value_str;
912
913   _dbus_string_init_const_len (&value_str, value, n_elements);
914
915   pos = insert_at;
916
917   if (!_dbus_string_copy_len (&value_str, 0, n_elements,
918                               str, pos))
919     return FALSE;
920
921   pos += n_elements;
922
923   if (pos_after)
924     *pos_after = pos;
925
926   return TRUE;
927 }
928
929 /**
930  * Swaps the elements of an array to the opposite byte order
931  *
932  * @param data start of array
933  * @param n_elements number of elements
934  * @param alignment size of each element
935  */
936 void
937 _dbus_swap_array (unsigned char *data,
938                   int            n_elements,
939                   int            alignment)
940 {
941   unsigned char *d;
942   unsigned char *end;
943
944   _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
945
946   /* we use const_data and cast it off so DBusString can be a const string
947    * for the unit tests. don't ask.
948    */
949   d = data;
950   end = d + (n_elements * alignment);
951   
952   if (alignment == 8)
953     {
954       while (d != end)
955         {
956 #ifdef DBUS_HAVE_INT64
957           *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
958 #else
959           swap_8_bytes ((DBusBasicValue*) d);
960 #endif
961           d += 8;
962         }
963     }
964   else if (alignment == 4)
965     {
966       while (d != end)
967         {
968           *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
969           d += 4;
970         }
971     }
972   else
973     {
974       _dbus_assert (alignment == 2);
975       
976       while (d != end)
977         {
978           *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
979           d += 2;
980         }
981     }
982 }
983
984 static void
985 swap_array (DBusString *str,
986             int         array_start,
987             int         n_elements,
988             int         byte_order,
989             int         alignment)
990 {
991   _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
992
993   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
994     {
995       /* we use const_data and cast it off so DBusString can be a const string
996        * for the unit tests. don't ask.
997        */
998       _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
999                         n_elements, alignment);
1000     }
1001 }
1002
1003 static dbus_bool_t
1004 marshal_fixed_multi (DBusString           *str,
1005                      int                   insert_at,
1006                      const DBusBasicValue *value,
1007                      int                   n_elements,
1008                      int                   byte_order,
1009                      int                   alignment,
1010                      int                  *pos_after)
1011 {
1012   int old_string_len;
1013   int array_start;
1014   DBusString t;
1015   int len_in_bytes;
1016
1017   _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
1018   
1019   old_string_len = _dbus_string_get_length (str);
1020
1021   len_in_bytes = n_elements * alignment;
1022   array_start = insert_at;
1023   
1024   /* Note that we do alignment padding unconditionally
1025    * even if the array is empty; this means that
1026    * padding + len is always equal to the number of bytes
1027    * in the array.
1028    */
1029
1030   if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1031     goto error;
1032
1033   _dbus_string_init_const_len (&t,
1034                                (const unsigned char*) value,
1035                                len_in_bytes);
1036
1037   if (!_dbus_string_copy (&t, 0,
1038                           str, array_start))
1039     goto error;
1040
1041   swap_array (str, array_start, n_elements, byte_order, alignment);
1042
1043   if (pos_after)
1044     *pos_after = array_start + len_in_bytes;
1045   
1046   return TRUE;
1047
1048  error:
1049   _dbus_string_delete (str, insert_at,
1050                        _dbus_string_get_length (str) - old_string_len);
1051
1052   return FALSE;
1053 }
1054
1055 /**
1056  * Marshals a block of values of fixed-length type all at once, as an
1057  * optimization.  _dbus_type_is_fixed() returns #TRUE for fixed-length
1058  * types, which are the basic types minus the string-like types.
1059  *
1060  * The value argument should be the adddress of an
1061  * array, so e.g. "const dbus_uint32_t**"
1062  *
1063  * @param str string to marshal to
1064  * @param insert_at where to insert the value
1065  * @param element_type type of array elements
1066  * @param value address of an array to marshal
1067  * @param n_elements number of elements in the array
1068  * @param byte_order byte order
1069  * @param pos_after #NULL or the position after the type
1070  * @returns #TRUE on success
1071  **/
1072 dbus_bool_t
1073 _dbus_marshal_write_fixed_multi (DBusString *str,
1074                                  int         insert_at,
1075                                  int         element_type,
1076                                  const void *value,
1077                                  int         n_elements,
1078                                  int         byte_order,
1079                                  int        *pos_after)
1080 {
1081   const void* vp = *(const DBusBasicValue**)value;
1082   
1083   _dbus_assert (_dbus_type_is_fixed (element_type));
1084   _dbus_assert (n_elements >= 0);
1085
1086 #if 0
1087   _dbus_verbose ("writing %d elements of %s\n",
1088                  n_elements, _dbus_type_to_string (element_type));
1089 #endif
1090   
1091   switch (element_type)
1092     {
1093     case DBUS_TYPE_BYTE:
1094       return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1095       break;
1096     case DBUS_TYPE_INT16:
1097     case DBUS_TYPE_UINT16:
1098       return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1099       /* FIXME: we canonicalize to 0 or 1 for the single boolean case
1100        * should we here too ? */
1101     case DBUS_TYPE_BOOLEAN:
1102     case DBUS_TYPE_INT32:
1103     case DBUS_TYPE_UINT32:
1104       return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1105       break;
1106     case DBUS_TYPE_INT64:
1107     case DBUS_TYPE_UINT64:
1108     case DBUS_TYPE_DOUBLE:
1109       return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1110       break;
1111
1112     default:
1113       _dbus_assert_not_reached ("non fixed type in array write");
1114       break;
1115     }
1116
1117   return FALSE;
1118 }
1119
1120
1121 /**
1122  * Skips over a basic-typed value, reporting the following position.
1123  *
1124  * @param str the string containing the data
1125  * @param type type of value to read
1126  * @param byte_order the byte order
1127  * @param pos pointer to position in the string,
1128  *            updated on return to new position
1129  **/
1130 void
1131 _dbus_marshal_skip_basic (const DBusString      *str,
1132                           int                    type,
1133                           int                    byte_order,
1134                           int                   *pos)
1135 {
1136   _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1137                 byte_order == DBUS_BIG_ENDIAN);
1138   
1139   switch (type)
1140     {
1141     case DBUS_TYPE_BYTE:
1142       (*pos)++;
1143       break;
1144     case DBUS_TYPE_INT16:
1145     case DBUS_TYPE_UINT16:
1146       *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1147       *pos += 2;
1148       break;
1149     case DBUS_TYPE_BOOLEAN:
1150     case DBUS_TYPE_INT32:
1151     case DBUS_TYPE_UINT32:
1152       *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1153       *pos += 4;
1154       break;
1155     case DBUS_TYPE_INT64:
1156     case DBUS_TYPE_UINT64:
1157     case DBUS_TYPE_DOUBLE:
1158       *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1159       *pos += 8;
1160       break;
1161     case DBUS_TYPE_STRING:
1162     case DBUS_TYPE_OBJECT_PATH:
1163       {
1164         int len;
1165
1166         len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1167         
1168         *pos += len + 1; /* length plus nul */
1169       }
1170       break;
1171     case DBUS_TYPE_SIGNATURE:
1172       {
1173         int len;
1174
1175         len = _dbus_string_get_byte (str, *pos);
1176
1177         *pos += len + 2; /* length byte plus length plus nul */
1178       }
1179       break;
1180     default:
1181       _dbus_warn ("type %s not a basic type\n",
1182                   _dbus_type_to_string (type));
1183       _dbus_assert_not_reached ("not a basic type");
1184       break;
1185     }
1186 }
1187
1188 /**
1189  * Skips an array, returning the next position.
1190  *
1191  * @param str the string containing the data
1192  * @param element_type the type of array elements
1193  * @param byte_order the byte order
1194  * @param pos pointer to position in the string,
1195  *            updated on return to new position
1196  */
1197 void
1198 _dbus_marshal_skip_array (const DBusString  *str,
1199                           int                element_type,
1200                           int                byte_order,
1201                           int               *pos)
1202 {
1203   dbus_uint32_t array_len;
1204   int i;
1205   int alignment;
1206
1207   i = _DBUS_ALIGN_VALUE (*pos, 4);
1208
1209   array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1210
1211   alignment = _dbus_type_get_alignment (element_type);
1212
1213   i = _DBUS_ALIGN_VALUE (i, alignment);
1214
1215   *pos = i + array_len;
1216 }
1217
1218 /**
1219  * Gets the alignment requirement for the given type;
1220  * will be 1, 4, or 8.
1221  *
1222  * @param typecode the type
1223  * @returns alignment of 1, 4, or 8
1224  */
1225 int
1226 _dbus_type_get_alignment (int typecode)
1227 {
1228   switch (typecode)
1229     {
1230     case DBUS_TYPE_BYTE:
1231     case DBUS_TYPE_VARIANT:
1232     case DBUS_TYPE_SIGNATURE:
1233       return 1;
1234     case DBUS_TYPE_INT16:
1235     case DBUS_TYPE_UINT16:
1236       return 2;
1237     case DBUS_TYPE_BOOLEAN:
1238     case DBUS_TYPE_INT32:
1239     case DBUS_TYPE_UINT32:
1240       /* this stuff is 4 since it starts with a length */
1241     case DBUS_TYPE_STRING:
1242     case DBUS_TYPE_OBJECT_PATH:
1243     case DBUS_TYPE_ARRAY:
1244       return 4;
1245     case DBUS_TYPE_INT64:
1246     case DBUS_TYPE_UINT64:
1247     case DBUS_TYPE_DOUBLE:
1248       /* struct is 8 since it could contain an 8-aligned item
1249        * and it's simpler to just always align structs to 8;
1250        * we want the amount of padding in a struct of a given
1251        * type to be predictable, not location-dependent.
1252        * DICT_ENTRY is always the same as struct.
1253        */
1254     case DBUS_TYPE_STRUCT:
1255     case DBUS_TYPE_DICT_ENTRY:
1256       return 8;
1257
1258     default:
1259       _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1260       return 0;
1261     }
1262 }
1263
1264
1265 /**
1266  * Return #TRUE if the typecode is a valid typecode.
1267  * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1268  * random unknown bytes aren't either. This function is safe with
1269  * untrusted data.
1270  *
1271  * @returns #TRUE if valid
1272  */
1273 dbus_bool_t
1274 _dbus_type_is_valid (int typecode)
1275 {
1276   switch (typecode)
1277     {
1278     case DBUS_TYPE_BYTE:
1279     case DBUS_TYPE_BOOLEAN:
1280     case DBUS_TYPE_INT16:
1281     case DBUS_TYPE_UINT16:
1282     case DBUS_TYPE_INT32:
1283     case DBUS_TYPE_UINT32:
1284     case DBUS_TYPE_INT64:
1285     case DBUS_TYPE_UINT64:
1286     case DBUS_TYPE_DOUBLE:
1287     case DBUS_TYPE_STRING:
1288     case DBUS_TYPE_OBJECT_PATH:
1289     case DBUS_TYPE_SIGNATURE:
1290     case DBUS_TYPE_ARRAY:
1291     case DBUS_TYPE_STRUCT:
1292     case DBUS_TYPE_DICT_ENTRY:
1293     case DBUS_TYPE_VARIANT:
1294       return TRUE;
1295
1296     default:
1297       return FALSE;
1298     }
1299 }
1300
1301 /**
1302  * Tells you whether values of this type can change length if you set
1303  * them to some other value. For this purpose, you assume that the
1304  * first byte of the old and new value would be in the same location,
1305  * so alignment padding is not a factor.
1306  *
1307  * @returns #FALSE if the type can occupy different lengths
1308  */
1309 dbus_bool_t
1310 _dbus_type_is_fixed (int typecode)
1311 {
1312   switch (typecode)
1313     {
1314     case DBUS_TYPE_BYTE:
1315     case DBUS_TYPE_BOOLEAN:
1316     case DBUS_TYPE_INT16:
1317     case DBUS_TYPE_UINT16:
1318     case DBUS_TYPE_INT32:
1319     case DBUS_TYPE_UINT32:
1320     case DBUS_TYPE_INT64:
1321     case DBUS_TYPE_UINT64:
1322     case DBUS_TYPE_DOUBLE:
1323       return TRUE;
1324     default:
1325       return FALSE;
1326     }
1327 }
1328
1329 /**
1330  * Returns a string describing the given type.
1331  *
1332  * @param typecode the type to describe
1333  * @returns a constant string describing the type
1334  */
1335 const char *
1336 _dbus_type_to_string (int typecode)
1337 {
1338   switch (typecode)
1339     {
1340     case DBUS_TYPE_INVALID:
1341       return "invalid";
1342     case DBUS_TYPE_BOOLEAN:
1343       return "boolean";
1344     case DBUS_TYPE_BYTE:
1345       return "byte";
1346     case DBUS_TYPE_INT16:
1347       return "int16";
1348     case DBUS_TYPE_UINT16:
1349       return "uint16";
1350     case DBUS_TYPE_INT32:
1351       return "int32";
1352     case DBUS_TYPE_UINT32:
1353       return "uint32";
1354     case DBUS_TYPE_DOUBLE:
1355       return "double";
1356     case DBUS_TYPE_STRING:
1357       return "string";
1358     case DBUS_TYPE_OBJECT_PATH:
1359       return "object_path";
1360     case DBUS_TYPE_SIGNATURE:
1361       return "signature";
1362     case DBUS_TYPE_STRUCT:
1363       return "struct";
1364     case DBUS_TYPE_DICT_ENTRY:
1365       return "dict_entry";
1366     case DBUS_TYPE_ARRAY:
1367       return "array";
1368     case DBUS_TYPE_VARIANT:
1369       return "variant";
1370     case DBUS_STRUCT_BEGIN_CHAR:
1371       return "begin_struct";
1372     case DBUS_STRUCT_END_CHAR:
1373       return "end_struct";
1374     case DBUS_DICT_ENTRY_BEGIN_CHAR:
1375       return "begin_dict_entry";
1376     case DBUS_DICT_ENTRY_END_CHAR:
1377       return "end_dict_entry";
1378     default:
1379       return "unknown";
1380     }
1381 }
1382
1383 /**
1384  * If in verbose mode, print a block of binary data.
1385  *
1386  * @todo right now it prints even if not in verbose mode
1387  *
1388  * @param data the data
1389  * @param len the length of the data
1390  * @param offset where to start counting for byte indexes
1391  */
1392 void
1393 _dbus_verbose_bytes (const unsigned char *data,
1394                      int                  len,
1395                      int                  offset)
1396 {
1397   int i;
1398   const unsigned char *aligned;
1399
1400   _dbus_assert (len >= 0);
1401
1402   /* Print blanks on first row if appropriate */
1403   aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1404   if (aligned > data)
1405     aligned -= 4;
1406   _dbus_assert (aligned <= data);
1407
1408   if (aligned != data)
1409     {
1410       _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1411       while (aligned != data)
1412         {
1413           _dbus_verbose ("    ");
1414           ++aligned;
1415         }
1416     }
1417
1418   /* now print the bytes */
1419   i = 0;
1420   while (i < len)
1421     {
1422       if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1423         {
1424           _dbus_verbose ("%4d\t%p: ",
1425                          offset + i, &data[i]);
1426         }
1427
1428       if (data[i] >= 32 &&
1429           data[i] <= 126)
1430         _dbus_verbose (" '%c' ", data[i]);
1431       else
1432         _dbus_verbose ("0x%s%x ",
1433                        data[i] <= 0xf ? "0" : "", data[i]);
1434
1435       ++i;
1436
1437       if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1438         {
1439           if (i > 3)
1440             _dbus_verbose ("BE: %d LE: %d",
1441                            _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1442                            _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1443
1444           if (i > 7 &&
1445               _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1446             {
1447 #ifdef DBUS_HAVE_INT64
1448               /* I think I probably mean "GNU libc printf" and not "GNUC"
1449                * but we'll wait until someone complains. If you hit this,
1450                * just turn off verbose mode as a workaround.
1451                */
1452 #if __GNUC__
1453               _dbus_verbose (" u64: 0x%llx",
1454                              *(dbus_uint64_t*)&data[i-8]);
1455 #endif
1456 #endif
1457               _dbus_verbose (" dbl: %g",
1458                              *(double*)&data[i-8]);
1459             }
1460
1461           _dbus_verbose ("\n");
1462         }
1463     }
1464
1465   _dbus_verbose ("\n");
1466 }
1467
1468 /**
1469  * Dump the given part of the string to verbose log.
1470  *
1471  * @param str the string
1472  * @param start the start of range to dump
1473  * @param len length of range
1474  */
1475 void
1476 _dbus_verbose_bytes_of_string (const DBusString    *str,
1477                                int                  start,
1478                                int                  len)
1479 {
1480   const char *d;
1481   int real_len;
1482
1483   real_len = _dbus_string_get_length (str);
1484
1485   _dbus_assert (start >= 0);
1486
1487   if (start > real_len)
1488     {
1489       _dbus_verbose ("  [%d,%d) is not inside string of length %d\n",
1490                      start, len, real_len);
1491       return;
1492     }
1493
1494   if ((start + len) > real_len)
1495     {
1496       _dbus_verbose ("  [%d,%d) extends outside string of length %d\n",
1497                      start, len, real_len);
1498       len = real_len - start;
1499     }
1500
1501   d = _dbus_string_get_const_data_len (str, start, len);
1502
1503   _dbus_verbose_bytes (d, len, start);
1504 }
1505
1506 static int
1507 map_type_char_to_type (int t)
1508 {
1509   if (t == DBUS_STRUCT_BEGIN_CHAR)
1510     return DBUS_TYPE_STRUCT;
1511   else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1512     return DBUS_TYPE_DICT_ENTRY;
1513   else
1514     {
1515       _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1516       _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1517       return t;
1518     }
1519 }
1520
1521 /**
1522  * Get the first type in the signature. The difference between this
1523  * and just getting the first byte of the signature is that you won't
1524  * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1525  * instead.
1526  *
1527  * @param str string containing signature
1528  * @param pos where the signature starts
1529  * @returns the first type in the signature
1530  */
1531 int
1532 _dbus_first_type_in_signature (const DBusString *str,
1533                                int               pos)
1534 {
1535   return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1536 }
1537
1538 /**
1539  * Similar to #_dbus_first_type_in_signature, but operates
1540  * on a C string buffer.
1541  *
1542  * @param str a C string buffer
1543  * @param pos where the signature starts
1544  * @returns the first type in the signature
1545  */
1546 int
1547 _dbus_first_type_in_signature_c_str (const char       *str,
1548                                      int               pos)
1549 {
1550   return map_type_char_to_type (str[pos]);
1551 }
1552
1553 /** @} */
1554
1555 #ifdef DBUS_BUILD_TESTS
1556 #include "dbus-test.h"
1557 #include <stdio.h>
1558
1559 static void
1560 swap_test_array (void *array,
1561                  int   len_bytes,
1562                  int   byte_order,
1563                  int   alignment)
1564 {
1565   DBusString t;
1566
1567   if (alignment == 1)
1568     return;
1569   
1570   _dbus_string_init_const_len (&t, array, len_bytes);
1571   swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1572 }
1573
1574 #define MARSHAL_BASIC(typename, byte_order, literal)                    \
1575   do {                                                                  \
1576      v_##typename = literal;                                            \
1577      if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename,   \
1578                                     &v_##typename,                      \
1579                                     byte_order, NULL))                  \
1580        _dbus_assert_not_reached ("no memory");                          \
1581    } while (0)
1582
1583 #define DEMARSHAL_BASIC(typename, byte_order)                                   \
1584   do {                                                                          \
1585     _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename,   \
1586                               byte_order, &pos);                                \
1587   } while (0)
1588
1589 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal)                        \
1590   do {                                                                                  \
1591     DEMARSHAL_BASIC (typename, byte_order);                                             \
1592     if (literal != v_##typename)                                                        \
1593       {                                                                                 \
1594         _dbus_verbose_bytes_of_string (&str, dump_pos,                                  \
1595                                      _dbus_string_get_length (&str) - dump_pos);        \
1596         _dbus_assert_not_reached ("demarshaled wrong value");                           \
1597       }                                                                                 \
1598   } while (0)
1599
1600 #define MARSHAL_TEST(typename, byte_order, literal)             \
1601   do {                                                          \
1602     MARSHAL_BASIC (typename, byte_order, literal);              \
1603     dump_pos = pos;                                             \
1604     DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal);  \
1605   } while (0)
1606
1607 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal)                              \
1608   do {                                                                                  \
1609     MARSHAL_BASIC (typename, byte_order, literal);                                      \
1610     dump_pos = pos;                                                                     \
1611     DEMARSHAL_BASIC (typename, byte_order);                                             \
1612     if (strcmp (literal, v_##typename) != 0)                                            \
1613       {                                                                                 \
1614         _dbus_verbose_bytes_of_string (&str, dump_pos,                                  \
1615                                        _dbus_string_get_length (&str) - dump_pos);      \
1616         _dbus_warn ("literal '%s'\nvalue  '%s'\n", literal, v_##typename);              \
1617         _dbus_assert_not_reached ("demarshaled wrong value");                           \
1618       }                                                                                 \
1619   } while (0)
1620
1621 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal)                                      \
1622   do {                                                                                          \
1623      int next;                                                                                  \
1624      v_UINT32 = sizeof(literal);                                                                \
1625      if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32,                    \
1626                                      byte_order, &next))                                        \
1627        _dbus_assert_not_reached ("no memory");                                                  \
1628      v_ARRAY_##typename = literal;                                                              \
1629      if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename,                    \
1630                                            &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal),      \
1631                                            byte_order, NULL))                                   \
1632        _dbus_assert_not_reached ("no memory");                                                  \
1633    } while (0)
1634
1635 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order)                                             \
1636   do {                                                                                          \
1637     int next;                                                                                   \
1638     alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename);                                \
1639     v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next);                   \
1640     _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename,      \
1641                                     v_UINT32/alignment,                                         \
1642                                     byte_order, NULL);                                          \
1643     swap_test_array (v_ARRAY_##typename, v_UINT32,                                              \
1644                      byte_order, alignment);                                                    \
1645   } while (0)
1646
1647 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal)                  \
1648   do {                                                                                  \
1649     DEMARSHAL_FIXED_ARRAY (typename, byte_order);                                       \
1650     if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0))                    \
1651       {                                                                                 \
1652         _dbus_verbose ("MARSHALED DATA\n");                                             \
1653         _dbus_verbose_bytes_of_string (&str, dump_pos,                                  \
1654                                       _dbus_string_get_length (&str) - dump_pos);       \
1655         _dbus_verbose ("LITERAL DATA\n");                                               \
1656         _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0);                      \
1657         _dbus_verbose ("READ DATA\n");                                                  \
1658         _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0);           \
1659         _dbus_assert_not_reached ("demarshaled wrong fixed array value");               \
1660       }                                                                                 \
1661   } while (0)
1662
1663 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal)         \
1664   do {                                                                  \
1665     MARSHAL_FIXED_ARRAY (typename, byte_order, literal);                \
1666     dump_pos = pos;                                                     \
1667     DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal);    \
1668   } while (0)
1669
1670 dbus_bool_t
1671 _dbus_marshal_test (void)
1672 {
1673   int alignment;
1674   DBusString str;
1675   int pos, dump_pos;
1676   unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1677   dbus_int16_t array2[3] = { 124, 457, 780 };
1678   dbus_int32_t array4[3] = { 123, 456, 789 };
1679 #ifdef DBUS_HAVE_INT64
1680   dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1681                              DBUS_INT64_CONSTANT (0x456ffffffff),
1682                              DBUS_INT64_CONSTANT (0x789ffffffff) };
1683   dbus_int64_t *v_ARRAY_INT64;
1684 #endif
1685   unsigned char *v_ARRAY_BYTE;
1686   dbus_int16_t *v_ARRAY_INT16;
1687   dbus_uint16_t *v_ARRAY_UINT16;
1688   dbus_int32_t *v_ARRAY_INT32;
1689   dbus_uint32_t *v_ARRAY_UINT32;
1690   double *v_ARRAY_DOUBLE;
1691   DBusString t;
1692   double v_DOUBLE;
1693   double t_DOUBLE;
1694   dbus_int16_t v_INT16;
1695   dbus_uint16_t v_UINT16;
1696   dbus_int32_t v_INT32;
1697   dbus_uint32_t v_UINT32;
1698   dbus_int64_t v_INT64;
1699   dbus_uint64_t v_UINT64;
1700   unsigned char v_BYTE;
1701   dbus_bool_t v_BOOLEAN;
1702   const char *v_STRING;
1703   const char *v_SIGNATURE;
1704   const char *v_OBJECT_PATH;
1705   int byte_order;
1706
1707   if (!_dbus_string_init (&str))
1708     _dbus_assert_not_reached ("failed to init string");
1709
1710   pos = 0;
1711
1712   /* Marshal doubles */
1713   MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1714   DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1715   t_DOUBLE = 3.14;
1716   if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1717     _dbus_assert_not_reached ("got wrong double value");
1718
1719   MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1720   DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1721   t_DOUBLE = 3.14;
1722   if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1723     _dbus_assert_not_reached ("got wrong double value");
1724
1725   /* Marshal signed 16 integers */
1726   MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1727   MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1728
1729   /* Marshal unsigned 16 integers */
1730   MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1731   MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1732   
1733   /* Marshal signed integers */
1734   MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1735   MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1736
1737   /* Marshal unsigned integers */
1738   MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1739   MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1740
1741 #ifdef DBUS_HAVE_INT64
1742   /* Marshal signed integers */
1743   MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1744   MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1745
1746   /* Marshal unsigned integers */
1747   MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1748   MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1749 #endif /* DBUS_HAVE_INT64 */
1750
1751   /* Marshal byte */
1752   MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1753   MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1754
1755   /* Marshal all possible bools! */
1756   MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1757   MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1758   MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1759   MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1760
1761   /* Marshal strings */
1762   MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1763   MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1764   MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1765   MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1766
1767   /* object paths */
1768   MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1769   MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1770
1771   /* signatures */
1772   MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1773   MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1774   MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1775   MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1776
1777   /* Arrays */
1778   MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1779   MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1780   MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2);
1781   MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2);
1782   
1783   MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1784   MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1785   MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4);
1786   MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4);
1787
1788   MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1789   MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1790   
1791 #ifdef DBUS_HAVE_INT64
1792   MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1793   MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1794 #endif
1795
1796 #if 0
1797
1798   /*
1799    * FIXME restore the set/pack tests
1800    */
1801
1802 #ifdef DBUS_HAVE_INT64
1803   /* set/pack 64-bit integers */
1804   _dbus_string_set_length (&str, 8);
1805
1806   /* signed little */
1807   _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1808                            0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1809
1810   _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1811                 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1812                                     _dbus_string_get_const_data (&str)));
1813
1814   /* signed big */
1815   _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1816                            0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1817
1818   _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1819                 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1820                                     _dbus_string_get_const_data (&str)));
1821
1822   /* signed little pack */
1823   _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1824                     DBUS_LITTLE_ENDIAN,
1825                     _dbus_string_get_data (&str));
1826
1827   _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1828                 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1829                                     _dbus_string_get_const_data (&str)));
1830
1831   /* signed big pack */
1832   _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1833                     DBUS_BIG_ENDIAN,
1834                     _dbus_string_get_data (&str));
1835
1836   _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1837                 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1838                                     _dbus_string_get_const_data (&str)));
1839
1840   /* unsigned little */
1841   _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1842                             0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1843
1844   _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1845                 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1846                                      _dbus_string_get_const_data (&str)));
1847
1848   /* unsigned big */
1849   _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1850                             0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1851
1852   _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1853                 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1854                                      _dbus_string_get_const_data (&str)));
1855
1856   /* unsigned little pack */
1857   _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1858                      DBUS_LITTLE_ENDIAN,
1859                      _dbus_string_get_data (&str));
1860
1861   _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1862                 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1863                                      _dbus_string_get_const_data (&str)));
1864
1865   /* unsigned big pack */
1866   _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1867                      DBUS_BIG_ENDIAN,
1868                      _dbus_string_get_data (&str));
1869
1870   _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1871                 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1872                                      _dbus_string_get_const_data (&str)));
1873 #endif /* DBUS_HAVE_INT64 */
1874
1875   /* set/pack 32-bit integers */
1876   _dbus_string_set_length (&str, 4);
1877
1878   /* signed little */
1879   _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1880                            0, -0x123456);
1881
1882   _dbus_assert (-0x123456 ==
1883                 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1884                                     _dbus_string_get_const_data (&str)));
1885
1886   /* signed big */
1887   _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1888                            0, -0x123456);
1889
1890   _dbus_assert (-0x123456 ==
1891                 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1892                                     _dbus_string_get_const_data (&str)));
1893
1894   /* signed little pack */
1895   _dbus_pack_int32 (-0x123456,
1896                     DBUS_LITTLE_ENDIAN,
1897                     _dbus_string_get_data (&str));
1898
1899   _dbus_assert (-0x123456 ==
1900                 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1901                                     _dbus_string_get_const_data (&str)));
1902
1903   /* signed big pack */
1904   _dbus_pack_int32 (-0x123456,
1905                     DBUS_BIG_ENDIAN,
1906                     _dbus_string_get_data (&str));
1907
1908   _dbus_assert (-0x123456 ==
1909                 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1910                                     _dbus_string_get_const_data (&str)));
1911
1912   /* unsigned little */
1913   _dbus_marshal_set_uint32 (&str,
1914                             0, 0x123456,
1915                             DBUS_LITTLE_ENDIAN);
1916
1917   _dbus_assert (0x123456 ==
1918                 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1919                                      _dbus_string_get_const_data (&str)));
1920
1921   /* unsigned big */
1922   _dbus_marshal_set_uint32 (&str,
1923                             0, 0x123456,
1924                             DBUS_BIG_ENDIAN);
1925
1926   _dbus_assert (0x123456 ==
1927                 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1928                                      _dbus_string_get_const_data (&str)));
1929
1930   /* unsigned little pack */
1931   _dbus_pack_uint32 (0x123456,
1932                      DBUS_LITTLE_ENDIAN,
1933                      _dbus_string_get_data (&str));
1934
1935   _dbus_assert (0x123456 ==
1936                 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1937                                      _dbus_string_get_const_data (&str)));
1938
1939   /* unsigned big pack */
1940   _dbus_pack_uint32 (0x123456,
1941                      DBUS_BIG_ENDIAN,
1942                      _dbus_string_get_data (&str));
1943
1944   _dbus_assert (0x123456 ==
1945                 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1946                                      _dbus_string_get_const_data (&str)));
1947
1948 #endif /* set/pack tests for integers */
1949
1950   /* Strings in-place set */
1951   byte_order = DBUS_LITTLE_ENDIAN;
1952   while (TRUE)
1953     {
1954       /* Init a string */
1955       _dbus_string_set_length (&str, 0);
1956
1957       /* reset pos for the macros */
1958       pos = 0;
1959
1960       MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1961
1962       /* Set it to something longer */
1963       _dbus_string_init_const (&t, "Hello world foo");
1964
1965       v_STRING = _dbus_string_get_const_data (&t);
1966       _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1967                                &v_STRING, byte_order, NULL, NULL);
1968
1969       _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1970                                 &v_STRING, byte_order,
1971                                 NULL);
1972       _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1973
1974       /* Set it to something shorter */
1975       _dbus_string_init_const (&t, "Hello");
1976
1977       v_STRING = _dbus_string_get_const_data (&t);
1978       _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1979                                &v_STRING, byte_order, NULL, NULL);
1980       _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1981                                 &v_STRING, byte_order,
1982                                 NULL);
1983       _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1984
1985       /* Do the other byte order */
1986       if (byte_order == DBUS_LITTLE_ENDIAN)
1987         byte_order = DBUS_BIG_ENDIAN;
1988       else
1989         break;
1990     }
1991
1992   /* Clean up */
1993   _dbus_string_free (&str);
1994
1995   return TRUE;
1996 }
1997
1998 #endif /* DBUS_BUILD_TESTS */