1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-basic.c Marshalling routines for basic (primitive) types
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-internals.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-signature.h"
32 #if defined(__GNUC__) && (__GNUC__ >= 4)
33 # define _DBUS_ASSERT_ALIGNMENT(type, op, val) \
34 _DBUS_STATIC_ASSERT (__extension__ __alignof__ (type) op val)
36 # define _DBUS_ASSERT_ALIGNMENT(type, op, val) do { } while (0)
39 /* True by definition, but just for completeness... */
40 _DBUS_STATIC_ASSERT (sizeof (char) == 1);
41 _DBUS_ASSERT_ALIGNMENT (char, ==, 1);
43 _DBUS_STATIC_ASSERT (sizeof (dbus_int16_t) == 2);
44 _DBUS_ASSERT_ALIGNMENT (dbus_int16_t, <=, 2);
45 _DBUS_STATIC_ASSERT (sizeof (dbus_uint16_t) == 2);
46 _DBUS_ASSERT_ALIGNMENT (dbus_uint16_t, <=, 2);
48 _DBUS_STATIC_ASSERT (sizeof (dbus_int32_t) == 4);
49 _DBUS_ASSERT_ALIGNMENT (dbus_int32_t, <=, 4);
50 _DBUS_STATIC_ASSERT (sizeof (dbus_uint32_t) == 4);
51 _DBUS_ASSERT_ALIGNMENT (dbus_uint32_t, <=, 4);
52 _DBUS_STATIC_ASSERT (sizeof (dbus_bool_t) == 4);
53 _DBUS_ASSERT_ALIGNMENT (dbus_bool_t, <=, 4);
55 _DBUS_STATIC_ASSERT (sizeof (double) == 8);
56 _DBUS_ASSERT_ALIGNMENT (double, <=, 8);
58 #ifdef DBUS_HAVE_INT64
59 _DBUS_STATIC_ASSERT (sizeof (dbus_int64_t) == 8);
60 _DBUS_ASSERT_ALIGNMENT (dbus_int64_t, <=, 8);
61 _DBUS_STATIC_ASSERT (sizeof (dbus_uint64_t) == 8);
62 _DBUS_ASSERT_ALIGNMENT (dbus_uint64_t, <=, 8);
66 * @defgroup DBusMarshal marshaling and unmarshaling
67 * @ingroup DBusInternals
68 * @brief functions to marshal/unmarshal data from the wire
70 * Types and functions related to converting primitive data types from
71 * wire format to native machine format, and vice versa.
73 * A signature is just a string with multiple types one after the other.
74 * for example a type is "i" or "(ii)", a signature is "i(ii)"
75 * where i is int and (ii) is struct { int; int; }
81 pack_2_octets (dbus_uint16_t value,
85 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
87 if ((byte_order) == DBUS_LITTLE_ENDIAN)
88 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
90 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
94 pack_4_octets (dbus_uint32_t value,
98 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
100 if ((byte_order) == DBUS_LITTLE_ENDIAN)
101 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
103 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
107 pack_8_octets (DBusBasicValue value,
111 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
113 #ifdef DBUS_HAVE_INT64
114 if ((byte_order) == DBUS_LITTLE_ENDIAN)
115 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
117 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
119 *(DBus8ByteStruct*)data = value.u64;
120 swap_8_octets ((DBusBasicValue*)data, byte_order);
125 * Packs a 32 bit unsigned integer into a data pointer.
127 * @param value the value
128 * @param byte_order the byte order to use
129 * @param data the data pointer
132 _dbus_pack_uint32 (dbus_uint32_t value,
136 pack_4_octets (value, byte_order, data);
139 #ifndef DBUS_HAVE_INT64
142 swap_bytes (unsigned char *data,
145 unsigned char *p1 = data;
146 unsigned char *p2 = data + len - 1;
150 unsigned char tmp = *p1;
158 #endif /* !DBUS_HAVE_INT64 */
161 swap_8_octets (DBusBasicValue *value,
164 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
166 #ifdef DBUS_HAVE_INT64
167 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
169 swap_bytes ((unsigned char *)value, 8);
175 static DBusBasicValue
176 unpack_8_octets (int byte_order,
177 const unsigned char *data)
181 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
182 _dbus_assert (sizeof (r) == 8);
184 #ifdef DBUS_HAVE_INT64
185 if (byte_order == DBUS_LITTLE_ENDIAN)
186 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
188 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
190 r.u64 = *(DBus8ByteStruct*)data;
191 swap_8_octets (&r, byte_order);
198 #ifndef _dbus_unpack_uint16
200 * Unpacks a 16 bit unsigned integer from a data pointer
202 * @param byte_order The byte order to use
203 * @param data the data pointer
204 * @returns the integer
207 _dbus_unpack_uint16 (int byte_order,
208 const unsigned char *data)
210 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
212 if (byte_order == DBUS_LITTLE_ENDIAN)
213 return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
215 return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
217 #endif /* _dbus_unpack_uint16 */
219 #ifndef _dbus_unpack_uint32
221 * Unpacks a 32 bit unsigned integer from a data pointer
223 * @param byte_order The byte order to use
224 * @param data the data pointer
225 * @returns the integer
228 _dbus_unpack_uint32 (int byte_order,
229 const unsigned char *data)
231 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
233 if (byte_order == DBUS_LITTLE_ENDIAN)
234 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
236 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
238 #endif /* _dbus_unpack_uint32 */
241 set_2_octets (DBusString *str,
248 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
249 byte_order == DBUS_BIG_ENDIAN);
251 data = _dbus_string_get_data_len (str, offset, 2);
253 pack_2_octets (value, byte_order, data);
257 set_4_octets (DBusString *str,
264 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
265 byte_order == DBUS_BIG_ENDIAN);
267 data = _dbus_string_get_data_len (str, offset, 4);
269 pack_4_octets (value, byte_order, data);
273 set_8_octets (DBusString *str,
275 DBusBasicValue value,
280 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
281 byte_order == DBUS_BIG_ENDIAN);
283 data = _dbus_string_get_data_len (str, offset, 8);
285 pack_8_octets (value, byte_order, data);
289 * Sets the 4 bytes at the given offset to a marshaled unsigned
290 * integer, replacing anything found there previously.
292 * @param str the string to write the marshalled int to
293 * @param pos the byte offset where int should be written
294 * @param value the value
295 * @param byte_order the byte order to use
299 _dbus_marshal_set_uint32 (DBusString *str,
304 set_4_octets (str, pos, value, byte_order);
308 * Sets the existing marshaled string at the given offset with
309 * a new marshaled string. The given offset must point to
310 * an existing string or the wrong length will be deleted
311 * and replaced with the new string.
313 * Note: no attempt is made by this function to re-align
314 * any data which has been already marshalled after this
315 * string. Use with caution.
317 * @param str the string to write the marshalled string to
318 * @param pos the position of the marshaled string length
319 * @param value the value
320 * @param byte_order the byte order to use
321 * @param old_end_pos place to store byte after the nul byte of the old value
322 * @param new_end_pos place to store byte after the nul byte of the new value
323 * @returns #TRUE on success, #FALSE if no memory
327 set_string (DBusString *str,
334 int old_len, new_len;
337 _dbus_string_init_const (&dstr, value);
339 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
340 old_len = _dbus_unpack_uint32 (byte_order,
341 _dbus_string_get_const_data_len (str, pos, 4));
343 new_len = _dbus_string_get_length (&dstr);
345 if (!_dbus_string_replace_len (&dstr, 0, new_len,
346 str, pos + 4, old_len))
349 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
352 *old_end_pos = pos + 4 + old_len + 1;
354 *new_end_pos = pos + 4 + new_len + 1;
360 * Sets the existing marshaled signature at the given offset to a new
361 * marshaled signature. Same basic ideas as set_string().
363 * @param str the string to write the marshalled signature to
364 * @param pos the position of the marshaled signature length
365 * @param value the value
366 * @param byte_order the byte order to use
367 * @param old_end_pos place to store byte after the nul byte of the old value
368 * @param new_end_pos place to store byte after the nul byte of the new value
369 * @returns #TRUE on success, #FALSE if no memory
373 set_signature (DBusString *str,
380 int old_len, new_len;
383 _dbus_string_init_const (&dstr, value);
385 old_len = _dbus_string_get_byte (str, pos);
386 new_len = _dbus_string_get_length (&dstr);
388 if (!_dbus_string_replace_len (&dstr, 0, new_len,
389 str, pos + 1, old_len))
392 _dbus_string_set_byte (str, pos, new_len);
395 *old_end_pos = pos + 1 + old_len + 1;
397 *new_end_pos = pos + 1 + new_len + 1;
403 * Sets an existing basic type value to a new value.
404 * Arguments work the same way as _dbus_marshal_basic_type().
406 * @param str the string
407 * @param pos location of the current value
408 * @param type the type of the current and new values
409 * @param value the address of the new value
410 * @param byte_order byte order for marshaling
411 * @param old_end_pos location to store end position of the old value, or #NULL
412 * @param new_end_pos location to store end position of the new value, or #NULL
413 * @returns #FALSE if no memory
416 _dbus_marshal_set_basic (DBusString *str,
424 const DBusBasicValue *vp;
431 _dbus_string_set_byte (str, pos, vp->byt);
433 *old_end_pos = pos + 1;
435 *new_end_pos = pos + 1;
438 case DBUS_TYPE_INT16:
439 case DBUS_TYPE_UINT16:
440 pos = _DBUS_ALIGN_VALUE (pos, 2);
441 set_2_octets (str, pos, vp->u16, byte_order);
443 *old_end_pos = pos + 2;
445 *new_end_pos = pos + 2;
448 case DBUS_TYPE_BOOLEAN:
449 case DBUS_TYPE_INT32:
450 case DBUS_TYPE_UINT32:
451 case DBUS_TYPE_UNIX_FD:
452 pos = _DBUS_ALIGN_VALUE (pos, 4);
453 set_4_octets (str, pos, vp->u32, byte_order);
455 *old_end_pos = pos + 4;
457 *new_end_pos = pos + 4;
460 case DBUS_TYPE_INT64:
461 case DBUS_TYPE_UINT64:
462 case DBUS_TYPE_DOUBLE:
463 pos = _DBUS_ALIGN_VALUE (pos, 8);
464 set_8_octets (str, pos, *vp, byte_order);
466 *old_end_pos = pos + 8;
468 *new_end_pos = pos + 8;
471 case DBUS_TYPE_STRING:
472 case DBUS_TYPE_OBJECT_PATH:
473 pos = _DBUS_ALIGN_VALUE (pos, 4);
474 _dbus_assert (vp->str != NULL);
475 return set_string (str, pos, vp->str, byte_order,
476 old_end_pos, new_end_pos);
478 case DBUS_TYPE_SIGNATURE:
479 _dbus_assert (vp->str != NULL);
480 return set_signature (str, pos, vp->str, byte_order,
481 old_end_pos, new_end_pos);
484 _dbus_assert_not_reached ("not a basic type");
491 * Convenience function to demarshal a 32 bit unsigned integer.
493 * @param str the string containing the data
494 * @param byte_order the byte order
495 * @param pos the position in the string
496 * @param new_pos the new position of the string
497 * @returns the demarshaled integer.
500 _dbus_marshal_read_uint32 (const DBusString *str,
505 pos = _DBUS_ALIGN_VALUE (pos, 4);
510 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
512 return _dbus_unpack_uint32 (byte_order,
513 _dbus_string_get_const_data (str) + pos);
517 * Demarshals a basic-typed value. The "value" pointer is always
518 * the address of a variable of the basic type. So e.g.
519 * if the basic type is "double" then the pointer is
520 * a double*, and if it's "char*" then the pointer is
523 * A value of type #DBusBasicValue is guaranteed to be large enough to
524 * hold any of the types that may be returned, which is handy if you
525 * are trying to do things generically. For example you can pass
526 * a DBusBasicValue* in to this function, and then pass the same
527 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
528 * move a value from one place to another.
530 * @param str the string containing the data
531 * @param pos position in the string
532 * @param type type of value to demarshal
533 * @param value pointer to return value data
534 * @param byte_order the byte order
535 * @param new_pos pointer to update with new position, or #NULL
538 _dbus_marshal_read_basic (const DBusString *str,
545 const char *str_data;
547 _dbus_assert (dbus_type_is_basic (type));
549 str_data = _dbus_string_get_const_data (str);
551 /* Below we volatile types to avoid aliasing issues;
552 * see http://bugs.freedesktop.org/show_bug.cgi?id=20137
559 volatile unsigned char *vp = value;
560 *vp = (unsigned char) _dbus_string_get_byte (str, pos);
564 case DBUS_TYPE_INT16:
565 case DBUS_TYPE_UINT16:
567 volatile dbus_uint16_t *vp = value;
568 pos = _DBUS_ALIGN_VALUE (pos, 2);
569 *vp = *(dbus_uint16_t *)(str_data + pos);
570 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
571 *vp = DBUS_UINT16_SWAP_LE_BE (*vp);
575 case DBUS_TYPE_INT32:
576 case DBUS_TYPE_UINT32:
577 case DBUS_TYPE_BOOLEAN:
578 case DBUS_TYPE_UNIX_FD:
580 volatile dbus_uint32_t *vp = value;
581 pos = _DBUS_ALIGN_VALUE (pos, 4);
582 *vp = *(dbus_uint32_t *)(str_data + pos);
583 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
584 *vp = DBUS_UINT32_SWAP_LE_BE (*vp);
588 case DBUS_TYPE_INT64:
589 case DBUS_TYPE_UINT64:
590 case DBUS_TYPE_DOUBLE:
592 volatile dbus_uint64_t *vp = value;
593 pos = _DBUS_ALIGN_VALUE (pos, 8);
594 #ifdef DBUS_HAVE_INT64
595 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
596 *vp = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
598 *vp = *(dbus_uint64_t*)(str_data + pos);
600 *vp = *(DBus8ByteStruct*) (str_data + pos);
601 swap_8_octets (vp, byte_order);
606 case DBUS_TYPE_STRING:
607 case DBUS_TYPE_OBJECT_PATH:
610 volatile char **vp = value;
612 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
614 *vp = (char*) str_data + pos;
616 pos += len + 1; /* length plus nul */
619 case DBUS_TYPE_SIGNATURE:
622 volatile char **vp = value;
624 len = _dbus_string_get_byte (str, pos);
627 *vp = (char*) str_data + pos;
629 pos += len + 1; /* length plus nul */
633 _dbus_warn_check_failed ("type %s %d not a basic type\n",
634 _dbus_type_to_string (type), type);
635 _dbus_assert_not_reached ("not a basic type");
644 marshal_2_octets (DBusString *str,
653 _dbus_assert (sizeof (value) == 2);
655 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
656 value = DBUS_UINT16_SWAP_LE_BE (value);
658 orig_len = _dbus_string_get_length (str);
660 retval = _dbus_string_insert_2_aligned (str, insert_at,
661 (const unsigned char *)&value);
665 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
666 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
673 marshal_4_octets (DBusString *str,
682 _dbus_assert (sizeof (value) == 4);
684 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
685 value = DBUS_UINT32_SWAP_LE_BE (value);
687 orig_len = _dbus_string_get_length (str);
689 retval = _dbus_string_insert_4_aligned (str, insert_at,
690 (const unsigned char *)&value);
694 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
695 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
702 marshal_8_octets (DBusString *str,
704 DBusBasicValue value,
711 _dbus_assert (sizeof (value) == 8);
713 swap_8_octets (&value, byte_order);
715 orig_len = _dbus_string_get_length (str);
717 retval = _dbus_string_insert_8_aligned (str, insert_at,
718 (const unsigned char *)&value);
721 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
729 MARSHAL_AS_SIGNATURE,
730 MARSHAL_AS_BYTE_ARRAY
734 marshal_len_followed_by_bytes (int marshal_as,
737 const unsigned char *value,
738 int data_len, /* doesn't include nul if any */
743 DBusString value_str;
746 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
747 if (insert_at > _dbus_string_get_length (str))
748 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
749 insert_at, _dbus_string_get_length (str), data_len);
751 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
752 value_len = data_len;
754 value_len = data_len + 1; /* value has a nul */
756 _dbus_string_init_const_len (&value_str, value, value_len);
760 if (marshal_as == MARSHAL_AS_SIGNATURE)
762 _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
763 _dbus_assert (data_len <= 255); /* same as max sig len right now */
765 if (!_dbus_string_insert_byte (str, pos, data_len))
772 if (!marshal_4_octets (str, pos, data_len,
777 if (!_dbus_string_copy_len (&value_str, 0, value_len,
783 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
785 _dbus_verbose_bytes_of_string (str, pos, value_len);
796 /* Delete what we've inserted */
797 _dbus_string_delete (str, insert_at, pos - insert_at);
803 marshal_string (DBusString *str,
809 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
810 str, insert_at, value,
812 byte_order, pos_after);
816 marshal_signature (DBusString *str,
821 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
822 str, insert_at, value,
824 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
829 * Marshals a basic-typed value. The "value" pointer is always the
830 * address of a variable containing the basic type value.
831 * So for example for int32 it will be dbus_int32_t*, and
832 * for string it will be const char**. This is for symmetry
833 * with _dbus_marshal_read_basic() and to have a simple
836 * @param str string to marshal to
837 * @param insert_at where to insert the value
838 * @param type type of value
839 * @param value pointer to a variable containing the value
840 * @param byte_order byte order
841 * @param pos_after #NULL or the position after the type
842 * @returns #TRUE on success
845 _dbus_marshal_write_basic (DBusString *str,
852 const DBusBasicValue *vp;
854 _dbus_assert (dbus_type_is_basic (type));
861 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
864 *pos_after = insert_at + 1;
867 case DBUS_TYPE_INT16:
868 case DBUS_TYPE_UINT16:
869 return marshal_2_octets (str, insert_at, vp->u16,
870 byte_order, pos_after);
872 case DBUS_TYPE_BOOLEAN:
873 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
874 byte_order, pos_after);
876 case DBUS_TYPE_INT32:
877 case DBUS_TYPE_UINT32:
878 case DBUS_TYPE_UNIX_FD:
879 return marshal_4_octets (str, insert_at, vp->u32,
880 byte_order, pos_after);
882 case DBUS_TYPE_INT64:
883 case DBUS_TYPE_UINT64:
884 case DBUS_TYPE_DOUBLE:
885 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
888 case DBUS_TYPE_STRING:
889 case DBUS_TYPE_OBJECT_PATH:
890 _dbus_assert (vp->str != NULL);
891 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
893 case DBUS_TYPE_SIGNATURE:
894 _dbus_assert (vp->str != NULL);
895 return marshal_signature (str, insert_at, vp->str, pos_after);
898 _dbus_assert_not_reached ("not a basic type");
905 marshal_1_octets_array (DBusString *str,
907 const unsigned char *value,
913 DBusString value_str;
915 _dbus_string_init_const_len (&value_str, value, n_elements);
919 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
932 * Swaps the elements of an array to the opposite byte order
934 * @param data start of array
935 * @param n_elements number of elements
936 * @param alignment size of each element
939 _dbus_swap_array (unsigned char *data,
946 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
948 /* we use const_data and cast it off so DBusString can be a const string
949 * for the unit tests. don't ask.
952 end = d + (n_elements * alignment);
958 #ifdef DBUS_HAVE_INT64
959 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
961 swap_8_bytes ((DBusBasicValue*) d);
966 else if (alignment == 4)
970 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
976 _dbus_assert (alignment == 2);
980 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
987 swap_array (DBusString *str,
993 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
995 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
997 /* we use const_data and cast it off so DBusString can be a const string
998 * for the unit tests. don't ask.
1000 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
1001 n_elements, alignment);
1006 marshal_fixed_multi (DBusString *str,
1008 const DBusBasicValue *value,
1019 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
1021 old_string_len = _dbus_string_get_length (str);
1023 len_in_bytes = n_elements * alignment;
1024 array_start = insert_at;
1026 /* Note that we do alignment padding unconditionally
1027 * even if the array is empty; this means that
1028 * padding + len is always equal to the number of bytes
1032 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1035 _dbus_string_init_const_len (&t,
1036 (const unsigned char*) value,
1039 if (!_dbus_string_copy (&t, 0,
1043 swap_array (str, array_start, n_elements, byte_order, alignment);
1046 *pos_after = array_start + len_in_bytes;
1051 _dbus_string_delete (str, insert_at,
1052 _dbus_string_get_length (str) - old_string_len);
1058 * Marshals a block of values of fixed-length type all at once, as an
1059 * optimization. dbus_type_is_fixed() returns #TRUE for fixed-length
1060 * types, which are the basic types minus the string-like types.
1062 * The value argument should be the adddress of an
1063 * array, so e.g. "const dbus_uint32_t**"
1065 * @param str string to marshal to
1066 * @param insert_at where to insert the value
1067 * @param element_type type of array elements
1068 * @param value address of an array to marshal
1069 * @param n_elements number of elements in the array
1070 * @param byte_order byte order
1071 * @param pos_after #NULL or the position after the type
1072 * @returns #TRUE on success
1075 _dbus_marshal_write_fixed_multi (DBusString *str,
1083 const void* vp = *(const DBusBasicValue**)value;
1085 _dbus_assert (dbus_type_is_fixed (element_type));
1086 _dbus_assert (n_elements >= 0);
1089 _dbus_verbose ("writing %d elements of %s\n",
1090 n_elements, _dbus_type_to_string (element_type));
1093 switch (element_type)
1095 case DBUS_TYPE_BYTE:
1096 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1098 case DBUS_TYPE_INT16:
1099 case DBUS_TYPE_UINT16:
1100 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1101 case DBUS_TYPE_BOOLEAN:
1102 case DBUS_TYPE_INT32:
1103 case DBUS_TYPE_UINT32:
1104 case DBUS_TYPE_UNIX_FD:
1105 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1107 case DBUS_TYPE_INT64:
1108 case DBUS_TYPE_UINT64:
1109 case DBUS_TYPE_DOUBLE:
1110 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1114 _dbus_assert_not_reached ("non fixed type in array write");
1123 * Skips over a basic-typed value, reporting the following position.
1125 * @param str the string containing the data
1126 * @param type type of value to read
1127 * @param byte_order the byte order
1128 * @param pos pointer to position in the string,
1129 * updated on return to new position
1132 _dbus_marshal_skip_basic (const DBusString *str,
1137 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1138 byte_order == DBUS_BIG_ENDIAN);
1142 case DBUS_TYPE_BYTE:
1145 case DBUS_TYPE_INT16:
1146 case DBUS_TYPE_UINT16:
1147 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1150 case DBUS_TYPE_BOOLEAN:
1151 case DBUS_TYPE_INT32:
1152 case DBUS_TYPE_UINT32:
1153 case DBUS_TYPE_UNIX_FD:
1154 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1157 case DBUS_TYPE_INT64:
1158 case DBUS_TYPE_UINT64:
1159 case DBUS_TYPE_DOUBLE:
1160 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1163 case DBUS_TYPE_STRING:
1164 case DBUS_TYPE_OBJECT_PATH:
1168 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1170 *pos += len + 1; /* length plus nul */
1173 case DBUS_TYPE_SIGNATURE:
1177 len = _dbus_string_get_byte (str, *pos);
1179 *pos += len + 2; /* length byte plus length plus nul */
1183 _dbus_warn ("type %s not a basic type\n",
1184 _dbus_type_to_string (type));
1185 _dbus_assert_not_reached ("not a basic type");
1191 * Skips an array, returning the next position.
1193 * @param str the string containing the data
1194 * @param element_type the type of array elements
1195 * @param byte_order the byte order
1196 * @param pos pointer to position in the string,
1197 * updated on return to new position
1200 _dbus_marshal_skip_array (const DBusString *str,
1205 dbus_uint32_t array_len;
1209 i = _DBUS_ALIGN_VALUE (*pos, 4);
1211 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1213 alignment = _dbus_type_get_alignment (element_type);
1215 i = _DBUS_ALIGN_VALUE (i, alignment);
1217 *pos = i + array_len;
1221 * Gets the alignment requirement for the given type;
1222 * will be 1, 4, or 8.
1224 * @param typecode the type
1225 * @returns alignment of 1, 4, or 8
1228 _dbus_type_get_alignment (int typecode)
1232 case DBUS_TYPE_BYTE:
1233 case DBUS_TYPE_VARIANT:
1234 case DBUS_TYPE_SIGNATURE:
1236 case DBUS_TYPE_INT16:
1237 case DBUS_TYPE_UINT16:
1239 case DBUS_TYPE_BOOLEAN:
1240 case DBUS_TYPE_INT32:
1241 case DBUS_TYPE_UINT32:
1242 case DBUS_TYPE_UNIX_FD:
1243 /* this stuff is 4 since it starts with a length */
1244 case DBUS_TYPE_STRING:
1245 case DBUS_TYPE_OBJECT_PATH:
1246 case DBUS_TYPE_ARRAY:
1248 case DBUS_TYPE_INT64:
1249 case DBUS_TYPE_UINT64:
1250 case DBUS_TYPE_DOUBLE:
1251 /* struct is 8 since it could contain an 8-aligned item
1252 * and it's simpler to just always align structs to 8;
1253 * we want the amount of padding in a struct of a given
1254 * type to be predictable, not location-dependent.
1255 * DICT_ENTRY is always the same as struct.
1257 case DBUS_TYPE_STRUCT:
1258 case DBUS_TYPE_DICT_ENTRY:
1262 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1268 * Returns a string describing the given type.
1270 * @param typecode the type to describe
1271 * @returns a constant string describing the type
1274 _dbus_type_to_string (int typecode)
1278 case DBUS_TYPE_INVALID:
1280 case DBUS_TYPE_BOOLEAN:
1282 case DBUS_TYPE_BYTE:
1284 case DBUS_TYPE_INT16:
1286 case DBUS_TYPE_UINT16:
1288 case DBUS_TYPE_INT32:
1290 case DBUS_TYPE_UINT32:
1292 case DBUS_TYPE_INT64:
1294 case DBUS_TYPE_UINT64:
1296 case DBUS_TYPE_DOUBLE:
1298 case DBUS_TYPE_STRING:
1300 case DBUS_TYPE_OBJECT_PATH:
1301 return "object_path";
1302 case DBUS_TYPE_SIGNATURE:
1304 case DBUS_TYPE_STRUCT:
1306 case DBUS_TYPE_DICT_ENTRY:
1307 return "dict_entry";
1308 case DBUS_TYPE_ARRAY:
1310 case DBUS_TYPE_VARIANT:
1312 case DBUS_STRUCT_BEGIN_CHAR:
1313 return "begin_struct";
1314 case DBUS_STRUCT_END_CHAR:
1315 return "end_struct";
1316 case DBUS_DICT_ENTRY_BEGIN_CHAR:
1317 return "begin_dict_entry";
1318 case DBUS_DICT_ENTRY_END_CHAR:
1319 return "end_dict_entry";
1320 case DBUS_TYPE_UNIX_FD:
1328 * If in verbose mode, print a block of binary data.
1330 * @param data the data
1331 * @param len the length of the data
1332 * @param offset where to start counting for byte indexes
1335 _dbus_verbose_bytes (const unsigned char *data,
1340 const unsigned char *aligned;
1342 _dbus_assert (len >= 0);
1344 if (!_dbus_is_verbose())
1347 /* Print blanks on first row if appropriate */
1348 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1351 _dbus_assert (aligned <= data);
1353 if (aligned != data)
1355 _dbus_verbose ("%4ld\t%p: ", - (long)(data - aligned), aligned);
1356 while (aligned != data)
1358 _dbus_verbose (" ");
1363 /* now print the bytes */
1367 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1369 _dbus_verbose ("%4d\t%p: ",
1370 offset + i, &data[i]);
1373 if (data[i] >= 32 &&
1375 _dbus_verbose (" '%c' ", data[i]);
1377 _dbus_verbose ("0x%s%x ",
1378 data[i] <= 0xf ? "0" : "", data[i]);
1382 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1385 _dbus_verbose ("BE: %d LE: %d",
1386 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1387 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1390 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1392 #ifdef DBUS_INT64_PRINTF_MODIFIER
1393 _dbus_verbose (" u64: 0x%" DBUS_INT64_PRINTF_MODIFIER "x",
1394 *(dbus_uint64_t*)&data[i-8]);
1396 _dbus_verbose (" dbl: %g",
1397 *(double*)&data[i-8]);
1400 _dbus_verbose ("\n");
1404 _dbus_verbose ("\n");
1408 * Dump the given part of the string to verbose log.
1410 * @param str the string
1411 * @param start the start of range to dump
1412 * @param len length of range
1415 _dbus_verbose_bytes_of_string (const DBusString *str,
1422 real_len = _dbus_string_get_length (str);
1424 _dbus_assert (start >= 0);
1426 if (start > real_len)
1428 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1429 start, len, real_len);
1433 if ((start + len) > real_len)
1435 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1436 start, len, real_len);
1437 len = real_len - start;
1440 d = _dbus_string_get_const_data_len (str, start, len);
1442 _dbus_verbose_bytes (d, len, start);
1446 map_type_char_to_type (int t)
1448 if (t == DBUS_STRUCT_BEGIN_CHAR)
1449 return DBUS_TYPE_STRUCT;
1450 else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1451 return DBUS_TYPE_DICT_ENTRY;
1454 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1455 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1461 * Get the first type in the signature. The difference between this
1462 * and just getting the first byte of the signature is that you won't
1463 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1466 * @param str string containing signature
1467 * @param pos where the signature starts
1468 * @returns the first type in the signature
1471 _dbus_first_type_in_signature (const DBusString *str,
1474 return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1478 * Similar to #_dbus_first_type_in_signature, but operates
1479 * on a C string buffer.
1481 * @param str a C string buffer
1482 * @param pos where the signature starts
1483 * @returns the first type in the signature
1486 _dbus_first_type_in_signature_c_str (const char *str,
1489 return map_type_char_to_type (str[pos]);
1494 #ifdef DBUS_BUILD_TESTS
1495 #include "dbus-test.h"
1499 * Reads a block of fixed-length basic values, as an optimization
1500 * vs. reading each one individually into a new buffer.
1502 * This function returns the data in-place; it does not make a copy,
1503 * and it does not swap the bytes.
1505 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
1506 * and the "value" argument should be a "const double**" and so on.
1508 * @param str the string to read from
1509 * @param pos position to read from
1510 * @param element_type type of array elements
1511 * @param value place to return the array
1512 * @param n_elements number of array elements to read
1513 * @param byte_order the byte order, used to read the array length
1514 * @param new_pos #NULL or location to store a position after the elements
1517 _dbus_marshal_read_fixed_multi (const DBusString *str,
1528 _dbus_assert (dbus_type_is_fixed (element_type));
1529 _dbus_assert (dbus_type_is_basic (element_type));
1532 _dbus_verbose ("reading %d elements of %s\n",
1533 n_elements, _dbus_type_to_string (element_type));
1536 alignment = _dbus_type_get_alignment (element_type);
1538 pos = _DBUS_ALIGN_VALUE (pos, alignment);
1540 array_len = n_elements * alignment;
1542 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
1544 *new_pos = pos + array_len;
1548 swap_test_array (void *array,
1558 _dbus_string_init_const_len (&t, array, len_bytes);
1559 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1562 #define MARSHAL_BASIC(typename, byte_order, literal) \
1564 v_##typename = literal; \
1565 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1567 byte_order, NULL)) \
1568 _dbus_assert_not_reached ("no memory"); \
1571 #define DEMARSHAL_BASIC(typename, byte_order) \
1573 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1574 byte_order, &pos); \
1577 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1579 DEMARSHAL_BASIC (typename, byte_order); \
1580 if (literal != v_##typename) \
1582 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1583 _dbus_string_get_length (&str) - dump_pos); \
1584 _dbus_assert_not_reached ("demarshaled wrong value"); \
1588 #define MARSHAL_TEST(typename, byte_order, literal) \
1590 MARSHAL_BASIC (typename, byte_order, literal); \
1592 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1595 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1597 MARSHAL_BASIC (typename, byte_order, literal); \
1599 DEMARSHAL_BASIC (typename, byte_order); \
1600 if (strcmp (literal, v_##typename) != 0) \
1602 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1603 _dbus_string_get_length (&str) - dump_pos); \
1604 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1605 _dbus_assert_not_reached ("demarshaled wrong value"); \
1609 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1612 v_UINT32 = sizeof(literal); \
1613 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1614 byte_order, &next)) \
1615 _dbus_assert_not_reached ("no memory"); \
1616 v_ARRAY_##typename = literal; \
1617 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1618 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1619 byte_order, NULL)) \
1620 _dbus_assert_not_reached ("no memory"); \
1623 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1626 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1627 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1628 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1629 v_UINT32/alignment, \
1630 byte_order, NULL); \
1631 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1632 byte_order, alignment); \
1635 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1637 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1638 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1640 _dbus_verbose ("MARSHALED DATA\n"); \
1641 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1642 _dbus_string_get_length (&str) - dump_pos); \
1643 _dbus_verbose ("LITERAL DATA\n"); \
1644 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1645 _dbus_verbose ("READ DATA\n"); \
1646 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1647 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1651 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1653 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1655 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1659 _dbus_marshal_test (void)
1664 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1665 dbus_int16_t array2[3] = { 124, 457, 780 };
1666 dbus_int32_t array4[3] = { 123, 456, 789 };
1667 #ifdef DBUS_HAVE_INT64
1668 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1669 DBUS_INT64_CONSTANT (0x456ffffffff),
1670 DBUS_INT64_CONSTANT (0x789ffffffff) };
1671 dbus_int64_t *v_ARRAY_INT64;
1673 unsigned char *v_ARRAY_BYTE;
1674 dbus_int16_t *v_ARRAY_INT16;
1675 dbus_uint16_t *v_ARRAY_UINT16;
1676 dbus_int32_t *v_ARRAY_INT32;
1677 dbus_uint32_t *v_ARRAY_UINT32;
1681 dbus_int16_t v_INT16;
1682 dbus_uint16_t v_UINT16;
1683 dbus_int32_t v_INT32;
1684 dbus_uint32_t v_UINT32;
1685 dbus_int64_t v_INT64;
1686 dbus_uint64_t v_UINT64;
1687 unsigned char v_BYTE;
1688 dbus_bool_t v_BOOLEAN;
1689 const char *v_STRING;
1690 const char *v_SIGNATURE;
1691 const char *v_OBJECT_PATH;
1694 if (!_dbus_string_init (&str))
1695 _dbus_assert_not_reached ("failed to init string");
1699 /* Marshal doubles */
1700 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1701 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1703 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1704 _dbus_assert_not_reached ("got wrong double value");
1706 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1707 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1709 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1710 _dbus_assert_not_reached ("got wrong double value");
1712 /* Marshal signed 16 integers */
1713 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1714 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1716 /* Marshal unsigned 16 integers */
1717 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1718 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1720 /* Marshal signed integers */
1721 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1722 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1724 /* Marshal unsigned integers */
1725 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1726 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1728 #ifdef DBUS_HAVE_INT64
1729 /* Marshal signed integers */
1730 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1731 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1733 /* Marshal unsigned integers */
1734 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1735 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1736 #endif /* DBUS_HAVE_INT64 */
1739 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1740 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1742 /* Marshal all possible bools! */
1743 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1744 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1745 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1746 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1748 /* Marshal strings */
1749 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1750 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1751 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1752 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1755 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1756 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1759 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1760 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1761 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1762 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1765 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1766 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1767 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2);
1768 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2);
1770 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1771 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1772 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4);
1773 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4);
1775 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1776 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1778 #ifdef DBUS_HAVE_INT64
1779 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1780 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1786 * FIXME restore the set/pack tests
1789 #ifdef DBUS_HAVE_INT64
1790 /* set/pack 64-bit integers */
1791 _dbus_string_set_length (&str, 8);
1794 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1795 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1797 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1798 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1799 _dbus_string_get_const_data (&str)));
1802 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1803 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1805 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1806 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1807 _dbus_string_get_const_data (&str)));
1809 /* signed little pack */
1810 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1812 _dbus_string_get_data (&str));
1814 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1815 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1816 _dbus_string_get_const_data (&str)));
1818 /* signed big pack */
1819 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1821 _dbus_string_get_data (&str));
1823 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1824 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1825 _dbus_string_get_const_data (&str)));
1827 /* unsigned little */
1828 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1829 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1831 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1832 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1833 _dbus_string_get_const_data (&str)));
1836 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1837 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1839 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1840 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1841 _dbus_string_get_const_data (&str)));
1843 /* unsigned little pack */
1844 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1846 _dbus_string_get_data (&str));
1848 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1849 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1850 _dbus_string_get_const_data (&str)));
1852 /* unsigned big pack */
1853 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1855 _dbus_string_get_data (&str));
1857 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1858 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1859 _dbus_string_get_const_data (&str)));
1860 #endif /* DBUS_HAVE_INT64 */
1862 /* set/pack 32-bit integers */
1863 _dbus_string_set_length (&str, 4);
1866 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1869 _dbus_assert (-0x123456 ==
1870 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1871 _dbus_string_get_const_data (&str)));
1874 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1877 _dbus_assert (-0x123456 ==
1878 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1879 _dbus_string_get_const_data (&str)));
1881 /* signed little pack */
1882 _dbus_pack_int32 (-0x123456,
1884 _dbus_string_get_data (&str));
1886 _dbus_assert (-0x123456 ==
1887 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1888 _dbus_string_get_const_data (&str)));
1890 /* signed big pack */
1891 _dbus_pack_int32 (-0x123456,
1893 _dbus_string_get_data (&str));
1895 _dbus_assert (-0x123456 ==
1896 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1897 _dbus_string_get_const_data (&str)));
1899 /* unsigned little */
1900 _dbus_marshal_set_uint32 (&str,
1902 DBUS_LITTLE_ENDIAN);
1904 _dbus_assert (0x123456 ==
1905 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1906 _dbus_string_get_const_data (&str)));
1909 _dbus_marshal_set_uint32 (&str,
1913 _dbus_assert (0x123456 ==
1914 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1915 _dbus_string_get_const_data (&str)));
1917 /* unsigned little pack */
1918 _dbus_pack_uint32 (0x123456,
1920 _dbus_string_get_data (&str));
1922 _dbus_assert (0x123456 ==
1923 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1924 _dbus_string_get_const_data (&str)));
1926 /* unsigned big pack */
1927 _dbus_pack_uint32 (0x123456,
1929 _dbus_string_get_data (&str));
1931 _dbus_assert (0x123456 ==
1932 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1933 _dbus_string_get_const_data (&str)));
1935 #endif /* set/pack tests for integers */
1937 /* Strings in-place set */
1938 byte_order = DBUS_LITTLE_ENDIAN;
1942 _dbus_string_set_length (&str, 0);
1944 /* reset pos for the macros */
1947 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1949 /* Set it to something longer */
1950 _dbus_string_init_const (&t, "Hello world foo");
1952 v_STRING = _dbus_string_get_const_data (&t);
1953 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1954 &v_STRING, byte_order, NULL, NULL);
1956 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1957 &v_STRING, byte_order,
1959 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1961 /* Set it to something shorter */
1962 _dbus_string_init_const (&t, "Hello");
1964 v_STRING = _dbus_string_get_const_data (&t);
1965 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1966 &v_STRING, byte_order, NULL, NULL);
1967 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1968 &v_STRING, byte_order,
1970 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1972 /* Do the other byte order */
1973 if (byte_order == DBUS_LITTLE_ENDIAN)
1974 byte_order = DBUS_BIG_ENDIAN;
1980 _dbus_string_free (&str);
1985 #endif /* DBUS_BUILD_TESTS */