1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.c Marshalling routines
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003, 2004 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-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 UTF-8 string
613 * @todo: If the string append fails we need to restore
614 * the old length. (also for other marshallers)
616 * @param str the string to append the marshalled value to
617 * @param byte_order the byte order to use
618 * @param value the string
619 * @param len length of string to marshal in bytes
620 * @returns #TRUE on success
623 _dbus_marshal_string_len (DBusString *str,
630 old_string_len = _dbus_string_get_length (str);
632 if (!_dbus_marshal_uint32 (str, byte_order, len))
634 /* Restore the previous length */
635 _dbus_string_set_length (str, old_string_len);
640 if (!_dbus_string_append_len (str, value, len))
644 if (!_dbus_string_lengthen (str, 1))
651 * Marshals a byte array
653 * @param str the string to append the marshalled value to
654 * @param byte_order the byte order to use
655 * @param value the array
656 * @param len number of elements in the array
657 * @returns #TRUE on success
660 _dbus_marshal_byte_array (DBusString *str,
662 const unsigned char *value,
667 old_string_len = _dbus_string_get_length (str);
669 if (!_dbus_marshal_uint32 (str, byte_order, len))
671 /* Restore the previous length */
672 _dbus_string_set_length (str, old_string_len);
680 return _dbus_string_append_len (str, value, len);
684 marshal_4_octets_array (DBusString *str,
686 const dbus_uint32_t *value,
692 old_string_len = _dbus_string_get_length (str);
694 if (!_dbus_marshal_uint32 (str, byte_order, len * 4))
697 array_start = _dbus_string_get_length (str);
699 if (!_dbus_string_append_len (str, (const unsigned char*) value,
703 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
705 const unsigned char *d;
706 const unsigned char *end;
708 d = _dbus_string_get_data (str) + array_start;
712 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
720 /* Restore previous length */
721 _dbus_string_set_length (str, old_string_len);
727 marshal_8_octets_array (DBusString *str,
729 const DBusOctets8 *value,
735 old_string_len = _dbus_string_get_length (str);
737 if (!_dbus_marshal_uint32 (str, byte_order, len * 8))
740 array_start = _dbus_string_get_length (str);
742 if (!_dbus_string_append_len (str, (const unsigned char*) value,
746 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
748 const unsigned char *d;
749 const unsigned char *end;
751 d = _dbus_string_get_data (str) + array_start;
755 #ifdef DBUS_HAVE_INT64
756 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
758 swap_bytes ((unsigned char*) d, 8);
767 /* Restore previous length */
768 _dbus_string_set_length (str, old_string_len);
774 * Marshals a 32 bit signed integer array
776 * @param str the string to append the marshalled value to
777 * @param byte_order the byte order to use
778 * @param value the array
779 * @param len the length of the array
780 * @returns #TRUE on success
783 _dbus_marshal_int32_array (DBusString *str,
785 const dbus_int32_t *value,
788 return marshal_4_octets_array (str, byte_order,
789 (const dbus_uint32_t*) value,
794 * Marshals a 32 bit unsigned integer array
796 * @param str the string to append the marshalled value to
797 * @param byte_order the byte order to use
798 * @param value the array
799 * @param len the length of the array
800 * @returns #TRUE on success
803 _dbus_marshal_uint32_array (DBusString *str,
805 const dbus_uint32_t *value,
808 return marshal_4_octets_array (str, byte_order,
813 #ifdef DBUS_HAVE_INT64
816 * Marshals a 64 bit signed integer array
818 * @param str the string to append the marshalled value to
819 * @param byte_order the byte order to use
820 * @param value the array
821 * @param len the length of the array
822 * @returns #TRUE on success
825 _dbus_marshal_int64_array (DBusString *str,
827 const dbus_int64_t *value,
830 return marshal_8_octets_array (str, byte_order,
831 (const DBusOctets8*) value,
836 * Marshals a 64 bit unsigned integer array
838 * @param str the string to append the marshalled value to
839 * @param byte_order the byte order to use
840 * @param value the array
841 * @param len the length of the array
842 * @returns #TRUE on success
845 _dbus_marshal_uint64_array (DBusString *str,
847 const dbus_uint64_t *value,
850 return marshal_8_octets_array (str, byte_order,
851 (const DBusOctets8*) value,
855 #endif /* DBUS_HAVE_INT64 */
858 * Marshals a double array
860 * @param str the string to append the marshalled value to
861 * @param byte_order the byte order to use
862 * @param value the array
863 * @param len the length of the array
864 * @returns #TRUE on success
867 _dbus_marshal_double_array (DBusString *str,
872 return marshal_8_octets_array (str, byte_order,
873 (const DBusOctets8*) value,
878 * Marshals a string array
880 * @param str the string to append the marshalled value to
881 * @param byte_order the byte order to use
882 * @param value the array
883 * @param len the length of the array
884 * @returns #TRUE on success
887 _dbus_marshal_string_array (DBusString *str,
892 int i, old_string_len, array_start;
894 old_string_len = _dbus_string_get_length (str);
896 /* Set the length to 0 temporarily */
897 if (!_dbus_marshal_uint32 (str, byte_order, 0))
900 array_start = _dbus_string_get_length (str);
902 for (i = 0; i < len; i++)
903 if (!_dbus_marshal_string (str, byte_order, value[i]))
906 /* Write the length now that we know it */
907 _dbus_marshal_set_uint32 (str, byte_order,
908 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
909 _dbus_string_get_length (str) - array_start);
914 /* Restore previous length */
915 _dbus_string_set_length (str, old_string_len);
921 * Marshals an object path value.
923 * @param str the string to append the marshalled value to
924 * @param byte_order the byte order to use
925 * @param path the path
926 * @param path_len length of the path
927 * @returns #TRUE on success
930 _dbus_marshal_object_path (DBusString *str,
935 int array_start, old_string_len;
938 old_string_len = _dbus_string_get_length (str);
940 /* Set the length to 0 temporarily */
941 if (!_dbus_marshal_uint32 (str, byte_order, 0))
944 array_start = _dbus_string_get_length (str);
949 if (!_dbus_string_append_byte (str, '/'))
952 if (!_dbus_string_append (str, path[0]))
958 /* Write the length now that we know it */
959 _dbus_marshal_set_uint32 (str, byte_order,
960 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
961 _dbus_string_get_length (str) - array_start);
966 /* Restore the previous length */
967 _dbus_string_set_length (str, old_string_len);
973 demarshal_4_octets (const DBusString *str,
978 const DBusRealString *real = (const DBusRealString*) str;
980 pos = _DBUS_ALIGN_VALUE (pos, 4);
985 return unpack_4_octets (byte_order, real->str + pos);
989 demarshal_8_octets (const DBusString *str,
994 const DBusRealString *real = (const DBusRealString*) str;
996 pos = _DBUS_ALIGN_VALUE (pos, 8);
1001 return unpack_8_octets (byte_order, real->str + pos);
1005 * Demarshals a double.
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 double.
1014 _dbus_demarshal_double (const DBusString *str,
1021 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1027 * Demarshals a 32 bit signed integer.
1029 * @param str the string containing the data
1030 * @param byte_order the byte order
1031 * @param pos the position in the string
1032 * @param new_pos the new position of the string
1033 * @returns the demarshaled integer.
1036 _dbus_demarshal_int32 (const DBusString *str,
1041 return (dbus_int32_t) demarshal_4_octets (str, byte_order, pos, new_pos);
1045 * Demarshals a 32 bit unsigned integer.
1047 * @param str the string containing the data
1048 * @param byte_order the byte order
1049 * @param pos the position in the string
1050 * @param new_pos the new position of the string
1051 * @returns the demarshaled integer.
1054 _dbus_demarshal_uint32 (const DBusString *str,
1059 return demarshal_4_octets (str, byte_order, pos, new_pos);
1062 #ifdef DBUS_HAVE_INT64
1065 * Demarshals a 64 bit signed integer.
1067 * @param str the string containing the data
1068 * @param byte_order the byte order
1069 * @param pos the position in the string
1070 * @param new_pos the new position of the string
1071 * @returns the demarshaled integer.
1074 _dbus_demarshal_int64 (const DBusString *str,
1081 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1087 * Demarshals a 64 bit unsigned integer.
1089 * @param str the string containing the data
1090 * @param byte_order the byte order
1091 * @param pos the position in the string
1092 * @param new_pos the new position of the string
1093 * @returns the demarshaled integer.
1096 _dbus_demarshal_uint64 (const DBusString *str,
1103 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1108 #endif /* DBUS_HAVE_INT64 */
1111 * Demarshals a basic type
1113 * @param str the string containing the data
1114 * @param type type of value to demarshal
1115 * @param value pointer to return value data
1116 * @param byte_order the byte order
1117 * @param pos pointer to position in the string,
1118 * updated on return to new position
1121 _dbus_demarshal_basic_type (const DBusString *str,
1127 const char *str_data = _dbus_string_get_const_data (str);
1131 case DBUS_TYPE_BYTE:
1132 case DBUS_TYPE_BOOLEAN:
1133 *(unsigned char *) value = _dbus_string_get_byte (str, *pos);
1136 case DBUS_TYPE_INT32:
1137 case DBUS_TYPE_UINT32:
1138 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1139 *(dbus_uint32_t *) value = *(dbus_uint32_t *)(str_data + *pos);
1140 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1141 *(dbus_uint32_t *) value = DBUS_UINT32_SWAP_LE_BE (*(dbus_uint32_t *) value);
1144 #ifdef DBUS_HAVE_INT64
1145 case DBUS_TYPE_INT64:
1146 case DBUS_TYPE_UINT64:
1147 #endif /* DBUS_HAVE_INT64 */
1148 case DBUS_TYPE_DOUBLE:
1149 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1150 memcpy (value, str_data + *pos, 8);
1151 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1152 #ifdef DBUS_HAVE_INT64
1153 *(dbus_uint64_t *) value = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t *) value);
1155 swap_bytes (value, 8);
1160 _dbus_assert_not_reached ("not a basic type");
1166 * Demarshals an UTF-8 string.
1168 * @todo Should we check the string to make sure
1169 * that it's valid UTF-8, and maybe "fix" the string
1172 * @todo Should probably demarshal to a DBusString,
1173 * having memcpy() in here is Evil(tm).
1175 * @param str the string containing the data
1176 * @param byte_order the byte order
1177 * @param pos the position in the string
1178 * @param new_pos the new position of the string
1179 * @returns the demarshaled string.
1182 _dbus_demarshal_string (const DBusString *str,
1191 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1193 retval = dbus_malloc (len + 1);
1198 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1203 memcpy (retval, data, len + 1);
1206 *new_pos = pos + len + 1;
1212 * Demarshals a byte array.
1214 * @todo Should probably demarshal to a DBusString,
1215 * having memcpy() in here is Evil(tm).
1217 * @param str the string containing the data
1218 * @param byte_order the byte order
1219 * @param pos the position in the string
1220 * @param new_pos the new position of the string
1221 * @param array the array
1222 * @param array_len length of the demarshaled data
1224 * @returns #TRUE on success
1227 _dbus_demarshal_byte_array (const DBusString *str,
1231 unsigned char **array,
1235 unsigned char *retval;
1238 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1251 retval = dbus_malloc (len);
1256 data = _dbus_string_get_const_data_len (str, pos, len);
1264 memcpy (retval, data, len);
1267 *new_pos = pos + len;
1276 demarshal_4_octets_array (const DBusString *str,
1280 dbus_uint32_t **array,
1284 dbus_uint32_t *retval;
1287 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1301 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1305 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1307 for (i = 0; i < len; i++)
1308 retval[i] = DBUS_UINT32_SWAP_LE_BE (retval[i]);
1312 *new_pos = pos + byte_len;
1321 demarshal_8_octets_array (const DBusString *str,
1325 DBusOctets8 **array,
1329 DBusOctets8 *retval;
1332 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1346 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1350 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1352 for (i = 0; i < len; i++)
1354 #ifdef DBUS_HAVE_INT64
1355 retval[i].u = DBUS_UINT64_SWAP_LE_BE (retval[i].u);
1357 swap_bytes ((unsigned char *) &retval[i], 8);
1363 *new_pos = pos + byte_len;
1372 * Demarshals a 32 bit signed integer array.
1374 * @param str the string containing the data
1375 * @param byte_order the byte order
1376 * @param pos the position in the string
1377 * @param new_pos the new position of the string
1378 * @param array the array
1379 * @param array_len length of the demarshaled data
1380 * @returns #TRUE on success
1383 _dbus_demarshal_int32_array (const DBusString *str,
1387 dbus_int32_t **array,
1390 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1391 (dbus_uint32_t**) array, array_len);
1395 * Demarshals a 32 bit unsigned integer array.
1397 * @param str the string containing the data
1398 * @param byte_order the byte order
1399 * @param pos the position in the string
1400 * @param new_pos the new position of the string
1401 * @param array the array
1402 * @param array_len length of the demarshaled data
1403 * @returns #TRUE on success
1406 _dbus_demarshal_uint32_array (const DBusString *str,
1410 dbus_uint32_t **array,
1413 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1417 #ifdef DBUS_HAVE_INT64
1420 * Demarshals a 64 bit signed integer array.
1422 * @param str the string containing the data
1423 * @param byte_order the byte order
1424 * @param pos the position in the string
1425 * @param new_pos the new position of the string
1426 * @param array the array
1427 * @param array_len length of the demarshaled data
1428 * @returns #TRUE on success
1431 _dbus_demarshal_int64_array (const DBusString *str,
1435 dbus_int64_t **array,
1438 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1439 (DBusOctets8**) array, array_len);
1443 * Demarshals a 64 bit unsigned integer array.
1445 * @param str the string containing the data
1446 * @param byte_order the byte order
1447 * @param pos the position in the string
1448 * @param new_pos the new position of the string
1449 * @param array the array
1450 * @param array_len length of the demarshaled data
1451 * @returns #TRUE on success
1454 _dbus_demarshal_uint64_array (const DBusString *str,
1458 dbus_uint64_t **array,
1461 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1462 (DBusOctets8**) array, array_len);
1465 #endif /* DBUS_HAVE_INT64 */
1468 * Demarshals a double array.
1470 * @param str the string containing the data
1471 * @param byte_order the byte order
1472 * @param pos the position in the string
1473 * @param new_pos the new position of the string
1474 * @param array the array
1475 * @param array_len length of the demarshaled data
1476 * @returns #TRUE on success
1479 _dbus_demarshal_double_array (const DBusString *str,
1486 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1487 (DBusOctets8**) array, array_len);
1492 * Demarshals an array of basic types
1494 * @param str the string containing the data
1495 * @param element_type type of array elements to demarshal
1496 * @param array pointer to pointer to array data
1497 * @param array_len pointer to array length
1498 * @param byte_order the byte order
1499 * @param pos pointer to position in the string,
1500 * updated on return to new position
1503 _dbus_demarshal_basic_type_array (const DBusString *str,
1510 switch (element_type)
1512 case DBUS_TYPE_BOOLEAN:
1513 /* FIXME: do we want to post-normalize these ? */
1514 case DBUS_TYPE_BYTE:
1515 return _dbus_demarshal_byte_array (str, byte_order, *pos, pos,
1516 (unsigned char **)array, array_len);
1518 case DBUS_TYPE_INT32:
1519 case DBUS_TYPE_UINT32:
1520 return demarshal_4_octets_array (str, byte_order, *pos, pos,
1521 (dbus_uint32_t **)array, array_len);
1523 #ifdef DBUS_HAVE_INT64
1524 case DBUS_TYPE_INT64:
1525 case DBUS_TYPE_UINT64:
1526 #endif /* DBUS_HAVE_INT64 */
1527 case DBUS_TYPE_DOUBLE:
1528 return demarshal_8_octets_array (str, byte_order, *pos, pos,
1529 (DBusOctets8**) array, array_len);
1531 _dbus_assert_not_reached ("not a basic type");
1538 * Demarshals a string array.
1540 * @param str the string containing the data
1541 * @param byte_order the byte order
1542 * @param pos the position in the string
1543 * @param new_pos the new position of the string
1544 * @param array the array
1545 * @param array_len location for length of the demarshaled data or NULL
1546 * @returns #TRUE on success
1549 _dbus_demarshal_string_array (const DBusString *str,
1561 bytes_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1576 end_pos = pos + bytes_len;
1578 retval = dbus_new (char *, allocated);
1583 while (pos < end_pos)
1585 retval[len] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1587 if (retval[len] == NULL)
1592 if (len >= allocated - 1) /* -1 for NULL termination */
1595 newp = dbus_realloc (retval,
1596 sizeof (char*) * allocated * 2);
1616 for (i = 0; i < len; i++)
1617 dbus_free (retval[i]);
1623 /** Set to 1 to get a bunch of spew about disassembling the path string */
1624 #define VERBOSE_DECOMPOSE 0
1627 * Decompose an object path. A path of just "/" is
1628 * represented as an empty vector of strings.
1630 * @param data the path data
1631 * @param len the length of the path string
1632 * @param path address to store new object path
1633 * @param path_len length of stored path
1636 _dbus_decompose_path (const char* data,
1645 _dbus_assert (data != NULL);
1647 #if VERBOSE_DECOMPOSE
1648 _dbus_verbose ("Decomposing path \"%s\"\n",
1661 retval = dbus_new0 (char*, n_components + 1);
1674 while (j < len && data[j] != '/')
1677 /* Now [i, j) is the path component */
1678 _dbus_assert (i < j);
1679 _dbus_assert (data[i] != '/');
1680 _dbus_assert (j == len || data[j] == '/');
1682 #if VERBOSE_DECOMPOSE
1683 _dbus_verbose (" (component in [%d,%d))\n",
1687 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1688 if (retval[comp] == NULL)
1690 dbus_free_string_array (retval);
1693 retval[comp][j-i] = '\0';
1694 #if VERBOSE_DECOMPOSE
1695 _dbus_verbose (" (component %d = \"%s\")\n",
1696 comp, retval[comp]);
1702 _dbus_assert (i == len);
1706 *path_len = n_components;
1712 * Demarshals an object path. A path of just "/" is
1713 * represented as an empty vector of strings.
1715 * @param str the string containing the data
1716 * @param byte_order the byte order
1717 * @param pos the position in the string
1718 * @param new_pos the new position of the string
1719 * @param path address to store new object path
1720 * @param path_len length of stored path
1723 _dbus_demarshal_object_path (const DBusString *str,
1733 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1734 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1736 if (!_dbus_decompose_path (data, len, path, path_len))
1740 *new_pos = pos + len + 1;
1746 * Returns the position right after the end of an argument. PERFORMS
1747 * NO VALIDATION WHATSOEVER. The message must have been previously
1750 * @param str a string
1751 * @param byte_order the byte order to use
1752 * @param type the type of the argument
1753 * @param pos the pos where the arg starts
1754 * @param end_pos pointer where the position right
1755 * after the end position will follow
1756 * @returns TRUE if more data exists after the arg
1759 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1765 if (pos >= _dbus_string_get_length (str))
1770 case DBUS_TYPE_INVALID:
1778 case DBUS_TYPE_BYTE:
1782 case DBUS_TYPE_BOOLEAN:
1786 case DBUS_TYPE_INT32:
1787 case DBUS_TYPE_UINT32:
1788 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
1791 case DBUS_TYPE_INT64:
1792 case DBUS_TYPE_UINT64:
1793 case DBUS_TYPE_DOUBLE:
1795 *end_pos = _DBUS_ALIGN_VALUE (pos, 8) + 8;
1798 case DBUS_TYPE_OBJECT_PATH:
1799 case DBUS_TYPE_STRING:
1803 /* Demarshal the length */
1804 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1806 *end_pos = pos + len + 1;
1810 case DBUS_TYPE_CUSTOM:
1814 /* Demarshal the string length */
1815 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1819 /* Demarshal the data length */
1820 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1822 *end_pos = pos + len;
1826 case DBUS_TYPE_ARRAY:
1830 /* Demarshal the length */
1831 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1833 *end_pos = pos + len;
1837 case DBUS_TYPE_DICT:
1841 /* Demarshal the length */
1842 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1844 *end_pos = pos + len;
1849 _dbus_warn ("Unknown message arg type %d\n", type);
1850 _dbus_assert_not_reached ("Unknown message argument type\n");
1854 if (*end_pos > _dbus_string_get_length (str))
1861 * Demarshals and validates a length; returns < 0 if the validation
1862 * fails. The length is required to be small enough that
1863 * len*sizeof(double) will not overflow, and small enough to fit in a
1864 * signed integer. DOES NOT check whether the length points
1865 * beyond the end of the string, because it doesn't know the
1866 * size of array elements.
1868 * @param str the string
1869 * @param byte_order the byte order
1870 * @param pos the unaligned string position (snap to next aligned)
1871 * @param new_pos return location for new position.
1874 demarshal_and_validate_len (const DBusString *str,
1879 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1882 _dbus_assert (new_pos != NULL);
1884 if ((align_4 + 4) > _dbus_string_get_length (str))
1886 _dbus_verbose ("not enough room in message for array length\n");
1890 if (!_dbus_string_validate_nul (str, pos,
1893 _dbus_verbose ("array length alignment padding not initialized to nul at %d\n", pos);
1897 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1899 /* note that the len is the number of bytes, so we need it to be
1900 * at least SIZE_T_MAX, but make it smaller just to keep things
1901 * sane. We end up using ints for most sizes to avoid unsigned mess
1902 * so limit to maximum 32-bit signed int divided by at least 8, more
1903 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1905 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1906 if (len > MAX_ARRAY_LENGTH)
1908 _dbus_verbose ("array length %u exceeds maximum of %u at pos %d\n",
1909 len, MAX_ARRAY_LENGTH, pos);
1917 validate_string (const DBusString *str,
1919 int len_without_nul,
1922 *end_pos = pos + len_without_nul + 1;
1924 if (*end_pos > _dbus_string_get_length (str))
1926 _dbus_verbose ("string length outside length of the message\n");
1930 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1932 _dbus_verbose ("string arg not nul-terminated\n");
1936 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1938 _dbus_verbose ("string is not valid UTF-8\n");
1946 * Validates and returns a typecode at a specific position
1949 * @param str a string
1950 * @param type the type of the argument
1951 * @param pos the pos where the typecode starts
1952 * @param end_pos pointer where the position right
1953 * after the end position will follow
1954 * @returns #TRUE if the type is valid.
1957 _dbus_marshal_validate_type (const DBusString *str,
1964 if (pos >= _dbus_string_get_length (str))
1967 data = _dbus_string_get_const_data_len (str, pos, 1);
1969 if (_dbus_type_is_valid (*data))
1972 if (end_pos != NULL)
1977 _dbus_verbose ("'%c' %d invalid type code\n", (int) *data, (int) *data);
1982 /* Faster validator for array data that doesn't call
1983 * validate_arg for each value
1986 validate_array_data (const DBusString *str,
1997 case DBUS_TYPE_INVALID:
2004 case DBUS_TYPE_OBJECT_PATH:
2005 case DBUS_TYPE_STRING:
2006 case DBUS_TYPE_CUSTOM:
2007 case DBUS_TYPE_ARRAY:
2008 case DBUS_TYPE_DICT:
2009 /* This clean recursion to validate_arg is what we
2010 * are doing logically for all types, but we don't
2011 * really want to call validate_arg for every byte
2012 * in a byte array, so the primitive types are
2017 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
2018 type, array_type_pos, pos, &pos))
2023 case DBUS_TYPE_BYTE:
2027 case DBUS_TYPE_BOOLEAN:
2032 c = _dbus_string_get_byte (str, pos);
2034 if (!(c == 0 || c == 1))
2036 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
2044 case DBUS_TYPE_INT32:
2045 case DBUS_TYPE_UINT32:
2046 /* Call validate arg one time to check alignment padding
2049 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
2050 type, array_type_pos, pos, &pos))
2052 pos = _DBUS_ALIGN_VALUE (end, 4);
2055 case DBUS_TYPE_INT64:
2056 case DBUS_TYPE_UINT64:
2057 case DBUS_TYPE_DOUBLE:
2058 /* Call validate arg one time to check alignment padding
2061 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
2062 type, array_type_pos, pos, &pos))
2064 pos = _DBUS_ALIGN_VALUE (end, 8);
2068 _dbus_verbose ("Unknown message arg type %d\n", type);
2078 * Validates an argument of a specific type, checking that it
2079 * is well-formed, for example no ludicrous length fields, strings
2080 * are nul-terminated, etc.
2081 * Returns the end position of the argument in end_pos, and
2082 * returns #TRUE if a valid arg begins at "pos"
2084 * @todo security: need to audit this function.
2086 * @param str a string
2087 * @param byte_order the byte order to use
2088 * @param depth current recursion depth, to prevent excessive recursion
2089 * @param type the type of the argument
2090 * @param array_type_pos the position of the current array type, or
2091 * -1 if not in an array
2092 * @param pos the pos where the arg starts
2093 * @param end_pos pointer where the position right
2094 * after the end position will follow
2095 * @returns #TRUE if the arg is valid.
2098 _dbus_marshal_validate_arg (const DBusString *str,
2106 if (pos > _dbus_string_get_length (str))
2108 _dbus_verbose ("Validation went off the end of the message\n");
2112 #define MAX_VALIDATION_DEPTH 32
2114 if (depth > MAX_VALIDATION_DEPTH)
2116 _dbus_verbose ("Maximum recursion depth reached validating message\n");
2122 case DBUS_TYPE_INVALID:
2130 case DBUS_TYPE_BYTE:
2131 if (1 > _dbus_string_get_length (str) - pos)
2133 _dbus_verbose ("no room for byte value\n");
2140 case DBUS_TYPE_BOOLEAN:
2144 if (1 > _dbus_string_get_length (str) - pos)
2146 _dbus_verbose ("no room for boolean value\n");
2150 c = _dbus_string_get_byte (str, pos);
2152 if (!(c == 0 || c == 1))
2154 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
2162 case DBUS_TYPE_INT32:
2163 case DBUS_TYPE_UINT32:
2165 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
2167 if (!_dbus_string_validate_nul (str, pos,
2170 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
2174 *end_pos = align_4 + 4;
2178 case DBUS_TYPE_INT64:
2179 case DBUS_TYPE_UINT64:
2180 case DBUS_TYPE_DOUBLE:
2182 int align_8 = _DBUS_ALIGN_VALUE (pos, 8);
2184 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
2186 if (!_dbus_string_validate_nul (str, pos,
2189 _dbus_verbose ("double/int64/uint64/objid alignment padding not initialized to nul at %d\n", pos);
2193 *end_pos = align_8 + 8;
2197 case DBUS_TYPE_OBJECT_PATH:
2198 case DBUS_TYPE_STRING:
2202 /* Demarshal the length, which does NOT include
2205 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2209 if (!validate_string (str, pos, len, end_pos))
2212 if (type == DBUS_TYPE_OBJECT_PATH)
2214 if (!_dbus_string_validate_path (str, pos, len))
2220 case DBUS_TYPE_CUSTOM:
2224 /* Demarshal the string length, which does NOT include
2227 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2231 if (!validate_string (str, pos, len, &pos))
2235 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2239 *end_pos = pos + len;
2243 case DBUS_TYPE_ARRAY:
2249 if (array_type_pos == -1)
2251 array_type_pos = pos;
2255 if (!_dbus_marshal_validate_type (str, pos, &array_type, &pos))
2257 _dbus_verbose ("invalid array type\n");
2261 /* NIL values take up no space, so you couldn't iterate over an array of them.
2262 * array of nil seems useless anyway; the useful thing might be array of
2263 * (nil OR string) but we have no framework for that.
2265 if (array_type == DBUS_TYPE_NIL)
2267 _dbus_verbose ("array of NIL is not allowed\n");
2271 while (array_type == DBUS_TYPE_ARRAY);
2276 if (!_dbus_marshal_validate_type (str, array_type_pos, &array_type, NULL))
2278 _dbus_verbose ("invalid array type\n");
2282 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2285 _dbus_verbose ("invalid array length (<0)\n");
2289 if (len > _dbus_string_get_length (str) - pos)
2291 _dbus_verbose ("array length outside length of the message\n");
2297 if (len > 0 && !validate_array_data (str, byte_order, depth + 1,
2298 array_type, array_type_pos,
2301 _dbus_verbose ("invalid array data\n");
2307 /* This should not be able to happen, as long as validate_arg moves forward;
2308 * but the check is here just to be paranoid.
2310 _dbus_verbose ("array length %d specified was longer than actual array contents by %d\n",
2317 _dbus_verbose ("array contents exceeds array length %d by %d\n", len, pos - end);
2325 case DBUS_TYPE_DICT:
2331 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2335 if (len > _dbus_string_get_length (str) - pos)
2337 _dbus_verbose ("dict length outside length of the message\n");
2346 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2347 DBUS_TYPE_STRING, -1, pos, &pos))
2350 if (!_dbus_marshal_validate_type (str, pos, &dict_type, &pos))
2352 _dbus_verbose ("invalid dict entry type at offset %d\n", pos);
2356 /* Validate element */
2357 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2358 dict_type, -1, pos, &pos))
2360 _dbus_verbose ("dict arg invalid at offset %d\n", pos);
2367 _dbus_verbose ("dict contents exceed stated dict length\n");
2376 _dbus_verbose ("Unknown message arg type %d\n", type);
2380 if (*end_pos > _dbus_string_get_length (str))
2387 * Return #TRUE if the typecode is a valid typecode
2389 * @returns #TRUE if valid
2392 _dbus_type_is_valid (int typecode)
2397 case DBUS_TYPE_BYTE:
2398 case DBUS_TYPE_BOOLEAN:
2399 case DBUS_TYPE_INT32:
2400 case DBUS_TYPE_UINT32:
2401 case DBUS_TYPE_INT64:
2402 case DBUS_TYPE_UINT64:
2403 case DBUS_TYPE_DOUBLE:
2404 case DBUS_TYPE_STRING:
2405 case DBUS_TYPE_CUSTOM:
2406 case DBUS_TYPE_ARRAY:
2407 case DBUS_TYPE_DICT:
2408 case DBUS_TYPE_OBJECT_PATH:
2417 * If in verbose mode, print a block of binary data.
2419 * @todo right now it prints even if not in verbose mode
2421 * @param data the data
2422 * @param len the length of the data
2425 _dbus_verbose_bytes (const unsigned char *data,
2429 const unsigned char *aligned;
2431 _dbus_assert (len >= 0);
2433 /* Print blanks on first row if appropriate */
2434 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
2437 _dbus_assert (aligned <= data);
2439 if (aligned != data)
2441 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
2442 while (aligned != data)
2444 _dbus_verbose (" ");
2449 /* now print the bytes */
2453 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2455 _dbus_verbose ("%4d\t%p: ",
2459 if (data[i] >= 32 &&
2461 _dbus_verbose (" '%c' ", data[i]);
2463 _dbus_verbose ("0x%s%x ",
2464 data[i] <= 0xf ? "0" : "", data[i]);
2468 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2471 _dbus_verbose ("BE: %d LE: %d",
2472 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
2473 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
2476 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
2478 _dbus_verbose (" dbl: %g",
2479 *(double*)&data[i-8]);
2482 _dbus_verbose ("\n");
2486 _dbus_verbose ("\n");
2490 * Dump the given part of the string to verbose log.
2492 * @param str the string
2493 * @param start the start of range to dump
2494 * @param len length of range
2497 _dbus_verbose_bytes_of_string (const DBusString *str,
2504 real_len = _dbus_string_get_length (str);
2506 _dbus_assert (start >= 0);
2508 if (start > real_len)
2510 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
2511 start, len, real_len);
2515 if ((start + len) > real_len)
2517 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
2518 start, len, real_len);
2519 len = real_len - start;
2522 d = _dbus_string_get_const_data_len (str, start, len);
2524 _dbus_verbose_bytes (d, len);
2528 * Marshals a basic type
2530 * @param str string to marshal to
2531 * @param type type of value
2532 * @param value pointer to value
2533 * @param byte_order byte order
2534 * @returns #TRUE on success
2537 _dbus_marshal_basic_type (DBusString *str,
2546 case DBUS_TYPE_BYTE:
2547 case DBUS_TYPE_BOOLEAN:
2548 retval = _dbus_string_append_byte (str, *(unsigned char *)value);
2550 case DBUS_TYPE_INT32:
2551 case DBUS_TYPE_UINT32:
2552 return marshal_4_octets (str, byte_order, *(dbus_uint32_t *)value);
2554 #ifdef DBUS_HAVE_INT64
2555 case DBUS_TYPE_INT64:
2556 case DBUS_TYPE_UINT64:
2557 retval = _dbus_marshal_uint64 (str, byte_order, *(dbus_uint64_t *)value);
2559 #endif /* DBUS_HAVE_INT64 */
2560 case DBUS_TYPE_DOUBLE:
2561 retval = _dbus_marshal_double (str, byte_order, *(double *)value);
2564 _dbus_assert_not_reached ("not a basic type");
2572 * Marshals a basic type array
2574 * @param str string to marshal to
2575 * @param element_type type of array elements
2576 * @param value pointer to value
2577 * @param len length of value data in elements
2578 * @param byte_order byte order
2579 * @returns #TRUE on success
2582 _dbus_marshal_basic_type_array (DBusString *str,
2588 switch (element_type)
2590 case DBUS_TYPE_BOOLEAN:
2591 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
2592 * should we here too ? */
2593 case DBUS_TYPE_BYTE:
2594 return _dbus_marshal_byte_array (str, byte_order, value, len);
2596 case DBUS_TYPE_INT32:
2597 case DBUS_TYPE_UINT32:
2598 return marshal_4_octets_array (str, byte_order, value, len);
2600 #ifdef DBUS_HAVE_INT64
2601 case DBUS_TYPE_INT64:
2602 case DBUS_TYPE_UINT64:
2603 #endif /* DBUS_HAVE_INT64 */
2604 case DBUS_TYPE_DOUBLE:
2605 return marshal_8_octets_array (str, byte_order, value, len);
2608 _dbus_assert_not_reached ("non basic type in array");
2616 #ifdef DBUS_BUILD_TESTS
2617 #include "dbus-test.h"
2621 _dbus_marshal_test (void)
2626 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
2627 #ifdef DBUS_HAVE_INT64
2628 dbus_int64_t array3[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
2629 DBUS_INT64_CONSTANT (0x456ffffffff),
2630 DBUS_INT64_CONSTANT (0x789ffffffff) }, *array4;
2635 if (!_dbus_string_init (&str))
2636 _dbus_assert_not_reached ("failed to init string");
2638 /* Marshal doubles */
2639 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2640 _dbus_assert_not_reached ("could not marshal double value");
2641 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2642 _dbus_assert_not_reached ("demarshal failed");
2644 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2645 _dbus_assert_not_reached ("could not marshal double value");
2646 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2647 _dbus_assert_not_reached ("demarshal failed");
2649 /* Marshal signed integers */
2650 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2651 _dbus_assert_not_reached ("could not marshal signed integer value");
2652 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2653 _dbus_assert_not_reached ("demarshal failed");
2655 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2656 _dbus_assert_not_reached ("could not marshal signed integer value");
2657 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2658 _dbus_assert_not_reached ("demarshal failed");
2660 /* Marshal unsigned integers */
2661 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2662 _dbus_assert_not_reached ("could not marshal signed integer value");
2663 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2664 _dbus_assert_not_reached ("demarshal failed");
2666 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2667 _dbus_assert_not_reached ("could not marshal signed integer value");
2668 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2669 _dbus_assert_not_reached ("demarshal failed");
2671 #ifdef DBUS_HAVE_INT64
2672 /* Marshal signed integers */
2673 if (!_dbus_marshal_int64 (&str, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2674 _dbus_assert_not_reached ("could not marshal signed integer value");
2675 if (_dbus_demarshal_int64 (&str, DBUS_BIG_ENDIAN, pos, &pos) != DBUS_INT64_CONSTANT (-0x123456789abc7))
2676 _dbus_assert_not_reached ("demarshal failed");
2678 if (!_dbus_marshal_int64 (&str, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2679 _dbus_assert_not_reached ("could not marshal signed integer value");
2680 if (_dbus_demarshal_int64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) != DBUS_INT64_CONSTANT (-0x123456789abc7))
2681 _dbus_assert_not_reached ("demarshal failed");
2683 /* Marshal unsigned integers */
2684 if (!_dbus_marshal_uint64 (&str, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2685 _dbus_assert_not_reached ("could not marshal signed integer value");
2686 if (!(_dbus_demarshal_uint64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2687 _dbus_assert_not_reached ("demarshal failed");
2689 if (!_dbus_marshal_uint64 (&str, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2690 _dbus_assert_not_reached ("could not marshal signed integer value");
2691 if (!(_dbus_demarshal_uint64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2692 _dbus_assert_not_reached ("demarshal failed");
2693 #endif /* DBUS_HAVE_INT64 */
2695 /* Marshal strings */
2696 tmp1 = "This is the dbus test string";
2697 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2698 _dbus_assert_not_reached ("could not marshal string");
2699 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2700 if (!strcmp (tmp1, tmp2) == 0)
2701 _dbus_assert_not_reached ("demarshal failed");
2704 tmp1 = "This is the dbus test string";
2705 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2706 _dbus_assert_not_reached ("could not marshal string");
2707 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2708 if (!strcmp (tmp1, tmp2) == 0)
2709 _dbus_assert_not_reached ("demarshal failed");
2712 /* Marshal signed integer arrays */
2713 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2714 _dbus_assert_not_reached ("could not marshal integer array");
2715 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2716 _dbus_assert_not_reached ("could not demarshal integer array");
2719 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2722 #ifdef DBUS_HAVE_INT64
2723 /* Marshal 64-bit signed integer arrays */
2724 if (!_dbus_marshal_int64_array (&str, DBUS_BIG_ENDIAN, array3, 3))
2725 _dbus_assert_not_reached ("could not marshal integer array");
2726 if (!_dbus_demarshal_int64_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array4, &len))
2727 _dbus_assert_not_reached ("could not demarshal integer array");
2730 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2733 /* set/pack 64-bit integers */
2734 _dbus_string_set_length (&str, 8);
2737 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
2738 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2740 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2741 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2742 _dbus_string_get_const_data (&str)));
2745 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
2746 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2748 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2749 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2750 _dbus_string_get_const_data (&str)));
2752 /* signed little pack */
2753 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2755 _dbus_string_get_data (&str));
2757 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2758 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2759 _dbus_string_get_const_data (&str)));
2761 /* signed big pack */
2762 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2764 _dbus_string_get_data (&str));
2766 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2767 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2768 _dbus_string_get_const_data (&str)));
2770 /* unsigned little */
2771 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
2772 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2774 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2775 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2776 _dbus_string_get_const_data (&str)));
2779 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
2780 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2782 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2783 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2784 _dbus_string_get_const_data (&str)));
2786 /* unsigned little pack */
2787 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2789 _dbus_string_get_data (&str));
2791 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2792 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2793 _dbus_string_get_const_data (&str)));
2795 /* unsigned big pack */
2796 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2798 _dbus_string_get_data (&str));
2800 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2801 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2802 _dbus_string_get_const_data (&str)));
2806 /* set/pack 32-bit integers */
2807 _dbus_string_set_length (&str, 4);
2810 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
2813 _dbus_assert (-0x123456 ==
2814 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2815 _dbus_string_get_const_data (&str)));
2818 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
2821 _dbus_assert (-0x123456 ==
2822 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2823 _dbus_string_get_const_data (&str)));
2825 /* signed little pack */
2826 _dbus_pack_int32 (-0x123456,
2828 _dbus_string_get_data (&str));
2830 _dbus_assert (-0x123456 ==
2831 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2832 _dbus_string_get_const_data (&str)));
2834 /* signed big pack */
2835 _dbus_pack_int32 (-0x123456,
2837 _dbus_string_get_data (&str));
2839 _dbus_assert (-0x123456 ==
2840 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2841 _dbus_string_get_const_data (&str)));
2843 /* unsigned little */
2844 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
2847 _dbus_assert (0x123456 ==
2848 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2849 _dbus_string_get_const_data (&str)));
2852 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
2855 _dbus_assert (0x123456 ==
2856 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2857 _dbus_string_get_const_data (&str)));
2859 /* unsigned little pack */
2860 _dbus_pack_uint32 (0x123456,
2862 _dbus_string_get_data (&str));
2864 _dbus_assert (0x123456 ==
2865 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2866 _dbus_string_get_const_data (&str)));
2868 /* unsigned big pack */
2869 _dbus_pack_uint32 (0x123456,
2871 _dbus_string_get_data (&str));
2873 _dbus_assert (0x123456 ==
2874 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2875 _dbus_string_get_const_data (&str)));
2880 _dbus_string_set_length (&str, 0);
2882 _dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN,
2885 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2886 _dbus_assert (strcmp (s, "Hello world") == 0);
2889 _dbus_string_init_const (&t, "Hello world foo");
2891 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2892 &t, _dbus_string_get_length (&t));
2894 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2895 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2898 _dbus_string_init_const (&t, "Hello");
2900 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2901 &t, _dbus_string_get_length (&t));
2903 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2904 _dbus_assert (strcmp (s, "Hello") == 0);
2907 /* Strings (big endian) */
2909 _dbus_string_set_length (&str, 0);
2911 _dbus_marshal_string (&str, DBUS_BIG_ENDIAN,
2914 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2915 _dbus_assert (strcmp (s, "Hello world") == 0);
2918 _dbus_string_init_const (&t, "Hello world foo");
2920 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2921 &t, _dbus_string_get_length (&t));
2923 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2924 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2927 _dbus_string_init_const (&t, "Hello");
2929 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2930 &t, _dbus_string_get_length (&t));
2932 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2933 _dbus_assert (strcmp (s, "Hello") == 0);
2936 _dbus_string_free (&str);
2941 #endif /* DBUS_BUILD_TESTS */