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