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 1.0 we aren't using this function (except in the test suite)
602 * add #ifdefs around it
604 * @param str the string to read from
605 * @param pos position to read from
606 * @param element_type type of array elements
607 * @param value place to return the array
608 * @param n_elements number of array elements to read
609 * @param byte_order the byte order, used to read the array length
610 * @param new_pos #NULL or location to store a position after the elements
613 _dbus_marshal_read_fixed_multi (const DBusString *str,
624 _dbus_assert (dbus_type_is_fixed (element_type));
625 _dbus_assert (dbus_type_is_basic (element_type));
628 _dbus_verbose ("reading %d elements of %s\n",
629 n_elements, _dbus_type_to_string (element_type));
632 alignment = _dbus_type_get_alignment (element_type);
634 pos = _DBUS_ALIGN_VALUE (pos, alignment);
636 array_len = n_elements * alignment;
638 *(const DBusBasicValue**) value = (void*) _dbus_string_get_const_data_len (str, pos, array_len);
640 *new_pos = pos + array_len;
644 marshal_2_octets (DBusString *str,
653 _dbus_assert (sizeof (value) == 2);
655 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
656 value = DBUS_UINT16_SWAP_LE_BE (value);
658 orig_len = _dbus_string_get_length (str);
660 retval = _dbus_string_insert_2_aligned (str, insert_at,
661 (const unsigned char *)&value);
665 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
666 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
673 marshal_4_octets (DBusString *str,
682 _dbus_assert (sizeof (value) == 4);
684 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
685 value = DBUS_UINT32_SWAP_LE_BE (value);
687 orig_len = _dbus_string_get_length (str);
689 retval = _dbus_string_insert_4_aligned (str, insert_at,
690 (const unsigned char *)&value);
694 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
695 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
702 marshal_8_octets (DBusString *str,
704 DBusBasicValue value,
711 _dbus_assert (sizeof (value) == 8);
713 swap_8_octets (&value, byte_order);
715 orig_len = _dbus_string_get_length (str);
717 retval = _dbus_string_insert_8_aligned (str, insert_at,
718 (const unsigned char *)&value);
721 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
729 MARSHAL_AS_SIGNATURE,
730 MARSHAL_AS_BYTE_ARRAY
734 marshal_len_followed_by_bytes (int marshal_as,
737 const unsigned char *value,
738 int data_len, /* doesn't include nul if any */
743 DBusString value_str;
746 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
747 if (insert_at > _dbus_string_get_length (str))
748 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
749 insert_at, _dbus_string_get_length (str), data_len);
751 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
752 value_len = data_len;
754 value_len = data_len + 1; /* value has a nul */
756 _dbus_string_init_const_len (&value_str, value, value_len);
760 if (marshal_as == MARSHAL_AS_SIGNATURE)
762 _dbus_assert (data_len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
763 _dbus_assert (data_len <= 255); /* same as max sig len right now */
765 if (!_dbus_string_insert_byte (str, pos, data_len))
772 if (!marshal_4_octets (str, pos, data_len,
777 if (!_dbus_string_copy_len (&value_str, 0, value_len,
783 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
785 _dbus_verbose_bytes_of_string (str, pos, value_len);
796 /* Delete what we've inserted */
797 _dbus_string_delete (str, insert_at, pos - insert_at);
803 marshal_string (DBusString *str,
809 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
810 str, insert_at, value,
812 byte_order, pos_after);
816 marshal_signature (DBusString *str,
821 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
822 str, insert_at, value,
824 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
829 * Marshals a basic-typed value. The "value" pointer is always the
830 * address of a variable containing the basic type value.
831 * So for example for int32 it will be dbus_int32_t*, and
832 * for string it will be const char**. This is for symmetry
833 * with _dbus_marshal_read_basic() and to have a simple
836 * @param str string to marshal to
837 * @param insert_at where to insert the value
838 * @param type type of value
839 * @param value pointer to a variable containing the value
840 * @param byte_order byte order
841 * @param pos_after #NULL or the position after the type
842 * @returns #TRUE on success
845 _dbus_marshal_write_basic (DBusString *str,
852 const DBusBasicValue *vp;
854 _dbus_assert (dbus_type_is_basic (type));
861 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
864 *pos_after = insert_at + 1;
867 case DBUS_TYPE_INT16:
868 case DBUS_TYPE_UINT16:
869 return marshal_2_octets (str, insert_at, vp->u16,
870 byte_order, pos_after);
872 case DBUS_TYPE_BOOLEAN:
873 return marshal_4_octets (str, insert_at, vp->u32 != FALSE,
874 byte_order, pos_after);
876 case DBUS_TYPE_INT32:
877 case DBUS_TYPE_UINT32:
878 return marshal_4_octets (str, insert_at, vp->u32,
879 byte_order, pos_after);
881 case DBUS_TYPE_INT64:
882 case DBUS_TYPE_UINT64:
883 case DBUS_TYPE_DOUBLE:
884 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
887 case DBUS_TYPE_STRING:
888 case DBUS_TYPE_OBJECT_PATH:
889 _dbus_assert (vp->str != NULL);
890 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
892 case DBUS_TYPE_SIGNATURE:
893 _dbus_assert (vp->str != NULL);
894 return marshal_signature (str, insert_at, vp->str, pos_after);
897 _dbus_assert_not_reached ("not a basic type");
904 marshal_1_octets_array (DBusString *str,
906 const unsigned char *value,
912 DBusString value_str;
914 _dbus_string_init_const_len (&value_str, value, n_elements);
918 if (!_dbus_string_copy_len (&value_str, 0, n_elements,
931 * Swaps the elements of an array to the opposite byte order
933 * @param data start of array
934 * @param n_elements number of elements
935 * @param alignment size of each element
938 _dbus_swap_array (unsigned char *data,
945 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data);
947 /* we use const_data and cast it off so DBusString can be a const string
948 * for the unit tests. don't ask.
951 end = d + (n_elements * alignment);
957 #ifdef DBUS_HAVE_INT64
958 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
960 swap_8_bytes ((DBusBasicValue*) d);
965 else if (alignment == 4)
969 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
975 _dbus_assert (alignment == 2);
979 *((dbus_uint16_t*)d) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)d));
986 swap_array (DBusString *str,
992 _dbus_assert (_DBUS_ALIGN_VALUE (array_start, alignment) == (unsigned) array_start);
994 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
996 /* we use const_data and cast it off so DBusString can be a const string
997 * for the unit tests. don't ask.
999 _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start),
1000 n_elements, alignment);
1005 marshal_fixed_multi (DBusString *str,
1007 const DBusBasicValue *value,
1018 _dbus_assert (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / alignment);
1020 old_string_len = _dbus_string_get_length (str);
1022 len_in_bytes = n_elements * alignment;
1023 array_start = insert_at;
1025 /* Note that we do alignment padding unconditionally
1026 * even if the array is empty; this means that
1027 * padding + len is always equal to the number of bytes
1031 if (!_dbus_string_insert_alignment (str, &array_start, alignment))
1034 _dbus_string_init_const_len (&t,
1035 (const unsigned char*) value,
1038 if (!_dbus_string_copy (&t, 0,
1042 swap_array (str, array_start, n_elements, byte_order, alignment);
1045 *pos_after = array_start + len_in_bytes;
1050 _dbus_string_delete (str, insert_at,
1051 _dbus_string_get_length (str) - old_string_len);
1057 * Marshals a block of values of fixed-length type all at once, as an
1058 * optimization. dbus_type_is_fixed() returns #TRUE for fixed-length
1059 * types, which are the basic types minus the string-like types.
1061 * The value argument should be the adddress of an
1062 * array, so e.g. "const dbus_uint32_t**"
1064 * @param str string to marshal to
1065 * @param insert_at where to insert the value
1066 * @param element_type type of array elements
1067 * @param value address of an array to marshal
1068 * @param n_elements number of elements in the array
1069 * @param byte_order byte order
1070 * @param pos_after #NULL or the position after the type
1071 * @returns #TRUE on success
1074 _dbus_marshal_write_fixed_multi (DBusString *str,
1082 const void* vp = *(const DBusBasicValue**)value;
1084 _dbus_assert (dbus_type_is_fixed (element_type));
1085 _dbus_assert (n_elements >= 0);
1088 _dbus_verbose ("writing %d elements of %s\n",
1089 n_elements, _dbus_type_to_string (element_type));
1092 switch (element_type)
1094 case DBUS_TYPE_BYTE:
1095 return marshal_1_octets_array (str, insert_at, vp, n_elements, byte_order, pos_after);
1097 case DBUS_TYPE_INT16:
1098 case DBUS_TYPE_UINT16:
1099 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 2, pos_after);
1100 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
1101 * should we here too ? */
1102 case DBUS_TYPE_BOOLEAN:
1103 case DBUS_TYPE_INT32:
1104 case DBUS_TYPE_UINT32:
1105 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 4, pos_after);
1107 case DBUS_TYPE_INT64:
1108 case DBUS_TYPE_UINT64:
1109 case DBUS_TYPE_DOUBLE:
1110 return marshal_fixed_multi (str, insert_at, vp, n_elements, byte_order, 8, pos_after);
1114 _dbus_assert_not_reached ("non fixed type in array write");
1123 * Skips over a basic-typed value, reporting the following position.
1125 * @param str the string containing the data
1126 * @param type type of value to read
1127 * @param byte_order the byte order
1128 * @param pos pointer to position in the string,
1129 * updated on return to new position
1132 _dbus_marshal_skip_basic (const DBusString *str,
1137 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
1138 byte_order == DBUS_BIG_ENDIAN);
1142 case DBUS_TYPE_BYTE:
1145 case DBUS_TYPE_INT16:
1146 case DBUS_TYPE_UINT16:
1147 *pos = _DBUS_ALIGN_VALUE (*pos, 2);
1150 case DBUS_TYPE_BOOLEAN:
1151 case DBUS_TYPE_INT32:
1152 case DBUS_TYPE_UINT32:
1153 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1156 case DBUS_TYPE_INT64:
1157 case DBUS_TYPE_UINT64:
1158 case DBUS_TYPE_DOUBLE:
1159 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1162 case DBUS_TYPE_STRING:
1163 case DBUS_TYPE_OBJECT_PATH:
1167 len = _dbus_marshal_read_uint32 (str, *pos, byte_order, pos);
1169 *pos += len + 1; /* length plus nul */
1172 case DBUS_TYPE_SIGNATURE:
1176 len = _dbus_string_get_byte (str, *pos);
1178 *pos += len + 2; /* length byte plus length plus nul */
1182 _dbus_warn ("type %s not a basic type\n",
1183 _dbus_type_to_string (type));
1184 _dbus_assert_not_reached ("not a basic type");
1190 * Skips an array, returning the next position.
1192 * @param str the string containing the data
1193 * @param element_type the type of array elements
1194 * @param byte_order the byte order
1195 * @param pos pointer to position in the string,
1196 * updated on return to new position
1199 _dbus_marshal_skip_array (const DBusString *str,
1204 dbus_uint32_t array_len;
1208 i = _DBUS_ALIGN_VALUE (*pos, 4);
1210 array_len = _dbus_marshal_read_uint32 (str, i, byte_order, &i);
1212 alignment = _dbus_type_get_alignment (element_type);
1214 i = _DBUS_ALIGN_VALUE (i, alignment);
1216 *pos = i + array_len;
1220 * Gets the alignment requirement for the given type;
1221 * will be 1, 4, or 8.
1223 * @param typecode the type
1224 * @returns alignment of 1, 4, or 8
1227 _dbus_type_get_alignment (int typecode)
1231 case DBUS_TYPE_BYTE:
1232 case DBUS_TYPE_VARIANT:
1233 case DBUS_TYPE_SIGNATURE:
1235 case DBUS_TYPE_INT16:
1236 case DBUS_TYPE_UINT16:
1238 case DBUS_TYPE_BOOLEAN:
1239 case DBUS_TYPE_INT32:
1240 case DBUS_TYPE_UINT32:
1241 /* this stuff is 4 since it starts with a length */
1242 case DBUS_TYPE_STRING:
1243 case DBUS_TYPE_OBJECT_PATH:
1244 case DBUS_TYPE_ARRAY:
1246 case DBUS_TYPE_INT64:
1247 case DBUS_TYPE_UINT64:
1248 case DBUS_TYPE_DOUBLE:
1249 /* struct is 8 since it could contain an 8-aligned item
1250 * and it's simpler to just always align structs to 8;
1251 * we want the amount of padding in a struct of a given
1252 * type to be predictable, not location-dependent.
1253 * DICT_ENTRY is always the same as struct.
1255 case DBUS_TYPE_STRUCT:
1256 case DBUS_TYPE_DICT_ENTRY:
1260 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1267 * Return #TRUE if the typecode is a valid typecode.
1268 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1269 * random unknown bytes aren't either. This function is safe with
1272 * @returns #TRUE if valid
1275 _dbus_type_is_valid (int typecode)
1279 case DBUS_TYPE_BYTE:
1280 case DBUS_TYPE_BOOLEAN:
1281 case DBUS_TYPE_INT16:
1282 case DBUS_TYPE_UINT16:
1283 case DBUS_TYPE_INT32:
1284 case DBUS_TYPE_UINT32:
1285 case DBUS_TYPE_INT64:
1286 case DBUS_TYPE_UINT64:
1287 case DBUS_TYPE_DOUBLE:
1288 case DBUS_TYPE_STRING:
1289 case DBUS_TYPE_OBJECT_PATH:
1290 case DBUS_TYPE_SIGNATURE:
1291 case DBUS_TYPE_ARRAY:
1292 case DBUS_TYPE_STRUCT:
1293 case DBUS_TYPE_DICT_ENTRY:
1294 case DBUS_TYPE_VARIANT:
1303 * Returns a string describing the given type.
1305 * @param typecode the type to describe
1306 * @returns a constant string describing the type
1309 _dbus_type_to_string (int typecode)
1313 case DBUS_TYPE_INVALID:
1315 case DBUS_TYPE_BOOLEAN:
1317 case DBUS_TYPE_BYTE:
1319 case DBUS_TYPE_INT16:
1321 case DBUS_TYPE_UINT16:
1323 case DBUS_TYPE_INT32:
1325 case DBUS_TYPE_UINT32:
1327 case DBUS_TYPE_INT64:
1329 case DBUS_TYPE_UINT64:
1331 case DBUS_TYPE_DOUBLE:
1333 case DBUS_TYPE_STRING:
1335 case DBUS_TYPE_OBJECT_PATH:
1336 return "object_path";
1337 case DBUS_TYPE_SIGNATURE:
1339 case DBUS_TYPE_STRUCT:
1341 case DBUS_TYPE_DICT_ENTRY:
1342 return "dict_entry";
1343 case DBUS_TYPE_ARRAY:
1345 case DBUS_TYPE_VARIANT:
1347 case DBUS_STRUCT_BEGIN_CHAR:
1348 return "begin_struct";
1349 case DBUS_STRUCT_END_CHAR:
1350 return "end_struct";
1351 case DBUS_DICT_ENTRY_BEGIN_CHAR:
1352 return "begin_dict_entry";
1353 case DBUS_DICT_ENTRY_END_CHAR:
1354 return "end_dict_entry";
1361 * If in verbose mode, print a block of binary data.
1363 * @todo 1.0 right now it prints even if not in verbose mode
1364 * check for verbose mode and return if not
1366 * @param data the data
1367 * @param len the length of the data
1368 * @param offset where to start counting for byte indexes
1371 _dbus_verbose_bytes (const unsigned char *data,
1376 const unsigned char *aligned;
1378 _dbus_assert (len >= 0);
1380 /* Print blanks on first row if appropriate */
1381 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1384 _dbus_assert (aligned <= data);
1386 if (aligned != data)
1388 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1389 while (aligned != data)
1391 _dbus_verbose (" ");
1396 /* now print the bytes */
1400 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1402 _dbus_verbose ("%4d\t%p: ",
1403 offset + i, &data[i]);
1406 if (data[i] >= 32 &&
1408 _dbus_verbose (" '%c' ", data[i]);
1410 _dbus_verbose ("0x%s%x ",
1411 data[i] <= 0xf ? "0" : "", data[i]);
1415 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1418 _dbus_verbose ("BE: %d LE: %d",
1419 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1420 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1423 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1425 #ifdef DBUS_HAVE_INT64
1426 /* I think I probably mean "GNU libc printf" and not "GNUC"
1427 * but we'll wait until someone complains. If you hit this,
1428 * just turn off verbose mode as a workaround.
1431 _dbus_verbose (" u64: 0x%llx",
1432 *(dbus_uint64_t*)&data[i-8]);
1435 _dbus_verbose (" dbl: %g",
1436 *(double*)&data[i-8]);
1439 _dbus_verbose ("\n");
1443 _dbus_verbose ("\n");
1447 * Dump the given part of the string to verbose log.
1449 * @param str the string
1450 * @param start the start of range to dump
1451 * @param len length of range
1454 _dbus_verbose_bytes_of_string (const DBusString *str,
1461 real_len = _dbus_string_get_length (str);
1463 _dbus_assert (start >= 0);
1465 if (start > real_len)
1467 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1468 start, len, real_len);
1472 if ((start + len) > real_len)
1474 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1475 start, len, real_len);
1476 len = real_len - start;
1479 d = _dbus_string_get_const_data_len (str, start, len);
1481 _dbus_verbose_bytes (d, len, start);
1485 map_type_char_to_type (int t)
1487 if (t == DBUS_STRUCT_BEGIN_CHAR)
1488 return DBUS_TYPE_STRUCT;
1489 else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
1490 return DBUS_TYPE_DICT_ENTRY;
1493 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
1494 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
1500 * Get the first type in the signature. The difference between this
1501 * and just getting the first byte of the signature is that you won't
1502 * get DBUS_STRUCT_BEGIN_CHAR, you'll get DBUS_TYPE_STRUCT
1505 * @param str string containing signature
1506 * @param pos where the signature starts
1507 * @returns the first type in the signature
1510 _dbus_first_type_in_signature (const DBusString *str,
1513 return map_type_char_to_type (_dbus_string_get_byte (str, pos));
1517 * Similar to #_dbus_first_type_in_signature, but operates
1518 * on a C string buffer.
1520 * @param str a C string buffer
1521 * @param pos where the signature starts
1522 * @returns the first type in the signature
1525 _dbus_first_type_in_signature_c_str (const char *str,
1528 return map_type_char_to_type (str[pos]);
1533 #ifdef DBUS_BUILD_TESTS
1534 #include "dbus-test.h"
1538 swap_test_array (void *array,
1548 _dbus_string_init_const_len (&t, array, len_bytes);
1549 swap_array (&t, 0, len_bytes / alignment, byte_order, alignment);
1552 #define MARSHAL_BASIC(typename, byte_order, literal) \
1554 v_##typename = literal; \
1555 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_##typename, \
1557 byte_order, NULL)) \
1558 _dbus_assert_not_reached ("no memory"); \
1561 #define DEMARSHAL_BASIC(typename, byte_order) \
1563 _dbus_marshal_read_basic (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1564 byte_order, &pos); \
1567 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1569 DEMARSHAL_BASIC (typename, byte_order); \
1570 if (literal != v_##typename) \
1572 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1573 _dbus_string_get_length (&str) - dump_pos); \
1574 _dbus_assert_not_reached ("demarshaled wrong value"); \
1578 #define MARSHAL_TEST(typename, byte_order, literal) \
1580 MARSHAL_BASIC (typename, byte_order, literal); \
1582 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1585 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1587 MARSHAL_BASIC (typename, byte_order, literal); \
1589 DEMARSHAL_BASIC (typename, byte_order); \
1590 if (strcmp (literal, v_##typename) != 0) \
1592 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1593 _dbus_string_get_length (&str) - dump_pos); \
1594 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1595 _dbus_assert_not_reached ("demarshaled wrong value"); \
1599 #define MARSHAL_FIXED_ARRAY(typename, byte_order, literal) \
1602 v_UINT32 = sizeof(literal); \
1603 if (!_dbus_marshal_write_basic (&str, pos, DBUS_TYPE_UINT32, &v_UINT32, \
1604 byte_order, &next)) \
1605 _dbus_assert_not_reached ("no memory"); \
1606 v_ARRAY_##typename = literal; \
1607 if (!_dbus_marshal_write_fixed_multi (&str, next, DBUS_TYPE_##typename, \
1608 &v_ARRAY_##typename, _DBUS_N_ELEMENTS(literal), \
1609 byte_order, NULL)) \
1610 _dbus_assert_not_reached ("no memory"); \
1613 #define DEMARSHAL_FIXED_ARRAY(typename, byte_order) \
1616 alignment = _dbus_type_get_alignment (DBUS_TYPE_##typename); \
1617 v_UINT32 = _dbus_marshal_read_uint32 (&str, dump_pos, byte_order, &next); \
1618 _dbus_marshal_read_fixed_multi (&str, next, DBUS_TYPE_##typename, &v_ARRAY_##typename, \
1619 v_UINT32/alignment, \
1620 byte_order, NULL); \
1621 swap_test_array (v_ARRAY_##typename, v_UINT32, \
1622 byte_order, alignment); \
1625 #define DEMARSHAL_FIXED_ARRAY_AND_CHECK(typename, byte_order, literal) \
1627 DEMARSHAL_FIXED_ARRAY (typename, byte_order); \
1628 if (memcmp (literal, v_ARRAY_##typename, sizeof (literal) != 0)) \
1630 _dbus_verbose ("MARSHALED DATA\n"); \
1631 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1632 _dbus_string_get_length (&str) - dump_pos); \
1633 _dbus_verbose ("LITERAL DATA\n"); \
1634 _dbus_verbose_bytes ((char*)literal, sizeof (literal), 0); \
1635 _dbus_verbose ("READ DATA\n"); \
1636 _dbus_verbose_bytes ((char*)v_ARRAY_##typename, sizeof (literal), 0); \
1637 _dbus_assert_not_reached ("demarshaled wrong fixed array value"); \
1641 #define MARSHAL_TEST_FIXED_ARRAY(typename, byte_order, literal) \
1643 MARSHAL_FIXED_ARRAY (typename, byte_order, literal); \
1645 DEMARSHAL_FIXED_ARRAY_AND_CHECK (typename, byte_order, literal); \
1649 _dbus_marshal_test (void)
1654 unsigned char array1[5] = { 3, 4, 0, 1, 9 };
1655 dbus_int16_t array2[3] = { 124, 457, 780 };
1656 dbus_int32_t array4[3] = { 123, 456, 789 };
1657 #ifdef DBUS_HAVE_INT64
1658 dbus_int64_t array8[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1659 DBUS_INT64_CONSTANT (0x456ffffffff),
1660 DBUS_INT64_CONSTANT (0x789ffffffff) };
1661 dbus_int64_t *v_ARRAY_INT64;
1663 unsigned char *v_ARRAY_BYTE;
1664 dbus_int16_t *v_ARRAY_INT16;
1665 dbus_uint16_t *v_ARRAY_UINT16;
1666 dbus_int32_t *v_ARRAY_INT32;
1667 dbus_uint32_t *v_ARRAY_UINT32;
1671 dbus_int16_t v_INT16;
1672 dbus_uint16_t v_UINT16;
1673 dbus_int32_t v_INT32;
1674 dbus_uint32_t v_UINT32;
1675 dbus_int64_t v_INT64;
1676 dbus_uint64_t v_UINT64;
1677 unsigned char v_BYTE;
1678 dbus_bool_t v_BOOLEAN;
1679 const char *v_STRING;
1680 const char *v_SIGNATURE;
1681 const char *v_OBJECT_PATH;
1684 if (!_dbus_string_init (&str))
1685 _dbus_assert_not_reached ("failed to init string");
1689 /* Marshal doubles */
1690 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1691 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1693 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1694 _dbus_assert_not_reached ("got wrong double value");
1696 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1697 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1699 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1700 _dbus_assert_not_reached ("got wrong double value");
1702 /* Marshal signed 16 integers */
1703 MARSHAL_TEST (INT16, DBUS_BIG_ENDIAN, -12345);
1704 MARSHAL_TEST (INT16, DBUS_LITTLE_ENDIAN, -12345);
1706 /* Marshal unsigned 16 integers */
1707 MARSHAL_TEST (UINT16, DBUS_BIG_ENDIAN, 0x1234);
1708 MARSHAL_TEST (UINT16, DBUS_LITTLE_ENDIAN, 0x1234);
1710 /* Marshal signed integers */
1711 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1712 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1714 /* Marshal unsigned integers */
1715 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1716 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1718 #ifdef DBUS_HAVE_INT64
1719 /* Marshal signed integers */
1720 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1721 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1723 /* Marshal unsigned integers */
1724 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1725 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1726 #endif /* DBUS_HAVE_INT64 */
1729 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1730 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1732 /* Marshal all possible bools! */
1733 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1734 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1735 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1736 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1738 /* Marshal strings */
1739 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1740 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1741 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1742 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1745 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1746 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1749 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1750 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1751 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1752 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1755 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_BIG_ENDIAN, array2);
1756 MARSHAL_TEST_FIXED_ARRAY (INT16, DBUS_LITTLE_ENDIAN, array2);
1757 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_BIG_ENDIAN, array2);
1758 MARSHAL_TEST_FIXED_ARRAY (UINT16, DBUS_LITTLE_ENDIAN, array2);
1760 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_BIG_ENDIAN, array4);
1761 MARSHAL_TEST_FIXED_ARRAY (INT32, DBUS_LITTLE_ENDIAN, array4);
1762 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_BIG_ENDIAN, array4);
1763 MARSHAL_TEST_FIXED_ARRAY (UINT32, DBUS_LITTLE_ENDIAN, array4);
1765 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_BIG_ENDIAN, array1);
1766 MARSHAL_TEST_FIXED_ARRAY (BYTE, DBUS_LITTLE_ENDIAN, array1);
1768 #ifdef DBUS_HAVE_INT64
1769 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_BIG_ENDIAN, array8);
1770 MARSHAL_TEST_FIXED_ARRAY (INT64, DBUS_LITTLE_ENDIAN, array8);
1776 * FIXME restore the set/pack tests
1779 #ifdef DBUS_HAVE_INT64
1780 /* set/pack 64-bit integers */
1781 _dbus_string_set_length (&str, 8);
1784 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1785 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1787 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1788 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1789 _dbus_string_get_const_data (&str)));
1792 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1793 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1795 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1796 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1797 _dbus_string_get_const_data (&str)));
1799 /* signed little pack */
1800 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1802 _dbus_string_get_data (&str));
1804 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1805 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1806 _dbus_string_get_const_data (&str)));
1808 /* signed big pack */
1809 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1811 _dbus_string_get_data (&str));
1813 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1814 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1815 _dbus_string_get_const_data (&str)));
1817 /* unsigned little */
1818 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1819 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1821 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1822 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1823 _dbus_string_get_const_data (&str)));
1826 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1827 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1829 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1830 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1831 _dbus_string_get_const_data (&str)));
1833 /* unsigned little pack */
1834 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1836 _dbus_string_get_data (&str));
1838 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1839 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1840 _dbus_string_get_const_data (&str)));
1842 /* unsigned big pack */
1843 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1845 _dbus_string_get_data (&str));
1847 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1848 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1849 _dbus_string_get_const_data (&str)));
1850 #endif /* DBUS_HAVE_INT64 */
1852 /* set/pack 32-bit integers */
1853 _dbus_string_set_length (&str, 4);
1856 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1859 _dbus_assert (-0x123456 ==
1860 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1861 _dbus_string_get_const_data (&str)));
1864 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1867 _dbus_assert (-0x123456 ==
1868 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1869 _dbus_string_get_const_data (&str)));
1871 /* signed little pack */
1872 _dbus_pack_int32 (-0x123456,
1874 _dbus_string_get_data (&str));
1876 _dbus_assert (-0x123456 ==
1877 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1878 _dbus_string_get_const_data (&str)));
1880 /* signed big pack */
1881 _dbus_pack_int32 (-0x123456,
1883 _dbus_string_get_data (&str));
1885 _dbus_assert (-0x123456 ==
1886 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1887 _dbus_string_get_const_data (&str)));
1889 /* unsigned little */
1890 _dbus_marshal_set_uint32 (&str,
1892 DBUS_LITTLE_ENDIAN);
1894 _dbus_assert (0x123456 ==
1895 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1896 _dbus_string_get_const_data (&str)));
1899 _dbus_marshal_set_uint32 (&str,
1903 _dbus_assert (0x123456 ==
1904 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1905 _dbus_string_get_const_data (&str)));
1907 /* unsigned little pack */
1908 _dbus_pack_uint32 (0x123456,
1910 _dbus_string_get_data (&str));
1912 _dbus_assert (0x123456 ==
1913 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1914 _dbus_string_get_const_data (&str)));
1916 /* unsigned big pack */
1917 _dbus_pack_uint32 (0x123456,
1919 _dbus_string_get_data (&str));
1921 _dbus_assert (0x123456 ==
1922 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1923 _dbus_string_get_const_data (&str)));
1925 #endif /* set/pack tests for integers */
1927 /* Strings in-place set */
1928 byte_order = DBUS_LITTLE_ENDIAN;
1932 _dbus_string_set_length (&str, 0);
1934 /* reset pos for the macros */
1937 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1939 /* Set it to something longer */
1940 _dbus_string_init_const (&t, "Hello world foo");
1942 v_STRING = _dbus_string_get_const_data (&t);
1943 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1944 &v_STRING, byte_order, NULL, NULL);
1946 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1947 &v_STRING, byte_order,
1949 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1951 /* Set it to something shorter */
1952 _dbus_string_init_const (&t, "Hello");
1954 v_STRING = _dbus_string_get_const_data (&t);
1955 _dbus_marshal_set_basic (&str, 0, DBUS_TYPE_STRING,
1956 &v_STRING, byte_order, NULL, NULL);
1957 _dbus_marshal_read_basic (&str, 0, DBUS_TYPE_STRING,
1958 &v_STRING, byte_order,
1960 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1962 /* Do the other byte order */
1963 if (byte_order == DBUS_LITTLE_ENDIAN)
1964 byte_order = DBUS_BIG_ENDIAN;
1970 _dbus_string_free (&str);
1975 #endif /* DBUS_BUILD_TESTS */