1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-basic.c Marshalling routines for basic (primitive) types
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-internals.h"
26 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
27 #include "dbus-string-private.h"
29 #include "dbus-marshal-basic.h"
34 * @defgroup DBusMarshal marshaling and unmarshaling
35 * @ingroup DBusInternals
36 * @brief functions to marshal/unmarshal data from the wire
38 * Types and functions related to converting primitive data types from
39 * wire format to native machine format, and vice versa.
41 * A signature is just a string with multiple types one after the other.
42 * for example a type is "i" or "(ii)", a signature is "i(ii)"
43 * where i is int and (ii) is struct { int; int; }
49 unpack_4_octets (int byte_order,
50 const unsigned char *data)
52 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
54 if (byte_order == DBUS_LITTLE_ENDIAN)
55 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
57 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
60 #ifndef DBUS_HAVE_INT64
63 swap_bytes (unsigned char *data,
66 unsigned char *p1 = data;
67 unsigned char *p2 = data + len - 1;
71 unsigned char tmp = *p1;
79 #endif /* !DBUS_HAVE_INT64 */
82 * Union used to manipulate 8 bytes as if they
87 #ifdef DBUS_HAVE_INT64
88 dbus_int64_t s; /**< 64-bit integer */
89 dbus_uint64_t u; /**< 64-bit unsigned integer */
91 double d; /**< double */
95 unpack_8_octets (int byte_order,
96 const unsigned char *data)
100 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
101 _dbus_assert (sizeof (r) == 8);
103 #ifdef DBUS_HAVE_INT64
104 if (byte_order == DBUS_LITTLE_ENDIAN)
105 r.u = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
107 r.u = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
109 r.d = *(double*)data;
110 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
111 swap_bytes ((unsigned char*) &r, sizeof (r));
118 * Unpacks a 32 bit unsigned integer from a data pointer
120 * @param byte_order The byte order to use
121 * @param data the data pointer
122 * @returns the integer
125 _dbus_unpack_uint32 (int byte_order,
126 const unsigned char *data)
128 return unpack_4_octets (byte_order, data);
132 * Unpacks a 32 bit signed integer from a data pointer
134 * @param byte_order The byte order to use
135 * @param data the data pointer
136 * @returns the integer
139 _dbus_unpack_int32 (int byte_order,
140 const unsigned char *data)
142 return (dbus_int32_t) unpack_4_octets (byte_order, data);
145 #ifdef DBUS_HAVE_INT64
147 * Unpacks a 64 bit unsigned integer from a data pointer
149 * @param byte_order The byte order to use
150 * @param data the data pointer
151 * @returns the integer
154 _dbus_unpack_uint64 (int byte_order,
155 const unsigned char *data)
159 r = unpack_8_octets (byte_order, data);
165 * Unpacks a 64 bit signed integer from a data pointer
167 * @param byte_order The byte order to use
168 * @param data the data pointer
169 * @returns the integer
172 _dbus_unpack_int64 (int byte_order,
173 const unsigned char *data)
177 r = unpack_8_octets (byte_order, data);
182 #endif /* DBUS_HAVE_INT64 */
185 pack_4_octets (dbus_uint32_t value,
189 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
191 if ((byte_order) == DBUS_LITTLE_ENDIAN)
192 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
194 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
198 swap_8_octets (DBusOctets8 *value,
201 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
203 #ifdef DBUS_HAVE_INT64
204 value->u = DBUS_UINT64_SWAP_LE_BE (value->u);
206 swap_bytes ((unsigned char *)value, 8);
212 pack_8_octets (DBusOctets8 value,
216 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
218 #ifdef DBUS_HAVE_INT64
219 if ((byte_order) == DBUS_LITTLE_ENDIAN)
220 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u);
222 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u);
224 memcpy (data, &value, 8);
225 swap_8_octets ((DBusOctets8*)data, byte_order);
230 * Packs a 32 bit unsigned integer into a data pointer.
232 * @param value the value
233 * @param byte_order the byte order to use
234 * @param data the data pointer
237 _dbus_pack_uint32 (dbus_uint32_t value,
241 pack_4_octets (value, byte_order, data);
245 * Packs a 32 bit signed integer into a data pointer.
247 * @param value the value
248 * @param byte_order the byte order to use
249 * @param data the data pointer
252 _dbus_pack_int32 (dbus_int32_t value,
256 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
259 #ifdef DBUS_HAVE_INT64
261 * Packs a 64 bit unsigned integer into a data pointer.
263 * @param value the value
264 * @param byte_order the byte order to use
265 * @param data the data pointer
268 _dbus_pack_uint64 (dbus_uint64_t value,
274 pack_8_octets (r, byte_order, data);
278 * Packs a 64 bit signed integer into a data pointer.
280 * @param value the value
281 * @param byte_order the byte order to use
282 * @param data the data pointer
285 _dbus_pack_int64 (dbus_int64_t value,
291 pack_8_octets (r, byte_order, data);
293 #endif /* DBUS_HAVE_INT64 */
296 set_4_octets (DBusString *str,
303 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
304 byte_order == DBUS_BIG_ENDIAN);
306 data = _dbus_string_get_data_len (str, offset, 4);
308 _dbus_pack_uint32 (value, byte_order, data);
312 set_8_octets (DBusString *str,
319 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
320 byte_order == DBUS_BIG_ENDIAN);
322 data = _dbus_string_get_data_len (str, offset, 8);
324 pack_8_octets (value, byte_order, data);
328 * Sets the 4 bytes at the given offset to a marshaled signed integer,
329 * replacing anything found there previously.
331 * @param str the string to write the marshalled int to
332 * @param offset the byte offset where int should be written
333 * @param byte_order the byte order to use
334 * @param value the value
338 _dbus_marshal_set_int32 (DBusString *str,
343 set_4_octets (str, byte_order, offset, (dbus_uint32_t) value);
347 * Sets the 4 bytes at the given offset to a marshaled unsigned
348 * integer, replacing anything found there previously.
350 * @param str the string to write the marshalled int to
351 * @param offset the byte offset where int should be written
352 * @param byte_order the byte order to use
353 * @param value the value
357 _dbus_marshal_set_uint32 (DBusString *str,
362 set_4_octets (str, byte_order, offset, value);
365 #ifdef DBUS_HAVE_INT64
368 * Sets the 8 bytes at the given offset to a marshaled signed integer,
369 * replacing anything found there previously.
371 * @param str the string to write the marshalled int to
372 * @param offset the byte offset where int should be written
373 * @param byte_order the byte order to use
374 * @param value the value
378 _dbus_marshal_set_int64 (DBusString *str,
385 set_8_octets (str, byte_order, offset, r);
389 * Sets the 8 bytes at the given offset to a marshaled unsigned
390 * integer, replacing anything found there previously.
392 * @param str the string to write the marshalled int to
393 * @param offset the byte offset where int should be written
394 * @param byte_order the byte order to use
395 * @param value the value
399 _dbus_marshal_set_uint64 (DBusString *str,
406 set_8_octets (str, byte_order, offset, r);
408 #endif /* DBUS_HAVE_INT64 */
411 * Sets the existing marshaled string at the given offset with
412 * a new marshaled string. The given offset must point to
413 * an existing string or the wrong length will be deleted
414 * and replaced with the new string.
416 * Note: no attempt is made by this function to re-align
417 * any data which has been already marshalled after this
418 * string. Use with caution.
420 * @param str the string to write the marshalled string to
421 * @param offset the byte offset where string should be written
422 * @param byte_order the byte order to use
423 * @param value the value
424 * @param len the length to use
425 * @returns #TRUE on success
429 _dbus_marshal_set_string (DBusString *str,
432 const DBusString *value,
437 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
438 byte_order == DBUS_BIG_ENDIAN);
440 old_len = _dbus_demarshal_uint32 (str, byte_order,
443 if (!_dbus_string_replace_len (value, 0, len,
444 str, offset + 4, old_len))
447 _dbus_marshal_set_uint32 (str, byte_order,
454 * Sets the existing marshaled object path at the given offset to a new
455 * value. The given offset must point to an existing object path or this
456 * function doesn't make sense.
458 * @todo implement this function
460 * @param str the string to write the marshalled path to
461 * @param offset the byte offset where path should be written
462 * @param byte_order the byte order to use
463 * @param path the new path
464 * @param path_len number of elements in the path
467 _dbus_marshal_set_object_path (DBusString *str,
478 marshal_4_octets (DBusString *str,
483 _dbus_assert (sizeof (value) == 4);
485 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
486 value = DBUS_UINT32_SWAP_LE_BE (value);
488 return _dbus_string_insert_4_aligned (str, insert_at,
489 (const unsigned char *)&value);
493 marshal_8_octets (DBusString *str,
498 _dbus_assert (sizeof (value) == 8);
500 swap_8_octets (&value, byte_order);
502 return _dbus_string_insert_8_aligned (str, insert_at,
503 (const unsigned char *)&value);
507 * Marshals a double value.
509 * @param str the string to append the marshalled value to
510 * @param byte_order the byte order to use
511 * @param value the value
512 * @returns #TRUE on success
515 _dbus_marshal_double (DBusString *str,
521 return marshal_8_octets (str, _dbus_string_get_length (str),
526 * Marshals a 32 bit signed integer value.
528 * @param str the string to append the marshalled value to
529 * @param byte_order the byte order to use
530 * @param value the value
531 * @returns #TRUE on success
534 _dbus_marshal_int32 (DBusString *str,
538 return marshal_4_octets (str, _dbus_string_get_length (str),
539 byte_order, (dbus_uint32_t) value);
543 * Marshals a 32 bit unsigned integer value.
545 * @param str the string to append the marshalled value to
546 * @param byte_order the byte order to use
547 * @param value the value
548 * @returns #TRUE on success
551 _dbus_marshal_uint32 (DBusString *str,
555 return marshal_4_octets (str, _dbus_string_get_length (str),
560 #ifdef DBUS_HAVE_INT64
562 * Marshals a 64 bit signed integer value.
564 * @param str the string to append the marshalled value to
565 * @param byte_order the byte order to use
566 * @param value the value
567 * @returns #TRUE on success
570 _dbus_marshal_int64 (DBusString *str,
576 return marshal_8_octets (str, _dbus_string_get_length (str),
581 * Marshals a 64 bit unsigned integer value.
583 * @param str the string to append the marshalled value to
584 * @param byte_order the byte order to use
585 * @param value the value
586 * @returns #TRUE on success
589 _dbus_marshal_uint64 (DBusString *str,
595 return marshal_8_octets (str, _dbus_string_get_length (str),
599 #endif /* DBUS_HAVE_INT64 */
602 * Marshals a UTF-8 string
604 * @todo: If the string append fails we need to restore
605 * the old length. (also for other marshallers)
607 * @param str the string to append the marshalled value to
608 * @param byte_order the byte order to use
609 * @param value the string
610 * @returns #TRUE on success
613 _dbus_marshal_string (DBusString *str,
617 int len, old_string_len;
619 old_string_len = _dbus_string_get_length (str);
621 len = strlen (value);
623 if (!_dbus_marshal_uint32 (str, byte_order, len))
625 /* Restore the previous length */
626 _dbus_string_set_length (str, old_string_len);
631 return _dbus_string_append_len (str, value, len + 1);
635 * Marshals a UTF-8 string
637 * @todo: If the string append fails we need to restore
638 * the old length. (also for other marshallers)
640 * @param str the string to append the marshalled value to
641 * @param byte_order the byte order to use
642 * @param value the string
643 * @param len length of string to marshal in bytes
644 * @returns #TRUE on success
647 _dbus_marshal_string_len (DBusString *str,
654 old_string_len = _dbus_string_get_length (str);
656 if (!_dbus_marshal_uint32 (str, byte_order, len))
658 /* Restore the previous length */
659 _dbus_string_set_length (str, old_string_len);
664 if (!_dbus_string_append_len (str, value, len))
668 if (!_dbus_string_lengthen (str, 1))
675 _dbus_marshal_signature (DBusString *str,
678 int len, old_string_len;
680 old_string_len = _dbus_string_get_length (str);
682 len = strlen (value);
684 _dbus_assert (len <= DBUS_MAXIMUM_SIGNATURE_LENGTH);
686 if (!_dbus_string_append_byte (str, len))
688 /* Restore the previous length */
689 _dbus_string_set_length (str, old_string_len);
694 return _dbus_string_append_len (str, value, len + 1);
698 * Marshals a byte array
700 * @param str the string to append the marshalled value to
701 * @param byte_order the byte order to use
702 * @param value the array
703 * @param len number of elements in the array
704 * @returns #TRUE on success
707 _dbus_marshal_byte_array (DBusString *str,
709 const unsigned char *value,
714 old_string_len = _dbus_string_get_length (str);
716 if (!_dbus_marshal_uint32 (str, byte_order, len))
718 /* Restore the previous length */
719 _dbus_string_set_length (str, old_string_len);
727 return _dbus_string_append_len (str, value, len);
731 marshal_4_octets_array (DBusString *str,
733 const dbus_uint32_t *value,
739 old_string_len = _dbus_string_get_length (str);
741 if (!_dbus_marshal_uint32 (str, byte_order, len * 4))
744 array_start = _dbus_string_get_length (str);
746 if (!_dbus_string_append_len (str, (const unsigned char*) value,
750 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
752 const unsigned char *d;
753 const unsigned char *end;
755 d = _dbus_string_get_data (str) + array_start;
759 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
767 /* Restore previous length */
768 _dbus_string_set_length (str, old_string_len);
774 marshal_8_octets_array (DBusString *str,
776 const DBusOctets8 *value,
782 old_string_len = _dbus_string_get_length (str);
784 /* The array length is the length in bytes of the array,
785 * *excluding* alignment padding.
787 if (!_dbus_marshal_uint32 (str, byte_order, len * 8))
790 array_start = _dbus_string_get_length (str);
792 /* Note that we do alignment padding unconditionally
793 * even if the array is empty; this means that
794 * padding + len is always equal to the number of bytes
798 if (!_dbus_string_align_length (str, 8))
801 if (!_dbus_string_append_len (str, (const unsigned char*) value,
805 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
807 const unsigned char *d;
808 const unsigned char *end;
810 d = _dbus_string_get_data (str) + array_start;
814 #ifdef DBUS_HAVE_INT64
815 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
817 swap_bytes ((unsigned char*) d, 8);
826 /* Restore previous length */
827 _dbus_string_set_length (str, old_string_len);
833 * Marshals a 32 bit signed integer array
835 * @param str the string to append the marshalled value to
836 * @param byte_order the byte order to use
837 * @param value the array
838 * @param len the length of the array
839 * @returns #TRUE on success
842 _dbus_marshal_int32_array (DBusString *str,
844 const dbus_int32_t *value,
847 return marshal_4_octets_array (str, byte_order,
848 (const dbus_uint32_t*) value,
853 * Marshals a 32 bit unsigned integer array
855 * @param str the string to append the marshalled value to
856 * @param byte_order the byte order to use
857 * @param value the array
858 * @param len the length of the array
859 * @returns #TRUE on success
862 _dbus_marshal_uint32_array (DBusString *str,
864 const dbus_uint32_t *value,
867 return marshal_4_octets_array (str, byte_order,
872 #ifdef DBUS_HAVE_INT64
875 * Marshals a 64 bit signed integer array
877 * @param str the string to append the marshalled value to
878 * @param byte_order the byte order to use
879 * @param value the array
880 * @param len the length of the array
881 * @returns #TRUE on success
884 _dbus_marshal_int64_array (DBusString *str,
886 const dbus_int64_t *value,
889 return marshal_8_octets_array (str, byte_order,
890 (const DBusOctets8*) value,
895 * Marshals a 64 bit unsigned integer array
897 * @param str the string to append the marshalled value to
898 * @param byte_order the byte order to use
899 * @param value the array
900 * @param len the length of the array
901 * @returns #TRUE on success
904 _dbus_marshal_uint64_array (DBusString *str,
906 const dbus_uint64_t *value,
909 return marshal_8_octets_array (str, byte_order,
910 (const DBusOctets8*) value,
914 #endif /* DBUS_HAVE_INT64 */
917 * Marshals a double array
919 * @param str the string to append the marshalled value to
920 * @param byte_order the byte order to use
921 * @param value the array
922 * @param len the length of the array
923 * @returns #TRUE on success
926 _dbus_marshal_double_array (DBusString *str,
931 return marshal_8_octets_array (str, byte_order,
932 (const DBusOctets8*) value,
937 * Marshals a string array
939 * @param str the string to append the marshalled value to
940 * @param byte_order the byte order to use
941 * @param value the array
942 * @param len the length of the array
943 * @returns #TRUE on success
946 _dbus_marshal_string_array (DBusString *str,
951 int i, old_string_len, array_start;
953 old_string_len = _dbus_string_get_length (str);
955 /* Set the length to 0 temporarily */
956 if (!_dbus_marshal_uint32 (str, byte_order, 0))
959 array_start = _dbus_string_get_length (str);
961 for (i = 0; i < len; i++)
962 if (!_dbus_marshal_string (str, byte_order, value[i]))
965 /* Write the length now that we know it */
966 _dbus_marshal_set_uint32 (str, byte_order,
967 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
968 _dbus_string_get_length (str) - array_start);
973 /* Restore previous length */
974 _dbus_string_set_length (str, old_string_len);
980 * Marshals an object path value.
982 * @param str the string to append the marshalled value to
983 * @param byte_order the byte order to use
984 * @param path the path
985 * @param path_len length of the path
986 * @returns #TRUE on success
989 _dbus_marshal_object_path (DBusString *str,
994 int array_start, old_string_len;
997 old_string_len = _dbus_string_get_length (str);
999 /* Set the length to 0 temporarily */
1000 if (!_dbus_marshal_uint32 (str, byte_order, 0))
1003 array_start = _dbus_string_get_length (str);
1006 while (i < path_len)
1008 if (!_dbus_string_append_byte (str, '/'))
1011 if (!_dbus_string_append (str, path[0]))
1017 /* Write the length now that we know it */
1018 _dbus_marshal_set_uint32 (str, byte_order,
1019 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
1020 _dbus_string_get_length (str) - array_start);
1025 /* Restore the previous length */
1026 _dbus_string_set_length (str, old_string_len);
1031 static dbus_uint32_t
1032 demarshal_4_octets (const DBusString *str,
1037 const DBusRealString *real = (const DBusRealString*) str;
1039 pos = _DBUS_ALIGN_VALUE (pos, 4);
1044 return unpack_4_octets (byte_order, real->str + pos);
1048 demarshal_8_octets (const DBusString *str,
1053 const DBusRealString *real = (const DBusRealString*) str;
1055 pos = _DBUS_ALIGN_VALUE (pos, 8);
1060 return unpack_8_octets (byte_order, real->str + pos);
1064 * Demarshals a double.
1066 * @param str the string containing the data
1067 * @param byte_order the byte order
1068 * @param pos the position in the string
1069 * @param new_pos the new position of the string
1070 * @returns the demarshaled double.
1073 _dbus_demarshal_double (const DBusString *str,
1080 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1086 * Demarshals a 32 bit signed integer.
1088 * @param str the string containing the data
1089 * @param byte_order the byte order
1090 * @param pos the position in the string
1091 * @param new_pos the new position of the string
1092 * @returns the demarshaled integer.
1095 _dbus_demarshal_int32 (const DBusString *str,
1100 return (dbus_int32_t) demarshal_4_octets (str, byte_order, pos, new_pos);
1104 * Demarshals a 32 bit unsigned integer.
1106 * @param str the string containing the data
1107 * @param byte_order the byte order
1108 * @param pos the position in the string
1109 * @param new_pos the new position of the string
1110 * @returns the demarshaled integer.
1113 _dbus_demarshal_uint32 (const DBusString *str,
1118 return demarshal_4_octets (str, byte_order, pos, new_pos);
1121 #ifdef DBUS_HAVE_INT64
1124 * Demarshals a 64 bit signed integer.
1126 * @param str the string containing the data
1127 * @param byte_order the byte order
1128 * @param pos the position in the string
1129 * @param new_pos the new position of the string
1130 * @returns the demarshaled integer.
1133 _dbus_demarshal_int64 (const DBusString *str,
1140 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1146 * Demarshals a 64 bit unsigned integer.
1148 * @param str the string containing the data
1149 * @param byte_order the byte order
1150 * @param pos the position in the string
1151 * @param new_pos the new position of the string
1152 * @returns the demarshaled integer.
1155 _dbus_demarshal_uint64 (const DBusString *str,
1162 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1167 #endif /* DBUS_HAVE_INT64 */
1170 * Demarshals a basic type
1172 * @param str the string containing the data
1173 * @param type type of value to demarshal
1174 * @param value pointer to return value data
1175 * @param byte_order the byte order
1176 * @param pos pointer to position in the string,
1177 * updated on return to new position
1180 _dbus_demarshal_basic_type (const DBusString *str,
1186 const char *str_data = _dbus_string_get_const_data (str);
1190 case DBUS_TYPE_BYTE:
1191 case DBUS_TYPE_BOOLEAN:
1192 *(unsigned char *) value = _dbus_string_get_byte (str, *pos);
1195 case DBUS_TYPE_INT32:
1196 case DBUS_TYPE_UINT32:
1197 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1198 *(dbus_uint32_t *) value = *(dbus_uint32_t *)(str_data + *pos);
1199 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1200 *(dbus_uint32_t *) value = DBUS_UINT32_SWAP_LE_BE (*(dbus_uint32_t *) value);
1203 #ifdef DBUS_HAVE_INT64
1204 case DBUS_TYPE_INT64:
1205 case DBUS_TYPE_UINT64:
1206 #endif /* DBUS_HAVE_INT64 */
1207 case DBUS_TYPE_DOUBLE:
1208 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1209 memcpy (value, str_data + *pos, 8);
1210 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1211 #ifdef DBUS_HAVE_INT64
1212 *(dbus_uint64_t *) value = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t *) value);
1214 swap_bytes (value, 8);
1218 case DBUS_TYPE_STRING:
1219 case DBUS_TYPE_OBJECT_PATH:
1223 len = _dbus_demarshal_uint32 (str, byte_order, *pos, pos);
1225 *(const char**) value = str_data + *pos;
1227 *pos += len + 1; /* length plus nul */
1230 case DBUS_TYPE_SIGNATURE:
1234 len = _dbus_string_get_byte (str, *pos);
1237 *(const char**) value = str_data + *pos;
1239 *pos += len + 1; /* length plus nul */
1243 _dbus_verbose ("type %s not a basic type\n",
1244 _dbus_type_to_string (type));
1245 _dbus_assert_not_reached ("not a basic type");
1251 * Demarshals an UTF-8 string.
1253 * @todo Should we check the string to make sure
1254 * that it's valid UTF-8, and maybe "fix" the string
1257 * @todo Should probably demarshal to a DBusString,
1258 * having memcpy() in here is Evil(tm).
1260 * @param str the string containing the data
1261 * @param byte_order the byte order
1262 * @param pos the position in the string
1263 * @param new_pos the new position of the string
1264 * @returns the demarshaled string.
1267 _dbus_demarshal_string (const DBusString *str,
1276 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1278 retval = dbus_malloc (len + 1);
1283 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1288 memcpy (retval, data, len + 1);
1291 *new_pos = pos + len + 1;
1297 * Demarshals a byte array.
1299 * @todo Should probably demarshal to a DBusString,
1300 * having memcpy() in here is Evil(tm).
1302 * @param str the string containing the data
1303 * @param byte_order the byte order
1304 * @param pos the position in the string
1305 * @param new_pos the new position of the string
1306 * @param array the array
1307 * @param array_len length of the demarshaled data
1309 * @returns #TRUE on success
1312 _dbus_demarshal_byte_array (const DBusString *str,
1316 unsigned char **array,
1320 unsigned char *retval;
1323 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1336 retval = dbus_malloc (len);
1341 data = _dbus_string_get_const_data_len (str, pos, len);
1349 memcpy (retval, data, len);
1352 *new_pos = pos + len;
1361 demarshal_4_octets_array (const DBusString *str,
1365 dbus_uint32_t **array,
1369 dbus_uint32_t *retval;
1372 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1386 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1390 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1392 for (i = 0; i < len; i++)
1393 retval[i] = DBUS_UINT32_SWAP_LE_BE (retval[i]);
1397 *new_pos = pos + byte_len;
1406 demarshal_8_octets_array (const DBusString *str,
1410 DBusOctets8 **array,
1414 DBusOctets8 *retval;
1417 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1419 pos = _DBUS_ALIGN_VALUE (pos, 8);
1434 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1438 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1440 for (i = 0; i < len; i++)
1442 #ifdef DBUS_HAVE_INT64
1443 retval[i].u = DBUS_UINT64_SWAP_LE_BE (retval[i].u);
1445 swap_bytes ((unsigned char *) &retval[i], 8);
1451 *new_pos = pos + byte_len;
1460 * Demarshals a 32 bit signed integer array.
1462 * @param str the string containing the data
1463 * @param byte_order the byte order
1464 * @param pos the position in the string
1465 * @param new_pos the new position of the string
1466 * @param array the array
1467 * @param array_len length of the demarshaled data
1468 * @returns #TRUE on success
1471 _dbus_demarshal_int32_array (const DBusString *str,
1475 dbus_int32_t **array,
1478 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1479 (dbus_uint32_t**) array, array_len);
1483 * Demarshals a 32 bit unsigned integer array.
1485 * @param str the string containing the data
1486 * @param byte_order the byte order
1487 * @param pos the position in the string
1488 * @param new_pos the new position of the string
1489 * @param array the array
1490 * @param array_len length of the demarshaled data
1491 * @returns #TRUE on success
1494 _dbus_demarshal_uint32_array (const DBusString *str,
1498 dbus_uint32_t **array,
1501 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1505 #ifdef DBUS_HAVE_INT64
1508 * Demarshals a 64 bit signed integer array.
1510 * @param str the string containing the data
1511 * @param byte_order the byte order
1512 * @param pos the position in the string
1513 * @param new_pos the new position of the string
1514 * @param array the array
1515 * @param array_len length of the demarshaled data
1516 * @returns #TRUE on success
1519 _dbus_demarshal_int64_array (const DBusString *str,
1523 dbus_int64_t **array,
1526 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1527 (DBusOctets8**) array, array_len);
1531 * Demarshals a 64 bit unsigned integer array.
1533 * @param str the string containing the data
1534 * @param byte_order the byte order
1535 * @param pos the position in the string
1536 * @param new_pos the new position of the string
1537 * @param array the array
1538 * @param array_len length of the demarshaled data
1539 * @returns #TRUE on success
1542 _dbus_demarshal_uint64_array (const DBusString *str,
1546 dbus_uint64_t **array,
1549 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1550 (DBusOctets8**) array, array_len);
1553 #endif /* DBUS_HAVE_INT64 */
1556 * Demarshals a double array.
1558 * @param str the string containing the data
1559 * @param byte_order the byte order
1560 * @param pos the position in the string
1561 * @param new_pos the new position of the string
1562 * @param array the array
1563 * @param array_len length of the demarshaled data
1564 * @returns #TRUE on success
1567 _dbus_demarshal_double_array (const DBusString *str,
1574 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1575 (DBusOctets8**) array, array_len);
1580 * Demarshals an array of basic types
1582 * @param str the string containing the data
1583 * @param element_type type of array elements to demarshal
1584 * @param array pointer to pointer to array data
1585 * @param array_len pointer to array length
1586 * @param byte_order the byte order
1587 * @param pos pointer to position in the string,
1588 * updated on return to new position
1591 _dbus_demarshal_basic_type_array (const DBusString *str,
1598 switch (element_type)
1600 case DBUS_TYPE_BOOLEAN:
1601 /* FIXME: do we want to post-normalize these ? */
1602 case DBUS_TYPE_BYTE:
1603 return _dbus_demarshal_byte_array (str, byte_order, *pos, pos,
1604 (unsigned char **)array, array_len);
1606 case DBUS_TYPE_INT32:
1607 case DBUS_TYPE_UINT32:
1608 return demarshal_4_octets_array (str, byte_order, *pos, pos,
1609 (dbus_uint32_t **)array, array_len);
1611 #ifdef DBUS_HAVE_INT64
1612 case DBUS_TYPE_INT64:
1613 case DBUS_TYPE_UINT64:
1614 #endif /* DBUS_HAVE_INT64 */
1615 case DBUS_TYPE_DOUBLE:
1616 return demarshal_8_octets_array (str, byte_order, *pos, pos,
1617 (DBusOctets8**) array, array_len);
1619 _dbus_assert_not_reached ("not a basic type");
1626 * Demarshals a string array.
1628 * @param str the string containing the data
1629 * @param byte_order the byte order
1630 * @param pos the position in the string
1631 * @param new_pos the new position of the string
1632 * @param array the array
1633 * @param array_len location for length of the demarshaled data or NULL
1634 * @returns #TRUE on success
1637 _dbus_demarshal_string_array (const DBusString *str,
1649 bytes_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1664 end_pos = pos + bytes_len;
1666 retval = dbus_new (char *, allocated);
1671 while (pos < end_pos)
1673 retval[len] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1675 if (retval[len] == NULL)
1680 if (len >= allocated - 1) /* -1 for NULL termination */
1683 newp = dbus_realloc (retval,
1684 sizeof (char*) * allocated * 2);
1704 for (i = 0; i < len; i++)
1705 dbus_free (retval[i]);
1711 /** Set to 1 to get a bunch of spew about disassembling the path string */
1712 #define VERBOSE_DECOMPOSE 0
1715 * Decompose an object path. A path of just "/" is
1716 * represented as an empty vector of strings.
1718 * @param data the path data
1719 * @param len the length of the path string
1720 * @param path address to store new object path
1721 * @param path_len length of stored path
1724 _dbus_decompose_path (const char* data,
1733 _dbus_assert (data != NULL);
1735 #if VERBOSE_DECOMPOSE
1736 _dbus_verbose ("Decomposing path \"%s\"\n",
1749 retval = dbus_new0 (char*, n_components + 1);
1762 while (j < len && data[j] != '/')
1765 /* Now [i, j) is the path component */
1766 _dbus_assert (i < j);
1767 _dbus_assert (data[i] != '/');
1768 _dbus_assert (j == len || data[j] == '/');
1770 #if VERBOSE_DECOMPOSE
1771 _dbus_verbose (" (component in [%d,%d))\n",
1775 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1776 if (retval[comp] == NULL)
1778 dbus_free_string_array (retval);
1781 retval[comp][j-i] = '\0';
1782 #if VERBOSE_DECOMPOSE
1783 _dbus_verbose (" (component %d = \"%s\")\n",
1784 comp, retval[comp]);
1790 _dbus_assert (i == len);
1794 *path_len = n_components;
1800 * Demarshals an object path. A path of just "/" is
1801 * represented as an empty vector of strings.
1803 * @param str the string containing the data
1804 * @param byte_order the byte order
1805 * @param pos the position in the string
1806 * @param new_pos the new position of the string
1807 * @param path address to store new object path
1808 * @param path_len length of stored path
1811 _dbus_demarshal_object_path (const DBusString *str,
1821 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1822 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1824 if (!_dbus_decompose_path (data, len, path, path_len))
1828 *new_pos = pos + len + 1;
1834 * Skips over a basic type, reporting the following position.
1836 * @param str the string containing the data
1837 * @param type type of value to demarshal
1838 * @param byte_order the byte order
1839 * @param pos pointer to position in the string,
1840 * updated on return to new position
1843 _dbus_marshal_skip_basic_type (const DBusString *str,
1850 case DBUS_TYPE_BYTE:
1851 case DBUS_TYPE_BOOLEAN:
1854 case DBUS_TYPE_INT32:
1855 case DBUS_TYPE_UINT32:
1856 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
1859 #ifdef DBUS_HAVE_INT64
1860 case DBUS_TYPE_INT64:
1861 case DBUS_TYPE_UINT64:
1862 #endif /* DBUS_HAVE_INT64 */
1863 case DBUS_TYPE_DOUBLE:
1864 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
1867 case DBUS_TYPE_STRING:
1868 case DBUS_TYPE_OBJECT_PATH:
1872 len = _dbus_demarshal_uint32 (str, byte_order, *pos, pos);
1874 *pos += len + 1; /* length plus nul */
1877 case DBUS_TYPE_SIGNATURE:
1881 len = _dbus_string_get_byte (str, *pos);
1883 *pos += len + 2; /* length byte plus length plus nul */
1887 _dbus_verbose ("type %s not a basic type\n",
1888 _dbus_type_to_string (type));
1889 _dbus_assert_not_reached ("not a basic type");
1895 * Skips an array, returning the next position.
1897 * @param str the string containing the data
1898 * @param byte_order the byte order
1899 * @param pos pointer to position in the string,
1900 * updated on return to new position
1903 _dbus_marshal_skip_array (const DBusString *str,
1909 len = _dbus_demarshal_uint32 (str, byte_order, *pos, pos);
1911 /* FIXME we need to insert alignment padding according to array type */
1917 * Returns the position right after the end of an argument. PERFORMS
1918 * NO VALIDATION WHATSOEVER. The message must have been previously
1921 * @param str a string
1922 * @param byte_order the byte order to use
1923 * @param type the type of the argument
1924 * @param pos the pos where the arg starts
1925 * @param end_pos pointer where the position right
1926 * after the end position will follow
1927 * @returns TRUE if more data exists after the arg
1930 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1936 if (pos >= _dbus_string_get_length (str))
1941 case DBUS_TYPE_INVALID:
1945 case DBUS_TYPE_BYTE:
1949 case DBUS_TYPE_BOOLEAN:
1953 case DBUS_TYPE_INT32:
1954 case DBUS_TYPE_UINT32:
1955 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
1958 case DBUS_TYPE_INT64:
1959 case DBUS_TYPE_UINT64:
1960 case DBUS_TYPE_DOUBLE:
1962 *end_pos = _DBUS_ALIGN_VALUE (pos, 8) + 8;
1965 case DBUS_TYPE_OBJECT_PATH:
1966 case DBUS_TYPE_STRING:
1970 /* Demarshal the length */
1971 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1973 *end_pos = pos + len + 1;
1977 case DBUS_TYPE_ARRAY:
1981 /* Demarshal the length */
1982 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1984 /* FIXME needs to align to the right boundary for the array type */
1985 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + len;
1990 _dbus_warn ("Unknown message arg type %d\n", type);
1991 _dbus_assert_not_reached ("Unknown message argument type\n");
1995 if (*end_pos > _dbus_string_get_length (str))
2002 * Demarshals and validates a length; returns < 0 if the validation
2003 * fails. The length is required to be small enough that
2004 * len*sizeof(double) will not overflow, and small enough to fit in a
2005 * signed integer. DOES NOT check whether the length points
2006 * beyond the end of the string, because it doesn't know the
2007 * size of array elements.
2009 * @param str the string
2010 * @param byte_order the byte order
2011 * @param pos the unaligned string position (snap to next aligned)
2012 * @param new_pos return location for new position.
2015 demarshal_and_validate_len (const DBusString *str,
2020 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
2023 _dbus_assert (new_pos != NULL);
2025 if ((align_4 + 4) > _dbus_string_get_length (str))
2027 _dbus_verbose ("not enough room in message for array length\n");
2031 if (!_dbus_string_validate_nul (str, pos,
2034 _dbus_verbose ("array length alignment padding not initialized to nul at %d\n", pos);
2038 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
2040 /* note that the len is the number of bytes, so we need it to be
2041 * at least SIZE_T_MAX, but make it smaller just to keep things
2042 * sane. We end up using ints for most sizes to avoid unsigned mess
2043 * so limit to maximum 32-bit signed int divided by at least 8, more
2044 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
2046 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
2047 if (len > MAX_ARRAY_LENGTH)
2049 _dbus_verbose ("array length %u exceeds maximum of %u at pos %d\n",
2050 len, MAX_ARRAY_LENGTH, pos);
2058 validate_string (const DBusString *str,
2060 int len_without_nul,
2063 *end_pos = pos + len_without_nul + 1;
2065 if (*end_pos > _dbus_string_get_length (str))
2067 _dbus_verbose ("string length outside length of the message\n");
2071 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
2073 _dbus_verbose ("string arg not nul-terminated\n");
2077 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
2079 _dbus_verbose ("string is not valid UTF-8\n");
2087 * Validates and returns a typecode at a specific position
2090 * @param str a string
2091 * @param type the type of the argument
2092 * @param pos the pos where the typecode starts
2093 * @param end_pos pointer where the position right
2094 * after the end position will follow
2095 * @returns #TRUE if the type is valid.
2098 _dbus_marshal_validate_type (const DBusString *str,
2105 if (pos >= _dbus_string_get_length (str))
2108 data = _dbus_string_get_const_data_len (str, pos, 1);
2110 if (_dbus_type_is_valid (*data))
2113 if (end_pos != NULL)
2118 _dbus_verbose ("'%c' %d invalid type code\n", (int) *data, (int) *data);
2123 /* Faster validator for array data that doesn't call
2124 * validate_arg for each value
2127 validate_array_data (const DBusString *str,
2138 case DBUS_TYPE_INVALID:
2142 case DBUS_TYPE_OBJECT_PATH:
2143 case DBUS_TYPE_STRING:
2144 case DBUS_TYPE_ARRAY:
2145 /* This clean recursion to validate_arg is what we
2146 * are doing logically for all types, but we don't
2147 * really want to call validate_arg for every byte
2148 * in a byte array, so the primitive types are
2153 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
2154 type, array_type_pos, pos, &pos))
2159 case DBUS_TYPE_BYTE:
2163 case DBUS_TYPE_BOOLEAN:
2168 c = _dbus_string_get_byte (str, pos);
2170 if (!(c == 0 || c == 1))
2172 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
2180 case DBUS_TYPE_INT32:
2181 case DBUS_TYPE_UINT32:
2182 /* Call validate arg one time to check alignment padding
2185 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
2186 type, array_type_pos, pos, &pos))
2188 pos = _DBUS_ALIGN_VALUE (end, 4);
2191 case DBUS_TYPE_INT64:
2192 case DBUS_TYPE_UINT64:
2193 case DBUS_TYPE_DOUBLE:
2194 /* Call validate arg one time to check alignment padding
2197 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
2198 type, array_type_pos, pos, &pos))
2200 pos = _DBUS_ALIGN_VALUE (end, 8);
2204 _dbus_verbose ("Unknown message arg type %d\n", type);
2214 * Validates an argument of a specific type, checking that it
2215 * is well-formed, for example no ludicrous length fields, strings
2216 * are nul-terminated, etc.
2217 * Returns the end position of the argument in end_pos, and
2218 * returns #TRUE if a valid arg begins at "pos"
2220 * @todo security: need to audit this function.
2222 * @param str a string
2223 * @param byte_order the byte order to use
2224 * @param depth current recursion depth, to prevent excessive recursion
2225 * @param type the type of the argument
2226 * @param array_type_pos the position of the current array type, or
2227 * -1 if not in an array
2228 * @param pos the pos where the arg starts
2229 * @param end_pos pointer where the position right
2230 * after the end position will follow
2231 * @returns #TRUE if the arg is valid.
2234 _dbus_marshal_validate_arg (const DBusString *str,
2242 if (pos > _dbus_string_get_length (str))
2244 _dbus_verbose ("Validation went off the end of the message\n");
2248 #define MAX_VALIDATION_DEPTH 32
2250 if (depth > MAX_VALIDATION_DEPTH)
2252 _dbus_verbose ("Maximum recursion depth reached validating message\n");
2258 case DBUS_TYPE_INVALID:
2262 case DBUS_TYPE_BYTE:
2263 if (1 > _dbus_string_get_length (str) - pos)
2265 _dbus_verbose ("no room for byte value\n");
2272 case DBUS_TYPE_BOOLEAN:
2276 if (1 > _dbus_string_get_length (str) - pos)
2278 _dbus_verbose ("no room for boolean value\n");
2282 c = _dbus_string_get_byte (str, pos);
2284 if (!(c == 0 || c == 1))
2286 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
2294 case DBUS_TYPE_INT32:
2295 case DBUS_TYPE_UINT32:
2297 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
2299 if (!_dbus_string_validate_nul (str, pos,
2302 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
2306 *end_pos = align_4 + 4;
2310 case DBUS_TYPE_INT64:
2311 case DBUS_TYPE_UINT64:
2312 case DBUS_TYPE_DOUBLE:
2314 int align_8 = _DBUS_ALIGN_VALUE (pos, 8);
2316 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
2318 if (!_dbus_string_validate_nul (str, pos,
2321 _dbus_verbose ("double/int64/uint64/objid alignment padding not initialized to nul at %d\n", pos);
2325 *end_pos = align_8 + 8;
2329 case DBUS_TYPE_OBJECT_PATH:
2330 case DBUS_TYPE_STRING:
2334 /* Demarshal the length, which does NOT include
2337 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2341 if (!validate_string (str, pos, len, end_pos))
2344 if (type == DBUS_TYPE_OBJECT_PATH)
2346 if (!_dbus_string_validate_path (str, pos, len))
2352 case DBUS_TYPE_ARRAY:
2358 if (array_type_pos == -1)
2360 array_type_pos = pos;
2364 if (!_dbus_marshal_validate_type (str, pos, &array_type, &pos))
2366 _dbus_verbose ("invalid array type\n");
2370 while (array_type == DBUS_TYPE_ARRAY);
2375 if (!_dbus_marshal_validate_type (str, array_type_pos, &array_type, NULL))
2377 _dbus_verbose ("invalid array type\n");
2381 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2384 _dbus_verbose ("invalid array length (<0)\n");
2388 if (len > _dbus_string_get_length (str) - pos)
2390 _dbus_verbose ("array length outside length of the message\n");
2396 if (len > 0 && !validate_array_data (str, byte_order, depth + 1,
2397 array_type, array_type_pos,
2400 _dbus_verbose ("invalid array data\n");
2406 /* This should not be able to happen, as long as validate_arg moves forward;
2407 * but the check is here just to be paranoid.
2409 _dbus_verbose ("array length %d specified was longer than actual array contents by %d\n",
2416 _dbus_verbose ("array contents exceeds array length %d by %d\n", len, pos - end);
2425 _dbus_verbose ("Unknown message arg type %d\n", type);
2429 if (*end_pos > _dbus_string_get_length (str))
2436 * Return #TRUE if the typecode is a valid typecode
2438 * @returns #TRUE if valid
2441 _dbus_type_is_valid (int typecode)
2445 case DBUS_TYPE_BYTE:
2446 case DBUS_TYPE_BOOLEAN:
2447 case DBUS_TYPE_INT32:
2448 case DBUS_TYPE_UINT32:
2449 case DBUS_TYPE_INT64:
2450 case DBUS_TYPE_UINT64:
2451 case DBUS_TYPE_DOUBLE:
2452 case DBUS_TYPE_STRING:
2453 case DBUS_TYPE_OBJECT_PATH:
2454 case DBUS_TYPE_ARRAY:
2455 case DBUS_TYPE_STRUCT:
2456 case DBUS_TYPE_VARIANT:
2465 * Gets the alignment requirement for the given type;
2466 * will be 1, 4, or 8.
2468 * @param typecode the type
2469 * @returns alignment of 1, 4, or 8
2472 _dbus_type_get_alignment (int typecode)
2476 case DBUS_TYPE_BYTE:
2477 case DBUS_TYPE_BOOLEAN:
2478 case DBUS_TYPE_VARIANT:
2479 case DBUS_TYPE_SIGNATURE:
2481 case DBUS_TYPE_INT32:
2482 case DBUS_TYPE_UINT32:
2483 /* this stuff is 4 since it starts with a length */
2484 case DBUS_TYPE_STRING:
2485 case DBUS_TYPE_OBJECT_PATH:
2486 case DBUS_TYPE_ARRAY:
2488 case DBUS_TYPE_INT64:
2489 case DBUS_TYPE_UINT64:
2490 case DBUS_TYPE_DOUBLE:
2491 /* struct is 8 since it could contain an 8-aligned item
2492 * and it's simpler to just always align structs to 8;
2493 * we want the amount of padding in a struct of a given
2494 * type to be predictable, not location-dependent.
2496 case DBUS_TYPE_STRUCT:
2500 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
2506 * If in verbose mode, print a block of binary data.
2508 * @todo right now it prints even if not in verbose mode
2510 * @param data the data
2511 * @param len the length of the data
2512 * @param offset where to start counting for byte indexes
2515 _dbus_verbose_bytes (const unsigned char *data,
2520 const unsigned char *aligned;
2522 _dbus_assert (len >= 0);
2524 /* Print blanks on first row if appropriate */
2525 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
2528 _dbus_assert (aligned <= data);
2530 if (aligned != data)
2532 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
2533 while (aligned != data)
2535 _dbus_verbose (" ");
2540 /* now print the bytes */
2544 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2546 _dbus_verbose ("%4d\t%p: ",
2547 offset + i, &data[i]);
2550 if (data[i] >= 32 &&
2552 _dbus_verbose (" '%c' ", data[i]);
2554 _dbus_verbose ("0x%s%x ",
2555 data[i] <= 0xf ? "0" : "", data[i]);
2559 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2562 _dbus_verbose ("BE: %d LE: %d",
2563 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
2564 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
2567 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
2569 _dbus_verbose (" dbl: %g",
2570 *(double*)&data[i-8]);
2573 _dbus_verbose ("\n");
2577 _dbus_verbose ("\n");
2581 * Dump the given part of the string to verbose log.
2583 * @param str the string
2584 * @param start the start of range to dump
2585 * @param len length of range
2588 _dbus_verbose_bytes_of_string (const DBusString *str,
2595 real_len = _dbus_string_get_length (str);
2597 _dbus_assert (start >= 0);
2599 if (start > real_len)
2601 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
2602 start, len, real_len);
2606 if ((start + len) > real_len)
2608 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
2609 start, len, real_len);
2610 len = real_len - start;
2613 d = _dbus_string_get_const_data_len (str, start, len);
2615 _dbus_verbose_bytes (d, len, start);
2619 * Marshals a basic type
2621 * @param str string to marshal to
2622 * @param insert_at where to insert the value
2623 * @param type type of value
2624 * @param value pointer to value
2625 * @param byte_order byte order
2626 * @returns #TRUE on success
2629 _dbus_marshal_basic_type (DBusString *str,
2639 case DBUS_TYPE_BYTE:
2640 case DBUS_TYPE_BOOLEAN:
2641 retval = _dbus_string_insert_byte (str, insert_at, *(const unsigned char *)value);
2643 case DBUS_TYPE_INT32:
2644 case DBUS_TYPE_UINT32:
2645 return marshal_4_octets (str, insert_at, byte_order, *(const dbus_uint32_t *)value);
2647 #ifdef DBUS_HAVE_INT64
2648 case DBUS_TYPE_INT64:
2649 case DBUS_TYPE_UINT64:
2652 r.u = *(const dbus_uint64_t *)value;
2653 retval = marshal_8_octets (str, insert_at, byte_order, r);
2656 #endif /* DBUS_HAVE_INT64 */
2657 case DBUS_TYPE_DOUBLE:
2660 r.d = *(const double *)value;
2661 retval = marshal_8_octets (str, insert_at, byte_order, r);
2664 case DBUS_TYPE_STRING:
2665 case DBUS_TYPE_OBJECT_PATH:
2666 retval = _dbus_marshal_string (str, byte_order, (const char*) value);
2668 case DBUS_TYPE_SIGNATURE:
2669 retval = _dbus_marshal_signature (str, (const char*) value);
2672 _dbus_assert_not_reached ("not a basic type");
2680 * Marshals a basic type array
2682 * @param str string to marshal to
2683 * @param insert_at where to insert the value
2684 * @param element_type type of array elements
2685 * @param value pointer to value
2686 * @param len length of value data in elements
2687 * @param byte_order byte order
2688 * @returns #TRUE on success
2691 _dbus_marshal_basic_type_array (DBusString *str,
2698 /* FIXME use the insert_at arg */
2700 switch (element_type)
2702 case DBUS_TYPE_BOOLEAN:
2703 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
2704 * should we here too ? */
2705 case DBUS_TYPE_BYTE:
2706 return _dbus_marshal_byte_array (str, byte_order, value, len);
2708 case DBUS_TYPE_INT32:
2709 case DBUS_TYPE_UINT32:
2710 return marshal_4_octets_array (str, byte_order, value, len);
2712 #ifdef DBUS_HAVE_INT64
2713 case DBUS_TYPE_INT64:
2714 case DBUS_TYPE_UINT64:
2715 #endif /* DBUS_HAVE_INT64 */
2716 case DBUS_TYPE_DOUBLE:
2717 return marshal_8_octets_array (str, byte_order, value, len);
2720 _dbus_assert_not_reached ("non basic type in array");
2728 #ifdef DBUS_BUILD_TESTS
2729 #include "dbus-test.h"
2733 _dbus_marshal_test (void)
2738 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
2739 #ifdef DBUS_HAVE_INT64
2740 dbus_int64_t array3[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
2741 DBUS_INT64_CONSTANT (0x456ffffffff),
2742 DBUS_INT64_CONSTANT (0x789ffffffff) }, *array4;
2747 if (!_dbus_string_init (&str))
2748 _dbus_assert_not_reached ("failed to init string");
2750 /* Marshal doubles */
2751 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2752 _dbus_assert_not_reached ("could not marshal double value");
2753 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2754 _dbus_assert_not_reached ("demarshal failed");
2756 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2757 _dbus_assert_not_reached ("could not marshal double value");
2758 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2759 _dbus_assert_not_reached ("demarshal failed");
2761 /* Marshal signed integers */
2762 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2763 _dbus_assert_not_reached ("could not marshal signed integer value");
2764 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2765 _dbus_assert_not_reached ("demarshal failed");
2767 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2768 _dbus_assert_not_reached ("could not marshal signed integer value");
2769 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2770 _dbus_assert_not_reached ("demarshal failed");
2772 /* Marshal unsigned integers */
2773 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2774 _dbus_assert_not_reached ("could not marshal signed integer value");
2775 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2776 _dbus_assert_not_reached ("demarshal failed");
2778 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2779 _dbus_assert_not_reached ("could not marshal signed integer value");
2780 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2781 _dbus_assert_not_reached ("demarshal failed");
2783 #ifdef DBUS_HAVE_INT64
2784 /* Marshal signed integers */
2785 if (!_dbus_marshal_int64 (&str, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2786 _dbus_assert_not_reached ("could not marshal signed integer value");
2787 if (_dbus_demarshal_int64 (&str, DBUS_BIG_ENDIAN, pos, &pos) != DBUS_INT64_CONSTANT (-0x123456789abc7))
2788 _dbus_assert_not_reached ("demarshal failed");
2790 if (!_dbus_marshal_int64 (&str, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2791 _dbus_assert_not_reached ("could not marshal signed integer value");
2792 if (_dbus_demarshal_int64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) != DBUS_INT64_CONSTANT (-0x123456789abc7))
2793 _dbus_assert_not_reached ("demarshal failed");
2795 /* Marshal unsigned integers */
2796 if (!_dbus_marshal_uint64 (&str, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2797 _dbus_assert_not_reached ("could not marshal signed integer value");
2798 if (!(_dbus_demarshal_uint64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2799 _dbus_assert_not_reached ("demarshal failed");
2801 if (!_dbus_marshal_uint64 (&str, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2802 _dbus_assert_not_reached ("could not marshal signed integer value");
2803 if (!(_dbus_demarshal_uint64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2804 _dbus_assert_not_reached ("demarshal failed");
2805 #endif /* DBUS_HAVE_INT64 */
2807 /* Marshal strings */
2808 tmp1 = "This is the dbus test string";
2809 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2810 _dbus_assert_not_reached ("could not marshal string");
2811 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2812 if (!strcmp (tmp1, tmp2) == 0)
2813 _dbus_assert_not_reached ("demarshal failed");
2816 tmp1 = "This is the dbus test string";
2817 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2818 _dbus_assert_not_reached ("could not marshal string");
2819 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2820 if (!strcmp (tmp1, tmp2) == 0)
2821 _dbus_assert_not_reached ("demarshal failed");
2824 /* Marshal signed integer arrays */
2825 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2826 _dbus_assert_not_reached ("could not marshal integer array");
2827 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2828 _dbus_assert_not_reached ("could not demarshal integer array");
2831 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2834 #ifdef DBUS_HAVE_INT64
2835 /* Marshal 64-bit signed integer arrays */
2836 if (!_dbus_marshal_int64_array (&str, DBUS_BIG_ENDIAN, array3, 3))
2837 _dbus_assert_not_reached ("could not marshal integer array");
2838 if (!_dbus_demarshal_int64_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array4, &len))
2839 _dbus_assert_not_reached ("could not demarshal integer array");
2842 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2845 /* set/pack 64-bit integers */
2846 _dbus_string_set_length (&str, 8);
2849 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
2850 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2852 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2853 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2854 _dbus_string_get_const_data (&str)));
2857 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
2858 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2860 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2861 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2862 _dbus_string_get_const_data (&str)));
2864 /* signed little pack */
2865 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2867 _dbus_string_get_data (&str));
2869 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2870 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2871 _dbus_string_get_const_data (&str)));
2873 /* signed big pack */
2874 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2876 _dbus_string_get_data (&str));
2878 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2879 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2880 _dbus_string_get_const_data (&str)));
2882 /* unsigned little */
2883 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
2884 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2886 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2887 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2888 _dbus_string_get_const_data (&str)));
2891 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
2892 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2894 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2895 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2896 _dbus_string_get_const_data (&str)));
2898 /* unsigned little pack */
2899 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2901 _dbus_string_get_data (&str));
2903 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2904 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2905 _dbus_string_get_const_data (&str)));
2907 /* unsigned big pack */
2908 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2910 _dbus_string_get_data (&str));
2912 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2913 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2914 _dbus_string_get_const_data (&str)));
2918 /* set/pack 32-bit integers */
2919 _dbus_string_set_length (&str, 4);
2922 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
2925 _dbus_assert (-0x123456 ==
2926 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2927 _dbus_string_get_const_data (&str)));
2930 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
2933 _dbus_assert (-0x123456 ==
2934 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2935 _dbus_string_get_const_data (&str)));
2937 /* signed little pack */
2938 _dbus_pack_int32 (-0x123456,
2940 _dbus_string_get_data (&str));
2942 _dbus_assert (-0x123456 ==
2943 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2944 _dbus_string_get_const_data (&str)));
2946 /* signed big pack */
2947 _dbus_pack_int32 (-0x123456,
2949 _dbus_string_get_data (&str));
2951 _dbus_assert (-0x123456 ==
2952 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2953 _dbus_string_get_const_data (&str)));
2955 /* unsigned little */
2956 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
2959 _dbus_assert (0x123456 ==
2960 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2961 _dbus_string_get_const_data (&str)));
2964 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
2967 _dbus_assert (0x123456 ==
2968 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2969 _dbus_string_get_const_data (&str)));
2971 /* unsigned little pack */
2972 _dbus_pack_uint32 (0x123456,
2974 _dbus_string_get_data (&str));
2976 _dbus_assert (0x123456 ==
2977 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2978 _dbus_string_get_const_data (&str)));
2980 /* unsigned big pack */
2981 _dbus_pack_uint32 (0x123456,
2983 _dbus_string_get_data (&str));
2985 _dbus_assert (0x123456 ==
2986 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2987 _dbus_string_get_const_data (&str)));
2992 _dbus_string_set_length (&str, 0);
2994 _dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN,
2997 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2998 _dbus_assert (strcmp (s, "Hello world") == 0);
3001 _dbus_string_init_const (&t, "Hello world foo");
3003 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
3004 &t, _dbus_string_get_length (&t));
3006 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
3007 _dbus_assert (strcmp (s, "Hello world foo") == 0);
3010 _dbus_string_init_const (&t, "Hello");
3012 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
3013 &t, _dbus_string_get_length (&t));
3015 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
3016 _dbus_assert (strcmp (s, "Hello") == 0);
3019 /* Strings (big endian) */
3021 _dbus_string_set_length (&str, 0);
3023 _dbus_marshal_string (&str, DBUS_BIG_ENDIAN,
3026 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
3027 _dbus_assert (strcmp (s, "Hello world") == 0);
3030 _dbus_string_init_const (&t, "Hello world foo");
3032 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
3033 &t, _dbus_string_get_length (&t));
3035 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
3036 _dbus_assert (strcmp (s, "Hello world foo") == 0);
3039 _dbus_string_init_const (&t, "Hello");
3041 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
3042 &t, _dbus_string_get_length (&t));
3044 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
3045 _dbus_assert (strcmp (s, "Hello") == 0);
3048 _dbus_string_free (&str);
3053 #endif /* DBUS_BUILD_TESTS */