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 DBusObjectID object_id;
87 unpack_8_octets (int byte_order,
88 const unsigned char *data)
92 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
93 _dbus_assert (sizeof (r) == 8);
95 #ifdef DBUS_HAVE_INT64
96 if (byte_order == DBUS_LITTLE_ENDIAN)
97 r.u = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
99 r.u = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
101 r.d = *(double*)data;
102 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
103 swap_bytes ((unsigned char*) &r, sizeof (r));
110 * Unpacks a 32 bit unsigned integer from a data pointer
112 * @param byte_order The byte order to use
113 * @param data the data pointer
114 * @returns the integer
117 _dbus_unpack_uint32 (int byte_order,
118 const unsigned char *data)
120 return unpack_4_octets (byte_order, data);
124 * Unpacks a 32 bit signed integer from a data pointer
126 * @param byte_order The byte order to use
127 * @param data the data pointer
128 * @returns the integer
131 _dbus_unpack_int32 (int byte_order,
132 const unsigned char *data)
134 return (dbus_int32_t) unpack_4_octets (byte_order, data);
137 #ifdef DBUS_HAVE_INT64
139 * Unpacks a 64 bit unsigned integer from a data pointer
141 * @param byte_order The byte order to use
142 * @param data the data pointer
143 * @returns the integer
146 _dbus_unpack_uint64 (int byte_order,
147 const unsigned char *data)
151 r = unpack_8_octets (byte_order, data);
157 * Unpacks a 64 bit signed integer from a data pointer
159 * @param byte_order The byte order to use
160 * @param data the data pointer
161 * @returns the integer
164 _dbus_unpack_int64 (int byte_order,
165 const unsigned char *data)
169 r = unpack_8_octets (byte_order, data);
174 #endif /* DBUS_HAVE_INT64 */
177 pack_4_octets (dbus_uint32_t value,
181 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
183 if ((byte_order) == DBUS_LITTLE_ENDIAN)
184 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
186 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
190 pack_8_octets (DBusOctets8 value,
194 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
196 #ifdef DBUS_HAVE_INT64
197 if ((byte_order) == DBUS_LITTLE_ENDIAN)
198 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u);
200 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u);
202 memcpy (data, &value, 8);
203 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
204 swap_bytes ((unsigned char *)data, 8);
209 * Packs a 32 bit unsigned integer into a data pointer.
211 * @param value the value
212 * @param byte_order the byte order to use
213 * @param data the data pointer
216 _dbus_pack_uint32 (dbus_uint32_t value,
220 pack_4_octets (value, byte_order, data);
224 * Packs a 32 bit signed integer into a data pointer.
226 * @param value the value
227 * @param byte_order the byte order to use
228 * @param data the data pointer
231 _dbus_pack_int32 (dbus_int32_t value,
235 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
238 #ifdef DBUS_HAVE_INT64
240 * Packs a 64 bit unsigned integer into a data pointer.
242 * @param value the value
243 * @param byte_order the byte order to use
244 * @param data the data pointer
247 _dbus_pack_uint64 (dbus_uint64_t value,
253 pack_8_octets (r, byte_order, data);
257 * Packs a 64 bit signed integer into a data pointer.
259 * @param value the value
260 * @param byte_order the byte order to use
261 * @param data the data pointer
264 _dbus_pack_int64 (dbus_int64_t value,
270 pack_8_octets (r, byte_order, data);
272 #endif /* DBUS_HAVE_INT64 */
275 set_4_octets (DBusString *str,
282 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
283 byte_order == DBUS_BIG_ENDIAN);
285 data = _dbus_string_get_data_len (str, offset, 4);
287 _dbus_pack_uint32 (value, byte_order, data);
291 set_8_octets (DBusString *str,
298 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
299 byte_order == DBUS_BIG_ENDIAN);
301 data = _dbus_string_get_data_len (str, offset, 8);
303 pack_8_octets (value, byte_order, data);
307 * Sets the 4 bytes at the given offset to a marshaled signed integer,
308 * replacing anything found there previously.
310 * @param str the string to write the marshalled int to
311 * @param offset the byte offset where int should be written
312 * @param byte_order the byte order to use
313 * @param value the value
317 _dbus_marshal_set_int32 (DBusString *str,
322 set_4_octets (str, byte_order, offset, (dbus_uint32_t) value);
326 * Sets the 4 bytes at the given offset to a marshaled unsigned
327 * integer, replacing anything found there previously.
329 * @param str the string to write the marshalled int to
330 * @param offset the byte offset where int should be written
331 * @param byte_order the byte order to use
332 * @param value the value
336 _dbus_marshal_set_uint32 (DBusString *str,
341 set_4_octets (str, byte_order, offset, value);
344 #ifdef DBUS_HAVE_INT64
347 * Sets the 8 bytes at the given offset to a marshaled signed integer,
348 * replacing anything found there previously.
350 * @param str the string to write the marshalled int to
351 * @param offset the byte offset where int should be written
352 * @param byte_order the byte order to use
353 * @param value the value
357 _dbus_marshal_set_int64 (DBusString *str,
364 set_8_octets (str, byte_order, offset, r);
368 * Sets the 8 bytes at the given offset to a marshaled unsigned
369 * integer, replacing anything found there previously.
371 * @param str the string to write the marshalled int to
372 * @param offset the byte offset where int should be written
373 * @param byte_order the byte order to use
374 * @param value the value
378 _dbus_marshal_set_uint64 (DBusString *str,
385 set_8_octets (str, byte_order, offset, r);
387 #endif /* DBUS_HAVE_INT64 */
390 * Sets the existing marshaled string at the given offset with
391 * a new marshaled string. The given offset must point to
392 * an existing string or the wrong length will be deleted
393 * and replaced with the new string.
395 * @param str the string to write the marshalled string to
396 * @param offset the byte offset where string should be written
397 * @param byte_order the byte order to use
398 * @param value the value
399 * @param len the length to use
400 * @returns #TRUE on success
404 _dbus_marshal_set_string (DBusString *str,
407 const DBusString *value,
412 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
413 byte_order == DBUS_BIG_ENDIAN);
415 old_len = _dbus_demarshal_uint32 (str, byte_order,
418 if (!_dbus_string_replace_len (value, 0, len,
419 str, offset + 4, old_len))
422 _dbus_marshal_set_uint32 (str, byte_order,
429 * Sets the existing marshaled object ID at the given offset to a new
430 * value. The given offset must point to an existing object ID or this
431 * function doesn't make sense.
433 * @param str the string to write the marshalled string to
434 * @param offset the byte offset where string should be written
435 * @param byte_order the byte order to use
436 * @param value the new value
439 _dbus_marshal_set_object_id (DBusString *str,
442 const DBusObjectID *value)
446 r.object_id = *value;
448 set_8_octets (str, byte_order, offset, r);
452 marshal_4_octets (DBusString *str,
456 _dbus_assert (sizeof (value) == 4);
458 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
459 value = DBUS_UINT32_SWAP_LE_BE (value);
461 return _dbus_string_append_4_aligned (str,
462 (const unsigned char *)&value);
466 marshal_8_octets (DBusString *str,
470 _dbus_assert (sizeof (value) == 8);
472 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
473 pack_8_octets (value, byte_order, (unsigned char*) &value); /* pack into self, swapping as we go */
475 return _dbus_string_append_8_aligned (str,
476 (const unsigned char *)&value);
480 * Marshals a double value.
482 * @param str the string to append the marshalled value to
483 * @param byte_order the byte order to use
484 * @param value the value
485 * @returns #TRUE on success
488 _dbus_marshal_double (DBusString *str,
494 return marshal_8_octets (str, byte_order, r);
498 * Marshals a 32 bit signed integer value.
500 * @param str the string to append the marshalled value to
501 * @param byte_order the byte order to use
502 * @param value the value
503 * @returns #TRUE on success
506 _dbus_marshal_int32 (DBusString *str,
510 return marshal_4_octets (str, byte_order, (dbus_uint32_t) value);
514 * Marshals a 32 bit unsigned integer value.
516 * @param str the string to append the marshalled value to
517 * @param byte_order the byte order to use
518 * @param value the value
519 * @returns #TRUE on success
522 _dbus_marshal_uint32 (DBusString *str,
526 return marshal_4_octets (str, byte_order, value);
530 #ifdef DBUS_HAVE_INT64
532 * Marshals a 64 bit signed integer value.
534 * @param str the string to append the marshalled value to
535 * @param byte_order the byte order to use
536 * @param value the value
537 * @returns #TRUE on success
540 _dbus_marshal_int64 (DBusString *str,
546 return marshal_8_octets (str, byte_order, r);
550 * Marshals a 64 bit unsigned integer value.
552 * @param str the string to append the marshalled value to
553 * @param byte_order the byte order to use
554 * @param value the value
555 * @returns #TRUE on success
558 _dbus_marshal_uint64 (DBusString *str,
564 return marshal_8_octets (str, byte_order, r);
567 #endif /* DBUS_HAVE_INT64 */
570 * Marshals a UTF-8 string
572 * @todo: If the string append fails we need to restore
573 * the old length. (also for other marshallers)
575 * @param str the string to append the marshalled value to
576 * @param byte_order the byte order to use
577 * @param value the string
578 * @returns #TRUE on success
581 _dbus_marshal_string (DBusString *str,
585 int len, old_string_len;
587 old_string_len = _dbus_string_get_length (str);
589 len = strlen (value);
591 if (!_dbus_marshal_uint32 (str, byte_order, len))
593 /* Restore the previous length */
594 _dbus_string_set_length (str, old_string_len);
599 return _dbus_string_append_len (str, value, len + 1);
603 * Marshals a byte array
605 * @param str the string to append the marshalled value to
606 * @param byte_order the byte order to use
607 * @param value the array
608 * @param len number of elements in the array
609 * @returns #TRUE on success
612 _dbus_marshal_byte_array (DBusString *str,
614 const unsigned char *value,
619 old_string_len = _dbus_string_get_length (str);
621 if (!_dbus_marshal_uint32 (str, byte_order, len))
623 /* Restore the previous length */
624 _dbus_string_set_length (str, old_string_len);
632 return _dbus_string_append_len (str, value, len);
636 marshal_4_octets_array (DBusString *str,
638 const dbus_uint32_t *value,
644 old_string_len = _dbus_string_get_length (str);
646 if (!_dbus_marshal_uint32 (str, byte_order, len * 4))
649 array_start = _dbus_string_get_length (str);
651 if (!_dbus_string_append_len (str, (const unsigned char*) value,
655 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
657 const unsigned char *d;
658 const unsigned char *end;
660 d = _dbus_string_get_data (str) + array_start;
664 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
672 /* Restore previous length */
673 _dbus_string_set_length (str, old_string_len);
679 marshal_8_octets_array (DBusString *str,
681 const DBusOctets8 *value,
687 old_string_len = _dbus_string_get_length (str);
689 if (!_dbus_marshal_uint32 (str, byte_order, len * 8))
692 array_start = _dbus_string_get_length (str);
694 if (!_dbus_string_append_len (str, (const unsigned char*) value,
698 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
700 const unsigned char *d;
701 const unsigned char *end;
703 d = _dbus_string_get_data (str) + array_start;
707 #ifdef DBUS_HAVE_INT64
708 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
710 swap_bytes ((unsigned char*) d, 8);
719 /* Restore previous length */
720 _dbus_string_set_length (str, old_string_len);
726 * Marshals a 32 bit signed integer array
728 * @param str the string to append the marshalled value to
729 * @param byte_order the byte order to use
730 * @param value the array
731 * @param len the length of the array
732 * @returns #TRUE on success
735 _dbus_marshal_int32_array (DBusString *str,
737 const dbus_int32_t *value,
740 return marshal_4_octets_array (str, byte_order,
741 (const dbus_uint32_t*) value,
746 * Marshals a 32 bit unsigned integer array
748 * @param str the string to append the marshalled value to
749 * @param byte_order the byte order to use
750 * @param value the array
751 * @param len the length of the array
752 * @returns #TRUE on success
755 _dbus_marshal_uint32_array (DBusString *str,
757 const dbus_uint32_t *value,
760 return marshal_4_octets_array (str, byte_order,
765 #ifdef DBUS_HAVE_INT64
768 * Marshals a 64 bit signed integer array
770 * @param str the string to append the marshalled value to
771 * @param byte_order the byte order to use
772 * @param value the array
773 * @param len the length of the array
774 * @returns #TRUE on success
777 _dbus_marshal_int64_array (DBusString *str,
779 const dbus_int64_t *value,
782 return marshal_8_octets_array (str, byte_order,
783 (const DBusOctets8*) value,
788 * Marshals a 64 bit unsigned integer array
790 * @param str the string to append the marshalled value to
791 * @param byte_order the byte order to use
792 * @param value the array
793 * @param len the length of the array
794 * @returns #TRUE on success
797 _dbus_marshal_uint64_array (DBusString *str,
799 const dbus_uint64_t *value,
802 return marshal_8_octets_array (str, byte_order,
803 (const DBusOctets8*) value,
807 #endif /* DBUS_HAVE_INT64 */
810 * Marshals a double array
812 * @param str the string to append the marshalled value to
813 * @param byte_order the byte order to use
814 * @param value the array
815 * @param len the length of the array
816 * @returns #TRUE on success
819 _dbus_marshal_double_array (DBusString *str,
824 return marshal_8_octets_array (str, byte_order,
825 (const DBusOctets8*) value,
830 * Marshals a string array
832 * @param str the string to append the marshalled value to
833 * @param byte_order the byte order to use
834 * @param value the array
835 * @param len the length of the array
836 * @returns #TRUE on success
839 _dbus_marshal_string_array (DBusString *str,
844 int i, old_string_len, array_start;
846 old_string_len = _dbus_string_get_length (str);
848 /* Set the length to 0 temporarily */
849 if (!_dbus_marshal_uint32 (str, byte_order, 0))
852 array_start = _dbus_string_get_length (str);
854 for (i = 0; i < len; i++)
855 if (!_dbus_marshal_string (str, byte_order, value[i]))
858 /* Write the length now that we know it */
859 _dbus_marshal_set_uint32 (str, byte_order,
860 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
861 _dbus_string_get_length (str) - array_start);
866 /* Restore previous length */
867 _dbus_string_set_length (str, old_string_len);
873 * Marshals an object ID value.
875 * @param str the string to append the marshalled value to
876 * @param byte_order the byte order to use
877 * @param value the value
878 * @returns #TRUE on success
881 _dbus_marshal_object_id (DBusString *str,
883 const DBusObjectID *value)
887 r.object_id = *value;
889 return marshal_8_octets (str, byte_order, r);
893 demarshal_4_octets (const DBusString *str,
898 const DBusRealString *real = (const DBusRealString*) str;
900 pos = _DBUS_ALIGN_VALUE (pos, 4);
905 return unpack_4_octets (byte_order, real->str + pos);
909 demarshal_8_octets (const DBusString *str,
914 const DBusRealString *real = (const DBusRealString*) str;
916 pos = _DBUS_ALIGN_VALUE (pos, 8);
921 return unpack_8_octets (byte_order, real->str + pos);
925 * Demarshals a double.
927 * @param str the string containing the data
928 * @param byte_order the byte order
929 * @param pos the position in the string
930 * @param new_pos the new position of the string
931 * @returns the demarshaled double.
934 _dbus_demarshal_double (const DBusString *str,
941 r = demarshal_8_octets (str, byte_order, pos, new_pos);
947 * Demarshals a 32 bit signed integer.
949 * @param str the string containing the data
950 * @param byte_order the byte order
951 * @param pos the position in the string
952 * @param new_pos the new position of the string
953 * @returns the demarshaled integer.
956 _dbus_demarshal_int32 (const DBusString *str,
961 return (dbus_int32_t) demarshal_4_octets (str, byte_order, pos, new_pos);
965 * Demarshals a 32 bit unsigned integer.
967 * @param str the string containing the data
968 * @param byte_order the byte order
969 * @param pos the position in the string
970 * @param new_pos the new position of the string
971 * @returns the demarshaled integer.
974 _dbus_demarshal_uint32 (const DBusString *str,
979 return demarshal_4_octets (str, byte_order, pos, new_pos);
982 #ifdef DBUS_HAVE_INT64
985 * Demarshals a 64 bit signed integer.
987 * @param str the string containing the data
988 * @param byte_order the byte order
989 * @param pos the position in the string
990 * @param new_pos the new position of the string
991 * @returns the demarshaled integer.
994 _dbus_demarshal_int64 (const DBusString *str,
1001 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1007 * Demarshals a 64 bit unsigned integer.
1009 * @param str the string containing the data
1010 * @param byte_order the byte order
1011 * @param pos the position in the string
1012 * @param new_pos the new position of the string
1013 * @returns the demarshaled integer.
1016 _dbus_demarshal_uint64 (const DBusString *str,
1023 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1028 #endif /* DBUS_HAVE_INT64 */
1031 * Demarshals an UTF-8 string.
1033 * @todo Should we check the string to make sure
1034 * that it's valid UTF-8, and maybe "fix" the string
1037 * @todo Should probably demarshal to a DBusString,
1038 * having memcpy() in here is Evil(tm).
1040 * @param str the string containing the data
1041 * @param byte_order the byte order
1042 * @param pos the position in the string
1043 * @param new_pos the new position of the string
1044 * @returns the demarshaled string.
1047 _dbus_demarshal_string (const DBusString *str,
1056 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1058 retval = dbus_malloc (len + 1);
1063 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1068 memcpy (retval, data, len + 1);
1071 *new_pos = pos + len + 1;
1077 * Demarshals a byte array.
1079 * @todo Should probably demarshal to a DBusString,
1080 * having memcpy() in here is Evil(tm).
1082 * @param str the string containing the data
1083 * @param byte_order the byte order
1084 * @param pos the position in the string
1085 * @param new_pos the new position of the string
1086 * @param array the array
1087 * @param array_len length of the demarshaled data
1089 * @returns #TRUE on success
1092 _dbus_demarshal_byte_array (const DBusString *str,
1096 unsigned char **array,
1100 unsigned char *retval;
1103 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1116 retval = dbus_malloc (len);
1121 data = _dbus_string_get_const_data_len (str, pos, len);
1129 memcpy (retval, data, len);
1132 *new_pos = pos + len;
1141 demarshal_4_octets_array (const DBusString *str,
1145 dbus_uint32_t **array,
1149 dbus_uint32_t *retval;
1152 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1166 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1170 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1172 for (i = 0; i < len; i++)
1173 retval[i] = DBUS_UINT32_SWAP_LE_BE (retval[i]);
1177 *new_pos = pos + byte_len;
1186 demarshal_8_octets_array (const DBusString *str,
1190 DBusOctets8 **array,
1194 DBusOctets8 *retval;
1197 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1211 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1215 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1217 for (i = 0; i < len; i++)
1219 #ifdef DBUS_HAVE_INT64
1220 retval[i].u = DBUS_UINT64_SWAP_LE_BE (retval[i].u);
1222 swap_bytes ((unsigned char *) &retval[i], 8);
1228 *new_pos = pos + byte_len;
1237 * Demarshals a 32 bit signed integer array.
1239 * @param str the string containing the data
1240 * @param byte_order the byte order
1241 * @param pos the position in the string
1242 * @param new_pos the new position of the string
1243 * @param array the array
1244 * @param array_len length of the demarshaled data
1245 * @returns #TRUE on success
1248 _dbus_demarshal_int32_array (const DBusString *str,
1252 dbus_int32_t **array,
1255 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1256 (dbus_uint32_t**) array, array_len);
1260 * Demarshals a 32 bit unsigned 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_uint32_array (const DBusString *str,
1275 dbus_uint32_t **array,
1278 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1282 #ifdef DBUS_HAVE_INT64
1285 * Demarshals a 64 bit signed integer array.
1287 * @param str the string containing the data
1288 * @param byte_order the byte order
1289 * @param pos the position in the string
1290 * @param new_pos the new position of the string
1291 * @param array the array
1292 * @param array_len length of the demarshaled data
1293 * @returns #TRUE on success
1296 _dbus_demarshal_int64_array (const DBusString *str,
1300 dbus_int64_t **array,
1303 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1304 (DBusOctets8**) array, array_len);
1308 * Demarshals a 64 bit unsigned 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_uint64_array (const DBusString *str,
1323 dbus_uint64_t **array,
1326 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1327 (DBusOctets8**) array, array_len);
1330 #endif /* DBUS_HAVE_INT64 */
1333 * Demarshals a double array.
1335 * @param str the string containing the data
1336 * @param byte_order the byte order
1337 * @param pos the position in the string
1338 * @param new_pos the new position of the string
1339 * @param array the array
1340 * @param array_len length of the demarshaled data
1341 * @returns #TRUE on success
1344 _dbus_demarshal_double_array (const DBusString *str,
1351 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1352 (DBusOctets8**) array, array_len);
1356 * Demarshals a string 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_string_array (const DBusString *str,
1379 bytes_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1394 end_pos = pos + bytes_len;
1396 retval = dbus_new (char *, allocated);
1401 while (pos < end_pos)
1403 retval[len] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1405 if (retval[len] == NULL)
1410 if (len >= allocated - 1) /* -1 for NULL termination */
1413 newp = dbus_realloc (retval,
1414 sizeof (char*) * allocated * 2);
1434 for (i = 0; i < len; i++)
1435 dbus_free (retval[i]);
1442 * Demarshals an object ID.
1444 * @param str the string containing the data
1445 * @param byte_order the byte order
1446 * @param pos the position in the string
1447 * @param new_pos the new position of the string
1448 * @param value address to store new object ID
1451 _dbus_demarshal_object_id (const DBusString *str,
1455 DBusObjectID *value)
1459 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1461 *value = r.object_id;
1465 * Returns the position right after the end of an argument. PERFORMS
1466 * NO VALIDATION WHATSOEVER. The message must have been previously
1469 * @param str a string
1470 * @param byte_order the byte order to use
1471 * @param type the type of the argument
1472 * @param pos the pos where the arg starts
1473 * @param end_pos pointer where the position right
1474 * after the end position will follow
1475 * @returns TRUE if more data exists after the arg
1478 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1484 if (pos >= _dbus_string_get_length (str))
1489 case DBUS_TYPE_INVALID:
1497 case DBUS_TYPE_BYTE:
1501 case DBUS_TYPE_BOOLEAN:
1505 case DBUS_TYPE_INT32:
1506 case DBUS_TYPE_UINT32:
1507 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
1510 case DBUS_TYPE_INT64:
1511 case DBUS_TYPE_UINT64:
1512 case DBUS_TYPE_OBJECT_ID:
1513 case DBUS_TYPE_DOUBLE:
1515 *end_pos = _DBUS_ALIGN_VALUE (pos, 8) + 8;
1518 case DBUS_TYPE_STRING:
1522 /* Demarshal the length */
1523 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1525 *end_pos = pos + len + 1;
1529 case DBUS_TYPE_NAMED:
1533 /* Demarshal the string length */
1534 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1536 *end_pos = pos + len + 1;
1538 /* Demarshal the data length */
1539 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1541 *end_pos = pos + len;
1545 case DBUS_TYPE_ARRAY:
1549 /* Demarshal the length */
1550 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1552 *end_pos = pos + len;
1556 case DBUS_TYPE_DICT:
1560 /* Demarshal the length */
1561 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1563 *end_pos = pos + len;
1568 _dbus_warn ("Unknown message arg type %d\n", type);
1569 _dbus_assert_not_reached ("Unknown message argument type\n");
1573 if (*end_pos > _dbus_string_get_length (str))
1580 * Demarshals and validates a length; returns < 0 if the validation
1581 * fails. The length is required to be small enough that
1582 * len*sizeof(double) will not overflow, and small enough to fit in a
1583 * signed integer. DOES NOT check whether the length points
1584 * beyond the end of the string, because it doesn't know the
1585 * size of array elements.
1587 * @param str the string
1588 * @param byte_order the byte order
1589 * @param pos the unaligned string position (snap to next aligned)
1590 * @param new_pos return location for new position.
1593 demarshal_and_validate_len (const DBusString *str,
1598 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1601 _dbus_assert (new_pos != NULL);
1603 if ((align_4 + 4) > _dbus_string_get_length (str))
1605 _dbus_verbose ("not enough room in message for array length\n");
1609 if (!_dbus_string_validate_nul (str, pos,
1612 _dbus_verbose ("array length alignment padding not initialized to nul\n");
1616 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1618 /* note that the len is the number of bytes, so we need it to be
1619 * at least SIZE_T_MAX, but make it smaller just to keep things
1620 * sane. We end up using ints for most sizes to avoid unsigned mess
1621 * so limit to maximum 32-bit signed int divided by at least 8, more
1622 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1624 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1625 if (len > MAX_ARRAY_LENGTH)
1627 _dbus_verbose ("array length %u exceeds maximum of %u\n",
1628 len, MAX_ARRAY_LENGTH);
1636 validate_string (const DBusString *str,
1638 int len_without_nul,
1641 *end_pos = pos + len_without_nul + 1;
1643 if (*end_pos > _dbus_string_get_length (str))
1645 _dbus_verbose ("string length outside length of the message\n");
1649 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1651 _dbus_verbose ("string arg not nul-terminated\n");
1655 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1657 _dbus_verbose ("string is not valid UTF-8\n");
1665 * Validates and returns a typecode at a specific position
1668 * @param str a string
1669 * @param type the type of the argument
1670 * @param pos the pos where the typecode starts
1671 * @param end_pos pointer where the position right
1672 * after the end position will follow
1673 * @returns #TRUE if the type is valid.
1676 _dbus_marshal_validate_type (const DBusString *str,
1683 if (pos >= _dbus_string_get_length (str))
1686 data = _dbus_string_get_const_data_len (str, pos, 1);
1688 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1691 if (end_pos != NULL)
1699 /* Faster validator for array data that doesn't call
1700 * validate_arg for each value
1703 validate_array_data (const DBusString *str,
1714 case DBUS_TYPE_INVALID:
1721 case DBUS_TYPE_STRING:
1722 case DBUS_TYPE_NAMED:
1723 case DBUS_TYPE_ARRAY:
1724 case DBUS_TYPE_DICT:
1725 /* This clean recursion to validate_arg is what we
1726 * are doing logically for all types, but we don't
1727 * really want to call validate_arg for every byte
1728 * in a byte array, so the primitive types are
1733 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1734 type, array_type_pos, pos, &pos))
1739 case DBUS_TYPE_BYTE:
1743 case DBUS_TYPE_BOOLEAN:
1748 c = _dbus_string_get_byte (str, pos);
1750 if (!(c == 0 || c == 1))
1752 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1760 case DBUS_TYPE_INT32:
1761 case DBUS_TYPE_UINT32:
1762 /* Call validate arg one time to check alignment padding
1765 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1766 type, array_type_pos, pos, &pos))
1768 pos = _DBUS_ALIGN_VALUE (end, 4);
1771 case DBUS_TYPE_INT64:
1772 case DBUS_TYPE_UINT64:
1773 case DBUS_TYPE_DOUBLE:
1774 case DBUS_TYPE_OBJECT_ID:
1775 /* Call validate arg one time to check alignment padding
1778 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1779 type, array_type_pos, pos, &pos))
1781 pos = _DBUS_ALIGN_VALUE (end, 8);
1785 _dbus_verbose ("Unknown message arg type %d\n", type);
1795 * Validates an argument of a specific type, checking that it
1796 * is well-formed, for example no ludicrous length fields, strings
1797 * are nul-terminated, etc.
1798 * Returns the end position of the argument in end_pos, and
1799 * returns #TRUE if a valid arg begins at "pos"
1801 * @todo security: need to audit this function.
1803 * @todo For array types that can't be invalid, we should not
1804 * walk the whole array validating it. e.g. just skip all the
1805 * int values in an int array.
1807 * @param str a string
1808 * @param byte_order the byte order to use
1809 * @param depth current recursion depth, to prevent excessive recursion
1810 * @param type the type of the argument
1811 * @param array_type_pos the position of the current array type, or
1812 * -1 if not in an array
1813 * @param pos the pos where the arg starts
1814 * @param end_pos pointer where the position right
1815 * after the end position will follow
1816 * @returns #TRUE if the arg is valid.
1819 _dbus_marshal_validate_arg (const DBusString *str,
1827 if (pos > _dbus_string_get_length (str))
1829 _dbus_verbose ("Validation went off the end of the message\n");
1833 #define MAX_VALIDATION_DEPTH 32
1835 if (depth > MAX_VALIDATION_DEPTH)
1837 _dbus_verbose ("Maximum recursion depth reached validating message\n");
1843 case DBUS_TYPE_INVALID:
1851 case DBUS_TYPE_BYTE:
1852 if (1 > _dbus_string_get_length (str) - pos)
1854 _dbus_verbose ("no room for byte value\n");
1861 case DBUS_TYPE_BOOLEAN:
1865 if (1 > _dbus_string_get_length (str) - pos)
1867 _dbus_verbose ("no room for boolean value\n");
1871 c = _dbus_string_get_byte (str, pos);
1873 if (!(c == 0 || c == 1))
1875 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1883 case DBUS_TYPE_INT32:
1884 case DBUS_TYPE_UINT32:
1886 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1888 if (!_dbus_string_validate_nul (str, pos,
1891 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
1895 *end_pos = align_4 + 4;
1899 case DBUS_TYPE_INT64:
1900 case DBUS_TYPE_UINT64:
1901 case DBUS_TYPE_DOUBLE:
1902 case DBUS_TYPE_OBJECT_ID:
1904 int align_8 = _DBUS_ALIGN_VALUE (pos, 8);
1906 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
1908 if (!_dbus_string_validate_nul (str, pos,
1911 _dbus_verbose ("double/int64/uint64/objid alignment padding not initialized to nul\n");
1915 *end_pos = align_8 + 8;
1919 case DBUS_TYPE_STRING:
1923 /* Demarshal the length, which does NOT include
1926 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1930 if (!validate_string (str, pos, len, end_pos))
1935 case DBUS_TYPE_NAMED:
1939 /* Demarshal the string length, which does NOT include
1942 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1946 if (!validate_string (str, pos, len, &pos))
1950 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1954 *end_pos = pos + len;
1958 case DBUS_TYPE_ARRAY:
1964 if (array_type_pos == -1)
1966 array_type_pos = pos;
1970 if (!_dbus_marshal_validate_type (str, pos, &array_type, &pos))
1972 _dbus_verbose ("invalid array type\n");
1976 /* NIL values take up no space, so you couldn't iterate over an array of them.
1977 * array of nil seems useless anyway; the useful thing might be array of
1978 * (nil OR string) but we have no framework for that.
1980 if (array_type == DBUS_TYPE_NIL)
1982 _dbus_verbose ("array of NIL is not allowed\n");
1986 while (array_type == DBUS_TYPE_ARRAY);
1991 if (!_dbus_marshal_validate_type (str, array_type_pos, &array_type, NULL))
1993 _dbus_verbose ("invalid array type\n");
1997 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2001 if (len > _dbus_string_get_length (str) - pos)
2003 _dbus_verbose ("array length outside length of the message\n");
2009 if (!validate_array_data (str, byte_order, depth + 1,
2010 array_type, array_type_pos,
2016 /* This should not be able to happen, as long as validate_arg moves forward;
2017 * but the check is here just to be paranoid.
2019 _dbus_verbose ("array length %d specified was longer than actual array contents by %d\n",
2026 _dbus_verbose ("array contents exceeds array length %d by %d\n", len, pos - end);
2034 case DBUS_TYPE_DICT:
2040 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2044 if (len > _dbus_string_get_length (str) - pos)
2046 _dbus_verbose ("dict length outside length of the message\n");
2055 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2056 DBUS_TYPE_STRING, -1, pos, &pos))
2059 if (!_dbus_marshal_validate_type (str, pos, &dict_type, &pos))
2061 _dbus_verbose ("invalid dict entry type at offset %d\n", pos);
2065 /* Validate element */
2066 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2067 dict_type, -1, pos, &pos))
2073 _dbus_verbose ("dict contents exceed stated dict length\n");
2082 _dbus_verbose ("Unknown message arg type %d\n", type);
2086 if (*end_pos > _dbus_string_get_length (str))
2094 * If in verbose mode, print a block of binary data.
2096 * @todo right now it prints even if not in verbose mode
2098 * @param data the data
2099 * @param len the length of the data
2102 _dbus_verbose_bytes (const unsigned char *data,
2106 const unsigned char *aligned;
2108 _dbus_assert (len >= 0);
2110 /* Print blanks on first row if appropriate */
2111 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
2114 _dbus_assert (aligned <= data);
2116 if (aligned != data)
2118 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
2119 while (aligned != data)
2121 _dbus_verbose (" ");
2126 /* now print the bytes */
2130 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2132 _dbus_verbose ("%4d\t%p: ",
2136 if (data[i] >= 32 &&
2138 _dbus_verbose (" '%c' ", data[i]);
2140 _dbus_verbose ("0x%s%x ",
2141 data[i] <= 0xf ? "0" : "", data[i]);
2145 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2148 _dbus_verbose ("BE: %d LE: %d",
2149 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
2150 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
2153 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
2155 _dbus_verbose (" dbl: %g",
2156 *(double*)&data[i-8]);
2159 _dbus_verbose ("\n");
2163 _dbus_verbose ("\n");
2167 * Dump the given part of the string to verbose log.
2169 * @param str the string
2170 * @param start the start of range to dump
2171 * @param len length of range
2174 _dbus_verbose_bytes_of_string (const DBusString *str,
2181 real_len = _dbus_string_get_length (str);
2183 _dbus_assert (start >= 0);
2185 if (start > real_len)
2187 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
2188 start, len, real_len);
2192 if ((start + len) > real_len)
2194 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
2195 start, len, real_len);
2196 len = real_len - start;
2199 d = _dbus_string_get_const_data_len (str, start, len);
2201 _dbus_verbose_bytes (d, len);
2206 #ifdef DBUS_BUILD_TESTS
2207 #include "dbus-test.h"
2211 _dbus_marshal_test (void)
2216 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
2217 #ifdef DBUS_HAVE_INT64
2218 dbus_int64_t array3[3] = { 0x123ffffffff, 0x456ffffffff, 0x789ffffffff }, *array4;
2222 DBusObjectID obj_id, obj_id2;
2224 if (!_dbus_string_init (&str))
2225 _dbus_assert_not_reached ("failed to init string");
2227 /* Marshal doubles */
2228 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2229 _dbus_assert_not_reached ("could not marshal double value");
2230 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2231 _dbus_assert_not_reached ("demarshal failed");
2233 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2234 _dbus_assert_not_reached ("could not marshal double value");
2235 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2236 _dbus_assert_not_reached ("demarshal failed");
2238 /* Marshal signed integers */
2239 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2240 _dbus_assert_not_reached ("could not marshal signed integer value");
2241 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2242 _dbus_assert_not_reached ("demarshal failed");
2244 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2245 _dbus_assert_not_reached ("could not marshal signed integer value");
2246 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2247 _dbus_assert_not_reached ("demarshal failed");
2249 /* Marshal unsigned integers */
2250 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2251 _dbus_assert_not_reached ("could not marshal signed integer value");
2252 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2253 _dbus_assert_not_reached ("demarshal failed");
2255 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2256 _dbus_assert_not_reached ("could not marshal signed integer value");
2257 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2258 _dbus_assert_not_reached ("demarshal failed");
2260 #ifdef DBUS_HAVE_INT64
2261 /* Marshal signed integers */
2262 if (!_dbus_marshal_int64 (&str, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2263 _dbus_assert_not_reached ("could not marshal signed integer value");
2264 if (!_dbus_demarshal_int64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2265 _dbus_assert_not_reached ("demarshal failed");
2267 if (!_dbus_marshal_int64 (&str, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2268 _dbus_assert_not_reached ("could not marshal signed integer value");
2269 if (!_dbus_demarshal_int64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2270 _dbus_assert_not_reached ("demarshal failed");
2272 /* Marshal unsigned integers */
2273 if (!_dbus_marshal_uint64 (&str, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2274 _dbus_assert_not_reached ("could not marshal signed integer value");
2275 if (!(_dbus_demarshal_uint64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2276 _dbus_assert_not_reached ("demarshal failed");
2278 if (!_dbus_marshal_uint64 (&str, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2279 _dbus_assert_not_reached ("could not marshal signed integer value");
2280 if (!(_dbus_demarshal_uint64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2281 _dbus_assert_not_reached ("demarshal failed");
2282 #endif /* DBUS_HAVE_INT64 */
2284 /* Marshal object IDs */
2285 dbus_object_id_set_server_bits (&obj_id, 0xfffe);
2286 dbus_object_id_set_client_bits (&obj_id, 0xaacc);
2287 dbus_object_id_set_instance_bits (&obj_id, 0x70f00f0f);
2289 if (!_dbus_marshal_object_id (&str, DBUS_BIG_ENDIAN, &obj_id))
2290 _dbus_assert_not_reached ("could not marshal object ID value");
2291 _dbus_demarshal_object_id (&str, DBUS_BIG_ENDIAN, pos, &pos, &obj_id2);
2292 if (!dbus_object_id_equal (&obj_id, &obj_id2))
2293 _dbus_assert_not_reached ("demarshal failed");
2295 if (!_dbus_marshal_object_id (&str, DBUS_LITTLE_ENDIAN, &obj_id))
2296 _dbus_assert_not_reached ("could not marshal object ID value");
2297 _dbus_demarshal_object_id (&str, DBUS_LITTLE_ENDIAN, pos, &pos, &obj_id2);
2298 if (!dbus_object_id_equal (&obj_id, &obj_id2))
2299 _dbus_assert_not_reached ("demarshal failed");
2301 /* Marshal strings */
2302 tmp1 = "This is the dbus test string";
2303 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2304 _dbus_assert_not_reached ("could not marshal string");
2305 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2306 if (!strcmp (tmp1, tmp2) == 0)
2307 _dbus_assert_not_reached ("demarshal failed");
2310 tmp1 = "This is the dbus test string";
2311 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2312 _dbus_assert_not_reached ("could not marshal string");
2313 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2314 if (!strcmp (tmp1, tmp2) == 0)
2315 _dbus_assert_not_reached ("demarshal failed");
2318 /* Marshal signed integer arrays */
2319 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2320 _dbus_assert_not_reached ("could not marshal integer array");
2321 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2322 _dbus_assert_not_reached ("could not demarshal integer array");
2325 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2328 #ifdef DBUS_HAVE_INT64
2329 /* Marshal 64-bit signed integer arrays */
2330 if (!_dbus_marshal_int64_array (&str, DBUS_BIG_ENDIAN, array3, 3))
2331 _dbus_assert_not_reached ("could not marshal integer array");
2332 if (!_dbus_demarshal_int64_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array4, &len))
2333 _dbus_assert_not_reached ("could not demarshal integer array");
2336 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2339 /* set/pack 64-bit integers */
2340 _dbus_string_set_length (&str, 8);
2343 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
2344 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2346 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2347 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2348 _dbus_string_get_const_data (&str)));
2351 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
2352 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2354 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2355 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2356 _dbus_string_get_const_data (&str)));
2358 /* signed little pack */
2359 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2361 _dbus_string_get_data (&str));
2363 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2364 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2365 _dbus_string_get_const_data (&str)));
2367 /* signed big pack */
2368 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2370 _dbus_string_get_data (&str));
2372 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2373 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2374 _dbus_string_get_const_data (&str)));
2376 /* unsigned little */
2377 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
2378 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2380 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2381 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2382 _dbus_string_get_const_data (&str)));
2385 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
2386 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2388 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2389 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2390 _dbus_string_get_const_data (&str)));
2392 /* unsigned little pack */
2393 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2395 _dbus_string_get_data (&str));
2397 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2398 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2399 _dbus_string_get_const_data (&str)));
2401 /* unsigned big pack */
2402 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2404 _dbus_string_get_data (&str));
2406 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2407 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2408 _dbus_string_get_const_data (&str)));
2412 /* set/pack 32-bit integers */
2413 _dbus_string_set_length (&str, 4);
2416 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
2419 _dbus_assert (-0x123456 ==
2420 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2421 _dbus_string_get_const_data (&str)));
2424 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
2427 _dbus_assert (-0x123456 ==
2428 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2429 _dbus_string_get_const_data (&str)));
2431 /* signed little pack */
2432 _dbus_pack_int32 (-0x123456,
2434 _dbus_string_get_data (&str));
2436 _dbus_assert (-0x123456 ==
2437 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2438 _dbus_string_get_const_data (&str)));
2440 /* signed big pack */
2441 _dbus_pack_int32 (-0x123456,
2443 _dbus_string_get_data (&str));
2445 _dbus_assert (-0x123456 ==
2446 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2447 _dbus_string_get_const_data (&str)));
2449 /* unsigned little */
2450 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
2453 _dbus_assert (0x123456 ==
2454 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2455 _dbus_string_get_const_data (&str)));
2458 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
2461 _dbus_assert (0x123456 ==
2462 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2463 _dbus_string_get_const_data (&str)));
2465 /* unsigned little pack */
2466 _dbus_pack_uint32 (0x123456,
2468 _dbus_string_get_data (&str));
2470 _dbus_assert (0x123456 ==
2471 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2472 _dbus_string_get_const_data (&str)));
2474 /* unsigned big pack */
2475 _dbus_pack_uint32 (0x123456,
2477 _dbus_string_get_data (&str));
2479 _dbus_assert (0x123456 ==
2480 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2481 _dbus_string_get_const_data (&str)));
2486 _dbus_string_set_length (&str, 0);
2488 _dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN,
2491 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2492 _dbus_assert (strcmp (s, "Hello world") == 0);
2495 _dbus_string_init_const (&t, "Hello world foo");
2497 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2498 &t, _dbus_string_get_length (&t));
2500 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2501 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2504 _dbus_string_init_const (&t, "Hello");
2506 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2507 &t, _dbus_string_get_length (&t));
2509 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2510 _dbus_assert (strcmp (s, "Hello") == 0);
2513 /* Strings (big endian) */
2515 _dbus_string_set_length (&str, 0);
2517 _dbus_marshal_string (&str, DBUS_BIG_ENDIAN,
2520 s = _dbus_demarshal_string (&str, DBUS_BIG_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_BIG_ENDIAN, 0,
2527 &t, _dbus_string_get_length (&t));
2529 s = _dbus_demarshal_string (&str, DBUS_BIG_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_BIG_ENDIAN, 0,
2536 &t, _dbus_string_get_length (&t));
2538 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2539 _dbus_assert (strcmp (s, "Hello") == 0);
2543 _dbus_string_free (&str);
2548 #endif /* DBUS_BUILD_TESTS */