1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.c Marshalling routines
4 * Copyright (C) 2002 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-marshal.h"
25 #include "dbus-internals.h"
26 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
27 #include "dbus-string-private.h"
33 swap_bytes (unsigned char *data,
36 unsigned char *p1 = data;
37 unsigned char *p2 = data + len - 1;
41 unsigned char tmp = *p1;
51 * @defgroup DBusMarshal marshaling and unmarshaling
52 * @ingroup DBusInternals
53 * @brief functions to marshal/unmarshal data from the wire
55 * Types and functions related to converting primitive data types from
56 * wire format to native machine format, and vice versa.
62 * Unpacks a 32 bit unsigned integer from a data pointer
64 * @param byte_order The byte order to use
65 * @param data the data pointer
66 * @returns the integer
69 _dbus_unpack_uint32 (int byte_order,
70 const unsigned char *data)
72 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
74 if (byte_order == DBUS_LITTLE_ENDIAN)
75 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
77 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
81 * Unpacks a 32 bit signed integer from a data pointer
83 * @param byte_order The byte order to use
84 * @param data the data pointer
85 * @returns the integer
88 _dbus_unpack_int32 (int byte_order,
89 const unsigned char *data)
91 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
93 if (byte_order == DBUS_LITTLE_ENDIAN)
94 return DBUS_INT32_FROM_LE (*(dbus_int32_t*)data);
96 return DBUS_INT32_FROM_BE (*(dbus_int32_t*)data);
100 * Packs a 32 bit unsigned integer into a data pointer.
102 * @param value the value
103 * @param byte_order the byte order to use
104 * @param data the data pointer
107 _dbus_pack_uint32 (dbus_uint32_t value,
111 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
113 if ((byte_order) == DBUS_LITTLE_ENDIAN)
114 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
116 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
120 * Packs a 32 bit signed integer into a data pointer.
122 * @param value the value
123 * @param byte_order the byte order to use
124 * @param data the data pointer
127 _dbus_pack_int32 (dbus_int32_t value,
131 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
133 if ((byte_order) == DBUS_LITTLE_ENDIAN)
134 *((dbus_int32_t*)(data)) = DBUS_INT32_TO_LE (value);
136 *((dbus_int32_t*)(data)) = DBUS_INT32_TO_BE (value);
140 * Sets the 4 bytes at the given offset to a marshaled signed integer,
141 * replacing anything found there previously.
143 * @param str the string to write the marshalled int to
144 * @param offset the byte offset where int should be written
145 * @param byte_order the byte order to use
146 * @param value the value
150 _dbus_marshal_set_int32 (DBusString *str,
157 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
158 byte_order == DBUS_BIG_ENDIAN);
160 _dbus_string_get_data_len (str, &data, offset, 4);
162 _dbus_pack_int32 (value, byte_order, data);
166 * Sets the 4 bytes at the given offset to a marshaled unsigned
167 * integer, replacing anything found there previously.
169 * @param str the string to write the marshalled int to
170 * @param offset the byte offset where int should be written
171 * @param byte_order the byte order to use
172 * @param value the value
176 _dbus_marshal_set_uint32 (DBusString *str,
183 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
184 byte_order == DBUS_BIG_ENDIAN);
186 _dbus_string_get_data_len (str, &data, offset, 4);
188 _dbus_pack_uint32 (value, byte_order, data);
192 * Sets the existing marshaled string at the given offset with
193 * a new marshaled string. The given offset must point to
194 * an existing string or the wrong length will be deleted
195 * and replaced with the new string.
197 * @param str the string to write the marshalled string to
198 * @param offset the byte offset where string should be written
199 * @param byte_order the byte order to use
200 * @param value the value
201 * @param len the length to use
202 * @returns #TRUE on success
206 _dbus_marshal_set_string (DBusString *str,
209 const DBusString *value,
214 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
215 byte_order == DBUS_BIG_ENDIAN);
217 old_len = _dbus_demarshal_uint32 (str, byte_order,
220 if (!_dbus_string_replace_len (value, 0, len,
221 str, offset + 4, old_len))
224 _dbus_marshal_set_uint32 (str, byte_order,
231 * Marshals a double value.
233 * @param str the string to append the marshalled value to
234 * @param byte_order the byte order to use
235 * @param value the value
236 * @returns #TRUE on success
239 _dbus_marshal_double (DBusString *str,
243 _dbus_assert (sizeof (double) == 8);
245 if (!_dbus_string_align_length (str, sizeof (double)))
248 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
249 swap_bytes ((unsigned char *)&value, sizeof (double));
251 return _dbus_string_append_len (str, (const char *)&value, sizeof (double));
255 * Marshals a 32 bit signed integer value.
257 * @param str the string to append the marshalled value to
258 * @param byte_order the byte order to use
259 * @param value the value
260 * @returns #TRUE on success
263 _dbus_marshal_int32 (DBusString *str,
267 if (!_dbus_string_align_length (str, sizeof (dbus_int32_t)))
270 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
271 value = DBUS_INT32_SWAP_LE_BE (value);
273 return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_int32_t));
277 * Marshals a 32 bit unsigned integer value.
279 * @param str the string to append the marshalled value to
280 * @param byte_order the byte order to use
281 * @param value the value
282 * @returns #TRUE on success
285 _dbus_marshal_uint32 (DBusString *str,
289 if (!_dbus_string_align_length (str, sizeof (dbus_uint32_t)))
292 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
293 value = DBUS_UINT32_SWAP_LE_BE (value);
295 return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_uint32_t));
299 * Marshals a UTF-8 string
301 * @param str the string to append the marshalled value to
302 * @param byte_order the byte order to use
303 * @param value the string
304 * @returns #TRUE on success
307 _dbus_marshal_string (DBusString *str,
311 int len, old_string_len;
313 old_string_len = _dbus_string_get_length (str);
315 len = strlen (value);
317 if (!_dbus_marshal_uint32 (str, byte_order, len))
319 /* Restore the previous length */
320 _dbus_string_set_length (str, old_string_len);
325 return _dbus_string_append_len (str, value, len + 1);
329 * Marshals a byte array
331 * @param str the string to append the marshalled value to
332 * @param byte_order the byte order to use
333 * @param value the array
334 * @param len number of elements in the array
335 * @returns #TRUE on success
338 _dbus_marshal_byte_array (DBusString *str,
340 const unsigned char *value,
345 old_string_len = _dbus_string_get_length (str);
347 if (!_dbus_marshal_uint32 (str, byte_order, len))
349 /* Restore the previous length */
350 _dbus_string_set_length (str, old_string_len);
358 return _dbus_string_append_len (str, value, len);
362 * Marshals a 32 bit signed integer array
364 * @param str the string to append the marshalled value to
365 * @param byte_order the byte order to use
366 * @param value the array
367 * @param len the length of the array
368 * @returns #TRUE on success
371 _dbus_marshal_int32_array (DBusString *str,
373 const dbus_int32_t *value,
376 int i, old_string_len;
378 old_string_len = _dbus_string_get_length (str);
380 if (!_dbus_marshal_uint32 (str, byte_order, len))
383 for (i = 0; i < len; i++)
384 if (!_dbus_marshal_int32 (str, byte_order, value[i]))
390 /* Restore previous length */
391 _dbus_string_set_length (str, old_string_len);
397 * Marshals a 32 bit unsigned integer array
399 * @param str the string to append the marshalled value to
400 * @param byte_order the byte order to use
401 * @param value the array
402 * @param len the length of the array
403 * @returns #TRUE on success
406 _dbus_marshal_uint32_array (DBusString *str,
408 const dbus_uint32_t *value,
411 int i, old_string_len;
413 old_string_len = _dbus_string_get_length (str);
415 if (!_dbus_marshal_uint32 (str, byte_order, len))
418 for (i = 0; i < len; i++)
419 if (!_dbus_marshal_uint32 (str, byte_order, value[i]))
425 /* Restore previous length */
426 _dbus_string_set_length (str, old_string_len);
432 * Marshals a double array
434 * @param str the string to append the marshalled value to
435 * @param byte_order the byte order to use
436 * @param value the array
437 * @param len the length of the array
438 * @returns #TRUE on success
441 _dbus_marshal_double_array (DBusString *str,
446 int i, old_string_len;
448 old_string_len = _dbus_string_get_length (str);
450 if (!_dbus_marshal_uint32 (str, byte_order, len))
453 for (i = 0; i < len; i++)
454 if (!_dbus_marshal_double (str, byte_order, value[i]))
460 /* Restore previous length */
461 _dbus_string_set_length (str, old_string_len);
467 * Marshals a string array
469 * @param str the string to append the marshalled value to
470 * @param byte_order the byte order to use
471 * @param value the array
472 * @param len the length of the array
473 * @returns #TRUE on success
476 _dbus_marshal_string_array (DBusString *str,
481 int i, old_string_len;
483 old_string_len = _dbus_string_get_length (str);
485 if (!_dbus_marshal_uint32 (str, byte_order, len))
488 for (i = 0; i < len; i++)
489 if (!_dbus_marshal_string (str, byte_order, value[i]))
495 /* Restore previous length */
496 _dbus_string_set_length (str, old_string_len);
504 * @param str the string to append the marshalled value to
505 * @param byte_order the byte order to use
506 * @param dict the dict
507 * @returns #TRUE on success
510 _dbus_marshal_dict (DBusString *str,
518 old_string_len = _dbus_string_get_length (str);
520 if (!dbus_dict_get_keys (dict, &keys, &len))
526 if (!_dbus_marshal_string_array (str, byte_order,
527 (const char **)keys, len))
530 for (i = 0; i < len; i++)
534 value_type = dbus_dict_get_value_type (dict, keys[i]);
536 if (!_dbus_string_append_byte (str, value_type))
539 switch (dbus_dict_get_value_type (dict, keys[i]))
541 case DBUS_TYPE_BOOLEAN:
545 if (!dbus_dict_get_boolean (dict, keys[i], &value))
548 if (!_dbus_string_append_byte (str, (value != FALSE)))
554 case DBUS_TYPE_INT32:
558 if (!dbus_dict_get_int32 (dict, keys[i], &value))
561 if (!_dbus_marshal_int32 (str, byte_order, value))
566 case DBUS_TYPE_UINT32:
570 if (!dbus_dict_get_uint32 (dict, keys[i], &value))
573 if (!_dbus_marshal_uint32 (str, byte_order, value))
578 case DBUS_TYPE_DOUBLE:
582 if (!dbus_dict_get_double (dict, keys[i], &value))
585 if (!_dbus_marshal_double (str, byte_order, value))
590 case DBUS_TYPE_INT32_ARRAY:
592 const dbus_int32_t *value;
595 if (!dbus_dict_get_int32_array (dict, keys[i], &value, &len))
598 if (!_dbus_marshal_int32_array (str, byte_order, value, len))
603 case DBUS_TYPE_STRING:
607 if (!dbus_dict_get_string (dict, keys[i], &value))
610 if (!_dbus_marshal_string (str, byte_order, value))
615 case DBUS_TYPE_BOOLEAN_ARRAY:
617 const unsigned char *value;
620 if (!dbus_dict_get_boolean_array (dict, keys[i], &value, &len))
623 if (!_dbus_marshal_byte_array (str, byte_order, value, len))
628 case DBUS_TYPE_UINT32_ARRAY:
630 const dbus_uint32_t *value;
633 if (!dbus_dict_get_uint32_array (dict, keys[i], &value, &len))
636 if (!_dbus_marshal_uint32_array (str, byte_order, value, len))
641 case DBUS_TYPE_DOUBLE_ARRAY:
646 if (!dbus_dict_get_double_array (dict, keys[i], &value, &len))
649 if (!_dbus_marshal_double_array (str, byte_order, value, len))
654 case DBUS_TYPE_STRING_ARRAY:
659 if (!dbus_dict_get_string_array (dict, keys[i], &value, &len))
662 if (!_dbus_marshal_string_array (str, byte_order, (const char **)value, len))
668 _dbus_warn ("unkown value type %d\n", dbus_dict_get_value_type (dict, keys[i]));
669 _dbus_assert_not_reached ("unknown value type in dict");
673 dbus_free_string_array (keys);
679 dbus_free_string_array (keys);
681 /* Restore previous length */
682 _dbus_string_set_length (str, old_string_len);
689 * Demarshals a double.
691 * @param str the string containing the data
692 * @param byte_order the byte order
693 * @param pos the position in the string
694 * @param new_pos the new position of the string
695 * @returns the demarshaled double.
698 _dbus_demarshal_double (const DBusString *str,
706 pos = _DBUS_ALIGN_VALUE (pos, sizeof (double));
708 _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (double));
710 retval = *(double *)buffer;
712 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
713 swap_bytes ((unsigned char *)&retval, sizeof (double));
716 *new_pos = pos + sizeof (double);
722 * Demarshals a 32 bit signed integer.
724 * @param str the string containing the data
725 * @param byte_order the byte order
726 * @param pos the position in the string
727 * @param new_pos the new position of the string
728 * @returns the demarshaled integer.
731 _dbus_demarshal_int32 (const DBusString *str,
736 const DBusRealString *real = (const DBusRealString*) str;
738 pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_int32_t));
741 *new_pos = pos + sizeof (dbus_int32_t);
743 if (byte_order == DBUS_LITTLE_ENDIAN)
744 return DBUS_INT32_FROM_LE (*(dbus_int32_t*)(real->str + pos));
746 return DBUS_INT32_FROM_BE (*(dbus_int32_t*)(real->str + pos));
750 * Demarshals a 32 bit unsigned integer.
752 * @param str the string containing the data
753 * @param byte_order the byte order
754 * @param pos the position in the string
755 * @param new_pos the new position of the string
756 * @returns the demarshaled integer.
759 _dbus_demarshal_uint32 (const DBusString *str,
764 const DBusRealString *real = (const DBusRealString*) str;
766 pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
769 *new_pos = pos + sizeof (dbus_uint32_t);
771 if (byte_order == DBUS_LITTLE_ENDIAN)
772 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(real->str + pos));
774 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(real->str + pos));
778 * Demarshals an UTF-8 string.
780 * @todo Should we check the string to make sure
781 * that it's valid UTF-8, and maybe "fix" the string
784 * @todo Should probably demarshal to a DBusString,
785 * having memcpy() in here is Evil(tm).
787 * @param str the string containing the data
788 * @param byte_order the byte order
789 * @param pos the position in the string
790 * @param new_pos the new position of the string
791 * @returns the demarshaled string.
794 _dbus_demarshal_string (const DBusString *str,
803 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
805 retval = dbus_malloc (len + 1);
810 _dbus_string_get_const_data_len (str, &data, pos, len);
815 memcpy (retval, data, len + 1);
818 *new_pos = pos + len + 1;
824 * Demarshals a byte array.
826 * @todo Should probably demarshal to a DBusString,
827 * having memcpy() in here is Evil(tm).
829 * @param str the string containing the data
830 * @param byte_order the byte order
831 * @param pos the position in the string
832 * @param array the array
833 * @param array_len length of the demarshaled data
835 * @returns #TRUE on success
838 _dbus_demarshal_byte_array (const DBusString *str,
842 unsigned char **array,
846 unsigned char *retval;
849 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
862 retval = dbus_malloc (len);
867 _dbus_string_get_const_data_len (str, &data, pos, len);
875 memcpy (retval, data, len);
878 *new_pos = pos + len;
887 * Demarshals a 32 bit signed integer array.
889 * @param str the string containing the data
890 * @param byte_order the byte order
891 * @param pos the position in the string
892 * @param new_pos the new position of the string
893 * @param array the array
894 * @param array_len length of the demarshaled data
895 * @returns #TRUE on success
898 _dbus_demarshal_int32_array (const DBusString *str,
902 dbus_int32_t **array,
906 dbus_int32_t *retval;
908 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
921 retval = dbus_new (dbus_int32_t, len);
926 for (i = 0; i < len; i++)
927 retval[i] = _dbus_demarshal_int32 (str, byte_order, pos, &pos);
939 * Demarshals a 32 bit unsigned integer array.
941 * @param str the string containing the data
942 * @param byte_order the byte order
943 * @param pos the position in the string
944 * @param new_pos the new position of the string
945 * @param array the array
946 * @param array_len length of the demarshaled data
947 * @returns #TRUE on success
950 _dbus_demarshal_uint32_array (const DBusString *str,
954 dbus_uint32_t **array,
958 dbus_uint32_t *retval;
960 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
973 retval = dbus_new (dbus_uint32_t, len);
978 for (i = 0; i < len; i++)
979 retval[i] = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
991 * Demarshals a double array.
993 * @param str the string containing the data
994 * @param byte_order the byte order
995 * @param pos the position in the string
996 * @param new_pos the new position of the string
997 * @param array the array
998 * @param array_len length of the demarshaled data
999 * @returns #TRUE on success
1002 _dbus_demarshal_double_array (const DBusString *str,
1012 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1025 retval = dbus_new (double, len);
1030 for (i = 0; i < len; i++)
1031 retval[i] = _dbus_demarshal_double (str, byte_order, pos, &pos);
1043 * Demarshals a string array.
1045 * @param str the string containing the data
1046 * @param byte_order the byte order
1047 * @param pos the position in the string
1048 * @param new_pos the new position of the string
1049 * @param array the array
1050 * @param array_len length of the demarshaled data
1051 * @returns #TRUE on success
1054 _dbus_demarshal_string_array (const DBusString *str,
1064 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1077 retval = dbus_new (char *, len + 1);
1084 for (i = 0; i < len; i++)
1086 retval[i] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1101 for (j = 0; j < i; j++)
1102 dbus_free (retval[i]);
1111 * @param str the string containing the data
1112 * @param byte_order the byte order
1113 * @param pos the position in the string
1114 * @param new_pos the new position in the string
1115 * @param dict the dict
1116 * @returns #TRUE on success.
1119 _dbus_demarshal_dict (const DBusString *str,
1128 *dict = dbus_dict_new ();
1132 if (!_dbus_demarshal_string_array (str, byte_order, pos, &pos, &keys, &len))
1135 for (i = 0; i < len; i++)
1139 switch ((value_type = _dbus_string_get_byte (str, pos ++)))
1141 case DBUS_TYPE_BOOLEAN:
1145 value = _dbus_string_get_byte (str, pos ++);
1147 if (!dbus_dict_set_boolean (*dict, keys[i], value))
1151 case DBUS_TYPE_INT32:
1155 value = _dbus_demarshal_int32 (str, byte_order, pos, &pos);
1157 if (!dbus_dict_set_int32 (*dict, keys[i], value))
1162 case DBUS_TYPE_UINT32:
1164 dbus_uint32_t value;
1166 value = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1168 if (!dbus_dict_set_uint32 (*dict, keys[i], value))
1173 case DBUS_TYPE_DOUBLE:
1177 value = _dbus_demarshal_double (str, byte_order, pos, &pos);
1179 if (!dbus_dict_set_double (*dict, keys[i], value))
1184 case DBUS_TYPE_STRING:
1188 value = _dbus_demarshal_string (str, byte_order, pos, &pos);
1193 if (!dbus_dict_set_string (*dict, keys[i], value))
1203 case DBUS_TYPE_BOOLEAN_ARRAY:
1205 unsigned char *value;
1208 if (!_dbus_demarshal_byte_array (str, byte_order, pos, &pos, &value, &len))
1211 if (!dbus_dict_set_boolean_array (*dict, keys[i], value, len))
1220 case DBUS_TYPE_INT32_ARRAY:
1222 dbus_int32_t *value;
1225 if (!_dbus_demarshal_int32_array (str, byte_order, pos, &pos, &value, &len))
1228 if (!dbus_dict_set_int32_array (*dict, keys[i], value, len))
1237 case DBUS_TYPE_UINT32_ARRAY:
1239 dbus_uint32_t *value;
1242 if (!_dbus_demarshal_uint32_array (str, byte_order, pos, &pos, &value, &len))
1245 if (!dbus_dict_set_uint32_array (*dict, keys[i], value, len))
1254 case DBUS_TYPE_DOUBLE_ARRAY:
1259 if (!_dbus_demarshal_double_array (str, byte_order, pos, &pos, &value, &len))
1262 if (!dbus_dict_set_double_array (*dict, keys[i], value, len))
1271 case DBUS_TYPE_BYTE_ARRAY:
1273 unsigned char *value;
1276 if (!_dbus_demarshal_byte_array (str, byte_order, pos, &pos, &value, &len))
1279 if (!dbus_dict_set_byte_array (*dict, keys[i], value, len))
1288 case DBUS_TYPE_STRING_ARRAY:
1293 if (!_dbus_demarshal_string_array (str, byte_order, pos, &pos, &value, &len))
1296 if (!dbus_dict_set_string_array (*dict, keys[i], (const char **)value, len))
1298 dbus_free_string_array (value);
1302 dbus_free_string_array (value);
1306 _dbus_warn ("unknown value type %d\n", value_type);
1307 _dbus_assert_not_reached ("unknown value arg");
1311 dbus_free_string_array (keys);
1315 dbus_free_string_array (keys);
1316 dbus_dict_unref (*dict);
1322 * Returns the position right after the end of an argument. PERFORMS
1323 * NO VALIDATION WHATSOEVER. The message must have been previously
1326 * @param str a string
1327 * @param byte_order the byte order to use
1328 * @param pos the pos where the arg starts
1329 * @param end_pos pointer where the position right
1330 * after the end position will follow
1331 * @returns TRUE if more data exists after the arg
1334 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1341 if (pos >= _dbus_string_get_length (str))
1344 _dbus_string_get_const_data_len (str, &data, pos, 1);
1348 case DBUS_TYPE_INVALID:
1356 case DBUS_TYPE_BOOLEAN:
1360 case DBUS_TYPE_INT32:
1361 *end_pos = _DBUS_ALIGN_VALUE (pos + 1, sizeof (dbus_int32_t)) + sizeof (dbus_int32_t);
1365 case DBUS_TYPE_UINT32:
1366 *end_pos = _DBUS_ALIGN_VALUE (pos + 1, sizeof (dbus_uint32_t)) + sizeof (dbus_uint32_t);
1370 case DBUS_TYPE_DOUBLE:
1371 *end_pos = _DBUS_ALIGN_VALUE (pos + 1, sizeof (double)) + sizeof (double);
1375 case DBUS_TYPE_STRING:
1379 /* Demarshal the length */
1380 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1382 *end_pos = pos + len + 1;
1386 case DBUS_TYPE_BOOLEAN_ARRAY:
1387 case DBUS_TYPE_BYTE_ARRAY:
1391 /* Demarshal the length */
1392 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1394 *end_pos = pos + len;
1398 case DBUS_TYPE_INT32_ARRAY:
1402 /* Demarshal the length */
1403 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &new_pos);
1405 *end_pos = _DBUS_ALIGN_VALUE (new_pos, sizeof (dbus_int32_t))
1406 + (len * sizeof (dbus_int32_t));
1410 case DBUS_TYPE_UINT32_ARRAY:
1414 /* Demarshal the length */
1415 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &new_pos);
1417 *end_pos = _DBUS_ALIGN_VALUE (new_pos, sizeof (dbus_uint32_t))
1418 + (len * sizeof (dbus_uint32_t));
1422 case DBUS_TYPE_DOUBLE_ARRAY:
1426 /* Demarshal the length */
1427 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &new_pos);
1429 *end_pos = _DBUS_ALIGN_VALUE (new_pos, sizeof (double))
1430 + (len * sizeof (double));
1434 case DBUS_TYPE_STRING_ARRAY:
1438 /* Demarshal the length */
1439 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1441 for (i = 0; i < len; i++)
1445 /* Demarshal string length */
1446 str_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1454 case DBUS_TYPE_DICT:
1458 /* Demarshal the length */
1459 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1461 for (i = 0; i < len; i++)
1465 /* Demarshal string length */
1466 str_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1470 /* Now check the values */
1471 for (i = 0; i < len; i++)
1473 if (!_dbus_marshal_get_arg_end_pos (str, byte_order, pos, &pos))
1482 _dbus_warn ("Unknown message arg type %d\n", *data);
1483 _dbus_assert_not_reached ("Unknown message argument type\n");
1487 if (*end_pos > _dbus_string_get_length (str))
1494 * Demarshals and validates a length; returns < 0 if the validation
1495 * fails. The length is required to be small enough that
1496 * len*sizeof(double) will not overflow, and small enough to fit in a
1497 * signed integer. DOES NOT check whether the length points
1498 * beyond the end of the string, because it doesn't know the
1499 * size of array elements.
1501 * @param str the string
1502 * @param byte_order the byte order
1503 * @param pos the unaligned string position (snap to next aligned)
1504 * @param new_pos return location for new position.
1507 demarshal_and_validate_len (const DBusString *str,
1512 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1515 _dbus_assert (new_pos != NULL);
1517 if ((align_4 + 4) > _dbus_string_get_length (str))
1519 _dbus_verbose ("not enough room in message for array length\n");
1523 if (!_dbus_string_validate_nul (str, pos,
1526 _dbus_verbose ("array length alignment padding not initialized to nul\n");
1530 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1532 /* note that the len may be a number of doubles, so we need it to be
1533 * at least SIZE_T_MAX / 8, but make it smaller just to keep things
1534 * sane. We end up using ints for most sizes to avoid unsigned mess
1535 * so limit to maximum 32-bit signed int divided by at least 8, more
1536 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1538 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1539 if (len > MAX_ARRAY_LENGTH)
1541 _dbus_verbose ("array length %u exceeds maximum of %u\n",
1542 len, MAX_ARRAY_LENGTH);
1550 validate_string (const DBusString *str,
1552 int len_without_nul,
1555 *end_pos = pos + len_without_nul + 1;
1557 if (*end_pos > _dbus_string_get_length (str))
1559 _dbus_verbose ("string length outside length of the message\n");
1563 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1565 _dbus_verbose ("string arg not nul-terminated\n");
1569 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1571 _dbus_verbose ("string is not valid UTF-8\n");
1579 * Validates an argument, checking that it is well-formed, for example
1580 * no ludicrous length fields, strings are nul-terminated, etc.
1581 * Returns the end position of the argument in end_pos, and
1582 * returns #TRUE if a valid arg begins at "pos"
1584 * @todo security: need to audit this function.
1586 * @param str a string
1587 * @param byte_order the byte order to use
1588 * @param pos the pos where the arg starts (offset of its typecode)
1589 * @param end_pos pointer where the position right
1590 * after the end position will follow
1591 * @returns #TRUE if the arg is valid.
1594 _dbus_marshal_validate_arg (const DBusString *str,
1601 if (pos >= _dbus_string_get_length (str))
1604 _dbus_string_get_const_data_len (str, &data, pos, 1);
1608 case DBUS_TYPE_INVALID:
1616 case DBUS_TYPE_BOOLEAN:
1620 if (2 > _dbus_string_get_length (str) - pos)
1622 _dbus_verbose ("no room for boolean value\n");
1626 c = _dbus_string_get_byte (str, pos + 1);
1628 if (c != 0 && c != 1)
1630 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1637 case DBUS_TYPE_INT32:
1638 case DBUS_TYPE_UINT32:
1640 int align_4 = _DBUS_ALIGN_VALUE (pos + 1, 4);
1642 if (!_dbus_string_validate_nul (str, pos + 1,
1645 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
1649 *end_pos = align_4 + 4;
1653 case DBUS_TYPE_DOUBLE:
1655 int align_8 = _DBUS_ALIGN_VALUE (pos + 1, 8);
1657 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
1659 if (!_dbus_string_validate_nul (str, pos + 1,
1662 _dbus_verbose ("double alignment padding not initialized to nul\n");
1666 *end_pos = align_8 + 8;
1670 case DBUS_TYPE_STRING:
1674 /* Demarshal the length, which does NOT include
1677 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1681 if (!validate_string (str, pos, len, end_pos))
1686 case DBUS_TYPE_BOOLEAN_ARRAY:
1690 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1694 if (len > _dbus_string_get_length (str) - pos)
1696 _dbus_verbose ("boolean array length outside length of the message\n");
1703 unsigned char c = _dbus_string_get_byte (str, pos + i);
1705 if (c != 0 && c != 1)
1707 _dbus_verbose ("boolean value must be either 0 or 1, not %d (pos %d)\n", c, pos);
1713 *end_pos = pos + len;
1716 case DBUS_TYPE_BYTE_ARRAY:
1720 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1724 *end_pos = pos + len;
1728 case DBUS_TYPE_INT32_ARRAY:
1729 case DBUS_TYPE_UINT32_ARRAY:
1733 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1737 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned int) pos);
1739 *end_pos = pos + len * 4;
1743 case DBUS_TYPE_DOUBLE_ARRAY:
1748 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1756 align_8 = _DBUS_ALIGN_VALUE (pos, 8);
1757 if (!_dbus_string_validate_nul (str, pos,
1760 _dbus_verbose ("double array alignment padding not initialized to nul\n");
1764 *end_pos = align_8 + len * 8;
1769 case DBUS_TYPE_STRING_ARRAY:
1774 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1778 for (i = 0; i < len; i++)
1782 str_len = demarshal_and_validate_len (str, byte_order,
1787 if (!validate_string (str, pos, str_len, &pos))
1795 case DBUS_TYPE_DICT:
1800 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1804 for (i = 0; i < len; i++)
1808 str_len = demarshal_and_validate_len (str, byte_order,
1813 if (!validate_string (str, pos, str_len, &pos))
1817 /* Now validate each argument */
1818 for (i = 0; i < len; i++)
1820 if (pos >= _dbus_string_get_length (str))
1822 _dbus_verbose ("not enough values in dict\n");
1826 if (_dbus_string_get_byte (str, pos) == DBUS_TYPE_NIL)
1828 _dbus_verbose ("can't have NIL values in dicts\n");
1832 if (!_dbus_marshal_validate_arg (str, byte_order, pos, &pos))
1842 _dbus_verbose ("Unknown message arg type %d\n", *data);
1846 if (*end_pos > _dbus_string_get_length (str))
1854 * If in verbose mode, print a block of binary data.
1856 * @todo right now it prints even if not in verbose mode
1858 * @param data the data
1859 * @param len the length of the data
1862 _dbus_verbose_bytes (const unsigned char *data,
1866 const unsigned char *aligned;
1868 _dbus_assert (len >= 0);
1870 /* Print blanks on first row if appropriate */
1871 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1874 _dbus_assert (aligned <= data);
1876 if (aligned != data)
1878 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1879 while (aligned != data)
1881 _dbus_verbose (" ");
1886 /* now print the bytes */
1890 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1892 _dbus_verbose ("%4d\t%p: ",
1896 if (data[i] >= 32 &&
1898 _dbus_verbose (" '%c' ", data[i]);
1900 _dbus_verbose ("0x%s%x ",
1901 data[i] <= 0xf ? "0" : "", data[i]);
1905 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1908 _dbus_verbose ("BE: %d LE: %d",
1909 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1910 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1913 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1915 _dbus_verbose (" dbl: %g",
1916 *(double*)&data[i-8]);
1919 _dbus_verbose ("\n");
1923 _dbus_verbose ("\n");
1927 * Dump the given part of the string to verbose log.
1929 * @param str the string
1930 * @param start the start of range to dump
1931 * @param len length of range
1934 _dbus_verbose_bytes_of_string (const DBusString *str,
1941 real_len = _dbus_string_get_length (str);
1943 _dbus_assert (start >= 0);
1945 if (start > real_len)
1947 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1948 start, len, real_len);
1952 if ((start + len) > real_len)
1954 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1955 start, len, real_len);
1956 len = real_len - start;
1960 _dbus_string_get_const_data_len (str, &d, start, len);
1962 _dbus_verbose_bytes (d, len);
1967 #ifdef DBUS_BUILD_TESTS
1968 #include "dbus-test.h"
1972 _dbus_marshal_test (void)
1976 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
1977 int pos = 0, i, len;
1978 dbus_bool_t our_bool;
1979 dbus_int32_t our_int;
1980 dbus_uint32_t our_uint;
1982 const char *our_string;
1983 const unsigned char boolean_array[] = { TRUE, FALSE, FALSE, TRUE };
1984 const unsigned char *our_boolean_array;
1985 const dbus_int32_t int32_array[] = { 0x12345678, -1911, 0, 0xaffe, 0xedd1e };
1986 const dbus_int32_t *our_int32_array;
1987 const dbus_uint32_t uint32_array[] = { 0x12345678, 0, 0xdeadbeef, 0x87654321, 0xffffffff };
1988 const dbus_uint32_t *our_uint32_array;
1989 const double double_array[] = { 3.14159, 1.2345, 6.7890 };
1990 const double *our_double_array;
1991 const char *string_array[] = { "This", "Is", "A", "Test" };
1992 const char **our_string_array;
1995 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1996 _dbus_assert_not_reached ("failed to init string");
1998 /* Marshal doubles */
1999 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2000 _dbus_assert_not_reached ("could not marshal double value");
2001 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2002 _dbus_assert_not_reached ("demarshal failed");
2004 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2005 _dbus_assert_not_reached ("could not marshal double value");
2006 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2007 _dbus_assert_not_reached ("demarshal failed");
2009 /* Marshal signed integers */
2010 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2011 _dbus_assert_not_reached ("could not marshal signed integer value");
2012 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2013 _dbus_assert_not_reached ("demarshal failed");
2015 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2016 _dbus_assert_not_reached ("could not marshal signed integer value");
2017 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2018 _dbus_assert_not_reached ("demarshal failed");
2020 /* Marshal unsigned integers */
2021 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2022 _dbus_assert_not_reached ("could not marshal signed integer value");
2023 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2024 _dbus_assert_not_reached ("demarshal failed");
2026 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2027 _dbus_assert_not_reached ("could not marshal signed integer value");
2028 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2029 _dbus_assert_not_reached ("demarshal failed");
2031 /* Marshal strings */
2032 tmp1 = "This is the dbus test string";
2033 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2034 _dbus_assert_not_reached ("could not marshal string");
2035 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2036 if (!strcmp (tmp1, tmp2) == 0)
2037 _dbus_assert_not_reached ("demarshal failed");
2040 tmp1 = "This is the dbus test string";
2041 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2042 _dbus_assert_not_reached ("could not marshal string");
2043 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2044 if (!strcmp (tmp1, tmp2) == 0)
2045 _dbus_assert_not_reached ("demarshal failed");
2048 /* Marshal signed integer arrays */
2049 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2050 _dbus_assert_not_reached ("could not marshal integer array");
2051 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2052 _dbus_assert_not_reached ("could not demarshal integer array");
2055 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2060 dict = dbus_dict_new ();
2062 if (dbus_dict_get_value_type (dict, "foo") != DBUS_TYPE_NIL)
2063 _dbus_assert_not_reached ("didn't return DBUS_TYPE_NIL for non-existant entry");
2065 if (!dbus_dict_set_boolean (dict, "boolean", TRUE))
2066 _dbus_assert_not_reached ("could not add boolean value");
2068 if (!dbus_dict_set_int32 (dict, "int32", 0x12345678))
2069 _dbus_assert_not_reached ("could not add int32 value");
2071 if (!dbus_dict_set_uint32 (dict, "uint32", 0x87654321))
2072 _dbus_assert_not_reached ("could not add uint32 value");
2074 if (!dbus_dict_set_double (dict, "double", 3.14159))
2075 _dbus_assert_not_reached ("could not add double value");
2077 if (!dbus_dict_set_string (dict, "string", "test string"))
2078 _dbus_assert_not_reached ("could not add string value");
2080 if (!dbus_dict_set_boolean_array (dict, "boolean_array", boolean_array, 4))
2081 _dbus_assert_not_reached ("could not add boolean array");
2083 if (!dbus_dict_set_int32_array (dict, "int32_array", int32_array, 5))
2084 _dbus_assert_not_reached ("could not add int32 array");
2086 if (!dbus_dict_set_uint32_array (dict, "uint32_array", uint32_array, 5))
2087 _dbus_assert_not_reached ("could not add uint32 array");
2089 if (!dbus_dict_set_double_array (dict, "double_array", double_array, 3))
2090 _dbus_assert_not_reached ("could not add double array");
2092 if (!dbus_dict_set_string_array (dict, "string_array", string_array, 4))
2093 _dbus_assert_not_reached ("could not add string array");
2095 if (!_dbus_marshal_dict (&str, DBUS_BIG_ENDIAN, dict))
2096 _dbus_assert_not_reached ("could not marshal dict");
2098 dbus_dict_unref (dict);
2100 if (!_dbus_demarshal_dict (&str, DBUS_BIG_ENDIAN, pos, &pos, &dict))
2101 _dbus_assert_not_reached ("could not demarshal dict");
2103 if (!dbus_dict_get_boolean (dict, "boolean", &our_bool) ||
2105 _dbus_assert_not_reached ("could not get boolean value");
2107 if (!dbus_dict_get_int32 (dict, "int32", &our_int) || our_int != 0x12345678)
2108 _dbus_assert_not_reached ("could not get int32 value or int32 values differ");
2110 if (!dbus_dict_get_uint32 (dict, "uint32", &our_uint) || our_uint != 0x87654321)
2111 _dbus_assert_not_reached ("could not get uint32 value or uint32 values differ");
2113 if (!dbus_dict_get_double (dict, "double", &our_double)
2114 || our_double != 3.14159)
2115 _dbus_assert_not_reached ("could not get double value or double values differ");
2117 if (!dbus_dict_get_string (dict, "string", &our_string) || strcmp (our_string, "test string") != 0)
2118 _dbus_assert_not_reached ("could not get string value or string values differ");
2120 if (!dbus_dict_get_boolean_array (dict, "boolean_array", &our_boolean_array, &len) ||
2121 len != 4 || memcmp (boolean_array, our_boolean_array, 4) != 0)
2122 _dbus_assert_not_reached ("could not get boolean array value or boolean array values differ");
2124 if (!dbus_dict_get_int32_array (dict, "int32_array", &our_int32_array, &len) ||
2125 len != 5 || memcmp (int32_array, our_int32_array, 5 * sizeof (dbus_int32_t)) != 0)
2126 _dbus_assert_not_reached ("could not get int32 array value or int32 array values differ");
2128 if (!dbus_dict_get_uint32_array (dict, "uint32_array", &our_uint32_array, &len) ||
2129 len != 5 || memcmp (uint32_array, our_uint32_array, 5 * sizeof (dbus_uint32_t) ) != 0)
2130 _dbus_assert_not_reached ("could not get uint32 array value or uint32 array values differ");
2132 if (!dbus_dict_get_double_array (dict, "double_array", &our_double_array, &len) ||
2133 len != 3 || memcmp (double_array, our_double_array, 3 * sizeof (double)) != 0)
2134 _dbus_assert_not_reached ("could not get double array value or double array values differ");
2136 if (!dbus_dict_get_string_array (dict, "string_array", &our_string_array, &len))
2137 _dbus_assert_not_reached ("could not get string array value");
2140 _dbus_assert_not_reached ("string array lengths differ");
2142 for (i = 0; i < len; i++)
2144 if (strcmp (our_string_array[i], string_array[i]) != 0)
2145 _dbus_assert_not_reached ("string array fields differ");
2148 dbus_dict_unref (dict);
2150 _dbus_string_free (&str);
2156 #endif /* DBUS_BUILD_TESTS */