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
486 * @param str the string containing the data
487 * @param type type of value to demarshal
488 * @param value pointer to return value data
489 * @param byte_order the byte order
490 * @param pos position in the string
491 * @param new_pos pointer to update with new position, or #NULL
494 _dbus_demarshal_basic_type (const DBusString *str,
501 const char *str_data;
503 str_data = _dbus_string_get_const_data (str);
508 case DBUS_TYPE_BOOLEAN:
509 *(unsigned char *) value = _dbus_string_get_byte (str, pos);
512 case DBUS_TYPE_INT32:
513 case DBUS_TYPE_UINT32:
514 pos = _DBUS_ALIGN_VALUE (pos, 4);
515 *(dbus_uint32_t *) value = *(dbus_uint32_t *)(str_data + pos);
516 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
517 *(dbus_uint32_t *) value = DBUS_UINT32_SWAP_LE_BE (*(dbus_uint32_t *) value);
520 #ifdef DBUS_HAVE_INT64
521 case DBUS_TYPE_INT64:
522 case DBUS_TYPE_UINT64:
523 #endif /* DBUS_HAVE_INT64 */
524 case DBUS_TYPE_DOUBLE:
525 pos = _DBUS_ALIGN_VALUE (pos, 8);
526 memcpy (value, str_data + pos, 8);
527 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
528 #ifdef DBUS_HAVE_INT64
529 *(dbus_uint64_t *) value = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t *) value);
531 swap_bytes (value, 8);
535 case DBUS_TYPE_STRING:
536 case DBUS_TYPE_OBJECT_PATH:
540 len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
542 *(const char**) value = str_data + pos;
544 pos += len + 1; /* length plus nul */
547 case DBUS_TYPE_SIGNATURE:
551 len = _dbus_string_get_byte (str, pos);
554 *(const char**) value = str_data + pos;
556 pos += len + 1; /* length plus nul */
560 _dbus_warn ("type %s not a basic type\n",
561 _dbus_type_to_string (type));
562 _dbus_assert_not_reached ("not a basic type");
571 * Skips over a basic type, reporting the following position.
573 * @param str the string containing the data
574 * @param type type of value to demarshal
575 * @param byte_order the byte order
576 * @param pos pointer to position in the string,
577 * updated on return to new position
580 _dbus_marshal_skip_basic_type (const DBusString *str,
588 case DBUS_TYPE_BOOLEAN:
591 case DBUS_TYPE_INT32:
592 case DBUS_TYPE_UINT32:
593 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
596 #ifdef DBUS_HAVE_INT64
597 case DBUS_TYPE_INT64:
598 case DBUS_TYPE_UINT64:
599 #endif /* DBUS_HAVE_INT64 */
600 case DBUS_TYPE_DOUBLE:
601 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
604 case DBUS_TYPE_STRING:
605 case DBUS_TYPE_OBJECT_PATH:
609 len = _dbus_demarshal_uint32 (str, byte_order, *pos, pos);
611 *pos += len + 1; /* length plus nul */
614 case DBUS_TYPE_SIGNATURE:
618 len = _dbus_string_get_byte (str, *pos);
620 *pos += len + 2; /* length byte plus length plus nul */
624 _dbus_warn ("type %s not a basic type\n",
625 _dbus_type_to_string (type));
626 _dbus_assert_not_reached ("not a basic type");
632 * Skips an array, returning the next position.
634 * @param str the string containing the data
635 * @param byte_order the byte order
636 * @param pos pointer to position in the string,
637 * updated on return to new position
640 _dbus_marshal_skip_array (const DBusString *str,
645 dbus_uint32_t array_len;
649 i = _DBUS_ALIGN_VALUE (*pos, 4);
651 _dbus_demarshal_basic_type (str,
658 alignment = _dbus_type_get_alignment (element_type);
660 i = _DBUS_ALIGN_VALUE (i, alignment);
662 *pos = i + array_len;
666 * Return #TRUE if the typecode is a valid typecode
668 * @returns #TRUE if valid
671 _dbus_type_is_valid (int typecode)
676 case DBUS_TYPE_BOOLEAN:
677 case DBUS_TYPE_INT32:
678 case DBUS_TYPE_UINT32:
679 case DBUS_TYPE_INT64:
680 case DBUS_TYPE_UINT64:
681 case DBUS_TYPE_DOUBLE:
682 case DBUS_TYPE_STRING:
683 case DBUS_TYPE_OBJECT_PATH:
684 case DBUS_TYPE_SIGNATURE:
685 case DBUS_TYPE_ARRAY:
686 case DBUS_TYPE_STRUCT:
687 case DBUS_TYPE_VARIANT:
696 * Gets the alignment requirement for the given type;
697 * will be 1, 4, or 8.
699 * @param typecode the type
700 * @returns alignment of 1, 4, or 8
703 _dbus_type_get_alignment (int typecode)
708 case DBUS_TYPE_BOOLEAN:
709 case DBUS_TYPE_VARIANT:
710 case DBUS_TYPE_SIGNATURE:
712 case DBUS_TYPE_INT32:
713 case DBUS_TYPE_UINT32:
714 /* this stuff is 4 since it starts with a length */
715 case DBUS_TYPE_STRING:
716 case DBUS_TYPE_OBJECT_PATH:
717 case DBUS_TYPE_ARRAY:
719 case DBUS_TYPE_INT64:
720 case DBUS_TYPE_UINT64:
721 case DBUS_TYPE_DOUBLE:
722 /* struct is 8 since it could contain an 8-aligned item
723 * and it's simpler to just always align structs to 8;
724 * we want the amount of padding in a struct of a given
725 * type to be predictable, not location-dependent.
727 case DBUS_TYPE_STRUCT:
731 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
737 * If in verbose mode, print a block of binary data.
739 * @todo right now it prints even if not in verbose mode
741 * @param data the data
742 * @param len the length of the data
743 * @param offset where to start counting for byte indexes
746 _dbus_verbose_bytes (const unsigned char *data,
751 const unsigned char *aligned;
753 _dbus_assert (len >= 0);
755 /* Print blanks on first row if appropriate */
756 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
759 _dbus_assert (aligned <= data);
763 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
764 while (aligned != data)
771 /* now print the bytes */
775 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
777 _dbus_verbose ("%4d\t%p: ",
778 offset + i, &data[i]);
783 _dbus_verbose (" '%c' ", data[i]);
785 _dbus_verbose ("0x%s%x ",
786 data[i] <= 0xf ? "0" : "", data[i]);
790 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
793 _dbus_verbose ("BE: %d LE: %d",
794 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
795 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
798 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
800 _dbus_verbose (" dbl: %g",
801 *(double*)&data[i-8]);
804 _dbus_verbose ("\n");
808 _dbus_verbose ("\n");
812 * Dump the given part of the string to verbose log.
814 * @param str the string
815 * @param start the start of range to dump
816 * @param len length of range
819 _dbus_verbose_bytes_of_string (const DBusString *str,
826 real_len = _dbus_string_get_length (str);
828 _dbus_assert (start >= 0);
830 if (start > real_len)
832 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
833 start, len, real_len);
837 if ((start + len) > real_len)
839 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
840 start, len, real_len);
841 len = real_len - start;
844 d = _dbus_string_get_const_data_len (str, start, len);
846 _dbus_verbose_bytes (d, len, start);
850 marshal_4_octets (DBusString *str,
859 _dbus_assert (sizeof (value) == 4);
861 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
862 value = DBUS_UINT32_SWAP_LE_BE (value);
864 orig_len = _dbus_string_get_length (str);
866 retval = _dbus_string_insert_4_aligned (str, insert_at,
867 (const unsigned char *)&value);
871 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
872 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
879 marshal_8_octets (DBusString *str,
888 _dbus_assert (sizeof (value) == 8);
890 swap_8_octets (&value, byte_order);
892 orig_len = _dbus_string_get_length (str);
894 retval = _dbus_string_insert_8_aligned (str, insert_at,
895 (const unsigned char *)&value);
898 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
906 MARSHAL_AS_SIGNATURE,
907 MARSHAL_AS_BYTE_ARRAY
911 marshal_len_followed_by_bytes (int marshal_as,
914 const unsigned char *value,
915 int data_len, /* doesn't include nul if any */
920 DBusString value_str;
923 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
924 value_len = data_len;
926 value_len = data_len + 1; /* value has a nul */
928 /* FIXME this is probably broken for byte arrays because
929 * DBusString wants strings to be nul-terminated?
930 * Maybe I planned on this when writing init_const_len though
932 _dbus_string_init_const_len (&value_str, value, value_len);
936 if (marshal_as == MARSHAL_AS_SIGNATURE)
938 if (!_dbus_string_insert_byte (str, pos, data_len))
945 if (!marshal_4_octets (str, pos, data_len,
950 if (!_dbus_string_copy_len (&value_str, 0, value_len,
956 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
958 _dbus_verbose_bytes_of_string (str, pos, value_len);
969 /* Delete what we've inserted */
970 _dbus_string_delete (str, insert_at, pos - insert_at);
976 marshal_string (DBusString *str,
982 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
983 str, insert_at, value,
985 byte_order, pos_after);
989 marshal_signature (DBusString *str,
994 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
995 str, insert_at, value,
997 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
1002 * Marshals a basic type. The "value" pointer is always the
1003 * address of a variable containing the basic type value.
1004 * So for example for int32 it will be dbus_int32_t*, and
1005 * for string it will be const char**. This is for symmetry
1006 * with _dbus_demarshal_basic_type() and to have a simple
1009 * @param str string to marshal to
1010 * @param insert_at where to insert the value
1011 * @param type type of value
1012 * @param value pointer to a variable containing the value
1013 * @param byte_order byte order
1014 * @param pos_after #NULL or the position after the type
1015 * @returns #TRUE on success
1018 _dbus_marshal_basic_type (DBusString *str,
1027 case DBUS_TYPE_BYTE:
1028 case DBUS_TYPE_BOOLEAN:
1029 if (!_dbus_string_insert_byte (str, insert_at, *(const unsigned char *)value))
1032 *pos_after = insert_at + 1;
1035 case DBUS_TYPE_INT32:
1036 case DBUS_TYPE_UINT32:
1037 return marshal_4_octets (str, insert_at, *(const dbus_uint32_t *)value,
1038 byte_order, pos_after);
1040 #ifdef DBUS_HAVE_INT64
1041 case DBUS_TYPE_INT64:
1042 case DBUS_TYPE_UINT64:
1045 r.u = *(const dbus_uint64_t *)value;
1046 return marshal_8_octets (str, insert_at, r, byte_order, pos_after);
1049 #endif /* DBUS_HAVE_INT64 */
1050 case DBUS_TYPE_DOUBLE:
1053 r.d = *(const double *)value;
1054 return marshal_8_octets (str, insert_at, r, byte_order, pos_after);
1057 case DBUS_TYPE_STRING:
1058 case DBUS_TYPE_OBJECT_PATH:
1059 return marshal_string (str, insert_at, *(const char**) value, byte_order, pos_after);
1061 case DBUS_TYPE_SIGNATURE:
1062 return marshal_signature (str, insert_at, *(const char**) value, pos_after);
1065 _dbus_assert_not_reached ("not a basic type");
1072 marshal_1_octets_array (DBusString *str,
1074 const unsigned char *value,
1079 return marshal_len_followed_by_bytes (MARSHAL_AS_BYTE_ARRAY,
1080 str, insert_at, value, len,
1081 byte_order, pos_after);
1085 marshal_4_octets_array (DBusString *str,
1087 const dbus_uint32_t *value,
1094 _dbus_assert_not_reached ("FIXME insert_at");
1096 old_string_len = _dbus_string_get_length (str);
1098 if (!marshal_4_octets (str, insert_at, len*4, byte_order, NULL))
1101 array_start = _dbus_string_get_length (str);
1103 if (!_dbus_string_append_len (str, (const unsigned char*) value,
1107 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1109 const unsigned char *d;
1110 const unsigned char *end;
1112 d = _dbus_string_get_data (str) + array_start;
1116 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
1124 /* Restore previous length */
1125 _dbus_string_set_length (str, old_string_len);
1131 marshal_8_octets_array (DBusString *str,
1133 const DBusOctets8 *value,
1140 _dbus_assert_not_reached ("FIXME insert_at");
1142 old_string_len = _dbus_string_get_length (str);
1144 /* The array length is the length in bytes of the array,
1145 * *excluding* alignment padding.
1147 if (!marshal_4_octets (str, insert_at, len*8, byte_order, NULL))
1150 array_start = _dbus_string_get_length (str);
1152 /* Note that we do alignment padding unconditionally
1153 * even if the array is empty; this means that
1154 * padding + len is always equal to the number of bytes
1158 if (!_dbus_string_align_length (str, 8))
1161 if (!_dbus_string_append_len (str, (const unsigned char*) value,
1165 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
1167 const unsigned char *d;
1168 const unsigned char *end;
1170 d = _dbus_string_get_data (str) + array_start;
1174 #ifdef DBUS_HAVE_INT64
1175 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
1177 swap_bytes ((unsigned char*) d, 8);
1186 /* Restore previous length */
1187 _dbus_string_set_length (str, old_string_len);
1193 * Marshals a basic type array
1195 * @param str string to marshal to
1196 * @param insert_at where to insert the value
1197 * @param element_type type of array elements
1198 * @param value pointer to value
1199 * @param len length of value data in elements
1200 * @param byte_order byte order
1201 * @param pos_after #NULL or the position after the type
1202 * @returns #TRUE on success
1205 _dbus_marshal_basic_type_array (DBusString *str,
1213 /* FIXME use the insert_at arg and fill in pos_after */
1215 switch (element_type)
1217 case DBUS_TYPE_BOOLEAN:
1218 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
1219 * should we here too ? */
1220 case DBUS_TYPE_BYTE:
1221 return marshal_1_octets_array (str, insert_at, value, len, byte_order, pos_after);
1223 case DBUS_TYPE_INT32:
1224 case DBUS_TYPE_UINT32:
1225 return marshal_4_octets_array (str, insert_at, value, len, byte_order);
1227 #ifdef DBUS_HAVE_INT64
1228 case DBUS_TYPE_INT64:
1229 case DBUS_TYPE_UINT64:
1230 #endif /* DBUS_HAVE_INT64 */
1231 case DBUS_TYPE_DOUBLE:
1232 return marshal_8_octets_array (str, insert_at, value, len, byte_order);
1235 case DBUS_TYPE_STRING:
1236 case DBUS_TYPE_OBJECT_PATH:
1237 _dbus_assert_not_reached ("handle string arrays");
1240 case DBUS_TYPE_SIGNATURE:
1241 _dbus_assert_not_reached ("handle signature");
1245 _dbus_assert_not_reached ("non basic type in array");
1254 #ifdef DBUS_BUILD_TESTS
1255 #include "dbus-test.h"
1258 #define MARSHAL_BASIC(typename, byte_order, literal) \
1260 v_##typename = literal; \
1261 if (!_dbus_marshal_basic_type (&str, pos, DBUS_TYPE_##typename, \
1263 byte_order, NULL)) \
1264 _dbus_assert_not_reached ("no memory"); \
1267 #define DEMARSHAL_BASIC(typename, byte_order) \
1269 _dbus_demarshal_basic_type (&str, DBUS_TYPE_##typename, &v_##typename, \
1270 byte_order, pos, &pos); \
1273 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1275 DEMARSHAL_BASIC (typename, byte_order); \
1276 if (literal != v_##typename) \
1278 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1279 _dbus_string_get_length (&str) - dump_pos); \
1280 _dbus_assert_not_reached ("demarshaled wrong value"); \
1284 #define MARSHAL_TEST(typename, byte_order, literal) \
1286 MARSHAL_BASIC (typename, byte_order, literal); \
1288 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1291 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1293 MARSHAL_BASIC (typename, byte_order, literal); \
1295 DEMARSHAL_BASIC (typename, byte_order); \
1296 if (strcmp (literal, v_##typename) != 0) \
1298 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1299 _dbus_string_get_length (&str) - dump_pos); \
1300 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1301 _dbus_assert_not_reached ("demarshaled wrong value"); \
1306 _dbus_marshal_test (void)
1311 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
1312 #ifdef DBUS_HAVE_INT64
1313 dbus_int64_t array3[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1314 DBUS_INT64_CONSTANT (0x456ffffffff),
1315 DBUS_INT64_CONSTANT (0x789ffffffff) }, *array4;
1321 dbus_int32_t v_INT32;
1322 dbus_uint32_t v_UINT32;
1323 dbus_int64_t v_INT64;
1324 dbus_uint64_t v_UINT64;
1325 unsigned char v_BYTE;
1326 unsigned char v_BOOLEAN;
1327 const char *v_STRING;
1328 const char *v_SIGNATURE;
1329 const char *v_OBJECT_PATH;
1332 if (!_dbus_string_init (&str))
1333 _dbus_assert_not_reached ("failed to init string");
1337 /* Marshal doubles */
1338 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1339 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1341 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1342 _dbus_assert_not_reached ("got wrong double value");
1344 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1345 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1347 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1348 _dbus_assert_not_reached ("got wrong double value");
1350 /* Marshal signed integers */
1351 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1352 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1354 /* Marshal unsigned integers */
1355 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1356 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1358 #ifdef DBUS_HAVE_INT64
1359 /* Marshal signed integers */
1360 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1361 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1363 /* Marshal unsigned integers */
1364 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1365 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1366 #endif /* DBUS_HAVE_INT64 */
1369 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1370 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1372 /* Marshal all possible bools! */
1373 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1374 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1375 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1376 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1378 /* Marshal strings */
1379 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1380 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1381 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1382 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1385 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1386 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1389 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1390 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1391 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1392 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1394 /* set/pack 64-bit integers */
1395 _dbus_string_set_length (&str, 8);
1398 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1399 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1401 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1402 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1403 _dbus_string_get_const_data (&str)));
1406 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1407 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1409 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1410 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1411 _dbus_string_get_const_data (&str)));
1413 /* signed little pack */
1414 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1416 _dbus_string_get_data (&str));
1418 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1419 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1420 _dbus_string_get_const_data (&str)));
1422 /* signed big pack */
1423 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1425 _dbus_string_get_data (&str));
1427 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1428 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1429 _dbus_string_get_const_data (&str)));
1431 /* unsigned little */
1432 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1433 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1435 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1436 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1437 _dbus_string_get_const_data (&str)));
1440 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1441 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1443 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1444 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1445 _dbus_string_get_const_data (&str)));
1447 /* unsigned little pack */
1448 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1450 _dbus_string_get_data (&str));
1452 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1453 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1454 _dbus_string_get_const_data (&str)));
1456 /* unsigned big pack */
1457 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1459 _dbus_string_get_data (&str));
1461 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1462 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1463 _dbus_string_get_const_data (&str)));
1465 /* set/pack 32-bit integers */
1466 _dbus_string_set_length (&str, 4);
1469 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1472 _dbus_assert (-0x123456 ==
1473 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1474 _dbus_string_get_const_data (&str)));
1477 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1480 _dbus_assert (-0x123456 ==
1481 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1482 _dbus_string_get_const_data (&str)));
1484 /* signed little pack */
1485 _dbus_pack_int32 (-0x123456,
1487 _dbus_string_get_data (&str));
1489 _dbus_assert (-0x123456 ==
1490 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1491 _dbus_string_get_const_data (&str)));
1493 /* signed big pack */
1494 _dbus_pack_int32 (-0x123456,
1496 _dbus_string_get_data (&str));
1498 _dbus_assert (-0x123456 ==
1499 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1500 _dbus_string_get_const_data (&str)));
1502 /* unsigned little */
1503 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
1506 _dbus_assert (0x123456 ==
1507 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1508 _dbus_string_get_const_data (&str)));
1511 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
1514 _dbus_assert (0x123456 ==
1515 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1516 _dbus_string_get_const_data (&str)));
1518 /* unsigned little pack */
1519 _dbus_pack_uint32 (0x123456,
1521 _dbus_string_get_data (&str));
1523 _dbus_assert (0x123456 ==
1524 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1525 _dbus_string_get_const_data (&str)));
1527 /* unsigned big pack */
1528 _dbus_pack_uint32 (0x123456,
1530 _dbus_string_get_data (&str));
1532 _dbus_assert (0x123456 ==
1533 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1534 _dbus_string_get_const_data (&str)));
1536 /* Strings in-place set */
1537 byte_order = DBUS_LITTLE_ENDIAN;
1541 _dbus_string_set_length (&str, 0);
1543 /* reset pos for the macros */
1546 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1548 /* Set it to something longer */
1549 _dbus_string_init_const (&t, "Hello world foo");
1551 _dbus_marshal_set_string (&str, byte_order, 0,
1552 &t, _dbus_string_get_length (&t));
1554 _dbus_demarshal_basic_type (&str, DBUS_TYPE_STRING,
1555 &v_STRING, byte_order,
1557 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1559 /* Set it to something shorter */
1560 _dbus_string_init_const (&t, "Hello");
1562 _dbus_marshal_set_string (&str, byte_order, 0,
1563 &t, _dbus_string_get_length (&t));
1564 _dbus_demarshal_basic_type (&str, DBUS_TYPE_STRING,
1565 &v_STRING, byte_order,
1567 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1569 /* Do the other byte order */
1570 if (byte_order == DBUS_LITTLE_ENDIAN)
1571 byte_order = DBUS_BIG_ENDIAN;
1577 _dbus_string_free (&str);
1582 #endif /* DBUS_BUILD_TESTS */