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