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