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