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"
33 * @defgroup DBusMarshal marshaling and unmarshaling
34 * @ingroup DBusInternals
35 * @brief functions to marshal/unmarshal data from the wire
37 * Types and functions related to converting primitive data types from
38 * wire format to native machine format, and vice versa.
40 * A signature is just a string with multiple types one after the other.
41 * for example a type is "i" or "(ii)", a signature is "i(ii)"
42 * where i is int and (ii) is struct { int; int; }
48 pack_2_octets (dbus_uint16_t value,
52 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
54 if ((byte_order) == DBUS_LITTLE_ENDIAN)
55 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
57 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
61 pack_4_octets (dbus_uint32_t value,
65 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
67 if ((byte_order) == DBUS_LITTLE_ENDIAN)
68 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
70 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
74 pack_8_octets (DBusBasicValue value,
78 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
80 #ifdef DBUS_HAVE_INT64
81 if ((byte_order) == DBUS_LITTLE_ENDIAN)
82 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
84 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
86 *(DBus8ByteStruct*)data = value.u64;
87 swap_8_octets ((DBusBasicValue*)data, byte_order);
92 * Packs a 32 bit unsigned 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_uint32 (dbus_uint32_t value,
103 pack_4_octets (value, byte_order, data);
106 #ifndef DBUS_HAVE_INT64
109 swap_bytes (unsigned char *data,
112 unsigned char *p1 = data;
113 unsigned char *p2 = data + len - 1;
117 unsigned char tmp = *p1;
125 #endif /* !DBUS_HAVE_INT64 */
128 swap_8_octets (DBusBasicValue *value,
131 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
133 #ifdef DBUS_HAVE_INT64
134 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
136 swap_bytes ((unsigned char *)value, 8);
142 static DBusBasicValue
143 unpack_8_octets (int byte_order,
144 const unsigned char *data)
148 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
149 _dbus_assert (sizeof (r) == 8);
151 #ifdef DBUS_HAVE_INT64
152 if (byte_order == DBUS_LITTLE_ENDIAN)
153 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
155 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
157 r.u64 = *(DBus8ByteStruct*)data;
158 swap_8_octets (&r, byte_order);
165 #ifndef _dbus_unpack_uint16
167 * Unpacks a 16 bit unsigned integer from a data pointer
169 * @param byte_order The byte order to use
170 * @param data the data pointer
171 * @returns the integer
174 _dbus_unpack_uint16 (int byte_order,
175 const unsigned char *data)
177 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
179 if (byte_order == DBUS_LITTLE_ENDIAN)
180 return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
182 return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
184 #endif /* _dbus_unpack_uint16 */
186 #ifndef _dbus_unpack_uint32
188 * Unpacks a 32 bit unsigned integer from a data pointer
190 * @param byte_order The byte order to use
191 * @param data the data pointer
192 * @returns the integer
195 _dbus_unpack_uint32 (int byte_order,
196 const unsigned char *data)
198 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
200 if (byte_order == DBUS_LITTLE_ENDIAN)
201 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
203 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
205 #endif /* _dbus_unpack_uint32 */
208 set_2_octets (DBusString *str,
215 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
216 byte_order == DBUS_BIG_ENDIAN);
218 data = _dbus_string_get_data_len (str, offset, 2);
220 pack_2_octets (value, byte_order, data);
224 set_4_octets (DBusString *str,
231 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
232 byte_order == DBUS_BIG_ENDIAN);
234 data = _dbus_string_get_data_len (str, offset, 4);
236 pack_4_octets (value, byte_order, data);
240 set_8_octets (DBusString *str,
242 DBusBasicValue value,
247 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
248 byte_order == DBUS_BIG_ENDIAN);
250 data = _dbus_string_get_data_len (str, offset, 8);
252 pack_8_octets (value, byte_order, data);
256 * Sets the 4 bytes at the given offset to a marshaled unsigned
257 * integer, replacing anything found there previously.
259 * @param str the string to write the marshalled int to
260 * @param pos the byte offset where int should be written
261 * @param value the value
262 * @param byte_order the byte order to use
266 _dbus_marshal_set_uint32 (DBusString *str,
271 set_4_octets (str, pos, value, byte_order);
275 * Sets the existing marshaled string at the given offset with
276 * a new marshaled string. The given offset must point to
277 * an existing string or the wrong length will be deleted
278 * and replaced with the new string.
280 * Note: no attempt is made by this function to re-align
281 * any data which has been already marshalled after this
282 * string. Use with caution.
284 * @param str the string to write the marshalled string to
285 * @param pos the position of the marshaled string length
286 * @param value the value
287 * @param byte_order the byte order to use
288 * @param old_end_pos place to store byte after the nul byte of the old value
289 * @param new_end_pos place to store byte after the nul byte of the new value
290 * @returns #TRUE on success, #FALSE if no memory
294 set_string (DBusString *str,
301 int old_len, new_len;
304 _dbus_string_init_const (&dstr, value);
306 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
307 old_len = _dbus_unpack_uint32 (byte_order,
308 _dbus_string_get_const_data_len (str, pos, 4));
310 new_len = _dbus_string_get_length (&dstr);
312 if (!_dbus_string_replace_len (&dstr, 0, new_len,
313 str, pos + 4, old_len))
316 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
319 *old_end_pos = pos + 4 + old_len + 1;
321 *new_end_pos = pos + 4 + new_len + 1;
327 * Sets the existing marshaled signature at the given offset to a new
328 * marshaled signature. Same basic ideas as set_string().
330 * @param str the string to write the marshalled signature to
331 * @param pos the position of the marshaled signature length
332 * @param value the value
333 * @param byte_order the byte order to use
334 * @param old_end_pos place to store byte after the nul byte of the old value
335 * @param new_end_pos place to store byte after the nul byte of the new value
336 * @returns #TRUE on success, #FALSE if no memory
340 set_signature (DBusString *str,
347 int old_len, new_len;
350 _dbus_string_init_const (&dstr, value);
352 old_len = _dbus_string_get_byte (str, pos);
353 new_len = _dbus_string_get_length (&dstr);
355 if (!_dbus_string_replace_len (&dstr, 0, new_len,
356 str, pos + 1, old_len))
359 _dbus_string_set_byte (str, pos, new_len);
362 *old_end_pos = pos + 1 + old_len + 1;
364 *new_end_pos = pos + 1 + new_len + 1;
370 * Sets an existing basic type value to a new value.
371 * Arguments work the same way as _dbus_marshal_basic_type().
373 * @param str the string
374 * @param pos location of the current value
375 * @param type the type of the current and new values
376 * @param value the address of the new value
377 * @param byte_order byte order for marshaling
378 * @param old_end_pos location to store end position of the old value, or #NULL
379 * @param new_end_pos location to store end position of the new value, or #NULL
380 * @returns #FALSE if no memory
383 _dbus_marshal_set_basic (DBusString *str,
391 const DBusBasicValue *vp;
398 _dbus_string_set_byte (str, pos, vp->byt);
400 *old_end_pos = pos + 1;
402 *new_end_pos = pos + 1;
405 case DBUS_TYPE_INT16:
406 case DBUS_TYPE_UINT16:
407 pos = _DBUS_ALIGN_VALUE (pos, 2);
408 set_2_octets (str, pos, vp->u16, byte_order);
410 *old_end_pos = pos + 2;
412 *new_end_pos = pos + 2;
415 case DBUS_TYPE_BOOLEAN:
416 case DBUS_TYPE_INT32:
417 case DBUS_TYPE_UINT32:
418 case DBUS_TYPE_UNIX_FD:
419 pos = _DBUS_ALIGN_VALUE (pos, 4);
420 set_4_octets (str, pos, vp->u32, byte_order);
422 *old_end_pos = pos + 4;
424 *new_end_pos = pos + 4;
427 case DBUS_TYPE_INT64:
428 case DBUS_TYPE_UINT64:
429 case DBUS_TYPE_DOUBLE:
430 pos = _DBUS_ALIGN_VALUE (pos, 8);
431 set_8_octets (str, pos, *vp, byte_order);
433 *old_end_pos = pos + 8;
435 *new_end_pos = pos + 8;
438 case DBUS_TYPE_STRING:
439 case DBUS_TYPE_OBJECT_PATH:
440 pos = _DBUS_ALIGN_VALUE (pos, 4);
441 _dbus_assert (vp->str != NULL);
442 return set_string (str, pos, vp->str, byte_order,
443 old_end_pos, new_end_pos);
445 case DBUS_TYPE_SIGNATURE:
446 _dbus_assert (vp->str != NULL);
447 return set_signature (str, pos, vp->str, byte_order,
448 old_end_pos, new_end_pos);
451 _dbus_assert_not_reached ("not a basic type");
458 * Convenience function to demarshal a 32 bit unsigned integer.
460 * @param str the string containing the data
461 * @param byte_order the byte order
462 * @param pos the position in the string
463 * @param new_pos the new position of the string
464 * @returns the demarshaled integer.
467 _dbus_marshal_read_uint32 (const DBusString *str,
472 pos = _DBUS_ALIGN_VALUE (pos, 4);
477 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
479 return _dbus_unpack_uint32 (byte_order,
480 _dbus_string_get_const_data (str) + pos);
484 * Demarshals a basic-typed value. The "value" pointer is always
485 * the address of a variable of the basic type. So e.g.
486 * if the basic type is "double" then the pointer is
487 * a double*, and if it's "char*" then the pointer is
490 * A value of type #DBusBasicValue is guaranteed to be large enough to
491 * hold any of the types that may be returned, which is handy if you
492 * are trying to do things generically. For example you can pass
493 * a DBusBasicValue* in to this function, and then pass the same
494 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
495 * move a value from one place to another.
497 * @param str the string containing the data
498 * @param pos position in the string
499 * @param type type of value to demarshal
500 * @param value pointer to return value data
501 * @param byte_order the byte order
502 * @param new_pos pointer to update with new position, or #NULL
505 _dbus_marshal_read_basic (const DBusString *str,
512 const char *str_data;
514 _dbus_assert (dbus_type_is_basic (type));
516 str_data = _dbus_string_get_const_data (str);
518 /* Below we volatile types to avoid aliasing issues;
519 * see http://bugs.freedesktop.org/show_bug.cgi?id=20137
526 volatile unsigned char *vp = value;
527 *vp = (unsigned char) _dbus_string_get_byte (str, pos);
531 case DBUS_TYPE_INT16:
532 case DBUS_TYPE_UINT16:
534 volatile dbus_uint16_t *vp = value;
535 pos = _DBUS_ALIGN_VALUE (pos, 2);
536 *vp = *(dbus_uint16_t *)(str_data + pos);
537 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
538 *vp = DBUS_UINT16_SWAP_LE_BE (*vp);
542 case DBUS_TYPE_INT32:
543 case DBUS_TYPE_UINT32:
544 case DBUS_TYPE_BOOLEAN:
545 case DBUS_TYPE_UNIX_FD:
547 volatile dbus_uint32_t *vp = value;
548 pos = _DBUS_ALIGN_VALUE (pos, 4);
549 *vp = *(dbus_uint32_t *)(str_data + pos);
550 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
551 *vp = DBUS_UINT32_SWAP_LE_BE (*vp);
555 case DBUS_TYPE_INT64:
556 case DBUS_TYPE_UINT64:
557 case DBUS_TYPE_DOUBLE:
559 volatile dbus_uint64_t *vp = value;
560 pos = _DBUS_ALIGN_VALUE (pos, 8);
561 #ifdef DBUS_HAVE_INT64
562 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
563 *vp = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
565 *vp = *(dbus_uint64_t*)(str_data + pos);
567 *vp = *(DBus8ByteStruct*) (str_data + pos);
568 swap_8_octets (vp, byte_order);
573 case DBUS_TYPE_STRING:
574 case DBUS_TYPE_OBJECT_PATH:
577 volatile char **vp = value;
579 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
581 *vp = (char*) str_data + pos;
583 pos += len + 1; /* length plus nul */
586 case DBUS_TYPE_SIGNATURE:
589 volatile char **vp = value;
591 len = _dbus_string_get_byte (str, pos);
594 *vp = (char*) str_data + pos;
596 pos += len + 1; /* length plus nul */
600 _dbus_warn_check_failed ("type %s %d not a basic type\n",
601 _dbus_type_to_string (type), type);
602 _dbus_assert_not_reached ("not a basic type");
611 marshal_2_octets (DBusString *str,
620 _dbus_assert (sizeof (value) == 2);
622 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
623 value = DBUS_UINT16_SWAP_LE_BE (value);
625 orig_len = _dbus_string_get_length (str);
627 retval = _dbus_string_insert_2_aligned (str, insert_at,
628 (const unsigned char *)&value);
632 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
633 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
640 marshal_4_octets (DBusString *str,
649 _dbus_assert (sizeof (value) == 4);
651 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
652 value = DBUS_UINT32_SWAP_LE_BE (value);
654 orig_len = _dbus_string_get_length (str);
656 retval = _dbus_string_insert_4_aligned (str, insert_at,
657 (const unsigned char *)&value);
661 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
662 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
669 marshal_8_octets (DBusString *str,
671 DBusBasicValue value,
678 _dbus_assert (sizeof (value) == 8);
680 swap_8_octets (&value, byte_order);
682 orig_len = _dbus_string_get_length (str);
684 retval = _dbus_string_insert_8_aligned (str, insert_at,
685 (const unsigned char *)&value);
688 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
696 MARSHAL_AS_SIGNATURE,
697 MARSHAL_AS_BYTE_ARRAY
701 marshal_len_followed_by_bytes (int marshal_as,
704 const unsigned char *value,
705 int data_len, /* doesn't include nul if any */
710 DBusString value_str;
713 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
714 if (insert_at > _dbus_string_get_length (str))
715 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
716 insert_at, _dbus_string_get_length (str), data_len);
718 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
719 value_len = data_len;
721 value_len = data_len + 1; /* value has a nul */
723 _dbus_string_init_const_len (&value_str, value, value_len);
727 if (marshal_as == MARSHAL_AS_SIGNATURE)
729 _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
730 _dbus_assert (data_len <= 255); /* same as max sig len right now */
732 if (!_dbus_string_insert_byte (str, pos, data_len))
739 if (!marshal_4_octets (str, pos, data_len,
744 if (!_dbus_string_copy_len (&value_str, 0, value_len,
750 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
752 _dbus_verbose_bytes_of_string (str, pos, value_len);
763 /* Delete what we've inserted */
764 _dbus_string_delete (str, insert_at, pos - insert_at);
770 marshal_string (DBusString *str,
776 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
777 str, insert_at, value,
779 byte_order, pos_after);
783 marshal_signature (DBusString *str,
788 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
789 str, insert_at, value,
791 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
796 * Marshals a basic-typed value. The "value" pointer is always the
797 * address of a variable containing the basic type value.
798 * So for example for int32 it will be dbus_int32_t*, and
799 * for string it will be const char**. This is for symmetry
800 * with _dbus_marshal_read_basic() and to have a simple
803 * @param str string to marshal to
804 * @param insert_at where to insert the value
805 * @param type type of value
806 * @param value pointer to a variable containing the value
807 * @param byte_order byte order
808 * @param pos_after #NULL or the position after the type
809 * @returns #TRUE on success
812 _dbus_marshal_write_basic (DBusString *str,
819 const DBusBasicValue *vp;
821 _dbus_assert (dbus_type_is_basic (type));
828 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
831 *pos_after = insert_at + 1;
834 case DBUS_TYPE_INT16:
835 case DBUS_TYPE_UINT16:
836 return marshal_2_octets (str, insert_at, vp->u16,
837 byte_order, pos_after);
839 case DBUS_TYPE_BOOLEAN:
840 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
841 byte_order, pos_after);
843 case DBUS_TYPE_INT32:
844 case DBUS_TYPE_UINT32:
845 case DBUS_TYPE_UNIX_FD:
846 return marshal_4_octets (str, insert_at, vp->u32,
847 byte_order, pos_after);
849 case DBUS_TYPE_INT64:
850 case DBUS_TYPE_UINT64:
851 case DBUS_TYPE_DOUBLE:
852 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
855 case DBUS_TYPE_STRING:
856 case DBUS_TYPE_OBJECT_PATH:
857 _dbus_assert (vp->str != NULL);
858 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
860 case DBUS_TYPE_SIGNATURE:
861 _dbus_assert (vp->str != NULL);
862 return marshal_signature (str, insert_at, vp->str, pos_after);
865 _dbus_assert_not_reached ("not a basic type");
872 marshal_1_octets_array (DBusString *str,
874 const unsigned char *value,
880 DBusString value_str;
882 _dbus_string_init_const_len (&value_str, value, n_elements);
886 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
899 * Swaps the elements of an array to the opposite byte order
901 * @param data start of array
902 * @param n_elements number of elements
903 * @param alignment size of each element
906 _dbus_swap_array (unsigned char *data,
913 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
915 /* we use const_data and cast it off so DBusString can be a const string
916 * for the unit tests. don't ask.
919 end = d + (n_elements * alignment);
925 #ifdef DBUS_HAVE_INT64
926 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
928 swap_8_bytes ((DBusBasicValue*) d);
933 else if (alignment == 4)
937 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
943 _dbus_assert (alignment == 2);
947 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
954 swap_array (DBusString *str,
960 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
962 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
964 /* we use const_data and cast it off so DBusString can be a const string
965 * for the unit tests. don't ask.
967 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
968 n_elements, alignment);
973 marshal_fixed_multi (DBusString *str,
975 const DBusBasicValue *value,
986 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
988 old_string_len = _dbus_string_get_length (str);
990 len_in_bytes = n_elements * alignment;
991 array_start = insert_at;
993 /* Note that we do alignment padding unconditionally
994 * even if the array is empty; this means that
995 * padding + len is always equal to the number of bytes
999 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1002 _dbus_string_init_const_len (&t,
1003 (const unsigned char*) value,
1006 if (!_dbus_string_copy (&t, 0,
1010 swap_array (str, array_start, n_elements, byte_order, alignment);
1013 *pos_after = array_start + len_in_bytes;
1018 _dbus_string_delete (str, insert_at,
1019 _dbus_string_get_length (str) - old_string_len);
1025 * Marshals a block of values of fixed-length type all at once, as an
1026 * optimization. dbus_type_is_fixed() returns #TRUE for fixed-length
1027 * types, which are the basic types minus the string-like types.
1029 * The value argument should be the adddress of an
1030 * array, so e.g. "const dbus_uint32_t**"
1032 * @param str string to marshal to
1033 * @param insert_at where to insert the value
1034 * @param element_type type of array elements
1035 * @param value address of an array to marshal
1036 * @param n_elements number of elements in the array
1037 * @param byte_order byte order
1038 * @param pos_after #NULL or the position after the type
1039 * @returns #TRUE on success
1042 _dbus_marshal_write_fixed_multi (DBusString *str,
1050 const void* vp = *(const DBusBasicValue**)value;
1052 _dbus_assert (dbus_type_is_fixed (element_type));
1053 _dbus_assert (n_elements >= 0);
1056 _dbus_verbose ("writing %d elements of %s\n",
1057 n_elements, _dbus_type_to_string (element_type));
1060 switch (element_type)
1062 case DBUS_TYPE_BYTE:
1063 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1065 case DBUS_TYPE_INT16:
1066 case DBUS_TYPE_UINT16:
1067 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1068 case DBUS_TYPE_BOOLEAN:
1069 case DBUS_TYPE_INT32:
1070 case DBUS_TYPE_UINT32:
1071 case DBUS_TYPE_UNIX_FD:
1072 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1074 case DBUS_TYPE_INT64:
1075 case DBUS_TYPE_UINT64:
1076 case DBUS_TYPE_DOUBLE:
1077 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1081 _dbus_assert_not_reached ("non fixed type in array write");
1090 * Skips over a basic-typed value, reporting the following position.
1092 * @param str the string containing the data
1093 * @param type type of value to read
1094 * @param byte_order the byte order
1095 * @param pos pointer to position in the string,
1096 * updated on return to new position
1099 _dbus_marshal_skip_basic (const DBusString *str,
1104 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1105 byte_order == DBUS_BIG_ENDIAN);
1109 case DBUS_TYPE_BYTE:
1112 case DBUS_TYPE_INT16:
1113 case DBUS_TYPE_UINT16:
1114 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1117 case DBUS_TYPE_BOOLEAN:
1118 case DBUS_TYPE_INT32:
1119 case DBUS_TYPE_UINT32:
1120 case DBUS_TYPE_UNIX_FD:
1121 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1124 case DBUS_TYPE_INT64:
1125 case DBUS_TYPE_UINT64:
1126 case DBUS_TYPE_DOUBLE:
1127 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1130 case DBUS_TYPE_STRING:
1131 case DBUS_TYPE_OBJECT_PATH:
1135 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1137 *pos += len + 1; /* length plus nul */
1140 case DBUS_TYPE_SIGNATURE:
1144 len = _dbus_string_get_byte (str, *pos);
1146 *pos += len + 2; /* length byte plus length plus nul */
1150 _dbus_warn ("type %s not a basic type\n",
1151 _dbus_type_to_string (type));
1152 _dbus_assert_not_reached ("not a basic type");
1158 * Skips an array, returning the next position.
1160 * @param str the string containing the data
1161 * @param element_type the type of array elements
1162 * @param byte_order the byte order
1163 * @param pos pointer to position in the string,
1164 * updated on return to new position
1167 _dbus_marshal_skip_array (const DBusString *str,
1172 dbus_uint32_t array_len;
1176 i = _DBUS_ALIGN_VALUE (*pos, 4);
1178 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1180 alignment = _dbus_type_get_alignment (element_type);
1182 i = _DBUS_ALIGN_VALUE (i, alignment);
1184 *pos = i + array_len;
1188 * Gets the alignment requirement for the given type;
1189 * will be 1, 4, or 8.
1191 * @param typecode the type
1192 * @returns alignment of 1, 4, or 8
1195 _dbus_type_get_alignment (int typecode)
1199 case DBUS_TYPE_BYTE:
1200 case DBUS_TYPE_VARIANT:
1201 case DBUS_TYPE_SIGNATURE:
1203 case DBUS_TYPE_INT16:
1204 case DBUS_TYPE_UINT16:
1206 case DBUS_TYPE_BOOLEAN:
1207 case DBUS_TYPE_INT32:
1208 case DBUS_TYPE_UINT32:
1209 case DBUS_TYPE_UNIX_FD:
1210 /* this stuff is 4 since it starts with a length */
1211 case DBUS_TYPE_STRING:
1212 case DBUS_TYPE_OBJECT_PATH:
1213 case DBUS_TYPE_ARRAY:
1215 case DBUS_TYPE_INT64:
1216 case DBUS_TYPE_UINT64:
1217 case DBUS_TYPE_DOUBLE:
1218 /* struct is 8 since it could contain an 8-aligned item
1219 * and it's simpler to just always align structs to 8;
1220 * we want the amount of padding in a struct of a given
1221 * type to be predictable, not location-dependent.
1222 * DICT_ENTRY is always the same as struct.
1224 case DBUS_TYPE_STRUCT:
1225 case DBUS_TYPE_DICT_ENTRY:
1229 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1236 * Return #TRUE if the typecode is a valid typecode.
1237 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1238 * random unknown bytes aren't either. This function is safe with
1241 * @returns #TRUE if valid
1244 _dbus_type_is_valid (int typecode)
1248 case DBUS_TYPE_BYTE:
1249 case DBUS_TYPE_BOOLEAN:
1250 case DBUS_TYPE_INT16:
1251 case DBUS_TYPE_UINT16:
1252 case DBUS_TYPE_INT32:
1253 case DBUS_TYPE_UINT32:
1254 case DBUS_TYPE_INT64:
1255 case DBUS_TYPE_UINT64:
1256 case DBUS_TYPE_DOUBLE:
1257 case DBUS_TYPE_STRING:
1258 case DBUS_TYPE_OBJECT_PATH:
1259 case DBUS_TYPE_SIGNATURE:
1260 case DBUS_TYPE_ARRAY:
1261 case DBUS_TYPE_STRUCT:
1262 case DBUS_TYPE_DICT_ENTRY:
1263 case DBUS_TYPE_VARIANT:
1264 case DBUS_TYPE_UNIX_FD:
1273 * Returns a string describing the given type.
1275 * @param typecode the type to describe
1276 * @returns a constant string describing the type
1279 _dbus_type_to_string (int typecode)
1283 case DBUS_TYPE_INVALID:
1285 case DBUS_TYPE_BOOLEAN:
1287 case DBUS_TYPE_BYTE:
1289 case DBUS_TYPE_INT16:
1291 case DBUS_TYPE_UINT16:
1293 case DBUS_TYPE_INT32:
1295 case DBUS_TYPE_UINT32:
1297 case DBUS_TYPE_INT64:
1299 case DBUS_TYPE_UINT64:
1301 case DBUS_TYPE_DOUBLE:
1303 case DBUS_TYPE_STRING:
1305 case DBUS_TYPE_OBJECT_PATH:
1306 return "object_path";
1307 case DBUS_TYPE_SIGNATURE:
1309 case DBUS_TYPE_STRUCT:
1311 case DBUS_TYPE_DICT_ENTRY:
1312 return "dict_entry";
1313 case DBUS_TYPE_ARRAY:
1315 case DBUS_TYPE_VARIANT:
1317 case DBUS_STRUCT_BEGIN_CHAR:
1318 return "begin_struct";
1319 case DBUS_STRUCT_END_CHAR:
1320 return "end_struct";
1321 case DBUS_DICT_ENTRY_BEGIN_CHAR:
1322 return "begin_dict_entry";
1323 case DBUS_DICT_ENTRY_END_CHAR:
1324 return "end_dict_entry";
1325 case DBUS_TYPE_UNIX_FD:
1333 * If in verbose mode, print a block of binary data.
1335 * @param data the data
1336 * @param len the length of the data
1337 * @param offset where to start counting for byte indexes
1340 _dbus_verbose_bytes (const unsigned char *data,
1345 const unsigned char *aligned;
1347 _dbus_assert (len >= 0);
1349 if (!_dbus_is_verbose())
1352 /* Print blanks on first row if appropriate */
1353 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1356 _dbus_assert (aligned <= data);
1358 if (aligned != data)
1360 _dbus_verbose ("%4ld\t%p: ", - (long)(data - aligned), aligned);
1361 while (aligned != data)
1363 _dbus_verbose (" ");
1368 /* now print the bytes */
1372 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1374 _dbus_verbose ("%4d\t%p: ",
1375 offset + i, &data[i]);
1378 if (data[i] >= 32 &&
1380 _dbus_verbose (" '%c' ", data[i]);
1382 _dbus_verbose ("0x%s%x ",
1383 data[i] <= 0xf ? "0" : "", data[i]);
1387 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1390 _dbus_verbose ("BE: %d LE: %d",
1391 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1392 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1395 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1397 #ifdef DBUS_INT64_PRINTF_MODIFIER
1398 _dbus_verbose (" u64: 0x%" DBUS_INT64_PRINTF_MODIFIER "x",
1399 *(dbus_uint64_t*)&data[i-8]);
1401 _dbus_verbose (" dbl: %g",
1402 *(double*)&data[i-8]);
1405 _dbus_verbose ("\n");
1409 _dbus_verbose ("\n");
1413 * Dump the given part of the string to verbose log.
1415 * @param str the string
1416 * @param start the start of range to dump
1417 * @param len length of range
1420 _dbus_verbose_bytes_of_string (const DBusString *str,
1427 real_len = _dbus_string_get_length (str);
1429 _dbus_assert (start >= 0);
1431 if (start > real_len)
1433 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1434 start, len, real_len);
1438 if ((start + len) > real_len)
1440 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1441 start, len, real_len);
1442 len = real_len - start;
1445 d = _dbus_string_get_const_data_len (str, start, len);
1447 _dbus_verbose_bytes (d, len, start);
1451 map_type_char_to_type (int t)
1453 if (t == DBUS_STRUCT_BEGIN_CHAR)
1454 return DBUS_TYPE_STRUCT;
1455 else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1456 return DBUS_TYPE_DICT_ENTRY;
1459 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1460 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1466 * Get the first type in the signature. The difference between this
1467 * and just getting the first byte of the signature is that you won't
1468 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1471 * @param str string containing signature
1472 * @param pos where the signature starts
1473 * @returns the first type in the signature
1476 _dbus_first_type_in_signature (const DBusString *str,
1479 return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1483 * Similar to #_dbus_first_type_in_signature, but operates
1484 * on a C string buffer.
1486 * @param str a C string buffer
1487 * @param pos where the signature starts
1488 * @returns the first type in the signature
1491 _dbus_first_type_in_signature_c_str (const char *str,
1494 return map_type_char_to_type (str[pos]);
1499 #ifdef DBUS_BUILD_TESTS
1500 #include "dbus-test.h"
1504 * Reads a block of fixed-length basic values, as an optimization
1505 * vs. reading each one individually into a new buffer.
1507 * This function returns the data in-place; it does not make a copy,
1508 * and it does not swap the bytes.
1510 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
1511 * and the "value" argument should be a "const double**" and so on.
1513 * @param str the string to read from
1514 * @param pos position to read from
1515 * @param element_type type of array elements
1516 * @param value place to return the array
1517 * @param n_elements number of array elements to read
1518 * @param byte_order the byte order, used to read the array length
1519 * @param new_pos #NULL or location to store a position after the elements
1522 _dbus_marshal_read_fixed_multi (const DBusString *str,
1533 _dbus_assert (dbus_type_is_fixed (element_type));
1534 _dbus_assert (dbus_type_is_basic (element_type));
1537 _dbus_verbose ("reading %d elements of %s\n",
1538 n_elements, _dbus_type_to_string (element_type));
1541 alignment = _dbus_type_get_alignment (element_type);
1543 pos = _DBUS_ALIGN_VALUE (pos, alignment);
1545 array_len = n_elements * alignment;
1547 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
1549 *new_pos = pos + array_len;
1553 swap_test_array (void *array,
1563 _dbus_string_init_const_len (&t, array, len_bytes);
1564 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1567 #define MARSHAL_BASIC(typename, byte_order, literal) \
1569 v_##typename = literal; \
1570 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1572 byte_order, NULL)) \
1573 _dbus_assert_not_reached ("no memory"); \
1576 #define DEMARSHAL_BASIC(typename, byte_order) \
1578 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1579 byte_order, &pos); \
1582 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1584 DEMARSHAL_BASIC (typename, byte_order); \
1585 if (literal != v_##typename) \
1587 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1588 _dbus_string_get_length (&str) - dump_pos); \
1589 _dbus_assert_not_reached ("demarshaled wrong value"); \
1593 #define MARSHAL_TEST(typename, byte_order, literal) \
1595 MARSHAL_BASIC (typename, byte_order, literal); \
1597 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1600 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1602 MARSHAL_BASIC (typename, byte_order, literal); \
1604 DEMARSHAL_BASIC (typename, byte_order); \
1605 if (strcmp (literal, v_##typename) != 0) \
1607 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1608 _dbus_string_get_length (&str) - dump_pos); \
1609 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1610 _dbus_assert_not_reached ("demarshaled wrong value"); \
1614 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1617 v_UINT32 = sizeof(literal); \
1618 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1619 byte_order, &next)) \
1620 _dbus_assert_not_reached ("no memory"); \
1621 v_ARRAY_##typename = literal; \
1622 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1623 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1624 byte_order, NULL)) \
1625 _dbus_assert_not_reached ("no memory"); \
1628 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1631 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1632 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1633 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1634 v_UINT32/alignment, \
1635 byte_order, NULL); \
1636 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1637 byte_order, alignment); \
1640 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1642 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1643 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1645 _dbus_verbose ("MARSHALED DATA\n"); \
1646 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1647 _dbus_string_get_length (&str) - dump_pos); \
1648 _dbus_verbose ("LITERAL DATA\n"); \
1649 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1650 _dbus_verbose ("READ DATA\n"); \
1651 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1652 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1656 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1658 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1660 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1664 _dbus_marshal_test (void)
1669 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1670 dbus_int16_t array2[3] = { 124, 457, 780 };
1671 dbus_int32_t array4[3] = { 123, 456, 789 };
1672 #ifdef DBUS_HAVE_INT64
1673 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1674 DBUS_INT64_CONSTANT (0x456ffffffff),
1675 DBUS_INT64_CONSTANT (0x789ffffffff) };
1676 dbus_int64_t *v_ARRAY_INT64;
1678 unsigned char *v_ARRAY_BYTE;
1679 dbus_int16_t *v_ARRAY_INT16;
1680 dbus_uint16_t *v_ARRAY_UINT16;
1681 dbus_int32_t *v_ARRAY_INT32;
1682 dbus_uint32_t *v_ARRAY_UINT32;
1686 dbus_int16_t v_INT16;
1687 dbus_uint16_t v_UINT16;
1688 dbus_int32_t v_INT32;
1689 dbus_uint32_t v_UINT32;
1690 dbus_int64_t v_INT64;
1691 dbus_uint64_t v_UINT64;
1692 unsigned char v_BYTE;
1693 dbus_bool_t v_BOOLEAN;
1694 const char *v_STRING;
1695 const char *v_SIGNATURE;
1696 const char *v_OBJECT_PATH;
1699 if (!_dbus_string_init (&str))
1700 _dbus_assert_not_reached ("failed to init string");
1704 /* Marshal doubles */
1705 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1706 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1708 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1709 _dbus_assert_not_reached ("got wrong double value");
1711 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1712 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1714 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1715 _dbus_assert_not_reached ("got wrong double value");
1717 /* Marshal signed 16 integers */
1718 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1719 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1721 /* Marshal unsigned 16 integers */
1722 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1723 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1725 /* Marshal signed integers */
1726 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1727 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1729 /* Marshal unsigned integers */
1730 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1731 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1733 #ifdef DBUS_HAVE_INT64
1734 /* Marshal signed integers */
1735 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1736 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1738 /* Marshal unsigned integers */
1739 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1740 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1741 #endif /* DBUS_HAVE_INT64 */
1744 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1745 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1747 /* Marshal all possible bools! */
1748 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1749 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1750 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1751 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1753 /* Marshal strings */
1754 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1755 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1756 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1757 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1760 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1761 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1764 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1765 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1766 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1767 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1770 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1771 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1772 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2);
1773 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2);
1775 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1776 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1777 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4);
1778 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4);
1780 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1781 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1783 #ifdef DBUS_HAVE_INT64
1784 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1785 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1791 * FIXME restore the set/pack tests
1794 #ifdef DBUS_HAVE_INT64
1795 /* set/pack 64-bit integers */
1796 _dbus_string_set_length (&str, 8);
1799 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1800 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1802 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1803 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1804 _dbus_string_get_const_data (&str)));
1807 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1808 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1810 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1811 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1812 _dbus_string_get_const_data (&str)));
1814 /* signed little pack */
1815 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1817 _dbus_string_get_data (&str));
1819 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1820 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1821 _dbus_string_get_const_data (&str)));
1823 /* signed big pack */
1824 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1826 _dbus_string_get_data (&str));
1828 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1829 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1830 _dbus_string_get_const_data (&str)));
1832 /* unsigned little */
1833 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1834 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1836 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1837 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1838 _dbus_string_get_const_data (&str)));
1841 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1842 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1844 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1845 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1846 _dbus_string_get_const_data (&str)));
1848 /* unsigned little pack */
1849 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1851 _dbus_string_get_data (&str));
1853 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1854 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1855 _dbus_string_get_const_data (&str)));
1857 /* unsigned big pack */
1858 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1860 _dbus_string_get_data (&str));
1862 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1863 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1864 _dbus_string_get_const_data (&str)));
1865 #endif /* DBUS_HAVE_INT64 */
1867 /* set/pack 32-bit integers */
1868 _dbus_string_set_length (&str, 4);
1871 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1874 _dbus_assert (-0x123456 ==
1875 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1876 _dbus_string_get_const_data (&str)));
1879 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1882 _dbus_assert (-0x123456 ==
1883 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1884 _dbus_string_get_const_data (&str)));
1886 /* signed little pack */
1887 _dbus_pack_int32 (-0x123456,
1889 _dbus_string_get_data (&str));
1891 _dbus_assert (-0x123456 ==
1892 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1893 _dbus_string_get_const_data (&str)));
1895 /* signed big pack */
1896 _dbus_pack_int32 (-0x123456,
1898 _dbus_string_get_data (&str));
1900 _dbus_assert (-0x123456 ==
1901 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1902 _dbus_string_get_const_data (&str)));
1904 /* unsigned little */
1905 _dbus_marshal_set_uint32 (&str,
1907 DBUS_LITTLE_ENDIAN);
1909 _dbus_assert (0x123456 ==
1910 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1911 _dbus_string_get_const_data (&str)));
1914 _dbus_marshal_set_uint32 (&str,
1918 _dbus_assert (0x123456 ==
1919 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1920 _dbus_string_get_const_data (&str)));
1922 /* unsigned little pack */
1923 _dbus_pack_uint32 (0x123456,
1925 _dbus_string_get_data (&str));
1927 _dbus_assert (0x123456 ==
1928 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1929 _dbus_string_get_const_data (&str)));
1931 /* unsigned big pack */
1932 _dbus_pack_uint32 (0x123456,
1934 _dbus_string_get_data (&str));
1936 _dbus_assert (0x123456 ==
1937 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1938 _dbus_string_get_const_data (&str)));
1940 #endif /* set/pack tests for integers */
1942 /* Strings in-place set */
1943 byte_order = DBUS_LITTLE_ENDIAN;
1947 _dbus_string_set_length (&str, 0);
1949 /* reset pos for the macros */
1952 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1954 /* Set it to something longer */
1955 _dbus_string_init_const (&t, "Hello world foo");
1957 v_STRING = _dbus_string_get_const_data (&t);
1958 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1959 &v_STRING, byte_order, NULL, NULL);
1961 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1962 &v_STRING, byte_order,
1964 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1966 /* Set it to something shorter */
1967 _dbus_string_init_const (&t, "Hello");
1969 v_STRING = _dbus_string_get_const_data (&t);
1970 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1971 &v_STRING, byte_order, NULL, NULL);
1972 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1973 &v_STRING, byte_order,
1975 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1977 /* Do the other byte order */
1978 if (byte_order == DBUS_LITTLE_ENDIAN)
1979 byte_order = DBUS_BIG_ENDIAN;
1985 _dbus_string_free (&str);
1990 #endif /* DBUS_BUILD_TESTS */