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