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);
92 * Packs a 32 bit signed integer into a data pointer.
94 * @param value the value
95 * @param byte_order the byte order to use
96 * @param data the data pointer
99 _dbus_pack_int32 (dbus_int32_t value,
103 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
106 #ifndef DBUS_HAVE_INT64
109 swap_bytes (unsigned char *data,
112 unsigned char *p1 = data;
113 unsigned char *p2 = data + len - 1;
117 unsigned char tmp = *p1;
125 #endif /* !DBUS_HAVE_INT64 */
128 swap_8_octets (DBusBasicValue *value,
131 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
133 #ifdef DBUS_HAVE_INT64
134 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
136 swap_bytes ((unsigned char *)value, 8);
142 static DBusBasicValue
143 unpack_8_octets (int byte_order,
144 const unsigned char *data)
148 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
149 _dbus_assert (sizeof (r) == 8);
151 #ifdef DBUS_HAVE_INT64
152 if (byte_order == DBUS_LITTLE_ENDIAN)
153 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
155 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
157 r.u64 = *(DBus8ByteStruct*)data;
158 swap_8_octets (&r, byte_order);
165 #ifndef _dbus_unpack_uint32
167 * Unpacks a 32 bit unsigned integer from a data pointer
169 * @param byte_order The byte order to use
170 * @param data the data pointer
171 * @returns the integer
174 _dbus_unpack_uint32 (int byte_order,
175 const unsigned char *data)
177 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
179 if (byte_order == DBUS_LITTLE_ENDIAN)
180 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
182 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
184 #endif /* _dbus_unpack_uint32 */
187 * Unpacks a 32 bit signed integer from a data pointer
189 * @param byte_order The byte order to use
190 * @param data the data pointer
191 * @returns the integer
194 _dbus_unpack_int32 (int byte_order,
195 const unsigned char *data)
197 return (dbus_int32_t) _dbus_unpack_uint32 (byte_order, data);
201 set_4_octets (DBusString *str,
208 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
209 byte_order == DBUS_BIG_ENDIAN);
211 data = _dbus_string_get_data_len (str, offset, 4);
213 _dbus_pack_uint32 (value, byte_order, data);
217 set_8_octets (DBusString *str,
219 DBusBasicValue value,
224 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
225 byte_order == DBUS_BIG_ENDIAN);
227 data = _dbus_string_get_data_len (str, offset, 8);
229 pack_8_octets (value, byte_order, data);
233 * Sets the 4 bytes at the given offset to a marshaled unsigned
234 * integer, replacing anything found there previously.
236 * @param str the string to write the marshalled int to
237 * @param pos the byte offset where int should be written
238 * @param value the value
239 * @param byte_order the byte order to use
243 _dbus_marshal_set_uint32 (DBusString *str,
248 set_4_octets (str, pos, value, byte_order);
252 * Sets the existing marshaled string at the given offset with
253 * a new marshaled string. The given offset must point to
254 * an existing string or the wrong length will be deleted
255 * and replaced with the new string.
257 * Note: no attempt is made by this function to re-align
258 * any data which has been already marshalled after this
259 * string. Use with caution.
261 * @param str the string to write the marshalled string to
262 * @param pos the position of the marshaled string length
263 * @param value the value
264 * @param byte_order the byte order to use
265 * @param old_end_pos place to store byte after the nul byte of the old value
266 * @param new_end_pos place to store byte after the nul byte of the new value
267 * @returns #TRUE on success, #FALSE if no memory
271 set_string (DBusString *str,
278 int old_len, new_len;
281 _dbus_string_init_const (&dstr, value);
283 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
284 old_len = _dbus_unpack_uint32 (byte_order,
285 _dbus_string_get_const_data_len (str, pos, 4));
287 new_len = _dbus_string_get_length (&dstr);
289 if (!_dbus_string_replace_len (&dstr, 0, new_len,
290 str, pos + 4, old_len))
293 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
296 *old_end_pos = pos + 4 + old_len + 1;
298 *new_end_pos = pos + 4 + new_len + 1;
304 * Sets the existing marshaled signature at the given offset to a new
305 * marshaled signature. Same basic ideas as set_string().
307 * @param str the string to write the marshalled signature to
308 * @param pos the position of the marshaled signature length
309 * @param value the value
310 * @param byte_order the byte order to use
311 * @param old_end_pos place to store byte after the nul byte of the old value
312 * @param new_end_pos place to store byte after the nul byte of the new value
313 * @returns #TRUE on success, #FALSE if no memory
317 set_signature (DBusString *str,
324 int old_len, new_len;
327 _dbus_string_init_const (&dstr, value);
329 old_len = _dbus_string_get_byte (str, pos);
330 new_len = _dbus_string_get_length (&dstr);
332 if (!_dbus_string_replace_len (&dstr, 0, new_len,
333 str, pos + 1, old_len))
336 _dbus_string_set_byte (str, pos, new_len);
339 *old_end_pos = pos + 1 + old_len + 1;
341 *new_end_pos = pos + 1 + new_len + 1;
347 * Sets an existing basic type value to a new value.
348 * Arguments work the same way as _dbus_marshal_basic_type().
350 * @param str the string
351 * @param pos location of the current value
352 * @param type the type of the current and new values
353 * @param value the address of the new value
354 * @param byte_order byte order for marshaling
355 * @param old_end_pos location to store end position of the old value, or #NULL
356 * @param new_end_pos location to store end position of the new value, or #NULL
357 * @returns #FALSE if no memory
360 _dbus_marshal_set_basic (DBusString *str,
368 const DBusBasicValue *vp;
375 case DBUS_TYPE_BOOLEAN:
376 _dbus_string_set_byte (str, pos, vp->byt);
378 *old_end_pos = pos + 1;
380 *new_end_pos = pos + 1;
383 case DBUS_TYPE_INT32:
384 case DBUS_TYPE_UINT32:
385 pos = _DBUS_ALIGN_VALUE (pos, 4);
386 set_4_octets (str, pos, vp->u32, byte_order);
388 *old_end_pos = pos + 4;
390 *new_end_pos = pos + 4;
393 case DBUS_TYPE_INT64:
394 case DBUS_TYPE_UINT64:
395 case DBUS_TYPE_DOUBLE:
396 pos = _DBUS_ALIGN_VALUE (pos, 8);
397 set_8_octets (str, pos, *vp, byte_order);
399 *old_end_pos = pos + 8;
401 *new_end_pos = pos + 8;
404 case DBUS_TYPE_STRING:
405 case DBUS_TYPE_OBJECT_PATH:
406 pos = _DBUS_ALIGN_VALUE (pos, 4);
407 _dbus_assert (vp->str != NULL);
408 return set_string (str, pos, vp->str, byte_order,
409 old_end_pos, new_end_pos);
411 case DBUS_TYPE_SIGNATURE:
412 _dbus_assert (vp->str != NULL);
413 return set_signature (str, pos, vp->str, byte_order,
414 old_end_pos, new_end_pos);
417 _dbus_assert_not_reached ("not a basic type");
424 * Convenience function to demarshal a 32 bit unsigned integer.
426 * @param str the string containing the data
427 * @param byte_order the byte order
428 * @param pos the position in the string
429 * @param new_pos the new position of the string
430 * @returns the demarshaled integer.
433 _dbus_marshal_read_uint32 (const DBusString *str,
438 pos = _DBUS_ALIGN_VALUE (pos, 4);
443 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
445 return _dbus_unpack_uint32 (byte_order,
446 _dbus_string_get_const_data (str) + pos);
450 * Demarshals a basic-typed value. The "value" pointer is always
451 * the address of a variable of the basic type. So e.g.
452 * if the basic type is "double" then the pointer is
453 * a double*, and if it's "char*" then the pointer is
456 * A value of type #DBusBasicValue is guaranteed to be large enough to
457 * hold any of the types that may be returned, which is handy if you
458 * are trying to do things generically. For example you can pass
459 * a DBusBasicValue* in to this function, and then pass the same
460 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
461 * move a value from one place to another.
463 * @param str the string containing the data
464 * @param pos position in the string
465 * @param type type of value to demarshal
466 * @param value pointer to return value data
467 * @param byte_order the byte order
468 * @param new_pos pointer to update with new position, or #NULL
471 _dbus_marshal_read_basic (const DBusString *str,
478 const char *str_data;
481 _dbus_assert (_dbus_type_is_basic (type));
483 str_data = _dbus_string_get_const_data (str);
489 case DBUS_TYPE_BOOLEAN:
490 vp->byt = _dbus_string_get_byte (str, pos);
493 case DBUS_TYPE_INT32:
494 case DBUS_TYPE_UINT32:
495 pos = _DBUS_ALIGN_VALUE (pos, 4);
496 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
497 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
498 vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
501 case DBUS_TYPE_INT64:
502 case DBUS_TYPE_UINT64:
503 case DBUS_TYPE_DOUBLE:
504 pos = _DBUS_ALIGN_VALUE (pos, 8);
505 #ifdef DBUS_HAVE_INT64
506 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
507 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
509 vp->u64 = *(dbus_uint64_t*)(str_data + pos);
511 vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
512 swap_8_octets (vp, byte_order);
516 case DBUS_TYPE_STRING:
517 case DBUS_TYPE_OBJECT_PATH:
521 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
523 vp->str = (char*) str_data + pos;
525 pos += len + 1; /* length plus nul */
528 case DBUS_TYPE_SIGNATURE:
532 len = _dbus_string_get_byte (str, pos);
535 vp->str = (char*) str_data + pos;
537 pos += len + 1; /* length plus nul */
541 _dbus_warn ("type %s not a basic type\n",
542 _dbus_type_to_string (type));
543 _dbus_assert_not_reached ("not a basic type");
552 * Reads a block of fixed-length basic values, as an optimization
553 * vs. reading each one individually into a new buffer.
555 * This function returns the data in-place; it does not make a copy,
556 * and it does not swap the bytes.
558 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
559 * and the "value" argument should be a "const double**" and so on.
561 * @todo we aren't using this function (except in the test suite)
563 * @param str the string to read from
564 * @param pos position to read from
565 * @param element_type type of array elements
566 * @param value place to return the array
567 * @param n_elements number of array elements to read
568 * @param byte_order the byte order, used to read the array length
569 * @param new_pos #NULL or location to store a position after the elements
572 _dbus_marshal_read_fixed_multi (const DBusString *str,
583 _dbus_assert (_dbus_type_is_fixed (element_type));
584 _dbus_assert (_dbus_type_is_basic (element_type));
587 _dbus_verbose ("reading %d elements of %s\n",
588 n_elements, _dbus_type_to_string (element_type));
591 alignment = _dbus_type_get_alignment (element_type);
593 pos = _DBUS_ALIGN_VALUE (pos, alignment);
595 array_len = n_elements * alignment;
597 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
599 *new_pos = pos + array_len;
603 marshal_4_octets (DBusString *str,
612 _dbus_assert (sizeof (value) == 4);
614 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
615 value = DBUS_UINT32_SWAP_LE_BE (value);
617 orig_len = _dbus_string_get_length (str);
619 retval = _dbus_string_insert_4_aligned (str, insert_at,
620 (const unsigned char *)&value);
624 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
625 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
632 marshal_8_octets (DBusString *str,
634 DBusBasicValue value,
641 _dbus_assert (sizeof (value) == 8);
643 swap_8_octets (&value, byte_order);
645 orig_len = _dbus_string_get_length (str);
647 retval = _dbus_string_insert_8_aligned (str, insert_at,
648 (const unsigned char *)&value);
651 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
659 MARSHAL_AS_SIGNATURE,
660 MARSHAL_AS_BYTE_ARRAY
664 marshal_len_followed_by_bytes (int marshal_as,
667 const unsigned char *value,
668 int data_len, /* doesn't include nul if any */
673 DBusString value_str;
676 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
677 if (insert_at > _dbus_string_get_length (str))
678 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
679 insert_at, _dbus_string_get_length (str), data_len);
681 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
682 value_len = data_len;
684 value_len = data_len + 1; /* value has a nul */
686 _dbus_string_init_const_len (&value_str, value, value_len);
690 if (marshal_as == MARSHAL_AS_SIGNATURE)
692 if (!_dbus_string_insert_byte (str, pos, data_len))
699 if (!marshal_4_octets (str, pos, data_len,
704 if (!_dbus_string_copy_len (&value_str, 0, value_len,
710 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
712 _dbus_verbose_bytes_of_string (str, pos, value_len);
723 /* Delete what we've inserted */
724 _dbus_string_delete (str, insert_at, pos - insert_at);
730 marshal_string (DBusString *str,
736 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
737 str, insert_at, value,
739 byte_order, pos_after);
743 marshal_signature (DBusString *str,
748 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
749 str, insert_at, value,
751 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
756 * Marshals a basic-typed value. The "value" pointer is always the
757 * address of a variable containing the basic type value.
758 * So for example for int32 it will be dbus_int32_t*, and
759 * for string it will be const char**. This is for symmetry
760 * with _dbus_marshal_read_basic() and to have a simple
763 * @param str string to marshal to
764 * @param insert_at where to insert the value
765 * @param type type of value
766 * @param value pointer to a variable containing the value
767 * @param byte_order byte order
768 * @param pos_after #NULL or the position after the type
769 * @returns #TRUE on success
772 _dbus_marshal_write_basic (DBusString *str,
779 const DBusBasicValue *vp;
781 _dbus_assert (_dbus_type_is_basic (type));
788 case DBUS_TYPE_BOOLEAN:
789 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
792 *pos_after = insert_at + 1;
795 case DBUS_TYPE_INT32:
796 case DBUS_TYPE_UINT32:
797 return marshal_4_octets (str, insert_at, vp->u32,
798 byte_order, pos_after);
800 case DBUS_TYPE_INT64:
801 case DBUS_TYPE_UINT64:
802 case DBUS_TYPE_DOUBLE:
803 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
806 case DBUS_TYPE_STRING:
807 case DBUS_TYPE_OBJECT_PATH:
808 _dbus_assert (vp->str != NULL);
809 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
811 case DBUS_TYPE_SIGNATURE:
812 _dbus_assert (vp->str != NULL);
813 return marshal_signature (str, insert_at, vp->str, pos_after);
816 _dbus_assert_not_reached ("not a basic type");
823 marshal_1_octets_array (DBusString *str,
825 const unsigned char *value,
831 DBusString value_str;
833 _dbus_string_init_const_len (&value_str, value, n_elements);
837 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
850 swap_array (DBusString *str,
856 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
858 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
863 /* we use const_data and cast it off so DBusString can be a const string
864 * for the unit tests. don't ask.
866 d = (unsigned char*) _dbus_string_get_const_data (str) + array_start;
867 end = d + n_elements * alignment;
873 #ifdef DBUS_HAVE_INT64
874 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
876 swap_8_bytes ((DBusBasicValue*) d);
883 _dbus_assert (alignment == 4);
887 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
895 marshal_fixed_multi (DBusString *str,
897 const DBusBasicValue *value,
908 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
910 old_string_len = _dbus_string_get_length (str);
912 len_in_bytes = n_elements * alignment;
913 array_start = insert_at;
915 /* Note that we do alignment padding unconditionally
916 * even if the array is empty; this means that
917 * padding + len is always equal to the number of bytes
921 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
924 _dbus_string_init_const_len (&t,
925 (const unsigned char*) value,
928 if (!_dbus_string_copy (&t, 0,
932 swap_array (str, array_start, n_elements, byte_order, alignment);
935 *pos_after = array_start + len_in_bytes;
940 _dbus_string_delete (str, insert_at,
941 _dbus_string_get_length (str) - old_string_len);
947 * Marshals a block of values of fixed-length type all at once, as an
948 * optimization. _dbus_type_is_fixed() returns #TRUE for fixed-length
949 * types, which are the basic types minus the string-like types.
951 * The value argument should be the adddress of an
952 * array, so e.g. "const dbus_uint32_t**"
954 * @param str string to marshal to
955 * @param insert_at where to insert the value
956 * @param element_type type of array elements
957 * @param value address of an array to marshal
958 * @param n_elements number of elements in the array
959 * @param byte_order byte order
960 * @param pos_after #NULL or the position after the type
961 * @returns #TRUE on success
964 _dbus_marshal_write_fixed_multi (DBusString *str,
972 const void* vp = *(const DBusBasicValue**)value;
974 _dbus_assert (_dbus_type_is_fixed (element_type));
975 _dbus_assert (n_elements >= 0);
978 _dbus_verbose ("writing %d elements of %s\n",
979 n_elements, _dbus_type_to_string (element_type));
982 switch (element_type)
984 case DBUS_TYPE_BOOLEAN:
985 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
986 * should we here too ? */
988 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
990 case DBUS_TYPE_INT32:
991 case DBUS_TYPE_UINT32:
992 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
994 case DBUS_TYPE_INT64:
995 case DBUS_TYPE_UINT64:
996 case DBUS_TYPE_DOUBLE:
997 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1001 _dbus_assert_not_reached ("non fixed type in array write");
1010 * Skips over a basic-typed value, reporting the following position.
1012 * @param str the string containing the data
1013 * @param type type of value to read
1014 * @param byte_order the byte order
1015 * @param pos pointer to position in the string,
1016 * updated on return to new position
1019 _dbus_marshal_skip_basic (const DBusString *str,
1024 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1025 byte_order == DBUS_BIG_ENDIAN);
1029 case DBUS_TYPE_BYTE:
1030 case DBUS_TYPE_BOOLEAN:
1033 case DBUS_TYPE_INT32:
1034 case DBUS_TYPE_UINT32:
1035 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1038 case DBUS_TYPE_INT64:
1039 case DBUS_TYPE_UINT64:
1040 case DBUS_TYPE_DOUBLE:
1041 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1044 case DBUS_TYPE_STRING:
1045 case DBUS_TYPE_OBJECT_PATH:
1049 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1051 *pos += len + 1; /* length plus nul */
1054 case DBUS_TYPE_SIGNATURE:
1058 len = _dbus_string_get_byte (str, *pos);
1060 *pos += len + 2; /* length byte plus length plus nul */
1064 _dbus_warn ("type %s not a basic type\n",
1065 _dbus_type_to_string (type));
1066 _dbus_assert_not_reached ("not a basic type");
1072 * Skips an array, returning the next position.
1074 * @param str the string containing the data
1075 * @param element_type the type of array elements
1076 * @param byte_order the byte order
1077 * @param pos pointer to position in the string,
1078 * updated on return to new position
1081 _dbus_marshal_skip_array (const DBusString *str,
1086 dbus_uint32_t array_len;
1090 i = _DBUS_ALIGN_VALUE (*pos, 4);
1092 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1094 alignment = _dbus_type_get_alignment (element_type);
1096 i = _DBUS_ALIGN_VALUE (i, alignment);
1098 *pos = i + array_len;
1102 * Gets the alignment requirement for the given type;
1103 * will be 1, 4, or 8.
1105 * @param typecode the type
1106 * @returns alignment of 1, 4, or 8
1109 _dbus_type_get_alignment (int typecode)
1113 case DBUS_TYPE_BYTE:
1114 case DBUS_TYPE_BOOLEAN:
1115 case DBUS_TYPE_VARIANT:
1116 case DBUS_TYPE_SIGNATURE:
1118 case DBUS_TYPE_INT32:
1119 case DBUS_TYPE_UINT32:
1120 /* this stuff is 4 since it starts with a length */
1121 case DBUS_TYPE_STRING:
1122 case DBUS_TYPE_OBJECT_PATH:
1123 case DBUS_TYPE_ARRAY:
1125 case DBUS_TYPE_INT64:
1126 case DBUS_TYPE_UINT64:
1127 case DBUS_TYPE_DOUBLE:
1128 /* struct is 8 since it could contain an 8-aligned item
1129 * and it's simpler to just always align structs to 8;
1130 * we want the amount of padding in a struct of a given
1131 * type to be predictable, not location-dependent.
1133 case DBUS_TYPE_STRUCT:
1137 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1144 * Return #TRUE if the typecode is a valid typecode.
1145 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1146 * random unknown bytes aren't either. This function is safe with
1149 * @returns #TRUE if valid
1152 _dbus_type_is_valid (int typecode)
1156 case DBUS_TYPE_BYTE:
1157 case DBUS_TYPE_BOOLEAN:
1158 case DBUS_TYPE_INT32:
1159 case DBUS_TYPE_UINT32:
1160 case DBUS_TYPE_INT64:
1161 case DBUS_TYPE_UINT64:
1162 case DBUS_TYPE_DOUBLE:
1163 case DBUS_TYPE_STRING:
1164 case DBUS_TYPE_OBJECT_PATH:
1165 case DBUS_TYPE_SIGNATURE:
1166 case DBUS_TYPE_ARRAY:
1167 case DBUS_TYPE_STRUCT:
1168 case DBUS_TYPE_VARIANT:
1176 /** macro that checks whether a typecode is a container type */
1177 #define TYPE_IS_CONTAINER(typecode) \
1178 ((typecode) == DBUS_TYPE_STRUCT || \
1179 (typecode) == DBUS_TYPE_VARIANT || \
1180 (typecode) == DBUS_TYPE_ARRAY)
1183 * A "container type" can contain basic types, or nested
1184 * container types. #DBUS_TYPE_INVALID is not a container type.
1185 * This function will crash if passed a typecode that isn't
1186 * in dbus-protocol.h
1188 * @returns #TRUE if type is a container
1191 _dbus_type_is_container (int typecode)
1193 /* only reasonable (non-line-noise) typecodes are allowed */
1194 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1195 return TYPE_IS_CONTAINER (typecode);
1199 * A "basic type" is a somewhat arbitrary concept, but the intent
1200 * is to include those types that are fully-specified by a single
1201 * typecode, with no additional type information or nested
1202 * values. So all numbers and strings are basic types and
1203 * structs, arrays, and variants are not basic types.
1204 * #DBUS_TYPE_INVALID is not a basic type.
1206 * This function is defined to return #TRUE for exactly those
1207 * types that can be written with _dbus_marshal_basic_type()
1208 * and read with _dbus_marshal_read_basic().
1210 * This function will crash if passed a typecode that isn't
1211 * in dbus-protocol.h
1213 * @returns #TRUE if type is basic
1216 _dbus_type_is_basic (int typecode)
1218 /* only reasonable (non-line-noise) typecodes are allowed */
1219 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1221 /* everything that isn't invalid or a container */
1222 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
1226 * Tells you whether values of this type can change length if you set
1227 * them to some other value. For this purpose, you assume that the
1228 * first byte of the old and new value would be in the same location,
1229 * so alignment padding is not a factor.
1231 * @returns #FALSE if the type can occupy different lengths
1234 _dbus_type_is_fixed (int typecode)
1238 case DBUS_TYPE_BYTE:
1239 case DBUS_TYPE_BOOLEAN:
1240 case DBUS_TYPE_INT32:
1241 case DBUS_TYPE_UINT32:
1242 case DBUS_TYPE_INT64:
1243 case DBUS_TYPE_UINT64:
1244 case DBUS_TYPE_DOUBLE:
1252 * Returns a string describing the given type.
1254 * @param typecode the type to describe
1255 * @returns a constant string describing the type
1258 _dbus_type_to_string (int typecode)
1262 case DBUS_TYPE_INVALID:
1264 case DBUS_TYPE_BOOLEAN:
1266 case DBUS_TYPE_BYTE:
1268 case DBUS_TYPE_INT32:
1270 case DBUS_TYPE_UINT32:
1272 case DBUS_TYPE_DOUBLE:
1274 case DBUS_TYPE_STRING:
1276 case DBUS_TYPE_OBJECT_PATH:
1277 return "object_path";
1278 case DBUS_TYPE_SIGNATURE:
1280 case DBUS_TYPE_STRUCT:
1282 case DBUS_TYPE_ARRAY:
1284 case DBUS_TYPE_VARIANT:
1286 case DBUS_STRUCT_BEGIN_CHAR:
1287 return "begin_struct";
1288 case DBUS_STRUCT_END_CHAR:
1289 return "end_struct";
1296 * If in verbose mode, print a block of binary data.
1298 * @todo right now it prints even if not in verbose mode
1300 * @param data the data
1301 * @param len the length of the data
1302 * @param offset where to start counting for byte indexes
1305 _dbus_verbose_bytes (const unsigned char *data,
1310 const unsigned char *aligned;
1312 _dbus_assert (len >= 0);
1314 /* Print blanks on first row if appropriate */
1315 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1318 _dbus_assert (aligned <= data);
1320 if (aligned != data)
1322 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1323 while (aligned != data)
1325 _dbus_verbose (" ");
1330 /* now print the bytes */
1334 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1336 _dbus_verbose ("%4d\t%p: ",
1337 offset + i, &data[i]);
1340 if (data[i] >= 32 &&
1342 _dbus_verbose (" '%c' ", data[i]);
1344 _dbus_verbose ("0x%s%x ",
1345 data[i] <= 0xf ? "0" : "", data[i]);
1349 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1352 _dbus_verbose ("BE: %d LE: %d",
1353 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1354 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1357 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1359 #ifdef DBUS_HAVE_INT64
1360 /* I think I probably mean "GNU libc printf" and not "GNUC"
1361 * but we'll wait until someone complains. If you hit this,
1362 * just turn off verbose mode as a workaround.
1365 _dbus_verbose (" u64: 0x%llx",
1366 *(dbus_uint64_t*)&data[i-8]);
1369 _dbus_verbose (" dbl: %g",
1370 *(double*)&data[i-8]);
1373 _dbus_verbose ("\n");
1377 _dbus_verbose ("\n");
1381 * Dump the given part of the string to verbose log.
1383 * @param str the string
1384 * @param start the start of range to dump
1385 * @param len length of range
1388 _dbus_verbose_bytes_of_string (const DBusString *str,
1395 real_len = _dbus_string_get_length (str);
1397 _dbus_assert (start >= 0);
1399 if (start > real_len)
1401 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1402 start, len, real_len);
1406 if ((start + len) > real_len)
1408 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1409 start, len, real_len);
1410 len = real_len - start;
1413 d = _dbus_string_get_const_data_len (str, start, len);
1415 _dbus_verbose_bytes (d, len, start);
1420 #ifdef DBUS_BUILD_TESTS
1421 #include "dbus-test.h"
1425 swap_test_array (void *array,
1435 _dbus_string_init_const_len (&t, array, len_bytes);
1436 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1439 #define MARSHAL_BASIC(typename, byte_order, literal) \
1441 v_##typename = literal; \
1442 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1444 byte_order, NULL)) \
1445 _dbus_assert_not_reached ("no memory"); \
1448 #define DEMARSHAL_BASIC(typename, byte_order) \
1450 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1451 byte_order, &pos); \
1454 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1456 DEMARSHAL_BASIC (typename, byte_order); \
1457 if (literal != v_##typename) \
1459 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1460 _dbus_string_get_length (&str) - dump_pos); \
1461 _dbus_assert_not_reached ("demarshaled wrong value"); \
1465 #define MARSHAL_TEST(typename, byte_order, literal) \
1467 MARSHAL_BASIC (typename, byte_order, literal); \
1469 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1472 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1474 MARSHAL_BASIC (typename, byte_order, literal); \
1476 DEMARSHAL_BASIC (typename, byte_order); \
1477 if (strcmp (literal, v_##typename) != 0) \
1479 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1480 _dbus_string_get_length (&str) - dump_pos); \
1481 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1482 _dbus_assert_not_reached ("demarshaled wrong value"); \
1486 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1489 v_UINT32 = sizeof(literal); \
1490 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1491 byte_order, &next)) \
1492 _dbus_assert_not_reached ("no memory"); \
1493 v_ARRAY_##typename = literal; \
1494 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1495 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1496 byte_order, NULL)) \
1497 _dbus_assert_not_reached ("no memory"); \
1500 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1503 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1504 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1505 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1506 v_UINT32/alignment, \
1507 byte_order, NULL); \
1508 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1509 byte_order, alignment); \
1512 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1514 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1515 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1517 _dbus_verbose ("MARSHALED DATA\n"); \
1518 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1519 _dbus_string_get_length (&str) - dump_pos); \
1520 _dbus_verbose ("LITERAL DATA\n"); \
1521 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1522 _dbus_verbose ("READ DATA\n"); \
1523 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1524 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1528 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1530 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1532 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1536 _dbus_marshal_test (void)
1541 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1542 dbus_int32_t array4[3] = { 123, 456, 789 };
1543 #ifdef DBUS_HAVE_INT64
1544 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1545 DBUS_INT64_CONSTANT (0x456ffffffff),
1546 DBUS_INT64_CONSTANT (0x789ffffffff) };
1547 dbus_int64_t *v_ARRAY_INT64;
1549 unsigned char *v_ARRAY_BYTE;
1550 dbus_int32_t *v_ARRAY_INT32;
1551 double *v_ARRAY_DOUBLE;
1555 dbus_int32_t v_INT32;
1556 dbus_uint32_t v_UINT32;
1557 dbus_int64_t v_INT64;
1558 dbus_uint64_t v_UINT64;
1559 unsigned char v_BYTE;
1560 unsigned char v_BOOLEAN;
1561 const char *v_STRING;
1562 const char *v_SIGNATURE;
1563 const char *v_OBJECT_PATH;
1566 if (!_dbus_string_init (&str))
1567 _dbus_assert_not_reached ("failed to init string");
1571 /* Marshal doubles */
1572 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1573 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1575 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1576 _dbus_assert_not_reached ("got wrong double value");
1578 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1579 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1581 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1582 _dbus_assert_not_reached ("got wrong double value");
1584 /* Marshal signed integers */
1585 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1586 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1588 /* Marshal unsigned integers */
1589 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1590 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1592 #ifdef DBUS_HAVE_INT64
1593 /* Marshal signed integers */
1594 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1595 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1597 /* Marshal unsigned integers */
1598 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1599 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1600 #endif /* DBUS_HAVE_INT64 */
1603 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1604 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1606 /* Marshal all possible bools! */
1607 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1608 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1609 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1610 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1612 /* Marshal strings */
1613 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1614 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1615 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1616 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1619 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1620 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1623 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1624 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1625 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1626 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1629 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1630 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1632 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1633 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1635 #ifdef DBUS_HAVE_INT64
1636 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1637 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1643 * FIXME restore the set/pack tests
1646 #ifdef DBUS_HAVE_INT64
1647 /* set/pack 64-bit integers */
1648 _dbus_string_set_length (&str, 8);
1651 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1652 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1654 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1655 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1656 _dbus_string_get_const_data (&str)));
1659 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1660 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1662 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1663 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1664 _dbus_string_get_const_data (&str)));
1666 /* signed little pack */
1667 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1669 _dbus_string_get_data (&str));
1671 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1672 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1673 _dbus_string_get_const_data (&str)));
1675 /* signed big pack */
1676 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1678 _dbus_string_get_data (&str));
1680 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1681 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1682 _dbus_string_get_const_data (&str)));
1684 /* unsigned little */
1685 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1686 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1688 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1689 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1690 _dbus_string_get_const_data (&str)));
1693 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1694 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1696 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1697 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1698 _dbus_string_get_const_data (&str)));
1700 /* unsigned little pack */
1701 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1703 _dbus_string_get_data (&str));
1705 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1706 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1707 _dbus_string_get_const_data (&str)));
1709 /* unsigned big pack */
1710 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1712 _dbus_string_get_data (&str));
1714 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1715 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1716 _dbus_string_get_const_data (&str)));
1717 #endif /* DBUS_HAVE_INT64 */
1719 /* set/pack 32-bit integers */
1720 _dbus_string_set_length (&str, 4);
1723 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1726 _dbus_assert (-0x123456 ==
1727 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1728 _dbus_string_get_const_data (&str)));
1731 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1734 _dbus_assert (-0x123456 ==
1735 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1736 _dbus_string_get_const_data (&str)));
1738 /* signed little pack */
1739 _dbus_pack_int32 (-0x123456,
1741 _dbus_string_get_data (&str));
1743 _dbus_assert (-0x123456 ==
1744 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1745 _dbus_string_get_const_data (&str)));
1747 /* signed big pack */
1748 _dbus_pack_int32 (-0x123456,
1750 _dbus_string_get_data (&str));
1752 _dbus_assert (-0x123456 ==
1753 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1754 _dbus_string_get_const_data (&str)));
1756 /* unsigned little */
1757 _dbus_marshal_set_uint32 (&str,
1759 DBUS_LITTLE_ENDIAN);
1761 _dbus_assert (0x123456 ==
1762 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1763 _dbus_string_get_const_data (&str)));
1766 _dbus_marshal_set_uint32 (&str,
1770 _dbus_assert (0x123456 ==
1771 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1772 _dbus_string_get_const_data (&str)));
1774 /* unsigned little pack */
1775 _dbus_pack_uint32 (0x123456,
1777 _dbus_string_get_data (&str));
1779 _dbus_assert (0x123456 ==
1780 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1781 _dbus_string_get_const_data (&str)));
1783 /* unsigned big pack */
1784 _dbus_pack_uint32 (0x123456,
1786 _dbus_string_get_data (&str));
1788 _dbus_assert (0x123456 ==
1789 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1790 _dbus_string_get_const_data (&str)));
1792 #endif /* set/pack tests for integers */
1794 /* Strings in-place set */
1795 byte_order = DBUS_LITTLE_ENDIAN;
1799 _dbus_string_set_length (&str, 0);
1801 /* reset pos for the macros */
1804 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1806 /* Set it to something longer */
1807 _dbus_string_init_const (&t, "Hello world foo");
1809 v_STRING = _dbus_string_get_const_data (&t);
1810 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1811 &v_STRING, byte_order, NULL, NULL);
1813 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1814 &v_STRING, byte_order,
1816 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1818 /* Set it to something shorter */
1819 _dbus_string_init_const (&t, "Hello");
1821 v_STRING = _dbus_string_get_const_data (&t);
1822 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1823 &v_STRING, byte_order, NULL, NULL);
1824 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1825 &v_STRING, byte_order,
1827 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1829 /* Do the other byte order */
1830 if (byte_order == DBUS_LITTLE_ENDIAN)
1831 byte_order = DBUS_BIG_ENDIAN;
1837 _dbus_string_free (&str);
1842 #endif /* DBUS_BUILD_TESTS */