1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.c Marshalling routines
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-marshal.h"
26 #include "dbus-internals.h"
27 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
28 #include "dbus-string-private.h"
33 * @defgroup DBusMarshal marshaling and unmarshaling
34 * @ingroup DBusInternals
35 * @brief functions to marshal/unmarshal data from the wire
37 * Types and functions related to converting primitive data types from
38 * wire format to native machine format, and vice versa.
44 unpack_4_octets (int byte_order,
45 const unsigned char *data)
47 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
49 if (byte_order == DBUS_LITTLE_ENDIAN)
50 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
52 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
55 #ifndef DBUS_HAVE_INT64
58 swap_bytes (unsigned char *data,
61 unsigned char *p1 = data;
62 unsigned char *p2 = data + len - 1;
66 unsigned char tmp = *p1;
74 #endif /* !DBUS_HAVE_INT64 */
78 #ifdef DBUS_HAVE_INT64
86 unpack_8_octets (int byte_order,
87 const unsigned char *data)
91 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
92 _dbus_assert (sizeof (r) == 8);
94 #ifdef DBUS_HAVE_INT64
95 if (byte_order == DBUS_LITTLE_ENDIAN)
96 r.u = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
98 r.u = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
100 r.d = *(double*)data;
101 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
102 swap_bytes ((unsigned char*) &r, sizeof (r));
109 * Unpacks a 32 bit unsigned integer from a data pointer
111 * @param byte_order The byte order to use
112 * @param data the data pointer
113 * @returns the integer
116 _dbus_unpack_uint32 (int byte_order,
117 const unsigned char *data)
119 return unpack_4_octets (byte_order, data);
123 * Unpacks a 32 bit signed integer from a data pointer
125 * @param byte_order The byte order to use
126 * @param data the data pointer
127 * @returns the integer
130 _dbus_unpack_int32 (int byte_order,
131 const unsigned char *data)
133 return (dbus_int32_t) unpack_4_octets (byte_order, data);
136 #ifdef DBUS_HAVE_INT64
138 * Unpacks a 64 bit unsigned integer from a data pointer
140 * @param byte_order The byte order to use
141 * @param data the data pointer
142 * @returns the integer
145 _dbus_unpack_uint64 (int byte_order,
146 const unsigned char *data)
150 r = unpack_8_octets (byte_order, data);
156 * Unpacks a 64 bit signed integer from a data pointer
158 * @param byte_order The byte order to use
159 * @param data the data pointer
160 * @returns the integer
163 _dbus_unpack_int64 (int byte_order,
164 const unsigned char *data)
168 r = unpack_8_octets (byte_order, data);
173 #endif /* DBUS_HAVE_INT64 */
176 pack_4_octets (dbus_uint32_t value,
180 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
182 if ((byte_order) == DBUS_LITTLE_ENDIAN)
183 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
185 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
189 pack_8_octets (DBusOctets8 value,
193 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
195 #ifdef DBUS_HAVE_INT64
196 if ((byte_order) == DBUS_LITTLE_ENDIAN)
197 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u);
199 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u);
201 memcpy (data, &value, 8);
202 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
203 swap_bytes ((unsigned char *)data, 8);
208 * Packs a 32 bit unsigned integer into a data pointer.
210 * @param value the value
211 * @param byte_order the byte order to use
212 * @param data the data pointer
215 _dbus_pack_uint32 (dbus_uint32_t value,
219 pack_4_octets (value, byte_order, data);
223 * Packs a 32 bit signed integer into a data pointer.
225 * @param value the value
226 * @param byte_order the byte order to use
227 * @param data the data pointer
230 _dbus_pack_int32 (dbus_int32_t value,
234 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
237 #ifdef DBUS_HAVE_INT64
239 * Packs a 64 bit unsigned integer into a data pointer.
241 * @param value the value
242 * @param byte_order the byte order to use
243 * @param data the data pointer
246 _dbus_pack_uint64 (dbus_uint64_t value,
252 pack_8_octets (r, byte_order, data);
256 * Packs a 64 bit signed integer into a data pointer.
258 * @param value the value
259 * @param byte_order the byte order to use
260 * @param data the data pointer
263 _dbus_pack_int64 (dbus_int64_t value,
269 pack_8_octets (r, byte_order, data);
271 #endif /* DBUS_HAVE_INT64 */
274 set_4_octets (DBusString *str,
281 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
282 byte_order == DBUS_BIG_ENDIAN);
284 data = _dbus_string_get_data_len (str, offset, 4);
286 _dbus_pack_uint32 (value, byte_order, data);
290 set_8_octets (DBusString *str,
297 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
298 byte_order == DBUS_BIG_ENDIAN);
300 data = _dbus_string_get_data_len (str, offset, 8);
302 pack_8_octets (value, byte_order, data);
306 * Sets the 4 bytes at the given offset to a marshaled signed integer,
307 * replacing anything found there previously.
309 * @param str the string to write the marshalled int to
310 * @param offset the byte offset where int should be written
311 * @param byte_order the byte order to use
312 * @param value the value
316 _dbus_marshal_set_int32 (DBusString *str,
321 set_4_octets (str, byte_order, offset, (dbus_uint32_t) value);
325 * Sets the 4 bytes at the given offset to a marshaled unsigned
326 * integer, replacing anything found there previously.
328 * @param str the string to write the marshalled int to
329 * @param offset the byte offset where int should be written
330 * @param byte_order the byte order to use
331 * @param value the value
335 _dbus_marshal_set_uint32 (DBusString *str,
340 set_4_octets (str, byte_order, offset, value);
343 #ifdef DBUS_HAVE_INT64
346 * Sets the 8 bytes at the given offset to a marshaled signed integer,
347 * replacing anything found there previously.
349 * @param str the string to write the marshalled int to
350 * @param offset the byte offset where int should be written
351 * @param byte_order the byte order to use
352 * @param value the value
356 _dbus_marshal_set_int64 (DBusString *str,
363 set_8_octets (str, byte_order, offset, r);
367 * Sets the 8 bytes at the given offset to a marshaled unsigned
368 * integer, replacing anything found there previously.
370 * @param str the string to write the marshalled int to
371 * @param offset the byte offset where int should be written
372 * @param byte_order the byte order to use
373 * @param value the value
377 _dbus_marshal_set_uint64 (DBusString *str,
384 set_8_octets (str, byte_order, offset, r);
386 #endif /* DBUS_HAVE_INT64 */
389 * Sets the existing marshaled string at the given offset with
390 * a new marshaled string. The given offset must point to
391 * an existing string or the wrong length will be deleted
392 * and replaced with the new string.
394 * @param str the string to write the marshalled string to
395 * @param offset the byte offset where string should be written
396 * @param byte_order the byte order to use
397 * @param value the value
398 * @param len the length to use
399 * @returns #TRUE on success
403 _dbus_marshal_set_string (DBusString *str,
406 const DBusString *value,
411 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
412 byte_order == DBUS_BIG_ENDIAN);
414 old_len = _dbus_demarshal_uint32 (str, byte_order,
417 if (!_dbus_string_replace_len (value, 0, len,
418 str, offset + 4, old_len))
421 _dbus_marshal_set_uint32 (str, byte_order,
428 * Sets the existing marshaled object path at the given offset to a new
429 * value. The given offset must point to an existing object path or this
430 * function doesn't make sense.
432 * @todo implement this function
434 * @param str the string to write the marshalled path to
435 * @param offset the byte offset where path should be written
436 * @param byte_order the byte order to use
437 * @param path the new path
438 * @param path_len number of elements in the path
441 _dbus_marshal_set_object_path (DBusString *str,
452 marshal_4_octets (DBusString *str,
456 _dbus_assert (sizeof (value) == 4);
458 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
459 value = DBUS_UINT32_SWAP_LE_BE (value);
461 return _dbus_string_append_4_aligned (str,
462 (const unsigned char *)&value);
466 marshal_8_octets (DBusString *str,
470 _dbus_assert (sizeof (value) == 8);
472 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
473 pack_8_octets (value, byte_order, (unsigned char*) &value); /* pack into self, swapping as we go */
475 return _dbus_string_append_8_aligned (str,
476 (const unsigned char *)&value);
480 * Marshals a double value.
482 * @param str the string to append the marshalled value to
483 * @param byte_order the byte order to use
484 * @param value the value
485 * @returns #TRUE on success
488 _dbus_marshal_double (DBusString *str,
494 return marshal_8_octets (str, byte_order, r);
498 * Marshals a 32 bit signed integer value.
500 * @param str the string to append the marshalled value to
501 * @param byte_order the byte order to use
502 * @param value the value
503 * @returns #TRUE on success
506 _dbus_marshal_int32 (DBusString *str,
510 return marshal_4_octets (str, byte_order, (dbus_uint32_t) value);
514 * Marshals a 32 bit unsigned integer value.
516 * @param str the string to append the marshalled value to
517 * @param byte_order the byte order to use
518 * @param value the value
519 * @returns #TRUE on success
522 _dbus_marshal_uint32 (DBusString *str,
526 return marshal_4_octets (str, byte_order, value);
530 #ifdef DBUS_HAVE_INT64
532 * Marshals a 64 bit signed integer value.
534 * @param str the string to append the marshalled value to
535 * @param byte_order the byte order to use
536 * @param value the value
537 * @returns #TRUE on success
540 _dbus_marshal_int64 (DBusString *str,
546 return marshal_8_octets (str, byte_order, r);
550 * Marshals a 64 bit unsigned integer value.
552 * @param str the string to append the marshalled value to
553 * @param byte_order the byte order to use
554 * @param value the value
555 * @returns #TRUE on success
558 _dbus_marshal_uint64 (DBusString *str,
564 return marshal_8_octets (str, byte_order, r);
567 #endif /* DBUS_HAVE_INT64 */
570 * Marshals a UTF-8 string
572 * @todo: If the string append fails we need to restore
573 * the old length. (also for other marshallers)
575 * @param str the string to append the marshalled value to
576 * @param byte_order the byte order to use
577 * @param value the string
578 * @returns #TRUE on success
581 _dbus_marshal_string (DBusString *str,
585 int len, old_string_len;
587 old_string_len = _dbus_string_get_length (str);
589 len = strlen (value);
591 if (!_dbus_marshal_uint32 (str, byte_order, len))
593 /* Restore the previous length */
594 _dbus_string_set_length (str, old_string_len);
599 return _dbus_string_append_len (str, value, len + 1);
603 * Marshals a byte array
605 * @param str the string to append the marshalled value to
606 * @param byte_order the byte order to use
607 * @param value the array
608 * @param len number of elements in the array
609 * @returns #TRUE on success
612 _dbus_marshal_byte_array (DBusString *str,
614 const unsigned char *value,
619 old_string_len = _dbus_string_get_length (str);
621 if (!_dbus_marshal_uint32 (str, byte_order, len))
623 /* Restore the previous length */
624 _dbus_string_set_length (str, old_string_len);
632 return _dbus_string_append_len (str, value, len);
636 marshal_4_octets_array (DBusString *str,
638 const dbus_uint32_t *value,
644 old_string_len = _dbus_string_get_length (str);
646 if (!_dbus_marshal_uint32 (str, byte_order, len * 4))
649 array_start = _dbus_string_get_length (str);
651 if (!_dbus_string_append_len (str, (const unsigned char*) value,
655 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
657 const unsigned char *d;
658 const unsigned char *end;
660 d = _dbus_string_get_data (str) + array_start;
664 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
672 /* Restore previous length */
673 _dbus_string_set_length (str, old_string_len);
679 marshal_8_octets_array (DBusString *str,
681 const DBusOctets8 *value,
687 old_string_len = _dbus_string_get_length (str);
689 if (!_dbus_marshal_uint32 (str, byte_order, len * 8))
692 array_start = _dbus_string_get_length (str);
694 if (!_dbus_string_append_len (str, (const unsigned char*) value,
698 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
700 const unsigned char *d;
701 const unsigned char *end;
703 d = _dbus_string_get_data (str) + array_start;
707 #ifdef DBUS_HAVE_INT64
708 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
710 swap_bytes ((unsigned char*) d, 8);
719 /* Restore previous length */
720 _dbus_string_set_length (str, old_string_len);
726 * Marshals a 32 bit signed integer array
728 * @param str the string to append the marshalled value to
729 * @param byte_order the byte order to use
730 * @param value the array
731 * @param len the length of the array
732 * @returns #TRUE on success
735 _dbus_marshal_int32_array (DBusString *str,
737 const dbus_int32_t *value,
740 return marshal_4_octets_array (str, byte_order,
741 (const dbus_uint32_t*) value,
746 * Marshals a 32 bit unsigned integer array
748 * @param str the string to append the marshalled value to
749 * @param byte_order the byte order to use
750 * @param value the array
751 * @param len the length of the array
752 * @returns #TRUE on success
755 _dbus_marshal_uint32_array (DBusString *str,
757 const dbus_uint32_t *value,
760 return marshal_4_octets_array (str, byte_order,
765 #ifdef DBUS_HAVE_INT64
768 * Marshals a 64 bit signed integer array
770 * @param str the string to append the marshalled value to
771 * @param byte_order the byte order to use
772 * @param value the array
773 * @param len the length of the array
774 * @returns #TRUE on success
777 _dbus_marshal_int64_array (DBusString *str,
779 const dbus_int64_t *value,
782 return marshal_8_octets_array (str, byte_order,
783 (const DBusOctets8*) value,
788 * Marshals a 64 bit unsigned integer array
790 * @param str the string to append the marshalled value to
791 * @param byte_order the byte order to use
792 * @param value the array
793 * @param len the length of the array
794 * @returns #TRUE on success
797 _dbus_marshal_uint64_array (DBusString *str,
799 const dbus_uint64_t *value,
802 return marshal_8_octets_array (str, byte_order,
803 (const DBusOctets8*) value,
807 #endif /* DBUS_HAVE_INT64 */
810 * Marshals a double array
812 * @param str the string to append the marshalled value to
813 * @param byte_order the byte order to use
814 * @param value the array
815 * @param len the length of the array
816 * @returns #TRUE on success
819 _dbus_marshal_double_array (DBusString *str,
824 return marshal_8_octets_array (str, byte_order,
825 (const DBusOctets8*) value,
830 * Marshals a string array
832 * @param str the string to append the marshalled value to
833 * @param byte_order the byte order to use
834 * @param value the array
835 * @param len the length of the array
836 * @returns #TRUE on success
839 _dbus_marshal_string_array (DBusString *str,
844 int i, old_string_len, array_start;
846 old_string_len = _dbus_string_get_length (str);
848 /* Set the length to 0 temporarily */
849 if (!_dbus_marshal_uint32 (str, byte_order, 0))
852 array_start = _dbus_string_get_length (str);
854 for (i = 0; i < len; i++)
855 if (!_dbus_marshal_string (str, byte_order, value[i]))
858 /* Write the length now that we know it */
859 _dbus_marshal_set_uint32 (str, byte_order,
860 _DBUS_ALIGN_VALUE (old_string_len, sizeof(dbus_uint32_t)),
861 _dbus_string_get_length (str) - array_start);
866 /* Restore previous length */
867 _dbus_string_set_length (str, old_string_len);
873 * Marshals an object path value.
875 * @todo implement this function
877 * @param str the string to append the marshalled value to
878 * @param byte_order the byte order to use
879 * @param path the path
880 * @param path_len length of the path
881 * @returns #TRUE on success
884 _dbus_marshal_object_path (DBusString *str,
893 demarshal_4_octets (const DBusString *str,
898 const DBusRealString *real = (const DBusRealString*) str;
900 pos = _DBUS_ALIGN_VALUE (pos, 4);
905 return unpack_4_octets (byte_order, real->str + pos);
909 demarshal_8_octets (const DBusString *str,
914 const DBusRealString *real = (const DBusRealString*) str;
916 pos = _DBUS_ALIGN_VALUE (pos, 8);
921 return unpack_8_octets (byte_order, real->str + pos);
925 * Demarshals a double.
927 * @param str the string containing the data
928 * @param byte_order the byte order
929 * @param pos the position in the string
930 * @param new_pos the new position of the string
931 * @returns the demarshaled double.
934 _dbus_demarshal_double (const DBusString *str,
941 r = demarshal_8_octets (str, byte_order, pos, new_pos);
947 * Demarshals a 32 bit signed integer.
949 * @param str the string containing the data
950 * @param byte_order the byte order
951 * @param pos the position in the string
952 * @param new_pos the new position of the string
953 * @returns the demarshaled integer.
956 _dbus_demarshal_int32 (const DBusString *str,
961 return (dbus_int32_t) demarshal_4_octets (str, byte_order, pos, new_pos);
965 * Demarshals a 32 bit unsigned integer.
967 * @param str the string containing the data
968 * @param byte_order the byte order
969 * @param pos the position in the string
970 * @param new_pos the new position of the string
971 * @returns the demarshaled integer.
974 _dbus_demarshal_uint32 (const DBusString *str,
979 return demarshal_4_octets (str, byte_order, pos, new_pos);
982 #ifdef DBUS_HAVE_INT64
985 * Demarshals a 64 bit signed integer.
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 * @returns the demarshaled integer.
994 _dbus_demarshal_int64 (const DBusString *str,
1001 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1007 * Demarshals a 64 bit unsigned integer.
1009 * @param str the string containing the data
1010 * @param byte_order the byte order
1011 * @param pos the position in the string
1012 * @param new_pos the new position of the string
1013 * @returns the demarshaled integer.
1016 _dbus_demarshal_uint64 (const DBusString *str,
1023 r = demarshal_8_octets (str, byte_order, pos, new_pos);
1028 #endif /* DBUS_HAVE_INT64 */
1031 * Demarshals an UTF-8 string.
1033 * @todo Should we check the string to make sure
1034 * that it's valid UTF-8, and maybe "fix" the string
1037 * @todo Should probably demarshal to a DBusString,
1038 * having memcpy() in here is Evil(tm).
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 of the string
1044 * @returns the demarshaled string.
1047 _dbus_demarshal_string (const DBusString *str,
1056 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1058 retval = dbus_malloc (len + 1);
1063 data = _dbus_string_get_const_data_len (str, pos, len + 1);
1068 memcpy (retval, data, len + 1);
1071 *new_pos = pos + len + 1;
1077 * Demarshals a byte array.
1079 * @todo Should probably demarshal to a DBusString,
1080 * having memcpy() in here is Evil(tm).
1082 * @param str the string containing the data
1083 * @param byte_order the byte order
1084 * @param pos the position in the string
1085 * @param new_pos the new position of the string
1086 * @param array the array
1087 * @param array_len length of the demarshaled data
1089 * @returns #TRUE on success
1092 _dbus_demarshal_byte_array (const DBusString *str,
1096 unsigned char **array,
1100 unsigned char *retval;
1103 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1116 retval = dbus_malloc (len);
1121 data = _dbus_string_get_const_data_len (str, pos, len);
1129 memcpy (retval, data, len);
1132 *new_pos = pos + len;
1141 demarshal_4_octets_array (const DBusString *str,
1145 dbus_uint32_t **array,
1149 dbus_uint32_t *retval;
1152 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1166 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1170 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1172 for (i = 0; i < len; i++)
1173 retval[i] = DBUS_UINT32_SWAP_LE_BE (retval[i]);
1177 *new_pos = pos + byte_len;
1186 demarshal_8_octets_array (const DBusString *str,
1190 DBusOctets8 **array,
1194 DBusOctets8 *retval;
1197 byte_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1211 if (!_dbus_string_copy_data_len (str, (char**) &retval,
1215 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1217 for (i = 0; i < len; i++)
1219 #ifdef DBUS_HAVE_INT64
1220 retval[i].u = DBUS_UINT64_SWAP_LE_BE (retval[i].u);
1222 swap_bytes ((unsigned char *) &retval[i], 8);
1228 *new_pos = pos + byte_len;
1237 * Demarshals a 32 bit signed integer array.
1239 * @param str the string containing the data
1240 * @param byte_order the byte order
1241 * @param pos the position in the string
1242 * @param new_pos the new position of the string
1243 * @param array the array
1244 * @param array_len length of the demarshaled data
1245 * @returns #TRUE on success
1248 _dbus_demarshal_int32_array (const DBusString *str,
1252 dbus_int32_t **array,
1255 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1256 (dbus_uint32_t**) array, array_len);
1260 * Demarshals a 32 bit unsigned integer array.
1262 * @param str the string containing the data
1263 * @param byte_order the byte order
1264 * @param pos the position in the string
1265 * @param new_pos the new position of the string
1266 * @param array the array
1267 * @param array_len length of the demarshaled data
1268 * @returns #TRUE on success
1271 _dbus_demarshal_uint32_array (const DBusString *str,
1275 dbus_uint32_t **array,
1278 return demarshal_4_octets_array (str, byte_order, pos, new_pos,
1282 #ifdef DBUS_HAVE_INT64
1285 * Demarshals a 64 bit signed integer array.
1287 * @param str the string containing the data
1288 * @param byte_order the byte order
1289 * @param pos the position in the string
1290 * @param new_pos the new position of the string
1291 * @param array the array
1292 * @param array_len length of the demarshaled data
1293 * @returns #TRUE on success
1296 _dbus_demarshal_int64_array (const DBusString *str,
1300 dbus_int64_t **array,
1303 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1304 (DBusOctets8**) array, array_len);
1308 * Demarshals a 64 bit unsigned integer array.
1310 * @param str the string containing the data
1311 * @param byte_order the byte order
1312 * @param pos the position in the string
1313 * @param new_pos the new position of the string
1314 * @param array the array
1315 * @param array_len length of the demarshaled data
1316 * @returns #TRUE on success
1319 _dbus_demarshal_uint64_array (const DBusString *str,
1323 dbus_uint64_t **array,
1326 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1327 (DBusOctets8**) array, array_len);
1330 #endif /* DBUS_HAVE_INT64 */
1333 * Demarshals a double array.
1335 * @param str the string containing the data
1336 * @param byte_order the byte order
1337 * @param pos the position in the string
1338 * @param new_pos the new position of the string
1339 * @param array the array
1340 * @param array_len length of the demarshaled data
1341 * @returns #TRUE on success
1344 _dbus_demarshal_double_array (const DBusString *str,
1351 return demarshal_8_octets_array (str, byte_order, pos, new_pos,
1352 (DBusOctets8**) array, array_len);
1356 * Demarshals a string array.
1358 * @param str the string containing the data
1359 * @param byte_order the byte order
1360 * @param pos the position in the string
1361 * @param new_pos the new position of the string
1362 * @param array the array
1363 * @param array_len length of the demarshaled data
1364 * @returns #TRUE on success
1367 _dbus_demarshal_string_array (const DBusString *str,
1379 bytes_len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1394 end_pos = pos + bytes_len;
1396 retval = dbus_new (char *, allocated);
1401 while (pos < end_pos)
1403 retval[len] = _dbus_demarshal_string (str, byte_order, pos, &pos);
1405 if (retval[len] == NULL)
1410 if (len >= allocated - 1) /* -1 for NULL termination */
1413 newp = dbus_realloc (retval,
1414 sizeof (char*) * allocated * 2);
1434 for (i = 0; i < len; i++)
1435 dbus_free (retval[i]);
1442 * Demarshals an object path.
1444 * @todo implement this function
1446 * @param str the string containing the data
1447 * @param byte_order the byte order
1448 * @param pos the position in the string
1449 * @param new_pos the new position of the string
1450 * @param path address to store new object path
1451 * @param path_len length of stored path
1454 _dbus_demarshal_object_path (const DBusString *str,
1465 * Returns the position right after the end of an argument. PERFORMS
1466 * NO VALIDATION WHATSOEVER. The message must have been previously
1469 * @param str a string
1470 * @param byte_order the byte order to use
1471 * @param type the type of the argument
1472 * @param pos the pos where the arg starts
1473 * @param end_pos pointer where the position right
1474 * after the end position will follow
1475 * @returns TRUE if more data exists after the arg
1478 _dbus_marshal_get_arg_end_pos (const DBusString *str,
1484 if (pos >= _dbus_string_get_length (str))
1489 case DBUS_TYPE_INVALID:
1497 case DBUS_TYPE_BYTE:
1501 case DBUS_TYPE_BOOLEAN:
1505 case DBUS_TYPE_INT32:
1506 case DBUS_TYPE_UINT32:
1507 *end_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
1510 case DBUS_TYPE_INT64:
1511 case DBUS_TYPE_UINT64:
1512 case DBUS_TYPE_DOUBLE:
1514 *end_pos = _DBUS_ALIGN_VALUE (pos, 8) + 8;
1517 case DBUS_TYPE_STRING:
1521 /* Demarshal the length */
1522 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1524 *end_pos = pos + len + 1;
1528 case DBUS_TYPE_NAMED:
1532 /* Demarshal the string length */
1533 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1535 *end_pos = pos + len + 1;
1537 /* Demarshal the data length */
1538 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1540 *end_pos = pos + len;
1544 case DBUS_TYPE_OBJECT_PATH:
1545 case DBUS_TYPE_ARRAY:
1549 /* Demarshal the length */
1550 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1552 *end_pos = pos + len;
1556 case DBUS_TYPE_DICT:
1560 /* Demarshal the length */
1561 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
1563 *end_pos = pos + len;
1568 _dbus_warn ("Unknown message arg type %d\n", type);
1569 _dbus_assert_not_reached ("Unknown message argument type\n");
1573 if (*end_pos > _dbus_string_get_length (str))
1580 * Demarshals and validates a length; returns < 0 if the validation
1581 * fails. The length is required to be small enough that
1582 * len*sizeof(double) will not overflow, and small enough to fit in a
1583 * signed integer. DOES NOT check whether the length points
1584 * beyond the end of the string, because it doesn't know the
1585 * size of array elements.
1587 * @param str the string
1588 * @param byte_order the byte order
1589 * @param pos the unaligned string position (snap to next aligned)
1590 * @param new_pos return location for new position.
1593 demarshal_and_validate_len (const DBusString *str,
1598 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1601 _dbus_assert (new_pos != NULL);
1603 if ((align_4 + 4) > _dbus_string_get_length (str))
1605 _dbus_verbose ("not enough room in message for array length\n");
1609 if (!_dbus_string_validate_nul (str, pos,
1612 _dbus_verbose ("array length alignment padding not initialized to nul\n");
1616 len = _dbus_demarshal_uint32 (str, byte_order, align_4, new_pos);
1618 /* note that the len is the number of bytes, so we need it to be
1619 * at least SIZE_T_MAX, but make it smaller just to keep things
1620 * sane. We end up using ints for most sizes to avoid unsigned mess
1621 * so limit to maximum 32-bit signed int divided by at least 8, more
1622 * for a bit of paranoia margin. INT_MAX/32 is about 65 megabytes.
1624 #define MAX_ARRAY_LENGTH (((unsigned int)_DBUS_INT_MAX) / 32)
1625 if (len > MAX_ARRAY_LENGTH)
1627 _dbus_verbose ("array length %u exceeds maximum of %u\n",
1628 len, MAX_ARRAY_LENGTH);
1636 validate_string (const DBusString *str,
1638 int len_without_nul,
1641 *end_pos = pos + len_without_nul + 1;
1643 if (*end_pos > _dbus_string_get_length (str))
1645 _dbus_verbose ("string length outside length of the message\n");
1649 if (_dbus_string_get_byte (str, pos + len_without_nul) != '\0')
1651 _dbus_verbose ("string arg not nul-terminated\n");
1655 if (!_dbus_string_validate_utf8 (str, pos, len_without_nul))
1657 _dbus_verbose ("string is not valid UTF-8\n");
1665 * Validates and returns a typecode at a specific position
1668 * @param str a string
1669 * @param type the type of the argument
1670 * @param pos the pos where the typecode starts
1671 * @param end_pos pointer where the position right
1672 * after the end position will follow
1673 * @returns #TRUE if the type is valid.
1676 _dbus_marshal_validate_type (const DBusString *str,
1683 if (pos >= _dbus_string_get_length (str))
1686 data = _dbus_string_get_const_data_len (str, pos, 1);
1688 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1691 if (end_pos != NULL)
1699 /* Faster validator for array data that doesn't call
1700 * validate_arg for each value
1703 validate_array_data (const DBusString *str,
1714 case DBUS_TYPE_INVALID:
1721 case DBUS_TYPE_OBJECT_PATH:
1722 case DBUS_TYPE_STRING:
1723 case DBUS_TYPE_NAMED:
1724 case DBUS_TYPE_ARRAY:
1725 case DBUS_TYPE_DICT:
1726 /* This clean recursion to validate_arg is what we
1727 * are doing logically for all types, but we don't
1728 * really want to call validate_arg for every byte
1729 * in a byte array, so the primitive types are
1734 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1735 type, array_type_pos, pos, &pos))
1740 case DBUS_TYPE_BYTE:
1744 case DBUS_TYPE_BOOLEAN:
1749 c = _dbus_string_get_byte (str, pos);
1751 if (!(c == 0 || c == 1))
1753 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1761 case DBUS_TYPE_INT32:
1762 case DBUS_TYPE_UINT32:
1763 /* Call validate arg one time to check alignment padding
1766 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1767 type, array_type_pos, pos, &pos))
1769 pos = _DBUS_ALIGN_VALUE (end, 4);
1772 case DBUS_TYPE_INT64:
1773 case DBUS_TYPE_UINT64:
1774 case DBUS_TYPE_DOUBLE:
1775 /* Call validate arg one time to check alignment padding
1778 if (!_dbus_marshal_validate_arg (str, byte_order, depth,
1779 type, array_type_pos, pos, &pos))
1781 pos = _DBUS_ALIGN_VALUE (end, 8);
1785 _dbus_verbose ("Unknown message arg type %d\n", type);
1795 * Validates an argument of a specific type, checking that it
1796 * is well-formed, for example no ludicrous length fields, strings
1797 * are nul-terminated, etc.
1798 * Returns the end position of the argument in end_pos, and
1799 * returns #TRUE if a valid arg begins at "pos"
1801 * @todo security: need to audit this function.
1803 * @todo For array types that can't be invalid, we should not
1804 * walk the whole array validating it. e.g. just skip all the
1805 * int values in an int array. (maybe this is already done now -hp)
1807 * @todo support DBUS_TYPE_OBJECT_PATH
1809 * @param str a string
1810 * @param byte_order the byte order to use
1811 * @param depth current recursion depth, to prevent excessive recursion
1812 * @param type the type of the argument
1813 * @param array_type_pos the position of the current array type, or
1814 * -1 if not in an array
1815 * @param pos the pos where the arg starts
1816 * @param end_pos pointer where the position right
1817 * after the end position will follow
1818 * @returns #TRUE if the arg is valid.
1821 _dbus_marshal_validate_arg (const DBusString *str,
1829 if (pos > _dbus_string_get_length (str))
1831 _dbus_verbose ("Validation went off the end of the message\n");
1835 #define MAX_VALIDATION_DEPTH 32
1837 if (depth > MAX_VALIDATION_DEPTH)
1839 _dbus_verbose ("Maximum recursion depth reached validating message\n");
1845 case DBUS_TYPE_INVALID:
1853 case DBUS_TYPE_BYTE:
1854 if (1 > _dbus_string_get_length (str) - pos)
1856 _dbus_verbose ("no room for byte value\n");
1863 case DBUS_TYPE_BOOLEAN:
1867 if (1 > _dbus_string_get_length (str) - pos)
1869 _dbus_verbose ("no room for boolean value\n");
1873 c = _dbus_string_get_byte (str, pos);
1875 if (!(c == 0 || c == 1))
1877 _dbus_verbose ("boolean value must be either 0 or 1, not %d\n", c);
1885 case DBUS_TYPE_INT32:
1886 case DBUS_TYPE_UINT32:
1888 int align_4 = _DBUS_ALIGN_VALUE (pos, 4);
1890 if (!_dbus_string_validate_nul (str, pos,
1893 _dbus_verbose ("int32/uint32 alignment padding not initialized to nul\n");
1897 *end_pos = align_4 + 4;
1901 case DBUS_TYPE_INT64:
1902 case DBUS_TYPE_UINT64:
1903 case DBUS_TYPE_DOUBLE:
1905 int align_8 = _DBUS_ALIGN_VALUE (pos, 8);
1907 _dbus_verbose_bytes_of_string (str, pos, (align_8 + 8 - pos));
1909 if (!_dbus_string_validate_nul (str, pos,
1912 _dbus_verbose ("double/int64/uint64/objid alignment padding not initialized to nul\n");
1916 *end_pos = align_8 + 8;
1920 case DBUS_TYPE_STRING:
1924 /* Demarshal the length, which does NOT include
1927 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1931 if (!validate_string (str, pos, len, end_pos))
1936 case DBUS_TYPE_NAMED:
1940 /* Demarshal the string length, which does NOT include
1943 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1947 if (!validate_string (str, pos, len, &pos))
1951 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
1955 *end_pos = pos + len;
1959 case DBUS_TYPE_ARRAY:
1965 if (array_type_pos == -1)
1967 array_type_pos = pos;
1971 if (!_dbus_marshal_validate_type (str, pos, &array_type, &pos))
1973 _dbus_verbose ("invalid array type\n");
1977 /* NIL values take up no space, so you couldn't iterate over an array of them.
1978 * array of nil seems useless anyway; the useful thing might be array of
1979 * (nil OR string) but we have no framework for that.
1981 if (array_type == DBUS_TYPE_NIL)
1983 _dbus_verbose ("array of NIL is not allowed\n");
1987 while (array_type == DBUS_TYPE_ARRAY);
1992 if (!_dbus_marshal_validate_type (str, array_type_pos, &array_type, NULL))
1994 _dbus_verbose ("invalid array type\n");
1998 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2002 if (len > _dbus_string_get_length (str) - pos)
2004 _dbus_verbose ("array length outside length of the message\n");
2010 if (!validate_array_data (str, byte_order, depth + 1,
2011 array_type, array_type_pos,
2017 /* This should not be able to happen, as long as validate_arg moves forward;
2018 * but the check is here just to be paranoid.
2020 _dbus_verbose ("array length %d specified was longer than actual array contents by %d\n",
2027 _dbus_verbose ("array contents exceeds array length %d by %d\n", len, pos - end);
2035 case DBUS_TYPE_DICT:
2041 len = demarshal_and_validate_len (str, byte_order, pos, &pos);
2045 if (len > _dbus_string_get_length (str) - pos)
2047 _dbus_verbose ("dict length outside length of the message\n");
2056 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2057 DBUS_TYPE_STRING, -1, pos, &pos))
2060 if (!_dbus_marshal_validate_type (str, pos, &dict_type, &pos))
2062 _dbus_verbose ("invalid dict entry type at offset %d\n", pos);
2066 /* Validate element */
2067 if (!_dbus_marshal_validate_arg (str, byte_order, depth + 1,
2068 dict_type, -1, pos, &pos))
2074 _dbus_verbose ("dict contents exceed stated dict length\n");
2083 _dbus_verbose ("Unknown message arg type %d\n", type);
2087 if (*end_pos > _dbus_string_get_length (str))
2095 * If in verbose mode, print a block of binary data.
2097 * @todo right now it prints even if not in verbose mode
2099 * @param data the data
2100 * @param len the length of the data
2103 _dbus_verbose_bytes (const unsigned char *data,
2107 const unsigned char *aligned;
2109 _dbus_assert (len >= 0);
2111 /* Print blanks on first row if appropriate */
2112 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
2115 _dbus_assert (aligned <= data);
2117 if (aligned != data)
2119 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
2120 while (aligned != data)
2122 _dbus_verbose (" ");
2127 /* now print the bytes */
2131 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2133 _dbus_verbose ("%4d\t%p: ",
2137 if (data[i] >= 32 &&
2139 _dbus_verbose (" '%c' ", data[i]);
2141 _dbus_verbose ("0x%s%x ",
2142 data[i] <= 0xf ? "0" : "", data[i]);
2146 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
2149 _dbus_verbose ("BE: %d LE: %d",
2150 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
2151 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
2154 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
2156 _dbus_verbose (" dbl: %g",
2157 *(double*)&data[i-8]);
2160 _dbus_verbose ("\n");
2164 _dbus_verbose ("\n");
2168 * Dump the given part of the string to verbose log.
2170 * @param str the string
2171 * @param start the start of range to dump
2172 * @param len length of range
2175 _dbus_verbose_bytes_of_string (const DBusString *str,
2182 real_len = _dbus_string_get_length (str);
2184 _dbus_assert (start >= 0);
2186 if (start > real_len)
2188 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
2189 start, len, real_len);
2193 if ((start + len) > real_len)
2195 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
2196 start, len, real_len);
2197 len = real_len - start;
2200 d = _dbus_string_get_const_data_len (str, start, len);
2202 _dbus_verbose_bytes (d, len);
2207 #ifdef DBUS_BUILD_TESTS
2208 #include "dbus-test.h"
2212 _dbus_marshal_test (void)
2217 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
2218 #ifdef DBUS_HAVE_INT64
2219 dbus_int64_t array3[3] = { 0x123ffffffff, 0x456ffffffff, 0x789ffffffff }, *array4;
2224 if (!_dbus_string_init (&str))
2225 _dbus_assert_not_reached ("failed to init string");
2227 /* Marshal doubles */
2228 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
2229 _dbus_assert_not_reached ("could not marshal double value");
2230 if (!_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14)
2231 _dbus_assert_not_reached ("demarshal failed");
2233 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
2234 _dbus_assert_not_reached ("could not marshal double value");
2235 if (!_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14)
2236 _dbus_assert_not_reached ("demarshal failed");
2238 /* Marshal signed integers */
2239 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
2240 _dbus_assert_not_reached ("could not marshal signed integer value");
2241 if (!_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678)
2242 _dbus_assert_not_reached ("demarshal failed");
2244 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
2245 _dbus_assert_not_reached ("could not marshal signed integer value");
2246 if (!_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678)
2247 _dbus_assert_not_reached ("demarshal failed");
2249 /* Marshal unsigned integers */
2250 if (!_dbus_marshal_uint32 (&str, DBUS_BIG_ENDIAN, 0x12345678))
2251 _dbus_assert_not_reached ("could not marshal signed integer value");
2252 if (!_dbus_demarshal_uint32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == 0x12345678)
2253 _dbus_assert_not_reached ("demarshal failed");
2255 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
2256 _dbus_assert_not_reached ("could not marshal signed integer value");
2257 if (!_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678)
2258 _dbus_assert_not_reached ("demarshal failed");
2260 #ifdef DBUS_HAVE_INT64
2261 /* Marshal signed integers */
2262 if (!_dbus_marshal_int64 (&str, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2263 _dbus_assert_not_reached ("could not marshal signed integer value");
2264 if (!_dbus_demarshal_int64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2265 _dbus_assert_not_reached ("demarshal failed");
2267 if (!_dbus_marshal_int64 (&str, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7)))
2268 _dbus_assert_not_reached ("could not marshal signed integer value");
2269 if (!_dbus_demarshal_int64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_INT64_CONSTANT (-0x123456789abc7))
2270 _dbus_assert_not_reached ("demarshal failed");
2272 /* Marshal unsigned integers */
2273 if (!_dbus_marshal_uint64 (&str, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2274 _dbus_assert_not_reached ("could not marshal signed integer value");
2275 if (!(_dbus_demarshal_uint64 (&str, DBUS_BIG_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2276 _dbus_assert_not_reached ("demarshal failed");
2278 if (!_dbus_marshal_uint64 (&str, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7)))
2279 _dbus_assert_not_reached ("could not marshal signed integer value");
2280 if (!(_dbus_demarshal_uint64 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == DBUS_UINT64_CONSTANT (0x123456789abc7)))
2281 _dbus_assert_not_reached ("demarshal failed");
2282 #endif /* DBUS_HAVE_INT64 */
2284 /* Marshal strings */
2285 tmp1 = "This is the dbus test string";
2286 if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
2287 _dbus_assert_not_reached ("could not marshal string");
2288 tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
2289 if (!strcmp (tmp1, tmp2) == 0)
2290 _dbus_assert_not_reached ("demarshal failed");
2293 tmp1 = "This is the dbus test string";
2294 if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
2295 _dbus_assert_not_reached ("could not marshal string");
2296 tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
2297 if (!strcmp (tmp1, tmp2) == 0)
2298 _dbus_assert_not_reached ("demarshal failed");
2301 /* Marshal signed integer arrays */
2302 if (!_dbus_marshal_int32_array (&str, DBUS_BIG_ENDIAN, array1, 3))
2303 _dbus_assert_not_reached ("could not marshal integer array");
2304 if (!_dbus_demarshal_int32_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array2, &len))
2305 _dbus_assert_not_reached ("could not demarshal integer array");
2308 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2311 #ifdef DBUS_HAVE_INT64
2312 /* Marshal 64-bit signed integer arrays */
2313 if (!_dbus_marshal_int64_array (&str, DBUS_BIG_ENDIAN, array3, 3))
2314 _dbus_assert_not_reached ("could not marshal integer array");
2315 if (!_dbus_demarshal_int64_array (&str, DBUS_BIG_ENDIAN, pos, &pos, &array4, &len))
2316 _dbus_assert_not_reached ("could not demarshal integer array");
2319 _dbus_assert_not_reached ("Signed integer array lengths differ!\n");
2322 /* set/pack 64-bit integers */
2323 _dbus_string_set_length (&str, 8);
2326 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
2327 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2329 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2330 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2331 _dbus_string_get_const_data (&str)));
2334 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
2335 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
2337 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2338 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2339 _dbus_string_get_const_data (&str)));
2341 /* signed little pack */
2342 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2344 _dbus_string_get_data (&str));
2346 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2347 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
2348 _dbus_string_get_const_data (&str)));
2350 /* signed big pack */
2351 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
2353 _dbus_string_get_data (&str));
2355 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
2356 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
2357 _dbus_string_get_const_data (&str)));
2359 /* unsigned little */
2360 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
2361 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2363 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2364 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2365 _dbus_string_get_const_data (&str)));
2368 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
2369 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
2371 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2372 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2373 _dbus_string_get_const_data (&str)));
2375 /* unsigned little pack */
2376 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2378 _dbus_string_get_data (&str));
2380 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2381 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
2382 _dbus_string_get_const_data (&str)));
2384 /* unsigned big pack */
2385 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
2387 _dbus_string_get_data (&str));
2389 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
2390 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
2391 _dbus_string_get_const_data (&str)));
2395 /* set/pack 32-bit integers */
2396 _dbus_string_set_length (&str, 4);
2399 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
2402 _dbus_assert (-0x123456 ==
2403 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2404 _dbus_string_get_const_data (&str)));
2407 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
2410 _dbus_assert (-0x123456 ==
2411 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2412 _dbus_string_get_const_data (&str)));
2414 /* signed little pack */
2415 _dbus_pack_int32 (-0x123456,
2417 _dbus_string_get_data (&str));
2419 _dbus_assert (-0x123456 ==
2420 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
2421 _dbus_string_get_const_data (&str)));
2423 /* signed big pack */
2424 _dbus_pack_int32 (-0x123456,
2426 _dbus_string_get_data (&str));
2428 _dbus_assert (-0x123456 ==
2429 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
2430 _dbus_string_get_const_data (&str)));
2432 /* unsigned little */
2433 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
2436 _dbus_assert (0x123456 ==
2437 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2438 _dbus_string_get_const_data (&str)));
2441 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
2444 _dbus_assert (0x123456 ==
2445 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2446 _dbus_string_get_const_data (&str)));
2448 /* unsigned little pack */
2449 _dbus_pack_uint32 (0x123456,
2451 _dbus_string_get_data (&str));
2453 _dbus_assert (0x123456 ==
2454 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
2455 _dbus_string_get_const_data (&str)));
2457 /* unsigned big pack */
2458 _dbus_pack_uint32 (0x123456,
2460 _dbus_string_get_data (&str));
2462 _dbus_assert (0x123456 ==
2463 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
2464 _dbus_string_get_const_data (&str)));
2469 _dbus_string_set_length (&str, 0);
2471 _dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN,
2474 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2475 _dbus_assert (strcmp (s, "Hello world") == 0);
2478 _dbus_string_init_const (&t, "Hello world foo");
2480 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2481 &t, _dbus_string_get_length (&t));
2483 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2484 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2487 _dbus_string_init_const (&t, "Hello");
2489 _dbus_marshal_set_string (&str, DBUS_LITTLE_ENDIAN, 0,
2490 &t, _dbus_string_get_length (&t));
2492 s = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, 0, NULL);
2493 _dbus_assert (strcmp (s, "Hello") == 0);
2496 /* Strings (big endian) */
2498 _dbus_string_set_length (&str, 0);
2500 _dbus_marshal_string (&str, DBUS_BIG_ENDIAN,
2503 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2504 _dbus_assert (strcmp (s, "Hello world") == 0);
2507 _dbus_string_init_const (&t, "Hello world foo");
2509 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2510 &t, _dbus_string_get_length (&t));
2512 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2513 _dbus_assert (strcmp (s, "Hello world foo") == 0);
2516 _dbus_string_init_const (&t, "Hello");
2518 _dbus_marshal_set_string (&str, DBUS_BIG_ENDIAN, 0,
2519 &t, _dbus_string_get_length (&t));
2521 s = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, 0, NULL);
2522 _dbus_assert (strcmp (s, "Hello") == 0);
2526 _dbus_string_free (&str);
2531 #endif /* DBUS_BUILD_TESTS */