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