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);
355 return _dbus_string_append_len (str, value, len);
359 * Marshals a 32 bit signed integer array
361 * @param str the string to append the marshalled value to
362 * @param byte_order the byte order to use
363 * @param value the array
364 * @param len the length of the array
365 * @returns #TRUE on success
368 _dbus_marshal_int32_array (DBusString *str,
370 const dbus_int32_t *value,
373 int i, old_string_len;
375 old_string_len = _dbus_string_get_length (str);
377 if (!_dbus_marshal_uint32 (str, byte_order, len))
380 for (i = 0; i < len; i++)
381 if (!_dbus_marshal_int32 (str, byte_order, value[i]))
387 /* Restore previous length */
388 _dbus_string_set_length (str, old_string_len);
394 * Marshals a 32 bit unsigned integer array
396 * @param str the string to append the marshalled value to
397 * @param byte_order the byte order to use
398 * @param value the array
399 * @param len the length of the array
400 * @returns #TRUE on success
403 _dbus_marshal_uint32_array (DBusString *str,
405 const dbus_uint32_t *value,
408 int i, old_string_len;
410 old_string_len = _dbus_string_get_length (str);
412 if (!_dbus_marshal_uint32 (str, byte_order, len))
415 for (i = 0; i < len; i++)
416 if (!_dbus_marshal_uint32 (str, byte_order, value[i]))
422 /* Restore previous length */
423 _dbus_string_set_length (str, old_string_len);
429 * Marshals a double array
431 * @param str the string to append the marshalled value to
432 * @param byte_order the byte order to use
433 * @param value the array
434 * @param len the length of the array
435 * @returns #TRUE on success
438 _dbus_marshal_double_array (DBusString *str,
443 int i, old_string_len;
445 old_string_len = _dbus_string_get_length (str);
447 if (!_dbus_marshal_uint32 (str, byte_order, len))
450 for (i = 0; i < len; i++)
451 if (!_dbus_marshal_double (str, byte_order, value[i]))
457 /* Restore previous length */
458 _dbus_string_set_length (str, old_string_len);
464 * Marshals a string array
466 * @param str the string to append the marshalled value to
467 * @param byte_order the byte order to use
468 * @param value the array
469 * @param len the length of the array
470 * @returns #TRUE on success
473 _dbus_marshal_string_array (DBusString *str,
478 int i, old_string_len;
480 old_string_len = _dbus_string_get_length (str);
482 if (!_dbus_marshal_uint32 (str, byte_order, len))
485 for (i = 0; i < len; i++)
486 if (!_dbus_marshal_string (str, byte_order, value[i]))
492 /* Restore previous length */
493 _dbus_string_set_length (str, old_string_len);
501 * @param str the string to append the marshalled value to
502 * @param byte_order the byte order to use
503 * @param dict the dict
504 * @returns #TRUE on success
507 _dbus_marshal_dict (DBusString *str,
515 old_string_len = _dbus_string_get_length (str);
517 if (!dbus_dict_get_keys (dict, &keys, &len))
523 if (!_dbus_marshal_string_array (str, byte_order,
527 for (i = 0; i < len; i++)
531 value_type = dbus_dict_get_value_type (dict, keys[i]);
533 if (!_dbus_string_append_byte (str, value_type))
536 switch (dbus_dict_get_value_type (dict, keys[i]))
538 case DBUS_TYPE_BOOLEAN:
542 if (!dbus_dict_get_boolean (dict, keys[i], &value))
545 if (!_dbus_string_append_byte (str, (value != FALSE)))
551 case DBUS_TYPE_INT32:
555 if (!dbus_dict_get_int32 (dict, keys[i], &value))
558 if (!_dbus_marshal_int32 (str, byte_order, value))
563 case DBUS_TYPE_UINT32:
567 if (!dbus_dict_get_uint32 (dict, keys[i], &value))
570 if (!_dbus_marshal_uint32 (str, byte_order, value))
575 case DBUS_TYPE_DOUBLE:
579 if (!dbus_dict_get_double (dict, keys[i], &value))
582 if (!_dbus_marshal_double (str, byte_order, value))
587 case DBUS_TYPE_INT32_ARRAY:
589 const dbus_int32_t *value;
592 if (!dbus_dict_get_int32_array (dict, keys[i], &value, &len))
595 if (!_dbus_marshal_int32_array (str, byte_order, value, len))
600 case DBUS_TYPE_STRING:
604 if (!dbus_dict_get_string (dict, keys[i], &value))
607 if (!_dbus_marshal_string (str, byte_order, value))
612 case DBUS_TYPE_BOOLEAN_ARRAY:
614 const unsigned char *value;
617 if (!dbus_dict_get_boolean_array (dict, keys[i], &value, &len))
620 if (!_dbus_marshal_byte_array (str, byte_order, value, len))
625 case DBUS_TYPE_UINT32_ARRAY:
627 const dbus_uint32_t *value;
630 if (!dbus_dict_get_uint32_array (dict, keys[i], &value, &len))
633 if (!_dbus_marshal_uint32_array (str, byte_order, value, len))
638 case DBUS_TYPE_DOUBLE_ARRAY:
643 if (!dbus_dict_get_double_array (dict, keys[i], &value, &len))
646 if (!_dbus_marshal_double_array (str, byte_order, value, len))
651 case DBUS_TYPE_STRING_ARRAY:
656 if (!dbus_dict_get_string_array (dict, keys[i], &value, &len))
659 if (!_dbus_marshal_string_array (str, byte_order, value, len))
665 _dbus_warn ("unknwon value type %d\n", dbus_dict_get_value_type (dict, keys[i]));
666 _dbus_assert_not_reached ("unknown value type in dict");
670 dbus_free_string_array (keys);
676 dbus_free_string_array (keys);
678 /* Restore previous length */
679 _dbus_string_set_length (str, old_string_len);
686 * Demarshals a double.
688 * @param str the string containing the data
689 * @param byte_order the byte order
690 * @param pos the position in the string
691 * @param new_pos the new position of the string
692 * @returns the demarshaled double.
695 _dbus_demarshal_double (const DBusString *str,
703 pos = _DBUS_ALIGN_VALUE (pos, sizeof (double));
705 _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (double));
707 retval = *(double *)buffer;
709 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
710 swap_bytes ((unsigned char *)&retval, sizeof (double));
713 *new_pos = pos + sizeof (double);
719 * Demarshals a 32 bit signed integer.
721 * @param str the string containing the data
722 * @param byte_order the byte order
723 * @param pos the position in the string
724 * @param new_pos the new position of the string
725 * @returns the demarshaled integer.
728 _dbus_demarshal_int32 (const DBusString *str,
733 const DBusRealString *real = (const DBusRealString*) str;
735 pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_int32_t));
738 *new_pos = pos + sizeof (dbus_int32_t);
740 if (byte_order == DBUS_LITTLE_ENDIAN)
741 return DBUS_INT32_FROM_LE (*(dbus_int32_t*)(real->str + pos));
743 return DBUS_INT32_FROM_BE (*(dbus_int32_t*)(real->str + pos));
747 * Demarshals a 32 bit unsigned integer.
749 * @param str the string containing the data
750 * @param byte_order the byte order
751 * @param pos the position in the string
752 * @param new_pos the new position of the string
753 * @returns the demarshaled integer.
756 _dbus_demarshal_uint32 (const DBusString *str,
761 const DBusRealString *real = (const DBusRealString*) str;
763 pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
766 *new_pos = pos + sizeof (dbus_uint32_t);
768 if (byte_order == DBUS_LITTLE_ENDIAN)
769 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(real->str + pos));
771 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(real->str + pos));
775 * Demarshals an UTF-8 string.
777 * @todo Should we check the string to make sure
778 * that it's valid UTF-8, and maybe "fix" the string
781 * @todo Should probably demarshal to a DBusString,
782 * having memcpy() in here is Evil(tm).
784 * @param str the string containing the data
785 * @param byte_order the byte order
786 * @param pos the position in the string
787 * @param new_pos the new position of the string
788 * @returns the demarshaled string.
791 _dbus_demarshal_string (const DBusString *str,
800 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
802 retval = dbus_malloc (len + 1);
807 _dbus_string_get_const_data_len (str, &data, pos, len);
812 memcpy (retval, data, len + 1);
815 *new_pos = pos + len + 1;
821 * Demarshals a byte array.
823 * @todo Should probably demarshal to a DBusString,
824 * having memcpy() in here is Evil(tm).
826 * @param str the string containing the data
827 * @param byte_order the byte order
828 * @param pos the position in the string
829 * @param new_pos the new position of the string
830 * @param array_len length of the demarshaled data
831 * @returns the demarshaled data.
834 _dbus_demarshal_byte_array (const DBusString *str,
841 unsigned char *retval;
844 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
846 retval = dbus_malloc (len);
851 _dbus_string_get_const_data_len (str, &data, pos, len);
856 memcpy (retval, data, len);
859 *new_pos = pos + len;
868 * Demarshals a 32 bit signed integer array.
870 * @param str the string containing the data
871 * @param byte_order the byte order
872 * @param pos the position in the string
873 * @param new_pos the new position of the string
874 * @param array_len length of the demarshaled data
875 * @returns the demarshaled data.
878 _dbus_demarshal_int32_array (const DBusString *str,
885 dbus_int32_t *retval;
887 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
889 retval = dbus_new (dbus_int32_t, len);
894 for (i = 0; i < len; i++)
895 retval[i] = _dbus_demarshal_int32 (str, byte_order, pos, &pos);
907 * Demarshals a 32 bit unsigned integer array.
909 * @param str the string containing the data
910 * @param byte_order the byte order
911 * @param pos the position in the string
912 * @param new_pos the new position of the string
913 * @param array_len length of the demarshaled data
914 * @returns the demarshaled data.
917 _dbus_demarshal_uint32_array (const DBusString *str,
924 dbus_uint32_t *retval;
926 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
928 retval = dbus_new (dbus_uint32_t, len);
933 for (i = 0; i < len; i++)
934 retval[i] = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
946 * Demarshals a double array.
948 * @param str the string containing the data
949 * @param byte_order the byte order
950 * @param pos the position in the string
951 * @param new_pos the new position of the string
952 * @param array_len length of the demarshaled data
953 * @returns the demarshaled data.
956 _dbus_demarshal_double_array (const DBusString *str,
965 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
967 retval = dbus_new (double, len);
972 for (i = 0; i < len; i++)
973 retval[i] = _dbus_demarshal_double (str, byte_order, pos, &pos);
985 * Demarshals a string array.
987 * @param str the string containing the data
988 * @param byte_order the byte order
989 * @param pos the position in the string
990 * @param new_pos the new position of the string
991 * @param array_len length of the demarshaled data
992 * @returns the demarshaled data.
995 _dbus_demarshal_string_array (const DBusString *str,
1004 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1006 retval = dbus_new (char *, len + 1);
1013 for (i = 0; i < len; i++)
1015 retval[i] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1030 for (j = 0; j < i; j++)
1031 dbus_free (retval[i]);
1040 * @param str the string containing the data
1041 * @param byte_order the byte order
1042 * @param pos the position in the string
1043 * @param new_pos the new position in the string
1044 * @returns the demarshalled dict
1047 _dbus_demarshal_dict (const DBusString *str,
1056 dict = dbus_dict_new ();
1060 keys = _dbus_demarshal_string_array (str, byte_order, pos, &pos, &len);
1065 for (i = 0; i < len; i++)
1069 switch ((value_type = _dbus_string_get_byte (str, pos ++)))
1071 case DBUS_TYPE_BOOLEAN:
1075 value = _dbus_string_get_byte (str, pos ++);
1077 if (!dbus_dict_set_boolean (dict, keys[i], value))
1081 case DBUS_TYPE_INT32:
1085 value = _dbus_demarshal_int32 (str, byte_order, pos, &pos);
1087 if (!dbus_dict_set_int32 (dict, keys[i], value))
1092 case DBUS_TYPE_UINT32:
1094 dbus_uint32_t value;
1096 value = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1098 if (!dbus_dict_set_uint32 (dict, keys[i], value))
1103 case DBUS_TYPE_DOUBLE:
1107 value = _dbus_demarshal_double (str, byte_order, pos, &pos);
1109 if (!dbus_dict_set_double (dict, keys[i], value))
1114 case DBUS_TYPE_STRING:
1118 value = _dbus_demarshal_string (str, byte_order, pos, &pos);
1123 if (!dbus_dict_set_string (dict, keys[i], value))
1133 case DBUS_TYPE_BOOLEAN_ARRAY:
1135 unsigned char *value;
1138 value = _dbus_demarshal_byte_array (str, byte_order, pos, &pos, &len);
1143 if (!dbus_dict_set_boolean_array (dict, keys[i], value, len))
1152 case DBUS_TYPE_INT32_ARRAY:
1154 dbus_int32_t *value;
1157 value = _dbus_demarshal_int32_array (str, byte_order, pos, &pos, &len);
1162 if (!dbus_dict_set_int32_array (dict, keys[i], value, len))
1171 case DBUS_TYPE_UINT32_ARRAY:
1173 dbus_uint32_t *value;
1176 value = _dbus_demarshal_uint32_array (str, byte_order, pos, &pos, &len);
1181 if (!dbus_dict_set_uint32_array (dict, keys[i], value, len))
1190 case DBUS_TYPE_DOUBLE_ARRAY:
1195 value = _dbus_demarshal_double_array (str, byte_order, pos, &pos, &len);
1200 if (!dbus_dict_set_double_array (dict, keys[i], value, len))
1209 case DBUS_TYPE_STRING_ARRAY:
1214 value = _dbus_demarshal_string_array (str, byte_order, pos, &pos, &len);
1219 if (!dbus_dict_set_string_array (dict, keys[i], value, len))
1221 dbus_free_string_array (value);
1225 dbus_free_string_array (value);
1229 _dbus_warn ("unknown value type %d\n", value_type);
1230 _dbus_assert_not_reached ("unknown value arg");
1234 dbus_free_string_array (keys);
1238 dbus_free_string_array (keys);
1239 dbus_dict_unref (dict);
1244 * Returns the position right after the end of an argument. PERFORMS
1245 * NO VALIDATION WHATSOEVER. The message must have been previously
1248 * @param str a string
1249 * @param byte_order the byte order to use
1250 * @param pos the pos where the arg starts
1251 * @param end_pos pointer where the position right
1252 * after the end position will follow
1253 * @returns TRUE if more data exists after the arg
1256 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1263 if (pos >= _dbus_string_get_length (str))
1266 _dbus_string_get_const_data_len (str, &data, pos, 1);
1270 case DBUS_TYPE_INVALID:
1278 case DBUS_TYPE_BOOLEAN:
1282 case DBUS_TYPE_INT32:
1283 *end_pos = _DBUS_ALIGN_VALUE (pos + 1, sizeof (dbus_int32_t)) + sizeof (dbus_int32_t);
1287 case DBUS_TYPE_UINT32:
1288 *end_pos = _DBUS_ALIGN_VALUE (pos + 1, sizeof (dbus_uint32_t)) + sizeof (dbus_uint32_t);
1292 case DBUS_TYPE_DOUBLE:
1293 *end_pos = _DBUS_ALIGN_VALUE (pos + 1, sizeof (double)) + sizeof (double);
1297 case DBUS_TYPE_STRING:
1301 /* Demarshal the length */
1302 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1304 *end_pos = pos + len + 1;
1308 case DBUS_TYPE_BOOLEAN_ARRAY:
1309 case DBUS_TYPE_BYTE_ARRAY:
1313 /* Demarshal the length */
1314 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1316 *end_pos = pos + len;
1320 case DBUS_TYPE_INT32_ARRAY:
1324 /* Demarshal the length */
1325 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &new_pos);
1327 *end_pos = _DBUS_ALIGN_VALUE (new_pos, sizeof (dbus_int32_t))
1328 + (len * sizeof (dbus_int32_t));
1332 case DBUS_TYPE_UINT32_ARRAY:
1336 /* Demarshal the length */
1337 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &new_pos);
1339 *end_pos = _DBUS_ALIGN_VALUE (new_pos, sizeof (dbus_uint32_t))
1340 + (len * sizeof (dbus_uint32_t));
1344 case DBUS_TYPE_DOUBLE_ARRAY:
1348 /* Demarshal the length */
1349 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &new_pos);
1351 *end_pos = _DBUS_ALIGN_VALUE (new_pos, sizeof (double))
1352 + (len * sizeof (double));
1356 case DBUS_TYPE_STRING_ARRAY:
1360 /* Demarshal the length */
1361 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1363 for (i = 0; i < len; i++)
1367 /* Demarshal string length */
1368 str_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1376 case DBUS_TYPE_DICT:
1380 /* Demarshal the length */
1381 len = _dbus_demarshal_uint32 (str, byte_order, pos + 1, &pos);
1383 for (i = 0; i < len; i++)
1387 /* Demarshal string length */
1388 str_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1392 /* Now check the values */
1393 for (i = 0; i < len; i++)
1395 if (!_dbus_marshal_get_arg_end_pos (str, byte_order, pos, &pos))
1404 _dbus_warn ("Unknown message arg type %d\n", *data);
1405 _dbus_assert_not_reached ("Unknown message argument type\n");
1409 if (*end_pos > _dbus_string_get_length (str))
1416 * Demarshals and validates a length; returns < 0 if the validation
1417 * fails. The length is required to be small enough that
1418 * len*sizeof(double) will not overflow, and small enough to fit in a
1419 * signed integer. DOES NOT check whether the length points
1420 * beyond the end of the string, because it doesn't know the
1421 * size of array elements.
1423 * @param str the string
1424 * @param byte_order the byte order
1425 * @param pos the unaligned string position (snap to next aligned)
1426 * @param new_pos return location for new position.
1429 demarshal_and_validate_len (const DBusString *str,
1434 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1437 _dbus_assert (new_pos != NULL);
1439 if ((align_4 + 4) >= _dbus_string_get_length (str))
1441 _dbus_verbose ("not enough room in message for array length\n");
1445 if (!_dbus_string_validate_nul (str, pos,
1448 _dbus_verbose ("array length alignment padding not initialized to nul\n");
1452 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1454 /* note that the len may be a number of doubles, so we need it to be
1455 * at least SIZE_T_MAX / 8, but make it smaller just to keep things
1456 * sane. We end up using ints for most sizes to avoid unsigned mess
1457 * so limit to maximum 32-bit signed int divided by at least 8, more
1458 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1460 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1461 if (len > MAX_ARRAY_LENGTH)
1463 _dbus_verbose ("array length %u exceeds maximum of %u\n",
1464 len, MAX_ARRAY_LENGTH);
1472 validate_string (const DBusString *str,
1474 int len_without_nul,
1477 *end_pos = pos + len_without_nul + 1;
1479 if (*end_pos > _dbus_string_get_length (str))
1481 _dbus_verbose ("string length outside length of the message\n");
1485 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1487 _dbus_verbose ("string arg not nul-terminated\n");
1491 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1493 _dbus_verbose ("string is not valid UTF-8\n");
1501 * Validates an argument, checking that it is well-formed, for example
1502 * no ludicrous length fields, strings are nul-terminated, etc.
1503 * Returns the end position of the argument in end_pos, and
1504 * returns #TRUE if a valid arg begins at "pos"
1506 * @todo security: need to audit this function.
1508 * @param str a string
1509 * @param byte_order the byte order to use
1510 * @param pos the pos where the arg starts (offset of its typecode)
1511 * @param end_pos pointer where the position right
1512 * after the end position will follow
1513 * @returns #TRUE if the arg is valid.
1516 _dbus_marshal_validate_arg (const DBusString *str,
1523 if (pos >= _dbus_string_get_length (str))
1526 _dbus_string_get_const_data_len (str, &data, pos, 1);
1530 case DBUS_TYPE_INVALID:
1538 case DBUS_TYPE_BOOLEAN:
1542 if (2 > _dbus_string_get_length (str) - pos)
1544 _dbus_verbose ("no room for boolean value\n");
1548 c = _dbus_string_get_byte (str, pos + 1);
1550 if (c != 0 && c != 1)
1552 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1559 case DBUS_TYPE_INT32:
1560 case DBUS_TYPE_UINT32:
1562 int align_4 = _DBUS_ALIGN_VALUE (pos + 1, 4);
1564 if (!_dbus_string_validate_nul (str, pos + 1,
1567 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
1571 *end_pos = align_4 + 4;
1575 case DBUS_TYPE_DOUBLE:
1577 int align_8 = _DBUS_ALIGN_VALUE (pos + 1, 8);
1579 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
1581 if (!_dbus_string_validate_nul (str, pos + 1,
1584 _dbus_verbose ("double alignment padding not initialized to nul\n");
1588 *end_pos = align_8 + 8;
1592 case DBUS_TYPE_STRING:
1596 /* Demarshal the length, which does NOT include
1599 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1603 if (!validate_string (str, pos, len, end_pos))
1608 case DBUS_TYPE_BOOLEAN_ARRAY:
1612 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1616 if (len > _dbus_string_get_length (str) - pos)
1618 _dbus_verbose ("boolean array length outside length of the message\n");
1625 unsigned char c = _dbus_string_get_byte (str, pos + i);
1627 if (c != 0 && c != 1)
1629 _dbus_verbose ("boolean value must be either 0 or 1, not %d (pos %d)\n", c, pos);
1635 *end_pos = pos + len;
1638 case DBUS_TYPE_BYTE_ARRAY:
1642 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1646 *end_pos = pos + len;
1650 case DBUS_TYPE_INT32_ARRAY:
1651 case DBUS_TYPE_UINT32_ARRAY:
1655 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1659 _dbus_assert (_DBUS_ALIGN_VALUE (pos, 4) == (unsigned int) pos);
1661 *end_pos = pos + len * 4;
1665 case DBUS_TYPE_DOUBLE_ARRAY:
1670 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1674 align_8 = _DBUS_ALIGN_VALUE (pos, 8);
1675 if (!_dbus_string_validate_nul (str, pos,
1678 _dbus_verbose ("double array alignment padding not initialized to nul\n");
1682 *end_pos = align_8 + len * 8;
1686 case DBUS_TYPE_STRING_ARRAY:
1691 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1695 for (i = 0; i < len; i++)
1699 str_len = demarshal_and_validate_len (str, byte_order,
1704 if (!validate_string (str, pos, str_len, &pos))
1712 case DBUS_TYPE_DICT:
1717 len = demarshal_and_validate_len (str, byte_order, pos + 1, &pos);
1721 for (i = 0; i < len; i++)
1725 str_len = demarshal_and_validate_len (str, byte_order,
1730 if (!validate_string (str, pos, str_len, &pos))
1734 /* Now validate each argument */
1735 for (i = 0; i < len; i++)
1737 if (pos >= _dbus_string_get_length (str))
1739 _dbus_verbose ("not enough values in dict\n");
1743 if (_dbus_string_get_byte (str, pos) == DBUS_TYPE_NIL)
1745 _dbus_verbose ("can't have NIL values in dicts\n");
1749 if (!_dbus_marshal_validate_arg (str, byte_order, pos, &pos))
1759 _dbus_verbose ("Unknown message arg type %d\n", *data);
1763 if (*end_pos > _dbus_string_get_length (str))
1771 * If in verbose mode, print a block of binary data.
1773 * @todo right now it prints even if not in verbose mode
1775 * @param data the data
1776 * @param len the length of the data
1779 _dbus_verbose_bytes (const unsigned char *data,
1783 const unsigned char *aligned;
1785 _dbus_assert (len >= 0);
1787 /* Print blanks on first row if appropriate */
1788 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1791 _dbus_assert (aligned <= data);
1793 if (aligned != data)
1795 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1796 while (aligned != data)
1798 _dbus_verbose (" ");
1803 /* now print the bytes */
1807 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1809 _dbus_verbose ("%4d\t%p: ",
1813 if (data[i] >= 32 &&
1815 _dbus_verbose (" '%c' ", data[i]);
1817 _dbus_verbose ("0x%s%x ",
1818 data[i] <= 0xf ? "0" : "", data[i]);
1822 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1825 _dbus_verbose ("BE: %d LE: %d",
1826 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1827 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1830 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1832 _dbus_verbose (" dbl: %g",
1833 *(double*)&data[i-8]);
1836 _dbus_verbose ("\n");
1840 _dbus_verbose ("\n");
1844 * Dump the given part of the string to verbose log.
1846 * @param str the string
1847 * @param start the start of range to dump
1848 * @param len length of range
1851 _dbus_verbose_bytes_of_string (const DBusString *str,
1858 real_len = _dbus_string_get_length (str);
1860 _dbus_assert (start >= 0);
1862 if (start > real_len)
1864 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1865 start, len, real_len);
1869 if ((start + len) > real_len)
1871 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1872 start, len, real_len);
1873 len = real_len - start;
1877 _dbus_string_get_const_data_len (str, &d, start, len);
1879 _dbus_verbose_bytes (d, len);
1884 #ifdef DBUS_BUILD_TESTS
1885 #include "dbus-test.h"
1889 _dbus_marshal_test (void)
1893 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
1894 int pos = 0, i, len;
1895 dbus_bool_t our_bool;
1896 dbus_int32_t our_int;
1897 dbus_uint32_t our_uint;
1899 const char *our_string;
1900 const unsigned char boolean_array[] = { TRUE, FALSE, FALSE, TRUE };
1901 const unsigned char *our_boolean_array;
1902 const dbus_int32_t int32_array[] = { 0x12345678, -1911, 0, 0xaffe, 0xedd1e };
1903 const dbus_int32_t *our_int32_array;
1904 const dbus_uint32_t uint32_array[] = { 0x12345678, 0, 0xdeadbeef, 0x87654321, 0xffffffff };
1905 const dbus_uint32_t *our_uint32_array;
1906 const double double_array[] = { 3.14159, 1.2345, 6.7890 };
1907 const double *our_double_array;
1908 const char *string_array[] = { "This", "Is", "A", "Test" };
1909 const char **our_string_array;
1912 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1913 _dbus_assert_not_reached ("failed to init string");
1915 /* Marshal doubles */
1916 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
1917 _dbus_assert_not_reached ("could not marshal double value");
1918 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
1919 _dbus_assert_not_reached ("demarshal failed");
1921 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
1922 _dbus_assert_not_reached ("could not marshal double value");
1923 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
1924 _dbus_assert_not_reached ("demarshal failed");
1926 /* Marshal signed integers */
1927 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
1928 _dbus_assert_not_reached ("could not marshal signed integer value");
1929 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
1930 _dbus_assert_not_reached ("demarshal failed");
1932 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
1933 _dbus_assert_not_reached ("could not marshal signed integer value");
1934 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
1935 _dbus_assert_not_reached ("demarshal failed");
1937 /* Marshal unsigned integers */
1938 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
1939 _dbus_assert_not_reached ("could not marshal signed integer value");
1940 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
1941 _dbus_assert_not_reached ("demarshal failed");
1943 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
1944 _dbus_assert_not_reached ("could not marshal signed integer value");
1945 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
1946 _dbus_assert_not_reached ("demarshal failed");
1948 /* Marshal strings */
1949 tmp1 = "This is the dbus test string";
1950 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
1951 _dbus_assert_not_reached ("could not marshal string");
1952 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
1953 if (!strcmp (tmp1, tmp2) == 0)
1954 _dbus_assert_not_reached ("demarshal failed");
1957 tmp1 = "This is the dbus test string";
1958 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
1959 _dbus_assert_not_reached ("could not marshal string");
1960 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
1961 if (!strcmp (tmp1, tmp2) == 0)
1962 _dbus_assert_not_reached ("demarshal failed");
1965 /* Marshal signed integer arrays */
1966 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
1967 _dbus_assert_not_reached ("could not marshal integer array");
1968 array2 = _dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &len);
1971 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
1976 dict = dbus_dict_new ();
1978 if (dbus_dict_get_value_type (dict, "foo") != DBUS_TYPE_NIL)
1979 _dbus_assert_not_reached ("didn't return DBUS_TYPE_NIL for non-existant entry");
1981 if (!dbus_dict_set_boolean (dict, "boolean", TRUE))
1982 _dbus_assert_not_reached ("could not add boolean value");
1984 if (!dbus_dict_set_int32 (dict, "int32", 0x12345678))
1985 _dbus_assert_not_reached ("could not add int32 value");
1987 if (!dbus_dict_set_uint32 (dict, "uint32", 0x87654321))
1988 _dbus_assert_not_reached ("could not add uint32 value");
1990 if (!dbus_dict_set_double (dict, "double", 3.14159))
1991 _dbus_assert_not_reached ("could not add double value");
1993 if (!dbus_dict_set_string (dict, "string", "test string"))
1994 _dbus_assert_not_reached ("could not add string value");
1996 if (!dbus_dict_set_boolean_array (dict, "boolean_array", boolean_array, 4))
1997 _dbus_assert_not_reached ("could not add boolean array");
1999 if (!dbus_dict_set_int32_array (dict, "int32_array", int32_array, 5))
2000 _dbus_assert_not_reached ("could not add int32 array");
2002 if (!dbus_dict_set_uint32_array (dict, "uint32_array", uint32_array, 5))
2003 _dbus_assert_not_reached ("could not add uint32 array");
2005 if (!dbus_dict_set_double_array (dict, "double_array", double_array, 3))
2006 _dbus_assert_not_reached ("could not add double array");
2008 if (!dbus_dict_set_string_array (dict, "string_array", string_array, 4))
2009 _dbus_assert_not_reached ("could not add string array");
2011 if (!_dbus_marshal_dict (&str, DBUS_BIG_ENDIAN, dict))
2012 _dbus_assert_not_reached ("could not marshal dict");
2014 dbus_dict_unref (dict);
2016 dict = _dbus_demarshal_dict (&str, DBUS_BIG_ENDIAN, pos, &pos);
2018 if (!dbus_dict_get_boolean (dict, "boolean", &our_bool) ||
2020 _dbus_assert_not_reached ("could not get boolean value");
2022 if (!dbus_dict_get_int32 (dict, "int32", &our_int) || our_int != 0x12345678)
2023 _dbus_assert_not_reached ("could not get int32 value or int32 values differ");
2025 if (!dbus_dict_get_uint32 (dict, "uint32", &our_uint) || our_uint != 0x87654321)
2026 _dbus_assert_not_reached ("could not get uint32 value or uint32 values differ");
2028 if (!dbus_dict_get_double (dict, "double", &our_double)
2029 || our_double != 3.14159)
2030 _dbus_assert_not_reached ("could not get double value or double values differ");
2032 if (!dbus_dict_get_string (dict, "string", &our_string) || strcmp (our_string, "test string") != 0)
2033 _dbus_assert_not_reached ("could not get string value or string values differ");
2035 if (!dbus_dict_get_boolean_array (dict, "boolean_array", &our_boolean_array, &len) ||
2036 len != 4 || memcmp (boolean_array, our_boolean_array, 4) != 0)
2037 _dbus_assert_not_reached ("could not get boolean array value or boolean array values differ");
2039 if (!dbus_dict_get_int32_array (dict, "int32_array", &our_int32_array, &len) ||
2040 len != 5 || memcmp (int32_array, our_int32_array, 5 * sizeof (dbus_int32_t)) != 0)
2041 _dbus_assert_not_reached ("could not get int32 array value or int32 array values differ");
2043 if (!dbus_dict_get_uint32_array (dict, "uint32_array", &our_uint32_array, &len) ||
2044 len != 5 || memcmp (uint32_array, our_uint32_array, 5 * sizeof (dbus_uint32_t) ) != 0)
2045 _dbus_assert_not_reached ("could not get uint32 array value or uint32 array values differ");
2047 if (!dbus_dict_get_double_array (dict, "double_array", &our_double_array, &len) ||
2048 len != 3 || memcmp (double_array, our_double_array, 3 * sizeof (double)) != 0)
2049 _dbus_assert_not_reached ("could not get double array value or double array values differ");
2051 if (!dbus_dict_get_string_array (dict, "string_array", &our_string_array, &len))
2052 _dbus_assert_not_reached ("could not get string array value");
2055 _dbus_assert_not_reached ("string array lengths differ");
2057 for (i = 0; i < len; i++)
2059 if (strcmp (our_string_array[i], string_array[i]) != 0)
2060 _dbus_assert_not_reached ("string array fields differ");
2063 dbus_dict_unref (dict);
2065 _dbus_string_free (&str);
2071 #endif /* DBUS_BUILD_TESTS */