1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-basic.c Marshalling routines for basic (primitive) types
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal-basic.h"
27 #include "dbus-signature.h"
32 * @defgroup DBusMarshal marshaling and unmarshaling
33 * @ingroup DBusInternals
34 * @brief functions to marshal/unmarshal data from the wire
36 * Types and functions related to converting primitive data types from
37 * wire format to native machine format, and vice versa.
39 * A signature is just a string with multiple types one after the other.
40 * for example a type is "i" or "(ii)", a signature is "i(ii)"
41 * where i is int and (ii) is struct { int; int; }
47 pack_2_octets (dbus_uint16_t value,
51 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
53 if ((byte_order) == DBUS_LITTLE_ENDIAN)
54 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_LE (value);
56 *((dbus_uint16_t*)(data)) = DBUS_UINT16_TO_BE (value);
60 pack_4_octets (dbus_uint32_t value,
64 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
66 if ((byte_order) == DBUS_LITTLE_ENDIAN)
67 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
69 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
73 pack_8_octets (DBusBasicValue value,
77 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
79 #ifdef DBUS_HAVE_INT64
80 if ((byte_order) == DBUS_LITTLE_ENDIAN)
81 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
83 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
85 *(DBus8ByteStruct*)data = value.u64;
86 swap_8_octets ((DBusBasicValue*)data, byte_order);
91 * Packs a 32 bit unsigned integer into a data pointer.
93 * @param value the value
94 * @param byte_order the byte order to use
95 * @param data the data pointer
98 _dbus_pack_uint32 (dbus_uint32_t value,
102 pack_4_octets (value, byte_order, data);
105 #ifndef DBUS_HAVE_INT64
108 swap_bytes (unsigned char *data,
111 unsigned char *p1 = data;
112 unsigned char *p2 = data + len - 1;
116 unsigned char tmp = *p1;
124 #endif /* !DBUS_HAVE_INT64 */
127 swap_8_octets (DBusBasicValue *value,
130 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
132 #ifdef DBUS_HAVE_INT64
133 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
135 swap_bytes ((unsigned char *)value, 8);
141 static DBusBasicValue
142 unpack_8_octets (int byte_order,
143 const unsigned char *data)
147 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
148 _dbus_assert (sizeof (r) == 8);
150 #ifdef DBUS_HAVE_INT64
151 if (byte_order == DBUS_LITTLE_ENDIAN)
152 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
154 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
156 r.u64 = *(DBus8ByteStruct*)data;
157 swap_8_octets (&r, byte_order);
164 #ifndef _dbus_unpack_uint16
166 * Unpacks a 16 bit unsigned integer from a data pointer
168 * @param byte_order The byte order to use
169 * @param data the data pointer
170 * @returns the integer
173 _dbus_unpack_uint16 (int byte_order,
174 const unsigned char *data)
176 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 2) == data);
178 if (byte_order == DBUS_LITTLE_ENDIAN)
179 return DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)data);
181 return DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)data);
183 #endif /* _dbus_unpack_uint16 */
185 #ifndef _dbus_unpack_uint32
187 * Unpacks a 32 bit unsigned integer from a data pointer
189 * @param byte_order The byte order to use
190 * @param data the data pointer
191 * @returns the integer
194 _dbus_unpack_uint32 (int byte_order,
195 const unsigned char *data)
197 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
199 if (byte_order == DBUS_LITTLE_ENDIAN)
200 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
202 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
204 #endif /* _dbus_unpack_uint32 */
207 set_2_octets (DBusString *str,
214 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
215 byte_order == DBUS_BIG_ENDIAN);
217 data = _dbus_string_get_data_len (str, offset, 2);
219 pack_2_octets (value, byte_order, data);
223 set_4_octets (DBusString *str,
230 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
231 byte_order == DBUS_BIG_ENDIAN);
233 data = _dbus_string_get_data_len (str, offset, 4);
235 pack_4_octets (value, byte_order, data);
239 set_8_octets (DBusString *str,
241 DBusBasicValue value,
246 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
247 byte_order == DBUS_BIG_ENDIAN);
249 data = _dbus_string_get_data_len (str, offset, 8);
251 pack_8_octets (value, byte_order, data);
255 * Sets the 4 bytes at the given offset to a marshaled unsigned
256 * integer, replacing anything found there previously.
258 * @param str the string to write the marshalled int to
259 * @param pos the byte offset where int should be written
260 * @param value the value
261 * @param byte_order the byte order to use
265 _dbus_marshal_set_uint32 (DBusString *str,
270 set_4_octets (str, pos, value, byte_order);
274 * Sets the existing marshaled string at the given offset with
275 * a new marshaled string. The given offset must point to
276 * an existing string or the wrong length will be deleted
277 * and replaced with the new string.
279 * Note: no attempt is made by this function to re-align
280 * any data which has been already marshalled after this
281 * string. Use with caution.
283 * @param str the string to write the marshalled string to
284 * @param pos the position of the marshaled string length
285 * @param value the value
286 * @param byte_order the byte order to use
287 * @param old_end_pos place to store byte after the nul byte of the old value
288 * @param new_end_pos place to store byte after the nul byte of the new value
289 * @returns #TRUE on success, #FALSE if no memory
293 set_string (DBusString *str,
300 int old_len, new_len;
303 _dbus_string_init_const (&dstr, value);
305 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned) pos);
306 old_len = _dbus_unpack_uint32 (byte_order,
307 _dbus_string_get_const_data_len (str, pos, 4));
309 new_len = _dbus_string_get_length (&dstr);
311 if (!_dbus_string_replace_len (&dstr, 0, new_len,
312 str, pos + 4, old_len))
315 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
318 *old_end_pos = pos + 4 + old_len + 1;
320 *new_end_pos = pos + 4 + new_len + 1;
326 * Sets the existing marshaled signature at the given offset to a new
327 * marshaled signature. Same basic ideas as set_string().
329 * @param str the string to write the marshalled signature to
330 * @param pos the position of the marshaled signature length
331 * @param value the value
332 * @param byte_order the byte order to use
333 * @param old_end_pos place to store byte after the nul byte of the old value
334 * @param new_end_pos place to store byte after the nul byte of the new value
335 * @returns #TRUE on success, #FALSE if no memory
339 set_signature (DBusString *str,
346 int old_len, new_len;
349 _dbus_string_init_const (&dstr, value);
351 old_len = _dbus_string_get_byte (str, pos);
352 new_len = _dbus_string_get_length (&dstr);
354 if (!_dbus_string_replace_len (&dstr, 0, new_len,
355 str, pos + 1, old_len))
358 _dbus_string_set_byte (str, pos, new_len);
361 *old_end_pos = pos + 1 + old_len + 1;
363 *new_end_pos = pos + 1 + new_len + 1;
369 * Sets an existing basic type value to a new value.
370 * Arguments work the same way as _dbus_marshal_basic_type().
372 * @param str the string
373 * @param pos location of the current value
374 * @param type the type of the current and new values
375 * @param value the address of the new value
376 * @param byte_order byte order for marshaling
377 * @param old_end_pos location to store end position of the old value, or #NULL
378 * @param new_end_pos location to store end position of the new value, or #NULL
379 * @returns #FALSE if no memory
382 _dbus_marshal_set_basic (DBusString *str,
390 const DBusBasicValue *vp;
397 _dbus_string_set_byte (str, pos, vp->byt);
399 *old_end_pos = pos + 1;
401 *new_end_pos = pos + 1;
404 case DBUS_TYPE_INT16:
405 case DBUS_TYPE_UINT16:
406 pos = _DBUS_ALIGN_VALUE (pos, 2);
407 set_2_octets (str, pos, vp->u16, byte_order);
409 *old_end_pos = pos + 2;
411 *new_end_pos = pos + 2;
414 case DBUS_TYPE_BOOLEAN:
415 case DBUS_TYPE_INT32:
416 case DBUS_TYPE_UINT32:
417 pos = _DBUS_ALIGN_VALUE (pos, 4);
418 set_4_octets (str, pos, vp->u32, byte_order);
420 *old_end_pos = pos + 4;
422 *new_end_pos = pos + 4;
425 case DBUS_TYPE_INT64:
426 case DBUS_TYPE_UINT64:
427 case DBUS_TYPE_DOUBLE:
428 pos = _DBUS_ALIGN_VALUE (pos, 8);
429 set_8_octets (str, pos, *vp, byte_order);
431 *old_end_pos = pos + 8;
433 *new_end_pos = pos + 8;
436 case DBUS_TYPE_STRING:
437 case DBUS_TYPE_OBJECT_PATH:
438 pos = _DBUS_ALIGN_VALUE (pos, 4);
439 _dbus_assert (vp->str != NULL);
440 return set_string (str, pos, vp->str, byte_order,
441 old_end_pos, new_end_pos);
443 case DBUS_TYPE_SIGNATURE:
444 _dbus_assert (vp->str != NULL);
445 return set_signature (str, pos, vp->str, byte_order,
446 old_end_pos, new_end_pos);
449 _dbus_assert_not_reached ("not a basic type");
456 * Convenience function to demarshal a 32 bit unsigned integer.
458 * @param str the string containing the data
459 * @param byte_order the byte order
460 * @param pos the position in the string
461 * @param new_pos the new position of the string
462 * @returns the demarshaled integer.
465 _dbus_marshal_read_uint32 (const DBusString *str,
470 pos = _DBUS_ALIGN_VALUE (pos, 4);
475 _dbus_assert (pos + 4 <= _dbus_string_get_length (str));
477 return _dbus_unpack_uint32 (byte_order,
478 _dbus_string_get_const_data (str) + pos);
482 * Demarshals a basic-typed value. The "value" pointer is always
483 * the address of a variable of the basic type. So e.g.
484 * if the basic type is "double" then the pointer is
485 * a double*, and if it's "char*" then the pointer is
488 * A value of type #DBusBasicValue is guaranteed to be large enough to
489 * hold any of the types that may be returned, which is handy if you
490 * are trying to do things generically. For example you can pass
491 * a DBusBasicValue* in to this function, and then pass the same
492 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
493 * move a value from one place to another.
495 * @param str the string containing the data
496 * @param pos position in the string
497 * @param type type of value to demarshal
498 * @param value pointer to return value data
499 * @param byte_order the byte order
500 * @param new_pos pointer to update with new position, or #NULL
503 _dbus_marshal_read_basic (const DBusString *str,
510 const char *str_data;
513 _dbus_assert (dbus_type_is_basic (type));
515 str_data = _dbus_string_get_const_data (str);
521 vp->byt = _dbus_string_get_byte (str, pos);
524 case DBUS_TYPE_INT16:
525 case DBUS_TYPE_UINT16:
526 pos = _DBUS_ALIGN_VALUE (pos, 2);
527 vp->u16 = *(dbus_uint16_t *)(str_data + pos);
528 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
529 vp->u16 = DBUS_UINT16_SWAP_LE_BE (vp->u16);
532 case DBUS_TYPE_INT32:
533 case DBUS_TYPE_UINT32:
534 case DBUS_TYPE_BOOLEAN:
535 pos = _DBUS_ALIGN_VALUE (pos, 4);
536 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
537 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
538 vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
541 case DBUS_TYPE_INT64:
542 case DBUS_TYPE_UINT64:
543 case DBUS_TYPE_DOUBLE:
544 pos = _DBUS_ALIGN_VALUE (pos, 8);
545 #ifdef DBUS_HAVE_INT64
546 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
547 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
549 vp->u64 = *(dbus_uint64_t*)(str_data + pos);
551 vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
552 swap_8_octets (vp, byte_order);
556 case DBUS_TYPE_STRING:
557 case DBUS_TYPE_OBJECT_PATH:
561 len = _dbus_marshal_read_uint32 (str, pos, byte_order, &pos);
563 vp->str = (char*) str_data + pos;
565 pos += len + 1; /* length plus nul */
568 case DBUS_TYPE_SIGNATURE:
572 len = _dbus_string_get_byte (str, pos);
575 vp->str = (char*) str_data + pos;
577 pos += len + 1; /* length plus nul */
581 _dbus_warn ("type %s %d not a basic type\n",
582 _dbus_type_to_string (type), type);
583 _dbus_assert_not_reached ("not a basic type");
592 * Reads a block of fixed-length basic values, as an optimization
593 * vs. reading each one individually into a new buffer.
595 * This function returns the data in-place; it does not make a copy,
596 * and it does not swap the bytes.
598 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
599 * and the "value" argument should be a "const double**" and so on.
601 * @todo we aren't using this function (except in the test suite)
603 * @param str the string to read from
604 * @param pos position to read from
605 * @param element_type type of array elements
606 * @param value place to return the array
607 * @param n_elements number of array elements to read
608 * @param byte_order the byte order, used to read the array length
609 * @param new_pos #NULL or location to store a position after the elements
612 _dbus_marshal_read_fixed_multi (const DBusString *str,
623 _dbus_assert (_dbus_type_is_fixed (element_type));
624 _dbus_assert (dbus_type_is_basic (element_type));
627 _dbus_verbose ("reading %d elements of %s\n",
628 n_elements, _dbus_type_to_string (element_type));
631 alignment = _dbus_type_get_alignment (element_type);
633 pos = _DBUS_ALIGN_VALUE (pos, alignment);
635 array_len = n_elements * alignment;
637 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
639 *new_pos = pos + array_len;
643 marshal_2_octets (DBusString *str,
652 _dbus_assert (sizeof (value) == 2);
654 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
655 value = DBUS_UINT16_SWAP_LE_BE (value);
657 orig_len = _dbus_string_get_length (str);
659 retval = _dbus_string_insert_2_aligned (str, insert_at,
660 (const unsigned char *)&value);
664 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
665 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
672 marshal_4_octets (DBusString *str,
681 _dbus_assert (sizeof (value) == 4);
683 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
684 value = DBUS_UINT32_SWAP_LE_BE (value);
686 orig_len = _dbus_string_get_length (str);
688 retval = _dbus_string_insert_4_aligned (str, insert_at,
689 (const unsigned char *)&value);
693 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
694 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
701 marshal_8_octets (DBusString *str,
703 DBusBasicValue value,
710 _dbus_assert (sizeof (value) == 8);
712 swap_8_octets (&value, byte_order);
714 orig_len = _dbus_string_get_length (str);
716 retval = _dbus_string_insert_8_aligned (str, insert_at,
717 (const unsigned char *)&value);
720 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
728 MARSHAL_AS_SIGNATURE,
729 MARSHAL_AS_BYTE_ARRAY
733 marshal_len_followed_by_bytes (int marshal_as,
736 const unsigned char *value,
737 int data_len, /* doesn't include nul if any */
742 DBusString value_str;
745 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
746 if (insert_at > _dbus_string_get_length (str))
747 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
748 insert_at, _dbus_string_get_length (str), data_len);
750 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
751 value_len = data_len;
753 value_len = data_len + 1; /* value has a nul */
755 _dbus_string_init_const_len (&value_str, value, value_len);
759 if (marshal_as == MARSHAL_AS_SIGNATURE)
761 _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
762 _dbus_assert (data_len <= 255); /* same as max sig len right now */
764 if (!_dbus_string_insert_byte (str, pos, data_len))
771 if (!marshal_4_octets (str, pos, data_len,
776 if (!_dbus_string_copy_len (&value_str, 0, value_len,
782 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
784 _dbus_verbose_bytes_of_string (str, pos, value_len);
795 /* Delete what we've inserted */
796 _dbus_string_delete (str, insert_at, pos - insert_at);
802 marshal_string (DBusString *str,
808 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
809 str, insert_at, value,
811 byte_order, pos_after);
815 marshal_signature (DBusString *str,
820 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
821 str, insert_at, value,
823 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
828 * Marshals a basic-typed value. The "value" pointer is always the
829 * address of a variable containing the basic type value.
830 * So for example for int32 it will be dbus_int32_t*, and
831 * for string it will be const char**. This is for symmetry
832 * with _dbus_marshal_read_basic() and to have a simple
835 * @param str string to marshal to
836 * @param insert_at where to insert the value
837 * @param type type of value
838 * @param value pointer to a variable containing the value
839 * @param byte_order byte order
840 * @param pos_after #NULL or the position after the type
841 * @returns #TRUE on success
844 _dbus_marshal_write_basic (DBusString *str,
851 const DBusBasicValue *vp;
853 _dbus_assert (dbus_type_is_basic (type));
860 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
863 *pos_after = insert_at + 1;
866 case DBUS_TYPE_INT16:
867 case DBUS_TYPE_UINT16:
868 return marshal_2_octets (str, insert_at, vp->u16,
869 byte_order, pos_after);
871 case DBUS_TYPE_BOOLEAN:
872 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
873 byte_order, pos_after);
875 case DBUS_TYPE_INT32:
876 case DBUS_TYPE_UINT32:
877 return marshal_4_octets (str, insert_at, vp->u32,
878 byte_order, pos_after);
880 case DBUS_TYPE_INT64:
881 case DBUS_TYPE_UINT64:
882 case DBUS_TYPE_DOUBLE:
883 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
886 case DBUS_TYPE_STRING:
887 case DBUS_TYPE_OBJECT_PATH:
888 _dbus_assert (vp->str != NULL);
889 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
891 case DBUS_TYPE_SIGNATURE:
892 _dbus_assert (vp->str != NULL);
893 return marshal_signature (str, insert_at, vp->str, pos_after);
896 _dbus_assert_not_reached ("not a basic type");
903 marshal_1_octets_array (DBusString *str,
905 const unsigned char *value,
911 DBusString value_str;
913 _dbus_string_init_const_len (&value_str, value, n_elements);
917 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
930 * Swaps the elements of an array to the opposite byte order
932 * @param data start of array
933 * @param n_elements number of elements
934 * @param alignment size of each element
937 _dbus_swap_array (unsigned char *data,
944 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
946 /* we use const_data and cast it off so DBusString can be a const string
947 * for the unit tests. don't ask.
950 end = d + (n_elements * alignment);
956 #ifdef DBUS_HAVE_INT64
957 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
959 swap_8_bytes ((DBusBasicValue*) d);
964 else if (alignment == 4)
968 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
974 _dbus_assert (alignment == 2);
978 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
985 swap_array (DBusString *str,
991 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
993 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
995 /* we use const_data and cast it off so DBusString can be a const string
996 * for the unit tests. don't ask.
998 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
999 n_elements, alignment);
1004 marshal_fixed_multi (DBusString *str,
1006 const DBusBasicValue *value,
1017 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
1019 old_string_len = _dbus_string_get_length (str);
1021 len_in_bytes = n_elements * alignment;
1022 array_start = insert_at;
1024 /* Note that we do alignment padding unconditionally
1025 * even if the array is empty; this means that
1026 * padding + len is always equal to the number of bytes
1030 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1033 _dbus_string_init_const_len (&t,
1034 (const unsigned char*) value,
1037 if (!_dbus_string_copy (&t, 0,
1041 swap_array (str, array_start, n_elements, byte_order, alignment);
1044 *pos_after = array_start + len_in_bytes;
1049 _dbus_string_delete (str, insert_at,
1050 _dbus_string_get_length (str) - old_string_len);
1056 * Marshals a block of values of fixed-length type all at once, as an
1057 * optimization. _dbus_type_is_fixed() returns #TRUE for fixed-length
1058 * types, which are the basic types minus the string-like types.
1060 * The value argument should be the adddress of an
1061 * array, so e.g. "const dbus_uint32_t**"
1063 * @param str string to marshal to
1064 * @param insert_at where to insert the value
1065 * @param element_type type of array elements
1066 * @param value address of an array to marshal
1067 * @param n_elements number of elements in the array
1068 * @param byte_order byte order
1069 * @param pos_after #NULL or the position after the type
1070 * @returns #TRUE on success
1073 _dbus_marshal_write_fixed_multi (DBusString *str,
1081 const void* vp = *(const DBusBasicValue**)value;
1083 _dbus_assert (_dbus_type_is_fixed (element_type));
1084 _dbus_assert (n_elements >= 0);
1087 _dbus_verbose ("writing %d elements of %s\n",
1088 n_elements, _dbus_type_to_string (element_type));
1091 switch (element_type)
1093 case DBUS_TYPE_BYTE:
1094 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1096 case DBUS_TYPE_INT16:
1097 case DBUS_TYPE_UINT16:
1098 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1099 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
1100 * should we here too ? */
1101 case DBUS_TYPE_BOOLEAN:
1102 case DBUS_TYPE_INT32:
1103 case DBUS_TYPE_UINT32:
1104 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1106 case DBUS_TYPE_INT64:
1107 case DBUS_TYPE_UINT64:
1108 case DBUS_TYPE_DOUBLE:
1109 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1113 _dbus_assert_not_reached ("non fixed type in array write");
1122 * Skips over a basic-typed value, reporting the following position.
1124 * @param str the string containing the data
1125 * @param type type of value to read
1126 * @param byte_order the byte order
1127 * @param pos pointer to position in the string,
1128 * updated on return to new position
1131 _dbus_marshal_skip_basic (const DBusString *str,
1136 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1137 byte_order == DBUS_BIG_ENDIAN);
1141 case DBUS_TYPE_BYTE:
1144 case DBUS_TYPE_INT16:
1145 case DBUS_TYPE_UINT16:
1146 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1149 case DBUS_TYPE_BOOLEAN:
1150 case DBUS_TYPE_INT32:
1151 case DBUS_TYPE_UINT32:
1152 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1155 case DBUS_TYPE_INT64:
1156 case DBUS_TYPE_UINT64:
1157 case DBUS_TYPE_DOUBLE:
1158 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1161 case DBUS_TYPE_STRING:
1162 case DBUS_TYPE_OBJECT_PATH:
1166 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1168 *pos += len + 1; /* length plus nul */
1171 case DBUS_TYPE_SIGNATURE:
1175 len = _dbus_string_get_byte (str, *pos);
1177 *pos += len + 2; /* length byte plus length plus nul */
1181 _dbus_warn ("type %s not a basic type\n",
1182 _dbus_type_to_string (type));
1183 _dbus_assert_not_reached ("not a basic type");
1189 * Skips an array, returning the next position.
1191 * @param str the string containing the data
1192 * @param element_type the type of array elements
1193 * @param byte_order the byte order
1194 * @param pos pointer to position in the string,
1195 * updated on return to new position
1198 _dbus_marshal_skip_array (const DBusString *str,
1203 dbus_uint32_t array_len;
1207 i = _DBUS_ALIGN_VALUE (*pos, 4);
1209 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1211 alignment = _dbus_type_get_alignment (element_type);
1213 i = _DBUS_ALIGN_VALUE (i, alignment);
1215 *pos = i + array_len;
1219 * Gets the alignment requirement for the given type;
1220 * will be 1, 4, or 8.
1222 * @param typecode the type
1223 * @returns alignment of 1, 4, or 8
1226 _dbus_type_get_alignment (int typecode)
1230 case DBUS_TYPE_BYTE:
1231 case DBUS_TYPE_VARIANT:
1232 case DBUS_TYPE_SIGNATURE:
1234 case DBUS_TYPE_INT16:
1235 case DBUS_TYPE_UINT16:
1237 case DBUS_TYPE_BOOLEAN:
1238 case DBUS_TYPE_INT32:
1239 case DBUS_TYPE_UINT32:
1240 /* this stuff is 4 since it starts with a length */
1241 case DBUS_TYPE_STRING:
1242 case DBUS_TYPE_OBJECT_PATH:
1243 case DBUS_TYPE_ARRAY:
1245 case DBUS_TYPE_INT64:
1246 case DBUS_TYPE_UINT64:
1247 case DBUS_TYPE_DOUBLE:
1248 /* struct is 8 since it could contain an 8-aligned item
1249 * and it's simpler to just always align structs to 8;
1250 * we want the amount of padding in a struct of a given
1251 * type to be predictable, not location-dependent.
1252 * DICT_ENTRY is always the same as struct.
1254 case DBUS_TYPE_STRUCT:
1255 case DBUS_TYPE_DICT_ENTRY:
1259 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1266 * Return #TRUE if the typecode is a valid typecode.
1267 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1268 * random unknown bytes aren't either. This function is safe with
1271 * @returns #TRUE if valid
1274 _dbus_type_is_valid (int typecode)
1278 case DBUS_TYPE_BYTE:
1279 case DBUS_TYPE_BOOLEAN:
1280 case DBUS_TYPE_INT16:
1281 case DBUS_TYPE_UINT16:
1282 case DBUS_TYPE_INT32:
1283 case DBUS_TYPE_UINT32:
1284 case DBUS_TYPE_INT64:
1285 case DBUS_TYPE_UINT64:
1286 case DBUS_TYPE_DOUBLE:
1287 case DBUS_TYPE_STRING:
1288 case DBUS_TYPE_OBJECT_PATH:
1289 case DBUS_TYPE_SIGNATURE:
1290 case DBUS_TYPE_ARRAY:
1291 case DBUS_TYPE_STRUCT:
1292 case DBUS_TYPE_DICT_ENTRY:
1293 case DBUS_TYPE_VARIANT:
1302 * Tells you whether values of this type can change length if you set
1303 * them to some other value. For this purpose, you assume that the
1304 * first byte of the old and new value would be in the same location,
1305 * so alignment padding is not a factor.
1307 * @returns #FALSE if the type can occupy different lengths
1310 _dbus_type_is_fixed (int typecode)
1314 case DBUS_TYPE_BYTE:
1315 case DBUS_TYPE_BOOLEAN:
1316 case DBUS_TYPE_INT16:
1317 case DBUS_TYPE_UINT16:
1318 case DBUS_TYPE_INT32:
1319 case DBUS_TYPE_UINT32:
1320 case DBUS_TYPE_INT64:
1321 case DBUS_TYPE_UINT64:
1322 case DBUS_TYPE_DOUBLE:
1330 * Returns a string describing the given type.
1332 * @param typecode the type to describe
1333 * @returns a constant string describing the type
1336 _dbus_type_to_string (int typecode)
1340 case DBUS_TYPE_INVALID:
1342 case DBUS_TYPE_BOOLEAN:
1344 case DBUS_TYPE_BYTE:
1346 case DBUS_TYPE_INT16:
1348 case DBUS_TYPE_UINT16:
1350 case DBUS_TYPE_INT32:
1352 case DBUS_TYPE_UINT32:
1354 case DBUS_TYPE_DOUBLE:
1356 case DBUS_TYPE_STRING:
1358 case DBUS_TYPE_OBJECT_PATH:
1359 return "object_path";
1360 case DBUS_TYPE_SIGNATURE:
1362 case DBUS_TYPE_STRUCT:
1364 case DBUS_TYPE_DICT_ENTRY:
1365 return "dict_entry";
1366 case DBUS_TYPE_ARRAY:
1368 case DBUS_TYPE_VARIANT:
1370 case DBUS_STRUCT_BEGIN_CHAR:
1371 return "begin_struct";
1372 case DBUS_STRUCT_END_CHAR:
1373 return "end_struct";
1374 case DBUS_DICT_ENTRY_BEGIN_CHAR:
1375 return "begin_dict_entry";
1376 case DBUS_DICT_ENTRY_END_CHAR:
1377 return "end_dict_entry";
1384 * If in verbose mode, print a block of binary data.
1386 * @todo right now it prints even if not in verbose mode
1388 * @param data the data
1389 * @param len the length of the data
1390 * @param offset where to start counting for byte indexes
1393 _dbus_verbose_bytes (const unsigned char *data,
1398 const unsigned char *aligned;
1400 _dbus_assert (len >= 0);
1402 /* Print blanks on first row if appropriate */
1403 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1406 _dbus_assert (aligned <= data);
1408 if (aligned != data)
1410 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1411 while (aligned != data)
1413 _dbus_verbose (" ");
1418 /* now print the bytes */
1422 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1424 _dbus_verbose ("%4d\t%p: ",
1425 offset + i, &data[i]);
1428 if (data[i] >= 32 &&
1430 _dbus_verbose (" '%c' ", data[i]);
1432 _dbus_verbose ("0x%s%x ",
1433 data[i] <= 0xf ? "0" : "", data[i]);
1437 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1440 _dbus_verbose ("BE: %d LE: %d",
1441 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1442 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1445 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1447 #ifdef DBUS_HAVE_INT64
1448 /* I think I probably mean "GNU libc printf" and not "GNUC"
1449 * but we'll wait until someone complains. If you hit this,
1450 * just turn off verbose mode as a workaround.
1453 _dbus_verbose (" u64: 0x%llx",
1454 *(dbus_uint64_t*)&data[i-8]);
1457 _dbus_verbose (" dbl: %g",
1458 *(double*)&data[i-8]);
1461 _dbus_verbose ("\n");
1465 _dbus_verbose ("\n");
1469 * Dump the given part of the string to verbose log.
1471 * @param str the string
1472 * @param start the start of range to dump
1473 * @param len length of range
1476 _dbus_verbose_bytes_of_string (const DBusString *str,
1483 real_len = _dbus_string_get_length (str);
1485 _dbus_assert (start >= 0);
1487 if (start > real_len)
1489 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1490 start, len, real_len);
1494 if ((start + len) > real_len)
1496 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1497 start, len, real_len);
1498 len = real_len - start;
1501 d = _dbus_string_get_const_data_len (str, start, len);
1503 _dbus_verbose_bytes (d, len, start);
1507 map_type_char_to_type (int t)
1509 if (t == DBUS_STRUCT_BEGIN_CHAR)
1510 return DBUS_TYPE_STRUCT;
1511 else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1512 return DBUS_TYPE_DICT_ENTRY;
1515 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1516 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1522 * Get the first type in the signature. The difference between this
1523 * and just getting the first byte of the signature is that you won't
1524 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1527 * @param str string containing signature
1528 * @param pos where the signature starts
1529 * @returns the first type in the signature
1532 _dbus_first_type_in_signature (const DBusString *str,
1535 return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1539 * Similar to #_dbus_first_type_in_signature, but operates
1540 * on a C string buffer.
1542 * @param str a C string buffer
1543 * @param pos where the signature starts
1544 * @returns the first type in the signature
1547 _dbus_first_type_in_signature_c_str (const char *str,
1550 return map_type_char_to_type (str[pos]);
1555 #ifdef DBUS_BUILD_TESTS
1556 #include "dbus-test.h"
1560 swap_test_array (void *array,
1570 _dbus_string_init_const_len (&t, array, len_bytes);
1571 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1574 #define MARSHAL_BASIC(typename, byte_order, literal) \
1576 v_##typename = literal; \
1577 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1579 byte_order, NULL)) \
1580 _dbus_assert_not_reached ("no memory"); \
1583 #define DEMARSHAL_BASIC(typename, byte_order) \
1585 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1586 byte_order, &pos); \
1589 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1591 DEMARSHAL_BASIC (typename, byte_order); \
1592 if (literal != v_##typename) \
1594 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1595 _dbus_string_get_length (&str) - dump_pos); \
1596 _dbus_assert_not_reached ("demarshaled wrong value"); \
1600 #define MARSHAL_TEST(typename, byte_order, literal) \
1602 MARSHAL_BASIC (typename, byte_order, literal); \
1604 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1607 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1609 MARSHAL_BASIC (typename, byte_order, literal); \
1611 DEMARSHAL_BASIC (typename, byte_order); \
1612 if (strcmp (literal, v_##typename) != 0) \
1614 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1615 _dbus_string_get_length (&str) - dump_pos); \
1616 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1617 _dbus_assert_not_reached ("demarshaled wrong value"); \
1621 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1624 v_UINT32 = sizeof(literal); \
1625 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1626 byte_order, &next)) \
1627 _dbus_assert_not_reached ("no memory"); \
1628 v_ARRAY_##typename = literal; \
1629 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1630 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1631 byte_order, NULL)) \
1632 _dbus_assert_not_reached ("no memory"); \
1635 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1638 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1639 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1640 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1641 v_UINT32/alignment, \
1642 byte_order, NULL); \
1643 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1644 byte_order, alignment); \
1647 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1649 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1650 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1652 _dbus_verbose ("MARSHALED DATA\n"); \
1653 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1654 _dbus_string_get_length (&str) - dump_pos); \
1655 _dbus_verbose ("LITERAL DATA\n"); \
1656 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1657 _dbus_verbose ("READ DATA\n"); \
1658 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1659 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1663 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1665 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1667 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1671 _dbus_marshal_test (void)
1676 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1677 dbus_int16_t array2[3] = { 124, 457, 780 };
1678 dbus_int32_t array4[3] = { 123, 456, 789 };
1679 #ifdef DBUS_HAVE_INT64
1680 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1681 DBUS_INT64_CONSTANT (0x456ffffffff),
1682 DBUS_INT64_CONSTANT (0x789ffffffff) };
1683 dbus_int64_t *v_ARRAY_INT64;
1685 unsigned char *v_ARRAY_BYTE;
1686 dbus_int16_t *v_ARRAY_INT16;
1687 dbus_uint16_t *v_ARRAY_UINT16;
1688 dbus_int32_t *v_ARRAY_INT32;
1689 dbus_uint32_t *v_ARRAY_UINT32;
1690 double *v_ARRAY_DOUBLE;
1694 dbus_int16_t v_INT16;
1695 dbus_uint16_t v_UINT16;
1696 dbus_int32_t v_INT32;
1697 dbus_uint32_t v_UINT32;
1698 dbus_int64_t v_INT64;
1699 dbus_uint64_t v_UINT64;
1700 unsigned char v_BYTE;
1701 dbus_bool_t v_BOOLEAN;
1702 const char *v_STRING;
1703 const char *v_SIGNATURE;
1704 const char *v_OBJECT_PATH;
1707 if (!_dbus_string_init (&str))
1708 _dbus_assert_not_reached ("failed to init string");
1712 /* Marshal doubles */
1713 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1714 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1716 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1717 _dbus_assert_not_reached ("got wrong double value");
1719 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1720 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1722 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1723 _dbus_assert_not_reached ("got wrong double value");
1725 /* Marshal signed 16 integers */
1726 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1727 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1729 /* Marshal unsigned 16 integers */
1730 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1731 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1733 /* Marshal signed integers */
1734 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1735 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1737 /* Marshal unsigned integers */
1738 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1739 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1741 #ifdef DBUS_HAVE_INT64
1742 /* Marshal signed integers */
1743 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1744 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1746 /* Marshal unsigned integers */
1747 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1748 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1749 #endif /* DBUS_HAVE_INT64 */
1752 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1753 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1755 /* Marshal all possible bools! */
1756 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1757 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1758 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1759 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1761 /* Marshal strings */
1762 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1763 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1764 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1765 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1768 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1769 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1772 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1773 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1774 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1775 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1778 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1779 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1780 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2);
1781 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2);
1783 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1784 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1785 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4);
1786 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4);
1788 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1789 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1791 #ifdef DBUS_HAVE_INT64
1792 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1793 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1799 * FIXME restore the set/pack tests
1802 #ifdef DBUS_HAVE_INT64
1803 /* set/pack 64-bit integers */
1804 _dbus_string_set_length (&str, 8);
1807 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1808 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1810 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1811 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1812 _dbus_string_get_const_data (&str)));
1815 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1816 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1818 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1819 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1820 _dbus_string_get_const_data (&str)));
1822 /* signed little pack */
1823 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1825 _dbus_string_get_data (&str));
1827 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1828 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1829 _dbus_string_get_const_data (&str)));
1831 /* signed big pack */
1832 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1834 _dbus_string_get_data (&str));
1836 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1837 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1838 _dbus_string_get_const_data (&str)));
1840 /* unsigned little */
1841 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1842 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1844 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1845 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1846 _dbus_string_get_const_data (&str)));
1849 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1850 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1852 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1853 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1854 _dbus_string_get_const_data (&str)));
1856 /* unsigned little pack */
1857 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1859 _dbus_string_get_data (&str));
1861 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1862 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1863 _dbus_string_get_const_data (&str)));
1865 /* unsigned big pack */
1866 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1868 _dbus_string_get_data (&str));
1870 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1871 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1872 _dbus_string_get_const_data (&str)));
1873 #endif /* DBUS_HAVE_INT64 */
1875 /* set/pack 32-bit integers */
1876 _dbus_string_set_length (&str, 4);
1879 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1882 _dbus_assert (-0x123456 ==
1883 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1884 _dbus_string_get_const_data (&str)));
1887 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1890 _dbus_assert (-0x123456 ==
1891 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1892 _dbus_string_get_const_data (&str)));
1894 /* signed little pack */
1895 _dbus_pack_int32 (-0x123456,
1897 _dbus_string_get_data (&str));
1899 _dbus_assert (-0x123456 ==
1900 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1901 _dbus_string_get_const_data (&str)));
1903 /* signed big pack */
1904 _dbus_pack_int32 (-0x123456,
1906 _dbus_string_get_data (&str));
1908 _dbus_assert (-0x123456 ==
1909 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1910 _dbus_string_get_const_data (&str)));
1912 /* unsigned little */
1913 _dbus_marshal_set_uint32 (&str,
1915 DBUS_LITTLE_ENDIAN);
1917 _dbus_assert (0x123456 ==
1918 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1919 _dbus_string_get_const_data (&str)));
1922 _dbus_marshal_set_uint32 (&str,
1926 _dbus_assert (0x123456 ==
1927 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1928 _dbus_string_get_const_data (&str)));
1930 /* unsigned little pack */
1931 _dbus_pack_uint32 (0x123456,
1933 _dbus_string_get_data (&str));
1935 _dbus_assert (0x123456 ==
1936 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1937 _dbus_string_get_const_data (&str)));
1939 /* unsigned big pack */
1940 _dbus_pack_uint32 (0x123456,
1942 _dbus_string_get_data (&str));
1944 _dbus_assert (0x123456 ==
1945 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1946 _dbus_string_get_const_data (&str)));
1948 #endif /* set/pack tests for integers */
1950 /* Strings in-place set */
1951 byte_order = DBUS_LITTLE_ENDIAN;
1955 _dbus_string_set_length (&str, 0);
1957 /* reset pos for the macros */
1960 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1962 /* Set it to something longer */
1963 _dbus_string_init_const (&t, "Hello world foo");
1965 v_STRING = _dbus_string_get_const_data (&t);
1966 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1967 &v_STRING, byte_order, NULL, NULL);
1969 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1970 &v_STRING, byte_order,
1972 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1974 /* Set it to something shorter */
1975 _dbus_string_init_const (&t, "Hello");
1977 v_STRING = _dbus_string_get_const_data (&t);
1978 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1979 &v_STRING, byte_order, NULL, NULL);
1980 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1981 &v_STRING, byte_order,
1983 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1985 /* Do the other byte order */
1986 if (byte_order == DBUS_LITTLE_ENDIAN)
1987 byte_order = DBUS_BIG_ENDIAN;
1993 _dbus_string_free (&str);
1998 #endif /* DBUS_BUILD_TESTS */