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