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, 2005 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 #include "dbus-marshal-basic.h"
31 * @defgroup DBusMarshal marshaling and unmarshaling
32 * @ingroup DBusInternals
33 * @brief functions to marshal/unmarshal data from the wire
35 * Types and functions related to converting primitive data types from
36 * wire format to native machine format, and vice versa.
38 * A signature is just a string with multiple types one after the other.
39 * for example a type is "i" or "(ii)", a signature is "i(ii)"
40 * where i is int and (ii) is struct { int; int; }
46 unpack_4_octets (int byte_order,
47 const unsigned char *data)
49 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
51 if (byte_order == DBUS_LITTLE_ENDIAN)
52 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
54 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
57 #ifndef DBUS_HAVE_INT64
60 swap_bytes (unsigned char *data,
63 unsigned char *p1 = data;
64 unsigned char *p2 = data + len - 1;
68 unsigned char tmp = *p1;
76 #endif /* !DBUS_HAVE_INT64 */
79 * Union used to manipulate 8 bytes as if they
84 #ifdef DBUS_HAVE_INT64
85 dbus_int64_t s; /**< 64-bit integer */
86 dbus_uint64_t u; /**< 64-bit unsigned integer */
88 double d; /**< double */
92 unpack_8_octets (int byte_order,
93 const unsigned char *data)
97 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
98 _dbus_assert (sizeof (r) == 8);
100 #ifdef DBUS_HAVE_INT64
101 if (byte_order == DBUS_LITTLE_ENDIAN)
102 r.u = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
104 r.u = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
106 r.d = *(double*)data;
107 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
108 swap_bytes ((unsigned char*) &r, sizeof (r));
115 * Unpacks a 32 bit unsigned integer from a data pointer
117 * @param byte_order The byte order to use
118 * @param data the data pointer
119 * @returns the integer
122 _dbus_unpack_uint32 (int byte_order,
123 const unsigned char *data)
125 return unpack_4_octets (byte_order, data);
129 * Unpacks a 32 bit signed integer from a data pointer
131 * @param byte_order The byte order to use
132 * @param data the data pointer
133 * @returns the integer
136 _dbus_unpack_int32 (int byte_order,
137 const unsigned char *data)
139 return (dbus_int32_t) unpack_4_octets (byte_order, data);
142 #ifdef DBUS_HAVE_INT64
144 * Unpacks a 64 bit unsigned integer from a data pointer
146 * @param byte_order The byte order to use
147 * @param data the data pointer
148 * @returns the integer
151 _dbus_unpack_uint64 (int byte_order,
152 const unsigned char *data)
156 r = unpack_8_octets (byte_order, data);
162 * Unpacks a 64 bit signed integer from a data pointer
164 * @param byte_order The byte order to use
165 * @param data the data pointer
166 * @returns the integer
169 _dbus_unpack_int64 (int byte_order,
170 const unsigned char *data)
174 r = unpack_8_octets (byte_order, data);
179 #endif /* DBUS_HAVE_INT64 */
182 pack_4_octets (dbus_uint32_t value,
186 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
188 if ((byte_order) == DBUS_LITTLE_ENDIAN)
189 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
191 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
195 swap_8_octets (DBusOctets8 *value,
198 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
200 #ifdef DBUS_HAVE_INT64
201 value->u = DBUS_UINT64_SWAP_LE_BE (value->u);
203 swap_bytes ((unsigned char *)value, 8);
209 pack_8_octets (DBusOctets8 value,
213 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
215 #ifdef DBUS_HAVE_INT64
216 if ((byte_order) == DBUS_LITTLE_ENDIAN)
217 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u);
219 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u);
221 memcpy (data, &value, 8);
222 swap_8_octets ((DBusOctets8*)data, byte_order);
227 * Packs a 32 bit unsigned integer into a data pointer.
229 * @param value the value
230 * @param byte_order the byte order to use
231 * @param data the data pointer
234 _dbus_pack_uint32 (dbus_uint32_t value,
238 pack_4_octets (value, byte_order, data);
242 * Packs a 32 bit signed integer into a data pointer.
244 * @param value the value
245 * @param byte_order the byte order to use
246 * @param data the data pointer
249 _dbus_pack_int32 (dbus_int32_t value,
253 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
256 #ifdef DBUS_HAVE_INT64
258 * Packs a 64 bit unsigned integer into a data pointer.
260 * @param value the value
261 * @param byte_order the byte order to use
262 * @param data the data pointer
265 _dbus_pack_uint64 (dbus_uint64_t value,
271 pack_8_octets (r, byte_order, data);
275 * Packs a 64 bit signed integer into a data pointer.
277 * @param value the value
278 * @param byte_order the byte order to use
279 * @param data the data pointer
282 _dbus_pack_int64 (dbus_int64_t value,
288 pack_8_octets (r, byte_order, data);
290 #endif /* DBUS_HAVE_INT64 */
293 set_4_octets (DBusString *str,
300 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
301 byte_order == DBUS_BIG_ENDIAN);
303 data = _dbus_string_get_data_len (str, offset, 4);
305 _dbus_pack_uint32 (value, byte_order, data);
309 set_8_octets (DBusString *str,
316 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
317 byte_order == DBUS_BIG_ENDIAN);
319 data = _dbus_string_get_data_len (str, offset, 8);
321 pack_8_octets (value, byte_order, data);
325 * Sets the 4 bytes at the given offset to a marshaled signed integer,
326 * 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_int32 (DBusString *str,
340 set_4_octets (str, byte_order, offset, (dbus_uint32_t) value);
344 * Sets the 4 bytes at the given offset to a marshaled unsigned
345 * integer, replacing anything found there previously.
347 * @param str the string to write the marshalled int to
348 * @param offset the byte offset where int should be written
349 * @param byte_order the byte order to use
350 * @param value the value
354 _dbus_marshal_set_uint32 (DBusString *str,
359 set_4_octets (str, byte_order, offset, value);
362 #ifdef DBUS_HAVE_INT64
365 * Sets the 8 bytes at the given offset to a marshaled signed integer,
366 * replacing anything found there previously.
368 * @param str the string to write the marshalled int to
369 * @param offset the byte offset where int should be written
370 * @param byte_order the byte order to use
371 * @param value the value
375 _dbus_marshal_set_int64 (DBusString *str,
382 set_8_octets (str, byte_order, offset, r);
386 * Sets the 8 bytes at the given offset to a marshaled unsigned
387 * integer, replacing anything found there previously.
389 * @param str the string to write the marshalled int to
390 * @param offset the byte offset where int should be written
391 * @param byte_order the byte order to use
392 * @param value the value
396 _dbus_marshal_set_uint64 (DBusString *str,
403 set_8_octets (str, byte_order, offset, r);
405 #endif /* DBUS_HAVE_INT64 */
408 * Sets the existing marshaled string at the given offset with
409 * a new marshaled string. The given offset must point to
410 * an existing string or the wrong length will be deleted
411 * and replaced with the new string.
413 * Note: no attempt is made by this function to re-align
414 * any data which has been already marshalled after this
415 * string. Use with caution.
417 * @param str the string to write the marshalled string to
418 * @param offset the byte offset where string should be written
419 * @param byte_order the byte order to use
420 * @param value the value
421 * @param len the length to use
422 * @returns #TRUE on success
426 _dbus_marshal_set_string (DBusString *str,
429 const DBusString *value,
434 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
435 byte_order == DBUS_BIG_ENDIAN);
437 old_len = _dbus_demarshal_uint32 (str, byte_order,
440 if (!_dbus_string_replace_len (value, 0, len,
441 str, offset + 4, old_len))
444 _dbus_marshal_set_uint32 (str, byte_order,
451 demarshal_4_octets (const DBusString *str,
456 pos = _DBUS_ALIGN_VALUE (pos, 4);
461 return unpack_4_octets (byte_order,
462 _dbus_string_get_const_data (str) + pos);
466 * Convenience function to demarshal a 32 bit unsigned integer.
468 * @param str the string containing the data
469 * @param byte_order the byte order
470 * @param pos the position in the string
471 * @param new_pos the new position of the string
472 * @returns the demarshaled integer.
475 _dbus_demarshal_uint32 (const DBusString *str,
480 return demarshal_4_octets (str, byte_order, pos, new_pos);
484 * Demarshals a basic type. The "value" pointer is always
485 * the address of a variable of the basic type. So e.g.
486 * if the basic type is "double" then the pointer is
487 * a double*, and if it's "char*" then the pointer is
490 * A value of type #DBusBasicValue is guaranteed to be large enough to
491 * hold any of the types that may be returned, which is handy if you
492 * are trying to do things generically. For example you can pass
493 * a DBusBasicValue* in to this function, and then pass the same
494 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
495 * move a value from one place to another.
497 * @param str the string containing the data
498 * @param type type of value to demarshal
499 * @param value pointer to return value data
500 * @param byte_order the byte order
501 * @param pos position in the string
502 * @param new_pos pointer to update with new position, or #NULL
505 _dbus_demarshal_basic_type (const DBusString *str,
512 const char *str_data;
515 str_data = _dbus_string_get_const_data (str);
521 case DBUS_TYPE_BOOLEAN:
522 vp->byt = _dbus_string_get_byte (str, pos);
525 case DBUS_TYPE_INT32:
526 case DBUS_TYPE_UINT32:
527 pos = _DBUS_ALIGN_VALUE (pos, 4);
528 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
529 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
530 vp->u32 = DBUS_UINT32_SWAP_LE_BE (*(dbus_uint32_t *) value);
533 #ifdef DBUS_HAVE_INT64
534 case DBUS_TYPE_INT64:
535 case DBUS_TYPE_UINT64:
536 #endif /* DBUS_HAVE_INT64 */
537 case DBUS_TYPE_DOUBLE:
538 pos = _DBUS_ALIGN_VALUE (pos, 8);
539 memcpy (vp, str_data + pos, 8);
540 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
541 #ifdef DBUS_HAVE_INT64
542 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t *) value);
544 swap_bytes (value, 8);
548 case DBUS_TYPE_STRING:
549 case DBUS_TYPE_OBJECT_PATH:
553 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
555 vp->str = (char*) str_data + pos;
557 pos += len + 1; /* length plus nul */
560 case DBUS_TYPE_SIGNATURE:
564 len = _dbus_string_get_byte (str, pos);
567 vp->str = (char*) str_data + pos;
569 pos += len + 1; /* length plus nul */
573 _dbus_warn ("type %s not a basic type\n",
574 _dbus_type_to_string (type));
575 _dbus_assert_not_reached ("not a basic type");
584 * Skips over a basic type, reporting the following position.
586 * @param str the string containing the data
587 * @param type type of value to demarshal
588 * @param byte_order the byte order
589 * @param pos pointer to position in the string,
590 * updated on return to new position
593 _dbus_marshal_skip_basic_type (const DBusString *str,
601 case DBUS_TYPE_BOOLEAN:
604 case DBUS_TYPE_INT32:
605 case DBUS_TYPE_UINT32:
606 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
609 #ifdef DBUS_HAVE_INT64
610 case DBUS_TYPE_INT64:
611 case DBUS_TYPE_UINT64:
612 #endif /* DBUS_HAVE_INT64 */
613 case DBUS_TYPE_DOUBLE:
614 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
617 case DBUS_TYPE_STRING:
618 case DBUS_TYPE_OBJECT_PATH:
622 len = _dbus_demarshal_uint32 (str, byte_order, *pos, pos);
624 *pos += len + 1; /* length plus nul */
627 case DBUS_TYPE_SIGNATURE:
631 len = _dbus_string_get_byte (str, *pos);
633 *pos += len + 2; /* length byte plus length plus nul */
637 _dbus_warn ("type %s not a basic type\n",
638 _dbus_type_to_string (type));
639 _dbus_assert_not_reached ("not a basic type");
645 * Skips an array, returning the next position.
647 * @param str the string containing the data
648 * @param byte_order the byte order
649 * @param pos pointer to position in the string,
650 * updated on return to new position
653 _dbus_marshal_skip_array (const DBusString *str,
658 dbus_uint32_t array_len;
662 i = _DBUS_ALIGN_VALUE (*pos, 4);
664 _dbus_demarshal_basic_type (str,
671 alignment = _dbus_type_get_alignment (element_type);
673 i = _DBUS_ALIGN_VALUE (i, alignment);
675 *pos = i + array_len;
679 * Return #TRUE if the typecode is a valid typecode
681 * @returns #TRUE if valid
684 _dbus_type_is_valid (int typecode)
689 case DBUS_TYPE_BOOLEAN:
690 case DBUS_TYPE_INT32:
691 case DBUS_TYPE_UINT32:
692 case DBUS_TYPE_INT64:
693 case DBUS_TYPE_UINT64:
694 case DBUS_TYPE_DOUBLE:
695 case DBUS_TYPE_STRING:
696 case DBUS_TYPE_OBJECT_PATH:
697 case DBUS_TYPE_SIGNATURE:
698 case DBUS_TYPE_ARRAY:
699 case DBUS_TYPE_STRUCT:
700 case DBUS_TYPE_VARIANT:
709 * Gets the alignment requirement for the given type;
710 * will be 1, 4, or 8.
712 * @param typecode the type
713 * @returns alignment of 1, 4, or 8
716 _dbus_type_get_alignment (int typecode)
721 case DBUS_TYPE_BOOLEAN:
722 case DBUS_TYPE_VARIANT:
723 case DBUS_TYPE_SIGNATURE:
725 case DBUS_TYPE_INT32:
726 case DBUS_TYPE_UINT32:
727 /* this stuff is 4 since it starts with a length */
728 case DBUS_TYPE_STRING:
729 case DBUS_TYPE_OBJECT_PATH:
730 case DBUS_TYPE_ARRAY:
732 case DBUS_TYPE_INT64:
733 case DBUS_TYPE_UINT64:
734 case DBUS_TYPE_DOUBLE:
735 /* struct is 8 since it could contain an 8-aligned item
736 * and it's simpler to just always align structs to 8;
737 * we want the amount of padding in a struct of a given
738 * type to be predictable, not location-dependent.
740 case DBUS_TYPE_STRUCT:
744 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
750 * If in verbose mode, print a block of binary data.
752 * @todo right now it prints even if not in verbose mode
754 * @param data the data
755 * @param len the length of the data
756 * @param offset where to start counting for byte indexes
759 _dbus_verbose_bytes (const unsigned char *data,
764 const unsigned char *aligned;
766 _dbus_assert (len >= 0);
768 /* Print blanks on first row if appropriate */
769 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
772 _dbus_assert (aligned <= data);
776 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
777 while (aligned != data)
784 /* now print the bytes */
788 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
790 _dbus_verbose ("%4d\t%p: ",
791 offset + i, &data[i]);
796 _dbus_verbose (" '%c' ", data[i]);
798 _dbus_verbose ("0x%s%x ",
799 data[i] <= 0xf ? "0" : "", data[i]);
803 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
806 _dbus_verbose ("BE: %d LE: %d",
807 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
808 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
811 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
813 _dbus_verbose (" dbl: %g",
814 *(double*)&data[i-8]);
817 _dbus_verbose ("\n");
821 _dbus_verbose ("\n");
825 * Dump the given part of the string to verbose log.
827 * @param str the string
828 * @param start the start of range to dump
829 * @param len length of range
832 _dbus_verbose_bytes_of_string (const DBusString *str,
839 real_len = _dbus_string_get_length (str);
841 _dbus_assert (start >= 0);
843 if (start > real_len)
845 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
846 start, len, real_len);
850 if ((start + len) > real_len)
852 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
853 start, len, real_len);
854 len = real_len - start;
857 d = _dbus_string_get_const_data_len (str, start, len);
859 _dbus_verbose_bytes (d, len, start);
863 marshal_4_octets (DBusString *str,
872 _dbus_assert (sizeof (value) == 4);
874 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
875 value = DBUS_UINT32_SWAP_LE_BE (value);
877 orig_len = _dbus_string_get_length (str);
879 retval = _dbus_string_insert_4_aligned (str, insert_at,
880 (const unsigned char *)&value);
884 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
885 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
892 marshal_8_octets (DBusString *str,
901 _dbus_assert (sizeof (value) == 8);
903 swap_8_octets (&value, byte_order);
905 orig_len = _dbus_string_get_length (str);
907 retval = _dbus_string_insert_8_aligned (str, insert_at,
908 (const unsigned char *)&value);
911 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
919 MARSHAL_AS_SIGNATURE,
920 MARSHAL_AS_BYTE_ARRAY
924 marshal_len_followed_by_bytes (int marshal_as,
927 const unsigned char *value,
928 int data_len, /* doesn't include nul if any */
933 DBusString value_str;
936 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
937 value_len = data_len;
939 value_len = data_len + 1; /* value has a nul */
941 /* FIXME this is probably broken for byte arrays because
942 * DBusString wants strings to be nul-terminated?
943 * Maybe I planned on this when writing init_const_len though
945 _dbus_string_init_const_len (&value_str, value, value_len);
949 if (marshal_as == MARSHAL_AS_SIGNATURE)
951 if (!_dbus_string_insert_byte (str, pos, data_len))
958 if (!marshal_4_octets (str, pos, data_len,
963 if (!_dbus_string_copy_len (&value_str, 0, value_len,
969 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
971 _dbus_verbose_bytes_of_string (str, pos, value_len);
982 /* Delete what we've inserted */
983 _dbus_string_delete (str, insert_at, pos - insert_at);
989 marshal_string (DBusString *str,
995 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
996 str, insert_at, value,
998 byte_order, pos_after);
1002 marshal_signature (DBusString *str,
1007 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
1008 str, insert_at, value,
1010 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
1015 * Marshals a basic type. The "value" pointer is always the
1016 * address of a variable containing the basic type value.
1017 * So for example for int32 it will be dbus_int32_t*, and
1018 * for string it will be const char**. This is for symmetry
1019 * with _dbus_demarshal_basic_type() and to have a simple
1022 * @param str string to marshal to
1023 * @param insert_at where to insert the value
1024 * @param type type of value
1025 * @param value pointer to a variable containing the value
1026 * @param byte_order byte order
1027 * @param pos_after #NULL or the position after the type
1028 * @returns #TRUE on success
1031 _dbus_marshal_basic_type (DBusString *str,
1038 const DBusBasicValue *vp;
1044 case DBUS_TYPE_BYTE:
1045 case DBUS_TYPE_BOOLEAN:
1046 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
1049 *pos_after = insert_at + 1;
1052 case DBUS_TYPE_INT32:
1053 case DBUS_TYPE_UINT32:
1054 return marshal_4_octets (str, insert_at, vp->u32,
1055 byte_order, pos_after);
1057 #ifdef DBUS_HAVE_INT64
1058 case DBUS_TYPE_INT64:
1059 case DBUS_TYPE_UINT64:
1063 return marshal_8_octets (str, insert_at, r, byte_order, pos_after);
1066 #endif /* DBUS_HAVE_INT64 */
1067 case DBUS_TYPE_DOUBLE:
1071 return marshal_8_octets (str, insert_at, r, byte_order, pos_after);
1074 case DBUS_TYPE_STRING:
1075 case DBUS_TYPE_OBJECT_PATH:
1076 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
1078 case DBUS_TYPE_SIGNATURE:
1079 return marshal_signature (str, insert_at, vp->str, pos_after);
1082 _dbus_assert_not_reached ("not a basic type");
1089 marshal_1_octets_array (DBusString *str,
1091 const unsigned char *value,
1096 return marshal_len_followed_by_bytes (MARSHAL_AS_BYTE_ARRAY,
1097 str, insert_at, value, len,
1098 byte_order, pos_after);
1102 marshal_4_octets_array (DBusString *str,
1104 const dbus_uint32_t *value,
1111 _dbus_assert_not_reached ("FIXME insert_at");
1113 old_string_len = _dbus_string_get_length (str);
1115 if (!marshal_4_octets (str, insert_at, len*4, byte_order, NULL))
1118 array_start = _dbus_string_get_length (str);
1120 if (!_dbus_string_append_len (str, (const unsigned char*) value,
1124 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1126 const unsigned char *d;
1127 const unsigned char *end;
1129 d = _dbus_string_get_data (str) + array_start;
1133 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
1141 /* Restore previous length */
1142 _dbus_string_set_length (str, old_string_len);
1148 marshal_8_octets_array (DBusString *str,
1150 const DBusOctets8 *value,
1157 _dbus_assert_not_reached ("FIXME insert_at");
1159 old_string_len = _dbus_string_get_length (str);
1161 /* The array length is the length in bytes of the array,
1162 * *excluding* alignment padding.
1164 if (!marshal_4_octets (str, insert_at, len*8, byte_order, NULL))
1167 array_start = _dbus_string_get_length (str);
1169 /* Note that we do alignment padding unconditionally
1170 * even if the array is empty; this means that
1171 * padding + len is always equal to the number of bytes
1175 if (!_dbus_string_align_length (str, 8))
1178 if (!_dbus_string_append_len (str, (const unsigned char*) value,
1182 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1184 const unsigned char *d;
1185 const unsigned char *end;
1187 d = _dbus_string_get_data (str) + array_start;
1191 #ifdef DBUS_HAVE_INT64
1192 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
1194 swap_bytes ((unsigned char*) d, 8);
1203 /* Restore previous length */
1204 _dbus_string_set_length (str, old_string_len);
1210 * Marshals a basic type array
1212 * @param str string to marshal to
1213 * @param insert_at where to insert the value
1214 * @param element_type type of array elements
1215 * @param value pointer to value
1216 * @param len length of value data in elements
1217 * @param byte_order byte order
1218 * @param pos_after #NULL or the position after the type
1219 * @returns #TRUE on success
1222 _dbus_marshal_basic_type_array (DBusString *str,
1230 /* FIXME use the insert_at arg and fill in pos_after */
1232 switch (element_type)
1234 case DBUS_TYPE_BOOLEAN:
1235 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
1236 * should we here too ? */
1237 case DBUS_TYPE_BYTE:
1238 return marshal_1_octets_array (str, insert_at, value, len, byte_order, pos_after);
1240 case DBUS_TYPE_INT32:
1241 case DBUS_TYPE_UINT32:
1242 return marshal_4_octets_array (str, insert_at, value, len, byte_order);
1244 #ifdef DBUS_HAVE_INT64
1245 case DBUS_TYPE_INT64:
1246 case DBUS_TYPE_UINT64:
1247 #endif /* DBUS_HAVE_INT64 */
1248 case DBUS_TYPE_DOUBLE:
1249 return marshal_8_octets_array (str, insert_at, value, len, byte_order);
1252 case DBUS_TYPE_STRING:
1253 case DBUS_TYPE_OBJECT_PATH:
1254 _dbus_assert_not_reached ("handle string arrays");
1257 case DBUS_TYPE_SIGNATURE:
1258 _dbus_assert_not_reached ("handle signature");
1262 _dbus_assert_not_reached ("non basic type in array");
1271 #ifdef DBUS_BUILD_TESTS
1272 #include "dbus-test.h"
1275 #define MARSHAL_BASIC(typename, byte_order, literal) \
1277 v_##typename = literal; \
1278 if (!_dbus_marshal_basic_type (&str, pos, DBUS_TYPE_##typename, \
1280 byte_order, NULL)) \
1281 _dbus_assert_not_reached ("no memory"); \
1284 #define DEMARSHAL_BASIC(typename, byte_order) \
1286 _dbus_demarshal_basic_type (&str, DBUS_TYPE_##typename, &v_##typename, \
1287 byte_order, pos, &pos); \
1290 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1292 DEMARSHAL_BASIC (typename, byte_order); \
1293 if (literal != v_##typename) \
1295 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1296 _dbus_string_get_length (&str) - dump_pos); \
1297 _dbus_assert_not_reached ("demarshaled wrong value"); \
1301 #define MARSHAL_TEST(typename, byte_order, literal) \
1303 MARSHAL_BASIC (typename, byte_order, literal); \
1305 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1308 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1310 MARSHAL_BASIC (typename, byte_order, literal); \
1312 DEMARSHAL_BASIC (typename, byte_order); \
1313 if (strcmp (literal, v_##typename) != 0) \
1315 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1316 _dbus_string_get_length (&str) - dump_pos); \
1317 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1318 _dbus_assert_not_reached ("demarshaled wrong value"); \
1323 _dbus_marshal_test (void)
1328 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
1329 #ifdef DBUS_HAVE_INT64
1330 dbus_int64_t array3[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1331 DBUS_INT64_CONSTANT (0x456ffffffff),
1332 DBUS_INT64_CONSTANT (0x789ffffffff) }, *array4;
1338 dbus_int32_t v_INT32;
1339 dbus_uint32_t v_UINT32;
1340 dbus_int64_t v_INT64;
1341 dbus_uint64_t v_UINT64;
1342 unsigned char v_BYTE;
1343 unsigned char v_BOOLEAN;
1344 const char *v_STRING;
1345 const char *v_SIGNATURE;
1346 const char *v_OBJECT_PATH;
1349 if (!_dbus_string_init (&str))
1350 _dbus_assert_not_reached ("failed to init string");
1354 /* Marshal doubles */
1355 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1356 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1358 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1359 _dbus_assert_not_reached ("got wrong double value");
1361 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1362 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1364 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1365 _dbus_assert_not_reached ("got wrong double value");
1367 /* Marshal signed integers */
1368 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1369 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1371 /* Marshal unsigned integers */
1372 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1373 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1375 #ifdef DBUS_HAVE_INT64
1376 /* Marshal signed integers */
1377 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1378 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1380 /* Marshal unsigned integers */
1381 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1382 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1383 #endif /* DBUS_HAVE_INT64 */
1386 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1387 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1389 /* Marshal all possible bools! */
1390 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1391 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1392 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1393 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1395 /* Marshal strings */
1396 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1397 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1398 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1399 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1402 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1403 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1406 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1407 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1408 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1409 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1411 /* set/pack 64-bit integers */
1412 _dbus_string_set_length (&str, 8);
1415 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1416 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1418 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1419 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1420 _dbus_string_get_const_data (&str)));
1423 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1424 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1426 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1427 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1428 _dbus_string_get_const_data (&str)));
1430 /* signed little pack */
1431 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1433 _dbus_string_get_data (&str));
1435 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1436 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1437 _dbus_string_get_const_data (&str)));
1439 /* signed big pack */
1440 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1442 _dbus_string_get_data (&str));
1444 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1445 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1446 _dbus_string_get_const_data (&str)));
1448 /* unsigned little */
1449 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1450 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1452 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1453 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1454 _dbus_string_get_const_data (&str)));
1457 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1458 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1460 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1461 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1462 _dbus_string_get_const_data (&str)));
1464 /* unsigned little pack */
1465 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1467 _dbus_string_get_data (&str));
1469 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1470 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1471 _dbus_string_get_const_data (&str)));
1473 /* unsigned big pack */
1474 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1476 _dbus_string_get_data (&str));
1478 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1479 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1480 _dbus_string_get_const_data (&str)));
1482 /* set/pack 32-bit integers */
1483 _dbus_string_set_length (&str, 4);
1486 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1489 _dbus_assert (-0x123456 ==
1490 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1491 _dbus_string_get_const_data (&str)));
1494 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1497 _dbus_assert (-0x123456 ==
1498 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1499 _dbus_string_get_const_data (&str)));
1501 /* signed little pack */
1502 _dbus_pack_int32 (-0x123456,
1504 _dbus_string_get_data (&str));
1506 _dbus_assert (-0x123456 ==
1507 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1508 _dbus_string_get_const_data (&str)));
1510 /* signed big pack */
1511 _dbus_pack_int32 (-0x123456,
1513 _dbus_string_get_data (&str));
1515 _dbus_assert (-0x123456 ==
1516 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1517 _dbus_string_get_const_data (&str)));
1519 /* unsigned little */
1520 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
1523 _dbus_assert (0x123456 ==
1524 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1525 _dbus_string_get_const_data (&str)));
1528 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
1531 _dbus_assert (0x123456 ==
1532 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1533 _dbus_string_get_const_data (&str)));
1535 /* unsigned little pack */
1536 _dbus_pack_uint32 (0x123456,
1538 _dbus_string_get_data (&str));
1540 _dbus_assert (0x123456 ==
1541 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1542 _dbus_string_get_const_data (&str)));
1544 /* unsigned big pack */
1545 _dbus_pack_uint32 (0x123456,
1547 _dbus_string_get_data (&str));
1549 _dbus_assert (0x123456 ==
1550 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1551 _dbus_string_get_const_data (&str)));
1553 /* Strings in-place set */
1554 byte_order = DBUS_LITTLE_ENDIAN;
1558 _dbus_string_set_length (&str, 0);
1560 /* reset pos for the macros */
1563 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1565 /* Set it to something longer */
1566 _dbus_string_init_const (&t, "Hello world foo");
1568 _dbus_marshal_set_string (&str, byte_order, 0,
1569 &t, _dbus_string_get_length (&t));
1571 _dbus_demarshal_basic_type (&str, DBUS_TYPE_STRING,
1572 &v_STRING, byte_order,
1574 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1576 /* Set it to something shorter */
1577 _dbus_string_init_const (&t, "Hello");
1579 _dbus_marshal_set_string (&str, byte_order, 0,
1580 &t, _dbus_string_get_length (&t));
1581 _dbus_demarshal_basic_type (&str, DBUS_TYPE_STRING,
1582 &v_STRING, byte_order,
1584 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1586 /* Do the other byte order */
1587 if (byte_order == DBUS_LITTLE_ENDIAN)
1588 byte_order = DBUS_BIG_ENDIAN;
1594 _dbus_string_free (&str);
1599 #endif /* DBUS_BUILD_TESTS */