1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.c Marshalling routines
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 1.2
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-marshal.h"
26 #include "dbus-internals.h"
27 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
28 #include "dbus-string-private.h"
33 * @defgroup DBusMarshal marshaling and unmarshaling
34 * @ingroup DBusInternals
35 * @brief functions to marshal/unmarshal data from the wire
37 * Types and functions related to converting primitive data types from
38 * wire format to native machine format, and vice versa.
44 unpack_4_octets (int byte_order,
45 const unsigned char *data)
47 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
49 if (byte_order == DBUS_LITTLE_ENDIAN)
50 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
52 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
55 #ifndef DBUS_HAVE_INT64
58 swap_bytes (unsigned char *data,
61 unsigned char *p1 = data;
62 unsigned char *p2 = data + len - 1;
66 unsigned char tmp = *p1;
74 #endif /* !DBUS_HAVE_INT64 */
78 #ifdef DBUS_HAVE_INT64
83 #ifdef WORDS_BIGENDIAN
99 unpack_8_octets (int byte_order,
100 const unsigned char *data)
104 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
105 _dbus_assert (sizeof (r) == 8);
107 #ifdef DBUS_HAVE_INT64
108 if (byte_order == DBUS_LITTLE_ENDIAN)
109 r.u = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
111 r.u = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
113 r.d = *(double*)data;
114 swap_bytes (&r, sizeof (r));
121 * Unpacks a 32 bit unsigned integer from a data pointer
123 * @param byte_order The byte order to use
124 * @param data the data pointer
125 * @returns the integer
128 _dbus_unpack_uint32 (int byte_order,
129 const unsigned char *data)
131 return unpack_4_octets (byte_order, data);
135 * Unpacks a 32 bit signed integer from a data pointer
137 * @param byte_order The byte order to use
138 * @param data the data pointer
139 * @returns the integer
142 _dbus_unpack_int32 (int byte_order,
143 const unsigned char *data)
145 return (dbus_int32_t) unpack_4_octets (byte_order, data);
148 #ifdef DBUS_HAVE_INT64
150 * Unpacks a 64 bit unsigned integer from a data pointer
152 * @param byte_order The byte order to use
153 * @param data the data pointer
154 * @returns the integer
157 _dbus_unpack_uint64 (int byte_order,
158 const unsigned char *data)
162 r = unpack_8_octets (byte_order, data);
168 * Unpacks a 64 bit signed integer from a data pointer
170 * @param byte_order The byte order to use
171 * @param data the data pointer
172 * @returns the integer
175 _dbus_unpack_int64 (int byte_order,
176 const unsigned char *data)
180 r = unpack_8_octets (byte_order, data);
185 #endif /* DBUS_HAVE_INT64 */
188 pack_4_octets (dbus_uint32_t value,
192 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
194 if ((byte_order) == DBUS_LITTLE_ENDIAN)
195 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
197 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
201 pack_8_octets (DBusOctets8 value,
205 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
207 #ifdef DBUS_HAVE_INT64
208 if ((byte_order) == DBUS_LITTLE_ENDIAN)
209 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u);
211 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u);
213 memcpy (data, &value, 8);
214 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
215 swap_bytes ((unsigned char *)data, 8);
220 * Packs a 32 bit unsigned integer into a data pointer.
222 * @param value the value
223 * @param byte_order the byte order to use
224 * @param data the data pointer
227 _dbus_pack_uint32 (dbus_uint32_t value,
231 pack_4_octets (value, byte_order, data);
235 * Packs a 32 bit signed integer into a data pointer.
237 * @param value the value
238 * @param byte_order the byte order to use
239 * @param data the data pointer
242 _dbus_pack_int32 (dbus_int32_t value,
246 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
249 #ifdef DBUS_HAVE_INT64
251 * Packs a 64 bit unsigned integer into a data pointer.
253 * @param value the value
254 * @param byte_order the byte order to use
255 * @param data the data pointer
258 _dbus_pack_uint64 (dbus_uint64_t value,
264 pack_8_octets (r, byte_order, data);
268 * Packs a 64 bit signed integer into a data pointer.
270 * @param value the value
271 * @param byte_order the byte order to use
272 * @param data the data pointer
275 _dbus_pack_int64 (dbus_int64_t value,
281 pack_8_octets (r, byte_order, data);
283 #endif /* DBUS_HAVE_INT64 */
286 set_4_octets (DBusString *str,
293 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
294 byte_order == DBUS_BIG_ENDIAN);
296 data = _dbus_string_get_data_len (str, offset, 4);
298 _dbus_pack_uint32 (value, byte_order, data);
302 set_8_octets (DBusString *str,
309 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
310 byte_order == DBUS_BIG_ENDIAN);
312 data = _dbus_string_get_data_len (str, offset, 8);
314 pack_8_octets (value, byte_order, data);
318 * Sets the 4 bytes at the given offset to a marshaled signed integer,
319 * replacing anything found there previously.
321 * @param str the string to write the marshalled int to
322 * @param offset the byte offset where int should be written
323 * @param byte_order the byte order to use
324 * @param value the value
328 _dbus_marshal_set_int32 (DBusString *str,
333 set_4_octets (str, byte_order, offset, (dbus_uint32_t) value);
337 * Sets the 4 bytes at the given offset to a marshaled unsigned
338 * integer, replacing anything found there previously.
340 * @param str the string to write the marshalled int to
341 * @param offset the byte offset where int should be written
342 * @param byte_order the byte order to use
343 * @param value the value
347 _dbus_marshal_set_uint32 (DBusString *str,
352 set_4_octets (str, byte_order, offset, value);
355 #ifdef DBUS_HAVE_INT64
358 * Sets the 8 bytes at the given offset to a marshaled signed integer,
359 * replacing anything found there previously.
361 * @param str the string to write the marshalled int to
362 * @param offset the byte offset where int should be written
363 * @param byte_order the byte order to use
364 * @param value the value
368 _dbus_marshal_set_int64 (DBusString *str,
375 set_8_octets (str, byte_order, offset, r);
379 * Sets the 8 bytes at the given offset to a marshaled unsigned
380 * integer, replacing anything found there previously.
382 * @param str the string to write the marshalled int to
383 * @param offset the byte offset where int should be written
384 * @param byte_order the byte order to use
385 * @param value the value
389 _dbus_marshal_set_uint64 (DBusString *str,
396 set_8_octets (str, byte_order, offset, r);
398 #endif /* DBUS_HAVE_INT64 */
401 * Sets the existing marshaled string at the given offset with
402 * a new marshaled string. The given offset must point to
403 * an existing string or the wrong length will be deleted
404 * and replaced with the new string.
406 * @param str the string to write the marshalled string to
407 * @param offset the byte offset where string should be written
408 * @param byte_order the byte order to use
409 * @param value the value
410 * @param len the length to use
411 * @returns #TRUE on success
415 _dbus_marshal_set_string (DBusString *str,
418 const DBusString *value,
423 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
424 byte_order == DBUS_BIG_ENDIAN);
426 old_len = _dbus_demarshal_uint32 (str, byte_order,
429 if (!_dbus_string_replace_len (value, 0, len,
430 str, offset + 4, old_len))
433 _dbus_marshal_set_uint32 (str, byte_order,
440 * Sets the existing marshaled object ID at the given offset to a new
441 * value. The given offset must point to an existing object ID or this
442 * function doesn't make sense.
444 * @param str the string to write the marshalled string to
445 * @param offset the byte offset where string should be written
446 * @param byte_order the byte order to use
447 * @param value the new value
450 _dbus_marshal_set_object_id (DBusString *str,
453 const DBusObjectID *value)
456 #ifdef DBUS_HAVE_INT64
457 r.u = dbus_object_id_get_as_integer (value);
459 r.bits.low = dbus_object_id_get_low_bits (value);
460 r.bits.high = dbus_object_id_get_high_bits (value);
462 _dbus_assert (r.bits.low == dbus_object_id_get_low_bits (value));
463 _dbus_assert (r.bits.high == dbus_object_id_get_high_bits (value));
465 set_8_octets (str, byte_order, offset, r);
469 marshal_4_octets (DBusString *str,
473 _dbus_assert (sizeof (value) == 4);
475 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
476 value = DBUS_UINT32_SWAP_LE_BE (value);
478 return _dbus_string_append_4_aligned (str,
479 (const unsigned char *)&value);
483 marshal_8_octets (DBusString *str,
487 _dbus_assert (sizeof (value) == 8);
489 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
490 pack_8_octets (value, byte_order, (unsigned char*) &value); /* pack into self, swapping as we go */
492 return _dbus_string_append_8_aligned (str,
493 (const unsigned char *)&value);
497 * Marshals a double value.
499 * @param str the string to append the marshalled value to
500 * @param byte_order the byte order to use
501 * @param value the value
502 * @returns #TRUE on success
505 _dbus_marshal_double (DBusString *str,
511 return marshal_8_octets (str, byte_order, r);
515 * Marshals a 32 bit signed integer value.
517 * @param str the string to append the marshalled value to
518 * @param byte_order the byte order to use
519 * @param value the value
520 * @returns #TRUE on success
523 _dbus_marshal_int32 (DBusString *str,
527 return marshal_4_octets (str, byte_order, (dbus_uint32_t) value);
531 * Marshals a 32 bit unsigned integer value.
533 * @param str the string to append the marshalled value to
534 * @param byte_order the byte order to use
535 * @param value the value
536 * @returns #TRUE on success
539 _dbus_marshal_uint32 (DBusString *str,
543 return marshal_4_octets (str, byte_order, value);
547 #ifdef DBUS_HAVE_INT64
549 * Marshals a 64 bit signed integer value.
551 * @param str the string to append the marshalled value to
552 * @param byte_order the byte order to use
553 * @param value the value
554 * @returns #TRUE on success
557 _dbus_marshal_int64 (DBusString *str,
563 return marshal_8_octets (str, byte_order, r);
567 * Marshals a 64 bit unsigned integer value.
569 * @param str the string to append the marshalled value to
570 * @param byte_order the byte order to use
571 * @param value the value
572 * @returns #TRUE on success
575 _dbus_marshal_uint64 (DBusString *str,
581 return marshal_8_octets (str, byte_order, r);
584 #endif /* DBUS_HAVE_INT64 */
587 * Marshals a UTF-8 string
589 * @todo: If the string append fails we need to restore
590 * the old length. (also for other marshallers)
592 * @param str the string to append the marshalled value to
593 * @param byte_order the byte order to use
594 * @param value the string
595 * @returns #TRUE on success
598 _dbus_marshal_string (DBusString *str,
602 int len, old_string_len;
604 old_string_len = _dbus_string_get_length (str);
606 len = strlen (value);
608 if (!_dbus_marshal_uint32 (str, byte_order, len))
610 /* Restore the previous length */
611 _dbus_string_set_length (str, old_string_len);
616 return _dbus_string_append_len (str, value, len + 1);
620 * Marshals a byte array
622 * @param str the string to append the marshalled value to
623 * @param byte_order the byte order to use
624 * @param value the array
625 * @param len number of elements in the array
626 * @returns #TRUE on success
629 _dbus_marshal_byte_array (DBusString *str,
631 const unsigned char *value,
636 old_string_len = _dbus_string_get_length (str);
638 if (!_dbus_marshal_uint32 (str, byte_order, len))
640 /* Restore the previous length */
641 _dbus_string_set_length (str, old_string_len);
649 return _dbus_string_append_len (str, value, len);
653 marshal_4_octets_array (DBusString *str,
655 const dbus_uint32_t *value,
661 old_string_len = _dbus_string_get_length (str);
663 if (!_dbus_marshal_uint32 (str, byte_order, len * 4))
666 array_start = _dbus_string_get_length (str);
668 if (!_dbus_string_append_len (str, (const unsigned char*) value,
672 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
674 const unsigned char *d;
675 const unsigned char *end;
677 d = _dbus_string_get_data (str) + array_start;
681 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
689 /* Restore previous length */
690 _dbus_string_set_length (str, old_string_len);
696 marshal_8_octets_array (DBusString *str,
698 const DBusOctets8 *value,
704 old_string_len = _dbus_string_get_length (str);
706 if (!_dbus_marshal_uint32 (str, byte_order, len * 8))
709 array_start = _dbus_string_get_length (str);
711 if (!_dbus_string_append_len (str, (const unsigned char*) value,
715 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
717 const unsigned char *d;
718 const unsigned char *end;
720 d = _dbus_string_get_data (str) + array_start;
724 #ifdef DBUS_HAVE_INT64
725 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
736 /* Restore previous length */
737 _dbus_string_set_length (str, old_string_len);
743 * Marshals a 32 bit signed integer array
745 * @param str the string to append the marshalled value to
746 * @param byte_order the byte order to use
747 * @param value the array
748 * @param len the length of the array
749 * @returns #TRUE on success
752 _dbus_marshal_int32_array (DBusString *str,
754 const dbus_int32_t *value,
757 return marshal_4_octets_array (str, byte_order,
758 (const dbus_uint32_t*) value,
763 * Marshals a 32 bit unsigned integer array
765 * @param str the string to append the marshalled value to
766 * @param byte_order the byte order to use
767 * @param value the array
768 * @param len the length of the array
769 * @returns #TRUE on success
772 _dbus_marshal_uint32_array (DBusString *str,
774 const dbus_uint32_t *value,
777 return marshal_4_octets_array (str, byte_order,
782 #ifdef DBUS_HAVE_INT64
785 * Marshals a 64 bit signed integer array
787 * @param str the string to append the marshalled value to
788 * @param byte_order the byte order to use
789 * @param value the array
790 * @param len the length of the array
791 * @returns #TRUE on success
794 _dbus_marshal_int64_array (DBusString *str,
796 const dbus_int64_t *value,
799 return marshal_8_octets_array (str, byte_order,
800 (const DBusOctets8*) value,
805 * Marshals a 64 bit unsigned integer array
807 * @param str the string to append the marshalled value to
808 * @param byte_order the byte order to use
809 * @param value the array
810 * @param len the length of the array
811 * @returns #TRUE on success
814 _dbus_marshal_uint64_array (DBusString *str,
816 const dbus_uint64_t *value,
819 return marshal_8_octets_array (str, byte_order,
820 (const DBusOctets8*) value,
824 #endif /* DBUS_HAVE_INT64 */
827 * Marshals a double array
829 * @param str the string to append the marshalled value to
830 * @param byte_order the byte order to use
831 * @param value the array
832 * @param len the length of the array
833 * @returns #TRUE on success
836 _dbus_marshal_double_array (DBusString *str,
841 return marshal_8_octets_array (str, byte_order,
842 (const DBusOctets8*) value,
847 * Marshals a string array
849 * @param str the string to append the marshalled value to
850 * @param byte_order the byte order to use
851 * @param value the array
852 * @param len the length of the array
853 * @returns #TRUE on success
856 _dbus_marshal_string_array (DBusString *str,
861 int i, old_string_len, array_start;
863 old_string_len = _dbus_string_get_length (str);
865 /* Set the length to 0 temporarily */
866 if (!_dbus_marshal_uint32 (str, byte_order, 0))
869 array_start = _dbus_string_get_length (str);
871 for (i = 0; i < len; i++)
872 if (!_dbus_marshal_string (str, byte_order, value[i]))
875 /* Write the length now that we know it */
876 _dbus_marshal_set_uint32 (str, byte_order,
877 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
878 _dbus_string_get_length (str) - array_start);
883 /* Restore previous length */
884 _dbus_string_set_length (str, old_string_len);
890 * Marshals an object ID value.
892 * @param str the string to append the marshalled value to
893 * @param byte_order the byte order to use
894 * @param value the value
895 * @returns #TRUE on success
898 _dbus_marshal_object_id (DBusString *str,
900 const DBusObjectID *value)
903 #ifdef DBUS_HAVE_INT64
904 r.u = dbus_object_id_get_as_integer (value);
906 r.bits.low = dbus_object_id_get_low_bits (value);
907 r.bits.high = dbus_object_id_get_high_bits (value);
909 _dbus_assert (r.bits.low == dbus_object_id_get_low_bits (value));
910 _dbus_assert (r.bits.high == dbus_object_id_get_high_bits (value));
912 return marshal_8_octets (str, byte_order, r);
916 demarshal_4_octets (const DBusString *str,
921 const DBusRealString *real = (const DBusRealString*) str;
923 pos = _DBUS_ALIGN_VALUE (pos, 4);
928 return unpack_4_octets (byte_order, real->str + pos);
932 demarshal_8_octets (const DBusString *str,
937 const DBusRealString *real = (const DBusRealString*) str;
939 pos = _DBUS_ALIGN_VALUE (pos, 8);
944 return unpack_8_octets (byte_order, real->str + pos);
948 * Demarshals a double.
950 * @param str the string containing the data
951 * @param byte_order the byte order
952 * @param pos the position in the string
953 * @param new_pos the new position of the string
954 * @returns the demarshaled double.
957 _dbus_demarshal_double (const DBusString *str,
964 r = demarshal_8_octets (str, byte_order, pos, new_pos);
970 * Demarshals a 32 bit signed integer.
972 * @param str the string containing the data
973 * @param byte_order the byte order
974 * @param pos the position in the string
975 * @param new_pos the new position of the string
976 * @returns the demarshaled integer.
979 _dbus_demarshal_int32 (const DBusString *str,
984 return (dbus_int32_t) demarshal_4_octets (str, byte_order, pos, new_pos);
988 * Demarshals a 32 bit unsigned integer.
990 * @param str the string containing the data
991 * @param byte_order the byte order
992 * @param pos the position in the string
993 * @param new_pos the new position of the string
994 * @returns the demarshaled integer.
997 _dbus_demarshal_uint32 (const DBusString *str,
1002 return demarshal_4_octets (str, byte_order, pos, new_pos);
1005 #ifdef DBUS_HAVE_INT64
1008 * Demarshals a 64 bit signed integer.
1010 * @param str the string containing the data
1011 * @param byte_order the byte order
1012 * @param pos the position in the string
1013 * @param new_pos the new position of the string
1014 * @returns the demarshaled integer.
1017 _dbus_demarshal_int64 (const DBusString *str,
1024 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1030 * Demarshals a 64 bit unsigned integer.
1032 * @param str the string containing the data
1033 * @param byte_order the byte order
1034 * @param pos the position in the string
1035 * @param new_pos the new position of the string
1036 * @returns the demarshaled integer.
1039 _dbus_demarshal_uint64 (const DBusString *str,
1046 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1051 #endif /* DBUS_HAVE_INT64 */
1054 * Demarshals an UTF-8 string.
1056 * @todo Should we check the string to make sure
1057 * that it's valid UTF-8, and maybe "fix" the string
1060 * @todo Should probably demarshal to a DBusString,
1061 * having memcpy() in here is Evil(tm).
1063 * @param str the string containing the data
1064 * @param byte_order the byte order
1065 * @param pos the position in the string
1066 * @param new_pos the new position of the string
1067 * @returns the demarshaled string.
1070 _dbus_demarshal_string (const DBusString *str,
1079 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1081 retval = dbus_malloc (len + 1);
1086 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1091 memcpy (retval, data, len + 1);
1094 *new_pos = pos + len + 1;
1100 * Demarshals a byte array.
1102 * @todo Should probably demarshal to a DBusString,
1103 * having memcpy() in here is Evil(tm).
1105 * @param str the string containing the data
1106 * @param byte_order the byte order
1107 * @param pos the position in the string
1108 * @param new_pos the new position of the string
1109 * @param array the array
1110 * @param array_len length of the demarshaled data
1112 * @returns #TRUE on success
1115 _dbus_demarshal_byte_array (const DBusString *str,
1119 unsigned char **array,
1123 unsigned char *retval;
1126 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1139 retval = dbus_malloc (len);
1144 data = _dbus_string_get_const_data_len (str, pos, len);
1152 memcpy (retval, data, len);
1155 *new_pos = pos + len;
1164 demarshal_4_octets_array (const DBusString *str,
1168 dbus_uint32_t **array,
1172 dbus_uint32_t *retval;
1175 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1189 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1193 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1195 for (i = 0; i < len; i++)
1196 retval[i] = DBUS_UINT32_SWAP_LE_BE (retval[i]);
1200 *new_pos = pos + byte_len;
1209 demarshal_8_octets_array (const DBusString *str,
1213 DBusOctets8 **array,
1217 DBusOctets8 *retval;
1220 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1234 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1238 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1240 for (i = 0; i < len; i++)
1242 #ifdef DBUS_HAVE_INT64
1243 retval[i].u = DBUS_UINT64_SWAP_LE_BE (retval[i].u);
1245 swap_bytes (&retval[i], 8);
1251 *new_pos = pos + byte_len;
1260 * Demarshals a 32 bit signed integer array.
1262 * @param str the string containing the data
1263 * @param byte_order the byte order
1264 * @param pos the position in the string
1265 * @param new_pos the new position of the string
1266 * @param array the array
1267 * @param array_len length of the demarshaled data
1268 * @returns #TRUE on success
1271 _dbus_demarshal_int32_array (const DBusString *str,
1275 dbus_int32_t **array,
1278 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1279 (dbus_uint32_t**) array, array_len);
1283 * Demarshals a 32 bit unsigned integer array.
1285 * @param str the string containing the data
1286 * @param byte_order the byte order
1287 * @param pos the position in the string
1288 * @param new_pos the new position of the string
1289 * @param array the array
1290 * @param array_len length of the demarshaled data
1291 * @returns #TRUE on success
1294 _dbus_demarshal_uint32_array (const DBusString *str,
1298 dbus_uint32_t **array,
1301 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1305 #ifdef DBUS_HAVE_INT64
1308 * Demarshals a 64 bit signed integer array.
1310 * @param str the string containing the data
1311 * @param byte_order the byte order
1312 * @param pos the position in the string
1313 * @param new_pos the new position of the string
1314 * @param array the array
1315 * @param array_len length of the demarshaled data
1316 * @returns #TRUE on success
1319 _dbus_demarshal_int64_array (const DBusString *str,
1323 dbus_int64_t **array,
1326 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1327 (DBusOctets8**) array, array_len);
1331 * Demarshals a 64 bit unsigned integer array.
1333 * @param str the string containing the data
1334 * @param byte_order the byte order
1335 * @param pos the position in the string
1336 * @param new_pos the new position of the string
1337 * @param array the array
1338 * @param array_len length of the demarshaled data
1339 * @returns #TRUE on success
1342 _dbus_demarshal_uint64_array (const DBusString *str,
1346 dbus_uint64_t **array,
1349 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1350 (DBusOctets8**) array, array_len);
1353 #endif /* DBUS_HAVE_INT64 */
1356 * Demarshals a double array.
1358 * @param str the string containing the data
1359 * @param byte_order the byte order
1360 * @param pos the position in the string
1361 * @param new_pos the new position of the string
1362 * @param array the array
1363 * @param array_len length of the demarshaled data
1364 * @returns #TRUE on success
1367 _dbus_demarshal_double_array (const DBusString *str,
1374 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1375 (DBusOctets8**) array, array_len);
1379 * Demarshals a string array.
1381 * @param str the string containing the data
1382 * @param byte_order the byte order
1383 * @param pos the position in the string
1384 * @param new_pos the new position of the string
1385 * @param array the array
1386 * @param array_len length of the demarshaled data
1387 * @returns #TRUE on success
1390 _dbus_demarshal_string_array (const DBusString *str,
1402 bytes_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1417 end_pos = pos + bytes_len;
1419 retval = dbus_new (char *, allocated);
1424 while (pos < end_pos)
1426 retval[len] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1428 if (retval[len] == NULL)
1433 if (len >= allocated - 1) /* -1 for NULL termination */
1436 newp = dbus_realloc (retval,
1437 sizeof (char*) * allocated * 2);
1457 for (i = 0; i < len; i++)
1458 dbus_free (retval[i]);
1465 * Demarshals an object ID.
1467 * @param str the string containing the data
1468 * @param byte_order the byte order
1469 * @param pos the position in the string
1470 * @param new_pos the new position of the string
1471 * @param value address to store new object ID
1474 _dbus_demarshal_object_id (const DBusString *str,
1478 DBusObjectID *value)
1482 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1484 #ifdef DBUS_HAVE_INT64
1485 dbus_object_id_set_as_integer (value, r.u);
1487 dbus_object_id_set_low_bits (value, r.bits.low);
1488 dbus_object_id_set_high_bits (value, r.bits.high);
1490 _dbus_assert (dbus_object_id_get_low_bits (value) == r.bits.low);
1491 _dbus_assert (dbus_object_id_get_high_bits (value) == r.bits.high);
1495 * Returns the position right after the end of an argument. PERFORMS
1496 * NO VALIDATION WHATSOEVER. The message must have been previously
1499 * @param str a string
1500 * @param byte_order the byte order to use
1501 * @param type the type of the argument
1502 * @param pos the pos where the arg starts
1503 * @param end_pos pointer where the position right
1504 * after the end position will follow
1505 * @returns TRUE if more data exists after the arg
1508 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1514 if (pos >= _dbus_string_get_length (str))
1519 case DBUS_TYPE_INVALID:
1527 case DBUS_TYPE_BYTE:
1531 case DBUS_TYPE_BOOLEAN:
1535 case DBUS_TYPE_INT32:
1536 case DBUS_TYPE_UINT32:
1537 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
1540 case DBUS_TYPE_INT64:
1541 case DBUS_TYPE_UINT64:
1542 case DBUS_TYPE_OBJECT_ID:
1543 case DBUS_TYPE_DOUBLE:
1545 *end_pos = _DBUS_ALIGN_VALUE (pos, 8) + 8;
1548 case DBUS_TYPE_STRING:
1552 /* Demarshal the length */
1553 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1555 *end_pos = pos + len + 1;
1559 case DBUS_TYPE_NAMED:
1563 /* Demarshal the string length */
1564 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1566 *end_pos = pos + len + 1;
1568 /* Demarshal the data length */
1569 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1571 *end_pos = pos + len;
1575 case DBUS_TYPE_ARRAY:
1579 /* Demarshal the length */
1580 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1582 *end_pos = pos + len;
1586 case DBUS_TYPE_DICT:
1590 /* Demarshal the length */
1591 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1593 *end_pos = pos + len;
1598 _dbus_warn ("Unknown message arg type %d\n", type);
1599 _dbus_assert_not_reached ("Unknown message argument type\n");
1603 if (*end_pos > _dbus_string_get_length (str))
1610 * Demarshals and validates a length; returns < 0 if the validation
1611 * fails. The length is required to be small enough that
1612 * len*sizeof(double) will not overflow, and small enough to fit in a
1613 * signed integer. DOES NOT check whether the length points
1614 * beyond the end of the string, because it doesn't know the
1615 * size of array elements.
1617 * @param str the string
1618 * @param byte_order the byte order
1619 * @param pos the unaligned string position (snap to next aligned)
1620 * @param new_pos return location for new position.
1623 demarshal_and_validate_len (const DBusString *str,
1628 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1631 _dbus_assert (new_pos != NULL);
1633 if ((align_4 + 4) > _dbus_string_get_length (str))
1635 _dbus_verbose ("not enough room in message for array length\n");
1639 if (!_dbus_string_validate_nul (str, pos,
1642 _dbus_verbose ("array length alignment padding not initialized to nul\n");
1646 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1648 /* note that the len is the number of bytes, so we need it to be
1649 * at least SIZE_T_MAX, but make it smaller just to keep things
1650 * sane. We end up using ints for most sizes to avoid unsigned mess
1651 * so limit to maximum 32-bit signed int divided by at least 8, more
1652 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1654 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1655 if (len > MAX_ARRAY_LENGTH)
1657 _dbus_verbose ("array length %u exceeds maximum of %u\n",
1658 len, MAX_ARRAY_LENGTH);
1666 validate_string (const DBusString *str,
1668 int len_without_nul,
1671 *end_pos = pos + len_without_nul + 1;
1673 if (*end_pos > _dbus_string_get_length (str))
1675 _dbus_verbose ("string length outside length of the message\n");
1679 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1681 _dbus_verbose ("string arg not nul-terminated\n");
1685 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1687 _dbus_verbose ("string is not valid UTF-8\n");
1695 * Validates and returns a typecode at a specific position
1698 * @param str a string
1699 * @param type the type of the argument
1700 * @param pos the pos where the typecode starts
1701 * @param end_pos pointer where the position right
1702 * after the end position will follow
1703 * @returns #TRUE if the type is valid.
1706 _dbus_marshal_validate_type (const DBusString *str,
1713 if (pos >= _dbus_string_get_length (str))
1716 data = _dbus_string_get_const_data_len (str, pos, 1);
1718 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1721 if (end_pos != NULL)
1729 /* Faster validator for array data that doesn't call
1730 * validate_arg for each value
1733 validate_array_data (const DBusString *str,
1744 case DBUS_TYPE_INVALID:
1751 case DBUS_TYPE_STRING:
1752 case DBUS_TYPE_NAMED:
1753 case DBUS_TYPE_ARRAY:
1754 case DBUS_TYPE_DICT:
1755 /* This clean recursion to validate_arg is what we
1756 * are doing logically for all types, but we don't
1757 * really want to call validate_arg for every byte
1758 * in a byte array, so the primitive types are
1763 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1764 type, array_type_pos, pos, &pos))
1769 case DBUS_TYPE_BYTE:
1773 case DBUS_TYPE_BOOLEAN:
1778 c = _dbus_string_get_byte (str, pos);
1780 if (!(c == 0 || c == 1))
1782 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1790 case DBUS_TYPE_INT32:
1791 case DBUS_TYPE_UINT32:
1792 /* Call validate arg one time to check alignment padding
1795 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1796 type, array_type_pos, pos, &pos))
1798 pos = _DBUS_ALIGN_VALUE (end, 4);
1801 case DBUS_TYPE_INT64:
1802 case DBUS_TYPE_UINT64:
1803 case DBUS_TYPE_DOUBLE:
1804 case DBUS_TYPE_OBJECT_ID:
1805 /* Call validate arg one time to check alignment padding
1808 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1809 type, array_type_pos, pos, &pos))
1811 pos = _DBUS_ALIGN_VALUE (end, 8);
1815 _dbus_verbose ("Unknown message arg type %d\n", type);
1825 * Validates an argument of a specific type, checking that it
1826 * is well-formed, for example no ludicrous length fields, strings
1827 * are nul-terminated, etc.
1828 * Returns the end position of the argument in end_pos, and
1829 * returns #TRUE if a valid arg begins at "pos"
1831 * @todo security: need to audit this function.
1833 * @todo For array types that can't be invalid, we should not
1834 * walk the whole array validating it. e.g. just skip all the
1835 * int values in an int array.
1837 * @param str a string
1838 * @param byte_order the byte order to use
1839 * @param depth current recursion depth, to prevent excessive recursion
1840 * @param type the type of the argument
1841 * @param array_type_pos the position of the current array type, or
1842 * -1 if not in an array
1843 * @param pos the pos where the arg starts
1844 * @param end_pos pointer where the position right
1845 * after the end position will follow
1846 * @returns #TRUE if the arg is valid.
1849 _dbus_marshal_validate_arg (const DBusString *str,
1857 if (pos > _dbus_string_get_length (str))
1859 _dbus_verbose ("Validation went off the end of the message\n");
1863 #define MAX_VALIDATION_DEPTH 32
1865 if (depth > MAX_VALIDATION_DEPTH)
1867 _dbus_verbose ("Maximum recursion depth reached validating message\n");
1873 case DBUS_TYPE_INVALID:
1881 case DBUS_TYPE_BYTE:
1882 if (1 > _dbus_string_get_length (str) - pos)
1884 _dbus_verbose ("no room for byte value\n");
1891 case DBUS_TYPE_BOOLEAN:
1895 if (1 > _dbus_string_get_length (str) - pos)
1897 _dbus_verbose ("no room for boolean value\n");
1901 c = _dbus_string_get_byte (str, pos);
1903 if (!(c == 0 || c == 1))
1905 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1913 case DBUS_TYPE_INT32:
1914 case DBUS_TYPE_UINT32:
1916 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1918 if (!_dbus_string_validate_nul (str, pos,
1921 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
1925 *end_pos = align_4 + 4;
1929 case DBUS_TYPE_INT64:
1930 case DBUS_TYPE_UINT64:
1931 case DBUS_TYPE_DOUBLE:
1932 case DBUS_TYPE_OBJECT_ID:
1934 int align_8 = _DBUS_ALIGN_VALUE (pos, 8);
1936 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
1938 if (!_dbus_string_validate_nul (str, pos,
1941 _dbus_verbose ("double/int64/uint64/objid alignment padding not initialized to nul\n");
1945 *end_pos = align_8 + 8;
1949 case DBUS_TYPE_STRING:
1953 /* Demarshal the length, which does NOT include
1956 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1960 if (!validate_string (str, pos, len, end_pos))
1965 case DBUS_TYPE_NAMED:
1969 /* Demarshal the string length, which does NOT include
1972 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1976 if (!validate_string (str, pos, len, &pos))
1980 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1984 *end_pos = pos + len;
1988 case DBUS_TYPE_ARRAY:
1994 if (array_type_pos == -1)
1996 array_type_pos = pos;
2000 if (!_dbus_marshal_validate_type (str, pos, &array_type, &pos))
2002 _dbus_verbose ("invalid array type\n");
2006 /* NIL values take up no space, so you couldn't iterate over an array of them.
2007 * array of nil seems useless anyway; the useful thing might be array of
2008 * (nil OR string) but we have no framework for that.
2010 if (array_type == DBUS_TYPE_NIL)
2012 _dbus_verbose ("array of NIL is not allowed\n");
2016 while (array_type == DBUS_TYPE_ARRAY);
2021 if (!_dbus_marshal_validate_type (str, array_type_pos, &array_type, NULL))
2023 _dbus_verbose ("invalid array type\n");
2027 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2031 if (len > _dbus_string_get_length (str) - pos)
2033 _dbus_verbose ("array length outside length of the message\n");
2039 if (!validate_array_data (str, byte_order, depth + 1,
2040 array_type, array_type_pos,
2046 /* This should not be able to happen, as long as validate_arg moves forward;
2047 * but the check is here just to be paranoid.
2049 _dbus_verbose ("array length %d specified was longer than actual array contents by %d\n",
2056 _dbus_verbose ("array contents exceeds array length %d by %d\n", len, pos - end);
2064 case DBUS_TYPE_DICT:
2070 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2074 if (len > _dbus_string_get_length (str) - pos)
2076 _dbus_verbose ("dict length outside length of the message\n");
2085 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2086 DBUS_TYPE_STRING, -1, pos, &pos))
2089 if (!_dbus_marshal_validate_type (str, pos, &dict_type, &pos))
2091 _dbus_verbose ("invalid dict entry type at offset %d\n", pos);
2095 /* Validate element */
2096 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2097 dict_type, -1, pos, &pos))
2103 _dbus_verbose ("dict contents exceed stated dict length\n");
2112 _dbus_verbose ("Unknown message arg type %d\n", type);
2116 if (*end_pos > _dbus_string_get_length (str))
2124 * If in verbose mode, print a block of binary data.
2126 * @todo right now it prints even if not in verbose mode
2128 * @param data the data
2129 * @param len the length of the data
2132 _dbus_verbose_bytes (const unsigned char *data,
2136 const unsigned char *aligned;
2138 _dbus_assert (len >= 0);
2140 /* Print blanks on first row if appropriate */
2141 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
2144 _dbus_assert (aligned <= data);
2146 if (aligned != data)
2148 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
2149 while (aligned != data)
2151 _dbus_verbose (" ");
2156 /* now print the bytes */
2160 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2162 _dbus_verbose ("%4d\t%p: ",
2166 if (data[i] >= 32 &&
2168 _dbus_verbose (" '%c' ", data[i]);
2170 _dbus_verbose ("0x%s%x ",
2171 data[i] <= 0xf ? "0" : "", data[i]);
2175 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2178 _dbus_verbose ("BE: %d LE: %d",
2179 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
2180 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
2183 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
2185 _dbus_verbose (" dbl: %g",
2186 *(double*)&data[i-8]);
2189 _dbus_verbose ("\n");
2193 _dbus_verbose ("\n");
2197 * Dump the given part of the string to verbose log.
2199 * @param str the string
2200 * @param start the start of range to dump
2201 * @param len length of range
2204 _dbus_verbose_bytes_of_string (const DBusString *str,
2211 real_len = _dbus_string_get_length (str);
2213 _dbus_assert (start >= 0);
2215 if (start > real_len)
2217 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
2218 start, len, real_len);
2222 if ((start + len) > real_len)
2224 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
2225 start, len, real_len);
2226 len = real_len - start;
2229 d = _dbus_string_get_const_data_len (str, start, len);
2231 _dbus_verbose_bytes (d, len);
2236 #ifdef DBUS_BUILD_TESTS
2237 #include "dbus-test.h"
2241 _dbus_marshal_test (void)
2246 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
2247 #ifdef DBUS_HAVE_INT64
2248 dbus_int64_t array3[3] = { 0x123ffffffff, 0x456ffffffff, 0x789ffffffff }, *array4;
2252 DBusObjectID obj_id, obj_id2;
2254 if (!_dbus_string_init (&str))
2255 _dbus_assert_not_reached ("failed to init string");
2257 /* Marshal doubles */
2258 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2259 _dbus_assert_not_reached ("could not marshal double value");
2260 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2261 _dbus_assert_not_reached ("demarshal failed");
2263 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2264 _dbus_assert_not_reached ("could not marshal double value");
2265 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2266 _dbus_assert_not_reached ("demarshal failed");
2268 /* Marshal signed integers */
2269 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2270 _dbus_assert_not_reached ("could not marshal signed integer value");
2271 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2272 _dbus_assert_not_reached ("demarshal failed");
2274 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2275 _dbus_assert_not_reached ("could not marshal signed integer value");
2276 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2277 _dbus_assert_not_reached ("demarshal failed");
2279 /* Marshal unsigned integers */
2280 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2281 _dbus_assert_not_reached ("could not marshal signed integer value");
2282 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2283 _dbus_assert_not_reached ("demarshal failed");
2285 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2286 _dbus_assert_not_reached ("could not marshal signed integer value");
2287 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2288 _dbus_assert_not_reached ("demarshal failed");
2290 #ifdef DBUS_HAVE_INT64
2291 /* Marshal signed integers */
2292 if (!_dbus_marshal_int64 (&str, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2293 _dbus_assert_not_reached ("could not marshal signed integer value");
2294 if (!_dbus_demarshal_int64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2295 _dbus_assert_not_reached ("demarshal failed");
2297 if (!_dbus_marshal_int64 (&str, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2298 _dbus_assert_not_reached ("could not marshal signed integer value");
2299 if (!_dbus_demarshal_int64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2300 _dbus_assert_not_reached ("demarshal failed");
2302 /* Marshal unsigned integers */
2303 if (!_dbus_marshal_uint64 (&str, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2304 _dbus_assert_not_reached ("could not marshal signed integer value");
2305 if (!(_dbus_demarshal_uint64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2306 _dbus_assert_not_reached ("demarshal failed");
2308 if (!_dbus_marshal_uint64 (&str, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2309 _dbus_assert_not_reached ("could not marshal signed integer value");
2310 if (!(_dbus_demarshal_uint64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2311 _dbus_assert_not_reached ("demarshal failed");
2312 #endif /* DBUS_HAVE_INT64 */
2314 /* Marshal object IDs */
2315 dbus_object_id_set_high_bits (&obj_id, 0xfffe);
2316 dbus_object_id_set_low_bits (&obj_id, 0xaacc);
2318 if (!_dbus_marshal_object_id (&str, DBUS_BIG_ENDIAN, &obj_id))
2319 _dbus_assert_not_reached ("could not marshal object ID value");
2320 _dbus_demarshal_object_id (&str, DBUS_BIG_ENDIAN, pos, &pos, &obj_id2);
2321 if (!dbus_object_id_equal (&obj_id, &obj_id2))
2322 _dbus_assert_not_reached ("demarshal failed");
2324 if (!_dbus_marshal_object_id (&str, DBUS_LITTLE_ENDIAN, &obj_id))
2325 _dbus_assert_not_reached ("could not marshal object ID value");
2326 _dbus_demarshal_object_id (&str, DBUS_LITTLE_ENDIAN, pos, &pos, &obj_id2);
2327 if (!dbus_object_id_equal (&obj_id, &obj_id2))
2328 _dbus_assert_not_reached ("demarshal failed");
2330 /* Marshal strings */
2331 tmp1 = "This is the dbus test string";
2332 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2333 _dbus_assert_not_reached ("could not marshal string");
2334 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2335 if (!strcmp (tmp1, tmp2) == 0)
2336 _dbus_assert_not_reached ("demarshal failed");
2339 tmp1 = "This is the dbus test string";
2340 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2341 _dbus_assert_not_reached ("could not marshal string");
2342 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2343 if (!strcmp (tmp1, tmp2) == 0)
2344 _dbus_assert_not_reached ("demarshal failed");
2347 /* Marshal signed integer arrays */
2348 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2349 _dbus_assert_not_reached ("could not marshal integer array");
2350 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2351 _dbus_assert_not_reached ("could not demarshal integer array");
2354 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2357 #ifdef DBUS_HAVE_INT64
2358 /* Marshal 64-bit signed integer arrays */
2359 if (!_dbus_marshal_int64_array (&str, DBUS_BIG_ENDIAN, array3, 3))
2360 _dbus_assert_not_reached ("could not marshal integer array");
2361 if (!_dbus_demarshal_int64_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array4, &len))
2362 _dbus_assert_not_reached ("could not demarshal integer array");
2365 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2368 /* set/pack 64-bit integers */
2369 _dbus_string_set_length (&str, 8);
2372 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
2373 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2375 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2376 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2377 _dbus_string_get_const_data (&str)));
2380 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
2381 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2383 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2384 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2385 _dbus_string_get_const_data (&str)));
2387 /* signed little pack */
2388 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2390 _dbus_string_get_data (&str));
2392 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2393 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2394 _dbus_string_get_const_data (&str)));
2396 /* signed big pack */
2397 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2399 _dbus_string_get_data (&str));
2401 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2402 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2403 _dbus_string_get_const_data (&str)));
2405 /* unsigned little */
2406 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
2407 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2409 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2410 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2411 _dbus_string_get_const_data (&str)));
2414 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
2415 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2417 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2418 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2419 _dbus_string_get_const_data (&str)));
2421 /* unsigned little pack */
2422 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2424 _dbus_string_get_data (&str));
2426 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2427 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2428 _dbus_string_get_const_data (&str)));
2430 /* unsigned big pack */
2431 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2433 _dbus_string_get_data (&str));
2435 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2436 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2437 _dbus_string_get_const_data (&str)));
2441 /* set/pack 32-bit integers */
2442 _dbus_string_set_length (&str, 4);
2445 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
2448 _dbus_assert (-0x123456 ==
2449 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2450 _dbus_string_get_const_data (&str)));
2453 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
2456 _dbus_assert (-0x123456 ==
2457 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2458 _dbus_string_get_const_data (&str)));
2460 /* signed little pack */
2461 _dbus_pack_int32 (-0x123456,
2463 _dbus_string_get_data (&str));
2465 _dbus_assert (-0x123456 ==
2466 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2467 _dbus_string_get_const_data (&str)));
2469 /* signed big pack */
2470 _dbus_pack_int32 (-0x123456,
2472 _dbus_string_get_data (&str));
2474 _dbus_assert (-0x123456 ==
2475 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2476 _dbus_string_get_const_data (&str)));
2478 /* unsigned little */
2479 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
2482 _dbus_assert (0x123456 ==
2483 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2484 _dbus_string_get_const_data (&str)));
2487 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
2490 _dbus_assert (0x123456 ==
2491 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2492 _dbus_string_get_const_data (&str)));
2494 /* unsigned little pack */
2495 _dbus_pack_uint32 (0x123456,
2497 _dbus_string_get_data (&str));
2499 _dbus_assert (0x123456 ==
2500 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2501 _dbus_string_get_const_data (&str)));
2503 /* unsigned big pack */
2504 _dbus_pack_uint32 (0x123456,
2506 _dbus_string_get_data (&str));
2508 _dbus_assert (0x123456 ==
2509 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2510 _dbus_string_get_const_data (&str)));
2515 _dbus_string_set_length (&str, 0);
2517 _dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN,
2520 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2521 _dbus_assert (strcmp (s, "Hello world") == 0);
2524 _dbus_string_init_const (&t, "Hello world foo");
2526 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2527 &t, _dbus_string_get_length (&t));
2529 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2530 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2533 _dbus_string_init_const (&t, "Hello");
2535 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2536 &t, _dbus_string_get_length (&t));
2538 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2539 _dbus_assert (strcmp (s, "Hello") == 0);
2542 /* Strings (big endian) */
2544 _dbus_string_set_length (&str, 0);
2546 _dbus_marshal_string (&str, DBUS_BIG_ENDIAN,
2549 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2550 _dbus_assert (strcmp (s, "Hello world") == 0);
2553 _dbus_string_init_const (&t, "Hello world foo");
2555 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2556 &t, _dbus_string_get_length (&t));
2558 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2559 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2562 _dbus_string_init_const (&t, "Hello");
2564 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2565 &t, _dbus_string_get_length (&t));
2567 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2568 _dbus_assert (strcmp (s, "Hello") == 0);
2572 _dbus_string_free (&str);
2577 #endif /* DBUS_BUILD_TESTS */