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 2.0
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 */
77 * Union used to manipulate 8 bytes as if they
82 #ifdef DBUS_HAVE_INT64
83 dbus_int64_t s; /**< 64-bit integer */
84 dbus_uint64_t u; /**< 64-bit unsinged integer */
86 double d; /**< double */
90 unpack_8_octets (int byte_order,
91 const unsigned char *data)
95 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
96 _dbus_assert (sizeof (r) == 8);
98 #ifdef DBUS_HAVE_INT64
99 if (byte_order == DBUS_LITTLE_ENDIAN)
100 r.u = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
102 r.u = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
104 r.d = *(double*)data;
105 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
106 swap_bytes ((unsigned char*) &r, sizeof (r));
113 * Unpacks a 32 bit unsigned integer from a data pointer
115 * @param byte_order The byte order to use
116 * @param data the data pointer
117 * @returns the integer
120 _dbus_unpack_uint32 (int byte_order,
121 const unsigned char *data)
123 return unpack_4_octets (byte_order, data);
127 * Unpacks a 32 bit signed integer from a data pointer
129 * @param byte_order The byte order to use
130 * @param data the data pointer
131 * @returns the integer
134 _dbus_unpack_int32 (int byte_order,
135 const unsigned char *data)
137 return (dbus_int32_t) unpack_4_octets (byte_order, data);
140 #ifdef DBUS_HAVE_INT64
142 * Unpacks a 64 bit unsigned integer from a data pointer
144 * @param byte_order The byte order to use
145 * @param data the data pointer
146 * @returns the integer
149 _dbus_unpack_uint64 (int byte_order,
150 const unsigned char *data)
154 r = unpack_8_octets (byte_order, data);
160 * Unpacks a 64 bit signed integer from a data pointer
162 * @param byte_order The byte order to use
163 * @param data the data pointer
164 * @returns the integer
167 _dbus_unpack_int64 (int byte_order,
168 const unsigned char *data)
172 r = unpack_8_octets (byte_order, data);
177 #endif /* DBUS_HAVE_INT64 */
180 pack_4_octets (dbus_uint32_t value,
184 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
186 if ((byte_order) == DBUS_LITTLE_ENDIAN)
187 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
189 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
193 pack_8_octets (DBusOctets8 value,
197 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
199 #ifdef DBUS_HAVE_INT64
200 if ((byte_order) == DBUS_LITTLE_ENDIAN)
201 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u);
203 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u);
205 memcpy (data, &value, 8);
206 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
207 swap_bytes ((unsigned char *)data, 8);
212 * Packs a 32 bit unsigned integer into a data pointer.
214 * @param value the value
215 * @param byte_order the byte order to use
216 * @param data the data pointer
219 _dbus_pack_uint32 (dbus_uint32_t value,
223 pack_4_octets (value, byte_order, data);
227 * Packs a 32 bit signed integer into a data pointer.
229 * @param value the value
230 * @param byte_order the byte order to use
231 * @param data the data pointer
234 _dbus_pack_int32 (dbus_int32_t value,
238 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
241 #ifdef DBUS_HAVE_INT64
243 * Packs a 64 bit unsigned integer into a data pointer.
245 * @param value the value
246 * @param byte_order the byte order to use
247 * @param data the data pointer
250 _dbus_pack_uint64 (dbus_uint64_t value,
256 pack_8_octets (r, byte_order, data);
260 * Packs a 64 bit signed integer into a data pointer.
262 * @param value the value
263 * @param byte_order the byte order to use
264 * @param data the data pointer
267 _dbus_pack_int64 (dbus_int64_t value,
273 pack_8_octets (r, byte_order, data);
275 #endif /* DBUS_HAVE_INT64 */
278 set_4_octets (DBusString *str,
285 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
286 byte_order == DBUS_BIG_ENDIAN);
288 data = _dbus_string_get_data_len (str, offset, 4);
290 _dbus_pack_uint32 (value, byte_order, data);
294 set_8_octets (DBusString *str,
301 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
302 byte_order == DBUS_BIG_ENDIAN);
304 data = _dbus_string_get_data_len (str, offset, 8);
306 pack_8_octets (value, byte_order, data);
310 * Sets the 4 bytes at the given offset to a marshaled signed integer,
311 * replacing anything found there previously.
313 * @param str the string to write the marshalled int to
314 * @param offset the byte offset where int should be written
315 * @param byte_order the byte order to use
316 * @param value the value
320 _dbus_marshal_set_int32 (DBusString *str,
325 set_4_octets (str, byte_order, offset, (dbus_uint32_t) value);
329 * Sets the 4 bytes at the given offset to a marshaled unsigned
330 * integer, replacing anything found there previously.
332 * @param str the string to write the marshalled int to
333 * @param offset the byte offset where int should be written
334 * @param byte_order the byte order to use
335 * @param value the value
339 _dbus_marshal_set_uint32 (DBusString *str,
344 set_4_octets (str, byte_order, offset, value);
347 #ifdef DBUS_HAVE_INT64
350 * Sets the 8 bytes at the given offset to a marshaled signed integer,
351 * replacing anything found there previously.
353 * @param str the string to write the marshalled int to
354 * @param offset the byte offset where int should be written
355 * @param byte_order the byte order to use
356 * @param value the value
360 _dbus_marshal_set_int64 (DBusString *str,
367 set_8_octets (str, byte_order, offset, r);
371 * Sets the 8 bytes at the given offset to a marshaled unsigned
372 * integer, replacing anything found there previously.
374 * @param str the string to write the marshalled int to
375 * @param offset the byte offset where int should be written
376 * @param byte_order the byte order to use
377 * @param value the value
381 _dbus_marshal_set_uint64 (DBusString *str,
388 set_8_octets (str, byte_order, offset, r);
390 #endif /* DBUS_HAVE_INT64 */
393 * Sets the existing marshaled string at the given offset with
394 * a new marshaled string. The given offset must point to
395 * an existing string or the wrong length will be deleted
396 * and replaced with the new string.
398 * Note: no attempt is made by this function to re-align
399 * any data which has been already marshalled after this
400 * string. Use with caution.
402 * @param str the string to write the marshalled string to
403 * @param offset the byte offset where string should be written
404 * @param byte_order the byte order to use
405 * @param value the value
406 * @param len the length to use
407 * @returns #TRUE on success
411 _dbus_marshal_set_string (DBusString *str,
414 const DBusString *value,
419 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
420 byte_order == DBUS_BIG_ENDIAN);
422 old_len = _dbus_demarshal_uint32 (str, byte_order,
425 if (!_dbus_string_replace_len (value, 0, len,
426 str, offset + 4, old_len))
429 _dbus_marshal_set_uint32 (str, byte_order,
436 * Sets the existing marshaled object path at the given offset to a new
437 * value. The given offset must point to an existing object path or this
438 * function doesn't make sense.
440 * @todo implement this function
442 * @param str the string to write the marshalled path to
443 * @param offset the byte offset where path should be written
444 * @param byte_order the byte order to use
445 * @param path the new path
446 * @param path_len number of elements in the path
449 _dbus_marshal_set_object_path (DBusString *str,
460 marshal_4_octets (DBusString *str,
464 _dbus_assert (sizeof (value) == 4);
466 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
467 value = DBUS_UINT32_SWAP_LE_BE (value);
469 return _dbus_string_append_4_aligned (str,
470 (const unsigned char *)&value);
474 marshal_8_octets (DBusString *str,
478 _dbus_assert (sizeof (value) == 8);
480 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
481 pack_8_octets (value, byte_order, (unsigned char*) &value); /* pack into self, swapping as we go */
483 return _dbus_string_append_8_aligned (str,
484 (const unsigned char *)&value);
488 * Marshals a double value.
490 * @param str the string to append the marshalled value to
491 * @param byte_order the byte order to use
492 * @param value the value
493 * @returns #TRUE on success
496 _dbus_marshal_double (DBusString *str,
502 return marshal_8_octets (str, byte_order, r);
506 * Marshals a 32 bit signed integer value.
508 * @param str the string to append the marshalled value to
509 * @param byte_order the byte order to use
510 * @param value the value
511 * @returns #TRUE on success
514 _dbus_marshal_int32 (DBusString *str,
518 return marshal_4_octets (str, byte_order, (dbus_uint32_t) value);
522 * Marshals a 32 bit unsigned integer value.
524 * @param str the string to append the marshalled value to
525 * @param byte_order the byte order to use
526 * @param value the value
527 * @returns #TRUE on success
530 _dbus_marshal_uint32 (DBusString *str,
534 return marshal_4_octets (str, byte_order, value);
538 #ifdef DBUS_HAVE_INT64
540 * Marshals a 64 bit signed integer value.
542 * @param str the string to append the marshalled value to
543 * @param byte_order the byte order to use
544 * @param value the value
545 * @returns #TRUE on success
548 _dbus_marshal_int64 (DBusString *str,
554 return marshal_8_octets (str, byte_order, r);
558 * Marshals a 64 bit unsigned integer value.
560 * @param str the string to append the marshalled value to
561 * @param byte_order the byte order to use
562 * @param value the value
563 * @returns #TRUE on success
566 _dbus_marshal_uint64 (DBusString *str,
572 return marshal_8_octets (str, byte_order, r);
575 #endif /* DBUS_HAVE_INT64 */
578 * Marshals a UTF-8 string
580 * @todo: If the string append fails we need to restore
581 * the old length. (also for other marshallers)
583 * @param str the string to append the marshalled value to
584 * @param byte_order the byte order to use
585 * @param value the string
586 * @returns #TRUE on success
589 _dbus_marshal_string (DBusString *str,
593 int len, old_string_len;
595 old_string_len = _dbus_string_get_length (str);
597 len = strlen (value);
599 if (!_dbus_marshal_uint32 (str, byte_order, len))
601 /* Restore the previous length */
602 _dbus_string_set_length (str, old_string_len);
607 return _dbus_string_append_len (str, value, len + 1);
611 * Marshals a byte array
613 * @param str the string to append the marshalled value to
614 * @param byte_order the byte order to use
615 * @param value the array
616 * @param len number of elements in the array
617 * @returns #TRUE on success
620 _dbus_marshal_byte_array (DBusString *str,
622 const unsigned char *value,
627 old_string_len = _dbus_string_get_length (str);
629 if (!_dbus_marshal_uint32 (str, byte_order, len))
631 /* Restore the previous length */
632 _dbus_string_set_length (str, old_string_len);
640 return _dbus_string_append_len (str, value, len);
644 marshal_4_octets_array (DBusString *str,
646 const dbus_uint32_t *value,
652 old_string_len = _dbus_string_get_length (str);
654 if (!_dbus_marshal_uint32 (str, byte_order, len * 4))
657 array_start = _dbus_string_get_length (str);
659 if (!_dbus_string_append_len (str, (const unsigned char*) value,
663 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
665 const unsigned char *d;
666 const unsigned char *end;
668 d = _dbus_string_get_data (str) + array_start;
672 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
680 /* Restore previous length */
681 _dbus_string_set_length (str, old_string_len);
687 marshal_8_octets_array (DBusString *str,
689 const DBusOctets8 *value,
695 old_string_len = _dbus_string_get_length (str);
697 if (!_dbus_marshal_uint32 (str, byte_order, len * 8))
700 array_start = _dbus_string_get_length (str);
702 if (!_dbus_string_append_len (str, (const unsigned char*) value,
706 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
708 const unsigned char *d;
709 const unsigned char *end;
711 d = _dbus_string_get_data (str) + array_start;
715 #ifdef DBUS_HAVE_INT64
716 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
718 swap_bytes ((unsigned char*) d, 8);
727 /* Restore previous length */
728 _dbus_string_set_length (str, old_string_len);
734 * Marshals a 32 bit signed integer array
736 * @param str the string to append the marshalled value to
737 * @param byte_order the byte order to use
738 * @param value the array
739 * @param len the length of the array
740 * @returns #TRUE on success
743 _dbus_marshal_int32_array (DBusString *str,
745 const dbus_int32_t *value,
748 return marshal_4_octets_array (str, byte_order,
749 (const dbus_uint32_t*) value,
754 * Marshals a 32 bit unsigned integer array
756 * @param str the string to append the marshalled value to
757 * @param byte_order the byte order to use
758 * @param value the array
759 * @param len the length of the array
760 * @returns #TRUE on success
763 _dbus_marshal_uint32_array (DBusString *str,
765 const dbus_uint32_t *value,
768 return marshal_4_octets_array (str, byte_order,
773 #ifdef DBUS_HAVE_INT64
776 * Marshals a 64 bit signed integer array
778 * @param str the string to append the marshalled value to
779 * @param byte_order the byte order to use
780 * @param value the array
781 * @param len the length of the array
782 * @returns #TRUE on success
785 _dbus_marshal_int64_array (DBusString *str,
787 const dbus_int64_t *value,
790 return marshal_8_octets_array (str, byte_order,
791 (const DBusOctets8*) value,
796 * Marshals a 64 bit unsigned integer array
798 * @param str the string to append the marshalled value to
799 * @param byte_order the byte order to use
800 * @param value the array
801 * @param len the length of the array
802 * @returns #TRUE on success
805 _dbus_marshal_uint64_array (DBusString *str,
807 const dbus_uint64_t *value,
810 return marshal_8_octets_array (str, byte_order,
811 (const DBusOctets8*) value,
815 #endif /* DBUS_HAVE_INT64 */
818 * Marshals a double array
820 * @param str the string to append the marshalled value to
821 * @param byte_order the byte order to use
822 * @param value the array
823 * @param len the length of the array
824 * @returns #TRUE on success
827 _dbus_marshal_double_array (DBusString *str,
832 return marshal_8_octets_array (str, byte_order,
833 (const DBusOctets8*) value,
838 * Marshals a string array
840 * @param str the string to append the marshalled value to
841 * @param byte_order the byte order to use
842 * @param value the array
843 * @param len the length of the array
844 * @returns #TRUE on success
847 _dbus_marshal_string_array (DBusString *str,
852 int i, old_string_len, array_start;
854 old_string_len = _dbus_string_get_length (str);
856 /* Set the length to 0 temporarily */
857 if (!_dbus_marshal_uint32 (str, byte_order, 0))
860 array_start = _dbus_string_get_length (str);
862 for (i = 0; i < len; i++)
863 if (!_dbus_marshal_string (str, byte_order, value[i]))
866 /* Write the length now that we know it */
867 _dbus_marshal_set_uint32 (str, byte_order,
868 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
869 _dbus_string_get_length (str) - array_start);
874 /* Restore previous length */
875 _dbus_string_set_length (str, old_string_len);
881 * Marshals an object path value.
883 * @param str the string to append the marshalled value to
884 * @param byte_order the byte order to use
885 * @param path the path
886 * @param path_len length of the path
887 * @returns #TRUE on success
890 _dbus_marshal_object_path (DBusString *str,
895 int array_start, old_string_len;
898 old_string_len = _dbus_string_get_length (str);
900 /* Set the length to 0 temporarily */
901 if (!_dbus_marshal_uint32 (str, byte_order, 0))
904 array_start = _dbus_string_get_length (str);
909 if (!_dbus_string_append_byte (str, '/'))
912 if (!_dbus_string_append (str, path[0]))
918 /* Write the length now that we know it */
919 _dbus_marshal_set_uint32 (str, byte_order,
920 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
921 _dbus_string_get_length (str) - array_start);
926 /* Restore the previous length */
927 _dbus_string_set_length (str, old_string_len);
933 demarshal_4_octets (const DBusString *str,
938 const DBusRealString *real = (const DBusRealString*) str;
940 pos = _DBUS_ALIGN_VALUE (pos, 4);
945 return unpack_4_octets (byte_order, real->str + pos);
949 demarshal_8_octets (const DBusString *str,
954 const DBusRealString *real = (const DBusRealString*) str;
956 pos = _DBUS_ALIGN_VALUE (pos, 8);
961 return unpack_8_octets (byte_order, real->str + pos);
965 * Demarshals a double.
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 double.
974 _dbus_demarshal_double (const DBusString *str,
981 r = demarshal_8_octets (str, byte_order, pos, new_pos);
987 * Demarshals a 32 bit signed integer.
989 * @param str the string containing the data
990 * @param byte_order the byte order
991 * @param pos the position in the string
992 * @param new_pos the new position of the string
993 * @returns the demarshaled integer.
996 _dbus_demarshal_int32 (const DBusString *str,
1001 return (dbus_int32_t) demarshal_4_octets (str, byte_order, pos, new_pos);
1005 * Demarshals a 32 bit unsigned integer.
1007 * @param str the string containing the data
1008 * @param byte_order the byte order
1009 * @param pos the position in the string
1010 * @param new_pos the new position of the string
1011 * @returns the demarshaled integer.
1014 _dbus_demarshal_uint32 (const DBusString *str,
1019 return demarshal_4_octets (str, byte_order, pos, new_pos);
1022 #ifdef DBUS_HAVE_INT64
1025 * Demarshals a 64 bit signed integer.
1027 * @param str the string containing the data
1028 * @param byte_order the byte order
1029 * @param pos the position in the string
1030 * @param new_pos the new position of the string
1031 * @returns the demarshaled integer.
1034 _dbus_demarshal_int64 (const DBusString *str,
1041 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1047 * Demarshals a 64 bit unsigned integer.
1049 * @param str the string containing the data
1050 * @param byte_order the byte order
1051 * @param pos the position in the string
1052 * @param new_pos the new position of the string
1053 * @returns the demarshaled integer.
1056 _dbus_demarshal_uint64 (const DBusString *str,
1063 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1068 #endif /* DBUS_HAVE_INT64 */
1071 * Demarshals a basic type
1073 * @param str the string containing the data
1074 * @param type type of value to demarshal
1075 * @param value pointer to return value data
1076 * @param byte_order the byte order
1077 * @param pos pointer to position in the string,
1078 * updated on return to new position
1081 _dbus_demarshal_basic_type (const DBusString *str,
1087 const char *str_data = _dbus_string_get_const_data (str);
1091 case DBUS_TYPE_BYTE:
1092 case DBUS_TYPE_BOOLEAN:
1093 *(unsigned char *) value = _dbus_string_get_byte (str, *pos);
1096 case DBUS_TYPE_INT32:
1097 case DBUS_TYPE_UINT32:
1098 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1099 *(dbus_uint32_t *) value = *(dbus_uint32_t *)(str_data + *pos);
1100 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1101 *(dbus_uint32_t *) value = DBUS_UINT32_SWAP_LE_BE (*(dbus_uint32_t *) value);
1104 #ifdef DBUS_HAVE_INT64
1105 case DBUS_TYPE_INT64:
1106 case DBUS_TYPE_UINT64:
1107 #endif /* DBUS_HAVE_INT64 */
1108 case DBUS_TYPE_DOUBLE:
1109 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1110 memcpy (value, str_data + *pos, 8);
1111 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1112 #ifdef DBUS_HAVE_INT64
1113 *(dbus_uint64_t *) value = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t *) value);
1115 swap_bytes (value, 8);
1120 _dbus_assert_not_reached ("not a basic type");
1126 * Demarshals an UTF-8 string.
1128 * @todo Should we check the string to make sure
1129 * that it's valid UTF-8, and maybe "fix" the string
1132 * @todo Should probably demarshal to a DBusString,
1133 * having memcpy() in here is Evil(tm).
1135 * @param str the string containing the data
1136 * @param byte_order the byte order
1137 * @param pos the position in the string
1138 * @param new_pos the new position of the string
1139 * @returns the demarshaled string.
1142 _dbus_demarshal_string (const DBusString *str,
1151 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1153 retval = dbus_malloc (len + 1);
1158 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1163 memcpy (retval, data, len + 1);
1166 *new_pos = pos + len + 1;
1172 * Demarshals a byte array.
1174 * @todo Should probably demarshal to a DBusString,
1175 * having memcpy() in here is Evil(tm).
1177 * @param str the string containing the data
1178 * @param byte_order the byte order
1179 * @param pos the position in the string
1180 * @param new_pos the new position of the string
1181 * @param array the array
1182 * @param array_len length of the demarshaled data
1184 * @returns #TRUE on success
1187 _dbus_demarshal_byte_array (const DBusString *str,
1191 unsigned char **array,
1195 unsigned char *retval;
1198 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1211 retval = dbus_malloc (len);
1216 data = _dbus_string_get_const_data_len (str, pos, len);
1224 memcpy (retval, data, len);
1227 *new_pos = pos + len;
1236 demarshal_4_octets_array (const DBusString *str,
1240 dbus_uint32_t **array,
1244 dbus_uint32_t *retval;
1247 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1261 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1265 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1267 for (i = 0; i < len; i++)
1268 retval[i] = DBUS_UINT32_SWAP_LE_BE (retval[i]);
1272 *new_pos = pos + byte_len;
1281 demarshal_8_octets_array (const DBusString *str,
1285 DBusOctets8 **array,
1289 DBusOctets8 *retval;
1292 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1306 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1310 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1312 for (i = 0; i < len; i++)
1314 #ifdef DBUS_HAVE_INT64
1315 retval[i].u = DBUS_UINT64_SWAP_LE_BE (retval[i].u);
1317 swap_bytes ((unsigned char *) &retval[i], 8);
1323 *new_pos = pos + byte_len;
1332 * Demarshals a 32 bit signed integer array.
1334 * @param str the string containing the data
1335 * @param byte_order the byte order
1336 * @param pos the position in the string
1337 * @param new_pos the new position of the string
1338 * @param array the array
1339 * @param array_len length of the demarshaled data
1340 * @returns #TRUE on success
1343 _dbus_demarshal_int32_array (const DBusString *str,
1347 dbus_int32_t **array,
1350 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1351 (dbus_uint32_t**) array, array_len);
1355 * Demarshals a 32 bit unsigned integer array.
1357 * @param str the string containing the data
1358 * @param byte_order the byte order
1359 * @param pos the position in the string
1360 * @param new_pos the new position of the string
1361 * @param array the array
1362 * @param array_len length of the demarshaled data
1363 * @returns #TRUE on success
1366 _dbus_demarshal_uint32_array (const DBusString *str,
1370 dbus_uint32_t **array,
1373 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1377 #ifdef DBUS_HAVE_INT64
1380 * Demarshals a 64 bit signed integer array.
1382 * @param str the string containing the data
1383 * @param byte_order the byte order
1384 * @param pos the position in the string
1385 * @param new_pos the new position of the string
1386 * @param array the array
1387 * @param array_len length of the demarshaled data
1388 * @returns #TRUE on success
1391 _dbus_demarshal_int64_array (const DBusString *str,
1395 dbus_int64_t **array,
1398 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1399 (DBusOctets8**) array, array_len);
1403 * Demarshals a 64 bit unsigned integer array.
1405 * @param str the string containing the data
1406 * @param byte_order the byte order
1407 * @param pos the position in the string
1408 * @param new_pos the new position of the string
1409 * @param array the array
1410 * @param array_len length of the demarshaled data
1411 * @returns #TRUE on success
1414 _dbus_demarshal_uint64_array (const DBusString *str,
1418 dbus_uint64_t **array,
1421 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1422 (DBusOctets8**) array, array_len);
1425 #endif /* DBUS_HAVE_INT64 */
1428 * Demarshals a double array.
1430 * @param str the string containing the data
1431 * @param byte_order the byte order
1432 * @param pos the position in the string
1433 * @param new_pos the new position of the string
1434 * @param array the array
1435 * @param array_len length of the demarshaled data
1436 * @returns #TRUE on success
1439 _dbus_demarshal_double_array (const DBusString *str,
1446 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1447 (DBusOctets8**) array, array_len);
1452 * Demarshals an array of basic types
1454 * @param str the string containing the data
1455 * @param element_type type of array elements to demarshal
1456 * @param array pointer to pointer to array data
1457 * @param array_len pointer to array length
1458 * @param byte_order the byte order
1459 * @param pos pointer to position in the string,
1460 * updated on return to new position
1463 _dbus_demarshal_basic_type_array (const DBusString *str,
1470 switch (element_type)
1472 case DBUS_TYPE_BOOLEAN:
1473 /* FIXME: do we want to post-normalize these ? */
1474 case DBUS_TYPE_BYTE:
1475 return _dbus_demarshal_byte_array (str, byte_order, *pos, pos,
1476 (unsigned char **)array, array_len);
1478 case DBUS_TYPE_INT32:
1479 case DBUS_TYPE_UINT32:
1480 return demarshal_4_octets_array (str, byte_order, *pos, pos,
1481 (dbus_uint32_t *) array, array_len);
1483 #ifdef DBUS_HAVE_INT64
1484 case DBUS_TYPE_INT64:
1485 case DBUS_TYPE_UINT64:
1486 #endif /* DBUS_HAVE_INT64 */
1487 case DBUS_TYPE_DOUBLE:
1488 return demarshal_8_octets_array (str, byte_order, *pos, pos,
1489 (DBusOctets8**) array, array_len);
1491 _dbus_assert_not_reached ("not a basic type");
1498 * Demarshals a string array.
1500 * @param str the string containing the data
1501 * @param byte_order the byte order
1502 * @param pos the position in the string
1503 * @param new_pos the new position of the string
1504 * @param array the array
1505 * @param array_len location for length of the demarshaled data or NULL
1506 * @returns #TRUE on success
1509 _dbus_demarshal_string_array (const DBusString *str,
1521 bytes_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1536 end_pos = pos + bytes_len;
1538 retval = dbus_new (char *, allocated);
1543 while (pos < end_pos)
1545 retval[len] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1547 if (retval[len] == NULL)
1552 if (len >= allocated - 1) /* -1 for NULL termination */
1555 newp = dbus_realloc (retval,
1556 sizeof (char*) * allocated * 2);
1576 for (i = 0; i < len; i++)
1577 dbus_free (retval[i]);
1583 /** Set to 1 to get a bunch of spew about disassembling the path string */
1584 #define VERBOSE_DECOMPOSE 0
1587 * Demarshals an object path. A path of just "/" is
1588 * represented as an empty vector of strings.
1590 * @param str the string containing the data
1591 * @param byte_order the byte order
1592 * @param pos the position in the string
1593 * @param new_pos the new position of the string
1594 * @param path address to store new object path
1595 * @param path_len length of stored path
1598 _dbus_demarshal_object_path (const DBusString *str,
1611 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1612 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1613 _dbus_assert (data != NULL);
1615 #if VERBOSE_DECOMPOSE
1616 _dbus_verbose ("Decomposing path \"%s\"\n",
1629 retval = dbus_new0 (char*, n_components + 1);
1642 while (j < len && data[j] != '/')
1645 /* Now [i, j) is the path component */
1646 _dbus_assert (i < j);
1647 _dbus_assert (data[i] != '/');
1648 _dbus_assert (j == len || data[j] == '/');
1650 #if VERBOSE_DECOMPOSE
1651 _dbus_verbose (" (component in [%d,%d))\n",
1655 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1656 if (retval[comp] == NULL)
1658 dbus_free_string_array (retval);
1661 retval[comp][j-i] = '\0';
1662 #if VERBOSE_DECOMPOSE
1663 _dbus_verbose (" (component %d = \"%s\")\n",
1664 comp, retval[comp]);
1670 _dbus_assert (i == len);
1674 *path_len = n_components;
1677 *new_pos = pos + len + 1;
1683 * Returns the position right after the end of an argument. PERFORMS
1684 * NO VALIDATION WHATSOEVER. The message must have been previously
1687 * @param str a string
1688 * @param byte_order the byte order to use
1689 * @param type the type of the argument
1690 * @param pos the pos where the arg starts
1691 * @param end_pos pointer where the position right
1692 * after the end position will follow
1693 * @returns TRUE if more data exists after the arg
1696 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1702 if (pos >= _dbus_string_get_length (str))
1707 case DBUS_TYPE_INVALID:
1715 case DBUS_TYPE_BYTE:
1719 case DBUS_TYPE_BOOLEAN:
1723 case DBUS_TYPE_INT32:
1724 case DBUS_TYPE_UINT32:
1725 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
1728 case DBUS_TYPE_INT64:
1729 case DBUS_TYPE_UINT64:
1730 case DBUS_TYPE_DOUBLE:
1732 *end_pos = _DBUS_ALIGN_VALUE (pos, 8) + 8;
1735 case DBUS_TYPE_OBJECT_PATH:
1736 case DBUS_TYPE_STRING:
1740 /* Demarshal the length */
1741 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1743 *end_pos = pos + len + 1;
1747 case DBUS_TYPE_CUSTOM:
1751 /* Demarshal the string length */
1752 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1756 /* Demarshal the data length */
1757 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1759 *end_pos = pos + len;
1763 case DBUS_TYPE_ARRAY:
1767 /* Demarshal the length */
1768 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1770 *end_pos = pos + len;
1774 case DBUS_TYPE_DICT:
1778 /* Demarshal the length */
1779 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1781 *end_pos = pos + len;
1786 _dbus_warn ("Unknown message arg type %d\n", type);
1787 _dbus_assert_not_reached ("Unknown message argument type\n");
1791 if (*end_pos > _dbus_string_get_length (str))
1798 * Demarshals and validates a length; returns < 0 if the validation
1799 * fails. The length is required to be small enough that
1800 * len*sizeof(double) will not overflow, and small enough to fit in a
1801 * signed integer. DOES NOT check whether the length points
1802 * beyond the end of the string, because it doesn't know the
1803 * size of array elements.
1805 * @param str the string
1806 * @param byte_order the byte order
1807 * @param pos the unaligned string position (snap to next aligned)
1808 * @param new_pos return location for new position.
1811 demarshal_and_validate_len (const DBusString *str,
1816 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1819 _dbus_assert (new_pos != NULL);
1821 if ((align_4 + 4) > _dbus_string_get_length (str))
1823 _dbus_verbose ("not enough room in message for array length\n");
1827 if (!_dbus_string_validate_nul (str, pos,
1830 _dbus_verbose ("array length alignment padding not initialized to nul at %d\n", pos);
1834 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1836 /* note that the len is the number of bytes, so we need it to be
1837 * at least SIZE_T_MAX, but make it smaller just to keep things
1838 * sane. We end up using ints for most sizes to avoid unsigned mess
1839 * so limit to maximum 32-bit signed int divided by at least 8, more
1840 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1842 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1843 if (len > MAX_ARRAY_LENGTH)
1845 _dbus_verbose ("array length %u exceeds maximum of %u at pos %d\n",
1846 len, MAX_ARRAY_LENGTH, pos);
1854 validate_string (const DBusString *str,
1856 int len_without_nul,
1859 *end_pos = pos + len_without_nul + 1;
1861 if (*end_pos > _dbus_string_get_length (str))
1863 _dbus_verbose ("string length outside length of the message\n");
1867 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1869 _dbus_verbose ("string arg not nul-terminated\n");
1873 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1875 _dbus_verbose ("string is not valid UTF-8\n");
1883 * Validates and returns a typecode at a specific position
1886 * @param str a string
1887 * @param type the type of the argument
1888 * @param pos the pos where the typecode starts
1889 * @param end_pos pointer where the position right
1890 * after the end position will follow
1891 * @returns #TRUE if the type is valid.
1894 _dbus_marshal_validate_type (const DBusString *str,
1901 if (pos >= _dbus_string_get_length (str))
1904 data = _dbus_string_get_const_data_len (str, pos, 1);
1906 if (_dbus_type_is_valid (*data))
1909 if (end_pos != NULL)
1914 _dbus_verbose ("'%c' %d invalid type code\n", (int) *data, (int) *data);
1919 /* Faster validator for array data that doesn't call
1920 * validate_arg for each value
1923 validate_array_data (const DBusString *str,
1934 case DBUS_TYPE_INVALID:
1941 case DBUS_TYPE_OBJECT_PATH:
1942 case DBUS_TYPE_STRING:
1943 case DBUS_TYPE_CUSTOM:
1944 case DBUS_TYPE_ARRAY:
1945 case DBUS_TYPE_DICT:
1946 /* This clean recursion to validate_arg is what we
1947 * are doing logically for all types, but we don't
1948 * really want to call validate_arg for every byte
1949 * in a byte array, so the primitive types are
1954 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1955 type, array_type_pos, pos, &pos))
1960 case DBUS_TYPE_BYTE:
1964 case DBUS_TYPE_BOOLEAN:
1969 c = _dbus_string_get_byte (str, pos);
1971 if (!(c == 0 || c == 1))
1973 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1981 case DBUS_TYPE_INT32:
1982 case DBUS_TYPE_UINT32:
1983 /* Call validate arg one time to check alignment padding
1986 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1987 type, array_type_pos, pos, &pos))
1989 pos = _DBUS_ALIGN_VALUE (end, 4);
1992 case DBUS_TYPE_INT64:
1993 case DBUS_TYPE_UINT64:
1994 case DBUS_TYPE_DOUBLE:
1995 /* Call validate arg one time to check alignment padding
1998 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1999 type, array_type_pos, pos, &pos))
2001 pos = _DBUS_ALIGN_VALUE (end, 8);
2005 _dbus_verbose ("Unknown message arg type %d\n", type);
2015 * Validates an argument of a specific type, checking that it
2016 * is well-formed, for example no ludicrous length fields, strings
2017 * are nul-terminated, etc.
2018 * Returns the end position of the argument in end_pos, and
2019 * returns #TRUE if a valid arg begins at "pos"
2021 * @todo security: need to audit this function.
2023 * @param str a string
2024 * @param byte_order the byte order to use
2025 * @param depth current recursion depth, to prevent excessive recursion
2026 * @param type the type of the argument
2027 * @param array_type_pos the position of the current array type, or
2028 * -1 if not in an array
2029 * @param pos the pos where the arg starts
2030 * @param end_pos pointer where the position right
2031 * after the end position will follow
2032 * @returns #TRUE if the arg is valid.
2035 _dbus_marshal_validate_arg (const DBusString *str,
2043 if (pos > _dbus_string_get_length (str))
2045 _dbus_verbose ("Validation went off the end of the message\n");
2049 #define MAX_VALIDATION_DEPTH 32
2051 if (depth > MAX_VALIDATION_DEPTH)
2053 _dbus_verbose ("Maximum recursion depth reached validating message\n");
2059 case DBUS_TYPE_INVALID:
2067 case DBUS_TYPE_BYTE:
2068 if (1 > _dbus_string_get_length (str) - pos)
2070 _dbus_verbose ("no room for byte value\n");
2077 case DBUS_TYPE_BOOLEAN:
2081 if (1 > _dbus_string_get_length (str) - pos)
2083 _dbus_verbose ("no room for boolean value\n");
2087 c = _dbus_string_get_byte (str, pos);
2089 if (!(c == 0 || c == 1))
2091 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
2099 case DBUS_TYPE_INT32:
2100 case DBUS_TYPE_UINT32:
2102 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
2104 if (!_dbus_string_validate_nul (str, pos,
2107 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
2111 *end_pos = align_4 + 4;
2115 case DBUS_TYPE_INT64:
2116 case DBUS_TYPE_UINT64:
2117 case DBUS_TYPE_DOUBLE:
2119 int align_8 = _DBUS_ALIGN_VALUE (pos, 8);
2121 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
2123 if (!_dbus_string_validate_nul (str, pos,
2126 _dbus_verbose ("double/int64/uint64/objid alignment padding not initialized to nul at %d\n", pos);
2130 *end_pos = align_8 + 8;
2134 case DBUS_TYPE_OBJECT_PATH:
2135 case DBUS_TYPE_STRING:
2139 /* Demarshal the length, which does NOT include
2142 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2146 if (!validate_string (str, pos, len, end_pos))
2149 if (type == DBUS_TYPE_OBJECT_PATH)
2151 if (!_dbus_string_validate_path (str, pos, len))
2157 case DBUS_TYPE_CUSTOM:
2161 /* Demarshal the string length, which does NOT include
2164 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2168 if (!validate_string (str, pos, len, &pos))
2172 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2176 *end_pos = pos + len;
2180 case DBUS_TYPE_ARRAY:
2186 if (array_type_pos == -1)
2188 array_type_pos = pos;
2192 if (!_dbus_marshal_validate_type (str, pos, &array_type, &pos))
2194 _dbus_verbose ("invalid array type\n");
2198 /* NIL values take up no space, so you couldn't iterate over an array of them.
2199 * array of nil seems useless anyway; the useful thing might be array of
2200 * (nil OR string) but we have no framework for that.
2202 if (array_type == DBUS_TYPE_NIL)
2204 _dbus_verbose ("array of NIL is not allowed\n");
2208 while (array_type == DBUS_TYPE_ARRAY);
2213 if (!_dbus_marshal_validate_type (str, array_type_pos, &array_type, NULL))
2215 _dbus_verbose ("invalid array type\n");
2219 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2222 _dbus_verbose ("invalid array length (<0)\n");
2226 if (len > _dbus_string_get_length (str) - pos)
2228 _dbus_verbose ("array length outside length of the message\n");
2234 if (len > 0 && !validate_array_data (str, byte_order, depth + 1,
2235 array_type, array_type_pos,
2238 _dbus_verbose ("invalid array data\n");
2244 /* This should not be able to happen, as long as validate_arg moves forward;
2245 * but the check is here just to be paranoid.
2247 _dbus_verbose ("array length %d specified was longer than actual array contents by %d\n",
2254 _dbus_verbose ("array contents exceeds array length %d by %d\n", len, pos - end);
2262 case DBUS_TYPE_DICT:
2268 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2272 if (len > _dbus_string_get_length (str) - pos)
2274 _dbus_verbose ("dict length outside length of the message\n");
2283 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2284 DBUS_TYPE_STRING, -1, pos, &pos))
2287 if (!_dbus_marshal_validate_type (str, pos, &dict_type, &pos))
2289 _dbus_verbose ("invalid dict entry type at offset %d\n", pos);
2293 /* Validate element */
2294 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2295 dict_type, -1, pos, &pos))
2297 _dbus_verbose ("dict arg invalid at offset %d\n", pos);
2304 _dbus_verbose ("dict contents exceed stated dict length\n");
2313 _dbus_verbose ("Unknown message arg type %d\n", type);
2317 if (*end_pos > _dbus_string_get_length (str))
2324 * Return #TRUE if the typecode is a valid typecode
2326 * @returns #TRUE if valid
2329 _dbus_type_is_valid (int typecode)
2334 case DBUS_TYPE_BYTE:
2335 case DBUS_TYPE_BOOLEAN:
2336 case DBUS_TYPE_INT32:
2337 case DBUS_TYPE_UINT32:
2338 case DBUS_TYPE_INT64:
2339 case DBUS_TYPE_UINT64:
2340 case DBUS_TYPE_DOUBLE:
2341 case DBUS_TYPE_STRING:
2342 case DBUS_TYPE_CUSTOM:
2343 case DBUS_TYPE_ARRAY:
2344 case DBUS_TYPE_DICT:
2345 case DBUS_TYPE_OBJECT_PATH:
2354 * If in verbose mode, print a block of binary data.
2356 * @todo right now it prints even if not in verbose mode
2358 * @param data the data
2359 * @param len the length of the data
2362 _dbus_verbose_bytes (const unsigned char *data,
2366 const unsigned char *aligned;
2368 _dbus_assert (len >= 0);
2370 /* Print blanks on first row if appropriate */
2371 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
2374 _dbus_assert (aligned <= data);
2376 if (aligned != data)
2378 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
2379 while (aligned != data)
2381 _dbus_verbose (" ");
2386 /* now print the bytes */
2390 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2392 _dbus_verbose ("%4d\t%p: ",
2396 if (data[i] >= 32 &&
2398 _dbus_verbose (" '%c' ", data[i]);
2400 _dbus_verbose ("0x%s%x ",
2401 data[i] <= 0xf ? "0" : "", data[i]);
2405 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2408 _dbus_verbose ("BE: %d LE: %d",
2409 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
2410 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
2413 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
2415 _dbus_verbose (" dbl: %g",
2416 *(double*)&data[i-8]);
2419 _dbus_verbose ("\n");
2423 _dbus_verbose ("\n");
2427 * Dump the given part of the string to verbose log.
2429 * @param str the string
2430 * @param start the start of range to dump
2431 * @param len length of range
2434 _dbus_verbose_bytes_of_string (const DBusString *str,
2441 real_len = _dbus_string_get_length (str);
2443 _dbus_assert (start >= 0);
2445 if (start > real_len)
2447 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
2448 start, len, real_len);
2452 if ((start + len) > real_len)
2454 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
2455 start, len, real_len);
2456 len = real_len - start;
2459 d = _dbus_string_get_const_data_len (str, start, len);
2461 _dbus_verbose_bytes (d, len);
2465 * Marshals a basic type
2467 * @param str string to marshal to
2468 * @param type type of value
2469 * @param value pointer to value
2470 * @param byte_order byte order
2471 * @returns #TRUE on success
2474 _dbus_marshal_basic_type (DBusString *str,
2483 case DBUS_TYPE_BYTE:
2484 case DBUS_TYPE_BOOLEAN:
2485 retval = _dbus_string_append_byte (str, *(unsigned char *)value);
2487 case DBUS_TYPE_INT32:
2488 case DBUS_TYPE_UINT32:
2489 return marshal_4_octets (str, byte_order, *(dbus_uint32_t *)value);
2491 #ifdef DBUS_HAVE_INT64
2492 case DBUS_TYPE_INT64:
2493 case DBUS_TYPE_UINT64:
2494 retval = _dbus_marshal_uint64 (str, byte_order, *(dbus_uint64_t *)value);
2496 #endif /* DBUS_HAVE_INT64 */
2497 case DBUS_TYPE_DOUBLE:
2498 retval = _dbus_marshal_double (str, byte_order, *(double *)value);
2501 _dbus_assert_not_reached ("not a basic type");
2509 * Marshals a basic type array
2511 * @param str string to marshal to
2512 * @param element_type type of array elements
2513 * @param value pointer to value
2514 * @param len length of value data in elements
2515 * @param byte_order byte order
2516 * @returns #TRUE on success
2519 _dbus_marshal_basic_type_array (DBusString *str,
2525 switch (element_type)
2527 case DBUS_TYPE_BOOLEAN:
2528 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
2529 * should we here too ? */
2530 case DBUS_TYPE_BYTE:
2531 return _dbus_marshal_byte_array (str, byte_order, value, len);
2533 case DBUS_TYPE_INT32:
2534 case DBUS_TYPE_UINT32:
2535 return marshal_4_octets_array (str, byte_order, value, len);
2537 #ifdef DBUS_HAVE_INT64
2538 case DBUS_TYPE_INT64:
2539 case DBUS_TYPE_UINT64:
2540 #endif /* DBUS_HAVE_INT64 */
2541 case DBUS_TYPE_DOUBLE:
2542 return marshal_8_octets_array (str, byte_order, value, len);
2545 _dbus_assert_not_reached ("non basic type in array");
2553 #ifdef DBUS_BUILD_TESTS
2554 #include "dbus-test.h"
2558 _dbus_marshal_test (void)
2563 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
2564 #ifdef DBUS_HAVE_INT64
2565 dbus_int64_t array3[3] = { 0x123ffffffff, 0x456ffffffff, 0x789ffffffff }, *array4;
2570 if (!_dbus_string_init (&str))
2571 _dbus_assert_not_reached ("failed to init string");
2573 /* Marshal doubles */
2574 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2575 _dbus_assert_not_reached ("could not marshal double value");
2576 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2577 _dbus_assert_not_reached ("demarshal failed");
2579 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2580 _dbus_assert_not_reached ("could not marshal double value");
2581 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2582 _dbus_assert_not_reached ("demarshal failed");
2584 /* Marshal signed integers */
2585 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2586 _dbus_assert_not_reached ("could not marshal signed integer value");
2587 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2588 _dbus_assert_not_reached ("demarshal failed");
2590 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2591 _dbus_assert_not_reached ("could not marshal signed integer value");
2592 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2593 _dbus_assert_not_reached ("demarshal failed");
2595 /* Marshal unsigned integers */
2596 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2597 _dbus_assert_not_reached ("could not marshal signed integer value");
2598 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2599 _dbus_assert_not_reached ("demarshal failed");
2601 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2602 _dbus_assert_not_reached ("could not marshal signed integer value");
2603 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2604 _dbus_assert_not_reached ("demarshal failed");
2606 #ifdef DBUS_HAVE_INT64
2607 /* Marshal signed integers */
2608 if (!_dbus_marshal_int64 (&str, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2609 _dbus_assert_not_reached ("could not marshal signed integer value");
2610 if (!_dbus_demarshal_int64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2611 _dbus_assert_not_reached ("demarshal failed");
2613 if (!_dbus_marshal_int64 (&str, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2614 _dbus_assert_not_reached ("could not marshal signed integer value");
2615 if (!_dbus_demarshal_int64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2616 _dbus_assert_not_reached ("demarshal failed");
2618 /* Marshal unsigned integers */
2619 if (!_dbus_marshal_uint64 (&str, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2620 _dbus_assert_not_reached ("could not marshal signed integer value");
2621 if (!(_dbus_demarshal_uint64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2622 _dbus_assert_not_reached ("demarshal failed");
2624 if (!_dbus_marshal_uint64 (&str, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2625 _dbus_assert_not_reached ("could not marshal signed integer value");
2626 if (!(_dbus_demarshal_uint64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2627 _dbus_assert_not_reached ("demarshal failed");
2628 #endif /* DBUS_HAVE_INT64 */
2630 /* Marshal strings */
2631 tmp1 = "This is the dbus test string";
2632 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2633 _dbus_assert_not_reached ("could not marshal string");
2634 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2635 if (!strcmp (tmp1, tmp2) == 0)
2636 _dbus_assert_not_reached ("demarshal failed");
2639 tmp1 = "This is the dbus test string";
2640 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2641 _dbus_assert_not_reached ("could not marshal string");
2642 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2643 if (!strcmp (tmp1, tmp2) == 0)
2644 _dbus_assert_not_reached ("demarshal failed");
2647 /* Marshal signed integer arrays */
2648 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2649 _dbus_assert_not_reached ("could not marshal integer array");
2650 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2651 _dbus_assert_not_reached ("could not demarshal integer array");
2654 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2657 #ifdef DBUS_HAVE_INT64
2658 /* Marshal 64-bit signed integer arrays */
2659 if (!_dbus_marshal_int64_array (&str, DBUS_BIG_ENDIAN, array3, 3))
2660 _dbus_assert_not_reached ("could not marshal integer array");
2661 if (!_dbus_demarshal_int64_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array4, &len))
2662 _dbus_assert_not_reached ("could not demarshal integer array");
2665 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2668 /* set/pack 64-bit integers */
2669 _dbus_string_set_length (&str, 8);
2672 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
2673 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2675 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2676 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2677 _dbus_string_get_const_data (&str)));
2680 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
2681 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2683 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2684 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2685 _dbus_string_get_const_data (&str)));
2687 /* signed little pack */
2688 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2690 _dbus_string_get_data (&str));
2692 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2693 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2694 _dbus_string_get_const_data (&str)));
2696 /* signed big pack */
2697 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2699 _dbus_string_get_data (&str));
2701 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2702 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2703 _dbus_string_get_const_data (&str)));
2705 /* unsigned little */
2706 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
2707 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2709 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2710 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2711 _dbus_string_get_const_data (&str)));
2714 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
2715 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2717 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2718 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2719 _dbus_string_get_const_data (&str)));
2721 /* unsigned little pack */
2722 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2724 _dbus_string_get_data (&str));
2726 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2727 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2728 _dbus_string_get_const_data (&str)));
2730 /* unsigned big pack */
2731 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2733 _dbus_string_get_data (&str));
2735 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2736 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2737 _dbus_string_get_const_data (&str)));
2741 /* set/pack 32-bit integers */
2742 _dbus_string_set_length (&str, 4);
2745 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
2748 _dbus_assert (-0x123456 ==
2749 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2750 _dbus_string_get_const_data (&str)));
2753 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
2756 _dbus_assert (-0x123456 ==
2757 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2758 _dbus_string_get_const_data (&str)));
2760 /* signed little pack */
2761 _dbus_pack_int32 (-0x123456,
2763 _dbus_string_get_data (&str));
2765 _dbus_assert (-0x123456 ==
2766 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2767 _dbus_string_get_const_data (&str)));
2769 /* signed big pack */
2770 _dbus_pack_int32 (-0x123456,
2772 _dbus_string_get_data (&str));
2774 _dbus_assert (-0x123456 ==
2775 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2776 _dbus_string_get_const_data (&str)));
2778 /* unsigned little */
2779 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
2782 _dbus_assert (0x123456 ==
2783 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2784 _dbus_string_get_const_data (&str)));
2787 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
2790 _dbus_assert (0x123456 ==
2791 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2792 _dbus_string_get_const_data (&str)));
2794 /* unsigned little pack */
2795 _dbus_pack_uint32 (0x123456,
2797 _dbus_string_get_data (&str));
2799 _dbus_assert (0x123456 ==
2800 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2801 _dbus_string_get_const_data (&str)));
2803 /* unsigned big pack */
2804 _dbus_pack_uint32 (0x123456,
2806 _dbus_string_get_data (&str));
2808 _dbus_assert (0x123456 ==
2809 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2810 _dbus_string_get_const_data (&str)));
2815 _dbus_string_set_length (&str, 0);
2817 _dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN,
2820 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2821 _dbus_assert (strcmp (s, "Hello world") == 0);
2824 _dbus_string_init_const (&t, "Hello world foo");
2826 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2827 &t, _dbus_string_get_length (&t));
2829 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2830 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2833 _dbus_string_init_const (&t, "Hello");
2835 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2836 &t, _dbus_string_get_length (&t));
2838 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2839 _dbus_assert (strcmp (s, "Hello") == 0);
2842 /* Strings (big endian) */
2844 _dbus_string_set_length (&str, 0);
2846 _dbus_marshal_string (&str, DBUS_BIG_ENDIAN,
2849 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2850 _dbus_assert (strcmp (s, "Hello world") == 0);
2853 _dbus_string_init_const (&t, "Hello world foo");
2855 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2856 &t, _dbus_string_get_length (&t));
2858 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2859 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2862 _dbus_string_init_const (&t, "Hello");
2864 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2865 &t, _dbus_string_get_length (&t));
2867 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2868 _dbus_assert (strcmp (s, "Hello") == 0);
2871 _dbus_string_free (&str);
2876 #endif /* DBUS_BUILD_TESTS */