1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal-basic.h"
31 * @defgroup DBusMarshal marshaling and unmarshaling
32 * @ingroup DBusInternals
33 * @brief functions to marshal/unmarshal data from the wire
35 * Types and functions related to converting primitive data types from
36 * wire format to native machine format, and vice versa.
38 * A signature is just a string with multiple types one after the other.
39 * for example a type is "i" or "(ii)", a signature is "i(ii)"
40 * where i is int and (ii) is struct { int; int; }
46 pack_4_octets (dbus_uint32_t value,
50 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
52 if ((byte_order) == DBUS_LITTLE_ENDIAN)
53 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
55 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
59 pack_8_octets (DBusBasicValue value,
63 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
65 #ifdef DBUS_HAVE_INT64
66 if ((byte_order) == DBUS_LITTLE_ENDIAN)
67 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
69 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
71 *(DBus8ByteStruct*)data = value.u64;
72 swap_8_octets ((DBusBasicValue*)data, byte_order);
77 * Packs a 32 bit unsigned integer into a data pointer.
79 * @param value the value
80 * @param byte_order the byte order to use
81 * @param data the data pointer
84 _dbus_pack_uint32 (dbus_uint32_t value,
88 pack_4_octets (value, byte_order, data);
91 #ifndef DBUS_HAVE_INT64
94 swap_bytes (unsigned char *data,
97 unsigned char *p1 = data;
98 unsigned char *p2 = data + len - 1;
102 unsigned char tmp = *p1;
110 #endif /* !DBUS_HAVE_INT64 */
113 swap_8_octets (DBusBasicValue *value,
116 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
118 #ifdef DBUS_HAVE_INT64
119 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
121 swap_bytes ((unsigned char *)value, 8);
127 static DBusBasicValue
128 unpack_8_octets (int byte_order,
129 const unsigned char *data)
133 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
134 _dbus_assert (sizeof (r) == 8);
136 #ifdef DBUS_HAVE_INT64
137 if (byte_order == DBUS_LITTLE_ENDIAN)
138 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
140 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
142 r.u64 = *(DBus8ByteStruct*)data;
143 swap_8_octets (&r, byte_order);
150 #ifndef _dbus_unpack_uint32
152 * Unpacks a 32 bit unsigned integer from a data pointer
154 * @param byte_order The byte order to use
155 * @param data the data pointer
156 * @returns the integer
159 _dbus_unpack_uint32 (int byte_order,
160 const unsigned char *data)
162 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
164 if (byte_order == DBUS_LITTLE_ENDIAN)
165 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
167 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
169 #endif /* _dbus_unpack_uint32 */
172 set_4_octets (DBusString *str,
179 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
180 byte_order == DBUS_BIG_ENDIAN);
182 data = _dbus_string_get_data_len (str, offset, 4);
184 _dbus_pack_uint32 (value, byte_order, data);
188 set_8_octets (DBusString *str,
190 DBusBasicValue value,
195 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
196 byte_order == DBUS_BIG_ENDIAN);
198 data = _dbus_string_get_data_len (str, offset, 8);
200 pack_8_octets (value, byte_order, data);
204 * Sets the 4 bytes at the given offset to a marshaled unsigned
205 * integer, replacing anything found there previously.
207 * @param str the string to write the marshalled int to
208 * @param pos the byte offset where int should be written
209 * @param value the value
210 * @param byte_order the byte order to use
214 _dbus_marshal_set_uint32 (DBusString *str,
219 set_4_octets (str, pos, value, byte_order);
223 * Sets the existing marshaled string at the given offset with
224 * a new marshaled string. The given offset must point to
225 * an existing string or the wrong length will be deleted
226 * and replaced with the new string.
228 * Note: no attempt is made by this function to re-align
229 * any data which has been already marshalled after this
230 * string. Use with caution.
232 * @param str the string to write the marshalled string to
233 * @param pos the position of the marshaled string length
234 * @param value the value
235 * @param byte_order the byte order to use
236 * @param old_end_pos place to store byte after the nul byte of the old value
237 * @param new_end_pos place to store byte after the nul byte of the new value
238 * @returns #TRUE on success, #FALSE if no memory
242 set_string (DBusString *str,
249 int old_len, new_len;
252 _dbus_string_init_const (&dstr, value);
254 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
255 old_len = _dbus_unpack_uint32 (byte_order,
256 _dbus_string_get_const_data_len (str, pos, 4));
258 new_len = _dbus_string_get_length (&dstr);
260 if (!_dbus_string_replace_len (&dstr, 0, new_len,
261 str, pos + 4, old_len))
264 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
267 *old_end_pos = pos + 4 + old_len + 1;
269 *new_end_pos = pos + 4 + new_len + 1;
275 * Sets the existing marshaled signature at the given offset to a new
276 * marshaled signature. Same basic ideas as set_string().
278 * @param str the string to write the marshalled signature to
279 * @param pos the position of the marshaled signature length
280 * @param value the value
281 * @param byte_order the byte order to use
282 * @param old_end_pos place to store byte after the nul byte of the old value
283 * @param new_end_pos place to store byte after the nul byte of the new value
284 * @returns #TRUE on success, #FALSE if no memory
288 set_signature (DBusString *str,
295 int old_len, new_len;
298 _dbus_string_init_const (&dstr, value);
300 old_len = _dbus_string_get_byte (str, pos);
301 new_len = _dbus_string_get_length (&dstr);
303 if (!_dbus_string_replace_len (&dstr, 0, new_len,
304 str, pos + 1, old_len))
307 _dbus_string_set_byte (str, pos, new_len);
310 *old_end_pos = pos + 1 + old_len + 1;
312 *new_end_pos = pos + 1 + new_len + 1;
318 * Sets an existing basic type value to a new value.
319 * Arguments work the same way as _dbus_marshal_basic_type().
321 * @param str the string
322 * @param pos location of the current value
323 * @param type the type of the current and new values
324 * @param value the address of the new value
325 * @param byte_order byte order for marshaling
326 * @param old_end_pos location to store end position of the old value, or #NULL
327 * @param new_end_pos location to store end position of the new value, or #NULL
328 * @returns #FALSE if no memory
331 _dbus_marshal_set_basic (DBusString *str,
339 const DBusBasicValue *vp;
346 _dbus_string_set_byte (str, pos, vp->byt);
348 *old_end_pos = pos + 1;
350 *new_end_pos = pos + 1;
353 case DBUS_TYPE_BOOLEAN:
354 case DBUS_TYPE_INT32:
355 case DBUS_TYPE_UINT32:
356 pos = _DBUS_ALIGN_VALUE (pos, 4);
357 set_4_octets (str, pos, vp->u32, byte_order);
359 *old_end_pos = pos + 4;
361 *new_end_pos = pos + 4;
364 case DBUS_TYPE_INT64:
365 case DBUS_TYPE_UINT64:
366 case DBUS_TYPE_DOUBLE:
367 pos = _DBUS_ALIGN_VALUE (pos, 8);
368 set_8_octets (str, pos, *vp, byte_order);
370 *old_end_pos = pos + 8;
372 *new_end_pos = pos + 8;
375 case DBUS_TYPE_STRING:
376 case DBUS_TYPE_OBJECT_PATH:
377 pos = _DBUS_ALIGN_VALUE (pos, 4);
378 _dbus_assert (vp->str != NULL);
379 return set_string (str, pos, vp->str, byte_order,
380 old_end_pos, new_end_pos);
382 case DBUS_TYPE_SIGNATURE:
383 _dbus_assert (vp->str != NULL);
384 return set_signature (str, pos, vp->str, byte_order,
385 old_end_pos, new_end_pos);
388 _dbus_assert_not_reached ("not a basic type");
395 * Convenience function to demarshal a 32 bit unsigned integer.
397 * @param str the string containing the data
398 * @param byte_order the byte order
399 * @param pos the position in the string
400 * @param new_pos the new position of the string
401 * @returns the demarshaled integer.
404 _dbus_marshal_read_uint32 (const DBusString *str,
409 pos = _DBUS_ALIGN_VALUE (pos, 4);
414 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
416 return _dbus_unpack_uint32 (byte_order,
417 _dbus_string_get_const_data (str) + pos);
421 * Demarshals a basic-typed value. The "value" pointer is always
422 * the address of a variable of the basic type. So e.g.
423 * if the basic type is "double" then the pointer is
424 * a double*, and if it's "char*" then the pointer is
427 * A value of type #DBusBasicValue is guaranteed to be large enough to
428 * hold any of the types that may be returned, which is handy if you
429 * are trying to do things generically. For example you can pass
430 * a DBusBasicValue* in to this function, and then pass the same
431 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
432 * move a value from one place to another.
434 * @param str the string containing the data
435 * @param pos position in the string
436 * @param type type of value to demarshal
437 * @param value pointer to return value data
438 * @param byte_order the byte order
439 * @param new_pos pointer to update with new position, or #NULL
442 _dbus_marshal_read_basic (const DBusString *str,
449 const char *str_data;
452 _dbus_assert (_dbus_type_is_basic (type));
454 str_data = _dbus_string_get_const_data (str);
460 vp->byt = _dbus_string_get_byte (str, pos);
463 case DBUS_TYPE_INT32:
464 case DBUS_TYPE_UINT32:
465 case DBUS_TYPE_BOOLEAN:
466 pos = _DBUS_ALIGN_VALUE (pos, 4);
467 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
468 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
469 vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
472 case DBUS_TYPE_INT64:
473 case DBUS_TYPE_UINT64:
474 case DBUS_TYPE_DOUBLE:
475 pos = _DBUS_ALIGN_VALUE (pos, 8);
476 #ifdef DBUS_HAVE_INT64
477 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
478 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
480 vp->u64 = *(dbus_uint64_t*)(str_data + pos);
482 vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
483 swap_8_octets (vp, byte_order);
487 case DBUS_TYPE_STRING:
488 case DBUS_TYPE_OBJECT_PATH:
492 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
494 vp->str = (char*) str_data + pos;
496 pos += len + 1; /* length plus nul */
499 case DBUS_TYPE_SIGNATURE:
503 len = _dbus_string_get_byte (str, pos);
506 vp->str = (char*) str_data + pos;
508 pos += len + 1; /* length plus nul */
512 _dbus_warn ("type %s not a basic type\n",
513 _dbus_type_to_string (type));
514 _dbus_assert_not_reached ("not a basic type");
523 * Reads a block of fixed-length basic values, as an optimization
524 * vs. reading each one individually into a new buffer.
526 * This function returns the data in-place; it does not make a copy,
527 * and it does not swap the bytes.
529 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
530 * and the "value" argument should be a "const double**" and so on.
532 * @todo we aren't using this function (except in the test suite)
534 * @param str the string to read from
535 * @param pos position to read from
536 * @param element_type type of array elements
537 * @param value place to return the array
538 * @param n_elements number of array elements to read
539 * @param byte_order the byte order, used to read the array length
540 * @param new_pos #NULL or location to store a position after the elements
543 _dbus_marshal_read_fixed_multi (const DBusString *str,
554 _dbus_assert (_dbus_type_is_fixed (element_type));
555 _dbus_assert (_dbus_type_is_basic (element_type));
558 _dbus_verbose ("reading %d elements of %s\n",
559 n_elements, _dbus_type_to_string (element_type));
562 alignment = _dbus_type_get_alignment (element_type);
564 pos = _DBUS_ALIGN_VALUE (pos, alignment);
566 array_len = n_elements * alignment;
568 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
570 *new_pos = pos + array_len;
574 marshal_4_octets (DBusString *str,
583 _dbus_assert (sizeof (value) == 4);
585 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
586 value = DBUS_UINT32_SWAP_LE_BE (value);
588 orig_len = _dbus_string_get_length (str);
590 retval = _dbus_string_insert_4_aligned (str, insert_at,
591 (const unsigned char *)&value);
595 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
596 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
603 marshal_8_octets (DBusString *str,
605 DBusBasicValue value,
612 _dbus_assert (sizeof (value) == 8);
614 swap_8_octets (&value, byte_order);
616 orig_len = _dbus_string_get_length (str);
618 retval = _dbus_string_insert_8_aligned (str, insert_at,
619 (const unsigned char *)&value);
622 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
630 MARSHAL_AS_SIGNATURE,
631 MARSHAL_AS_BYTE_ARRAY
635 marshal_len_followed_by_bytes (int marshal_as,
638 const unsigned char *value,
639 int data_len, /* doesn't include nul if any */
644 DBusString value_str;
647 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
648 if (insert_at > _dbus_string_get_length (str))
649 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
650 insert_at, _dbus_string_get_length (str), data_len);
652 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
653 value_len = data_len;
655 value_len = data_len + 1; /* value has a nul */
657 _dbus_string_init_const_len (&value_str, value, value_len);
661 if (marshal_as == MARSHAL_AS_SIGNATURE)
663 if (!_dbus_string_insert_byte (str, pos, data_len))
670 if (!marshal_4_octets (str, pos, data_len,
675 if (!_dbus_string_copy_len (&value_str, 0, value_len,
681 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
683 _dbus_verbose_bytes_of_string (str, pos, value_len);
694 /* Delete what we've inserted */
695 _dbus_string_delete (str, insert_at, pos - insert_at);
701 marshal_string (DBusString *str,
707 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
708 str, insert_at, value,
710 byte_order, pos_after);
714 marshal_signature (DBusString *str,
719 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
720 str, insert_at, value,
722 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
727 * Marshals a basic-typed value. The "value" pointer is always the
728 * address of a variable containing the basic type value.
729 * So for example for int32 it will be dbus_int32_t*, and
730 * for string it will be const char**. This is for symmetry
731 * with _dbus_marshal_read_basic() and to have a simple
734 * @param str string to marshal to
735 * @param insert_at where to insert the value
736 * @param type type of value
737 * @param value pointer to a variable containing the value
738 * @param byte_order byte order
739 * @param pos_after #NULL or the position after the type
740 * @returns #TRUE on success
743 _dbus_marshal_write_basic (DBusString *str,
750 const DBusBasicValue *vp;
752 _dbus_assert (_dbus_type_is_basic (type));
759 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
762 *pos_after = insert_at + 1;
765 case DBUS_TYPE_BOOLEAN:
766 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
767 byte_order, pos_after);
769 case DBUS_TYPE_INT32:
770 case DBUS_TYPE_UINT32:
771 return marshal_4_octets (str, insert_at, vp->u32,
772 byte_order, pos_after);
774 case DBUS_TYPE_INT64:
775 case DBUS_TYPE_UINT64:
776 case DBUS_TYPE_DOUBLE:
777 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
780 case DBUS_TYPE_STRING:
781 case DBUS_TYPE_OBJECT_PATH:
782 _dbus_assert (vp->str != NULL);
783 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
785 case DBUS_TYPE_SIGNATURE:
786 _dbus_assert (vp->str != NULL);
787 return marshal_signature (str, insert_at, vp->str, pos_after);
790 _dbus_assert_not_reached ("not a basic type");
797 marshal_1_octets_array (DBusString *str,
799 const unsigned char *value,
805 DBusString value_str;
807 _dbus_string_init_const_len (&value_str, value, n_elements);
811 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
824 swap_array (DBusString *str,
830 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
832 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
837 /* we use const_data and cast it off so DBusString can be a const string
838 * for the unit tests. don't ask.
840 d = (unsigned char*) _dbus_string_get_const_data (str) + array_start;
841 end = d + n_elements * alignment;
847 #ifdef DBUS_HAVE_INT64
848 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
850 swap_8_bytes ((DBusBasicValue*) d);
857 _dbus_assert (alignment == 4);
861 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
869 marshal_fixed_multi (DBusString *str,
871 const DBusBasicValue *value,
882 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
884 old_string_len = _dbus_string_get_length (str);
886 len_in_bytes = n_elements * alignment;
887 array_start = insert_at;
889 /* Note that we do alignment padding unconditionally
890 * even if the array is empty; this means that
891 * padding + len is always equal to the number of bytes
895 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
898 _dbus_string_init_const_len (&t,
899 (const unsigned char*) value,
902 if (!_dbus_string_copy (&t, 0,
906 swap_array (str, array_start, n_elements, byte_order, alignment);
909 *pos_after = array_start + len_in_bytes;
914 _dbus_string_delete (str, insert_at,
915 _dbus_string_get_length (str) - old_string_len);
921 * Marshals a block of values of fixed-length type all at once, as an
922 * optimization. _dbus_type_is_fixed() returns #TRUE for fixed-length
923 * types, which are the basic types minus the string-like types.
925 * The value argument should be the adddress of an
926 * array, so e.g. "const dbus_uint32_t**"
928 * @param str string to marshal to
929 * @param insert_at where to insert the value
930 * @param element_type type of array elements
931 * @param value address of an array to marshal
932 * @param n_elements number of elements in the array
933 * @param byte_order byte order
934 * @param pos_after #NULL or the position after the type
935 * @returns #TRUE on success
938 _dbus_marshal_write_fixed_multi (DBusString *str,
946 const void* vp = *(const DBusBasicValue**)value;
948 _dbus_assert (_dbus_type_is_fixed (element_type));
949 _dbus_assert (n_elements >= 0);
952 _dbus_verbose ("writing %d elements of %s\n",
953 n_elements, _dbus_type_to_string (element_type));
956 switch (element_type)
959 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
961 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
962 * should we here too ? */
963 case DBUS_TYPE_BOOLEAN:
964 case DBUS_TYPE_INT32:
965 case DBUS_TYPE_UINT32:
966 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
968 case DBUS_TYPE_INT64:
969 case DBUS_TYPE_UINT64:
970 case DBUS_TYPE_DOUBLE:
971 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
975 _dbus_assert_not_reached ("non fixed type in array write");
984 * Skips over a basic-typed value, reporting the following position.
986 * @param str the string containing the data
987 * @param type type of value to read
988 * @param byte_order the byte order
989 * @param pos pointer to position in the string,
990 * updated on return to new position
993 _dbus_marshal_skip_basic (const DBusString *str,
998 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
999 byte_order == DBUS_BIG_ENDIAN);
1003 case DBUS_TYPE_BYTE:
1006 case DBUS_TYPE_BOOLEAN:
1007 case DBUS_TYPE_INT32:
1008 case DBUS_TYPE_UINT32:
1009 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1012 case DBUS_TYPE_INT64:
1013 case DBUS_TYPE_UINT64:
1014 case DBUS_TYPE_DOUBLE:
1015 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1018 case DBUS_TYPE_STRING:
1019 case DBUS_TYPE_OBJECT_PATH:
1023 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1025 *pos += len + 1; /* length plus nul */
1028 case DBUS_TYPE_SIGNATURE:
1032 len = _dbus_string_get_byte (str, *pos);
1034 *pos += len + 2; /* length byte plus length plus nul */
1038 _dbus_warn ("type %s not a basic type\n",
1039 _dbus_type_to_string (type));
1040 _dbus_assert_not_reached ("not a basic type");
1046 * Skips an array, returning the next position.
1048 * @param str the string containing the data
1049 * @param element_type the type of array elements
1050 * @param byte_order the byte order
1051 * @param pos pointer to position in the string,
1052 * updated on return to new position
1055 _dbus_marshal_skip_array (const DBusString *str,
1060 dbus_uint32_t array_len;
1064 i = _DBUS_ALIGN_VALUE (*pos, 4);
1066 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1068 alignment = _dbus_type_get_alignment (element_type);
1070 i = _DBUS_ALIGN_VALUE (i, alignment);
1072 *pos = i + array_len;
1076 * Gets the alignment requirement for the given type;
1077 * will be 1, 4, or 8.
1079 * @param typecode the type
1080 * @returns alignment of 1, 4, or 8
1083 _dbus_type_get_alignment (int typecode)
1087 case DBUS_TYPE_BYTE:
1088 case DBUS_TYPE_VARIANT:
1089 case DBUS_TYPE_SIGNATURE:
1091 case DBUS_TYPE_BOOLEAN:
1092 case DBUS_TYPE_INT32:
1093 case DBUS_TYPE_UINT32:
1094 /* this stuff is 4 since it starts with a length */
1095 case DBUS_TYPE_STRING:
1096 case DBUS_TYPE_OBJECT_PATH:
1097 case DBUS_TYPE_ARRAY:
1099 case DBUS_TYPE_INT64:
1100 case DBUS_TYPE_UINT64:
1101 case DBUS_TYPE_DOUBLE:
1102 /* struct is 8 since it could contain an 8-aligned item
1103 * and it's simpler to just always align structs to 8;
1104 * we want the amount of padding in a struct of a given
1105 * type to be predictable, not location-dependent.
1107 case DBUS_TYPE_STRUCT:
1111 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1118 * Return #TRUE if the typecode is a valid typecode.
1119 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1120 * random unknown bytes aren't either. This function is safe with
1123 * @returns #TRUE if valid
1126 _dbus_type_is_valid (int typecode)
1130 case DBUS_TYPE_BYTE:
1131 case DBUS_TYPE_BOOLEAN:
1132 case DBUS_TYPE_INT32:
1133 case DBUS_TYPE_UINT32:
1134 case DBUS_TYPE_INT64:
1135 case DBUS_TYPE_UINT64:
1136 case DBUS_TYPE_DOUBLE:
1137 case DBUS_TYPE_STRING:
1138 case DBUS_TYPE_OBJECT_PATH:
1139 case DBUS_TYPE_SIGNATURE:
1140 case DBUS_TYPE_ARRAY:
1141 case DBUS_TYPE_STRUCT:
1142 case DBUS_TYPE_VARIANT:
1150 /** macro that checks whether a typecode is a container type */
1151 #define TYPE_IS_CONTAINER(typecode) \
1152 ((typecode) == DBUS_TYPE_STRUCT || \
1153 (typecode) == DBUS_TYPE_VARIANT || \
1154 (typecode) == DBUS_TYPE_ARRAY)
1157 * A "container type" can contain basic types, or nested
1158 * container types. #DBUS_TYPE_INVALID is not a container type.
1159 * This function will crash if passed a typecode that isn't
1160 * in dbus-protocol.h
1162 * @returns #TRUE if type is a container
1165 _dbus_type_is_container (int typecode)
1167 /* only reasonable (non-line-noise) typecodes are allowed */
1168 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1169 return TYPE_IS_CONTAINER (typecode);
1173 * A "basic type" is a somewhat arbitrary concept, but the intent
1174 * is to include those types that are fully-specified by a single
1175 * typecode, with no additional type information or nested
1176 * values. So all numbers and strings are basic types and
1177 * structs, arrays, and variants are not basic types.
1178 * #DBUS_TYPE_INVALID is not a basic type.
1180 * This function is defined to return #TRUE for exactly those
1181 * types that can be written with _dbus_marshal_basic_type()
1182 * and read with _dbus_marshal_read_basic().
1184 * This function will crash if passed a typecode that isn't
1185 * in dbus-protocol.h
1187 * @returns #TRUE if type is basic
1190 _dbus_type_is_basic (int typecode)
1192 /* only reasonable (non-line-noise) typecodes are allowed */
1193 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1195 /* everything that isn't invalid or a container */
1196 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
1200 * Tells you whether values of this type can change length if you set
1201 * them to some other value. For this purpose, you assume that the
1202 * first byte of the old and new value would be in the same location,
1203 * so alignment padding is not a factor.
1205 * @returns #FALSE if the type can occupy different lengths
1208 _dbus_type_is_fixed (int typecode)
1212 case DBUS_TYPE_BYTE:
1213 case DBUS_TYPE_BOOLEAN:
1214 case DBUS_TYPE_INT32:
1215 case DBUS_TYPE_UINT32:
1216 case DBUS_TYPE_INT64:
1217 case DBUS_TYPE_UINT64:
1218 case DBUS_TYPE_DOUBLE:
1226 * Returns a string describing the given type.
1228 * @param typecode the type to describe
1229 * @returns a constant string describing the type
1232 _dbus_type_to_string (int typecode)
1236 case DBUS_TYPE_INVALID:
1238 case DBUS_TYPE_BOOLEAN:
1240 case DBUS_TYPE_BYTE:
1242 case DBUS_TYPE_INT32:
1244 case DBUS_TYPE_UINT32:
1246 case DBUS_TYPE_DOUBLE:
1248 case DBUS_TYPE_STRING:
1250 case DBUS_TYPE_OBJECT_PATH:
1251 return "object_path";
1252 case DBUS_TYPE_SIGNATURE:
1254 case DBUS_TYPE_STRUCT:
1256 case DBUS_TYPE_ARRAY:
1258 case DBUS_TYPE_VARIANT:
1260 case DBUS_STRUCT_BEGIN_CHAR:
1261 return "begin_struct";
1262 case DBUS_STRUCT_END_CHAR:
1263 return "end_struct";
1270 * If in verbose mode, print a block of binary data.
1272 * @todo right now it prints even if not in verbose mode
1274 * @param data the data
1275 * @param len the length of the data
1276 * @param offset where to start counting for byte indexes
1279 _dbus_verbose_bytes (const unsigned char *data,
1284 const unsigned char *aligned;
1286 _dbus_assert (len >= 0);
1288 /* Print blanks on first row if appropriate */
1289 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1292 _dbus_assert (aligned <= data);
1294 if (aligned != data)
1296 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1297 while (aligned != data)
1299 _dbus_verbose (" ");
1304 /* now print the bytes */
1308 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1310 _dbus_verbose ("%4d\t%p: ",
1311 offset + i, &data[i]);
1314 if (data[i] >= 32 &&
1316 _dbus_verbose (" '%c' ", data[i]);
1318 _dbus_verbose ("0x%s%x ",
1319 data[i] <= 0xf ? "0" : "", data[i]);
1323 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1326 _dbus_verbose ("BE: %d LE: %d",
1327 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1328 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1331 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1333 #ifdef DBUS_HAVE_INT64
1334 /* I think I probably mean "GNU libc printf" and not "GNUC"
1335 * but we'll wait until someone complains. If you hit this,
1336 * just turn off verbose mode as a workaround.
1339 _dbus_verbose (" u64: 0x%llx",
1340 *(dbus_uint64_t*)&data[i-8]);
1343 _dbus_verbose (" dbl: %g",
1344 *(double*)&data[i-8]);
1347 _dbus_verbose ("\n");
1351 _dbus_verbose ("\n");
1355 * Dump the given part of the string to verbose log.
1357 * @param str the string
1358 * @param start the start of range to dump
1359 * @param len length of range
1362 _dbus_verbose_bytes_of_string (const DBusString *str,
1369 real_len = _dbus_string_get_length (str);
1371 _dbus_assert (start >= 0);
1373 if (start > real_len)
1375 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1376 start, len, real_len);
1380 if ((start + len) > real_len)
1382 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1383 start, len, real_len);
1384 len = real_len - start;
1387 d = _dbus_string_get_const_data_len (str, start, len);
1389 _dbus_verbose_bytes (d, len, start);
1393 * Get the first type in the signature. The difference between this
1394 * and just getting the first byte of the signature is that you won't
1395 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1398 * @param str string containing signature
1399 * @param pos where the signature starts
1400 * @returns the first type in the signature
1403 _dbus_first_type_in_signature (const DBusString *str,
1408 t = _dbus_string_get_byte (str, pos);
1410 if (t == DBUS_STRUCT_BEGIN_CHAR)
1411 return DBUS_TYPE_STRUCT;
1418 #ifdef DBUS_BUILD_TESTS
1419 #include "dbus-test.h"
1423 swap_test_array (void *array,
1433 _dbus_string_init_const_len (&t, array, len_bytes);
1434 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1437 #define MARSHAL_BASIC(typename, byte_order, literal) \
1439 v_##typename = literal; \
1440 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1442 byte_order, NULL)) \
1443 _dbus_assert_not_reached ("no memory"); \
1446 #define DEMARSHAL_BASIC(typename, byte_order) \
1448 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1449 byte_order, &pos); \
1452 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1454 DEMARSHAL_BASIC (typename, byte_order); \
1455 if (literal != v_##typename) \
1457 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1458 _dbus_string_get_length (&str) - dump_pos); \
1459 _dbus_assert_not_reached ("demarshaled wrong value"); \
1463 #define MARSHAL_TEST(typename, byte_order, literal) \
1465 MARSHAL_BASIC (typename, byte_order, literal); \
1467 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1470 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1472 MARSHAL_BASIC (typename, byte_order, literal); \
1474 DEMARSHAL_BASIC (typename, byte_order); \
1475 if (strcmp (literal, v_##typename) != 0) \
1477 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1478 _dbus_string_get_length (&str) - dump_pos); \
1479 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1480 _dbus_assert_not_reached ("demarshaled wrong value"); \
1484 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1487 v_UINT32 = sizeof(literal); \
1488 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1489 byte_order, &next)) \
1490 _dbus_assert_not_reached ("no memory"); \
1491 v_ARRAY_##typename = literal; \
1492 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1493 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1494 byte_order, NULL)) \
1495 _dbus_assert_not_reached ("no memory"); \
1498 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1501 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1502 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1503 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1504 v_UINT32/alignment, \
1505 byte_order, NULL); \
1506 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1507 byte_order, alignment); \
1510 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1512 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1513 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1515 _dbus_verbose ("MARSHALED DATA\n"); \
1516 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1517 _dbus_string_get_length (&str) - dump_pos); \
1518 _dbus_verbose ("LITERAL DATA\n"); \
1519 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1520 _dbus_verbose ("READ DATA\n"); \
1521 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1522 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1526 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1528 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1530 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1534 _dbus_marshal_test (void)
1539 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1540 dbus_int32_t array4[3] = { 123, 456, 789 };
1541 #ifdef DBUS_HAVE_INT64
1542 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1543 DBUS_INT64_CONSTANT (0x456ffffffff),
1544 DBUS_INT64_CONSTANT (0x789ffffffff) };
1545 dbus_int64_t *v_ARRAY_INT64;
1547 unsigned char *v_ARRAY_BYTE;
1548 dbus_int32_t *v_ARRAY_INT32;
1549 double *v_ARRAY_DOUBLE;
1553 dbus_int32_t v_INT32;
1554 dbus_uint32_t v_UINT32;
1555 dbus_int64_t v_INT64;
1556 dbus_uint64_t v_UINT64;
1557 unsigned char v_BYTE;
1558 dbus_bool_t v_BOOLEAN;
1559 const char *v_STRING;
1560 const char *v_SIGNATURE;
1561 const char *v_OBJECT_PATH;
1564 if (!_dbus_string_init (&str))
1565 _dbus_assert_not_reached ("failed to init string");
1569 /* Marshal doubles */
1570 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1571 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1573 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1574 _dbus_assert_not_reached ("got wrong double value");
1576 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1577 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1579 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1580 _dbus_assert_not_reached ("got wrong double value");
1582 /* Marshal signed integers */
1583 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1584 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1586 /* Marshal unsigned integers */
1587 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1588 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1590 #ifdef DBUS_HAVE_INT64
1591 /* Marshal signed integers */
1592 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1593 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1595 /* Marshal unsigned integers */
1596 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1597 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1598 #endif /* DBUS_HAVE_INT64 */
1601 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1602 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1604 /* Marshal all possible bools! */
1605 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1606 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1607 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1608 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1610 /* Marshal strings */
1611 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1612 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1613 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1614 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1617 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1618 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1621 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1622 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1623 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1624 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1627 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1628 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1630 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1631 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1633 #ifdef DBUS_HAVE_INT64
1634 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1635 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1641 * FIXME restore the set/pack tests
1644 #ifdef DBUS_HAVE_INT64
1645 /* set/pack 64-bit integers */
1646 _dbus_string_set_length (&str, 8);
1649 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1650 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1652 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1653 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1654 _dbus_string_get_const_data (&str)));
1657 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1658 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1660 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1661 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1662 _dbus_string_get_const_data (&str)));
1664 /* signed little pack */
1665 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1667 _dbus_string_get_data (&str));
1669 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1670 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1671 _dbus_string_get_const_data (&str)));
1673 /* signed big pack */
1674 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1676 _dbus_string_get_data (&str));
1678 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1679 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1680 _dbus_string_get_const_data (&str)));
1682 /* unsigned little */
1683 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1684 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1686 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1687 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1688 _dbus_string_get_const_data (&str)));
1691 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1692 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1694 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1695 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1696 _dbus_string_get_const_data (&str)));
1698 /* unsigned little pack */
1699 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1701 _dbus_string_get_data (&str));
1703 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1704 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1705 _dbus_string_get_const_data (&str)));
1707 /* unsigned big pack */
1708 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1710 _dbus_string_get_data (&str));
1712 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1713 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1714 _dbus_string_get_const_data (&str)));
1715 #endif /* DBUS_HAVE_INT64 */
1717 /* set/pack 32-bit integers */
1718 _dbus_string_set_length (&str, 4);
1721 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1724 _dbus_assert (-0x123456 ==
1725 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1726 _dbus_string_get_const_data (&str)));
1729 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1732 _dbus_assert (-0x123456 ==
1733 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1734 _dbus_string_get_const_data (&str)));
1736 /* signed little pack */
1737 _dbus_pack_int32 (-0x123456,
1739 _dbus_string_get_data (&str));
1741 _dbus_assert (-0x123456 ==
1742 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1743 _dbus_string_get_const_data (&str)));
1745 /* signed big pack */
1746 _dbus_pack_int32 (-0x123456,
1748 _dbus_string_get_data (&str));
1750 _dbus_assert (-0x123456 ==
1751 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1752 _dbus_string_get_const_data (&str)));
1754 /* unsigned little */
1755 _dbus_marshal_set_uint32 (&str,
1757 DBUS_LITTLE_ENDIAN);
1759 _dbus_assert (0x123456 ==
1760 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1761 _dbus_string_get_const_data (&str)));
1764 _dbus_marshal_set_uint32 (&str,
1768 _dbus_assert (0x123456 ==
1769 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1770 _dbus_string_get_const_data (&str)));
1772 /* unsigned little pack */
1773 _dbus_pack_uint32 (0x123456,
1775 _dbus_string_get_data (&str));
1777 _dbus_assert (0x123456 ==
1778 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1779 _dbus_string_get_const_data (&str)));
1781 /* unsigned big pack */
1782 _dbus_pack_uint32 (0x123456,
1784 _dbus_string_get_data (&str));
1786 _dbus_assert (0x123456 ==
1787 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1788 _dbus_string_get_const_data (&str)));
1790 #endif /* set/pack tests for integers */
1792 /* Strings in-place set */
1793 byte_order = DBUS_LITTLE_ENDIAN;
1797 _dbus_string_set_length (&str, 0);
1799 /* reset pos for the macros */
1802 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1804 /* Set it to something longer */
1805 _dbus_string_init_const (&t, "Hello world foo");
1807 v_STRING = _dbus_string_get_const_data (&t);
1808 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1809 &v_STRING, byte_order, NULL, NULL);
1811 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1812 &v_STRING, byte_order,
1814 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1816 /* Set it to something shorter */
1817 _dbus_string_init_const (&t, "Hello");
1819 v_STRING = _dbus_string_get_const_data (&t);
1820 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1821 &v_STRING, byte_order, NULL, NULL);
1822 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1823 &v_STRING, byte_order,
1825 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1827 /* Do the other byte order */
1828 if (byte_order == DBUS_LITTLE_ENDIAN)
1829 byte_order = DBUS_BIG_ENDIAN;
1835 _dbus_string_free (&str);
1840 #endif /* DBUS_BUILD_TESTS */