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);
107 unpack_4_octets (int byte_order,
108 const unsigned char *data)
110 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
112 if (byte_order == DBUS_LITTLE_ENDIAN)
113 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
115 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
118 #ifndef DBUS_HAVE_INT64
121 swap_bytes (unsigned char *data,
124 unsigned char *p1 = data;
125 unsigned char *p2 = data + len - 1;
129 unsigned char tmp = *p1;
137 #endif /* !DBUS_HAVE_INT64 */
140 swap_8_octets (DBusBasicValue *value,
143 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
145 #ifdef DBUS_HAVE_INT64
146 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
148 swap_bytes ((unsigned char *)value, 8);
154 static DBusBasicValue
155 unpack_8_octets (int byte_order,
156 const unsigned char *data)
160 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
161 _dbus_assert (sizeof (r) == 8);
163 #ifdef DBUS_HAVE_INT64
164 if (byte_order == DBUS_LITTLE_ENDIAN)
165 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
167 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
169 r.u64 = *(DBus8ByteStruct*)data;
170 swap_8_octets (&r, byte_order);
178 * Unpacks a 32 bit unsigned integer from a data pointer
180 * @param byte_order The byte order to use
181 * @param data the data pointer
182 * @returns the integer
185 _dbus_unpack_uint32 (int byte_order,
186 const unsigned char *data)
188 return unpack_4_octets (byte_order, data);
192 * Unpacks a 32 bit signed integer from a data pointer
194 * @param byte_order The byte order to use
195 * @param data the data pointer
196 * @returns the integer
199 _dbus_unpack_int32 (int byte_order,
200 const unsigned char *data)
202 return (dbus_int32_t) unpack_4_octets (byte_order, data);
206 set_4_octets (DBusString *str,
213 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
214 byte_order == DBUS_BIG_ENDIAN);
216 data = _dbus_string_get_data_len (str, offset, 4);
218 _dbus_pack_uint32 (value, byte_order, data);
222 set_8_octets (DBusString *str,
224 DBusBasicValue value,
229 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
230 byte_order == DBUS_BIG_ENDIAN);
232 data = _dbus_string_get_data_len (str, offset, 8);
234 pack_8_octets (value, byte_order, data);
238 * Sets the 4 bytes at the given offset to a marshaled unsigned
239 * integer, replacing anything found there previously.
241 * @param str the string to write the marshalled int to
242 * @param pos the byte offset where int should be written
243 * @param value the value
244 * @param byte_order the byte order to use
248 _dbus_marshal_set_uint32 (DBusString *str,
253 set_4_octets (str, pos, value, byte_order);
257 * Sets the existing marshaled string at the given offset with
258 * a new marshaled string. The given offset must point to
259 * an existing string or the wrong length will be deleted
260 * and replaced with the new string.
262 * Note: no attempt is made by this function to re-align
263 * any data which has been already marshalled after this
264 * string. Use with caution.
266 * @param str the string to write the marshalled string to
267 * @param pos the position of the marshaled string length
268 * @param value the value
269 * @param byte_order the byte order to use
270 * @param old_end_pos place to store byte after the nul byte of the old value
271 * @param new_end_pos place to store byte after the nul byte of the new value
272 * @returns #TRUE on success, #FALSE if no memory
276 set_string (DBusString *str,
283 int old_len, new_len;
286 _dbus_string_init_const (&dstr, value);
288 old_len = _dbus_marshal_read_uint32 (str, pos, byte_order, NULL);
290 new_len = _dbus_string_get_length (&dstr);
292 if (!_dbus_string_replace_len (&dstr, 0, new_len,
293 str, pos + 4, old_len))
296 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
299 *old_end_pos = pos + 4 + old_len + 1;
301 *new_end_pos = pos + 4 + new_len + 1;
307 * Sets the existing marshaled signature at the given offset to a new
308 * marshaled signature. Same basic ideas as set_string().
310 * @param str the string to write the marshalled signature to
311 * @param pos the position of the marshaled signature length
312 * @param value the value
313 * @param byte_order the byte order to use
314 * @param old_end_pos place to store byte after the nul byte of the old value
315 * @param new_end_pos place to store byte after the nul byte of the new value
316 * @returns #TRUE on success, #FALSE if no memory
320 set_signature (DBusString *str,
327 int old_len, new_len;
330 _dbus_string_init_const (&dstr, value);
332 old_len = _dbus_string_get_byte (str, pos);
333 new_len = _dbus_string_get_length (&dstr);
335 if (!_dbus_string_replace_len (&dstr, 0, new_len,
336 str, pos + 1, old_len))
339 _dbus_string_set_byte (str, pos, new_len);
342 *old_end_pos = pos + 1 + old_len + 1;
344 *new_end_pos = pos + 1 + new_len + 1;
350 * Sets an existing basic type value to a new value.
351 * Arguments work the same way as _dbus_marshal_basic_type().
353 * @param str the string
354 * @param pos location of the current value
355 * @param type the type of the current and new values
356 * @param value the address of the new value
357 * @param byte_order byte order for marshaling
358 * @param old_end_pos location to store end position of the old value, or #NULL
359 * @param new_end_pos location to store end position of the new value, or #NULL
360 * @returns #FALSE if no memory
363 _dbus_marshal_set_basic (DBusString *str,
371 const DBusBasicValue *vp;
378 case DBUS_TYPE_BOOLEAN:
379 _dbus_string_set_byte (str, pos, vp->byt);
381 *old_end_pos = pos + 1;
383 *new_end_pos = pos + 1;
386 case DBUS_TYPE_INT32:
387 case DBUS_TYPE_UINT32:
388 pos = _DBUS_ALIGN_VALUE (pos, 4);
389 set_4_octets (str, pos, vp->u32, byte_order);
391 *old_end_pos = pos + 4;
393 *new_end_pos = pos + 4;
396 case DBUS_TYPE_INT64:
397 case DBUS_TYPE_UINT64:
398 case DBUS_TYPE_DOUBLE:
399 pos = _DBUS_ALIGN_VALUE (pos, 8);
400 set_8_octets (str, pos, *vp, byte_order);
402 *old_end_pos = pos + 8;
404 *new_end_pos = pos + 8;
407 case DBUS_TYPE_STRING:
408 case DBUS_TYPE_OBJECT_PATH:
409 _dbus_assert (vp->str != NULL);
410 return set_string (str, pos, vp->str, byte_order,
411 old_end_pos, new_end_pos);
413 case DBUS_TYPE_SIGNATURE:
414 _dbus_assert (vp->str != NULL);
415 return set_signature (str, pos, vp->str, byte_order,
416 old_end_pos, new_end_pos);
419 _dbus_assert_not_reached ("not a basic type");
426 read_4_octets (const DBusString *str,
431 pos = _DBUS_ALIGN_VALUE (pos, 4);
436 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
438 return unpack_4_octets (byte_order,
439 _dbus_string_get_const_data (str) + pos);
443 * Convenience function to demarshal a 32 bit unsigned integer.
445 * @param str the string containing the data
446 * @param byte_order the byte order
447 * @param pos the position in the string
448 * @param new_pos the new position of the string
449 * @returns the demarshaled integer.
452 _dbus_marshal_read_uint32 (const DBusString *str,
457 return read_4_octets (str, pos, byte_order, new_pos);
461 * Demarshals a basic-typed value. The "value" pointer is always
462 * the address of a variable of the basic type. So e.g.
463 * if the basic type is "double" then the pointer is
464 * a double*, and if it's "char*" then the pointer is
467 * A value of type #DBusBasicValue is guaranteed to be large enough to
468 * hold any of the types that may be returned, which is handy if you
469 * are trying to do things generically. For example you can pass
470 * a DBusBasicValue* in to this function, and then pass the same
471 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
472 * move a value from one place to another.
474 * @param str the string containing the data
475 * @param pos position in the string
476 * @param type type of value to demarshal
477 * @param value pointer to return value data
478 * @param byte_order the byte order
479 * @param new_pos pointer to update with new position, or #NULL
482 _dbus_marshal_read_basic (const DBusString *str,
489 const char *str_data;
492 _dbus_assert (_dbus_type_is_basic (type));
494 str_data = _dbus_string_get_const_data (str);
500 case DBUS_TYPE_BOOLEAN:
501 vp->byt = _dbus_string_get_byte (str, pos);
504 case DBUS_TYPE_INT32:
505 case DBUS_TYPE_UINT32:
506 pos = _DBUS_ALIGN_VALUE (pos, 4);
507 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
508 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
509 vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
512 case DBUS_TYPE_INT64:
513 case DBUS_TYPE_UINT64:
514 case DBUS_TYPE_DOUBLE:
515 pos = _DBUS_ALIGN_VALUE (pos, 8);
516 #ifdef DBUS_HAVE_INT64
517 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
518 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
520 vp->u64 = *(dbus_uint64_t*)(str_data + pos);
522 vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
523 swap_8_octets (vp, byte_order);
527 case DBUS_TYPE_STRING:
528 case DBUS_TYPE_OBJECT_PATH:
532 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
534 vp->str = (char*) str_data + pos;
536 pos += len + 1; /* length plus nul */
539 case DBUS_TYPE_SIGNATURE:
543 len = _dbus_string_get_byte (str, pos);
546 vp->str = (char*) str_data + pos;
548 pos += len + 1; /* length plus nul */
552 _dbus_warn ("type %s not a basic type\n",
553 _dbus_type_to_string (type));
554 _dbus_assert_not_reached ("not a basic type");
563 * Reads a block of fixed-length basic values, as an optimization
564 * vs. reading each one individually into a new buffer.
566 * This function returns the data in-place; it does not make a copy,
567 * and it does not swap the bytes.
569 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
570 * and the "value" argument should be a "const double**" and so on.
572 * @todo we aren't using this function (except in the test suite)
574 * @param str the string to read from
575 * @param pos position to read from
576 * @param element_type type of array elements
577 * @param value place to return the array
578 * @param n_elements number of array elements to read
579 * @param byte_order the byte order, used to read the array length
580 * @param new_pos #NULL or location to store a position after the elements
583 _dbus_marshal_read_fixed_multi (const DBusString *str,
594 _dbus_assert (_dbus_type_is_fixed (element_type));
595 _dbus_assert (_dbus_type_is_basic (element_type));
598 _dbus_verbose ("reading %d elements of %s\n",
599 n_elements, _dbus_type_to_string (element_type));
602 alignment = _dbus_type_get_alignment (element_type);
604 pos = _DBUS_ALIGN_VALUE (pos, alignment);
606 array_len = n_elements * alignment;
608 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
610 *new_pos = pos + array_len;
614 marshal_4_octets (DBusString *str,
623 _dbus_assert (sizeof (value) == 4);
625 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
626 value = DBUS_UINT32_SWAP_LE_BE (value);
628 orig_len = _dbus_string_get_length (str);
630 retval = _dbus_string_insert_4_aligned (str, insert_at,
631 (const unsigned char *)&value);
635 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
636 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
643 marshal_8_octets (DBusString *str,
645 DBusBasicValue value,
652 _dbus_assert (sizeof (value) == 8);
654 swap_8_octets (&value, byte_order);
656 orig_len = _dbus_string_get_length (str);
658 retval = _dbus_string_insert_8_aligned (str, insert_at,
659 (const unsigned char *)&value);
662 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
670 MARSHAL_AS_SIGNATURE,
671 MARSHAL_AS_BYTE_ARRAY
675 marshal_len_followed_by_bytes (int marshal_as,
678 const unsigned char *value,
679 int data_len, /* doesn't include nul if any */
684 DBusString value_str;
687 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
688 if (insert_at > _dbus_string_get_length (str))
689 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
690 insert_at, _dbus_string_get_length (str), data_len);
692 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
693 value_len = data_len;
695 value_len = data_len + 1; /* value has a nul */
697 _dbus_string_init_const_len (&value_str, value, value_len);
701 if (marshal_as == MARSHAL_AS_SIGNATURE)
703 if (!_dbus_string_insert_byte (str, pos, data_len))
710 if (!marshal_4_octets (str, pos, data_len,
715 if (!_dbus_string_copy_len (&value_str, 0, value_len,
721 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
723 _dbus_verbose_bytes_of_string (str, pos, value_len);
734 /* Delete what we've inserted */
735 _dbus_string_delete (str, insert_at, pos - insert_at);
741 marshal_string (DBusString *str,
747 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
748 str, insert_at, value,
750 byte_order, pos_after);
754 marshal_signature (DBusString *str,
759 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
760 str, insert_at, value,
762 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
767 * Marshals a basic-typed value. The "value" pointer is always the
768 * address of a variable containing the basic type value.
769 * So for example for int32 it will be dbus_int32_t*, and
770 * for string it will be const char**. This is for symmetry
771 * with _dbus_marshal_read_basic() and to have a simple
774 * @param str string to marshal to
775 * @param insert_at where to insert the value
776 * @param type type of value
777 * @param value pointer to a variable containing the value
778 * @param byte_order byte order
779 * @param pos_after #NULL or the position after the type
780 * @returns #TRUE on success
783 _dbus_marshal_write_basic (DBusString *str,
790 const DBusBasicValue *vp;
792 _dbus_assert (_dbus_type_is_basic (type));
799 case DBUS_TYPE_BOOLEAN:
800 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
803 *pos_after = insert_at + 1;
806 case DBUS_TYPE_INT32:
807 case DBUS_TYPE_UINT32:
808 return marshal_4_octets (str, insert_at, vp->u32,
809 byte_order, pos_after);
811 case DBUS_TYPE_INT64:
812 case DBUS_TYPE_UINT64:
813 case DBUS_TYPE_DOUBLE:
814 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
817 case DBUS_TYPE_STRING:
818 case DBUS_TYPE_OBJECT_PATH:
819 _dbus_assert (vp->str != NULL);
820 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
822 case DBUS_TYPE_SIGNATURE:
823 _dbus_assert (vp->str != NULL);
824 return marshal_signature (str, insert_at, vp->str, pos_after);
827 _dbus_assert_not_reached ("not a basic type");
834 marshal_1_octets_array (DBusString *str,
836 const unsigned char *value,
842 DBusString value_str;
844 _dbus_string_init_const_len (&value_str, value, n_elements);
848 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
861 swap_array (DBusString *str,
867 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
869 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
874 /* we use const_data and cast it off so DBusString can be a const string
875 * for the unit tests. don't ask.
877 d = (unsigned char*) _dbus_string_get_const_data (str) + array_start;
878 end = d + n_elements * alignment;
884 #ifdef DBUS_HAVE_INT64
885 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
887 swap_8_bytes ((DBusBasicValue*) d);
894 _dbus_assert (alignment == 4);
898 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
906 marshal_fixed_multi (DBusString *str,
908 const DBusBasicValue *value,
919 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
921 old_string_len = _dbus_string_get_length (str);
923 len_in_bytes = n_elements * alignment;
924 array_start = insert_at;
926 /* Note that we do alignment padding unconditionally
927 * even if the array is empty; this means that
928 * padding + len is always equal to the number of bytes
932 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
935 _dbus_string_init_const_len (&t,
936 (const unsigned char*) value,
939 if (!_dbus_string_copy (&t, 0,
943 swap_array (str, array_start, n_elements, byte_order, alignment);
946 *pos_after = array_start + len_in_bytes;
951 _dbus_string_delete (str, insert_at,
952 _dbus_string_get_length (str) - old_string_len);
958 * Marshals a block of values of fixed-length type all at once, as an
959 * optimization. _dbus_type_is_fixed() returns #TRUE for fixed-length
960 * types, which are the basic types minus the string-like types.
962 * The value argument should be the adddress of an
963 * array, so e.g. "const dbus_uint32_t**"
965 * @param str string to marshal to
966 * @param insert_at where to insert the value
967 * @param element_type type of array elements
968 * @param value address of an array to marshal
969 * @param n_elements number of elements in the array
970 * @param byte_order byte order
971 * @param pos_after #NULL or the position after the type
972 * @returns #TRUE on success
975 _dbus_marshal_write_fixed_multi (DBusString *str,
983 const void* vp = *(const DBusBasicValue**)value;
985 _dbus_assert (_dbus_type_is_fixed (element_type));
986 _dbus_assert (n_elements >= 0);
989 _dbus_verbose ("writing %d elements of %s\n",
990 n_elements, _dbus_type_to_string (element_type));
993 switch (element_type)
995 case DBUS_TYPE_BOOLEAN:
996 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
997 * should we here too ? */
999 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1001 case DBUS_TYPE_INT32:
1002 case DBUS_TYPE_UINT32:
1003 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1005 case DBUS_TYPE_INT64:
1006 case DBUS_TYPE_UINT64:
1007 case DBUS_TYPE_DOUBLE:
1008 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1012 _dbus_assert_not_reached ("non fixed type in array write");
1021 * Skips over a basic-typed value, reporting the following position.
1023 * @param str the string containing the data
1024 * @param type type of value to read
1025 * @param byte_order the byte order
1026 * @param pos pointer to position in the string,
1027 * updated on return to new position
1030 _dbus_marshal_skip_basic (const DBusString *str,
1035 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1036 byte_order == DBUS_BIG_ENDIAN);
1040 case DBUS_TYPE_BYTE:
1041 case DBUS_TYPE_BOOLEAN:
1044 case DBUS_TYPE_INT32:
1045 case DBUS_TYPE_UINT32:
1046 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1049 case DBUS_TYPE_INT64:
1050 case DBUS_TYPE_UINT64:
1051 case DBUS_TYPE_DOUBLE:
1052 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1055 case DBUS_TYPE_STRING:
1056 case DBUS_TYPE_OBJECT_PATH:
1060 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1062 *pos += len + 1; /* length plus nul */
1065 case DBUS_TYPE_SIGNATURE:
1069 len = _dbus_string_get_byte (str, *pos);
1071 *pos += len + 2; /* length byte plus length plus nul */
1075 _dbus_warn ("type %s not a basic type\n",
1076 _dbus_type_to_string (type));
1077 _dbus_assert_not_reached ("not a basic type");
1083 * Skips an array, returning the next position.
1085 * @param str the string containing the data
1086 * @param element_type the type of array elements
1087 * @param byte_order the byte order
1088 * @param pos pointer to position in the string,
1089 * updated on return to new position
1092 _dbus_marshal_skip_array (const DBusString *str,
1097 dbus_uint32_t array_len;
1101 i = _DBUS_ALIGN_VALUE (*pos, 4);
1103 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1105 alignment = _dbus_type_get_alignment (element_type);
1107 i = _DBUS_ALIGN_VALUE (i, alignment);
1109 *pos = i + array_len;
1113 * Gets the alignment requirement for the given type;
1114 * will be 1, 4, or 8.
1116 * @param typecode the type
1117 * @returns alignment of 1, 4, or 8
1120 _dbus_type_get_alignment (int typecode)
1124 case DBUS_TYPE_BYTE:
1125 case DBUS_TYPE_BOOLEAN:
1126 case DBUS_TYPE_VARIANT:
1127 case DBUS_TYPE_SIGNATURE:
1129 case DBUS_TYPE_INT32:
1130 case DBUS_TYPE_UINT32:
1131 /* this stuff is 4 since it starts with a length */
1132 case DBUS_TYPE_STRING:
1133 case DBUS_TYPE_OBJECT_PATH:
1134 case DBUS_TYPE_ARRAY:
1136 case DBUS_TYPE_INT64:
1137 case DBUS_TYPE_UINT64:
1138 case DBUS_TYPE_DOUBLE:
1139 /* struct is 8 since it could contain an 8-aligned item
1140 * and it's simpler to just always align structs to 8;
1141 * we want the amount of padding in a struct of a given
1142 * type to be predictable, not location-dependent.
1144 case DBUS_TYPE_STRUCT:
1148 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1155 * Return #TRUE if the typecode is a valid typecode.
1156 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1157 * random unknown bytes aren't either. This function is safe with
1160 * @returns #TRUE if valid
1163 _dbus_type_is_valid (int typecode)
1167 case DBUS_TYPE_BYTE:
1168 case DBUS_TYPE_BOOLEAN:
1169 case DBUS_TYPE_INT32:
1170 case DBUS_TYPE_UINT32:
1171 case DBUS_TYPE_INT64:
1172 case DBUS_TYPE_UINT64:
1173 case DBUS_TYPE_DOUBLE:
1174 case DBUS_TYPE_STRING:
1175 case DBUS_TYPE_OBJECT_PATH:
1176 case DBUS_TYPE_SIGNATURE:
1177 case DBUS_TYPE_ARRAY:
1178 case DBUS_TYPE_STRUCT:
1179 case DBUS_TYPE_VARIANT:
1187 #define TYPE_IS_CONTAINER(typecode) \
1188 ((typecode) == DBUS_TYPE_STRUCT || \
1189 (typecode) == DBUS_TYPE_VARIANT || \
1190 (typecode) == DBUS_TYPE_ARRAY)
1193 * A "container type" can contain basic types, or nested
1194 * container types. #DBUS_TYPE_INVALID is not a container type.
1195 * This function will crash if passed a typecode that isn't
1196 * in dbus-protocol.h
1198 * @returns #TRUE if type is a container
1201 _dbus_type_is_container (int typecode)
1203 /* only reasonable (non-line-noise) typecodes are allowed */
1204 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1205 return TYPE_IS_CONTAINER (typecode);
1209 * A "basic type" is a somewhat arbitrary concept, but the intent
1210 * is to include those types that are fully-specified by a single
1211 * typecode, with no additional type information or nested
1212 * values. So all numbers and strings are basic types and
1213 * structs, arrays, and variants are not basic types.
1214 * #DBUS_TYPE_INVALID is not a basic type.
1216 * This function is defined to return #TRUE for exactly those
1217 * types that can be written with _dbus_marshal_basic_type()
1218 * and read with _dbus_marshal_read_basic().
1220 * This function will crash if passed a typecode that isn't
1221 * in dbus-protocol.h
1223 * @returns #TRUE if type is basic
1226 _dbus_type_is_basic (int typecode)
1228 /* only reasonable (non-line-noise) typecodes are allowed */
1229 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1231 /* everything that isn't invalid or a container */
1232 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
1236 * Tells you whether values of this type can change length if you set
1237 * them to some other value. For this purpose, you assume that the
1238 * first byte of the old and new value would be in the same location,
1239 * so alignment padding is not a factor.
1241 * @returns #FALSE if the type can occupy different lengths
1244 _dbus_type_is_fixed (int typecode)
1248 case DBUS_TYPE_BYTE:
1249 case DBUS_TYPE_BOOLEAN:
1250 case DBUS_TYPE_INT32:
1251 case DBUS_TYPE_UINT32:
1252 case DBUS_TYPE_INT64:
1253 case DBUS_TYPE_UINT64:
1254 case DBUS_TYPE_DOUBLE:
1262 * Returns a string describing the given type.
1264 * @param typecode the type to describe
1265 * @returns a constant string describing the type
1268 _dbus_type_to_string (int typecode)
1272 case DBUS_TYPE_INVALID:
1274 case DBUS_TYPE_BOOLEAN:
1276 case DBUS_TYPE_BYTE:
1278 case DBUS_TYPE_INT32:
1280 case DBUS_TYPE_UINT32:
1282 case DBUS_TYPE_DOUBLE:
1284 case DBUS_TYPE_STRING:
1286 case DBUS_TYPE_OBJECT_PATH:
1287 return "object_path";
1288 case DBUS_TYPE_SIGNATURE:
1290 case DBUS_TYPE_STRUCT:
1292 case DBUS_TYPE_ARRAY:
1294 case DBUS_TYPE_VARIANT:
1296 case DBUS_STRUCT_BEGIN_CHAR:
1297 return "begin_struct";
1298 case DBUS_STRUCT_END_CHAR:
1299 return "end_struct";
1306 * If in verbose mode, print a block of binary data.
1308 * @todo right now it prints even if not in verbose mode
1310 * @param data the data
1311 * @param len the length of the data
1312 * @param offset where to start counting for byte indexes
1315 _dbus_verbose_bytes (const unsigned char *data,
1320 const unsigned char *aligned;
1322 _dbus_assert (len >= 0);
1324 /* Print blanks on first row if appropriate */
1325 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1328 _dbus_assert (aligned <= data);
1330 if (aligned != data)
1332 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1333 while (aligned != data)
1335 _dbus_verbose (" ");
1340 /* now print the bytes */
1344 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1346 _dbus_verbose ("%4d\t%p: ",
1347 offset + i, &data[i]);
1350 if (data[i] >= 32 &&
1352 _dbus_verbose (" '%c' ", data[i]);
1354 _dbus_verbose ("0x%s%x ",
1355 data[i] <= 0xf ? "0" : "", data[i]);
1359 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1362 _dbus_verbose ("BE: %d LE: %d",
1363 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1364 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1367 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1369 #ifdef DBUS_HAVE_INT64
1370 /* I think I probably mean "GNU libc printf" and not "GNUC"
1371 * but we'll wait until someone complains. If you hit this,
1372 * just turn off verbose mode as a workaround.
1375 _dbus_verbose (" u64: 0x%llx",
1376 *(dbus_uint64_t*)&data[i-8]);
1379 _dbus_verbose (" dbl: %g",
1380 *(double*)&data[i-8]);
1383 _dbus_verbose ("\n");
1387 _dbus_verbose ("\n");
1391 * Dump the given part of the string to verbose log.
1393 * @param str the string
1394 * @param start the start of range to dump
1395 * @param len length of range
1398 _dbus_verbose_bytes_of_string (const DBusString *str,
1405 real_len = _dbus_string_get_length (str);
1407 _dbus_assert (start >= 0);
1409 if (start > real_len)
1411 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1412 start, len, real_len);
1416 if ((start + len) > real_len)
1418 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1419 start, len, real_len);
1420 len = real_len - start;
1423 d = _dbus_string_get_const_data_len (str, start, len);
1425 _dbus_verbose_bytes (d, len, start);
1430 #ifdef DBUS_BUILD_TESTS
1431 #include "dbus-test.h"
1435 swap_test_array (void *array,
1445 _dbus_string_init_const_len (&t, array, len_bytes);
1446 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1449 #define MARSHAL_BASIC(typename, byte_order, literal) \
1451 v_##typename = literal; \
1452 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1454 byte_order, NULL)) \
1455 _dbus_assert_not_reached ("no memory"); \
1458 #define DEMARSHAL_BASIC(typename, byte_order) \
1460 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1461 byte_order, &pos); \
1464 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1466 DEMARSHAL_BASIC (typename, byte_order); \
1467 if (literal != v_##typename) \
1469 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1470 _dbus_string_get_length (&str) - dump_pos); \
1471 _dbus_assert_not_reached ("demarshaled wrong value"); \
1475 #define MARSHAL_TEST(typename, byte_order, literal) \
1477 MARSHAL_BASIC (typename, byte_order, literal); \
1479 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1482 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1484 MARSHAL_BASIC (typename, byte_order, literal); \
1486 DEMARSHAL_BASIC (typename, byte_order); \
1487 if (strcmp (literal, v_##typename) != 0) \
1489 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1490 _dbus_string_get_length (&str) - dump_pos); \
1491 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1492 _dbus_assert_not_reached ("demarshaled wrong value"); \
1496 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1499 v_UINT32 = sizeof(literal); \
1500 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1501 byte_order, &next)) \
1502 _dbus_assert_not_reached ("no memory"); \
1503 v_ARRAY_##typename = literal; \
1504 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1505 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1506 byte_order, NULL)) \
1507 _dbus_assert_not_reached ("no memory"); \
1510 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1513 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1514 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1515 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1516 v_UINT32/alignment, \
1517 byte_order, NULL); \
1518 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1519 byte_order, alignment); \
1522 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1524 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1525 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1527 _dbus_verbose ("MARSHALED DATA\n"); \
1528 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1529 _dbus_string_get_length (&str) - dump_pos); \
1530 _dbus_verbose ("LITERAL DATA\n"); \
1531 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1532 _dbus_verbose ("READ DATA\n"); \
1533 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1534 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1538 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1540 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1542 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1546 _dbus_marshal_test (void)
1551 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1552 dbus_int32_t array4[3] = { 123, 456, 789 };
1553 #ifdef DBUS_HAVE_INT64
1554 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1555 DBUS_INT64_CONSTANT (0x456ffffffff),
1556 DBUS_INT64_CONSTANT (0x789ffffffff) };
1557 dbus_int64_t *v_ARRAY_INT64;
1559 unsigned char *v_ARRAY_BYTE;
1560 dbus_int32_t *v_ARRAY_INT32;
1561 double *v_ARRAY_DOUBLE;
1565 dbus_int32_t v_INT32;
1566 dbus_uint32_t v_UINT32;
1567 dbus_int64_t v_INT64;
1568 dbus_uint64_t v_UINT64;
1569 unsigned char v_BYTE;
1570 unsigned char v_BOOLEAN;
1571 const char *v_STRING;
1572 const char *v_SIGNATURE;
1573 const char *v_OBJECT_PATH;
1576 if (!_dbus_string_init (&str))
1577 _dbus_assert_not_reached ("failed to init string");
1581 /* Marshal doubles */
1582 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1583 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1585 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1586 _dbus_assert_not_reached ("got wrong double value");
1588 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1589 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1591 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1592 _dbus_assert_not_reached ("got wrong double value");
1594 /* Marshal signed integers */
1595 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1596 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1598 /* Marshal unsigned integers */
1599 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1600 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1602 #ifdef DBUS_HAVE_INT64
1603 /* Marshal signed integers */
1604 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1605 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1607 /* Marshal unsigned integers */
1608 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1609 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1610 #endif /* DBUS_HAVE_INT64 */
1613 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1614 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1616 /* Marshal all possible bools! */
1617 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1618 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1619 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1620 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1622 /* Marshal strings */
1623 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1624 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1625 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1626 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1629 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1630 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1633 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1634 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1635 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1636 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1639 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1640 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1642 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1643 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1645 #ifdef DBUS_HAVE_INT64
1646 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1647 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1653 * FIXME restore the set/pack tests
1656 #ifdef DBUS_HAVE_INT64
1657 /* set/pack 64-bit integers */
1658 _dbus_string_set_length (&str, 8);
1661 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1662 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1664 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1665 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1666 _dbus_string_get_const_data (&str)));
1669 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1670 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1672 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1673 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1674 _dbus_string_get_const_data (&str)));
1676 /* signed little pack */
1677 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1679 _dbus_string_get_data (&str));
1681 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1682 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1683 _dbus_string_get_const_data (&str)));
1685 /* signed big pack */
1686 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1688 _dbus_string_get_data (&str));
1690 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1691 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1692 _dbus_string_get_const_data (&str)));
1694 /* unsigned little */
1695 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1696 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1698 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1699 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1700 _dbus_string_get_const_data (&str)));
1703 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1704 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1706 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1707 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1708 _dbus_string_get_const_data (&str)));
1710 /* unsigned little pack */
1711 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1713 _dbus_string_get_data (&str));
1715 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1716 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1717 _dbus_string_get_const_data (&str)));
1719 /* unsigned big pack */
1720 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1722 _dbus_string_get_data (&str));
1724 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1725 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1726 _dbus_string_get_const_data (&str)));
1727 #endif /* DBUS_HAVE_INT64 */
1729 /* set/pack 32-bit integers */
1730 _dbus_string_set_length (&str, 4);
1733 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1736 _dbus_assert (-0x123456 ==
1737 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1738 _dbus_string_get_const_data (&str)));
1741 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1744 _dbus_assert (-0x123456 ==
1745 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1746 _dbus_string_get_const_data (&str)));
1748 /* signed little pack */
1749 _dbus_pack_int32 (-0x123456,
1751 _dbus_string_get_data (&str));
1753 _dbus_assert (-0x123456 ==
1754 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1755 _dbus_string_get_const_data (&str)));
1757 /* signed big pack */
1758 _dbus_pack_int32 (-0x123456,
1760 _dbus_string_get_data (&str));
1762 _dbus_assert (-0x123456 ==
1763 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1764 _dbus_string_get_const_data (&str)));
1766 /* unsigned little */
1767 _dbus_marshal_set_uint32 (&str,
1769 DBUS_LITTLE_ENDIAN);
1771 _dbus_assert (0x123456 ==
1772 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1773 _dbus_string_get_const_data (&str)));
1776 _dbus_marshal_set_uint32 (&str,
1780 _dbus_assert (0x123456 ==
1781 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1782 _dbus_string_get_const_data (&str)));
1784 /* unsigned little pack */
1785 _dbus_pack_uint32 (0x123456,
1787 _dbus_string_get_data (&str));
1789 _dbus_assert (0x123456 ==
1790 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1791 _dbus_string_get_const_data (&str)));
1793 /* unsigned big pack */
1794 _dbus_pack_uint32 (0x123456,
1796 _dbus_string_get_data (&str));
1798 _dbus_assert (0x123456 ==
1799 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1800 _dbus_string_get_const_data (&str)));
1802 #endif /* set/pack tests for integers */
1804 /* Strings in-place set */
1805 byte_order = DBUS_LITTLE_ENDIAN;
1809 _dbus_string_set_length (&str, 0);
1811 /* reset pos for the macros */
1814 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1816 /* Set it to something longer */
1817 _dbus_string_init_const (&t, "Hello world foo");
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);
1823 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1824 &v_STRING, byte_order,
1826 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1828 /* Set it to something shorter */
1829 _dbus_string_init_const (&t, "Hello");
1831 v_STRING = _dbus_string_get_const_data (&t);
1832 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1833 &v_STRING, byte_order, NULL, NULL);
1834 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1835 &v_STRING, byte_order,
1837 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1839 /* Do the other byte order */
1840 if (byte_order == DBUS_LITTLE_ENDIAN)
1841 byte_order = DBUS_BIG_ENDIAN;
1847 _dbus_string_free (&str);
1852 #endif /* DBUS_BUILD_TESTS */