1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-basic.c Marshalling routines for basic (primitive) types
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
26 #include "dbus-internals.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-signature.h"
32 #if defined(__GNUC__) && (__GNUC__ >= 4)
33 # define _DBUS_ASSERT_ALIGNMENT(type, op, val) \
34 _DBUS_STATIC_ASSERT (__extension__ __alignof__ (type) op val)
36 # define _DBUS_ASSERT_ALIGNMENT(type, op, val) do { } while (0)
39 /* True by definition, but just for completeness... */
40 _DBUS_STATIC_ASSERT (sizeof (char) == 1);
41 _DBUS_ASSERT_ALIGNMENT (char, ==, 1);
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);
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);
55 _DBUS_STATIC_ASSERT (sizeof (double) == 8);
56 _DBUS_ASSERT_ALIGNMENT (double, <=, 8);
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);
66 * @defgroup DBusMarshal marshaling and unmarshaling
67 * @ingroup DBusInternals
68 * @brief functions to marshal/unmarshal data from the wire
70 * Types and functions related to converting primitive data types from
71 * wire format to native machine format, and vice versa.
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; }
81 pack_2_octets (dbus_uint16_t value,
85 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
87 if ((byte_order) == DBUS_LITTLE_ENDIAN)
88 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
90 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
94 pack_4_octets (dbus_uint32_t value,
98 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
100 if ((byte_order) == DBUS_LITTLE_ENDIAN)
101 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
103 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
107 pack_8_octets (DBusBasicValue value,
111 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
113 #ifdef DBUS_HAVE_INT64
114 if ((byte_order) == DBUS_LITTLE_ENDIAN)
115 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
117 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
119 *(DBus8ByteStruct*)data = value.u64;
120 swap_8_octets ((DBusBasicValue*)data, byte_order);
125 * Packs a 32 bit unsigned integer into a data pointer.
127 * @param value the value
128 * @param byte_order the byte order to use
129 * @param data the data pointer
132 _dbus_pack_uint32 (dbus_uint32_t value,
136 pack_4_octets (value, byte_order, data);
139 #ifndef DBUS_HAVE_INT64
142 swap_bytes (unsigned char *data,
145 unsigned char *p1 = data;
146 unsigned char *p2 = data + len - 1;
150 unsigned char tmp = *p1;
158 #endif /* !DBUS_HAVE_INT64 */
161 swap_8_octets (DBusBasicValue *value,
164 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
166 #ifdef DBUS_HAVE_INT64
167 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
169 swap_bytes ((unsigned char *)value, 8);
175 static DBusBasicValue
176 unpack_8_octets (int byte_order,
177 const unsigned char *data)
181 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
182 _dbus_assert (sizeof (r) == 8);
184 #ifdef DBUS_HAVE_INT64
185 if (byte_order == DBUS_LITTLE_ENDIAN)
186 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
188 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
190 r.u64 = *(DBus8ByteStruct*)data;
191 swap_8_octets (&r, byte_order);
198 #ifndef _dbus_unpack_uint16
200 * Unpacks a 16 bit unsigned integer from a data pointer
202 * @param byte_order The byte order to use
203 * @param data the data pointer
204 * @returns the integer
207 _dbus_unpack_uint16 (int byte_order,
208 const unsigned char *data)
210 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
212 if (byte_order == DBUS_LITTLE_ENDIAN)
213 return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
215 return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
217 #endif /* _dbus_unpack_uint16 */
219 #ifndef _dbus_unpack_uint32
221 * Unpacks a 32 bit unsigned integer from a data pointer
223 * @param byte_order The byte order to use
224 * @param data the data pointer
225 * @returns the integer
228 _dbus_unpack_uint32 (int byte_order,
229 const unsigned char *data)
231 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
233 if (byte_order == DBUS_LITTLE_ENDIAN)
234 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
236 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
238 #endif /* _dbus_unpack_uint32 */
241 set_2_octets (DBusString *str,
248 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
249 byte_order == DBUS_BIG_ENDIAN);
251 data = _dbus_string_get_data_len (str, offset, 2);
253 pack_2_octets (value, byte_order, data);
257 set_4_octets (DBusString *str,
264 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
265 byte_order == DBUS_BIG_ENDIAN);
267 data = _dbus_string_get_data_len (str, offset, 4);
269 pack_4_octets (value, byte_order, data);
273 set_8_octets (DBusString *str,
275 DBusBasicValue value,
280 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
281 byte_order == DBUS_BIG_ENDIAN);
283 data = _dbus_string_get_data_len (str, offset, 8);
285 pack_8_octets (value, byte_order, data);
289 * Sets the 4 bytes at the given offset to a marshaled unsigned
290 * integer, replacing anything found there previously.
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
299 _dbus_marshal_set_uint32 (DBusString *str,
304 set_4_octets (str, pos, value, byte_order);
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.
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.
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
327 set_string (DBusString *str,
334 int old_len, new_len;
337 _dbus_string_init_const (&dstr, value);
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));
343 new_len = _dbus_string_get_length (&dstr);
345 if (!_dbus_string_replace_len (&dstr, 0, new_len,
346 str, pos + 4, old_len))
349 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
352 *old_end_pos = pos + 4 + old_len + 1;
354 *new_end_pos = pos + 4 + new_len + 1;
360 * Sets the existing marshaled signature at the given offset to a new
361 * marshaled signature. Same basic ideas as set_string().
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
373 set_signature (DBusString *str,
380 int old_len, new_len;
383 _dbus_string_init_const (&dstr, value);
385 old_len = _dbus_string_get_byte (str, pos);
386 new_len = _dbus_string_get_length (&dstr);
388 if (!_dbus_string_replace_len (&dstr, 0, new_len,
389 str, pos + 1, old_len))
392 _dbus_string_set_byte (str, pos, new_len);
395 *old_end_pos = pos + 1 + old_len + 1;
397 *new_end_pos = pos + 1 + new_len + 1;
403 * Sets an existing basic type value to a new value.
404 * Arguments work the same way as _dbus_marshal_basic_type().
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
416 _dbus_marshal_set_basic (DBusString *str,
424 const DBusBasicValue *vp;
431 _dbus_string_set_byte (str, pos, vp->byt);
433 *old_end_pos = pos + 1;
435 *new_end_pos = pos + 1;
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);
443 *old_end_pos = pos + 2;
445 *new_end_pos = pos + 2;
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);
455 *old_end_pos = pos + 4;
457 *new_end_pos = pos + 4;
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);
466 *old_end_pos = pos + 8;
468 *new_end_pos = pos + 8;
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);
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);
484 _dbus_assert_not_reached ("not a basic type");
491 * Convenience function to demarshal a 32 bit unsigned integer.
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.
500 _dbus_marshal_read_uint32 (const DBusString *str,
505 pos = _DBUS_ALIGN_VALUE (pos, 4);
510 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
512 return _dbus_unpack_uint32 (byte_order,
513 _dbus_string_get_const_data (str) + pos);
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
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.
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
538 _dbus_marshal_read_basic (const DBusString *str,
545 const char *str_data;
547 _dbus_assert (dbus_type_is_basic (type));
549 str_data = _dbus_string_get_const_data (str);
551 /* Below we volatile types to avoid aliasing issues;
552 * see http://bugs.freedesktop.org/show_bug.cgi?id=20137
559 volatile unsigned char *vp = value;
560 *vp = (unsigned char) _dbus_string_get_byte (str, pos);
564 case DBUS_TYPE_INT16:
565 case DBUS_TYPE_UINT16:
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);
575 case DBUS_TYPE_INT32:
576 case DBUS_TYPE_UINT32:
577 case DBUS_TYPE_BOOLEAN:
578 case DBUS_TYPE_UNIX_FD:
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);
588 case DBUS_TYPE_INT64:
589 case DBUS_TYPE_UINT64:
590 case DBUS_TYPE_DOUBLE:
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));
598 *vp = *(dbus_uint64_t*)(str_data + pos);
600 *vp = *(DBus8ByteStruct*) (str_data + pos);
601 swap_8_octets (vp, byte_order);
606 case DBUS_TYPE_STRING:
607 case DBUS_TYPE_OBJECT_PATH:
610 volatile char **vp = value;
612 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
614 *vp = (char*) str_data + pos;
616 pos += len + 1; /* length plus nul */
619 case DBUS_TYPE_SIGNATURE:
622 volatile char **vp = value;
624 len = _dbus_string_get_byte (str, pos);
627 *vp = (char*) str_data + pos;
629 pos += len + 1; /* length plus nul */
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");
644 marshal_2_octets (DBusString *str,
653 _dbus_assert (sizeof (value) == 2);
655 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
656 value = DBUS_UINT16_SWAP_LE_BE (value);
658 orig_len = _dbus_string_get_length (str);
660 retval = _dbus_string_insert_2_aligned (str, insert_at,
661 (const unsigned char *)&value);
665 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
666 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
673 marshal_4_octets (DBusString *str,
682 _dbus_assert (sizeof (value) == 4);
684 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
685 value = DBUS_UINT32_SWAP_LE_BE (value);
687 orig_len = _dbus_string_get_length (str);
689 retval = _dbus_string_insert_4_aligned (str, insert_at,
690 (const unsigned char *)&value);
694 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
695 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
702 marshal_8_octets (DBusString *str,
704 DBusBasicValue value,
711 _dbus_assert (sizeof (value) == 8);
713 swap_8_octets (&value, byte_order);
715 orig_len = _dbus_string_get_length (str);
717 retval = _dbus_string_insert_8_aligned (str, insert_at,
718 (const unsigned char *)&value);
721 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
729 MARSHAL_AS_SIGNATURE,
730 MARSHAL_AS_BYTE_ARRAY
734 marshal_len_followed_by_bytes (int marshal_as,
737 const unsigned char *value,
738 int data_len, /* doesn't include nul if any */
743 DBusString value_str;
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);
751 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
752 value_len = data_len;
754 value_len = data_len + 1; /* value has a nul */
756 _dbus_string_init_const_len (&value_str, value, value_len);
760 if (marshal_as == MARSHAL_AS_SIGNATURE)
762 _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
763 _dbus_assert (data_len <= 255); /* same as max sig len right now */
765 if (!_dbus_string_insert_byte (str, pos, data_len))
772 if (!marshal_4_octets (str, pos, data_len,
777 if (!_dbus_string_copy_len (&value_str, 0, value_len,
783 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
785 _dbus_verbose_bytes_of_string (str, pos, value_len);
796 /* Delete what we've inserted */
797 _dbus_string_delete (str, insert_at, pos - insert_at);
803 marshal_string (DBusString *str,
809 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
810 str, insert_at, value,
812 byte_order, pos_after);
816 marshal_signature (DBusString *str,
821 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
822 str, insert_at, value,
824 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
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
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
845 _dbus_marshal_write_basic (DBusString *str,
852 const DBusBasicValue *vp;
854 _dbus_assert (dbus_type_is_basic (type));
861 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
864 *pos_after = insert_at + 1;
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);
872 case DBUS_TYPE_BOOLEAN:
873 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
874 byte_order, pos_after);
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);
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);
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);
893 case DBUS_TYPE_SIGNATURE:
894 _dbus_assert (vp->str != NULL);
895 return marshal_signature (str, insert_at, vp->str, pos_after);
898 _dbus_assert_not_reached ("not a basic type");
905 marshal_1_octets_array (DBusString *str,
907 const unsigned char *value,
913 DBusString value_str;
915 _dbus_string_init_const_len (&value_str, value, n_elements);
919 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
932 * Swaps the elements of an array to the opposite byte order
934 * @param data start of array
935 * @param n_elements number of elements
936 * @param alignment size of each element
939 _dbus_swap_array (unsigned char *data,
946 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
948 /* we use const_data and cast it off so DBusString can be a const string
949 * for the unit tests. don't ask.
952 end = d + (n_elements * alignment);
958 #ifdef DBUS_HAVE_INT64
959 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
961 swap_8_bytes ((DBusBasicValue*) d);
966 else if (alignment == 4)
970 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
976 _dbus_assert (alignment == 2);
980 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
987 swap_array (DBusString *str,
993 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
995 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
997 /* we use const_data and cast it off so DBusString can be a const string
998 * for the unit tests. don't ask.
1000 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
1001 n_elements, alignment);
1006 marshal_fixed_multi (DBusString *str,
1008 const DBusBasicValue *value,
1019 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
1021 old_string_len = _dbus_string_get_length (str);
1023 len_in_bytes = n_elements * alignment;
1024 array_start = insert_at;
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
1032 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1035 _dbus_string_init_const_len (&t,
1036 (const unsigned char*) value,
1039 if (!_dbus_string_copy (&t, 0,
1043 swap_array (str, array_start, n_elements, byte_order, alignment);
1046 *pos_after = array_start + len_in_bytes;
1051 _dbus_string_delete (str, insert_at,
1052 _dbus_string_get_length (str) - old_string_len);
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.
1062 * The value argument should be the adddress of an
1063 * array, so e.g. "const dbus_uint32_t**"
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
1075 _dbus_marshal_write_fixed_multi (DBusString *str,
1083 const void* vp = *(const DBusBasicValue**)value;
1085 _dbus_assert (dbus_type_is_fixed (element_type));
1086 _dbus_assert (n_elements >= 0);
1089 _dbus_verbose ("writing %d elements of %s\n",
1090 n_elements, _dbus_type_to_string (element_type));
1093 switch (element_type)
1095 case DBUS_TYPE_BYTE:
1096 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
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);
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);
1114 _dbus_assert_not_reached ("non fixed type in array write");
1123 * Skips over a basic-typed value, reporting the following position.
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
1132 _dbus_marshal_skip_basic (const DBusString *str,
1137 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1138 byte_order == DBUS_BIG_ENDIAN);
1142 case DBUS_TYPE_BYTE:
1145 case DBUS_TYPE_INT16:
1146 case DBUS_TYPE_UINT16:
1147 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
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);
1157 case DBUS_TYPE_INT64:
1158 case DBUS_TYPE_UINT64:
1159 case DBUS_TYPE_DOUBLE:
1160 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1163 case DBUS_TYPE_STRING:
1164 case DBUS_TYPE_OBJECT_PATH:
1168 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1170 *pos += len + 1; /* length plus nul */
1173 case DBUS_TYPE_SIGNATURE:
1177 len = _dbus_string_get_byte (str, *pos);
1179 *pos += len + 2; /* length byte plus length plus nul */
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");
1191 * Skips an array, returning the next position.
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
1200 _dbus_marshal_skip_array (const DBusString *str,
1205 dbus_uint32_t array_len;
1209 i = _DBUS_ALIGN_VALUE (*pos, 4);
1211 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1213 alignment = _dbus_type_get_alignment (element_type);
1215 i = _DBUS_ALIGN_VALUE (i, alignment);
1217 *pos = i + array_len;
1221 * Gets the alignment requirement for the given type;
1222 * will be 1, 4, or 8.
1224 * @param typecode the type
1225 * @returns alignment of 1, 4, or 8
1228 _dbus_type_get_alignment (int typecode)
1232 case DBUS_TYPE_BYTE:
1233 case DBUS_TYPE_VARIANT:
1234 case DBUS_TYPE_SIGNATURE:
1236 case DBUS_TYPE_INT16:
1237 case DBUS_TYPE_UINT16:
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:
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.
1257 case DBUS_TYPE_STRUCT:
1258 case DBUS_TYPE_DICT_ENTRY:
1262 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
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
1274 * @returns #TRUE if valid
1277 _dbus_type_is_valid (int typecode)
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:
1306 * Returns a string describing the given type.
1308 * @param typecode the type to describe
1309 * @returns a constant string describing the type
1312 _dbus_type_to_string (int typecode)
1316 case DBUS_TYPE_INVALID:
1318 case DBUS_TYPE_BOOLEAN:
1320 case DBUS_TYPE_BYTE:
1322 case DBUS_TYPE_INT16:
1324 case DBUS_TYPE_UINT16:
1326 case DBUS_TYPE_INT32:
1328 case DBUS_TYPE_UINT32:
1330 case DBUS_TYPE_INT64:
1332 case DBUS_TYPE_UINT64:
1334 case DBUS_TYPE_DOUBLE:
1336 case DBUS_TYPE_STRING:
1338 case DBUS_TYPE_OBJECT_PATH:
1339 return "object_path";
1340 case DBUS_TYPE_SIGNATURE:
1342 case DBUS_TYPE_STRUCT:
1344 case DBUS_TYPE_DICT_ENTRY:
1345 return "dict_entry";
1346 case DBUS_TYPE_ARRAY:
1348 case DBUS_TYPE_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:
1366 * If in verbose mode, print a block of binary data.
1368 * @param data the data
1369 * @param len the length of the data
1370 * @param offset where to start counting for byte indexes
1373 _dbus_verbose_bytes (const unsigned char *data,
1378 const unsigned char *aligned;
1380 _dbus_assert (len >= 0);
1382 if (!_dbus_is_verbose())
1385 /* Print blanks on first row if appropriate */
1386 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1389 _dbus_assert (aligned <= data);
1391 if (aligned != data)
1393 _dbus_verbose ("%4ld\t%p: ", - (long)(data - aligned), aligned);
1394 while (aligned != data)
1396 _dbus_verbose (" ");
1401 /* now print the bytes */
1405 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1407 _dbus_verbose ("%4d\t%p: ",
1408 offset + i, &data[i]);
1411 if (data[i] >= 32 &&
1413 _dbus_verbose (" '%c' ", data[i]);
1415 _dbus_verbose ("0x%s%x ",
1416 data[i] <= 0xf ? "0" : "", data[i]);
1420 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
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]));
1428 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1430 #ifdef DBUS_INT64_PRINTF_MODIFIER
1431 _dbus_verbose (" u64: 0x%" DBUS_INT64_PRINTF_MODIFIER "x",
1432 *(dbus_uint64_t*)&data[i-8]);
1434 _dbus_verbose (" dbl: %g",
1435 *(double*)&data[i-8]);
1438 _dbus_verbose ("\n");
1442 _dbus_verbose ("\n");
1446 * Dump the given part of the string to verbose log.
1448 * @param str the string
1449 * @param start the start of range to dump
1450 * @param len length of range
1453 _dbus_verbose_bytes_of_string (const DBusString *str,
1460 real_len = _dbus_string_get_length (str);
1462 _dbus_assert (start >= 0);
1464 if (start > real_len)
1466 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1467 start, len, real_len);
1471 if ((start + len) > real_len)
1473 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1474 start, len, real_len);
1475 len = real_len - start;
1478 d = _dbus_string_get_const_data_len (str, start, len);
1480 _dbus_verbose_bytes (d, len, start);
1484 map_type_char_to_type (int t)
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;
1492 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1493 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
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
1504 * @param str string containing signature
1505 * @param pos where the signature starts
1506 * @returns the first type in the signature
1509 _dbus_first_type_in_signature (const DBusString *str,
1512 return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1516 * Similar to #_dbus_first_type_in_signature, but operates
1517 * on a C string buffer.
1519 * @param str a C string buffer
1520 * @param pos where the signature starts
1521 * @returns the first type in the signature
1524 _dbus_first_type_in_signature_c_str (const char *str,
1527 return map_type_char_to_type (str[pos]);
1532 #ifdef DBUS_BUILD_TESTS
1533 #include "dbus-test.h"
1537 * Reads a block of fixed-length basic values, as an optimization
1538 * vs. reading each one individually into a new buffer.
1540 * This function returns the data in-place; it does not make a copy,
1541 * and it does not swap the bytes.
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.
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
1555 _dbus_marshal_read_fixed_multi (const DBusString *str,
1566 _dbus_assert (dbus_type_is_fixed (element_type));
1567 _dbus_assert (dbus_type_is_basic (element_type));
1570 _dbus_verbose ("reading %d elements of %s\n",
1571 n_elements, _dbus_type_to_string (element_type));
1574 alignment = _dbus_type_get_alignment (element_type);
1576 pos = _DBUS_ALIGN_VALUE (pos, alignment);
1578 array_len = n_elements * alignment;
1580 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
1582 *new_pos = pos + array_len;
1586 swap_test_array (void *array,
1596 _dbus_string_init_const_len (&t, array, len_bytes);
1597 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1600 #define MARSHAL_BASIC(typename, byte_order, literal) \
1602 v_##typename = literal; \
1603 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1605 byte_order, NULL)) \
1606 _dbus_assert_not_reached ("no memory"); \
1609 #define DEMARSHAL_BASIC(typename, byte_order) \
1611 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1612 byte_order, &pos); \
1615 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1617 DEMARSHAL_BASIC (typename, byte_order); \
1618 if (literal != v_##typename) \
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"); \
1626 #define MARSHAL_TEST(typename, byte_order, literal) \
1628 MARSHAL_BASIC (typename, byte_order, literal); \
1630 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1633 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1635 MARSHAL_BASIC (typename, byte_order, literal); \
1637 DEMARSHAL_BASIC (typename, byte_order); \
1638 if (strcmp (literal, v_##typename) != 0) \
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"); \
1647 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
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"); \
1661 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
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); \
1673 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1675 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1676 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
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"); \
1689 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1691 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1693 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1697 _dbus_marshal_test (void)
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;
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;
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;
1732 if (!_dbus_string_init (&str))
1733 _dbus_assert_not_reached ("failed to init string");
1737 /* Marshal doubles */
1738 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1739 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1741 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1742 _dbus_assert_not_reached ("got wrong double value");
1744 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1745 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1747 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1748 _dbus_assert_not_reached ("got wrong double value");
1750 /* Marshal signed 16 integers */
1751 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1752 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1754 /* Marshal unsigned 16 integers */
1755 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1756 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1758 /* Marshal signed integers */
1759 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1760 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1762 /* Marshal unsigned integers */
1763 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1764 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
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));
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 */
1777 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1778 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
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);
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");
1793 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1794 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
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)");
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);
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);
1813 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1814 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
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);
1824 * FIXME restore the set/pack tests
1827 #ifdef DBUS_HAVE_INT64
1828 /* set/pack 64-bit integers */
1829 _dbus_string_set_length (&str, 8);
1832 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1833 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1835 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1836 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1837 _dbus_string_get_const_data (&str)));
1840 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1841 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1843 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1844 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1845 _dbus_string_get_const_data (&str)));
1847 /* signed little pack */
1848 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1850 _dbus_string_get_data (&str));
1852 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1853 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1854 _dbus_string_get_const_data (&str)));
1856 /* signed big pack */
1857 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1859 _dbus_string_get_data (&str));
1861 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1862 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1863 _dbus_string_get_const_data (&str)));
1865 /* unsigned little */
1866 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1867 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1869 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1870 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1871 _dbus_string_get_const_data (&str)));
1874 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1875 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1877 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1878 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1879 _dbus_string_get_const_data (&str)));
1881 /* unsigned little pack */
1882 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1884 _dbus_string_get_data (&str));
1886 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1887 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1888 _dbus_string_get_const_data (&str)));
1890 /* unsigned big pack */
1891 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1893 _dbus_string_get_data (&str));
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 */
1900 /* set/pack 32-bit integers */
1901 _dbus_string_set_length (&str, 4);
1904 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1907 _dbus_assert (-0x123456 ==
1908 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1909 _dbus_string_get_const_data (&str)));
1912 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1915 _dbus_assert (-0x123456 ==
1916 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1917 _dbus_string_get_const_data (&str)));
1919 /* signed little pack */
1920 _dbus_pack_int32 (-0x123456,
1922 _dbus_string_get_data (&str));
1924 _dbus_assert (-0x123456 ==
1925 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1926 _dbus_string_get_const_data (&str)));
1928 /* signed big pack */
1929 _dbus_pack_int32 (-0x123456,
1931 _dbus_string_get_data (&str));
1933 _dbus_assert (-0x123456 ==
1934 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1935 _dbus_string_get_const_data (&str)));
1937 /* unsigned little */
1938 _dbus_marshal_set_uint32 (&str,
1940 DBUS_LITTLE_ENDIAN);
1942 _dbus_assert (0x123456 ==
1943 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1944 _dbus_string_get_const_data (&str)));
1947 _dbus_marshal_set_uint32 (&str,
1951 _dbus_assert (0x123456 ==
1952 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1953 _dbus_string_get_const_data (&str)));
1955 /* unsigned little pack */
1956 _dbus_pack_uint32 (0x123456,
1958 _dbus_string_get_data (&str));
1960 _dbus_assert (0x123456 ==
1961 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1962 _dbus_string_get_const_data (&str)));
1964 /* unsigned big pack */
1965 _dbus_pack_uint32 (0x123456,
1967 _dbus_string_get_data (&str));
1969 _dbus_assert (0x123456 ==
1970 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1971 _dbus_string_get_const_data (&str)));
1973 #endif /* set/pack tests for integers */
1975 /* Strings in-place set */
1976 byte_order = DBUS_LITTLE_ENDIAN;
1980 _dbus_string_set_length (&str, 0);
1982 /* reset pos for the macros */
1985 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1987 /* Set it to something longer */
1988 _dbus_string_init_const (&t, "Hello world foo");
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);
1994 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1995 &v_STRING, byte_order,
1997 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1999 /* Set it to something shorter */
2000 _dbus_string_init_const (&t, "Hello");
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,
2008 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
2010 /* Do the other byte order */
2011 if (byte_order == DBUS_LITTLE_ENDIAN)
2012 byte_order = DBUS_BIG_ENDIAN;
2018 _dbus_string_free (&str);
2023 #endif /* DBUS_BUILD_TESTS */