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(PRIx64) && defined(DBUS_WIN)
36 #if defined(__GNUC__) && (__GNUC__ >= 4)
37 # define _DBUS_ASSERT_ALIGNMENT(type, op, val) \
38 _DBUS_STATIC_ASSERT (__extension__ __alignof__ (type) op val)
40 /* not gcc, so probably no alignof operator: just use a no-op statement
41 * that's valid in the same contexts */
42 # define _DBUS_ASSERT_ALIGNMENT(type, op, val) \
43 _DBUS_STATIC_ASSERT (TRUE)
46 /* True by definition, but just for completeness... */
47 _DBUS_STATIC_ASSERT (sizeof (char) == 1);
48 _DBUS_ASSERT_ALIGNMENT (char, ==, 1);
50 _DBUS_STATIC_ASSERT (sizeof (dbus_int16_t) == 2);
51 _DBUS_ASSERT_ALIGNMENT (dbus_int16_t, <=, 2);
52 _DBUS_STATIC_ASSERT (sizeof (dbus_uint16_t) == 2);
53 _DBUS_ASSERT_ALIGNMENT (dbus_uint16_t, <=, 2);
55 _DBUS_STATIC_ASSERT (sizeof (dbus_int32_t) == 4);
56 _DBUS_ASSERT_ALIGNMENT (dbus_int32_t, <=, 4);
57 _DBUS_STATIC_ASSERT (sizeof (dbus_uint32_t) == 4);
58 _DBUS_ASSERT_ALIGNMENT (dbus_uint32_t, <=, 4);
59 _DBUS_STATIC_ASSERT (sizeof (dbus_bool_t) == 4);
60 _DBUS_ASSERT_ALIGNMENT (dbus_bool_t, <=, 4);
62 _DBUS_STATIC_ASSERT (sizeof (double) == 8);
63 _DBUS_ASSERT_ALIGNMENT (double, <=, 8);
65 _DBUS_STATIC_ASSERT (sizeof (dbus_int64_t) == 8);
66 _DBUS_ASSERT_ALIGNMENT (dbus_int64_t, <=, 8);
67 _DBUS_STATIC_ASSERT (sizeof (dbus_uint64_t) == 8);
68 _DBUS_ASSERT_ALIGNMENT (dbus_uint64_t, <=, 8);
70 _DBUS_STATIC_ASSERT (sizeof (DBusBasicValue) >= 8);
71 /* The alignment of a DBusBasicValue might conceivably be > 8 because of the
72 * pointer, so we don't assert about it */
74 _DBUS_STATIC_ASSERT (sizeof (DBus8ByteStruct) == 8);
75 _DBUS_ASSERT_ALIGNMENT (DBus8ByteStruct, <=, 8);
78 * @defgroup DBusMarshal marshaling and unmarshaling
79 * @ingroup DBusInternals
80 * @brief functions to marshal/unmarshal data from the wire
82 * Types and functions related to converting primitive data types from
83 * wire format to native machine format, and vice versa.
85 * A signature is just a string with multiple types one after the other.
86 * for example a type is "i" or "(ii)", a signature is "i(ii)"
87 * where i is int and (ii) is struct { int; int; }
93 pack_2_octets (dbus_uint16_t value,
97 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
99 if ((byte_order) == DBUS_LITTLE_ENDIAN)
100 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
102 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
106 pack_4_octets (dbus_uint32_t value,
110 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
112 if ((byte_order) == DBUS_LITTLE_ENDIAN)
113 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
115 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
119 pack_8_octets (DBusBasicValue value,
123 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
125 if ((byte_order) == DBUS_LITTLE_ENDIAN)
126 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
128 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
132 * Packs a 32 bit unsigned integer into a data pointer.
134 * @param value the value
135 * @param byte_order the byte order to use
136 * @param data the data pointer
139 _dbus_pack_uint32 (dbus_uint32_t value,
143 pack_4_octets (value, byte_order, data);
147 swap_8_octets (DBusBasicValue *value,
150 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
152 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
156 #ifndef _dbus_unpack_uint16
158 * Unpacks a 16 bit unsigned integer from a data pointer
160 * @param byte_order The byte order to use
161 * @param data the data pointer
162 * @returns the integer
165 _dbus_unpack_uint16 (int byte_order,
166 const unsigned char *data)
168 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
170 if (byte_order == DBUS_LITTLE_ENDIAN)
171 return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
173 return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
175 #endif /* _dbus_unpack_uint16 */
177 #ifndef _dbus_unpack_uint32
179 * Unpacks a 32 bit unsigned integer from a data pointer
181 * @param byte_order The byte order to use
182 * @param data the data pointer
183 * @returns the integer
186 _dbus_unpack_uint32 (int byte_order,
187 const unsigned char *data)
189 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
191 if (byte_order == DBUS_LITTLE_ENDIAN)
192 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
194 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
196 #endif /* _dbus_unpack_uint32 */
199 set_2_octets (DBusString *str,
206 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
207 byte_order == DBUS_BIG_ENDIAN);
209 data = _dbus_string_get_data_len (str, offset, 2);
211 pack_2_octets (value, byte_order, (unsigned char *) data);
215 set_4_octets (DBusString *str,
222 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
223 byte_order == DBUS_BIG_ENDIAN);
225 data = _dbus_string_get_data_len (str, offset, 4);
227 pack_4_octets (value, byte_order, (unsigned char *) data);
231 set_8_octets (DBusString *str,
233 DBusBasicValue value,
238 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
239 byte_order == DBUS_BIG_ENDIAN);
241 data = _dbus_string_get_data_len (str, offset, 8);
243 pack_8_octets (value, byte_order, (unsigned char *) data);
247 * Sets the 4 bytes at the given offset to a marshaled unsigned
248 * integer, replacing anything found there previously.
250 * @param str the string to write the marshalled int to
251 * @param pos the byte offset where int should be written
252 * @param value the value
253 * @param byte_order the byte order to use
257 _dbus_marshal_set_uint32 (DBusString *str,
262 set_4_octets (str, pos, value, byte_order);
266 * Sets the existing marshaled string at the given offset with
267 * a new marshaled string. The given offset must point to
268 * an existing string or the wrong length will be deleted
269 * and replaced with the new string.
271 * Note: no attempt is made by this function to re-align
272 * any data which has been already marshalled after this
273 * string. Use with caution.
275 * @param str the string to write the marshalled string to
276 * @param pos the position of the marshaled string length
277 * @param value the value
278 * @param byte_order the byte order to use
279 * @param old_end_pos place to store byte after the nul byte of the old value
280 * @param new_end_pos place to store byte after the nul byte of the new value
281 * @returns #TRUE on success, #FALSE if no memory
285 set_string (DBusString *str,
292 int old_len, new_len;
295 _dbus_string_init_const (&dstr, value);
297 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
298 old_len = _dbus_unpack_uint32 (byte_order,
299 _dbus_string_get_const_udata_len (str, pos, 4));
301 new_len = _dbus_string_get_length (&dstr);
303 if (!_dbus_string_replace_len (&dstr, 0, new_len,
304 str, pos + 4, old_len))
307 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
310 *old_end_pos = pos + 4 + old_len + 1;
312 *new_end_pos = pos + 4 + new_len + 1;
318 * Sets the existing marshaled signature at the given offset to a new
319 * marshaled signature. Same basic ideas as set_string().
321 * @param str the string to write the marshalled signature to
322 * @param pos the position of the marshaled signature length
323 * @param value the value
324 * @param byte_order the byte order to use
325 * @param old_end_pos place to store byte after the nul byte of the old value
326 * @param new_end_pos place to store byte after the nul byte of the new value
327 * @returns #TRUE on success, #FALSE if no memory
331 set_signature (DBusString *str,
338 int old_len, new_len;
341 _dbus_string_init_const (&dstr, value);
343 old_len = _dbus_string_get_byte (str, pos);
344 new_len = _dbus_string_get_length (&dstr);
346 if (!_dbus_string_replace_len (&dstr, 0, new_len,
347 str, pos + 1, old_len))
350 _dbus_string_set_byte (str, pos, new_len);
353 *old_end_pos = pos + 1 + old_len + 1;
355 *new_end_pos = pos + 1 + new_len + 1;
361 * Sets an existing basic type value to a new value.
362 * Arguments work the same way as _dbus_marshal_basic_type().
364 * @param str the string
365 * @param pos location of the current value
366 * @param type the type of the current and new values
367 * @param value the address of the new value
368 * @param byte_order byte order for marshaling
369 * @param old_end_pos location to store end position of the old value, or #NULL
370 * @param new_end_pos location to store end position of the new value, or #NULL
371 * @returns #FALSE if no memory
374 _dbus_marshal_set_basic (DBusString *str,
382 const DBusBasicValue *vp;
389 _dbus_string_set_byte (str, pos, vp->byt);
391 *old_end_pos = pos + 1;
393 *new_end_pos = pos + 1;
396 case DBUS_TYPE_INT16:
397 case DBUS_TYPE_UINT16:
398 pos = _DBUS_ALIGN_VALUE (pos, 2);
399 set_2_octets (str, pos, vp->u16, byte_order);
401 *old_end_pos = pos + 2;
403 *new_end_pos = pos + 2;
406 case DBUS_TYPE_BOOLEAN:
407 case DBUS_TYPE_INT32:
408 case DBUS_TYPE_UINT32:
409 case DBUS_TYPE_UNIX_FD:
410 pos = _DBUS_ALIGN_VALUE (pos, 4);
411 set_4_octets (str, pos, vp->u32, byte_order);
413 *old_end_pos = pos + 4;
415 *new_end_pos = pos + 4;
418 case DBUS_TYPE_INT64:
419 case DBUS_TYPE_UINT64:
420 case DBUS_TYPE_DOUBLE:
421 pos = _DBUS_ALIGN_VALUE (pos, 8);
422 set_8_octets (str, pos, *vp, byte_order);
424 *old_end_pos = pos + 8;
426 *new_end_pos = pos + 8;
429 case DBUS_TYPE_STRING:
430 case DBUS_TYPE_OBJECT_PATH:
431 pos = _DBUS_ALIGN_VALUE (pos, 4);
432 _dbus_assert (vp->str != NULL);
433 return set_string (str, pos, vp->str, byte_order,
434 old_end_pos, new_end_pos);
436 case DBUS_TYPE_SIGNATURE:
437 _dbus_assert (vp->str != NULL);
438 return set_signature (str, pos, vp->str, byte_order,
439 old_end_pos, new_end_pos);
442 _dbus_assert_not_reached ("not a basic type");
449 * Convenience function to demarshal a 32 bit unsigned integer.
451 * @param str the string containing the data
452 * @param byte_order the byte order
453 * @param pos the position in the string
454 * @param new_pos the new position of the string
455 * @returns the demarshaled integer.
458 _dbus_marshal_read_uint32 (const DBusString *str,
463 pos = _DBUS_ALIGN_VALUE (pos, 4);
468 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
470 return _dbus_unpack_uint32 (byte_order,
471 _dbus_string_get_const_udata (str) + pos);
475 * Demarshals a basic-typed value. The "value" pointer is always
476 * the address of a variable of the basic type. So e.g.
477 * if the basic type is "double" then the pointer is
478 * a double*, and if it's "char*" then the pointer is
481 * A value of type #DBusBasicValue is guaranteed to be large enough to
482 * hold any of the types that may be returned, which is handy if you
483 * are trying to do things generically. For example you can pass
484 * a DBusBasicValue* in to this function, and then pass the same
485 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
486 * move a value from one place to another.
488 * @param str the string containing the data
489 * @param pos position in the string
490 * @param type type of value to demarshal
491 * @param value pointer to return value data
492 * @param byte_order the byte order
493 * @param new_pos pointer to update with new position, or #NULL
496 _dbus_marshal_read_basic (const DBusString *str,
503 const char *str_data;
505 _dbus_assert (dbus_type_is_basic (type));
507 str_data = _dbus_string_get_const_data (str);
509 /* Below we volatile types to avoid aliasing issues;
510 * see http://bugs.freedesktop.org/show_bug.cgi?id=20137
517 volatile unsigned char *vp = value;
518 *vp = (unsigned char) _dbus_string_get_byte (str, pos);
522 case DBUS_TYPE_INT16:
523 case DBUS_TYPE_UINT16:
525 volatile dbus_uint16_t *vp = value;
526 pos = _DBUS_ALIGN_VALUE (pos, 2);
527 *vp = *(dbus_uint16_t *)(str_data + pos);
528 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
529 *vp = DBUS_UINT16_SWAP_LE_BE (*vp);
533 case DBUS_TYPE_INT32:
534 case DBUS_TYPE_UINT32:
535 case DBUS_TYPE_BOOLEAN:
536 case DBUS_TYPE_UNIX_FD:
538 volatile dbus_uint32_t *vp = value;
539 pos = _DBUS_ALIGN_VALUE (pos, 4);
540 *vp = *(dbus_uint32_t *)(str_data + pos);
541 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
542 *vp = DBUS_UINT32_SWAP_LE_BE (*vp);
546 case DBUS_TYPE_INT64:
547 case DBUS_TYPE_UINT64:
548 case DBUS_TYPE_DOUBLE:
550 volatile dbus_uint64_t *vp = value;
551 pos = _DBUS_ALIGN_VALUE (pos, 8);
552 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
553 *vp = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
555 *vp = *(dbus_uint64_t*)(str_data + pos);
559 case DBUS_TYPE_STRING:
560 case DBUS_TYPE_OBJECT_PATH:
563 volatile char **vp = value;
565 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
567 *vp = (char*) str_data + pos;
569 pos += len + 1; /* length plus nul */
572 case DBUS_TYPE_SIGNATURE:
575 volatile char **vp = value;
577 len = _dbus_string_get_byte (str, pos);
580 *vp = (char*) str_data + pos;
582 pos += len + 1; /* length plus nul */
586 _dbus_warn_check_failed ("type %s %d not a basic type",
587 _dbus_type_to_string (type), type);
588 _dbus_assert_not_reached ("not a basic type");
597 marshal_2_octets (DBusString *str,
606 _DBUS_STATIC_ASSERT (sizeof (value) == 2);
608 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
609 value = DBUS_UINT16_SWAP_LE_BE (value);
611 orig_len = _dbus_string_get_length (str);
613 retval = _dbus_string_insert_2_aligned (str, insert_at,
614 (const unsigned char *)&value);
618 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
619 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
626 marshal_4_octets (DBusString *str,
635 _DBUS_STATIC_ASSERT (sizeof (value) == 4);
637 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
638 value = DBUS_UINT32_SWAP_LE_BE (value);
640 orig_len = _dbus_string_get_length (str);
642 retval = _dbus_string_insert_4_aligned (str, insert_at,
643 (const unsigned char *)&value);
647 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
648 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
655 marshal_8_octets (DBusString *str,
657 DBusBasicValue value,
664 _DBUS_STATIC_ASSERT (sizeof (value) == 8);
666 swap_8_octets (&value, byte_order);
668 orig_len = _dbus_string_get_length (str);
670 retval = _dbus_string_insert_8_aligned (str, insert_at,
671 (const unsigned char *)&value);
674 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
682 MARSHAL_AS_SIGNATURE,
683 MARSHAL_AS_BYTE_ARRAY
687 marshal_len_followed_by_bytes (int marshal_as,
690 const unsigned char *value,
691 int data_len, /* doesn't include nul if any */
696 DBusString value_str;
699 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
700 if (insert_at > _dbus_string_get_length (str))
701 _dbus_warn ("insert_at = %d string len = %d data_len = %d",
702 insert_at, _dbus_string_get_length (str), data_len);
704 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
705 value_len = data_len;
707 value_len = data_len + 1; /* value has a nul */
709 _dbus_string_init_const_len (&value_str, (const char *) value, value_len);
713 if (marshal_as == MARSHAL_AS_SIGNATURE)
715 _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
716 _dbus_assert (data_len <= 255); /* same as max sig len right now */
718 if (!_dbus_string_insert_byte (str, pos, data_len))
725 if (!marshal_4_octets (str, pos, data_len,
730 if (!_dbus_string_copy_len (&value_str, 0, value_len,
736 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
738 _dbus_verbose_bytes_of_string (str, pos, value_len);
749 /* Delete what we've inserted */
750 _dbus_string_delete (str, insert_at, pos - insert_at);
756 marshal_string (DBusString *str,
762 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
763 str, insert_at, (const unsigned char *) value,
765 byte_order, pos_after);
769 marshal_signature (DBusString *str,
774 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
775 str, insert_at, (const unsigned char *) value,
777 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
782 * Marshals a basic-typed value. The "value" pointer is always the
783 * address of a variable containing the basic type value.
784 * So for example for int32 it will be dbus_int32_t*, and
785 * for string it will be const char**. This is for symmetry
786 * with _dbus_marshal_read_basic() and to have a simple
789 * @param str string to marshal to
790 * @param insert_at where to insert the value
791 * @param type type of value
792 * @param value pointer to a variable containing the value
793 * @param byte_order byte order
794 * @param pos_after #NULL or the position after the type
795 * @returns #TRUE on success
798 _dbus_marshal_write_basic (DBusString *str,
805 const DBusBasicValue *vp;
807 _dbus_assert (dbus_type_is_basic (type));
814 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
817 *pos_after = insert_at + 1;
820 case DBUS_TYPE_INT16:
821 case DBUS_TYPE_UINT16:
822 return marshal_2_octets (str, insert_at, vp->u16,
823 byte_order, pos_after);
825 case DBUS_TYPE_BOOLEAN:
826 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
827 byte_order, pos_after);
829 case DBUS_TYPE_INT32:
830 case DBUS_TYPE_UINT32:
831 case DBUS_TYPE_UNIX_FD:
832 return marshal_4_octets (str, insert_at, vp->u32,
833 byte_order, pos_after);
835 case DBUS_TYPE_INT64:
836 case DBUS_TYPE_UINT64:
837 case DBUS_TYPE_DOUBLE:
838 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
841 case DBUS_TYPE_STRING:
842 case DBUS_TYPE_OBJECT_PATH:
843 _dbus_assert (vp->str != NULL);
844 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
846 case DBUS_TYPE_SIGNATURE:
847 _dbus_assert (vp->str != NULL);
848 return marshal_signature (str, insert_at, vp->str, pos_after);
851 _dbus_assert_not_reached ("not a basic type");
858 marshal_1_octets_array (DBusString *str,
860 const unsigned char *value,
866 DBusString value_str;
868 _dbus_string_init_const_len (&value_str, (const char *) value, n_elements);
872 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
885 * Swaps the elements of an array to the opposite byte order
887 * @param data start of array
888 * @param n_elements number of elements
889 * @param alignment size of each element
892 _dbus_swap_array (unsigned char *data,
899 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
901 /* we use const_data and cast it off so DBusString can be a const string
902 * for the unit tests. don't ask.
905 end = d + (n_elements * alignment);
911 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
915 else if (alignment == 4)
919 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
925 _dbus_assert (alignment == 2);
929 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
936 swap_array (DBusString *str,
942 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
944 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
946 /* we use const_data and cast it off so DBusString can be a const string
947 * for the unit tests. don't ask.
949 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
950 n_elements, alignment);
955 marshal_fixed_multi (DBusString *str,
957 const DBusBasicValue *value,
968 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
970 old_string_len = _dbus_string_get_length (str);
972 len_in_bytes = n_elements * alignment;
973 array_start = insert_at;
975 /* Note that we do alignment padding unconditionally
976 * even if the array is empty; this means that
977 * padding + len is always equal to the number of bytes
981 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
984 _dbus_string_init_const_len (&t,
985 (const char *) value,
988 if (!_dbus_string_copy (&t, 0,
992 swap_array (str, array_start, n_elements, byte_order, alignment);
995 *pos_after = array_start + len_in_bytes;
1000 _dbus_string_delete (str, insert_at,
1001 _dbus_string_get_length (str) - old_string_len);
1007 * Marshals a block of values of fixed-length type all at once, as an
1008 * optimization. dbus_type_is_fixed() returns #TRUE for fixed-length
1009 * types, which are the basic types minus the string-like types.
1011 * The value argument should be the adddress of an
1012 * array, so e.g. "const dbus_uint32_t**"
1014 * @param str string to marshal to
1015 * @param insert_at where to insert the value
1016 * @param element_type type of array elements
1017 * @param value address of an array to marshal
1018 * @param n_elements number of elements in the array
1019 * @param byte_order byte order
1020 * @param pos_after #NULL or the position after the type
1021 * @returns #TRUE on success
1024 _dbus_marshal_write_fixed_multi (DBusString *str,
1032 const void* vp = *(const DBusBasicValue**)value;
1034 _dbus_assert (dbus_type_is_fixed (element_type));
1035 _dbus_assert (n_elements >= 0);
1038 _dbus_verbose ("writing %d elements of %s\n",
1039 n_elements, _dbus_type_to_string (element_type));
1042 switch (element_type)
1044 case DBUS_TYPE_BYTE:
1045 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1047 case DBUS_TYPE_INT16:
1048 case DBUS_TYPE_UINT16:
1049 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1050 case DBUS_TYPE_BOOLEAN:
1051 case DBUS_TYPE_INT32:
1052 case DBUS_TYPE_UINT32:
1053 case DBUS_TYPE_UNIX_FD:
1054 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1056 case DBUS_TYPE_INT64:
1057 case DBUS_TYPE_UINT64:
1058 case DBUS_TYPE_DOUBLE:
1059 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1063 _dbus_assert_not_reached ("non fixed type in array write");
1072 * Skips over a basic-typed value, reporting the following position.
1074 * @param str the string containing the data
1075 * @param type type of value to read
1076 * @param byte_order the byte order
1077 * @param pos pointer to position in the string,
1078 * updated on return to new position
1081 _dbus_marshal_skip_basic (const DBusString *str,
1086 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1087 byte_order == DBUS_BIG_ENDIAN);
1091 case DBUS_TYPE_BYTE:
1094 case DBUS_TYPE_INT16:
1095 case DBUS_TYPE_UINT16:
1096 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1099 case DBUS_TYPE_BOOLEAN:
1100 case DBUS_TYPE_INT32:
1101 case DBUS_TYPE_UINT32:
1102 case DBUS_TYPE_UNIX_FD:
1103 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1106 case DBUS_TYPE_INT64:
1107 case DBUS_TYPE_UINT64:
1108 case DBUS_TYPE_DOUBLE:
1109 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1112 case DBUS_TYPE_STRING:
1113 case DBUS_TYPE_OBJECT_PATH:
1117 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1119 *pos += len + 1; /* length plus nul */
1122 case DBUS_TYPE_SIGNATURE:
1126 len = _dbus_string_get_byte (str, *pos);
1128 *pos += len + 2; /* length byte plus length plus nul */
1132 _dbus_warn ("type %s not a basic type",
1133 _dbus_type_to_string (type));
1134 _dbus_assert_not_reached ("not a basic type");
1140 * Skips an array, returning the next position.
1142 * @param str the string containing the data
1143 * @param element_type the type of array elements
1144 * @param byte_order the byte order
1145 * @param pos pointer to position in the string,
1146 * updated on return to new position
1149 _dbus_marshal_skip_array (const DBusString *str,
1154 dbus_uint32_t array_len;
1158 i = _DBUS_ALIGN_VALUE (*pos, 4);
1160 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1162 alignment = _dbus_type_get_alignment (element_type);
1164 i = _DBUS_ALIGN_VALUE (i, alignment);
1166 *pos = i + array_len;
1170 * Gets the alignment requirement for the given type;
1171 * will be 1, 4, or 8.
1173 * @param typecode the type
1174 * @returns alignment of 1, 4, or 8
1177 _dbus_type_get_alignment (int typecode)
1181 case DBUS_TYPE_BYTE:
1182 case DBUS_TYPE_VARIANT:
1183 case DBUS_TYPE_SIGNATURE:
1185 case DBUS_TYPE_INT16:
1186 case DBUS_TYPE_UINT16:
1188 case DBUS_TYPE_BOOLEAN:
1189 case DBUS_TYPE_INT32:
1190 case DBUS_TYPE_UINT32:
1191 case DBUS_TYPE_UNIX_FD:
1192 /* this stuff is 4 since it starts with a length */
1193 case DBUS_TYPE_STRING:
1194 case DBUS_TYPE_OBJECT_PATH:
1195 case DBUS_TYPE_ARRAY:
1197 case DBUS_TYPE_INT64:
1198 case DBUS_TYPE_UINT64:
1199 case DBUS_TYPE_DOUBLE:
1200 /* struct is 8 since it could contain an 8-aligned item
1201 * and it's simpler to just always align structs to 8;
1202 * we want the amount of padding in a struct of a given
1203 * type to be predictable, not location-dependent.
1204 * DICT_ENTRY is always the same as struct.
1206 case DBUS_TYPE_STRUCT:
1207 case DBUS_TYPE_DICT_ENTRY:
1211 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1217 * Returns a string describing the given type.
1219 * @param typecode the type to describe
1220 * @returns a constant string describing the type
1223 _dbus_type_to_string (int typecode)
1227 case DBUS_TYPE_INVALID:
1229 case DBUS_TYPE_BOOLEAN:
1231 case DBUS_TYPE_BYTE:
1233 case DBUS_TYPE_INT16:
1235 case DBUS_TYPE_UINT16:
1237 case DBUS_TYPE_INT32:
1239 case DBUS_TYPE_UINT32:
1241 case DBUS_TYPE_INT64:
1243 case DBUS_TYPE_UINT64:
1245 case DBUS_TYPE_DOUBLE:
1247 case DBUS_TYPE_STRING:
1249 case DBUS_TYPE_OBJECT_PATH:
1250 return "object_path";
1251 case DBUS_TYPE_SIGNATURE:
1253 case DBUS_TYPE_STRUCT:
1255 case DBUS_TYPE_DICT_ENTRY:
1256 return "dict_entry";
1257 case DBUS_TYPE_ARRAY:
1259 case DBUS_TYPE_VARIANT:
1261 case DBUS_STRUCT_BEGIN_CHAR:
1262 return "begin_struct";
1263 case DBUS_STRUCT_END_CHAR:
1264 return "end_struct";
1265 case DBUS_DICT_ENTRY_BEGIN_CHAR:
1266 return "begin_dict_entry";
1267 case DBUS_DICT_ENTRY_END_CHAR:
1268 return "end_dict_entry";
1269 case DBUS_TYPE_UNIX_FD:
1277 * If in verbose mode, print a block of binary data.
1279 * @param data the data
1280 * @param len the length of the data
1281 * @param offset where to start counting for byte indexes
1284 _dbus_verbose_bytes (const unsigned char *data,
1289 const unsigned char *aligned;
1291 _dbus_assert (len >= 0);
1293 if (!_dbus_is_verbose())
1296 /* Print blanks on first row if appropriate */
1297 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1300 _dbus_assert (aligned <= data);
1302 if (aligned != data)
1304 _dbus_verbose ("%4ld\t%p: ", - (long)(data - aligned), aligned);
1305 while (aligned != data)
1307 _dbus_verbose (" ");
1312 /* now print the bytes */
1316 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1318 _dbus_verbose ("%4d\t%p: ",
1319 offset + i, &data[i]);
1322 if (data[i] >= 32 &&
1324 _dbus_verbose (" '%c' ", data[i]);
1326 _dbus_verbose ("0x%s%x ",
1327 data[i] <= 0xf ? "0" : "", data[i]);
1331 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1334 _dbus_verbose ("BE: %d LE: %d",
1335 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1336 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1339 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1341 _dbus_verbose (" u64: 0x%" PRIx64,
1342 *(dbus_uint64_t*)&data[i-8]);
1343 _dbus_verbose (" dbl: %g",
1344 *(double*)&data[i-8]);
1347 _dbus_verbose ("\n");
1351 _dbus_verbose ("\n");
1355 * Dump the given part of the string to verbose log.
1357 * @param str the string
1358 * @param start the start of range to dump
1359 * @param len length of range
1362 _dbus_verbose_bytes_of_string (const DBusString *str,
1369 real_len = _dbus_string_get_length (str);
1371 _dbus_assert (start >= 0);
1373 if (start > real_len)
1375 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1376 start, len, real_len);
1380 if ((start + len) > real_len)
1382 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1383 start, len, real_len);
1384 len = real_len - start;
1387 d = _dbus_string_get_const_data_len (str, start, len);
1389 _dbus_verbose_bytes ((const unsigned char *) d, len, start);
1393 map_type_char_to_type (int t)
1395 if (t == DBUS_STRUCT_BEGIN_CHAR)
1396 return DBUS_TYPE_STRUCT;
1397 else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1398 return DBUS_TYPE_DICT_ENTRY;
1401 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1402 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1408 * Get the first type in the signature. The difference between this
1409 * and just getting the first byte of the signature is that you won't
1410 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1413 * @param str string containing signature
1414 * @param pos where the signature starts
1415 * @returns the first type in the signature
1418 _dbus_first_type_in_signature (const DBusString *str,
1421 return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1425 * Similar to #_dbus_first_type_in_signature, but operates
1426 * on a C string buffer.
1428 * @param str a C string buffer
1429 * @param pos where the signature starts
1430 * @returns the first type in the signature
1433 _dbus_first_type_in_signature_c_str (const char *str,
1436 return map_type_char_to_type (str[pos]);
1441 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1442 #include "dbus-test.h"
1446 * Reads a block of fixed-length basic values, as an optimization
1447 * vs. reading each one individually into a new buffer.
1449 * This function returns the data in-place; it does not make a copy,
1450 * and it does not swap the bytes.
1452 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
1453 * and the "value" argument should be a "const double**" and so on.
1455 * @param str the string to read from
1456 * @param pos position to read from
1457 * @param element_type type of array elements
1458 * @param value place to return the array
1459 * @param n_elements number of array elements to read
1460 * @param byte_order the byte order, used to read the array length
1461 * @param new_pos #NULL or location to store a position after the elements
1464 _dbus_marshal_read_fixed_multi (const DBusString *str,
1475 _dbus_assert (dbus_type_is_fixed (element_type));
1476 _dbus_assert (dbus_type_is_basic (element_type));
1479 _dbus_verbose ("reading %d elements of %s\n",
1480 n_elements, _dbus_type_to_string (element_type));
1483 alignment = _dbus_type_get_alignment (element_type);
1485 pos = _DBUS_ALIGN_VALUE (pos, alignment);
1487 array_len = n_elements * alignment;
1489 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
1491 *new_pos = pos + array_len;
1495 swap_test_array (void *array,
1505 _dbus_string_init_const_len (&t, array, len_bytes);
1506 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1509 #define MARSHAL_BASIC(typename, byte_order, literal) \
1511 v_##typename = literal; \
1512 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1514 byte_order, NULL)) \
1515 _dbus_assert_not_reached ("no memory"); \
1518 #define DEMARSHAL_BASIC(typename, byte_order) \
1520 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1521 byte_order, &pos); \
1524 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1526 DEMARSHAL_BASIC (typename, byte_order); \
1527 if (literal != v_##typename) \
1529 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1530 _dbus_string_get_length (&str) - dump_pos); \
1531 _dbus_assert_not_reached ("demarshaled wrong value"); \
1535 #define MARSHAL_TEST(typename, byte_order, literal) \
1537 MARSHAL_BASIC (typename, byte_order, literal); \
1539 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1542 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1544 MARSHAL_BASIC (typename, byte_order, literal); \
1546 DEMARSHAL_BASIC (typename, byte_order); \
1547 if (strcmp (literal, v_##typename) != 0) \
1549 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1550 _dbus_string_get_length (&str) - dump_pos); \
1551 _dbus_warn ("literal '%s'\nvalue '%s'", literal, v_##typename); \
1552 _dbus_assert_not_reached ("demarshaled wrong value"); \
1556 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1559 v_UINT32 = sizeof(literal); \
1560 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1561 byte_order, &next)) \
1562 _dbus_assert_not_reached ("no memory"); \
1563 v_ARRAY_##typename = literal; \
1564 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1565 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1566 byte_order, NULL)) \
1567 _dbus_assert_not_reached ("no memory"); \
1570 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1573 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1574 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1575 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1576 v_UINT32/alignment, \
1577 byte_order, NULL); \
1578 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1579 byte_order, alignment); \
1582 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1584 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1585 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal)) != 0) \
1587 _dbus_verbose ("MARSHALED DATA\n"); \
1588 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1589 _dbus_string_get_length (&str) - dump_pos); \
1590 _dbus_verbose ("LITERAL DATA\n"); \
1591 _dbus_verbose_bytes ((const unsigned char *) literal, sizeof (literal), 0); \
1592 _dbus_verbose ("READ DATA\n"); \
1593 _dbus_verbose_bytes ((const unsigned char *) v_ARRAY_##typename, sizeof (literal), 0); \
1594 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1598 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1600 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1602 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1606 _dbus_marshal_test (void)
1611 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1612 dbus_int16_t array2[3] = { 124, 457, 780 };
1613 dbus_uint16_t array2u[3] = { 124, 457, 780 };
1614 dbus_int32_t array4[3] = { 123, 456, 789 };
1615 dbus_uint32_t array4u[3] = { 123, 456, 789 };
1616 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1617 DBUS_INT64_CONSTANT (0x456ffffffff),
1618 DBUS_INT64_CONSTANT (0x789ffffffff) };
1619 dbus_int64_t *v_ARRAY_INT64;
1620 unsigned char *v_ARRAY_BYTE;
1621 dbus_int16_t *v_ARRAY_INT16;
1622 dbus_uint16_t *v_ARRAY_UINT16;
1623 dbus_int32_t *v_ARRAY_INT32;
1624 dbus_uint32_t *v_ARRAY_UINT32;
1628 dbus_int16_t v_INT16;
1629 dbus_uint16_t v_UINT16;
1630 dbus_int32_t v_INT32;
1631 dbus_uint32_t v_UINT32;
1632 dbus_int64_t v_INT64;
1633 dbus_uint64_t v_UINT64;
1634 unsigned char v_BYTE;
1635 dbus_bool_t v_BOOLEAN;
1636 const char *v_STRING;
1637 const char *v_SIGNATURE;
1638 const char *v_OBJECT_PATH;
1641 if (!_dbus_string_init (&str))
1642 _dbus_assert_not_reached ("failed to init string");
1646 /* Marshal doubles */
1647 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1648 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1650 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1651 _dbus_assert_not_reached ("got wrong double value");
1653 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1654 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1656 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1657 _dbus_assert_not_reached ("got wrong double value");
1659 /* Marshal signed 16 integers */
1660 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1661 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1663 /* Marshal unsigned 16 integers */
1664 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1665 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1667 /* Marshal signed integers */
1668 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1669 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1671 /* Marshal unsigned integers */
1672 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1673 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1675 /* Marshal signed integers */
1676 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1677 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1679 /* Marshal unsigned integers */
1680 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1681 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1684 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1685 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1687 /* Marshal all possible bools! */
1688 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1689 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1690 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1691 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1693 /* Marshal strings */
1694 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1695 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1696 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1697 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1700 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1701 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1704 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1705 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1706 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1707 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1710 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1711 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1712 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2u);
1713 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2u);
1715 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1716 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1717 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4u);
1718 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4u);
1720 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1721 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1723 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1724 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1729 * FIXME restore the set/pack tests
1732 /* set/pack 64-bit integers */
1733 _dbus_string_set_length (&str, 8);
1736 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1737 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1739 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1740 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1741 _dbus_string_get_const_data (&str)));
1744 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1745 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1747 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1748 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1749 _dbus_string_get_const_data (&str)));
1751 /* signed little pack */
1752 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1754 _dbus_string_get_data (&str));
1756 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1757 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1758 _dbus_string_get_const_data (&str)));
1760 /* signed big pack */
1761 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1763 _dbus_string_get_data (&str));
1765 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1766 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1767 _dbus_string_get_const_data (&str)));
1769 /* unsigned little */
1770 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1771 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1773 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1774 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1775 _dbus_string_get_const_data (&str)));
1778 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1779 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1781 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1782 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1783 _dbus_string_get_const_data (&str)));
1785 /* unsigned little pack */
1786 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1788 _dbus_string_get_data (&str));
1790 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1791 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1792 _dbus_string_get_const_data (&str)));
1794 /* unsigned big pack */
1795 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1797 _dbus_string_get_data (&str));
1799 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1800 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1801 _dbus_string_get_const_data (&str)));
1803 /* set/pack 32-bit integers */
1804 _dbus_string_set_length (&str, 4);
1807 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1810 _dbus_assert (-0x123456 ==
1811 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1812 _dbus_string_get_const_data (&str)));
1815 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1818 _dbus_assert (-0x123456 ==
1819 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1820 _dbus_string_get_const_data (&str)));
1822 /* signed little pack */
1823 _dbus_pack_int32 (-0x123456,
1825 _dbus_string_get_data (&str));
1827 _dbus_assert (-0x123456 ==
1828 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1829 _dbus_string_get_const_data (&str)));
1831 /* signed big pack */
1832 _dbus_pack_int32 (-0x123456,
1834 _dbus_string_get_data (&str));
1836 _dbus_assert (-0x123456 ==
1837 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1838 _dbus_string_get_const_data (&str)));
1840 /* unsigned little */
1841 _dbus_marshal_set_uint32 (&str,
1843 DBUS_LITTLE_ENDIAN);
1845 _dbus_assert (0x123456 ==
1846 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1847 _dbus_string_get_const_data (&str)));
1850 _dbus_marshal_set_uint32 (&str,
1854 _dbus_assert (0x123456 ==
1855 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1856 _dbus_string_get_const_data (&str)));
1858 /* unsigned little pack */
1859 _dbus_pack_uint32 (0x123456,
1861 _dbus_string_get_data (&str));
1863 _dbus_assert (0x123456 ==
1864 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1865 _dbus_string_get_const_data (&str)));
1867 /* unsigned big pack */
1868 _dbus_pack_uint32 (0x123456,
1870 _dbus_string_get_data (&str));
1872 _dbus_assert (0x123456 ==
1873 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1874 _dbus_string_get_const_data (&str)));
1876 #endif /* set/pack tests for integers */
1878 /* Strings in-place set */
1879 byte_order = DBUS_LITTLE_ENDIAN;
1883 _dbus_string_set_length (&str, 0);
1885 /* reset pos for the macros */
1888 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1890 /* Set it to something longer */
1891 _dbus_string_init_const (&t, "Hello world foo");
1893 v_STRING = _dbus_string_get_const_data (&t);
1894 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1895 &v_STRING, byte_order, NULL, NULL);
1897 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1898 &v_STRING, byte_order,
1900 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1902 /* Set it to something shorter */
1903 _dbus_string_init_const (&t, "Hello");
1905 v_STRING = _dbus_string_get_const_data (&t);
1906 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1907 &v_STRING, byte_order, NULL, NULL);
1908 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1909 &v_STRING, byte_order,
1911 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1913 /* Do the other byte order */
1914 if (byte_order == DBUS_LITTLE_ENDIAN)
1915 byte_order = DBUS_BIG_ENDIAN;
1921 _dbus_string_free (&str);
1926 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */