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_2_octets (dbus_uint16_t value,
50 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
52 if ((byte_order) == DBUS_LITTLE_ENDIAN)
53 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
55 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
59 pack_4_octets (dbus_uint32_t value,
63 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
65 if ((byte_order) == DBUS_LITTLE_ENDIAN)
66 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
68 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
72 pack_8_octets (DBusBasicValue value,
76 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
78 #ifdef DBUS_HAVE_INT64
79 if ((byte_order) == DBUS_LITTLE_ENDIAN)
80 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
82 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
84 *(DBus8ByteStruct*)data = value.u64;
85 swap_8_octets ((DBusBasicValue*)data, byte_order);
90 * Packs a 32 bit unsigned integer into a data pointer.
92 * @param value the value
93 * @param byte_order the byte order to use
94 * @param data the data pointer
97 _dbus_pack_uint32 (dbus_uint32_t value,
101 pack_4_octets (value, byte_order, data);
104 #ifndef DBUS_HAVE_INT64
107 swap_bytes (unsigned char *data,
110 unsigned char *p1 = data;
111 unsigned char *p2 = data + len - 1;
115 unsigned char tmp = *p1;
123 #endif /* !DBUS_HAVE_INT64 */
126 swap_8_octets (DBusBasicValue *value,
129 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
131 #ifdef DBUS_HAVE_INT64
132 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
134 swap_bytes ((unsigned char *)value, 8);
140 static DBusBasicValue
141 unpack_8_octets (int byte_order,
142 const unsigned char *data)
146 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
147 _dbus_assert (sizeof (r) == 8);
149 #ifdef DBUS_HAVE_INT64
150 if (byte_order == DBUS_LITTLE_ENDIAN)
151 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
153 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
155 r.u64 = *(DBus8ByteStruct*)data;
156 swap_8_octets (&r, byte_order);
163 #ifndef _dbus_unpack_uint16
165 * Unpacks a 16 bit unsigned integer from a data pointer
167 * @param byte_order The byte order to use
168 * @param data the data pointer
169 * @returns the integer
172 _dbus_unpack_uint16 (int byte_order,
173 const unsigned char *data)
175 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
177 if (byte_order == DBUS_LITTLE_ENDIAN)
178 return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
180 return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
182 #endif /* _dbus_unpack_uint16 */
184 #ifndef _dbus_unpack_uint32
186 * Unpacks a 32 bit unsigned integer from a data pointer
188 * @param byte_order The byte order to use
189 * @param data the data pointer
190 * @returns the integer
193 _dbus_unpack_uint32 (int byte_order,
194 const unsigned char *data)
196 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
198 if (byte_order == DBUS_LITTLE_ENDIAN)
199 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
201 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
203 #endif /* _dbus_unpack_uint32 */
206 set_2_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, 2);
218 pack_2_octets (value, byte_order, data);
222 set_4_octets (DBusString *str,
229 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
230 byte_order == DBUS_BIG_ENDIAN);
232 data = _dbus_string_get_data_len (str, offset, 4);
234 pack_4_octets (value, byte_order, data);
238 set_8_octets (DBusString *str,
240 DBusBasicValue value,
245 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
246 byte_order == DBUS_BIG_ENDIAN);
248 data = _dbus_string_get_data_len (str, offset, 8);
250 pack_8_octets (value, byte_order, data);
254 * Sets the 4 bytes at the given offset to a marshaled unsigned
255 * integer, replacing anything found there previously.
257 * @param str the string to write the marshalled int to
258 * @param pos the byte offset where int should be written
259 * @param value the value
260 * @param byte_order the byte order to use
264 _dbus_marshal_set_uint32 (DBusString *str,
269 set_4_octets (str, pos, value, byte_order);
273 * Sets the existing marshaled string at the given offset with
274 * a new marshaled string. The given offset must point to
275 * an existing string or the wrong length will be deleted
276 * and replaced with the new string.
278 * Note: no attempt is made by this function to re-align
279 * any data which has been already marshalled after this
280 * string. Use with caution.
282 * @param str the string to write the marshalled string to
283 * @param pos the position of the marshaled string length
284 * @param value the value
285 * @param byte_order the byte order to use
286 * @param old_end_pos place to store byte after the nul byte of the old value
287 * @param new_end_pos place to store byte after the nul byte of the new value
288 * @returns #TRUE on success, #FALSE if no memory
292 set_string (DBusString *str,
299 int old_len, new_len;
302 _dbus_string_init_const (&dstr, value);
304 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
305 old_len = _dbus_unpack_uint32 (byte_order,
306 _dbus_string_get_const_data_len (str, pos, 4));
308 new_len = _dbus_string_get_length (&dstr);
310 if (!_dbus_string_replace_len (&dstr, 0, new_len,
311 str, pos + 4, old_len))
314 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
317 *old_end_pos = pos + 4 + old_len + 1;
319 *new_end_pos = pos + 4 + new_len + 1;
325 * Sets the existing marshaled signature at the given offset to a new
326 * marshaled signature. Same basic ideas as set_string().
328 * @param str the string to write the marshalled signature to
329 * @param pos the position of the marshaled signature length
330 * @param value the value
331 * @param byte_order the byte order to use
332 * @param old_end_pos place to store byte after the nul byte of the old value
333 * @param new_end_pos place to store byte after the nul byte of the new value
334 * @returns #TRUE on success, #FALSE if no memory
338 set_signature (DBusString *str,
345 int old_len, new_len;
348 _dbus_string_init_const (&dstr, value);
350 old_len = _dbus_string_get_byte (str, pos);
351 new_len = _dbus_string_get_length (&dstr);
353 if (!_dbus_string_replace_len (&dstr, 0, new_len,
354 str, pos + 1, old_len))
357 _dbus_string_set_byte (str, pos, new_len);
360 *old_end_pos = pos + 1 + old_len + 1;
362 *new_end_pos = pos + 1 + new_len + 1;
368 * Sets an existing basic type value to a new value.
369 * Arguments work the same way as _dbus_marshal_basic_type().
371 * @param str the string
372 * @param pos location of the current value
373 * @param type the type of the current and new values
374 * @param value the address of the new value
375 * @param byte_order byte order for marshaling
376 * @param old_end_pos location to store end position of the old value, or #NULL
377 * @param new_end_pos location to store end position of the new value, or #NULL
378 * @returns #FALSE if no memory
381 _dbus_marshal_set_basic (DBusString *str,
389 const DBusBasicValue *vp;
396 _dbus_string_set_byte (str, pos, vp->byt);
398 *old_end_pos = pos + 1;
400 *new_end_pos = pos + 1;
403 case DBUS_TYPE_INT16:
404 case DBUS_TYPE_UINT16:
405 pos = _DBUS_ALIGN_VALUE (pos, 2);
406 set_2_octets (str, pos, vp->u16, byte_order);
408 *old_end_pos = pos + 2;
410 *new_end_pos = pos + 2;
413 case DBUS_TYPE_BOOLEAN:
414 case DBUS_TYPE_INT32:
415 case DBUS_TYPE_UINT32:
416 pos = _DBUS_ALIGN_VALUE (pos, 4);
417 set_4_octets (str, pos, vp->u32, byte_order);
419 *old_end_pos = pos + 4;
421 *new_end_pos = pos + 4;
424 case DBUS_TYPE_INT64:
425 case DBUS_TYPE_UINT64:
426 case DBUS_TYPE_DOUBLE:
427 pos = _DBUS_ALIGN_VALUE (pos, 8);
428 set_8_octets (str, pos, *vp, byte_order);
430 *old_end_pos = pos + 8;
432 *new_end_pos = pos + 8;
435 case DBUS_TYPE_STRING:
436 case DBUS_TYPE_OBJECT_PATH:
437 pos = _DBUS_ALIGN_VALUE (pos, 4);
438 _dbus_assert (vp->str != NULL);
439 return set_string (str, pos, vp->str, byte_order,
440 old_end_pos, new_end_pos);
442 case DBUS_TYPE_SIGNATURE:
443 _dbus_assert (vp->str != NULL);
444 return set_signature (str, pos, vp->str, byte_order,
445 old_end_pos, new_end_pos);
448 _dbus_assert_not_reached ("not a basic type");
455 * Convenience function to demarshal a 32 bit unsigned integer.
457 * @param str the string containing the data
458 * @param byte_order the byte order
459 * @param pos the position in the string
460 * @param new_pos the new position of the string
461 * @returns the demarshaled integer.
464 _dbus_marshal_read_uint32 (const DBusString *str,
469 pos = _DBUS_ALIGN_VALUE (pos, 4);
474 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
476 return _dbus_unpack_uint32 (byte_order,
477 _dbus_string_get_const_data (str) + pos);
481 * Demarshals a basic-typed value. The "value" pointer is always
482 * the address of a variable of the basic type. So e.g.
483 * if the basic type is "double" then the pointer is
484 * a double*, and if it's "char*" then the pointer is
487 * A value of type #DBusBasicValue is guaranteed to be large enough to
488 * hold any of the types that may be returned, which is handy if you
489 * are trying to do things generically. For example you can pass
490 * a DBusBasicValue* in to this function, and then pass the same
491 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
492 * move a value from one place to another.
494 * @param str the string containing the data
495 * @param pos position in the string
496 * @param type type of value to demarshal
497 * @param value pointer to return value data
498 * @param byte_order the byte order
499 * @param new_pos pointer to update with new position, or #NULL
502 _dbus_marshal_read_basic (const DBusString *str,
509 const char *str_data;
512 _dbus_assert (_dbus_type_is_basic (type));
514 str_data = _dbus_string_get_const_data (str);
520 vp->byt = _dbus_string_get_byte (str, pos);
523 case DBUS_TYPE_INT16:
524 case DBUS_TYPE_UINT16:
525 pos = _DBUS_ALIGN_VALUE (pos, 2);
526 vp->u16 = *(dbus_uint16_t *)(str_data + pos);
527 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
528 vp->u16 = DBUS_UINT16_SWAP_LE_BE (vp->u16);
531 case DBUS_TYPE_INT32:
532 case DBUS_TYPE_UINT32:
533 case DBUS_TYPE_BOOLEAN:
534 pos = _DBUS_ALIGN_VALUE (pos, 4);
535 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
536 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
537 vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
540 case DBUS_TYPE_INT64:
541 case DBUS_TYPE_UINT64:
542 case DBUS_TYPE_DOUBLE:
543 pos = _DBUS_ALIGN_VALUE (pos, 8);
544 #ifdef DBUS_HAVE_INT64
545 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
546 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
548 vp->u64 = *(dbus_uint64_t*)(str_data + pos);
550 vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
551 swap_8_octets (vp, byte_order);
555 case DBUS_TYPE_STRING:
556 case DBUS_TYPE_OBJECT_PATH:
560 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
562 vp->str = (char*) str_data + pos;
564 pos += len + 1; /* length plus nul */
567 case DBUS_TYPE_SIGNATURE:
571 len = _dbus_string_get_byte (str, pos);
574 vp->str = (char*) str_data + pos;
576 pos += len + 1; /* length plus nul */
580 _dbus_warn ("type %s not a basic type\n",
581 _dbus_type_to_string (type));
582 _dbus_assert_not_reached ("not a basic type");
591 * Reads a block of fixed-length basic values, as an optimization
592 * vs. reading each one individually into a new buffer.
594 * This function returns the data in-place; it does not make a copy,
595 * and it does not swap the bytes.
597 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
598 * and the "value" argument should be a "const double**" and so on.
600 * @todo we aren't using this function (except in the test suite)
602 * @param str the string to read from
603 * @param pos position to read from
604 * @param element_type type of array elements
605 * @param value place to return the array
606 * @param n_elements number of array elements to read
607 * @param byte_order the byte order, used to read the array length
608 * @param new_pos #NULL or location to store a position after the elements
611 _dbus_marshal_read_fixed_multi (const DBusString *str,
622 _dbus_assert (_dbus_type_is_fixed (element_type));
623 _dbus_assert (_dbus_type_is_basic (element_type));
626 _dbus_verbose ("reading %d elements of %s\n",
627 n_elements, _dbus_type_to_string (element_type));
630 alignment = _dbus_type_get_alignment (element_type);
632 pos = _DBUS_ALIGN_VALUE (pos, alignment);
634 array_len = n_elements * alignment;
636 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
638 *new_pos = pos + array_len;
642 marshal_2_octets (DBusString *str,
651 _dbus_assert (sizeof (value) == 2);
653 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
654 value = DBUS_UINT16_SWAP_LE_BE (value);
656 orig_len = _dbus_string_get_length (str);
658 retval = _dbus_string_insert_2_aligned (str, insert_at,
659 (const unsigned char *)&value);
663 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
664 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
671 marshal_4_octets (DBusString *str,
680 _dbus_assert (sizeof (value) == 4);
682 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
683 value = DBUS_UINT32_SWAP_LE_BE (value);
685 orig_len = _dbus_string_get_length (str);
687 retval = _dbus_string_insert_4_aligned (str, insert_at,
688 (const unsigned char *)&value);
692 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
693 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
700 marshal_8_octets (DBusString *str,
702 DBusBasicValue value,
709 _dbus_assert (sizeof (value) == 8);
711 swap_8_octets (&value, byte_order);
713 orig_len = _dbus_string_get_length (str);
715 retval = _dbus_string_insert_8_aligned (str, insert_at,
716 (const unsigned char *)&value);
719 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
727 MARSHAL_AS_SIGNATURE,
728 MARSHAL_AS_BYTE_ARRAY
732 marshal_len_followed_by_bytes (int marshal_as,
735 const unsigned char *value,
736 int data_len, /* doesn't include nul if any */
741 DBusString value_str;
744 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
745 if (insert_at > _dbus_string_get_length (str))
746 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
747 insert_at, _dbus_string_get_length (str), data_len);
749 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
750 value_len = data_len;
752 value_len = data_len + 1; /* value has a nul */
754 _dbus_string_init_const_len (&value_str, value, value_len);
758 if (marshal_as == MARSHAL_AS_SIGNATURE)
760 if (!_dbus_string_insert_byte (str, pos, data_len))
767 if (!marshal_4_octets (str, pos, data_len,
772 if (!_dbus_string_copy_len (&value_str, 0, value_len,
778 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
780 _dbus_verbose_bytes_of_string (str, pos, value_len);
791 /* Delete what we've inserted */
792 _dbus_string_delete (str, insert_at, pos - insert_at);
798 marshal_string (DBusString *str,
804 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
805 str, insert_at, value,
807 byte_order, pos_after);
811 marshal_signature (DBusString *str,
816 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
817 str, insert_at, value,
819 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
824 * Marshals a basic-typed value. The "value" pointer is always the
825 * address of a variable containing the basic type value.
826 * So for example for int32 it will be dbus_int32_t*, and
827 * for string it will be const char**. This is for symmetry
828 * with _dbus_marshal_read_basic() and to have a simple
831 * @param str string to marshal to
832 * @param insert_at where to insert the value
833 * @param type type of value
834 * @param value pointer to a variable containing the value
835 * @param byte_order byte order
836 * @param pos_after #NULL or the position after the type
837 * @returns #TRUE on success
840 _dbus_marshal_write_basic (DBusString *str,
847 const DBusBasicValue *vp;
849 _dbus_assert (_dbus_type_is_basic (type));
856 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
859 *pos_after = insert_at + 1;
862 case DBUS_TYPE_INT16:
863 case DBUS_TYPE_UINT16:
864 return marshal_2_octets (str, insert_at, vp->u16,
865 byte_order, pos_after);
867 case DBUS_TYPE_BOOLEAN:
868 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
869 byte_order, pos_after);
871 case DBUS_TYPE_INT32:
872 case DBUS_TYPE_UINT32:
873 return marshal_4_octets (str, insert_at, vp->u32,
874 byte_order, pos_after);
876 case DBUS_TYPE_INT64:
877 case DBUS_TYPE_UINT64:
878 case DBUS_TYPE_DOUBLE:
879 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
882 case DBUS_TYPE_STRING:
883 case DBUS_TYPE_OBJECT_PATH:
884 _dbus_assert (vp->str != NULL);
885 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
887 case DBUS_TYPE_SIGNATURE:
888 _dbus_assert (vp->str != NULL);
889 return marshal_signature (str, insert_at, vp->str, pos_after);
892 _dbus_assert_not_reached ("not a basic type");
899 marshal_1_octets_array (DBusString *str,
901 const unsigned char *value,
907 DBusString value_str;
909 _dbus_string_init_const_len (&value_str, value, n_elements);
913 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
926 * Swaps the elements of an array to the opposite byte order
928 * @param data start of array
929 * @param n_elements number of elements
930 * @param alignment size of each element
933 _dbus_swap_array (unsigned char *data,
940 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
942 /* we use const_data and cast it off so DBusString can be a const string
943 * for the unit tests. don't ask.
946 end = d + (n_elements * alignment);
952 #ifdef DBUS_HAVE_INT64
953 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
955 swap_8_bytes ((DBusBasicValue*) d);
960 else if (alignment == 4)
964 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
970 _dbus_assert (alignment == 2);
974 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
981 swap_array (DBusString *str,
987 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
989 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
991 /* we use const_data and cast it off so DBusString can be a const string
992 * for the unit tests. don't ask.
994 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
995 n_elements, alignment);
1000 marshal_fixed_multi (DBusString *str,
1002 const DBusBasicValue *value,
1013 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
1015 old_string_len = _dbus_string_get_length (str);
1017 len_in_bytes = n_elements * alignment;
1018 array_start = insert_at;
1020 /* Note that we do alignment padding unconditionally
1021 * even if the array is empty; this means that
1022 * padding + len is always equal to the number of bytes
1026 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1029 _dbus_string_init_const_len (&t,
1030 (const unsigned char*) value,
1033 if (!_dbus_string_copy (&t, 0,
1037 swap_array (str, array_start, n_elements, byte_order, alignment);
1040 *pos_after = array_start + len_in_bytes;
1045 _dbus_string_delete (str, insert_at,
1046 _dbus_string_get_length (str) - old_string_len);
1052 * Marshals a block of values of fixed-length type all at once, as an
1053 * optimization. _dbus_type_is_fixed() returns #TRUE for fixed-length
1054 * types, which are the basic types minus the string-like types.
1056 * The value argument should be the adddress of an
1057 * array, so e.g. "const dbus_uint32_t**"
1059 * @param str string to marshal to
1060 * @param insert_at where to insert the value
1061 * @param element_type type of array elements
1062 * @param value address of an array to marshal
1063 * @param n_elements number of elements in the array
1064 * @param byte_order byte order
1065 * @param pos_after #NULL or the position after the type
1066 * @returns #TRUE on success
1069 _dbus_marshal_write_fixed_multi (DBusString *str,
1077 const void* vp = *(const DBusBasicValue**)value;
1079 _dbus_assert (_dbus_type_is_fixed (element_type));
1080 _dbus_assert (n_elements >= 0);
1083 _dbus_verbose ("writing %d elements of %s\n",
1084 n_elements, _dbus_type_to_string (element_type));
1087 switch (element_type)
1089 case DBUS_TYPE_BYTE:
1090 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1092 case DBUS_TYPE_INT16:
1093 case DBUS_TYPE_UINT16:
1094 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1095 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
1096 * should we here too ? */
1097 case DBUS_TYPE_BOOLEAN:
1098 case DBUS_TYPE_INT32:
1099 case DBUS_TYPE_UINT32:
1100 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1102 case DBUS_TYPE_INT64:
1103 case DBUS_TYPE_UINT64:
1104 case DBUS_TYPE_DOUBLE:
1105 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1109 _dbus_assert_not_reached ("non fixed type in array write");
1118 * Skips over a basic-typed value, reporting the following position.
1120 * @param str the string containing the data
1121 * @param type type of value to read
1122 * @param byte_order the byte order
1123 * @param pos pointer to position in the string,
1124 * updated on return to new position
1127 _dbus_marshal_skip_basic (const DBusString *str,
1132 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1133 byte_order == DBUS_BIG_ENDIAN);
1137 case DBUS_TYPE_BYTE:
1140 case DBUS_TYPE_INT16:
1141 case DBUS_TYPE_UINT16:
1142 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1145 case DBUS_TYPE_BOOLEAN:
1146 case DBUS_TYPE_INT32:
1147 case DBUS_TYPE_UINT32:
1148 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1151 case DBUS_TYPE_INT64:
1152 case DBUS_TYPE_UINT64:
1153 case DBUS_TYPE_DOUBLE:
1154 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1157 case DBUS_TYPE_STRING:
1158 case DBUS_TYPE_OBJECT_PATH:
1162 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1164 *pos += len + 1; /* length plus nul */
1167 case DBUS_TYPE_SIGNATURE:
1171 len = _dbus_string_get_byte (str, *pos);
1173 *pos += len + 2; /* length byte plus length plus nul */
1177 _dbus_warn ("type %s not a basic type\n",
1178 _dbus_type_to_string (type));
1179 _dbus_assert_not_reached ("not a basic type");
1185 * Skips an array, returning the next position.
1187 * @param str the string containing the data
1188 * @param element_type the type of array elements
1189 * @param byte_order the byte order
1190 * @param pos pointer to position in the string,
1191 * updated on return to new position
1194 _dbus_marshal_skip_array (const DBusString *str,
1199 dbus_uint32_t array_len;
1203 i = _DBUS_ALIGN_VALUE (*pos, 4);
1205 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1207 alignment = _dbus_type_get_alignment (element_type);
1209 i = _DBUS_ALIGN_VALUE (i, alignment);
1211 *pos = i + array_len;
1215 * Gets the alignment requirement for the given type;
1216 * will be 1, 4, or 8.
1218 * @param typecode the type
1219 * @returns alignment of 1, 4, or 8
1222 _dbus_type_get_alignment (int typecode)
1226 case DBUS_TYPE_BYTE:
1227 case DBUS_TYPE_VARIANT:
1228 case DBUS_TYPE_SIGNATURE:
1230 case DBUS_TYPE_INT16:
1231 case DBUS_TYPE_UINT16:
1233 case DBUS_TYPE_BOOLEAN:
1234 case DBUS_TYPE_INT32:
1235 case DBUS_TYPE_UINT32:
1236 /* this stuff is 4 since it starts with a length */
1237 case DBUS_TYPE_STRING:
1238 case DBUS_TYPE_OBJECT_PATH:
1239 case DBUS_TYPE_ARRAY:
1241 case DBUS_TYPE_INT64:
1242 case DBUS_TYPE_UINT64:
1243 case DBUS_TYPE_DOUBLE:
1244 /* struct is 8 since it could contain an 8-aligned item
1245 * and it's simpler to just always align structs to 8;
1246 * we want the amount of padding in a struct of a given
1247 * type to be predictable, not location-dependent.
1248 * DICT_ENTRY is always the same as struct.
1250 case DBUS_TYPE_STRUCT:
1251 case DBUS_TYPE_DICT_ENTRY:
1255 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1262 * Return #TRUE if the typecode is a valid typecode.
1263 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1264 * random unknown bytes aren't either. This function is safe with
1267 * @returns #TRUE if valid
1270 _dbus_type_is_valid (int typecode)
1274 case DBUS_TYPE_BYTE:
1275 case DBUS_TYPE_BOOLEAN:
1276 case DBUS_TYPE_INT16:
1277 case DBUS_TYPE_UINT16:
1278 case DBUS_TYPE_INT32:
1279 case DBUS_TYPE_UINT32:
1280 case DBUS_TYPE_INT64:
1281 case DBUS_TYPE_UINT64:
1282 case DBUS_TYPE_DOUBLE:
1283 case DBUS_TYPE_STRING:
1284 case DBUS_TYPE_OBJECT_PATH:
1285 case DBUS_TYPE_SIGNATURE:
1286 case DBUS_TYPE_ARRAY:
1287 case DBUS_TYPE_STRUCT:
1288 case DBUS_TYPE_DICT_ENTRY:
1289 case DBUS_TYPE_VARIANT:
1297 /** macro that checks whether a typecode is a container type */
1298 #define TYPE_IS_CONTAINER(typecode) \
1299 ((typecode) == DBUS_TYPE_STRUCT || \
1300 (typecode) == DBUS_TYPE_DICT_ENTRY || \
1301 (typecode) == DBUS_TYPE_VARIANT || \
1302 (typecode) == DBUS_TYPE_ARRAY)
1305 * A "container type" can contain basic types, or nested
1306 * container types. #DBUS_TYPE_INVALID is not a container type.
1307 * This function will crash if passed a typecode that isn't
1308 * in dbus-protocol.h
1310 * @returns #TRUE if type is a container
1313 _dbus_type_is_container (int typecode)
1315 /* only reasonable (non-line-noise) typecodes are allowed */
1316 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1317 return TYPE_IS_CONTAINER (typecode);
1321 * A "basic type" is a somewhat arbitrary concept, but the intent
1322 * is to include those types that are fully-specified by a single
1323 * typecode, with no additional type information or nested
1324 * values. So all numbers and strings are basic types and
1325 * structs, arrays, and variants are not basic types.
1326 * #DBUS_TYPE_INVALID is not a basic type.
1328 * This function is defined to return #TRUE for exactly those
1329 * types that can be written with _dbus_marshal_basic_type()
1330 * and read with _dbus_marshal_read_basic().
1332 * This function will crash if passed a typecode that isn't
1333 * in dbus-protocol.h
1335 * @returns #TRUE if type is basic
1338 _dbus_type_is_basic (int typecode)
1340 /* only reasonable (non-line-noise) typecodes are allowed */
1341 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1343 /* everything that isn't invalid or a container */
1344 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
1348 * Tells you whether values of this type can change length if you set
1349 * them to some other value. For this purpose, you assume that the
1350 * first byte of the old and new value would be in the same location,
1351 * so alignment padding is not a factor.
1353 * @returns #FALSE if the type can occupy different lengths
1356 _dbus_type_is_fixed (int typecode)
1360 case DBUS_TYPE_BYTE:
1361 case DBUS_TYPE_BOOLEAN:
1362 case DBUS_TYPE_INT16:
1363 case DBUS_TYPE_UINT16:
1364 case DBUS_TYPE_INT32:
1365 case DBUS_TYPE_UINT32:
1366 case DBUS_TYPE_INT64:
1367 case DBUS_TYPE_UINT64:
1368 case DBUS_TYPE_DOUBLE:
1376 * Returns a string describing the given type.
1378 * @param typecode the type to describe
1379 * @returns a constant string describing the type
1382 _dbus_type_to_string (int typecode)
1386 case DBUS_TYPE_INVALID:
1388 case DBUS_TYPE_BOOLEAN:
1390 case DBUS_TYPE_BYTE:
1392 case DBUS_TYPE_INT16:
1394 case DBUS_TYPE_UINT16:
1396 case DBUS_TYPE_INT32:
1398 case DBUS_TYPE_UINT32:
1400 case DBUS_TYPE_DOUBLE:
1402 case DBUS_TYPE_STRING:
1404 case DBUS_TYPE_OBJECT_PATH:
1405 return "object_path";
1406 case DBUS_TYPE_SIGNATURE:
1408 case DBUS_TYPE_STRUCT:
1410 case DBUS_TYPE_DICT_ENTRY:
1411 return "dict_entry";
1412 case DBUS_TYPE_ARRAY:
1414 case DBUS_TYPE_VARIANT:
1416 case DBUS_STRUCT_BEGIN_CHAR:
1417 return "begin_struct";
1418 case DBUS_STRUCT_END_CHAR:
1419 return "end_struct";
1420 case DBUS_DICT_ENTRY_BEGIN_CHAR:
1421 return "begin_dict_entry";
1422 case DBUS_DICT_ENTRY_END_CHAR:
1423 return "end_dict_entry";
1430 * If in verbose mode, print a block of binary data.
1432 * @todo right now it prints even if not in verbose mode
1434 * @param data the data
1435 * @param len the length of the data
1436 * @param offset where to start counting for byte indexes
1439 _dbus_verbose_bytes (const unsigned char *data,
1444 const unsigned char *aligned;
1446 _dbus_assert (len >= 0);
1448 /* Print blanks on first row if appropriate */
1449 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1452 _dbus_assert (aligned <= data);
1454 if (aligned != data)
1456 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1457 while (aligned != data)
1459 _dbus_verbose (" ");
1464 /* now print the bytes */
1468 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1470 _dbus_verbose ("%4d\t%p: ",
1471 offset + i, &data[i]);
1474 if (data[i] >= 32 &&
1476 _dbus_verbose (" '%c' ", data[i]);
1478 _dbus_verbose ("0x%s%x ",
1479 data[i] <= 0xf ? "0" : "", data[i]);
1483 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1486 _dbus_verbose ("BE: %d LE: %d",
1487 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1488 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1491 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1493 #ifdef DBUS_HAVE_INT64
1494 /* I think I probably mean "GNU libc printf" and not "GNUC"
1495 * but we'll wait until someone complains. If you hit this,
1496 * just turn off verbose mode as a workaround.
1499 _dbus_verbose (" u64: 0x%llx",
1500 *(dbus_uint64_t*)&data[i-8]);
1503 _dbus_verbose (" dbl: %g",
1504 *(double*)&data[i-8]);
1507 _dbus_verbose ("\n");
1511 _dbus_verbose ("\n");
1515 * Dump the given part of the string to verbose log.
1517 * @param str the string
1518 * @param start the start of range to dump
1519 * @param len length of range
1522 _dbus_verbose_bytes_of_string (const DBusString *str,
1529 real_len = _dbus_string_get_length (str);
1531 _dbus_assert (start >= 0);
1533 if (start > real_len)
1535 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1536 start, len, real_len);
1540 if ((start + len) > real_len)
1542 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1543 start, len, real_len);
1544 len = real_len - start;
1547 d = _dbus_string_get_const_data_len (str, start, len);
1549 _dbus_verbose_bytes (d, len, start);
1553 * Get the first type in the signature. The difference between this
1554 * and just getting the first byte of the signature is that you won't
1555 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1558 * @param str string containing signature
1559 * @param pos where the signature starts
1560 * @returns the first type in the signature
1563 _dbus_first_type_in_signature (const DBusString *str,
1568 t = _dbus_string_get_byte (str, pos);
1570 if (t == DBUS_STRUCT_BEGIN_CHAR)
1571 return DBUS_TYPE_STRUCT;
1572 else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1573 return DBUS_TYPE_DICT_ENTRY;
1576 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1577 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1584 #ifdef DBUS_BUILD_TESTS
1585 #include "dbus-test.h"
1589 swap_test_array (void *array,
1599 _dbus_string_init_const_len (&t, array, len_bytes);
1600 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1603 #define MARSHAL_BASIC(typename, byte_order, literal) \
1605 v_##typename = literal; \
1606 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1608 byte_order, NULL)) \
1609 _dbus_assert_not_reached ("no memory"); \
1612 #define DEMARSHAL_BASIC(typename, byte_order) \
1614 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1615 byte_order, &pos); \
1618 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1620 DEMARSHAL_BASIC (typename, byte_order); \
1621 if (literal != v_##typename) \
1623 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1624 _dbus_string_get_length (&str) - dump_pos); \
1625 _dbus_assert_not_reached ("demarshaled wrong value"); \
1629 #define MARSHAL_TEST(typename, byte_order, literal) \
1631 MARSHAL_BASIC (typename, byte_order, literal); \
1633 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1636 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1638 MARSHAL_BASIC (typename, byte_order, literal); \
1640 DEMARSHAL_BASIC (typename, byte_order); \
1641 if (strcmp (literal, v_##typename) != 0) \
1643 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1644 _dbus_string_get_length (&str) - dump_pos); \
1645 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1646 _dbus_assert_not_reached ("demarshaled wrong value"); \
1650 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1653 v_UINT32 = sizeof(literal); \
1654 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1655 byte_order, &next)) \
1656 _dbus_assert_not_reached ("no memory"); \
1657 v_ARRAY_##typename = literal; \
1658 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1659 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1660 byte_order, NULL)) \
1661 _dbus_assert_not_reached ("no memory"); \
1664 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1667 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1668 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1669 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1670 v_UINT32/alignment, \
1671 byte_order, NULL); \
1672 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1673 byte_order, alignment); \
1676 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1678 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1679 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1681 _dbus_verbose ("MARSHALED DATA\n"); \
1682 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1683 _dbus_string_get_length (&str) - dump_pos); \
1684 _dbus_verbose ("LITERAL DATA\n"); \
1685 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1686 _dbus_verbose ("READ DATA\n"); \
1687 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1688 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1692 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1694 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1696 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1700 _dbus_marshal_test (void)
1705 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1706 dbus_int16_t array2[3] = { 124, 457, 780 };
1707 dbus_int32_t array4[3] = { 123, 456, 789 };
1708 #ifdef DBUS_HAVE_INT64
1709 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1710 DBUS_INT64_CONSTANT (0x456ffffffff),
1711 DBUS_INT64_CONSTANT (0x789ffffffff) };
1712 dbus_int64_t *v_ARRAY_INT64;
1714 unsigned char *v_ARRAY_BYTE;
1715 dbus_int16_t *v_ARRAY_INT16;
1716 dbus_uint16_t *v_ARRAY_UINT16;
1717 dbus_int32_t *v_ARRAY_INT32;
1718 dbus_uint32_t *v_ARRAY_UINT32;
1719 double *v_ARRAY_DOUBLE;
1723 dbus_int16_t v_INT16;
1724 dbus_uint16_t v_UINT16;
1725 dbus_int32_t v_INT32;
1726 dbus_uint32_t v_UINT32;
1727 dbus_int64_t v_INT64;
1728 dbus_uint64_t v_UINT64;
1729 unsigned char v_BYTE;
1730 dbus_bool_t v_BOOLEAN;
1731 const char *v_STRING;
1732 const char *v_SIGNATURE;
1733 const char *v_OBJECT_PATH;
1736 if (!_dbus_string_init (&str))
1737 _dbus_assert_not_reached ("failed to init string");
1741 /* Marshal doubles */
1742 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1743 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1745 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1746 _dbus_assert_not_reached ("got wrong double value");
1748 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1749 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1751 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1752 _dbus_assert_not_reached ("got wrong double value");
1754 /* Marshal signed 16 integers */
1755 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1756 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1758 /* Marshal unsigned 16 integers */
1759 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1760 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1762 /* Marshal signed integers */
1763 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1764 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1766 /* Marshal unsigned integers */
1767 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1768 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1770 #ifdef DBUS_HAVE_INT64
1771 /* Marshal signed integers */
1772 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1773 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1775 /* Marshal unsigned integers */
1776 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1777 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1778 #endif /* DBUS_HAVE_INT64 */
1781 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1782 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1784 /* Marshal all possible bools! */
1785 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1786 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1787 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1788 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1790 /* Marshal strings */
1791 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1792 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1793 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1794 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1797 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1798 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1801 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1802 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1803 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1804 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1807 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1808 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1809 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2);
1810 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2);
1812 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1813 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1814 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4);
1815 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4);
1817 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1818 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1820 #ifdef DBUS_HAVE_INT64
1821 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1822 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1828 * FIXME restore the set/pack tests
1831 #ifdef DBUS_HAVE_INT64
1832 /* set/pack 64-bit integers */
1833 _dbus_string_set_length (&str, 8);
1836 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1837 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1839 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1840 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1841 _dbus_string_get_const_data (&str)));
1844 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1845 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1847 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1848 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1849 _dbus_string_get_const_data (&str)));
1851 /* signed little pack */
1852 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1854 _dbus_string_get_data (&str));
1856 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1857 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1858 _dbus_string_get_const_data (&str)));
1860 /* signed big pack */
1861 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1863 _dbus_string_get_data (&str));
1865 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1866 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1867 _dbus_string_get_const_data (&str)));
1869 /* unsigned little */
1870 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1871 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1873 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1874 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1875 _dbus_string_get_const_data (&str)));
1878 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1879 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1881 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1882 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1883 _dbus_string_get_const_data (&str)));
1885 /* unsigned little pack */
1886 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1888 _dbus_string_get_data (&str));
1890 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1891 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1892 _dbus_string_get_const_data (&str)));
1894 /* unsigned big pack */
1895 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1897 _dbus_string_get_data (&str));
1899 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1900 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1901 _dbus_string_get_const_data (&str)));
1902 #endif /* DBUS_HAVE_INT64 */
1904 /* set/pack 32-bit integers */
1905 _dbus_string_set_length (&str, 4);
1908 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1911 _dbus_assert (-0x123456 ==
1912 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1913 _dbus_string_get_const_data (&str)));
1916 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1919 _dbus_assert (-0x123456 ==
1920 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1921 _dbus_string_get_const_data (&str)));
1923 /* signed little pack */
1924 _dbus_pack_int32 (-0x123456,
1926 _dbus_string_get_data (&str));
1928 _dbus_assert (-0x123456 ==
1929 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1930 _dbus_string_get_const_data (&str)));
1932 /* signed big pack */
1933 _dbus_pack_int32 (-0x123456,
1935 _dbus_string_get_data (&str));
1937 _dbus_assert (-0x123456 ==
1938 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1939 _dbus_string_get_const_data (&str)));
1941 /* unsigned little */
1942 _dbus_marshal_set_uint32 (&str,
1944 DBUS_LITTLE_ENDIAN);
1946 _dbus_assert (0x123456 ==
1947 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1948 _dbus_string_get_const_data (&str)));
1951 _dbus_marshal_set_uint32 (&str,
1955 _dbus_assert (0x123456 ==
1956 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1957 _dbus_string_get_const_data (&str)));
1959 /* unsigned little pack */
1960 _dbus_pack_uint32 (0x123456,
1962 _dbus_string_get_data (&str));
1964 _dbus_assert (0x123456 ==
1965 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1966 _dbus_string_get_const_data (&str)));
1968 /* unsigned big pack */
1969 _dbus_pack_uint32 (0x123456,
1971 _dbus_string_get_data (&str));
1973 _dbus_assert (0x123456 ==
1974 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1975 _dbus_string_get_const_data (&str)));
1977 #endif /* set/pack tests for integers */
1979 /* Strings in-place set */
1980 byte_order = DBUS_LITTLE_ENDIAN;
1984 _dbus_string_set_length (&str, 0);
1986 /* reset pos for the macros */
1989 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1991 /* Set it to something longer */
1992 _dbus_string_init_const (&t, "Hello world foo");
1994 v_STRING = _dbus_string_get_const_data (&t);
1995 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1996 &v_STRING, byte_order, NULL, NULL);
1998 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1999 &v_STRING, byte_order,
2001 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
2003 /* Set it to something shorter */
2004 _dbus_string_init_const (&t, "Hello");
2006 v_STRING = _dbus_string_get_const_data (&t);
2007 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
2008 &v_STRING, byte_order, NULL, NULL);
2009 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
2010 &v_STRING, byte_order,
2012 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
2014 /* Do the other byte order */
2015 if (byte_order == DBUS_LITTLE_ENDIAN)
2016 byte_order = DBUS_BIG_ENDIAN;
2022 _dbus_string_free (&str);
2027 #endif /* DBUS_BUILD_TESTS */